From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#68958: [PATCH] Support bookmarking Xref results buffers Date: Thu, 15 Feb 2024 10:28:02 +0100 Message-ID: References: <86le7wzcjj.fsf@gnu.org> <0b3f4669-180e-466f-96f3-7eeae994581f@gutov.dev> <1bea3fe4-51aa-418b-a55f-f09d0a4c558e@gutov.dev> <86il2qxg5c.fsf@mail.linkov.net> Reply-To: Eshel Yaron 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="4448"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Dmitry Gutov , Eli Zaretskii , 68958@debbugs.gnu.org To: Juri Linkov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Feb 15 10:29:05 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 1raY3F-0000vw-II for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 15 Feb 2024 10:29:05 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raY2v-0004Et-An; Thu, 15 Feb 2024 04:28:45 -0500 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 1raY2u-0004Ed-1t for bug-gnu-emacs@gnu.org; Thu, 15 Feb 2024 04:28:44 -0500 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 1raY2t-0004CK-Py for bug-gnu-emacs@gnu.org; Thu, 15 Feb 2024 04:28:43 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1raY3B-00029M-W2 for bug-gnu-emacs@gnu.org; Thu, 15 Feb 2024 04:29:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eshel Yaron Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 15 Feb 2024 09:29:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68958 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 68958-submit@debbugs.gnu.org id=B68958.17079893078212 (code B ref 68958); Thu, 15 Feb 2024 09:29:01 +0000 Original-Received: (at 68958) by debbugs.gnu.org; 15 Feb 2024 09:28:27 +0000 Original-Received: from localhost ([127.0.0.1]:54104 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1raY2c-00028N-CE for submit@debbugs.gnu.org; Thu, 15 Feb 2024 04:28:27 -0500 Original-Received: from mail.eshelyaron.com ([107.175.124.16]:48514 helo=eshelyaron.com) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1raY2a-00028D-AL for 68958@debbugs.gnu.org; Thu, 15 Feb 2024 04:28:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com; s=mail; t=1707989284; bh=oK7r/K+7kP7htLA8Pi9M1GIhmYo47HY8RwIh2GNnI5U=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=vUkMQfPEfwsaDdvyUMZtelbqc7F1INO2d39ldo85mwRqcrM+jsZuuLoSCrmK96HjQ kpOgNPXoNTjCLLnQwxN1KpK2VPoyyQJcJx16Kqfm6CCHT7GZlVIyqzgrrgUOaDjALO 2xQeF5q2fJEZsNwgJzMcepBllBxceOHpirXcs87h4+oMOandXV4EA+oKy/eatgRJF1 atRUtrWy8c/D/MLCpGKr+Cwmp5zbLhI54FydGmT0t86iL9yENGpW3xK+oboeC8FRqm 9h2LeZ112XSbCmA4wZWZkLULBOEXLadyur2hViFZxnRwcch+Ulx85PW3TLlrs65F7V wSB0W3z53gGnQ== In-Reply-To: <86il2qxg5c.fsf@mail.linkov.net> (Juri Linkov's message of "Thu, 15 Feb 2024 09:58:19 +0200") 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:280052 Archived-At: --=-=-= Content-Type: text/plain Hi Juri, Juri Linkov writes: >> Also, we'd probably want to limit the size of the printed representation >> (FETCHER created by project-find-regexp currently closes over the full list >> of files, that's too much to save; it will probably be a good idea to >> rewrite it to fetch the list of files anew). > > Indeed, I confirm the problem: reverting an xref buffer with 'g' > can't find matches in a new file added after the first run. > > Doesn't this hint that both a bookmark and a revert function > should store more high-level arguments? For example, for > 'project-find-regexp' it should be sufficient to store > its argument 'regexp' together with 'default-directory' > to reconstruct the previous xref buffer contents. > Then it could store these arguments in a buffer-local variable > that could be used to create a bookmark. Yes, that's almost exactly what I did in my bookmarking implementation. Only difference is I kept the whole project object instead of just 'default-directory', since different project types can use different information. I'm attaching below the patch I applied in my local branch, as a reference. > PS: With C-u 'project-find-regexp' reads more arguments > in the body, but such reading should be moved to the > interactive form anyway like 'rgrep' does. Agreed. This patch does that too: --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Support-bookmarking-project-find-regexp-results-buff.patch >From 5de2bfd3e2d672d0f955b916a200edc16dca6b07 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Mon, 12 Feb 2024 18:38:36 +0100 Subject: [PATCH] Support bookmarking 'project-find-regexp' results buffer * lisp/progmodes/project.el (xref-backend-apropos) (xref-backend-context, xref-backend-restore): New methods. (project-find-regexp, project-or-external-find-regexp): Use 'xref-make-fetcher' instead of a bespoke fetcher function to facilitate bookmarking the results buffer. * lisp/progmodes/xref.el (xref-bookmark-make-record): Use strings for 'format-spec' specifications. (xref-bookmark-jump): Autoload it. (xref-show-xrefs): Make DISPLAY-ACTION argument optional. (xref-make-fetcher): Autoload, and make BUFFER and POINT arguments optional, default to the current buffer and point. --- lisp/progmodes/project.el | 69 ++++++++++++++++++++------------------- lisp/progmodes/xref.el | 14 +++++--- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index 916a031ec60..6b0b4e86851 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -947,32 +947,44 @@ project-other-tab-command (declare-function grep-read-files "grep") (declare-function xref--find-ignores-arguments "xref") +(cl-defmethod xref-backend-context ((_backend (head project)) _id _kind)) +(cl-defmethod xref-backend-restore ((_backend (head project)) _context)) +(cl-defmethod xref-backend-apropos ((backend (head project)) pattern) + (project--find-regexp-in-files pattern (project-files (cdr backend)))) + +(cl-defmethod xref-backend-context ((_backend (head project-dir)) _id _kind)) +(cl-defmethod xref-backend-restore ((_backend (head project-dir)) _context)) +(cl-defmethod xref-backend-apropos ((backend (head project-dir)) pattern) + (project--find-regexp-in-files + pattern (project--files-in-directory (nth 1 backend) nil (nth 2 backend)))) + +(cl-defmethod xref-backend-context ((_backend (head project-ext)) _id _kind)) +(cl-defmethod xref-backend-restore ((_backend (head project-ext)) _context)) +(cl-defmethod xref-backend-apropos ((backend (head project-ext)) pattern) + (let ((pr (cdr backend))) + (project--find-regexp-in-files + pattern (project-files pr (cons (project-root pr) + (project-external-roots pr)))))) + ;;;###autoload -(defun project-find-regexp (regexp) +(defun project-find-regexp (regexp &optional dir pattern) "Find all matches for REGEXP in the current project's roots. -With \\[universal-argument] prefix, you can specify the directory -to search in, and the file name pattern to search for. The +With \\[universal-argument] prefix, you can specify the DIR +to search in, and the file name PATTERN to search for. The pattern may use abbreviations defined in `grep-files-aliases', e.g. entering `ch' is equivalent to `*.[ch]'. As whitespace triggers completion when entering a pattern, including it requires quoting, e.g. `\\[quoted-insert]'." - (interactive (list (project--read-regexp))) - (require 'xref) - (require 'grep) - (let* ((caller-dir default-directory) - (pr (project-current t)) - (default-directory (project-root pr)) - (files - (if (not current-prefix-arg) - (project-files pr) - (let ((dir (read-directory-name "Base directory: " - caller-dir nil t))) - (project--files-in-directory dir - nil - (grep-read-files regexp)))))) - (xref-show-xrefs - (apply-partially #'project--find-regexp-in-files regexp files) - nil))) + (interactive (let* ((regexp (project--read-regexp)) + (dir-pat (when current-prefix-arg + (cons (read-directory-name "Base directory: ") + (grep-read-files regexp))))) + (list regexp (car dir-pat) (cdr dir-pat)))) + (xref-show-xrefs (xref-make-fetcher + (if dir + (list 'project-dir dir pattern) + (cons 'project (project-current t))) + regexp 'apropos regexp))) (defun project--dir-ignores (project dir) (let ((root (project-root project))) @@ -987,20 +999,11 @@ project--dir-ignores ;;;###autoload (defun project-or-external-find-regexp (regexp) - "Find all matches for REGEXP in the project roots or external roots. -With \\[universal-argument] prefix, you can specify the file name -pattern to search for." + "Find all matches for REGEXP in the project roots or external roots." (interactive (list (project--read-regexp))) - (require 'xref) - (let* ((pr (project-current t)) - (default-directory (project-root pr)) - (files - (project-files pr (cons - (project-root pr) - (project-external-roots pr))))) - (xref-show-xrefs - (apply-partially #'project--find-regexp-in-files regexp files) - nil))) + (xref-show-xrefs (xref-make-fetcher + (cons 'project-ext (project-current t)) + regexp 'apropos regexp))) (defun project--find-regexp-in-files (regexp files) (unless files diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el index 6841f93a9c2..2742cc56ea1 100644 --- a/lisp/progmodes/xref.el +++ b/lisp/progmodes/xref.el @@ -1049,8 +1049,8 @@ xref-bookmark-make-record (user-error "Cannot bookmark due to unknown Xref backend")) `(,(format-spec xref-default-bookmark-name-format `((?i . ,xref--identifier) - (?k . ,xref--kind) - (?b . ,xref--backend))) + (?k . ,(symbol-name xref--kind)) + (?b . ,(prin1-to-string xref--backend)))) ,@(bookmark-make-record-default t) (backend . ,xref--backend) (context . ,(when (buffer-live-p xref--original-buffer) @@ -1064,6 +1064,7 @@ xref-bookmark-make-record (kind . ,xref--kind) (handler . xref-bookmark-jump))) +;;;###autoload (defun xref-bookmark-jump (bookmark) "Jump to Xref buffer bookmark BOOKMARK." (let* ((backend (bookmark-prop-get bookmark 'backend)) @@ -1587,7 +1588,7 @@ xref--read-identifier-history (defvar xref--read-pattern-history nil) ;;;###autoload -(defun xref-show-xrefs (fetcher display-action) +(defun xref-show-xrefs (fetcher &optional display-action) "Display some Xref values produced by FETCHER using DISPLAY-ACTION. The meanings of both arguments are the same as documented in `xref-show-xrefs-function'." @@ -1680,7 +1681,8 @@ xref--find-definitions (xref--create-fetcher id 'definitions id) display-action)) -(defun xref-make-fetcher (backend input kind identifier buffer point) +;;;###autoload +(defun xref-make-fetcher (backend input kind identifier &optional buffer point) "Return fetcher function for xrefs of kind KIND for IDENTIFIER using BACKEND. INPUT is the user input for the Xref operation, usually it is the same @@ -1690,7 +1692,9 @@ xref-make-fetcher The fetcher function returns a list of xrefs, and sets `xref-fetcher-alist', which see." - (let ((method (intern (format "xref-backend-%s" kind)))) + (let ((method (intern (format "xref-backend-%s" kind))) + (buffer (or buffer (current-buffer))) + (point (or point (point)))) (lambda () (setq xref-fetcher-alist (list (cons 'original-buffer buffer) (cons 'original-point point) -- 2.42.0 --=-=-=--