emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/paredit 18cf19f 164/224: Implement `paredit-preserving-col


From: ELPA Syncer
Subject: [nongnu] elpa/paredit 18cf19f 164/224: Implement `paredit-preserving-column'.
Date: Sat, 7 Aug 2021 09:22:42 -0400 (EDT)

branch: elpa/paredit
commit 18cf19f4d12dbe95b952b895c85579bb41801602
Author: Taylor R Campbell <campbell@mumble.net>
Commit: Taylor R Campbell <campbell@mumble.net>

    Implement `paredit-preserving-column'.
---
 paredit.el | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/paredit.el b/paredit.el
index e2d3f78..80d83d5 100644
--- a/paredit.el
+++ b/paredit.el
@@ -173,6 +173,21 @@ Signal an error if no clause matches."
 
   (put 'paredit-ignore-sexp-errors 'lisp-indent-function 0)
 
+  (defmacro paredit-preserving-column (&rest body)
+    "Evaluate BODY and restore point to former column, relative to code.
+Assumes BODY will change only indentation.
+If point was on code, it moves with the code.
+If point was on indentation, it stays in indentation."
+    (let ((column (make-symbol "column"))
+          (indentation (make-symbol "indentation")))
+      `(let ((,column (current-column))
+             (,indentation (paredit-current-indentation)))
+         (let ((value (progn ,@body)))
+           (paredit-restore-column ,column ,indentation)
+           value))))
+
+  (put 'paredit-preserving-column 'lisp-indent-function 0)
+
   nil)
 
 ;;;; Minor Mode Definition
@@ -2611,6 +2626,20 @@ This is independent of context -- it doesn't check what 
state the
   (save-excursion
     (back-to-indentation)
     (current-column)))
+
+(defun paredit-restore-column (column indentation)
+  ;; Preserve the point's position either in the indentation or in the
+  ;; code: if on code, move with the code; if in indentation, leave it
+  ;; in the indentation, either where it was (if still on indentation)
+  ;; or at the end of the indentation (if the code moved far enough
+  ;; left).
+  (let ((indentation* (paredit-current-indentation)))
+    (goto-char
+     (+ (point-at-bol)
+        (cond ((not (< column indentation))
+               (+ column (- indentation* indentation)))
+              ((<= indentation* column) indentation*)
+              (t column))))))
 
 ;;;; Initialization
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]