Default [MORTSCRIPT] Indent.mscr - A Simple Script Beautifier

Thought I should share this code snippet, because I am sure it does not exist a comparable one.

You simply copy and paste the code listet.

Code:
Select Code
//
// indent.mscr
//
//  2014 jwoegerbauer
// GPL v2
//
//
// This indent script changes the appearance of a MSCR script by inserting or deleting whitespace. 
// One issue in the formatting of MortScript code is how far each line should be indented from the
// left margin. When the beginning of a statement such as IF or FOR is encountered, the indentation
// level is increased by 1 (one). Consequently when the beginning of a statement such as ENDIF or 
// NEXT is encountered, the indentation level is decreased by 1 (one). The indentation (whitespace)
// is calculated as indentation level * TAB
//


// Get script file to be processed: if it's not passed as argument, a dialog will
// be shown you select the file from
fileIN = "" & argv[1]
If( fileIN EQ "" )
	fileIN = SelectFile("Select script file",0,"*.mscr", ("Please select script file that should get indented"))
EndIf
If( fileIN EQ "" )

	// Quit
	Exit
EndIf

fileOUT = fileIN & ".indented"
fileWrites = 0
indentLevel = 0
indentExtra = 0 // only set if SWITCH ... ENDSWITCH detected

lineIN = ReadLine(fileIn)

While(NOT IsEmpty(lineIN))

	fileWrites += 1

	// Left trim line
	idx = 1
	While((SubStr(lineIN, idx, 1) EQ " ") || (SubStr(lineIN, idx, 1) EQ "^TAB^"))
		idx += 1
	EndWhile
	lineOUT = SubStr(lineIN, idx)

	// Create a copy of trimmed line and convert it to upper case
	lineTMP = ToUpper(lineOUT)

	// Build indentation string
	indentationString = ""
	If (indentLevel >= 1)
		For idx = 1 To indentLevel
			indentationString &= "^TAB^"
		Next
	EndIf

	// Flags
	opening = 0
	closing = 0

        // Ignore if it's a comment line
	If((SubStr(lineIn, 1, 1) NE "#") && (SubStr(lineIn, 1, 1) NE "/"))

		// Check whether to indent/unindent
		opening = ((SubStr(lineTMP, 1, 2) EQ "IF") ? 1 : 0)
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 4) EQ "ELSE") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 6) EQ "ELSEIF") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP,1, 3) EQ "FOR") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 7) EQ "FOREACH") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 5) EQ "WHILE") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 6) EQ "SWITCH") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 4) EQ "CASE") ? 1 : 0)
			indentExtra = (opening ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 7) EQ "DEFAULT") ? 1 : 0)
			indentExtra = (opening ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 6) EQ "REPEAT") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 6) EQ "CHOICE") ? 1 : 0)
		EndIf
		If(NOT opening)
			opening = ((SubStr(lineTMP, 1, 13) EQ "CHOICEDEFAULT") ? 1 : 0)
		EndIf

		If(NOT opening)
			closing = ((SubStr(lineTMP, 1, 5) EQ "ENDIF") ? 1 : 0)
			If(NOT closing)
				closing = ((SubStr(lineTMP, 1, 4) EQ "NEXT") ? 1 : 0)
			EndIf
			If(NOT closing)
				closing = ((SubStr(lineTMP, 1, 10) EQ "ENDFOREACH") ? 1 : 0)
			EndIf
			If(NOT closing)
				closing = ((SubStr(lineTMP, 1, 8) EQ "ENDWHILE") ? 1 : 0)
			EndIf
			If(NOT closing)
				closing = ((SubStr(lineTMP, 1, 9) EQ "ENDSWITCH") ? 1 : 0)
				indentExtra = (closing ? -2 : 0)
			EndIf
			If(NOT closing)
				closing = ((SubStr(lineTMP, 1, 9) EQ "ENDREPEAT") ? 1 : 0)
			EndIf
			If(NOT closing)
				closing = ((SubStr(lineTMP, 1, 9) EQ "ENDCHOICE") ? 1 : 0)
			EndIf
		EndIf

	EndIf

	If( closing )

		// Rebuild identation string

		indentLevel -= 1

		// Handle SWITCH ... ENDSWITCH compound statements
		If (indentExtra <> 0)
			indentLevel += indentExtra
			indentExtra = 0
		EndIf

		indentationString = ""
		If (indentLevel >= 1)
			For idx = 1 To indentLevel
				indentationString &= "^TAB^"
			Next
		EndIf
	EndIf

	// Create line to be output
	lineOUT = (indentationString & lineOUT & "^NL^")

	fileAppend = ((fileWrites > 1) ? 1 : 0)
	WriteFile(fileOUT, lineOUT, fileAppend)

	// Force re-calculate indentation

	If(opening)
		
		// Handle SWITCH ... ENDSWITCH compound statements
 		If (indentExtra > 0)
			indentLevel += indentExtra
			indentExtra = 0
		EndIf

		indentLevel += 1
	EndIf

	lineIN = ReadLine(fileIn)

EndWhile

// Done
Exit
HTH
My devices:
GPS: Windows Embedded CE 6.0 Professional
Phablet: Android 4.0.4
Laptop: Windows 8.1.1
Phone: Windows Phone 8.0