'Copyright 2011, Clyde Wilson'Version 1.2'Please run this code and change it if necessary.'Let me know of any requests or changes and I'll'incorporate them in the next version.'Formatter runs in Liberty Basic or Just Basic'I like to use the formatter when tokenized and executed from the run menu!'In the Setup menu, choose Preferences and check Reload File On Activate'to get the quickest feedback!'The above is not necessary, just more fun...'Be sure to save your code before running formatter'otherwise you will lose any unsaved changes.'Formatter indents code blocks and continuation lines and inserts blank lines'around subs and functions. Blank lines inserted by the user are maintained.nomainwindim strtBlock$(100)dim endBlock$(100)dim alignLeft$(100)dim info$(10,10)'for file check
tabSize =4
insLines =1'For Debug, set to 1 if blank lines are to be inserted
remExcess =0'For Debug, set to 1 if excess blank lines are to be removed
blankLine=1onerrorgoto[error]gosub[fillArrays]gosub[openFiles][loop]doifeof(#text)<>0then[quit]lineinput#text, item$
wrkLine$ =trim$(item$)if remExcess =0thenexitdoloopwhilelen(wrkLine$)=0' or (wrkLine$="'") 'to remove single quote lines.
firstWord$ =lower$(word$( wrkLine$,1))gosub[chkExepts]gosub[chkKeys]gosub[wrtLine]goto[loop][quit]close#text
close#newText
[contErr]kill fName$ +" - bkp.bas"name fName$ +".bas"as fName$ +" - bkp.bas"name"formatterTempFile"as fName$ +".bas"notice"Finished"end[error]ifErr=0thenopen fName$ +" - bkp.bas"foroutputas#temp
close#temp
goto[contErr]else
noteStg$ ="You have received error #"; Errnotice"Error"+chr$(13)+ noteStg$ +chr$(13)+Err$+chr$(13)+ fName$
close#newText
kill"formatterTempFile"endendif[fillArrays]
counter =1dountil choice$ ="end"read choice$
if choice$ <>"end"then
strtBlock$(counter)= choice$
aryCtr = aryCtr +1
counter = counter +1endifloop
counter =1
choice$ =""dountil choice$ ="end"read choice$
if choice$ <>"end"then
endBlock$(counter)= choice$
ary2Ctr = ary2Ctr +1
counter = counter +1endifloop
counter =1
choice$ =""dountil choice$ ="end"read choice$
if choice$ <>"end"then
alignLeft$(counter)= choice$
ary3Ctr = ary3Ctr +1
counter = counter +1endifloopreturn[openFiles]confirm"Have you saved your current work?" ; ans$
if ans$="no"thennotice"Please save your current work before proceding"endendiffilesDefaultDir$,"formatterTempFile", info$()Ifval(info$(0,0))>0then'the file existsnotice"Please rename or delete formatterTempFile"endelseopen"formatterTempFile"foroutputas#newText
endifprompt"Please type a filename (without the extension)"; fName$
if fName$=""thennotice"Program Not Run - Please enter a filename"close#newText
kill"formatterTempFile"endendififlower$(fName$)="untitled"thennotice"Program must be saved under another name before continuing"close#newText
kill"formatterTempFile"endendifopen fName$ +".bas"forinputas#text
notice"Your original file will be found at ";fName$ +" - bkp.bas"
numTabs =1return[chkExepts]'Check exceptional cases
indentOk =1
somethingHere =0
wrkLineL$ =lower$( wrkLine$ )
secondWord$ =word$( wrkLineL$,2)if firstWord$ ="end"then firstWord$ = firstWord$ +" "+ secondWord$
if firstWord$ ="if"then
thenPos =instr( wrkLineL$," then ")if thenPos <>0then
thenPos = thenPos +6for counter = thenPos tolen( wrkLineL$ )ifmid$( wrkLineL$, counter,1)<>" "then
somethingHere =1exitforendifnextendifif somethingHere thenifmid$(wrkLineL$, counter,1)="'"then
indentOk =1else
indentOk =0endifendifendifreturn[chkKeys]
indent =0
outdent =0
saveTabs =0for counter =1to aryCtr
if strtBlock$( counter )= firstWord$ then
indent =1endifnextfor counter =1to ary2Ctr
if endBlock$( counter )= firstWord$ then outdent =1nextfor counter =1to ary3Ctr
if alignLeft$(counter)= firstWord$ thenif numTabs >1then saveTabs = numTabs -1
numTabs =0
indent =1endifnextif insLines and needBlanks and blankLine=0theniflen( wrkLine$)<>0thenprint#newText,""
needBlanks=0
blankLine=1endif
needBlanks=0ifinstr( firstWord$,"[")=1thenif numTabs >1then saveTabs = numTabs -1
numTabs =0
indent =1if insLines and blankLine=0thenprint#newText,""
blankLine=1endifendifif firstWord$="sub"or firstWord$="function"thenif insLines and blankLine=0thenprint#newText,""
blankLine=1endifendifif insLines and(firstWord$="end sub"or firstWord$="end function")then needBlanks=1return[wrtLine]if indent thenif firstWord$ ="case"thenif notFirstCase then
numTabs = numTabs -1else
notFirstCase =1endifendifgosub[prtWithTabs]if saveTabs >0then numTabs = saveTabs
if indentOk then numTabs = numTabs +1returnendifif outdent then
numTabs = numTabs -1gosub[prtWithTabs]if firstWord$ ="end select"then
numTabs = numTabs -1
notFirstCase =0endifif firstWord$ ="else"then numTabs = numTabs +1returnendifgosub[prtWithTabs]gosub[checkCont]return[prtWithTabs]' if insLines=0 or len( wrkLine$)<>0 or remExcess=0 or blankLine=0 thenfor counter =1to numTabs
print#newText,space$(tabSize);
nextprint#newText, wrkLine$
' end ifgosub[checkBlank]return[checkCont]
indentOk =1
somethingHere =0ifinstr(wrkLineL$,"_")then
endPos =instr( wrkLineL$,"_")
endPos = endPos +1for counter = endPos tolen( wrkLineL$ )ifmid$( wrkLineL$, counter,1)<>" "then
somethingHere =1exitforendifnextif somethingHere thenifmid$(wrkLineL$, counter,1)="'"then
indentOk =1else
indentOk =0endifendifendififinstr(wrkLineL$,"_")thenif indentOk thenif firstCont =0then
firstCont =1
numTabs = numTabs +1endifendifelseif firstCont then
numTabs = numTabs -1
firstCont =0endifendifreturn[checkBlank]if insLines andlen( wrkLine$)=0then
blankLine=1else
blankLine=0endifreturn'Data to start a blockdata"case"data"do"data"for"data"function"data"if"data"select"data"sub"data"while"data"end"'Data to end a blockdata"else"data"end function"data"end if"data"end select"data"end sub"data"loop"data"next"data"wend"data"end"'Data to align to left edgedata"end function"data"end sub"data"function"data"sub"data"end"