Hm, maybe there are different opinions about readable code, I love some empty lines within procedures, but don't like them seperating procedures, because a typical program will need a lot of scrolling even when all procedures are collapsed.
Therefore I personally like to have all comments for a procedure right/below the the first procedure line, so it will also be collapsable.
However, here's a fast possibility to get rid of empty lines - it's a quick and dirty code for now, but if it is useful, I could optimize it for recognizing "Procedure " and "Procedure." but not lines like "ProcedureVar=1". The same should work for "Macros" also...
But one after the other, here's the simple code which should be compiled as "Auto Compactor.exe" and copied into the "Catalog" directory of PureBasic. After that, just add is as a tool, using [%COMPILEFILE\..\Catalogs\Tool AutoCompactor.exe] for the command line, ["%TEMPFILE" %SELECTION] as arguments and [%COMPILEFILE\..] as the working directory (don't add the brackets).
Just activate also the options "wait until tool quits" and "reload source after tool has quit", that's it.
Code: Select all
;{ AutoCompactor by Michael Vogel Vo.o1}
; INSTALLATION:
; copy compiled exe into the directory "...\Purbasic\Catalogs"
; install the tool using the parameter "%TEMPFILE" %SELECTION
; use the PureBasic as the working directory (or adapt the OpenPreferences path below)
; Select 'Wait until tool quits' And 'Reload source after tool has quit'
; %COMPILEFILE\..\Catalogs\Tool AutoIndent.exe
; "%TEMPFILE" %SELECTION
; %COMPILEFILE\..
; Auto &Indent
; Menu Or Shortcut
; × Wait until tool quits
; × Reload Source after tool has quit
; × into the current source
; DESCRIPTION:
; With pressed shift key: remove all empty lines
; without shift key: just remove lines between endprocedure/procedure
; no selection: complete source code is affected
; selection of lines: only selected lines are changed
; HISTORY & ISSUES:
; first beta
;}
; Define
; EndDefine
Procedure.s RightTrim(s.s)
l.l=Len(s)
While l
l-1
b.l=PeekB(@s+l)
If b<>9 And b<>32
Break
EndIf
Wend
ProcedureReturn Left(s,l+1)
EndProcedure
Procedure.s LeftTrim(s.s)
l.l=Len(s)
c.l=0
While c<l
b.l=PeekB(@s+c)
If b<>9 And b<>32 ; Space & Tab
Break
EndIf
c+1
Wend
ProcedureReturn Mid(s,c+1,l-c)
EndProcedure
Procedure Init()
; Dateinamen holen...
CompilerIf 1
If CountProgramParameters()<>2
MessageBox_(0,"'AutoCompactor' benötigt Parameter!"+#CR$+"(%TEMPFILE und %SELECTION)","Fehler",#MB_ICONERROR| #MB_OK)
End
EndIf
Global OutputFile.s=ProgramParameter()
CompilerElse
Global OutputFile.s="Test.pb"
CompilerEndIf
Global InputFile.s=OutputFile+"~"
DeleteFile(InputFile)
If RenameFile(OutputFile,InputFile)=0
MessageBox_(0,"'AutoCompactor' konnte keine Temporärdatei erstellen","Fehler",#MB_ICONERROR| #MB_OK)
End
EndIf
; Selektion abfragen...
Global zeile.l=0
selektion.s=ProgramParameter()
Global Startzeile.l=Val(StringField(selektion, 1, "x"))
Global Endzeile.l=Val(StringField(selektion, 3, "x"))
;CreateFile(3,"c:\debug.log")
;WriteStringN(3,Str(Startzeile))
;WriteStringN(3,Str(Endzeile))
;CloseFile(3)
If Startzeile>=Endzeile
Startzeile=1
Endzeile=#MAXSHORT
EndIf
EndProcedure
Procedure Doit(mode.l)
; Doit(0) standard compacting
; Doit(1) complete compacting
;CreateFile(3,"c:\debug.log")
;WriteStringN(3,"s "+FormatDate("%hh:%ii:%ss", Date())+#TAB$+Str(GetTickCount_()))
Init()
ReadFile(1,InputFile)
CreateFile(2,OutputFile)
Protected RemoveFlag=mode
Protected Content.s
While Not(Eof(1))
z.s=ReadString(1)
zeile.l+1
z=righttrim(z) ; rechts kann man immer säubern
If zeile>=Startzeile And zeile<=Endzeile
Content=LCase(lefttrim(z))
If mode
RemoveFlag=#True
ElseIf Left(Content,9)="procedure"
RemoveFlag=#False
ElseIf Left(Content,12)="endprocedure"
RemoveFlag=#True
EndIf
If (RemoveFlag=#False) Or Len(Content)
WriteStringN(2,z)
EndIf
Else
WriteStringN(2,z)
EndIf ;
Wend
CloseFile(1)
CloseFile(2)
;WriteStringN(3,"e "+FormatDate("%hh:%ii:%ss", Date())+#TAB$+Str(GetTickCount_()))
;CloseFile(3)
EndProcedure
Doit((GetKeyState_(#VK_SHIFT)&$80))