From: Mark Wooding Date: Wed, 1 Jul 2020 18:52:49 +0000 (+0100) Subject: el/dot-emacs.el: Fix indentation of `loop'. X-Git-Url: https://git.distorted.org.uk/~mdw/profile/commitdiff_plain/1be8ceca19b38d9ee61869667a0ee41055fffe15 el/dot-emacs.el: Fix indentation of `loop'. This one has been annoying me for years. --- diff --git a/el/dot-emacs.el b/el/dot-emacs.el index 09da2d6..fc3c13f 100644 --- a/el/dot-emacs.el +++ b/el/dot-emacs.el @@ -4306,9 +4306,12 @@ that character only to be normal punctuation.") ;; Special indentation. -(setq lisp-simple-loop-indentation 2 - lisp-loop-keyword-indentation 6 - lisp-loop-forms-indentation 6) +(defvar mdw-lisp-loop-default-indent 2) + +(setq lisp-simple-loop-indentation 0 + lisp-loop-keyword-indentation 0 + lisp-loop-forms-indentation 2 + lisp-lambda-list-keyword-parameter-alignment t) (defun mdw-indent-funcall (path state indent-point sexp-column normal-indent) "Indent `funcall' more usefully. @@ -4327,6 +4330,58 @@ to `foo'." (current-column)))))))) (put 'funcall 'common-lisp-indent-function 'mdw-indent-funcall) +(defadvice common-lisp-loop-part-indentation + (around mdw-fix-loop-indentation (indent-point state) activate compile) + "Improve `loop' indentation. +If the first subform is on the same line as the `loop' keyword, then +align the other subforms beneath it. Otherwise, indent them +`mdw-lisp-loop-default-indent' columns in from the opening parenthesis." + + (let* ((loop-indentation (save-excursion + (goto-char (elt state 1)) + (current-column)))) + + ;; Don't really care about this. + (when (and (eq lisp-indent-backquote-substitution-mode 'corrected)) + (save-excursion + (goto-char (elt state 1)) + (cl-incf loop-indentation + (cond ((eq (char-before) ?,) -1) + ((and (eq (char-before) ?@) + (progn (backward-char) + (eq (char-before) ?,))) + -2) + (t 0))))) + + ;; If the first loop item is on the same line as the `loop' itself then + ;; use that as the baseline. Otherwise advance by the default indent. + (goto-char (cadr state)) + (forward-char 1) + (let ((baseline-indent + (if (= (line-number-at-pos) + (if (condition-case nil (progn (forward-sexp 2) t) + (scan-error nil)) + (progn (forward-sexp -1) (line-number-at-pos)) + -1)) + (current-column) + (+ loop-indentation mdw-lisp-loop-default-indent)))) + + (goto-char indent-point) + (beginning-of-line) + + (setq ad-return-value + (list + (cond ((not (lisp-extended-loop-p (elt state 1))) + (+ baseline-indent lisp-simple-loop-indentation)) + ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)") + (+ baseline-indent lisp-loop-keyword-indentation)) + (t + (+ baseline-indent lisp-loop-forms-indentation))) + + ;; Tell the caller that the next line needs recomputation, even + ;; though it doesn't start a sexp. + loop-indentation))))) + ;; SLIME setup. (defvar mdw-friendly-name "[mdw]"