From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#73172: [PATCH] Move to start of current header in diff-{file, hunk}-prev Date: Tue, 10 Sep 2024 14:40:04 -0400 Message-ID: Reply-To: Spencer Baugh Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12600"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Stefan Monnier , Dmitry Gutov To: 73172@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Sep 10 20:41:23 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1so5nn-0003A7-A7 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 10 Sep 2024 20:41:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1so5nQ-00065J-P5; Tue, 10 Sep 2024 14:41:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1so5nN-000655-To for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 14:40:58 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1so5nN-0002lu-JE for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 14:40:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=HPmXmKrRarZ+CsVvH/qjurQ0MEuROL7BMI26dsEr0YU=; b=u6GOyzmJTamYoWhJX/5hh4TvHLDUn/QqWsuwih9zml4sBGmDpuoAV1tbHfCiwVmTZwuV+8Zm3d4p/PtG7LMsE0JLSdgzS/5yk4rc0/Bl7vT+DpCGFHX8SlxmVWTsdDs6HpK0i/QUu053IR8oyy8SdZvI6j/fZxhJo4jOvn594qQGibkERIxdl8hcb2sP9IBOw66PTeWiX1DZnN6h8zFqqTxAdTnief00ClFb2K/TY4r0YlO4lAjincAwnLT1EDjlp3pMhRkPfPgVI/S/2Htr8kpWB0X7XvGmTpxpWCOz/JdWLOVtEK92SYS4JTxwq4mWvr4bZO1kGcLR3RMDdg/xog==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1so5nS-0003id-GI for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 14:41:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Spencer Baugh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 10 Sep 2024 18:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 73172 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.172599361814217 (code B ref -1); Tue, 10 Sep 2024 18:41:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 10 Sep 2024 18:40:18 +0000 Original-Received: from localhost ([127.0.0.1]:37013 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1so5mj-0003hE-AX for submit@debbugs.gnu.org; Tue, 10 Sep 2024 14:40:18 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:42850) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1so5mh-0003eV-5K for submit@debbugs.gnu.org; Tue, 10 Sep 2024 14:40:16 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1so5ma-00061O-Ry for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 14:40:08 -0400 Original-Received: from mxout1.mail.janestreet.com ([38.105.200.78]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1so5mX-0002iP-R7 for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 14:40:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=waixah; t=1725993604; bh=HPmXmKrRarZ+CsVvH/qjurQ0MEuROL7BMI26dsEr0YU=; h=From:To:Cc:Subject:Date; b=uwhAT5AdkRX7Yi3d0Ia/eNjucQ7OPfbX8gWAXtpbe5Snm4zsSf68SHC0DqRTGG1Gl Wv+I4DVIrbM82nkIfdu0/MVJcZe2soFQ2iOxqOoopd408AJwSzF+E11MbnsOZ3hSgm mNvG9xW2pidOc+lWvzfrxljSWMY8/uVGKOwMKFXhAjpeJBwAwc+f4clfJo1gMIe8NW XOwJqaaMtt8TxQqkfvjceIMT6S9eGVOVuI8oeGtYJcey0HvKmgNUT/3njGztj0QG1Q +iChcTbCebRDQ5ZDXNylpkO0kyAX2MLIdwzdIjGKMfy3iAVswHV5DlZe/4ihMURmSL ba82ecqp9UOUQ== Received-SPF: pass client-ip=38.105.200.78; envelope-from=sbaugh@janestreet.com; helo=mxout1.mail.janestreet.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:291577 Archived-At: --=-=-= Content-Type: text/plain Tags: patch (The following change is split across two patches; the first one, "move easy-mmode", fixes an unrelated FIXME, which makes the diff in the second patch simpler) If point was after a file or hunk header, the diff-file-prev and diff-hunk-prev commands would move to the start of that header. But if point was *within* the header, they would not move, and would report "No previous file" or "No previous hunk". This differs from the behavior of most other movement commands, e.g. backward-sexp or backward-sentence. This commit fixes diff-file-prev and diff-hunk-prev, as well as other easy-mmode-define-navigation BASE-prev commands. Now these commands move to the start of the containing "thing" just like other movement commands. * lisp/emacs-lisp/easy-mmode.el (easy-mmode--prev): Move to start of current match first. Also discussed here: https://lists.gnu.org/archive/html/help-gnu-emacs/2024-08/msg00367.html In GNU Emacs 29.2.50 (build 17, x86_64-pc-linux-gnu, X toolkit, cairo version 1.15.12, Xaw scroll bars) of 2024-09-06 built on igm-qws-u22796a Repository revision: e6d04c06a7eb6ce932b52a346368d02b7a811a00 Repository branch: emacs-29 Windowing system distributor 'The X.Org Foundation', version 11.0.12011000 System Description: Rocky Linux 8.10 (Green Obsidian) Configured using: 'configure --with-x-toolkit=lucid --without-gpm --without-gconf --without-selinux --without-imagemagick --with-modules --with-gif=no --with-cairo --with-rsvg --without-compress-install --with-native-compilation=aot --with-tree-sitter PKG_CONFIG_PATH=/usr/local/home/garnish/libtree-sitter/0.22.6-1/lib/pkgconfig/' --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Move-easy-mmode-define-navigation-logic-to-helper-fu.patch >From 93f50388bda9f986a5aa8e51378793031cfdce05 Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Tue, 10 Sep 2024 13:46:18 -0400 Subject: [PATCH] Move easy-mmode-define-navigation logic to helper functions The functions defined by easy-mmode-define-navigation are useful even if the easy-mmode-define-navigation macro is not used. Let's take a step towards exposing them by moving them out as helpers. This also makes the macro much easier to modify and work on. * lisp/emacs-lisp/easy-mmode.el (easy-mmode--prev) (easy-mmode--next): Add. (easy-mmode-define-navigation): Use easy-mmode--prev and easy-mmode--next. --- lisp/emacs-lisp/easy-mmode.el | 86 ++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el index a140027839e..d3dcab899d6 100644 --- a/lisp/emacs-lisp/easy-mmode.el +++ b/lisp/emacs-lisp/easy-mmode.el @@ -763,6 +763,48 @@ easy-mmode-defsyntax ;;; easy-mmode-define-navigation ;;; +(defun easy-mmode--prev (re name count &optional endfun narrowfun) + "Go to the previous COUNT'th occurence of RE. + +If none, error with NAME. + +ENDFUN and NARROWFUN are treated like in `easy-mmode-define-navigation'." + (unless count (setq count 1)) + (if (< count 0) (easy-mmode--next re name (- count) endfun narrowfun) + (let ((re-narrow (and narrowfun (prog1 (buffer-narrowed-p) (widen))))) + (unless (re-search-backward re nil t count) + (user-error "No previous %s" name)) + (when re-narrow (funcall narrowfun))))) + +(defun easy-mmode--next (re name count &optional endfun narrowfun) + "Go to the next COUNT'th occurence of RE. + +If none, error with NAME. + +ENDFUN and NARROWFUN are treated like in `easy-mmode-define-navigation'." + (unless count (setq count 1)) + (if (< count 0) (easy-mmode--prev re name (- count) endfun narrowfun) + (if (looking-at re) (setq count (1+ count))) + (let ((re-narrow (and narrowfun (prog1 (buffer-narrowed-p) (widen))))) + (if (not (re-search-forward re nil t count)) + (if (looking-at re) + (goto-char (or (if endfun (funcall endfun)) (point-max))) + (user-error "No next %s" name)) + (goto-char (match-beginning 0)) + (when (and (eq (current-buffer) (window-buffer)) + (called-interactively-p 'interactive)) + (let ((endpt (or (save-excursion + (if endfun (funcall endfun) + (re-search-forward re nil t 2))) + (point-max)))) + (unless (pos-visible-in-window-p endpt nil t) + (let ((ws (window-start))) + (recenter '(0)) + (if (< (window-start) ws) + ;; recenter scrolled in the wrong direction! + (set-window-start nil ws))))))) + (when re-narrow (funcall narrowfun))))) + (defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun &rest body) "Define BASE-next and BASE-prev to navigate in the buffer. @@ -780,53 +822,23 @@ easy-mmode-define-navigation (let* ((base-name (symbol-name base)) (prev-sym (intern (concat base-name "-prev"))) (next-sym (intern (concat base-name "-next"))) - (when-narrowed - (lambda (body) - (if (null narrowfun) body - `(let ((was-narrowed (prog1 (buffer-narrowed-p) (widen)))) - ,body - (when was-narrowed (funcall #',narrowfun))))))) + (endfun (when endfun `#',endfun)) + (narrowfun (when narrowfun `#',narrowfun))) (unless name (setq name base-name)) - ;; FIXME: Move most of those functions's bodies to helper functions! `(progn (defun ,next-sym (&optional count) ,(format "Go to the next COUNT'th %s. Interactively, COUNT is the prefix numeric argument, and defaults to 1." name) (interactive "p") - (unless count (setq count 1)) - (if (< count 0) (,prev-sym (- count)) - (if (looking-at ,re) (setq count (1+ count))) - ,(funcall when-narrowed - `(if (not (re-search-forward ,re nil t count)) - (if (looking-at ,re) - (goto-char (or ,(if endfun `(funcall #',endfun)) (point-max))) - (user-error "No next %s" ,name)) - (goto-char (match-beginning 0)) - (when (and (eq (current-buffer) (window-buffer)) - (called-interactively-p 'interactive)) - (let ((endpt (or (save-excursion - ,(if endfun `(funcall #',endfun) - `(re-search-forward ,re nil t 2))) - (point-max)))) - (unless (pos-visible-in-window-p endpt nil t) - (let ((ws (window-start))) - (recenter '(0)) - (if (< (window-start) ws) - ;; recenter scrolled in the wrong direction! - (set-window-start nil ws)))))))) - ,@body)) + (easy-mmode--next ,re ,name count ,endfun ,narrowfun) + ,@body) (put ',next-sym 'definition-name ',base) (defun ,prev-sym (&optional count) ,(format "Go to the previous COUNT'th %s. -Interactively, COUNT is the prefix numeric argument, and defaults to 1." - (or name base-name)) +Interactively, COUNT is the prefix numeric argument, and defaults to 1." name) (interactive "p") - (unless count (setq count 1)) - (if (< count 0) (,next-sym (- count)) - ,(funcall when-narrowed - `(unless (re-search-backward ,re nil t count) - (user-error "No previous %s" ,name))) - ,@body)) + (easy-mmode--prev ,re ,name count ,endfun ,narrowfun) + ,@body) (put ',prev-sym 'definition-name ',base)))) ;; When deleting these two, also delete them from loaddefs-gen.el. -- 2.39.3 --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Move-to-start-of-current-header-in-diff-file-hunk-pr.patch >From 3bf96598caae3746001fdd5d4f4a4d5a14bdf717 Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Tue, 10 Sep 2024 14:18:39 -0400 Subject: [PATCH] Move to start of current header in diff-{file,hunk}-prev If point was after a file or hunk header, the diff-file-prev and diff-hunk-prev commands would move to the start of that header. But if point was *within* the header, they would not move, and would report "No previous file" or "No previous hunk". This differs from the behavior of most other movement commands, e.g. backward-sexp or backward-sentence. This commit fixes diff-file-prev and diff-hunk-prev, as well as other easy-mmode-define-navigation BASE-prev commands. Now these commands move to the start of the containing "thing" just like other movement commands. * lisp/emacs-lisp/easy-mmode.el (easy-mmode--prev): Move to start of current match first. --- lisp/emacs-lisp/easy-mmode.el | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el index d3dcab899d6..7a94d832273 100644 --- a/lisp/emacs-lisp/easy-mmode.el +++ b/lisp/emacs-lisp/easy-mmode.el @@ -772,6 +772,17 @@ easy-mmode--prev (unless count (setq count 1)) (if (< count 0) (easy-mmode--next re name (- count) endfun narrowfun) (let ((re-narrow (and narrowfun (prog1 (buffer-narrowed-p) (widen))))) + ;; If point is inside a match for RE, move to its beginning like + ;; `backward-sexp' and other movement commands. + (when (and (not (zerop count)) + (save-excursion + ;; Make sure we're out of the current match if any. + (goto-char (if (re-search-backward re nil t 1) + (match-end 0) (point-min))) + (re-search-forward re nil t 1)) + (< (match-beginning 0) (point) (match-end 0))) + (goto-char (match-beginning 0)) + (setq count (1- count))) (unless (re-search-backward re nil t count) (user-error "No previous %s" name)) (when re-narrow (funcall narrowfun))))) -- 2.39.3 --=-=-=--