Search code examples
emacsmacroselispauto-indent

difference between two ways of specifying emacs lisp macro indent style


What is the difference or pros/cons between the two popular ways of specifying an indentation style for an Emacs Lisp macro I define?

The declare way:

(defmacro my-dotimes-1 (num &rest body)
  (declare (indent 1))  ; <---
  `(let ((it 0))
     (while (< it ,num)
       ,@body
       (setq it (1+ it)))))

The put way:

(defmacro my-dotimes-2 (num &rest body)
  `(let ((it 0))
     (while (< it ,num)
       ,@body
       (setq it (1+ it)))))
(put 'my-dotimes-2 'lisp-indent-function 1) ; <---

(The name it is not a gensym because the example is copied from the --dotimes macro of dash.el which is intended as an anaphoric macro.)


Solution

  • The only difference I know of is that declare only works for Emacs Lisp, whereas the put method works for other languages as well (that is, if they use a similar technique for managing their indentation).

    For instance, you can do things like

    (put 'match 'clojure-indent-function 2)
    

    To control how clojure-mode indents particular forms.

    It's also worth noting that while indentation levels are most often specified for macros it also works with functions.