Page 1 of 1

Option to remove all trailing spaces when a file is saved

Posted: Sun Jun 11, 2006 8:52 am
by Trond
Wish: Option to remove all trailing spaces when a file is saved

Edit: I mean on each line.

Posted: Sun Jun 11, 2006 12:39 pm
by Dare
Yes. Seconded.

Posted: Sun Jun 11, 2006 1:17 pm
by Dummy
You mean for PB IDE, right?

Posted: Sun Jun 11, 2006 1:39 pm
by Trond
Yes.

Posted: Mon Jun 12, 2006 10:42 am
by Michael Vogel
I am using a program which is started from the Tool menu (and it automatically refresh all intends on the fly :idea: )

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...



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)
[/code]

Posted: Mon Jun 12, 2006 4:30 pm
by Trond
How did you compile that?

Posted: Tue Jun 13, 2006 6:02 pm
by blueznl
Michael Vogel wrote:I am using a program which is started from the Tool menu (and it automatically refresh all intends on the fly :idea: )

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...
codecaddy will reformat and strip trailing spaces...

are you sure just moving the cursor is the solution? i don't think so, it looks like adding some data at the start of the file does cause the delays... are you sure?

Re: Option to remove all trailing spaces when a file is save

Posted: Sun Dec 08, 2013 11:43 pm
by luis
Just noticed for the 10th-million time and so searched to see if this was already requested.

And .... yes it was. I second this request, I don't see why we need to waste space on disk even if space is cheap :wink:

But it's more than that: editing the sources, every time you want to add something at the end of a line, you end up in some stupid point in the middle of the screen because there are tons of spaces for some reason on some lines. Then select, move the cursor, delete, and finally you can type.

Re: Option to remove all trailing spaces when a file is save

Posted: Mon Dec 09, 2013 1:53 am
by IdeasVacuum
+1
It's something that UltaEdit does (as an option), but also they can be trimmed at any time via a toolbar button.

Re: Option to remove all trailing spaces when a file is save

Posted: Mon Dec 09, 2013 1:00 pm
by Little John
+1