【问题标题】:Emacs proper cl-flet indentation?Emacs 正确的 cl-flet 缩进?
【发布时间】:2013-07-20 16:38:55
【问题描述】:

cl-flet 的当前缩进对我来说真的很难看。 例如:

(defun foo (lst)
  (cl-flet ((unusually-long-bar (x)
                                (1+ x)
                                (1+ x)
                                (1+ x)))
    (mapcar #'unusually-long-bar lst)))

我想把它设置成更合理的,比如:

(defun foo (lst)
  (cl-flet ((unusually-long-bar (x)
              (1+ x)
              (1+ x)
              (1+ x)))
    (mapcar #'unusually-long-bar lst)))

我该怎么做?

【问题讨论】:

  • cl-flet 中有一个 (declare (indent 1) ...) 表单。这决定了宏的缩进方式。我不认为有一个简单的补丁,但你知道,你可以使用函数引用代替1,并且该函数将以它必须缩进的形式调用,但我不确定它应该返回的确切内容。
  • 为什么不简单地打破函数unusually-long-bar的所有参数,而不是包装一些而另一些不?现在看起来(x) 与其他参数明显不同。

标签: emacs elisp


【解决方案1】:

以下应该有效:

(setq lisp-indent-function 'common-lisp-indent-function)
(eval-after-load "cl-indent"
  '(progn

    (put 'cl-flet 'common-lisp-indent-function 
     (get 'flet 'common-lisp-indent-function))

    ))

【讨论】:

  • 这适用于cl-flet,但现在我的if 语句被错误缩进:then 和else 子句现在处于同一级别。
  • 这是在 common lisp 中缩进 if 的标准方式。如果打扰您,请添加(put 'if 'common-lisp-indent-function 2)
  • 谢谢,它有效。但只有当我评估它时。我试着把它放在~/.emacs,在emacs-lisp-mode-hook,在eval-after-load - 不起作用。
  • 你不需要包装setq。您需要将所有 puts 放入 `(eval-after-load "cl-indent")
  • 我建议不要使用(setq lisp-indent-function #'common-lisp-indent-function)。您会发现许多 Elisp 表单不再正确缩进。
【解决方案2】:

作为 Sabof 答案的补充,这里是一个 sn-p,它将所有 Common Lisp 符号的缩进规则复制到它们的 cl- 前缀 Emacs 等效项(当后者存在时):

(load-library "cl-indent") ; defines the common-lisp-indent-function properties
(cl-loop for symbol being the symbols
         for cl-indent-rule = (get symbol 'common-lisp-indent-function)
         for elisp-equivalent = (intern-soft (concat "cl-" (symbol-name symbol)))
         when (and cl-indent-rule elisp-equivalent (fboundp elisp-equivalent))
         do (put elisp-equivalent 'common-lisp-indent-function cl-indent-rule))

【讨论】:

  • 我认为这里唯一缺少的是(setq lisp-indent-function 'common-lisp-indent-function)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
相关资源
最近更新 更多