Option to remove all trailing spaces when a file is saved
Posted: Sun Jun 11, 2006 8:52 am
Wish: Option to remove all trailing spaces when a file is saved
Edit: I mean on each line.
Edit: I mean on each line.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
;{ AutoIndent by Michael Vogel }
; 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
; known problems: when cursor is in line 1, the Procedure folds up (editor bug?)
; solved: "Else" was seen twice: because of "Else" and "ElseIf"
;}
; Define
Global indtxt.s=Chr(9); " "
Global LeftC.w
Global LeftD.w
Global RightC.w
Global RightD.w
Global RightP.w
#Words=46; muss stets "händisch" angepaßt werden
Global Dim Word.s(#Words)
Global Dim Exception.s(4)
Global exceptions.w=0
Global Dim Border(3)
z.w=0
i.w=0
While i<#Words
i+1
Read 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 "Interface "
Data.s "Enumeration"
Data.s "CompilerIf "
Data.s "CompilerSelect "
Data.s "Import"
Data.s "!" ; border(0)
; kurzzeitig ''ausrücken''
Data.s "Case "
Data.s "Else:" ; 2 Formen, sonst kommen wir uns mit ElseIf ins Gehege!
Data.s "Else " ; 2 Formen, sonst kommen wir uns mit ElseIf ins Gehege!
Data.s "ElseIf "
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 "EndInterface"
Data.s "EndEnumeration"
;Data.s "CompilerEndIf" ; wird schon bei 'EndIf' entdeckt
;Data.s "CompilerEndSelect" ; analog oben
Data.s "EndImport"
Data.s "!" ; border(2)
Data.s "Next" ; blöd, da kann nach 'Until' noch Text kommen...
Data.s "Until " ; detto
Data.s "!","" ; border(3)
EndDataSection
; EndDefine
Procedure.s RightTrim(s.s)
l.w=Len(s)
While l
l-1
b.w=PeekB(@s+l)
If b<>9 And b<>32
Break
EndIf
Wend
ProcedureReturn Left(s,l+1)
EndProcedure
Procedure.s LeftTrim(s.s)
l.w=Len(s)
c.w=0
While c<l
b.w=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.w=Len(s)
c.w=0
While c<l>0
s+chars
n-1
Wend
ProcedureReturn s
EndProcedure
;{ AutoIndent Installation }
; > 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 aftzer tool has quit'
;}
Procedure Init()
; Dateinamen holen...
CompilerIf 1
If CountProgramParameters()<2>=Endzeile
Startzeile=1
Endzeile=#MAXSHORT
EndIf
; Tabulator-Einstellungen holen...
OpenPreferences("PureBasic.prefs")
PreferenceGroup("Global")
If Val(ReadPreferenceString("RealTab","0"))
indtxt.s=Chr(9)
Else
indtxt.s=Space(Val(ReadPreferenceString("TabLength", "2")))
EndIf
ClosePreferences()
EndProcedure
Procedure Doit(mode.w)
; 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.w=0
While Not(Eof(1))
z.s=ReadString(1)
zeile.w+1
z=righttrim(z) ; rechts kann man immer säubern
If zeile>=Startzeile And zeile<Endzeile>0)
If Asc(z)=59 ; ";"
For i=1 To exceptions
If Mid(z,2,Len(exception(i)))=exception(i)
PokeB(@z,254) ; "þ"
Break
EndIf
Next i
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>RightD ; [2]
s=RightCut(s,RightC)
EndIf
EndIf
For i=1 To #Words
l.w=Len(word(i))
Select i
Case 1 To border(1)
p.w=1
Case 1 To border(2)
p.w=Len(s)-l+1
Case 1 To border(3)
p.w=RightP+1
EndSelect
If Mid(s+":",p,l)=word(i)
Select i
Case 1 To border(0)
afterburner.w+1
Case 1 To border(1)
indent-1
afterburner.w+1
Case 1 To border(3)
If afterburner
afterburner-1
Else
indent-1
EndIf
EndSelect
EndIf
Next i
If Asc(z)=254 ; "þ"
PokeB(@z,59) ; ";"
EndIf
z=strings(indent,indtxt)+z
;WriteStringN(2,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) & 128)
codecaddy will reformat and strip trailing spaces...Michael Vogel wrote:I am using a program which is started from the Tool menu (and it automatically refresh all intends on the fly)
The only problem is, that in long source files I have to move my cursor to the end of the file before starting the tool, otherwise the IDE takes a minute or two (![]()
) until it will be ready for work again...