Code alignment

Blitz3D Forums/Blitz3D Programming/Code alignment

This sounded like an interesting challenge... So I did one!

Code is not indented by intention!

Function Entry$(number,List$,delimeter$=",")
n=1
count = 1
found = False
start = 1

If number > 1
Repeat
If Mid(List,n,1)=delimeter
count = count + 1
If count = number
found=True
start = n + 1
Exit
EndIf
EndIf
n=n+1
Until n >= Len(List)
If found = False Then RuntimeError("List Element out of Range")
EndIf
Endof = Instr(List,delimeter,start)
If endof = 0 Then endof = Len(List)+1
Return Mid(List,start,endof-start)
End Function

Function countentries(List$,delimeter$=",")
t$ = Replace(List$,delimeter,"")
Return (Len(List)-Len(t))+1
End Function


Function removeindents$(l$)
ret$=""
For n=1 To Len(l$)
If Asc(Mid(l,n,1))>31 Then ret=ret+Mid(l,n,1)
Next
Return ret
End Function


; add addindent or decindent commands if I've missed any
addindent$="repeat,while,function,type,for"
addindentfuncs = countentries(addindent)
DebugLog addindentfuncs
decindent$="until,wend,end function,end type,next,endif"
decindentfuncs = countentries(decindent)

indent = 0

inputfilename$="autoindent.bb"
outputfilename$="autoindent.txt"


filein = ReadFile(inputfilename)
fileout = WriteFile(outputfilename)

addone=False

Repeat

l$ = removeindents(ReadLine(filein))

For n=1 To addindentfuncs
funky$ = Lower(entry(n,addindent))
If Left(Lower(l),Len(funky))=funky Then addone = True

; special if statement, checks if there's a 'then' in the line
If Left(Lower(l),2)="if" And Instr(Lower(l),"then")=0 Then addone = True

Next

For n=1 To decindentfuncs
funky$ = Lower(entry(n,decindent))
If Left(Lower(l),Len(funky))=funky Then indent = indent - 1
Next

tab$ = Chr(9)

indenter$ = ""
If indent > 0
For n=1 To indent
indenter = indenter + tab
Next
EndIf

If addone = True
indent = indent + 1
addone = False
EndIf

l = indenter + l

WriteLine fileout,l

Until Eof(filein)

CloseFile filein
CloseFile fileout