unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Marcin Borkowski <mbork@mbork.pl>
To: Drew Adams <drew.adams@oracle.com>
Cc: rfflrccrd@gmail.com, 21072@debbugs.gnu.org
Subject: bug#21072: 24.5; inconsistent behaviour of `C-M-h (mark-defun)' in Emacs Lisp
Date: Wed, 02 Nov 2016 08:28:16 +0100	[thread overview]
Message-ID: <87shra8hnz.fsf@mbork.pl> (raw)
In-Reply-To: <366cfd22-c35c-4980-bc81-7f6df6c180a0@default>


On 2016-10-11, at 23:15, Drew Adams <drew.adams@oracle.com> wrote:

> 3. With this at the top of *scratch* (note the blank line at top)
>    and point between the comment and the defun, each of `M-- C-M-h'
>    and `C-M-h' seems to loop indefinitely.
>
> -------------8<----------------
>
> ;; This buffer is for notes you don't want to save, and for Lisp evaluation.
> ;; If you want to create a file, visit that file with C-x C-f,
> ;; then enter the text in that file's own buffer.
>
> (defun a ()
>   nil)
> -------------8<----------------
>
> 4. And with the same thing, but without the blank line at the top,
>    both `M-- C-M-h' and `C-M-h' select the defun plus the comment,
>    except that they do not select the first comment line.  Intended?

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

--8<---------------cut here---------------start------------->8---

(defun mark-defun (&optional arg)
  "Put mark at end of this defun, point at beginning.
The defun marked is the one that contains point or follows point.
With positive ARG, mark this and that many next defuns.

If the mark is active, it marks the next defun after the one(s)
already marked.  With positive ARG, mark that many more defuns.
With negative ARG, mark that many more previous defuns."
  (interactive "p")
  (setq arg (or arg 1))
  (cond ((use-region-p)
         (if (>= arg 0)
             (set-mark
              (save-excursion
                (goto-char (mark))
                ;; change the dotimes below to (end-of-defun arg) once bug #24427 is fixed
                (dotimes (_ignore arg)
                  (end-of-defun))
                (point)))
           (beginning-of-defun-comments (- arg))))
        (t
         (let ((opoint (point))
               beg end)
           (push-mark opoint)
           ;; Try first in this order for the sake of languages with nested
           ;; functions where several can end at the same place as with the
           ;; offside rule, e.g. Python.
           (beginning-of-defun-comments)
           (setq beg (point))
           (end-of-defun)
           (setq end (point))
           (when (and (<= (point) opoint)
                      (> arg 0))
             ;; beginning-of-defun moved back one defun so we got the wrong
             ;; one.  If ARG < 0, however, we actually want to go back.
             (goto-char opoint)
             (end-of-defun)
             (setq end (point))
             (beginning-of-defun-comments)
             (setq beg (point)))
           (goto-char beg)
           (cond ((> arg 0)
                  ;; change the dotimes below to (end-of-defun arg) once bug #24427 is fixed
                  (dotimes (_ignore arg)
                    (end-of-defun))
                  (setq end (point))
                  (push-mark end nil t)
                  (goto-char beg))
                 (t
                  (goto-char beg)
                  (beginning-of-defun (1- (- arg)))
                  (push-mark end nil t))))))
  (while (progn
           (forward-line -1)
           (and (looking-at "^\\s-*$")
                (not (bobp)))))
  (or (bobp) (forward-line 1)))
--8<---------------cut here---------------end--------------->8---

> 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?  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.

Best,

-- 
Marcin Borkowski





  parent reply	other threads:[~2016-11-02  7:28 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-16  6:12 bug#21072: 24.5; inconsistent behaviour of `C-M-h (mark-defun)' in Emacs Lisp Raffaele Ricciardi
2016-04-25 11:11 ` Marcin Borkowski
2016-05-01 17:17   ` Eli Zaretskii
2016-05-01 17:49     ` Marcin Borkowski
2016-05-01 18:09       ` Eli Zaretskii
2016-05-01 19:45         ` Marcin Borkowski
2016-05-01 19:56           ` Eli Zaretskii
2016-05-03 18:58             ` Marcin Borkowski
2016-05-04 14:45               ` Eli Zaretskii
2016-05-06 12:27                 ` Marcin Borkowski
2016-05-06 12:59                   ` Eli Zaretskii
2016-05-07  3:47                     ` Marcin Borkowski
2016-05-07  5:07                       ` Drew Adams
2016-10-11 12:31                         ` Marcin Borkowski
2016-10-11 15:30                           ` Drew Adams
2016-10-11 17:07                             ` Marcin Borkowski
2016-10-11 17:52                               ` Clément Pit--Claudel
2016-10-11 20:26                               ` Drew Adams
2016-10-11 21:15                               ` Drew Adams
2016-10-28  5:35                                 ` Marcin Borkowski
2016-10-28 14:32                                   ` Drew Adams
2016-11-02  7:28                                 ` Marcin Borkowski [this message]
2016-11-02 18:25                                   ` Drew Adams
2016-11-04  7:48                                     ` Marcin Borkowski
2016-11-27  7:40                                       ` Marcin Borkowski
2016-11-27 18:51                                         ` Drew Adams
2017-02-07  6:22                                           ` Marcin Borkowski
2017-02-07 16:14                                             ` Drew Adams
2016-05-07  6:47                       ` Eli Zaretskii
2016-06-05  6:30                         ` Marcin Borkowski
2016-06-05  7:01                           ` bug#21072: Forgotten attachment (was: bug#21072: 24.5; inconsistent behaviour of `C-M-h (mark-defun)' in Emacs Lisp) Marcin Borkowski
2016-06-09 11:56                           ` bug#21072: 24.5; inconsistent behaviour of `C-M-h (mark-defun)' in Emacs Lisp Marcin Borkowski
2016-06-21  7:58                             ` Marcin Borkowski
2016-06-21  9:05                               ` Andreas Röhler
2016-06-21 10:07                                 ` Marcin Borkowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87shra8hnz.fsf@mbork.pl \
    --to=mbork@mbork.pl \
    --cc=21072@debbugs.gnu.org \
    --cc=drew.adams@oracle.com \
    --cc=rfflrccrd@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).