From 34e599de92271216f3ad6e63eb6adc80bd6ed6f4 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Wed, 16 Oct 2024 23:10:20 -0700 Subject: [PATCH] Fix adjust-parens indent stuff --- init.el | 59 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/init.el b/init.el index f8355ad..9969250 100644 --- a/init.el +++ b/init.el @@ -493,19 +493,56 @@ With NO-EDGE, return nil if beg or end fall on the edge of the range." (use-package adjust-parens :hook (prog-mode . adjust-parens-mode) :config - (defun my/normal-state-lisp-indent-adjust-parens () - "Like `lisp-indent-adjust-parens', but got to first char on line first." + (defun my/lisp-indent-adjust-parens () + "Like `lisp-indent-adjust-parens', but got to first char on line first. +Also, this works even if the region is active (it indents every line in the +region)." (interactive) - (back-to-indentation) - (lisp-indent-adjust-parens)) - (defun my/normal-state-lisp-dedent-adjust-parens () - "Like `lisp-dedent-adjust-parens', but got to first char on line first." + (save-mark-and-excursion + (let ((end (mark t)) + (line-count 1) + (indent-cols)) + (when (and (region-active-p) end) + (setq mark-active nil + line-count (count-lines (point) end)) + (when (> (point) end) + (let ((start (point))) + (goto-char end) + (setq end start)))) + ;; find the indentation column of each line + (save-excursion + (dotimes (_ line-count) + (back-to-indentation) + (push (- (point) (pos-bol)) indent-cols) + (forward-line)) + (cl-callf nreverse indent-cols)) + (cl-loop repeat line-count + for indent-col in indent-cols + for bol = (pos-bol) + do (back-to-indentation) + ;; skip this line if the indentation has changed + when (= (- (point) bol) indent-col) do + (lisp-indent-adjust-parens) + ;; if the indent failed, stop + (when (= (- (point) bol) indent-col) + (cl-return)) + do (forward-line))))) + (defun my/lisp-dedent-adjust-parens () + "Like `lisp-dedent-adjust-parens', but got to first char on line first. +Also, this works even if the region is active (it just jumps to the first line +in the region and indents once)." (interactive) - (back-to-indentation) - (lisp-dedent-adjust-parens)) - (evil-define-key 'normal adjust-parens-mode-map - (kbd "") #'my/normal-state-lisp-indent-adjust-parens - (kbd "") #'my/normal-state-lisp-dedent-adjust-parens)) + (save-mark-and-excursion + (let ((end (mark t))) + (when (and (region-active-p) end) + (setq mark-active nil) + (when (> (point) end) + (goto-char end)))) + (back-to-indentation) + (lisp-dedent-adjust-parens))) + (evil-define-key '(normal visual) adjust-parens-mode-map + (kbd "") #'my/lisp-indent-adjust-parens + (kbd "") #'my/lisp-dedent-adjust-parens)) ;; for when the files are just too large (use-package vlf