【问题标题】:How can I tell emacs how the arguments to my elisp function should be indented?我如何告诉 emacs 我的 elisp 函数的参数应该如何缩进?
【发布时间】:2010-08-05 05:57:43
【问题描述】:

我编写了与progn 类似的函数(实际上是一个宏,但无论如何)。如何告诉emacs这个函数应该像progn一样缩进?

【问题讨论】:

  • 你能详细说明一下吗。您想在emacs-lisp-mode 中使用这种缩进吗?
  • 您想正确缩进和突出显示语法,对吗?无论结构如何,它都应该可以工作(任何 s 表达式都应该正确缩进)。语法高亮由font-lock-keywords 控制。您可以发布“错误”行为的屏幕截图吗?

标签: elisp indentation auto-indent


【解决方案1】:

应该这样做

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

【讨论】:

  • 想详细说明,或者至少链接到文档?
  • 其实这好像没什么作用。
  • 我前段时间(我不记得在哪里)找到了一个页面,该页面描述了如何修复“if”、“do”和其他一些常见 lisp 结构的 emacs 缩进。从那个页面我决定在我的 .emacs 中加入一些建议(现在我将 lisp-indent-function 设置为 nil 表示“if”,设置为 2 表示“do”和“do*”)。我同意你的观点,list-indent-function 的文档不是那么详细。
  • 糟糕,如果我将 myfunc 更改为函数的实际名称,也许第一次就可以了。我最终选择了(put 'myfunc 'lisp-indent-function (get 'progn 'lisp-indent-function)),即“做任何progn 做的事情。”
  • @Ryan 这比我使用的试看方法还要好
【解决方案2】:

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

C-hig (elisp) Indenting Macros 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)) 表单确实 也适用于函数。我以前从未尝试过。字节编译器没有抱怨,所以我想它可以使用。有趣!
猜你喜欢
  • 2023-04-05
  • 2020-07-27
  • 2011-12-09
  • 2011-07-14
  • 2019-04-12
  • 1970-01-01
  • 2013-06-07
相关资源
最近更新 更多