indent - Manage the auto-indentation methods
0 indent "ind-no" "flags" [ "look-back" ] [ "indent-scheme" ]
indent "ind-no" "type" "token" [ "close" "ignore" ] [ "indent" ]
indent "ind-no" "t" "token" [ "token" ] "indent-scheme"
indent "ind-no" "x" | "w" "token" "indent" "indent-scheme"
The indent command creates and manages the auto-indenting methods, the process of creating a new indentation method is best described in File Language Templates. The command takes various forms as defined by the arguments. Each of the argument configurations is defined as follows:-
0 indent "ind-no" "flags" "look-back" [ "indent-scheme" ]
With an argument of 0, indent creates a new indentation method with the integer handle ind-no. The indentation method is assigned to a buffer by setting $buffer-indent(5) to ind-no. ind-no cannot be 0 as setting $buffer-indent to zero disables indentation. If the indentation method with the same ind-no already exists, then the existing method is deleted and a new method may be created.
flags Sets the indent bit flags where:-
0x01
0x02
This feature is only used when indent is required to support multiple complex indentation schemes within a single file, for example indentation of scripts within html, asp or php pages etc. When defined the given indent-scheme must also be defined using indent, the scheme's look-back argument is typically set to look back a long way to ensure correctness, i.e. 500 lines, but it should therefore contain few tokens to maintain performance.
0x04
0x08
0x10
look-back specifies the maximum number of lines, prior to the current line, considered when calculating the indentation of a line, i.e. if there are look-back number of lines between the line to be indented and the previous non-blank line then the current indentation is lost.
If look-back is set to 0 then the indentation is effectively disabled as the current indentation can never be found. The maximum value has been increase as of January 2004 from 255 to an integer (large number), a value of 10 is typically sufficient for a normal indent scheme and 500 for a fast look-back scheme.
0 indent "ind-no" "flags"
indent "ind-no" s "statement"
indent "ind-no" c "continue"
indent "ind-no" x "max"
indent "ind-no" b "brace"
indent "ind-no" e "braceStatement"
indent "ind-no" w "switch"
indent "ind-no" a "case"
indent "ind-no" m "margin"
indent "ind-no" u "comcont"
C mode is built into the editor by default and is enabled by setting the indent intialization flag bit 0x04. With this bit enabled then the indentation for 'C' mode is defined through the additional arguments. ind-no and flags have the same meaning as defined in the previous section. C mode is typically initialized with the value 12. The remaining C arguments are defined as follows:
indent "ind-no" "s" "statement" (=t)
{ xxxxxxxxxx xxxxxxxxxx
or
if(xxxxx) xxxxxxxxxx
C tokens are only used to indent the next line, whereas '{' are used in indenting every line to it's partnering '}'.
indent "ind-no" "c" "continue" (=3/2t)
thisIsAVeryLongVariableWhichMeansAssignmentsAreSplit = ThisIsTheFirstContinuedStatementLine + ThisIsTheSecondContinuedStatementLine + etc ;
The indent is changed if there is an open bracket, continued statements are indented to the depth of the open bracket plus one, e.g.
func(firstFuncArg, secondFuncArg, anotherBracketForFun(firstAnotherBracketForFunArg, secondAnotherBracketForFunArg), thirdFuncArg) ;
indent "ind-no" "x" "max"
The effect of max is described as follows; if max is set to a large value then the default open brace offset appearence is:-
longVariable = LongFunctionNameWhichMeans(isSoFar, OverAndYouRunOutOfRoom) ;
Setting max to 16 gives:
longVariable = LongFunctionNameWhichMeans(isSoFar, overAndYouRunOutOfRoom) ;
Where by the second argument indent has been artificially reduced because of it's length.
indent "ind-no" "b" "brace" (=0)
if(xxxxxx) { xxxxxxxxxx xxxxxxxxxx }
With a setting of 2, this would become:-
if(xxxxxx) { xxxxxxxxxx xxxxxxxxxx }
This works in conjunction with statement and braceStatement a change to statement will change the position of '{'s. In summary:
if (condition) if (condition) if (condition) { { { statement; statement; statement; } } } brace = 0 brace = 4 brace = 2
indent "ind-no" "e" "braceStatement" (t)
indent "ind-no" "w" "switch" (=0)
indent "ind-no" "a" "case" (=-t)
switch(xxxxxxxxx) { case 1: xxxxxxxxxx xxxxxxxxxx case 2: xxxxxxxxxx }
Setting the value to 4, increases the leading space on the "case" statement, e.g.
switch(xxxxxxxxx) { case 1: xxxxxxxxxx xxxxxxxxxx case 2: xxxxxxxxxx }
case sets the offset of the lines following a "case" statement, from the current indent. For example, using the default settings, if the current indent was 20 then a line starting with a "case" would be indented to 16, i.e.
xxxxxxxxxx case xxxxxxxxxx xxxxxxxxxx
This is used inside "switch" statements, the default setting give the following lay-out:-
switch(xxxxxxxxxx) { case 1: xxxxxxxxxx xxxxxxxxxx case 2:
This works in conjunction with the statement variable, a change to statement will change the position of '{'s.
In summary:
switch (variable) switch (variable) { { case 1: case 1: break; break; default: default: break; break; } } switch = 0 switch = t case = -t case = -t
indent "ind-no" "m" "margin" (=-1)
xxxxxx ;/
when a '*' is entered the line becomes
xxxxxx ; /*
The indenting of the "/*" occurs only if there is text on the line before it, and none after it. If the current column is already past margin then it is indented to the next tab stop.
A value of -1 disables this feature.
In summary:
statement; /* Comment */ statement; /* Comment */ margin = 40 margin = -1
indent "ind-no" "u" "comcont" (" * ")
/* xxxxxxxxxx @
where '@' is the current cursor position. With a setting of " * ", then:-
/* xxxxxxxxxx * @
In summary:
/* /* /* Comment * Comment ** Comment Comment */ * Comment ** Comment */ */ comcont = " * " comcont = "** " comcont = " "
The standard indent rule for setting up C and Java is defined as:-
0 indent .hilight.c 12 indent .hilight.c "s" t ; Default may be omitted indent .hilight.c "e" t ; Default may be omitted indent .hilight.c "c" 3/2t ; Default may be omitted indent .hilight.c "x" 0 ; Default may be omitted indent .hilight.c "b" 0 ; Default may be omitted indent .hilight.c "w" 0 ; Default may be omitted indent .hilight.c "a" -t ; Default may be omitted indent .hilight.c "m" -1 ; Default may be omitted indent .hilight.c "u" " * "
This is typically written with the defaults omitted.
0 indent .hilight.c 12 indent .hilight.c "u" " * "
If GNU Emacs indentation is required then the following definition may be used:-
0 indent .hilight.c 12 ; if (a == b) indent .hilight.c "e" t ; { indent .hilight.c "b" t ; c = d; indent .hilight.c "s" t ; } indent .hilight.c "u" " * "
The default configuration scheme is defined as:-
0 indent .hilight.c 12 ; if (a == b) indent .hilight.c "e" t ; { indent .hilight.c "b" 0 ; c = d; indent .hilight.c "s" t ; } indent .hilight.c "u" " * "
Where comments are to be aligned at the end of a statement then the margin parameter may be defined in <user>.emf.
indent .hilight.c "m" 40
indent "ind-no" "type" "token" [ "close" [ "ignore" ]] [ "indent" ] [ "indent2" ] [ "indent-scheme" ]
With the default argument of 1, indent creates a new rule for the indentation method ind-no which must have previously been defined and initialized.
The indentation of a line in a buffer, which is using an indentation method, is affected by the token types matched on the line (type f, o, s) and the current indentation (if line is not of type f).
The current indentation is determined by searching the previous lines (look-back) for the indentation of the last indented line. This may not simply be the indentation of the last non-blank line, the exact indentation is determined by searching for tokens in the line and assessing their effect on the indentation of the current line.
The format of the regex valid in the "token" and "close" arguments are the same as at used by hilight token creation, see hilight(2) for more information.
The indent argument specifies the indent to apply in characters. The indent may be specified as a literal integer value, a positive value increases the indent a negative value reduces the indent. The indent may be specified in terms of buffer specific generic value $buffer-indent-width(5) using the value t, indicating a tab width. The value of t may be prefixed with a signed integer or vulgar fraction which adjusts the size of the indentation width by the appropriate amount. Modifying the value of $buffer-indent-width modifies the indent width that is applied allowing the indentation method to be specified independently of the indentation width.
The indent tokens may be assigned one of the following types, using the type argument. If the type is specified in upper case then the token must be surrounded by non-alpha-numeric characters:
Fixed (type = 'f' or 'F')
indent .hilight.emf f "*" 0
producing
..... *label .....
The fixed token must be the first non-white character on the line, the rest of the line is ignored. The indentation of the previous line has no effect.
Indent-from-next-line-onward (type = 'n' or 'N')
indent .hilight.emf n "!if" t
Keeps the indentation of the !if line the same as the previous indentation, change the indentation on the following lines by an extra $buffer-indent-width characters, to produce:
.... !if ....
Indent-from-current-line-onward (type = 'o' or 'O')
indent .hilight.emf o "!endif" -t
decrement the indent of the !endif line and following lines by $buffer-indent-width spaces producing:
.... !endif ....
Universal-indent (type = 'u' or 'U')
indent .hilight.emf u "<cfelse" -t 3/2t
This has been used in the ColdFusion indentation to control the <cfelse> condition which requires control of both the current line and next line with different values from the same token. Note that a Indent-single could be implemented with this command using an argument of -t t.
Indent-single (type = 's' or 'S')
indent .hilight.emf o "!elif" -t
decrements the indentation of the !elif line by $buffer-indent-width characters, but restores the previous indentation after the current line, producing:
.... !elif ....
Bracket (type = 'b' or 'B')
When the previous line has an unmatched open bracket
When the previous line has an unmatched close bracket
For a bracket the only other argument given is the close. e.g. tcl's '(' and ')' brackets
indent .hilight.tcl b "(" ")"
Which produces:
.... .... (.... .... ....) ....
Continue (type = 'c' or 'C')
indent .hilight.tcl c "\\" 3/2t
A simple example is
.... 12345678901234567890 \ .... ....
When used in conjunction with brackets, the following effect is observed:
.... 12345678901234567890 \ ....(.... \ ....) \ .... \ .... ....
This shows why the first continuation line (the 123456... line) must be located and used as the base line from which the indentation is derived; again the look-back limits the search for this line.
Exclusion (type = 'e' or 'E')
indent .hilight.emf e "\"" "\"" "\\"
e.g. tcl's quotes
indent .hilight.tcl e "\"" "\"" "\\"
producing:-
.... ".... ignore { ... \" ... ignore another { token ... " ....
Ignore (type = 'i' or 'I')
indent .hilight.emf i ";"
Or tcl's '#' comment
indent .hilight.tcl i "#"
producing
.... # ... ignore this { indent token ....
Type (type = 't' or 'T')
If the indent-scheme is defined as zero (0) then the indentation scheme is set to the same value as the $buffer-indent(5) variable.
The following example is taken from the XML language definition . A fast-look back scheme is used (hilight.xmlT) to search for the alternative indentation schemes for javascript and vbscript. The t indent type defines the start of script tokens and to switch the indentation to the new indent type.
; Fast look-back indentation scheme 0 indent .hilight.xmlT 1 500 ; Indentation scheme 0 indent .hilight.xml 3 20 .hilight.xmlT !if &exist .indent.javascript indent .hilight.xmlT t "<script ... javascript>" .indent.javascript indent .hilight.xml x "<script ... javascript>" 2 .indent.javascript indent .indent.javascript w "</script>" -2 0 !endif !if &exist .indent.vbscript indent .hilight.xmlT t "<script ... vbscript>" .indent.vbscript indent .hilight.xml x "<script ... vbscript>" 2 .indent.vbscript indent .indent.vbscript w "</script>" -2 0 !endif indent .hilight.xmlT t "</script>" 0 indent .hilight.xml o "</script>" -2 ; Define the indentation tokens. XML is well defined so there are ; always opening and closing brackets. indent .hilight.xml e "\"" "\"" "\\" indent .hilight.xml b "<!--" "-->" indent .hilight.xml b "<\\?" "\\?>" indent .hilight.xml n "<[^/!]" 2 indent .hilight.xml o "</" -2 indent .hilight.xml o "/>" -2
Change (type = [x|X|w|W|v|V])
Refer the the type=t for an example of indentation switching.
Examples of indentation method creations can be found in macro files hkemf.emf, hktcl.emf and hkvrml.emf. The following example is taken from hkemf.emf:-
!if ¬ &exist .hilight.emf set-variable .hilight.emf &pinc .hilight.next 1 !endif ... 0 indent .hilight.emf 0 10 indent .hilight.emf N "define-macro" t indent .hilight.emf n "!if" t indent .hilight.emf s "!eli" -t indent .hilight.emf s "!els" -t indent .hilight.emf o "!end" -t indent .hilight.emf n "!whi" t indent .hilight.emf o "!don" -t indent .hilight.emf n "!rep" t indent .hilight.emf o "!until" -t indent .hilight.emf o "!ema" -t indent .hilight.emf e "\"" "\"" "\\" indent .hilight.emf i ";" indent .hilight.emf f "*" 0
Note that a .hilight command variable is typically used as a buffer with indentation rules will almost certainly have hilighting and the same variable is used to define the hilighting scheme.
The variables $indent-width(5), $buffer-indent-width(5) and the t indent notation were introduced in the November 2004 version of MicroEmacs.
The electric C mode cmode(2m) was removed in December 2004, as were the variables $c-brace(5), $c-case(5), $c-contcomm(5), $c-continue(5), $c-margin(5) and $c-statement(5). All replaced by the C Indent initialisation method.
The Universal indent (u) was introduced in the 2006 release.
File Language Templates, $buffer-indent(5), $buffer-indent-width(5), $indent-width(5), add-file-hook(2), hilight(2).
(c) Copyright JASSPA 2009
Last Modified: 2009/08/29
Generated On: 2009/10/12