[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From: Marcin Borkowski
Subject: bug#21072: 24.5; inconsistent behaviour of `C-M-h (mark-defun)' in Emacs Lisp
Date: Sun, 27 Nov 2016 08:40:06 +0100
User-agent: mu4e 0.9.17; emacs

On 2016-11-04, at 08:48, Marcin Borkowski <mbork@mbork.pl> wrote:

> Very nice trick with the 'last-command, thanks!  I included this in my
> code.  I will also write some tests for that (it seems to work, but...)
> and send the code soon.

Hi Drew, hi all,

so it turned out that my code only seemed to work.  (The last-command
trick was fine, but what I thought corrected the bob bug only introduced
another one.)  Here's the (yet another) version.  (I'm not sharing my
tests yet - I haven't yet written any tests covering the last-command
trick.)  I'd be very thankful for any feedback (although I will have
even less time for Emacs bug work during the next two months - not only
my son needs considerable time and effort, the same goes for my

--8<---------------cut here---------------start------------->8---
(defun in-comment-line-p ()
  "Return non-nil if the point is in a comment line.
See http://lists.gnu.org/archive/html/help-gnu-emacs/2016-08/msg00141.html";
    (forward-line 0)
    (unless (looking-at "^\\s-*$")
      (< (line-end-position)
         (let ((ppss (syntax-ppss)))
           (when (nth 4 ppss)
             (goto-char (nth 8 ppss)))
           (forward-comment (point-max))

(defun beginning-of-defun-comments (&optional arg)
  "Move to the beginning of ARGth defun, including comments."
  (interactive "^p")
  (unless arg (setq arg 1))
  (beginning-of-defun arg)
  (let (nbobp)
    (while (progn
             (setq nbobp (zerop (forward-line -1)))
             (and (in-comment-line-p)
    (when nbobp
      (forward-line 1))))

(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; with negative
ARG, change the direction of marking.

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))
  ;; Trick with 'mark-defun-back due to Drew Adams
  (when (eq last-command 'mark-defun-back)
    (setq arg (- arg)))
  (when (< arg 0)
    (setq this-command 'mark-defun-back))
  (cond ((use-region-p)
         (if (>= arg 0)
                (goto-char (mark))
                ;; change the dotimes below to (end-of-defun arg) once bug 
#24427 is fixed
                (dotimes (_ignore arg)
           (beginning-of-defun-comments (- arg))))
         (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.
           (setq beg (point))
           (setq end (point))
           (when (or (and (<= (point) opoint)
                          (> arg 0))
                     (= beg (point-min))) ; we were before the first defun!
             ;; 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)
             (setq end (point))
             (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)
                  (setq end (point))
                  (push-mark end nil t)
                  (goto-char beg))
                  (goto-char beg)
                  (beginning-of-defun (1- (- arg)))
                  (push-mark end nil t))))))
  (let (nbobp)
    (while (progn
             (setq nbobp (zerop (forward-line -1)))
             (and (looking-at "^\\s-*$")
    (when nbobp
      (forward-line 1))))
--8<---------------cut here---------------end--------------->8---


Marcin Borkowski

reply via email to

[Prev in Thread] Current Thread [Next in Thread]