adds/removes tabs to indent the active source code into a structured form.
Code: Select all
;{ AutoIndent by Michael Vogel V2.o2}
; 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: eliminates tabs and spaces
; without shift key: indents source code
; no selection: complete source code is affected
; selection of lines: only selected lines are changed
; HISTORY & ISSUES:
; known problems: when cursor is in line 1, the Procedure folds up (editor bug?)
; ":" in remark, like in "Until x>1; x : 0...5"
; solved: "Else" was seen twice: because of "Else" and "ElseIf"
;}
; Define
Global LeftC.l
Global LeftD.l
Global RightC.l
Global RightD.l
Global RightP.l
#Words=51
Global Dim Word.s(#Words)
Global Dim Exception.s(4)
Global exceptions.l=0
Global Dim Border(3)
z.l=0
i.l=0
While i<#Words
i+1
Read.s Word(i)
If Asc(word(i))=254 ; "þ"
exceptions+1
exception(exceptions)=Mid(word(i),2,Len(word(i))-1)
EndIf
If Word(i)="!"
i-1
Border(z)=i
;Debug Str(z)+": "+Str(i-1)+" = "+word(border(z))
z+1
EndIf
Wend
DataSection
Data.s "þ Define"
Data.s "þ{"
Data.s "Procedure "
Data.s "Procedure."
Data.s "ProcedureC"
Data.s "ProcedureDLL"
Data.s "DataSection"
Data.s "Macro "
Data.s "If "
Data.s "With "
Data.s "Select "
Data.s "While "
Data.s "For "
Data.s "ForEach "
Data.s "Repeat"
Data.s "Structure "
Data.s "StructureUnion"
Data.s "Interface "
Data.s "Enumeration"
Data.s "CompilerIf "
Data.s "CompilerSelect "
Data.s "ImportC "
Data.s "Import "
Data.s "!" ; border(0)
; kurzzeitig ''ausrücken''
Data.s "Case "
Data.s "Else:"
Data.s "Else "
Data.s "ElseIf "
Data.s "Default "
Data.s "Default:"
Data.s "CompilerElse"
Data.s "CompilerCase"
Data.s "CompilerDefault"
Data.s "!" ; border(1)
Data.s "þ EndDefine"
Data.s "þ}"
Data.s "EndProcedure"
Data.s "EndDataSection"
Data.s "EndMacro"
Data.s "EndIf"
Data.s "EndWith"
Data.s "EndSelect"
Data.s "Wend"
Data.s "ForEver"
Data.s "EndStructure"
Data.s "EndStructureUnion"
Data.s "EndInterface"
Data.s "EndEnumeration"
;Data.s "CompilerEndIf"
;Data.s "CompilerEndSelect"
Data.s "EndImport"
Data.s "!" ; border(2)
Data.s "Next "
Data.s "Next:"
Data.s "Until "
Data.s "!","" ; border(3)
EndDataSection
; 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 FindCommas(s.s)
LeftC=0
LeftD=0
RightC=0
RightD=0
RightP=0
l.l=Len(s)
c.l=0
While c<l
b.l=PeekB(@s+c)
c+1
Select b
Case 59 ; Semicolon
RightC=c
If LeftC=0 : LeftC=c : EndIf
Case 34 ; DoubleQuote
RightD=c
If LeftD=0 : LeftD=c : EndIf
Case 58 ; Colon
RightP=c
Case 9,32
If c=RightP+1 : RightP+1 : EndIf
EndSelect
Wend
EndProcedure
Procedure.s RightCut(s.s,x.l)
ProcedureReturn RTrim(Left(s,x-1))
EndProcedure
Procedure.s StringS(n, chars.s)
Protected s.s=""
While n>0
s+chars
n-1
Wend
ProcedureReturn s
EndProcedure
Procedure Init()
; Dateinamen holen...
CompilerIf 1
If CountProgramParameters()<>2
MessageBox_(0,"'AutoIndent' 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,"'AutoIndent' 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
OpenPreferences(GetPathPart(ProgramFilename())+"..\PureBasic.prefs"); ok
PreferenceGroup("Global")
If Val(ReadPreferenceString("RealTab","0")) Or (GetKeyState_(#VK_CONTROL)&$80)
Global indtxt.s=#TAB$
Else
Global indtxt.s=Space(Val(ReadPreferenceString("TabLength", "2")))
EndIf
ClosePreferences()
EndProcedure
Procedure Doit(mode.l)
; Doit(0) auto indent the source code
; Doit(1) trims source code from space And tab characters
;CreateFile(3,"c:\debug.log")
;WriteStringN(3,"s "+FormatDate("%hh:%ii:%ss", Date())+#TAB$+Str(GetTickCount_()))
init()
ReadFile(1,InputFile)
CreateFile(2,OutputFile)
indent.l=0
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
z=lefttrim(z) ; links nur, wenn gewünscht...
afterburner.l=0
If (mode=0) And (Len(z)>0)
If Asc(z)=';'; ";"
If Left(z,25)="; IDE Options = PureBasic"
indent=0
Else
For i=1 To exceptions
If Mid(z,2,Len(exception(i)))=exception(i)
PokeB(@z,254) ; "þ"
Break
EndIf
Next i
EndIf
EndIf
s.s=z
FindCommas(s)
; [1] If a=0 ; remark
; [2] If a=''x'' ; remark
; [3] If a=0 ; ''remark''
; [x] If a=''x'' ; ''remark''
If LeftC
If LeftD=0 Or LeftC<LeftD ; [1], [3]
s=RightCut(s,LeftC)
ElseIf RightC>RightD ; [2]
s=RightCut(s,RightC)
EndIf
EndIf
For i=1 To #Words
l.l=Len(word(i))
Select i
Case 1 To border(1)
p.l=1
Case 1 To border(2)
p.l=Len(s)-l+1
Case 1 To border(3)
p.l=RightP+1
EndSelect
If Mid(s+":",p,l)=word(i)
Select i
Case 1 To border(0)
afterburner.l+1
Case 1 To border(1)
indent-1
afterburner.l+1
Case 1 To border(3)
If afterburner
afterburner-1
Else
indent-1
EndIf
EndSelect
EndIf
Next i
If Asc(z)=254 ; "þ"
PokeB(@z,';') ; ";"
EndIf
z=strings(indent,indtxt)+z
;WriteStringN(3,s)
EndIf ; mode
EndIf ; Zeilenselektion
WriteStringN(2,z)
indent+afterburner
Wend
CloseFile(1)
CloseFile(2)
;WriteStringN(3,"e "+FormatDate("%hh:%ii:%ss", Date())+#TAB$+Str(GetTickCount_()))
;CloseFile(3)
EndProcedure
Doit((GetKeyState_(#VK_SHIFT)&$80))