;
; ------------------------------------------------------------
;
; Source Formatting By Berikco
;
; (c) 2002 - Benny Sels
;
; ------------------------------------------------------------
;
; For Purebasic Toolmenu version 3.40 and up
; Will format the source with TABS(Spaces)
; Thanks to PB for his help on the ';' comment finder
;
; --- Toolbox settings ---------------------------------------
;
; Arguments:
; First argument "%FILE"
; Second argument #Number of spaces for 1 Tab
; Example: "%FILE" 2
;
; Wait Until tool Quits = On
; Reload source after program is finished = On
;
; ------------------------------------------------------------
;
; 2002/10/09
; First version
;
; Note: When writing PureBasic *.pb sourcefile, don't add CRLF after last line!
; This will produce strange results at end of code :)
; So dont use WriteStringN() for last line
;
; Note: This source is formated by itself :)
;
DestFileName$ = ProgramParameter() ; Sourcefile to read
NumSpaces = Val(ProgramParameter()) ; Number of spaces
SourceFileName$ = DestFileName$+"2" ; The backup file *.pb2
#Source=1
#Destination=2
If FileSize(DestFileName$) ; Do nothing if source empty
If FileSize(SourceFileName$) ; If old backupFile exist, delete it
DeleteFile(SourceFileName$)
EndIf
If RenameFile(DestFileName$, SourceFileName$) ; Rename source to backup
If OpenFile(#Source,SourceFilename$) ; Open Source, now with *.pb2 extension
If CreateFile(#Destination,DestFileName$) ; Create new source
While Eof(#source)=false
UseFile(#Source)
a$=ReadString()
If Eof(#source)=false ; Check if read is last line in source
; If this is the case, must not write CRLF
a$=LTrim(a$) ;Remove old TABs (spaces in front)
a$=RTrim(a$) ;Remove all spaces at end
Tab+NextTab
NextTab=0
in=FindString(a$," ",1) ; Find PureBasic Keyword
If in>0
Vgl$=LCase(Left(a$,in-1)) ; Get only Keyword
Else
Vgl$=LCase(a$) ; Nothing after Keyword, use whole string
EndIf
; Check For Keyword that needs formatting
If Vgl$="procedure" Or Vgl$="procedure.b" Or Vgl$="procedure.w" Or Vgl$="procedure.l" Or Vgl$="procedure.s"
Tab=0
NextTab=1
ElseIf Vgl$="endprocedure"
Tab=0
ElseIf Vgl$="if" Or Vgl$="select" Or Vgl$="for" Or Vgl$="repeat" Or Vgl$="while"
f=0
b$=a$+";" ;little trick, add comment to line, only one routine needed to scan
For r=1 To Len(b$)
a=Asc(Mid(b$,r,1))
If a=34 : q=1-q : EndIf
If a=59
If q=0
code$=RTrim(Left(b$,r-1))
k=1
Repeat
k$= StringField(code$, k, " ")
k+1
If k$>""
KeyWord$=LCase(k$)
EndIf
Until k$=""
If keyword$="endif" Or keyword$="next" Or keyword$="wend"
NextTab=0
Else
NextTab=1
EndIf
EndIf
EndIf
Next
ElseIf Vgl$="endif" Or Vgl$="endstructure" Or Vgl$="next" Or Vgl$="until" Or Vgl$="endselect" Or Vgl$="wend"
Tab-1
ElseIf Vgl$="elseif" Or Vgl$="else" Or Vgl$="case"
Tab-1
NextTab=1
EndIf
Front$=Space(Tab * NumSpaces) ; Create Spaces needed
a$ = Front$ + a$ ; Put spaces before the line code
UseFile(#Destination)
WriteStringN(a$) ; Write in new source
Else
UseFile(#Destination)
WriteString(a$)
EndIf
Wend
CloseFile(#source)
CloseFile(#destination)
EndIf
EndIf
EndIf
EndIf
End
Restored from previous forum. Originally posted by Berikco.
Originally posted by Danilo
And it can be very useful when i paste unformated
source from the forum in the editor.
1 click and its perfectly readable.. !!