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

From: Zachary Kanfer <[email-masked]>
Date: Wed, 3 May 2023 02:10:01 -0400

> Thanks for the patch.  It would be nice to have such commands
> even not bound to default keys, so the users are free to bind
> them to any keys.

That's my hope, unless sufficiently good default keys can be found. I like
C-M-space and C-M-S-space, which are both currently bound to mark-sexp, but
changing the default keys is not trivially done.

> So looks like the best place to define the helper is simple.el,
> before mark-word.

Sure! I'll get a patch out for this.

> Another variant is to use a single argument JUMPFORM like
> in 'isearch-yank-internal' that allows not to leak the
> prefix argument to the helper function:

This is tempting. The downside I'm seeing to this -- which I'll think more
about to see if I can get around it -- is that we don't know if we need to
call JUMPFORM from point or mark.

We can do it from both, something like:

(defun mark--helper (fn-to-find-new-mark)
  "Extend region by calling FN-TO-FIND-NEW-MARK.

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* ((beginning-of-region (region-beginning))
             (end-of-region (region-end))
             (at-end-of-region (= end-of-region (point)))
             (move-from-front (save-excursion (goto-char
beginning-of-region)
                                              (funcall fn-to-find-new-mark)
                                              (point)))
             (move-from-end (save-excursion (goto-char end-of-region)
                                            (funcall fn-to-find-new-mark)
                                            (point)))
             (new-beginning-of-region (min beginning-of-region
move-from-front))
             (new-end-of-region (max end-of-region move-from-end)))
        (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 fn-to-find-new-mark)
                        (point)))
           (activate-mark))))

Downsides include:

1. We have to call the function twice each time. This doesn't seem like
such a big deal unless it's expensive or has side effects -- neither of
which should be the case.
2. The current implementation errors when there are no more objects to
mark. This doesn't. I think we probably could error if we don't change the
region.
3. I'm not 100% convinced this will always do the right thing. I would like
to be.
4. I'm not sure going to one argument is worth it. The two arguments seem
pretty simple; changing to one argument might add more complexity than it
removes.

On Fri, Apr 28, 2023 at 1:07 PM Juri Linkov <[email-masked]> wrote:

> > Attached is a patch with a few updates:
>
> Thanks for the patch.  It would be nice to have such commands
> even not bound to default keys, so the users are free to bind
> them to any keys.
>
> > 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).
>
> Let's see:
> - mark-sexp and mark-defun are defined in emacs-lisp/lisp.el
> - mark-page in textmodes/page.el
> - mark-paragraph in textmodes/paragraphs.el
> - mark-word in simple.el
>
> So looks like the best place to define the helper is simple.el,
> before mark-word.
>
> > +(defun mark--helper (move-fn number-of-things)
>
> A nicer name would be 'mark-thing' as a reference to thingatpt.el.
>
> > +  "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."
>
> Another variant is to use a single argument JUMPFORM like
> in 'isearch-yank-internal' that allows not to leak the
> prefix argument to the helper function:
>
>   (defun isearch-yank-char (&optional arg)
>     (isearch-yank-internal (lambda () (forward-char arg) (point))))
>