Hi Arash,

I'm attaching the revised patch with docstring, as requested. I also
fixed a typo in the docstring of `bib-cite-search-ring', which was
nearby. Hope that's OK, and let me know if any changes are required.

Best, David.

>From 8076a89a14e2fe52ca257af0a70bf84ec542c9df Mon Sep 17 00:00:00 2001
From: David Fussner <[email protected]>
Date: Sat, 13 Jun 2026 15:36:00 +0100
Subject: [PATCH] Modify `bib-find-next' to use xref commands

* bib-cite.el (bib-cite--next-ref): New helper function to replicate the
behavior of the obsolete `find-tag'.
(bib-find-next): Replace `find-tag' with previous.
---
 bib-cite.el | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 4 deletions(-)

diff --git a/bib-cite.el b/bib-cite.el
index ebcfbe63..26092b8c 100644
--- a/bib-cite.el
+++ b/bib-cite.el
@@ -593,6 +593,8 @@
 (declare-function reftex-view-crossref "ext:reftex-dcr"
                   (&optional arg auto-how fail-quietly))
 (declare-function outline-show-entry "ext:outline" ())
+(declare-function xref-quit-and-pop-marker-stack "xref" ())
+(declare-function xref-quit-and-goto-xref "xref" ())
 
 (defgroup bib-cite nil
   "bib-cite, LaTeX minor-mode to display \\cite, \\ref and \\label commands."
@@ -929,7 +931,81 @@ A TAGS file is created and used for multi-file documents under auctex."
       (bib-find-label)))))
 
 (defvar bib-cite-search-ring nil
-  "Bib-cite intenal variable to hold last \\ref or \\eqref find.")
+  "Bib-cite internal variable to hold last \\ref or \\eqref find.")
+
+(defun bib-cite--next-ref (str &optional bkwd)
+  "Find the next or previous \\ref to a given \\label.
+
+STR comes from the variable `last-tag' which the command `bib-find'
+sets.  Any non-nil PREV-P argument to `bib-find-next' sets BKWD here,
+producing a backward search.  The search itself follows the order in the
+*xref* buffer produced by `xref-find-apropos'."
+  (if str
+      (let* (notag
+	     (lino (concat (number-to-string (line-number-at-pos nil t)) ":"))
+	     (file (buffer-file-name))
+	     (tag (substring-no-properties str))
+	     (xrtag (seq-subseq tag (1+ (seq-position tag ?{))
+				(seq-position tag ?})))
+	     (re (concat bib-ref-regexpc xrtag "}"))
+	     (buf (xref-find-apropos xrtag)))
+	(if bkwd
+	    (with-current-buffer buf
+	      (search-forward file nil t)
+	      (re-search-forward "^[^[:digit:][:space:][:cntrl:]]" nil 1)
+	      (beginning-of-line)
+	      (condition-case nil
+		  (search-backward lino)
+		(error (progn
+                         (while
+			     (and
+			      (re-search-backward
+			       (concat
+			        "\\(?1:^[^[:digit:][:space:][:cntrl:]]\\)\\|"
+			        "^[[:space:]]*\\(?1:[[:digit:]]+:\\)")
+			       nil 1)
+			      (string-match
+			       "[[:digit:]]+:"
+			       (match-string-no-properties 1) nil t)
+			      (setq notag t)
+			      (string-version-lessp
+			       lino (match-string-no-properties 1))
+			      (setq notag 1)))
+                         (end-of-line))))
+	      (unless (or (re-search-backward re nil 1) (eq notag t) (bobp))
+		(forward-line -1))
+	      (condition-case nil
+		  (xref-quit-and-goto-xref)
+		(user-error (progn
+			      (xref-quit-and-pop-marker-stack)
+			      (message "No previous occurrence of %s" re)))))
+	  (with-current-buffer buf
+	    (search-forward file nil t)
+	    (condition-case nil
+		(search-forward lino)
+	      (error (progn
+		       (setq notag t)
+		       (while
+			   (and
+			    (re-search-forward
+			     (concat
+			      "\\(?1:^[^[:digit:][:space:][:cntrl:]]\\)\\|"
+			      "^[[:space:]]*\\(?1:[[:digit:]]+:\\)")
+			     nil 1)
+			    (string-match
+			     "[[:digit:]]+:"
+			     (match-string-no-properties 1) nil t)
+			    (string-version-lessp
+			     (match-string-no-properties 1) lino))))))
+	    (unless notag
+	      (forward-line))
+	    (re-search-forward re nil 1)
+	    (condition-case nil
+		(xref-quit-and-goto-xref)
+	      (user-error (progn
+			    (xref-quit-and-pop-marker-stack)
+			    (message "No next occurrence of %s" re)))))))
+    (message "Sorry, no previous reference to find.  Use bib-find?")))
 
 (defun bib-find-next (&optional prev-p)
   "Find next occurrence of a \\ref or \\eqref.
@@ -938,9 +1014,7 @@ documents, and the Emacs command `find-tag' doesn't allow to interactively
 find the next occurrence of a regexp."
   (interactive "P")
   (if (bib-master-file)                 ;Multi-file document
-      ;; FIXME: `find-tag' is replaced by `xref-find-definitions' in
-      ;; Emacs 25.1.  AUCTeX should track this change, sometime ...
-      (find-tag t (if prev-p '- t) t)
+      (bib-cite--next-ref (if (boundp 'last-tag) last-tag nil) prev-p)
     (if bib-cite-search-ring
         ;;FIXME: Should first make sure I move off initial \ref{}.
         (let ((regexp (concat bib-ref-regexpc bib-cite-search-ring "}")))
-- 
2.53.0

David Fussner <[email protected]> writes:

> Thanks Arash, I'll get to that when I get back. 
>
> Best, David. 
>
> On Tue, 23 Jun 2026, 09:05 Arash Esbati, <[email protected]> wrote:
>
>  Hi David,
>
>  David Fussner <[email protected]> writes:
>
>  > I'm away from my machine for a while, but a docstring seems a good
>  > idea.
>
>  Thanks, there is no rush, we can finalize this once you're back.  Maybe
>  Keita also has some comments on the code?
>
>  > How about something like:
>  >
>  > "Find the next or previous \ref to a given \label.
>  >
>  > The search follows the order in the *xref* buffer produced by
>  > `xref-find-apropos'."
>
>  Thanks, note that the docstring should describe the parameters, in this
>  case:
>
>    (defun bib-cite--next-ref (str &optional bkwd)
>
>  > I can provide a new patch when I return, or please modify as you see
>  > fit.
>
>  Great, we can then also add the xref-backend to AUCTeX.
>
>  Best, Arash

Reply via email to