我该如何告诉Emacs如何对我的elisp函数参数进行缩进?

3

我写了一个函数(实际上是宏,但无所谓),其工作方式类似于progn。我该如何告诉emacs这个函数应该像progn一样缩进?


你能详细说明一下吗?你想在emacs-lisp-mode中实现这种缩进方式吗? - Noufal Ibrahim
1
你想要正确缩进和突出语法,对吧?它应该适用于任何结构(任何S表达式都应该正确缩进)。语法高亮由font-lock-keywords控制。你能发布一张“错误”行为的截图吗? - Joel J. Adamson
2个回答

7
这应该可以做到。
(put 'myfunc 'lisp-indent-function 0)

对于lisp-indent-function的文档(通过C-h f lisp-indent-function RET找到)的翻译:

lisp-indent-function is a compiled Lisp function in `lisp-mode.el'.

(lisp-indent-function indent-point state)

This function is the normal value of the variable
`lisp-indent-function`. It is used when indenting a line within
a function call, to see if the called function says anything
special about how to indent the line.

indent-point is the position where the user typed TAB, or
equivalent. Point is located at the point to indent under
(for default indentation); state is the `parse-partial-sexp`
state for that position.

If the current line is in a call to a Lisp function which has
a non-nil property `lisp-indent-function`, that specifies how
to do the indentation.  The property value can be:

* `defun`, meaning indent `defun`-style;

* an integer N, meaning indent the first N arguments specially
  like ordinary function arguments and then indent any further
  arguments like a body;

* a function to call just as this function was called. If that
  function returns nil, that means it doesn't specify
  the indentation.

This function also returns nil meaning don't specify the
indentation.

能否详细说明一下,或者至少提供文档链接? - Ryan C. Thompson
实际上,那似乎没有做任何事情。 - Ryan C. Thompson
我之前找到过一篇页面(不记得在哪里了),介绍如何修复emacs对于“if”、“do”和其他几个常见的lisp结构的缩进。从那个页面上,我决定在我的.emacs文件中加入其中的一些建议(现在我已经将lisp-indent-function设置为“if”的nil值,而对于“do”和“do*”,则设置为2)。我同意你的看法,即list-indent-function的文档并不是很详细。 - 6502
糟糕,也许如果我把myfunc改成函数的实际名称,第一次就可以运行了。最后我选择使用(put 'myfunc 'lisp-indent-function (get 'progn 'lisp-indent-function)),即“做任何 progn 所做的事情”。 - Ryan C. Thompson
@Ryan,这比我使用的试错方法还要好。 - 6502

3

因为它“实际上是一个宏”:

C-hig (elisp) 缩进宏 RET

在这种情况下:

(defmacro my-macro (&rest body)
  "Does things"
  (declare (indent defun))
  `(progn ,@body))

(my-macro
  (message "foo")
  (message "bar"))

实验性地(在Emacs 24.3中),(declare (indent x))表单适用于函数。我以前从未尝试过这个。字节编译器不会抱怨,所以我想使用它应该是可以的。有趣! - phils

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接