Option to remove all trailing spaces when a file is saved

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Option to remove all trailing spaces when a file is saved

Post by Trond »

Wish: Option to remove all trailing spaces when a file is saved

Edit: I mean on each line.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Yes. Seconded.
Dare2 cut down to size
Dummy
Enthusiast
Enthusiast
Posts: 162
Joined: Wed Jun 09, 2004 11:10 am
Location: Germany
Contact:

Post by Dummy »

You mean for PB IDE, right?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Yes.
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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]
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

How did you compile that?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post by Little John »

+1
Post Reply