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

From: Zachary Kanfer <[email-masked]>
Date: Sun, 23 Apr 2023 01:33:44 -0400

Attached is a patch with a few updates:

1. Added documentation in doc/emacs/mark.texi, under the mark-sexp section.
I'm unfamiliar with standards for how the manuals are written, but am happy
to update further as needed.
2. Extracted a new helper function mark--helper. This takes two arguments
-- a move function, and a number of how many things to move.

The helper function is able to be used for all mark-*-forward and
mark-*-backward functions; I think this should handle at least some of your
concerns about the composability of the design -- different mark functions
can be made by passing a different move function. For example,
mark-word-forward and mark-word-backward would be defined this way:

(defun mark-word-forward (&optional number-of-words)
  "Mark NUMBER-OF-WORDS words forward.

 Repeated calls to this mark more words."
  (interactive "p")
  (mark--helper #'forward-word (or number-of-words 1)))

(defun mark-word-backward (&optional number-of-words)
  "Mark NUMBER-OF-WORDS words backward.

 Repeated calls to this mark more words."
  (interactive "p")
  (mark--helper #'forward-word (- (or number-of-words 1))))

I'm not exactly sure of the best place to put the helper function, nor
exactly how the different lisp files in Emacs work together. There's no
provide statement; are all the files in lisp/emacs-lisp loaded at the same
time? If so, I'll make the other relevant functions (for marking word,
defun, page, paragraph, line, and char).

Also, whatever the outcome of this patch, I think it would be advisable to
explain somewhere how mark-sexp extends region by the end of region
opposite point; this is not in the docstring or any other documentation
I've seen.

On Fri, Apr 21, 2023 at 9:10 AM Stefan Monnier <[email-masked]>
wrote:

> > 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'
>
> Agreed: we should move towards a more orthogonal/composable design,
> where the granularity of the operation (char, word, line, sexp,
> paragraph, page, defun, ...) and the operation itself (move, mark) and
> the direction and all independent such that we can use any combination
> of them.
>
> Both at the ELisp level and at the key-binding level.
>
>
>         Stefan
>
>
From ec5fb0b09dc05d5726874c316de0028a9a4c46b3 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

---
 doc/emacs/mark.texi     |  8 +++++++
 etc/NEWS                |  4 ++++
 lisp/emacs-lisp/lisp.el | 46 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)

diff --git a/doc/emacs/mark.texi b/doc/emacs/mark.texi
index 3f1c76c1591..9c230a72252 100644
--- a/doc/emacs/mark.texi
+++ b/doc/emacs/mark.texi
@@ -224,6 +224,14 @@ Marking Objects
 positive or negative numeric arguments move the mark forward or
 backward by the specified number of expressions.
 
+@findex mark-sexp-forward
+  @code{mark-sexp-forward} is similar to @code{mark-sexp}, but only
+  moves forward.
+
+@findex mark-sexp-backward
+  @code{mark-sexp-backward} is similar to @code{mark-sexp}, but only
+  moves backward.
+
    The other commands in the above list set both point and mark, so as
 to delimit an object in the buffer.  @kbd{M-h} (@code{mark-paragraph})
 marks paragraphs (@pxref{Paragraphs}), @kbd{C-M-h} (@code{mark-defun})
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..30194354f19 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -129,6 +129,52 @@ mark-sexp
 	    (point))
 	  nil t))))
 
+(defun mark--helper (move-fn number-of-things)
+  "Use MOVE-FN to move NUMBER-OF-THINGS things, extending region over them.
+
+The MOVE-FN should take a numeric argument, and move that many
+items forward (negative means backward).
+
+NUMBER-OF-THINGS is the number of additional things to move."
+  (if (use-region-p)
+      (let* ((forward (>= number-of-things 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
+                    (funcall move-fn number-of-things)
+                  (scan-error (user-error "No more in this direction!")))
+                (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)))
+    (progn (push-mark (save-excursion
+                        (funcall move-fn number-of-things)
+                        (point)))
+           (activate-mark))))
+
+(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--helper #'forward-sexp (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--helper #'forward-sexp (- (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