Page 1 of 1

[PB4]Add-in Tool to Strip Trailing Spaces

Posted: Wed Mar 01, 2006 1:33 pm
by akj
Code updated For 5.20+

This is a fairly simple example of "Tricks 'n' Tricks" but it may be useful to a newcomer to PureBasic in showing how easy it is to enhance the capabilities of the IDE (the PB editing environment) by writing Add-in Tools.

When source code is copied-and-pasted into PB from the Forums each line of the code tends to have one or more unwanted trailing spaces. Although this causes no problems, it does make the source file larger than necessary and for some reason I find this annoying. So I decided to write a simple program and make it readily accessible from the PB editor. If you wish to do the same, read on ...

To create this IDE add-on tool:

1. Compile the PB4 source code at the end of this post and create a .exe for it.

2. In the PB editor (IDE) take the menu path: Tools -> Configure Tools -> New

3. In the "Edit Tool Settings" dialogue box, leave all the text boxes blank, except for the following:
* Command line = Full pathame to the .exe file produced in step 1.
You should enclose the pathname in double quotes, especially if it contains any spaces.
* Arguments = "%TEMPFILE"
Include the double quotes. Try clicking the Info button.
* Name = Strip Trailing Spaces
Or any other name you prefer.
*Trigger = Menu or Shortcut
Other triggers could be chosen, but this one will do.

4. Still in the "Edit Tool Settings" dialogue box, leave all the tick boxes and radio buttons blank, except for the following:
* Wait until tool quits
* Run hidden
* Reload source after tool has quit
* Into current source

5. OK OK
Done! The tool has now been written and made available to the IDE.


To run the tool (remember the trigger was set to Menu or Shortcut), do the following:

6. Load into the PB editor the .pb or .pbi file whose source lines many include unwanted trailing spaces. Ensure this is the currently selected source code.

7. Take the menu path: Tools -> Strip Trailing Spaces
(Or whatever you called it in the 'Name' text box at step 3.)
This will now remove unwanted trailing characters from the selected source code. If you are happy with the result, save the code in the usual way.

Code: Select all

; Strip Trailing Spaces  AKJ  01-Mar-06
; Strips unwanted trailing characters from a text file
; The file's pathname must be given as the first command parameter
; The unwanted characters are:  tab  space  colon  semicolon
; If the stripped line is null, the first character will be reinstated if
;   it is either a colon or semicolon
; The program has no user interface and generates no error messages

EnableExplicit
Define path$, file, ln$, p, stripped=0

; Get text file's pathname
path$ = ProgramParameter()
If path$ = "": End: EndIf ; Quit without error message

; Scan and strip each line of the file, saving them to a linked list
file=ReadFile(#PB_Any, path$)
If file=0: End: EndIf ; Quit without error message
NewList lines$() ; To save the stripped prior to writing back the file
While Not Eof(file)
  ln$=ReadString(file)
  ; Set p to the position of the right-most wanted character
  For p=Len(ln$) To 1 Step -1
    If FindString(#TAB$+" :;", Mid(ln$,p,1), 1)=0: Break: EndIf
  Next p
  If p=0 And FindString(":;", Left(ln$,1), 1)<>0: p=1: EndIf; Reinstate first char?
  stripped+Len(ln$)-p ; Number of characters to be stripped
  ; Strip unwanted characters from the line and save the line
  AddElement(lines$()): lines$()=Left(ln$,p)
Wend
CloseFile(file)

; Update the file with the stripped lines
If stripped=0: End: EndIf ; Don't update if nothing has changed
file=CreateFile(#PB_Any, path$)
If file=0: End: EndIf ; Quit without error message
ForEach lines$()
  WriteStringN(file, lines$())
Next lines$()
CloseFile(file)

End