From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.bugs 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 Message-ID: <87wpfppe09.fsf@mbork.pl> References: <55A74B3C.8030608@gmail.com> <87k2jm2bay.fsf@amu.edu.pl> <83r3dln1ez.fsf@gnu.org> <87y47tzn0v.fsf@mbork.pl> <83lh3tmyzv.fsf@gnu.org> <87twihzhni.fsf@mbork.pl> <83fuu1mu25.fsf@gnu.org> <87eg9jynn0.fsf@mbork.pl> <83h9edkhjs.fsf@gnu.org> <874mabe5h6.fsf@mbork.pl> <83twibfil2.fsf@gnu.org> <87y47mcywp.fsf@mbork.pl> <87bmyrvzh1.fsf@mbork.pl> <878ttux198.fsf@mbork.pl> <366cfd22-c35c-4980-bc81-7f6df6c180a0@default> <87shra8hnz.fsf@mbork.pl> <87k2cj8z3w.fsf@mbork.pl> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1480232475 20480 195.159.176.226 (27 Nov 2016 07:41:15 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 27 Nov 2016 07:41:15 +0000 (UTC) User-Agent: mu4e 0.9.17; emacs 26.0.50.3 Cc: rfflrccrd@gmail.com, 21072@debbugs.gnu.org To: Drew Adams Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Nov 27 08:41:10 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cAu5F-0004c9-RI for geb-bug-gnu-emacs@m.gmane.org; Sun, 27 Nov 2016 08:41:10 +0100 Original-Received: from localhost ([::1]:53191 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cAu5J-0001Oa-Fs for geb-bug-gnu-emacs@m.gmane.org; Sun, 27 Nov 2016 02:41:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45156) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cAu5B-0001OJ-0W for bug-gnu-emacs@gnu.org; Sun, 27 Nov 2016 02:41:06 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cAu57-0006tS-U1 for bug-gnu-emacs@gnu.org; Sun, 27 Nov 2016 02:41:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:55704) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cAu57-0006sn-Qv for bug-gnu-emacs@gnu.org; Sun, 27 Nov 2016 02:41:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cAu57-0002eM-IO for bug-gnu-emacs@gnu.org; Sun, 27 Nov 2016 02:41:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Marcin Borkowski Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 27 Nov 2016 07:41:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 21072 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 21072-submit@debbugs.gnu.org id=B21072.148023241910127 (code B ref 21072); Sun, 27 Nov 2016 07:41:01 +0000 Original-Received: (at 21072) by debbugs.gnu.org; 27 Nov 2016 07:40:19 +0000 Original-Received: from localhost ([127.0.0.1]:42870 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cAu4R-0002dG-8w for submit@debbugs.gnu.org; Sun, 27 Nov 2016 02:40:19 -0500 Original-Received: from mail.mojserwer.eu ([195.110.48.8]:34638) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cAu4P-0002d7-Dm for 21072@debbugs.gnu.org; Sun, 27 Nov 2016 02:40:18 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id B18BC1DA037; Sun, 27 Nov 2016 08:40:15 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Zad5KLY2jmY9; Sun, 27 Nov 2016 08:40:12 +0100 (CET) Original-Received: from localhost (static-dwadziewiec-jedenpiec7.echostar.pl [109.232.29.157]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 6FF251DA02F; Sun, 27 Nov 2016 08:40:12 +0100 (CET) In-reply-to: <87k2cj8z3w.fsf@mbork.pl> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:126152 Archived-At: On 2016-11-04, at 08:48, Marcin Borkowski 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 students;-).) --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" (save-excursion (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)) (point)))))) (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) nbobp))) (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) (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 (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) (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)))))) (let (nbobp) (while (progn (setq nbobp (zerop (forward-line -1))) (and (looking-at "^\\s-*$") nbobp))) (when nbobp (forward-line 1)))) --8<---------------cut here---------------end--------------->8--- Best, -- Marcin Borkowski