Search code examples
emacslispindentation

What are the rules behind the typical indenting conventions of lisp?


Note to mods: Please note that this question is NOT AT ALL a question of taste. I am asking for specific information regarding the indenting conventions I have encountered in Spacemacs' default indentation rules for elisp.

Why is this code indented like this by formatters:

(defun lispy--maybe-safe-delete-region (beg end)
  "Delete the region from BEG to END.
If `lispy-safe-delete' is non-nil, exclude unmatched delimiters."
  (if lispy-safe-delete
      (let ((safe-regions (lispy--find-safe-regions beg end)))
        (dolist (safe-region safe-regions)
          (delete-region (car safe-region) (cdr safe-region)))
        (test))
    (delete-region beg end)
    (test)))

And not like this:

(defun lispy--maybe-safe-delete-region (beg end)
  "Delete the region from BEG to END.
If `lispy-safe-delete' is non-nil, exclude unmatched delimiters."
  (if lispy-safe-delete
      (let ((safe-regions (lispy--find-safe-regions beg end)))
        (dolist (safe-region safe-regions)
          (delete-region (car safe-region) (cdr safe-region)))
        (test))
      (delete-region beg end)
      (test)))

As you see, the default format has not indented the children of the if sexp at the same level. I don't understand the rules by which it operates, because the children of the let sexp ARE indented at the same level.


Solution

  • Because the IF operator of Emacs Lisp allows one then form and multiple else forms. The else forms are indented differently from the then form for readability.

    ELISP> (if (> 10 20)
               (print 'foo)
             (princ 'bar)
             (princ 'baz)
             nil)
    barbaz
    nil