Question

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.)

Was it helpful?

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.

OTHER TIPS

In addition to what jbm said, declare is not part of Emacs Lisp before version 22 (or it might be 21). So if you want your code to be usable also with older Emacs versions then do not use declare.

The first expands to the second, so there is no technical difference. Nowadays, the first is the recommended one, while the second is the underlying implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top