12

我不喜欢Elisp中 plist的缩进方式。

;; current   desired   Python (for comparison)
;; '(a 1     '(a 1     {'a': 1,
;;     b 2     b 2      'b': 2,
;;     c 3)    c 3)     'c': 3}

M-x emacs-version24.3.1 上尝试过,运行emacs -Q,输入 plist 并按下C-x h C-M-\

当它不是一个列表时,这个缩进是有意义的:

(mapcar (lambda (x) (x + 1))
        '(1 2 3 4))

如何更改格式设置,以便只有 plist(或者,如果不可能,所有引用列表)具有所需的矩形缩进,但其他所有内容的缩进保持不变?我需要将它本地存储在一个.el文件中,这样当我编辑这个文件时,它会根据需要缩进,但这种行为不会在其他任何地方结束。

4

2 回答 2

10

找到了:

(setq lisp-indent-function 'common-lisp-indent-function)

这是一个示例文件:

(setq x  '(a 1
           b 2
           c 3))

;;; Local Variables:
;;; lisp-indent-function: common-lisp-indent-function
;;; End:

我将在这里转储我的整个缩进配置:

(setq lisp-indent-function 'common-lisp-indent-function)
(put 'cl-flet 'common-lisp-indent-function
     (get 'flet 'common-lisp-indent-function))
(put 'cl-labels 'common-lisp-indent-function
     (get 'labels 'common-lisp-indent-function))
(put 'if 'common-lisp-indent-function 2)
(put 'dotimes-protect 'common-lisp-indent-function
     (get 'when 'common-lisp-indent-function))
于 2014-03-04T08:54:29.213 回答
5

您可以通过覆盖来修复这个(在我看来)错误lisp-indent-function。hack 的原始来源是这个 Github Gist ,它被引用了这个 Emacs Stack Exchange 答案的更多解释。

但是,像这样覆盖核心功能让我感到非常不舒服。一方面,它非常不透明——读者应该如何判断发生了什么变化?更糟糕的是——如果官方lisp-indent-function对未来的定义发生了变化怎么办?我怎么知道我需要更新我的 hack?

作为回应,我创建了el-patch专为解决此问题而设计的库。安装包后,可以覆盖lisp-indent-function如下:

(el-patch-defun lisp-indent-function (indent-point state)
  "This function is the normal value of the variable `lisp-indent-function'.
The function `calculate-lisp-indent' calls this to determine
if the arguments of a Lisp function call should be indented specially.

INDENT-POINT is the position at which the line being indented begins.
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 that has a non-nil
property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
it specifies how to indent.  The property value can be:

* `defun', meaning indent `defun'-style
  (this is also the case if there is no property and the function
  has a name that begins with \"def\", and three or more arguments);

* 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 that returns the indentation (or nil).
  `lisp-indent-function' calls this function with the same two arguments
  that it itself received.

This function returns either the indentation to use, or nil if the
Lisp function does not specify a special indentation."
  (el-patch-let (($cond (and (elt state 2)
                             (el-patch-wrap 1 1
                               (or (not (looking-at "\\sw\\|\\s_"))
                                   (looking-at ":")))))
                 ($then (progn
                          (if (not (> (save-excursion (forward-line 1) (point))
                                      calculate-lisp-indent-last-sexp))
                              (progn (goto-char calculate-lisp-indent-last-sexp)
                                     (beginning-of-line)
                                     (parse-partial-sexp (point)
                                                         calculate-lisp-indent-last-sexp 0 t)))
                          ;; Indent under the list or under the first sexp on the same
                          ;; line as calculate-lisp-indent-last-sexp.  Note that first
                          ;; thing on that line has to be complete sexp since we are
                          ;; inside the innermost containing sexp.
                          (backward-prefix-chars)
                          (current-column)))
                 ($else (let ((function (buffer-substring (point)
                                                          (progn (forward-sexp 1) (point))))
                              method)
                          (setq method (or (function-get (intern-soft function)
                                                         'lisp-indent-function)
                                           (get (intern-soft function) 'lisp-indent-hook)))
                          (cond ((or (eq method 'defun)
                                     (and (null method)
                                          (> (length function) 3)
                                          (string-match "\\`def" function)))
                                 (lisp-indent-defform state indent-point))
                                ((integerp method)
                                 (lisp-indent-specform method state
                                                       indent-point normal-indent))
                                (method
                                 (funcall method indent-point state))))))
    (let ((normal-indent (current-column))
          (el-patch-add
            (orig-point (point))))
      (goto-char (1+ (elt state 1)))
      (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
      (el-patch-swap
        (if $cond
            ;; car of form doesn't seem to be a symbol
            $then
          $else)
        (cond
         ;; car of form doesn't seem to be a symbol, or is a keyword
         ($cond $then)
         ((and (save-excursion
                 (goto-char indent-point)
                 (skip-syntax-forward " ")
                 (not (looking-at ":")))
               (save-excursion
                 (goto-char orig-point)
                 (looking-at ":")))
          (save-excursion
            (goto-char (+ 2 (elt state 1)))
            (current-column)))
         (t $else))))))
于 2017-03-21T02:48:23.793 回答