Lisp Indentation

The general rule of thumb is that a closing parenthesis should never be to the left of its matching opening parenthesis. All new lines should be a couple of spaces to the right of the opening parenthesis of the list they're in.

  (defun factorial (n)
    (let ((m 1))
      (loop while (> n 0)
            do (setf m (* m n))
               (decf n))
      m))
Emacs auto-indents this as:

  (defun factorial (n)
    (let ((m 1))
      (loop while (> n 0)
            do (setf m (* m n))  ;; !
            (decf n))
      m))
since only the 'loop' macro knows that both those clauses are connected with 'do'. So, even in some trivial cases, LispIndentation is not LispAutoIndentation as would be desirable. -- JesseMillikan

It seems that Emacs is using a simple algorithm: indent each line two spaces for each parenthesis still open. I'm no Emacs user so I don't know if that's the case in the actual implementation, but the method makes sense.


See LispParenthesesUseNetArticle and LostInaSeaofParentheses

See also CodingStyle, BracesAreGood :-)


CategoryLisp


EditText of this page (last edited February 17, 2011) or FindPage with title or text search