Rens Oliemans <[email protected]> writes:

> Arash Esbati <[email protected]> writes:
>
>> I admit this is also what I did, i.e., hitting ' ' for all labels and
>> not 'l', but you're right.  In theory, it should be enough to do this in
>> your init file:
>>
>> --8<---------------cut here---------------start------------->8---
>> (setq reftex-label-alist
>>       '(("\\lstinputlisting[]{}" ?l "lst:" "~\\ref{%s}" 2
>>       (regexp "[Ll]isting"))))
>> --8<---------------cut here---------------end--------------->8---
>>
>> But I couldn't get it working, can't tell why.
>
> Wow, I'll take a closer look at reftex-label-alist, I don't fully
> understand that docstring yet.

I think I now know what is happening: The regexp in
`reftex-label-regexps' has a flaw since it matches the end of the
optional argument; it actually should match the end of label.  With
that, `reftex-what-macro' can't do its job and the entry above is parsed
wrong.

Can you apply this patch to your local listings.el, recompile the file,
reset any customizations to `reftex-label-regexps' and
`reftex-label-alist', restart Emacs and try it again?

--8<---------------cut here---------------start------------->8---
diff --git a/style/listings.el b/style/listings.el
index e03b8216..a4e7cd65 100644
--- a/style/listings.el
+++ b/style/listings.el
@@ -1,6 +1,6 @@
 ;;; listings.el --- AUCTeX style for `listings.sty'  -*- lexical-binding: t; 
-*-

-;; Copyright (C) 2004-2024 Free Software Foundation, Inc.
+;; Copyright (C) 2004-2026 Free Software Foundation, Inc.

 ;; Author: Ralf Angeli <[email protected]>
 ;; Maintainer: [email protected]
@@ -434,9 +434,33 @@
    (when (fboundp 'reftex-add-label-environments)
      (reftex-add-label-environments
       '(("lstlisting" ?l "lst:" "~\\ref{%s}"
+         LaTeX-keyval-caption-reftex-context-function
+         (regexp "[Ll]isting"))
+        ("\\lstinputlisting[]{}" ?l "lst:" "~\\ref{%s}"
          LaTeX-keyval-caption-reftex-context-function
          (regexp "[Ll]isting")))))

+   (when (and (boundp 'reftex-label-regexps)
+              (fboundp 'reftex-compile-variables))
+     (let ((regexp (concat "\\\\lstinputlisting"
+                           ;; Match the opening [ and the following chars
+                           "\\[[^][]*"
+                           ;; Allow nested levels of chars enclosed in braces
+                           "\\(?:{[^}{]*"
+                           "\\(?:{[^}{]*"
+                           "\\(?:{[^}{]*}[^}{]*\\)*"
+                           "}[^}{]*\\)*"
+                           "}[^][]*\\)*"
+                           ;; Match the label key
+                           "\\<label[[:space:]]*=[[:space:]]*"
+                           ;; Match the label value; braces around the
+                           ;; value are optional from LaTeX POV, but
+                           ;; necessary for RefTeX for follow-mode:
+                           "{?\\(?1:[^] ,}\r\n\t%]+\\)")))
+       (unless (member regexp reftex-label-regexps)
+         (add-to-list 'reftex-label-regexps regexp t)
+         (reftex-compile-variables))))
+
    ;; Fontification
    (when (and (fboundp 'font-latex-add-keywords)
               (eq TeX-install-font-lock 'font-latex-setup))
--8<---------------cut here---------------end--------------->8---

Best, Arash

Reply via email to