Code archives/Algorithms/Auto code indenter
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| Somebody needed a program to do this... so I thought I'd have a crack at writing it. The code below is not indented on purpose! Give it an input file and an output file, run it and the code comes out all indented. There are 2 lists, addindent$ and decindent$, these hold the commands for adding and removing indents (with the exception of 'if' as this is a bit special). I've probably missed a couple so let me know which ones and I'll update the code as necessary. You will notice that some of the commands have a space after them, that's because you might have a variable called functionname so it needs the space to create seperation. Times it will NOT work: If you've got variable names like EndIfName it will go pear shaped. If you've got if statements without 'then' formed like 'If a=true a=false' Happy indenting! | |||||
; Code indenter written by Rob Farley (Dec 2004)
;
; Additional If-Then specials by Damien Sturdy
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
If Left(Lower(l),2)="if" And Instr(Lower(l),"then:")=1 Then addone = True
If Left(Lower(l),2)="if" And Instr(Lower(l),"then :")=1 Then addone = True
If Left(Lower(l),2)="if" And Right$(Lower(l),4)="then" Then addone = True
If Left(Lower(l),2)="if" And Instr(l,";")>1 Then
nn=Instr(l,";")
Repeat:nn=nn-1:Until Mid$(l,nn,1)<>";":nn=nn+1
Repeat:nn=nn-1:Until Mid$(l,nn,1)>" " Or nn<2
If nn>6 Then
;Print Lower(Mid$(l,nn-4,4))
If Lower(Mid$(l,nn-3,4))="then" Then addone=True
If Lower(Mid$(l,nn-4,5))="then:" Then addone=True
If Lower(Mid$(l,nn-5,6))="then :" Then addone=True
EndIf
EndIf
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
ExecFile outputfilename$ |
Comments
| ||
| Nice one.. great start. I like the code you did, and i like my modifications ^.^ this realy helped me out with a super-long BB file thatd have taken HOURS to have fixed!!! cheers :) |
| ||
| Another extremely useful snippet, Rob! |
Code Archives Forum