emacs-devel
[Top][All Lists]
Advanced

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

New command: move-past-close


From: Ivan Sokolov
Subject: New command: move-past-close
Date: Sat, 06 Nov 2021 01:59:31 +0300

Recently found move-past-close-and-reindent, useful indeed, but I
usually don't need reindent functionality, so I added a non-indenting
command.

I also replaced nth/elt usages in lisp.el with proper ppss accessors.

>From d2d40fa3f071c2137ea7686114a550ec0bb7c2ff Mon Sep 17 00:00:00 2001
From: Ivan Sokolov <ivan-p-sokolov@ya.ru>
Date: Sat, 6 Nov 2021 01:40:59 +0300
Subject: [PATCH 1/2] Use ppss accessors instead of nth and elt

* lisp/emacs-lisp/lisp.el: Use ppss accessors instead of nth and elt.
---
 lisp/emacs-lisp/lisp.el | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index 9b38d86e2c..e9932df8d0 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -227,13 +227,14 @@ up-list
             ;; string.
             (when no-syntax-crossing
               (let* ((syntax (syntax-ppss))
-                     (string-comment-start (nth 8 syntax)))
+                     (string-comment-start
+                      (ppss-comment-or-string-start syntax)))
                 (when string-comment-start
                   (save-excursion
                     (goto-char string-comment-start)
                     (narrow-to-region
                      (point)
-                     (if (nth 3 syntax) ; in string
+                     (if (ppss-string-terminator syntax) ; in string
                          (condition-case nil
                              (progn (forward-sexp) (point))
                            (scan-error (point-max)))
@@ -258,8 +259,8 @@ up-list
             ;; or end of that string.
             (and escape-strings
                  (or syntax (setf syntax (syntax-ppss)))
-                 (nth 3 syntax)
-                 (goto-char (nth 8 syntax))
+                 (ppss-string-terminator syntax)
+                 (goto-char (ppss-comment-or-string-start syntax))
                  (progn (when (> inc 0)
                           (forward-sexp))
                         t))
@@ -269,8 +270,8 @@ up-list
             ;; end of the comment.
             (and no-syntax-crossing
                  (or syntax (setf syntax (syntax-ppss)))
-                 (nth 4 syntax)
-                 (goto-char (nth 8 syntax))
+                 (ppss-comment-depth syntax)
+                 (goto-char (ppss-comment-or-string-start syntax))
                  (or (< inc 0)
                      (forward-comment 1))
                  (setf arg (+ arg inc)))
@@ -407,7 +408,7 @@ beginning-of-defun-raw
                                       "\\(?:" defun-prompt-regexp "\\)\\s(")
                             "^\\s(")
                                              nil 'move arg))
-                    (nth 8 (syntax-ppss))))
+                    (ppss-comment-or-string-start (syntax-ppss))))
            found)
         (progn (goto-char (1- (match-end 0)))
                 t)))
@@ -435,8 +436,8 @@ beginning-of-defun-raw
              encl-pos)
          ;; Back out of any comment/string, so that encl-pos will always
          ;; become nil if we're at top-level.
-         (when (nth 8 ppss)
-           (goto-char (nth 8 ppss))
+         (when (ppss-comment-or-string-start ppss)
+           (goto-char (ppss-comment-or-string-start ppss))
            (setq ppss (syntax-ppss)))  ; should be fast, due to cache.
          (setq encl-pos (syntax-ppss-toplevel-pos ppss))
          (if encl-pos (goto-char encl-pos))
@@ -471,10 +472,10 @@ beginning-of-defun--in-emptyish-line-p
   (save-excursion
     (forward-line 0)
     (let ((ppss (syntax-ppss)))
-      (and (null (nth 3 ppss))
+      (and (null (ppss-string-terminator ppss))
            (< (line-end-position)
-              (progn (when (nth 4 ppss)
-                       (goto-char (nth 8 ppss)))
+              (progn (when (ppss-comment-depth ppss)
+                       (goto-char (ppss-comment-or-string-start ppss)))
                      (forward-comment (point-max))
                      (point)))))))
 
@@ -486,9 +487,10 @@ beginning-of-defun-comments
   (let (first-line-p)
     (while (let ((ppss (progn (setq first-line-p (= (forward-line -1) -1))
                               (syntax-ppss (line-end-position)))))
-             (while (and (nth 4 ppss) ; If eol is in a line-spanning comment,
-                         (< (nth 8 ppss) (line-beginning-position)))
-               (goto-char (nth 8 ppss)) ; skip to comment start.
+             (while (and (ppss-comment-depth ppss) ; If eol is in a 
line-spanning comment,
+                         (< (ppss-comment-or-string-start ppss)
+                            (line-beginning-position)))
+               (goto-char (ppss-comment-or-string-start ppss)) ; skip to 
comment start.
                (setq ppss (syntax-ppss (line-end-position))))
              (and (not first-line-p)
                   (progn (skip-syntax-backward
@@ -893,7 +895,8 @@ move-past-close-and-reindent
                      (setq state (parse-partial-sexp (point) end nil nil
                                                      state))
                      ;; Check not in string or comment.
-                     (and (not (elt state 3)) (not (elt state 4))))))))
+                     (and (not (ppss-string-terminator state))
+                          (not (ppss-comment-depth state))))))))
     (delete-indentation))
   (forward-char 1)
   (newline-and-indent))
-- 
2.33.1

>From 64256cf84a67c5109b1e9e8603ebf2cecdb515fa Mon Sep 17 00:00:00 2001
From: Ivan Sokolov <ivan-p-sokolov@ya.ru>
Date: Sat, 6 Nov 2021 01:54:01 +0300
Subject: [PATCH 2/2] Add move-past-close

* lisp/emacs-lisp/lisp.el: add move-past-close.
---
 lisp/emacs-lisp/lisp.el | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index e9932df8d0..606a3be425 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -872,6 +872,12 @@ raise-sexp
 (defun move-past-close-and-reindent ()
   "Move past next `)', delete indentation before it, then indent after it."
   (interactive)
+  (move-past-close)
+  (newline-and-indent))
+
+(defun move-past-close ()
+  "Move past next `)' and delete indentation before it."
+  (interactive)
   (up-list 1)
   (forward-char -1)
   (while (save-excursion               ; this is my contribution
@@ -887,10 +893,10 @@ move-past-close-and-reindent
                          state)
                      (beginning-of-line)
                      ;; Get state at start of line.
-                     (setq state  (list 0 nil nil
-                                        (null (calculate-lisp-indent))
-                                        nil nil nil nil
-                                        nil))
+                     (setq state
+                           (make-ppss
+                            :depth 0
+                            :string-terminator (null (calculate-lisp-indent))))
                      ;; Parse state across the line to get state at end.
                      (setq state (parse-partial-sexp (point) end nil nil
                                                      state))
@@ -898,8 +904,7 @@ move-past-close-and-reindent
                      (and (not (ppss-string-terminator state))
                           (not (ppss-comment-depth state))))))))
     (delete-indentation))
-  (forward-char 1)
-  (newline-and-indent))
+  (forward-char 1))
 
 (defun check-parens ()                 ; lame name?
   "Check for unbalanced parentheses in the current buffer.
-- 
2.33.1


reply via email to

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