W3cubDocs

/Elisp

Sample Indentation Rules

Here is an example of an indentation function:

(defun sample-smie-rules (kind token)
  (pcase (cons kind token)
    (`(:elem . basic) sample-indent-basic)
    (`(,_ . ",") (smie-rule-separator kind))
    (`(:after . ":=") sample-indent-basic)
    (`(:before . ,(or `"begin" `"(" `"{")))
     (if (smie-rule-hanging-p) (smie-rule-parent)))
    (`(:before . "if")
     (and (not (smie-rule-bolp)) (smie-rule-prev-p "else")
          (smie-rule-parent)))))

A few things to note:

  • The first case indicates the basic indentation increment to use. If sample-indent-basic is nil, then SMIE uses the global setting smie-indent-basic. The major mode could have set smie-indent-basic buffer-locally instead, but that is discouraged.
  • The rule for the token "," make SMIE try to be more clever when the comma separator is placed at the beginning of lines. It tries to outdent the separator so as to align the code after the comma; for example:
    x = longfunctionname (
            arg1
          , arg2
        );
    
  • The rule for indentation after ":=" exists because otherwise SMIE would treat ":=" as an infix operator and would align the right argument with the left one.
  • The rule for indentation before "begin" is an example of the use of virtual indentation: This rule is used only when "begin" is hanging, which can happen only when "begin" is not at the beginning of a line. So this is not used when indenting "begin" itself but only when indenting something relative to this "begin". Concretely, this rule changes the indentation from:
        if x > 0 then begin
                dosomething(x);
            end
    

    to

        if x > 0 then begin
            dosomething(x);
        end
    
  • The rule for indentation before "if" is similar to the one for "begin", but where the purpose is to treat "else if" as a single unit, so as to align a sequence of tests rather than indent each test further to the right. This function does this only in the case where the "if" is not placed on a separate line, hence the smie-rule-bolp test.

    If we know that the "else" is always aligned with its "if" and is always at the beginning of a line, we can use a more efficient rule:

    ((equal token "if")
     (and (not (smie-rule-bolp))
          (smie-rule-prev-p "else")
          (save-excursion
            (sample-smie-backward-token)
            (cons 'column (current-column)))))
    

    The advantage of this formulation is that it reuses the indentation of the previous "else", rather than going all the way back to the first "if" of the sequence.

Copyright © 1990-1996, 1998-2019 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/SMIE-Indentation-Example.html