Subject: Re: bug#38258: 12.2.0; reftex-view-crossref on citation pre- and postnotes

From: Arash Esbati <[email-masked]>
Date: Wed, 10 Apr 2024 11:21:02 +0200

Hi Gustavo,

Gustavo Barros <[email-masked]> writes:

> To be very explicit, the command in question is
> `reftex-view-crossref`, and it is also called by
> `reftex-view-crossref-when-idle' which is responsible for the echo
> message.  `reftex-view-crossref` checks if point is within a macro
> with `(car (reftex-what-macro-safe 1))', and later checks if it is a
> citation macro with `(string-match
> "\\`\\\\cite\\|cite\\([s*]\\|texts?\\)?\\'\\|bibentry" macro)'. My
> suggestion is just to add a further check to see if we are indeed in a
> mandatory argument (a citation key).

Thanks for the explicit pointer.  I can think of something like this:

--8<---------------cut here---------------start------------->8---
(defun reftex-view-crossref-when-idle ()
  ;; Display info about crossref at point in echo area or a window.
  ;; This function was designed to work with an idle timer.
  ;; We try to get out of here as quickly as possible if the call is useless.
  (and reftex-mode
       ;; Make sure message area is free if we need it.
       (or (eq reftex-auto-view-crossref 'window) (not (current-message)))
       ;; Make sure we are not already displaying this one
       (not (memq last-command '(reftex-view-crossref
                                 reftex-mouse-view-crossref)))
       ;; Quick precheck if this might be a relevant spot
       ;; `reftex-view-crossref' will do a more thorough check.
       (save-excursion
         (search-backward "\\" nil t)
         (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\|bibentry\\)"))

       ;; Also check if point is inside a mandatory argument where the
       ;; cite/ref key usually resides: (bug#38258)
       (save-excursion
         (condition-case nil
             (let ((forward-sexp-function nil))
               (up-list -1)
               (= (following-char) ?\{))
           (error nil)))

       (condition-case nil
           (let ((current-prefix-arg nil))
             (cond
              ((eq reftex-auto-view-crossref t)
               (reftex-view-crossref -1 'echo 'quiet))
              ((eq reftex-auto-view-crossref 'window)
               (reftex-view-crossref -1 'tmp-window 'quiet))
              (t nil)))
         (error nil))))
--8<---------------cut here---------------end--------------->8---

Do you want to give it a try and see if it works as expected?  I think
you see the addition.

Best, Arash