Subject: bug#62892: proposal to extend mark-sexp to go forward and backward on command

From: Zachary Kanfer <[email-masked]>
Date: Thu, 20 Apr 2023 01:25:29 -0400

> If this is to be accepted, why not extend it to all like mark functions,
> listed below?  Implementing them should be pretty similar, and you might
> even be able to reuse the same helper for all these variants.  Also, the
> helper function might need to contain a double-dash in its name because
> this is inherently a private function.

This makes sense to me, although I have not found the same need for these
to be marked. I'm happy to extend them as such; I'll add that in a later
patch.

> Please use `quote' to quote things in Emacs docstrings.  Also, both here
> and in other docstrings, the newline immediately after the first line,
> while seems necessary, is usually redundant in Emacs docstrings.  And
> there is a typo: "amrk" -> "mark".
>
> I also think you shouldn't use wildcard-like elements in quotes (in your
> case, you reuse part of the name in the quote), but I'm not sure.

Done. Updated patch attached.

On Sun, Apr 16, 2023 at 11:17 PM Ruijie Yu <[email-masked]> wrote:

>
> Zachary Kanfer <[email-masked]> writes:
>
> > Attached is a patch to add #'mark-sexp-forwards and
> #'mark-sexp-backwards. These functions extend the region by sexps forward
> and
> > backward. They are mutually incremental: you can call mark-sexp-fowards,
> then mark-sexp-backwards twice, then mark-sexp-forwards. After
> > this, the region will consist of the two sexps before point and the two
> sexps after point.
>
> If this is to be accepted, why not extend it to all like mark functions,
> listed below?  Implementing them should be pretty similar, and you might
> even be able to reuse the same helper for all these variants.  Also, the
> helper function might need to contain a double-dash in its name because
> this is inherently a private function.
>
> - `mark-defun'
> - `mark-page'
> - `mark-paragraph'
> - `mark-word'
>
> > +(defun mark-sexp-helper (number-of-expressions)
> > +  "A helper function for 'mark-sexp-[forward, backward]'.
> > +
> > +If NUMBER-OF-EXPRESSIONS is positive, mark that many sexps forward;
> > +otherwise, amrk backward."
>
> Please use `quote' to quote things in Emacs docstrings.  Also, both here
> and in other docstrings, the newline immediately after the first line,
> while seems necessary, is usually redundant in Emacs docstrings.  And
> there is a typo: "amrk" -> "mark".
>
> I also think you shouldn't use wildcard-like elements in quotes (in your
> case, you reuse part of the name in the quote), but I'm not sure.
>
> --
> Best,
>
>
> RY
>
From 5b33520a73a8f116fb89ba253f6636c9573c9d2f Mon Sep 17 00:00:00 2001
From: Zachary Kanfer <[email-masked]>
Date: Sun, 16 Apr 2023 22:16:39 -0400
Subject: [PATCH] Add mark-sexp-forward, mark-sexp-backward

---
 etc/NEWS                |  4 ++++
 lisp/emacs-lisp/lisp.el | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index c61a9ec3c5f..9f2b7d21f9b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -81,6 +81,10 @@ mistaken compositions, this will now work as well.
 This works like 'kill-matching-buffers', but without asking for
 confirmation.
 
+---
+** New commands 'mark-sexp-forward' and 'mark-sexp-backward'.
+These work like mark-sexp, but explicitly allow sexps to be marked forward and backward.
+
 
 * Changes in Specialized Modes and Packages in Emacs 30.1
 
diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index 417c218c6d7..cdd9459c436 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -129,6 +129,47 @@ mark-sexp
 	    (point))
 	  nil t))))
 
+(defun mark-sexp--helper (number-of-expressions)
+  "A helper function for `mark-sexp-forward', `mark-sexp-backward'.
+
+If NUMBER-OF-EXPRESSIONS is positive, mark that many sexps forward;
+otherwise, mark backward."
+  (if (use-region-p)
+      (let* ((forward (>= number-of-expressions 0))
+             (beginning-of-region (region-beginning))
+             (end-of-region (region-end))
+             (at-end-of-region (= end-of-region (point)))
+             (new-border-point
+              (save-excursion
+                (goto-char (if forward (region-end) (region-beginning)))
+                (condition-case nil
+                    (forward-sexp number-of-expressions)
+                  (scan-error (user-error "No more s-expressions there!")))
+                (point)))
+             (new-beginning-of-region (min beginning-of-region new-border-point))
+             (new-end-of-region (max end-of-region new-border-point)))
+        (goto-char (if at-end-of-region
+                       new-end-of-region
+                     new-beginning-of-region))
+        (set-mark (if at-end-of-region
+                      new-beginning-of-region
+                    new-end-of-region)))
+    (mark-sexp number-of-expressions)))
+
+(defun mark-sexp-forward (&optional number-of-expressions)
+  "Mark NUMBER-OF-EXPRESSIONS s-expressions forward.
+
+ Repeated calls to this mark more s-expressions."
+  (interactive "p")
+  (mark-sexp--helper (or number-of-expressions 1)))
+
+(defun mark-sexp-backward (&optional number-of-expressions)
+  "Mark NUMBER-OF-EXPRESSIONS s-expressions backward.
+
+ Repeated calls to this mark more s-expressions."
+  (interactive "p")
+  (mark-sexp--helper (- (or number-of-expressions 1))))
+
 (defun forward-list (&optional arg interactive)
   "Move forward across one balanced group of parentheses.
 This command will also work on other parentheses-like expressions
-- 
2.38.4