Subject: RE: bug#21072: 24.5; inconsistent behaviour of `C-M-h (mark-defun)' in Emacs Lisp

From: Drew Adams <[email-masked]>
Date: Wed, 2 Nov 2016 11:25:20 -0700 (PDT)

> Well, both these behaviors are manifestations of the same bug.
> Below is the corrected version.  (And below that a question.)

The test cases I mentioned work now.  Thx.  I didn't try anything
beyond those cases.  Hopefully others will test a bit more.

> > 6. Interactively, I would rather see repeated use of `C-M-h',
> >    after an initial use of `C-M-h' with a negative prefix arg 
> >    (e.g. `M-- C-M-h'), continue to select defuns backward.
> >    IOW, not need to use `M--' explicitly for each `C-M-h'.
> >
> >    You can just hold down `C-M-h', to select multiple defuns
> >    forward.  I would like to be able to do the same thing,
> >    but backward, by using `M-- C-M-h C-M-h C-M-h C-M-h...'
> >    (just hold down the chord).
> >
> >    If you do that, then a negative prefix arg should not mean
> >    backward; it should just mean change direction (backward if
> >    previous command was not `mark-defun').
> 
> Just to be sure: you mean only the minus sign as argument, not
> a negative number?

No, not really.  But use your own judgment, I guess.

This is the kind of behavior I had in mind.  This is for
`transpose-sexps', but it shows the behavior.  _Any_ negative
arg flips the direction.  At the outset, a negative arg means
move backward.  The absolute value of ARG is the number of
sexps to move over.

(defun reversible-transpose-sexps (arg)
  "Reversible and repeatable `transpose-sexps'.
Like `transpose-sexps', but:
 1. Leaves point after the moved sexp.
 2. When repeated, a negative prefix arg flips the direction."
  (interactive "p")
  (when (eq last-command 'rev-transp-sexps-back) (setq arg  (- arg)))
  (transpose-sexps arg)
  (unless (natnump arg)
    (backward-sexp (abs arg))
    (skip-syntax-backward " .")
    (setq this-command  'rev-transp-sexps-back)))

(If you happen to try this with ARG=0, be aware that what you
see is just the peculiar `transpose-sexps' behavior for ARG=0.
This is not related to the code here.)

> I'm also wondering whether to allow that for
> non-interactive use, too: I'm pretty sure nobody would want to call
> (mark-defun '-) from Lisp code, and it might make testing slightly
> easier.

I think the behavior should be the same.  But see above.  The
arg passed should be numeric (positive, zero, or negative), IMO.