From b3105ee7e08ee76f06773ee61ef7bbc264dfefc7 Mon Sep 17 00:00:00 2001 From: akater Date: Fri, 24 Sep 2021 15:04:47 +0000 Subject: [PATCH] Indent bodies of local function definitions properly in elisp-mode * lisp/emacs-lisp/lisp-mode.el (lisp-indent-function): Check for local defforms (lisp--local-defform-body): New auxiliary function --- lisp/emacs-lisp/lisp-mode.el | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index eac3c03cd1..b9bfba60b1 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -29,6 +29,7 @@ ;;; Code: (eval-when-compile (require 'cl-lib)) +(eval-when-compile (require 'subr-x)) (defvar font-lock-comment-face) (defvar font-lock-doc-face) @@ -1105,6 +1106,26 @@ defun calculate-lisp-indent (&optional parse-start) (t normal-indent)))))) +(defun lisp--local-defform-body (state) + "Return non-nil if at local definition body according to STATE. + +STATE is the `parse-partial-sexp' state for current position." + (when-let ((start-of-innermost-containing-list (nth 1 state))) + (let* ((parents (nth 9 state)) second-order-parent + (second-cons-after (cddr parents))) + (while second-cons-after + (when (= start-of-innermost-containing-list + (car second-cons-after)) + (setq second-order-parent (car parents) + ;; Leave the loop. + second-cons-after nil)) + (pop second-cons-after) (pop parents)) + (and second-order-parent + (save-excursion + (goto-char (1+ second-order-parent)) + (memq (read (current-buffer)) + '(cl-flet cl-labels))))))) + (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 @@ -1138,7 +1159,8 @@ defun lisp-indent-function (indent-point state) (if (and (elt state 2) (not (looking-at "\\sw\\|\\s_"))) ;; car of form doesn't seem to be a symbol - (progn + (if (lisp--local-defform-body state) + (lisp-indent-defform state indent-point) (if (not (> (save-excursion (forward-line 1) (point)) calculate-lisp-indent-last-sexp)) (progn (goto-char calculate-lisp-indent-last-sexp) @@ -1160,7 +1182,9 @@ defun lisp-indent-function (indent-point state) (cond ((or (eq method 'defun) (and (null method) (> (length function) 3) - (string-match "\\`def" function))) + (string-match "\\`def" function)) + ;; Check whether we are in flet or labels. + (lisp--local-defform-body state)) (lisp-indent-defform state indent-point)) ((integerp method) (lisp-indent-specform method state -- 2.32.0