From 818cfc03800831db4bf734ee089ffb3c0a5e9e3c Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 1 May 2022 09:11:41 -0500 Subject: Return point to the same point relative of defun in +init-sort --- lisp/+init.el | 85 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/lisp/+init.el b/lisp/+init.el index a4c3b5a..8f999f9 100644 --- a/lisp/+init.el +++ b/lisp/+init.el @@ -28,44 +28,55 @@ within that group, forms with a HEAD of `:require' are sorted first, and `:straight' HEADs are sorted last. All other forms are sorted lexigraphically." (interactive) - (save-excursion - (save-restriction - (widen) - (+lisp-sort-sexps - (point-min) (point-max) - ;; Key function - nil - ;; Sort function - (lambda (s1 s2) - (let ((s1 (cdr s1)) (s2 (cdr s2))) - (cond - ;; Sort everything /not/ `setup' /before/ `setup' - ((and (+init--sexp-setup-p s1) - (not (+init--sexp-setup-p s2))) - nil) - ((and (+init--sexp-setup-p s2) - (not (+init--sexp-setup-p s1))) - t) - ;; otherwise... - (t (let ((s1-straight (+init--sexp-setup-p s1 :straight)) - (s2-straight (+init--sexp-setup-p s2 :straight)) - (s1-require (+init--sexp-setup-p s1 :require)) - (s2-require (+init--sexp-setup-p s2 :require))) + ;; I have to make my own "version" of `save-excursion', since the mark and + ;; point are lost (I think that's the problem) when sorting the buffer. + (let* ((current-point (point)) + (current-defun (beginning-of-defun)) + (defun-point (- current-point (point))) + (current-defun-re (buffer-substring-no-properties (line-beginning-position) + (line-end-position)))) + (widen) ; It makes no sense to `save-restriction' + (+lisp-sort-sexps + (point-min) (point-max) + ;; Key function + nil + ;; Sort function + (lambda (s1 s2) + (let ((s1 (cdr s1)) (s2 (cdr s2))) (cond - ;; `:straight' setups have extra processing - ((and s1-straight s2-straight) - (let* ((r (rx (: ":straight" (? "-when") (* space) (? "(")))) - (s1 (replace-regexp-in-string r "" s1)) - (s2 (replace-regexp-in-string r "" s2))) - (string< s1 s2))) - ;; `:require' setups go first - ((and s1-require (not s2-require)) t) - ((and s2-require (not s1-require)) nil) - ;; `:straight' setups go last - ((and s1-straight (not s2-straight)) nil) - ((and s2-straight (not s1-straight)) t) - ;; otherwise, sort lexigraphically - (t (string< s1 s2)))))))))))) + ;; Sort everything /not/ `setup' /before/ `setup' + ((and (+init--sexp-setup-p s1) + (not (+init--sexp-setup-p s2))) + nil) + ((and (+init--sexp-setup-p s2) + (not (+init--sexp-setup-p s1))) + t) + ;; otherwise... + (t (let ((s1-straight (+init--sexp-setup-p s1 :straight)) + (s2-straight (+init--sexp-setup-p s2 :straight)) + (s1-require (+init--sexp-setup-p s1 :require)) + (s2-require (+init--sexp-setup-p s2 :require))) + (cond + ;; `:straight' setups have extra processing + ((and s1-straight s2-straight) + (let* ((r (rx (: ":straight" (? "-when") (* space) (? "(")))) + (s1 (replace-regexp-in-string r "" s1)) + (s2 (replace-regexp-in-string r "" s2))) + (string< s1 s2))) + ;; `:require' setups go first + ((and s1-require (not s2-require)) t) + ((and s2-require (not s1-require)) nil) + ;; `:straight' setups go last + ((and s1-straight (not s2-straight)) nil) + ((and s2-straight (not s1-straight)) t) + ;; otherwise, sort lexigraphically + (t (string< s1 s2))))))))) + ;; Return to original point relative to the defun we were in + (goto-char (point-min)) + (re-search-forward current-defun-re) + (beginning-of-defun) + (goto-char (+ (point) defun-point)) + )) (defun +init-sort-then-save () "Sort init.el, then save it." -- cgit 1.4.1-21-gabe81