Subject: Re: bug#53749: 29.0.50; [PATCH] Xref backend for TeX buffers

From: David Fussner <[email-masked]>
Date: Mon, 16 Sep 2024 14:30:20 +0100

Hi Mattias, Arash,

Here's a patch to fix the regexps and delete the unnecessary variable.
Any thoughts?

I'll look at adding new vars for the TeX escape and grouping chars,
creating a new bug number when I have a working patch.

Thanks, David.

On Mon, 16 Sept 2024 at 09:33, David Fussner <[email-masked]> wrote:
>
> Hi Mattias,
>
> Thanks for this, and of course you are quite right on both counts:
>
> > but it also depends on what you want that `tex-thingatpt-exclude-chars` variable to be
>
> I believe it's time I abandoned this piece of idiocy, already flagged
> by several commentators before you. The basic motivation was to allow
> using the new xref code in TeX files that used non-standard escape and
> grouping characters. Both etags and AUCTeX address this possibility,
> but the latter does it right by having three separate variables:
> TeX-esc, TeX-grop, and TeX-grcl, so that these can be part of the
> whole regular expression apparatus for syntax highlighting and
> everything else. I had in the back of my mind to implement something
> similar in tex-mode, but the half-measure of
> `tex-thingatpt-exclude-chars` is the wrong way to go.
>
> I propose, therefore, to eliminate this variable from tex-mode and
> just hard code the standard escape and grouping characters for the
> time being, as in the rest of tex-mode.el, then to submit later a
> patch to implement something similar to what AUCTeX does, and one
> interoperable with AUCTeX, also.
>
> > This is not a correct way to build a regexp;
>
> Given the above changes, would you and your electronic servant be OK
> with code like the following?
>
> (re-search-forward "[][\\{}\"*`'#=&()%,|$[:cntrl:][:blank:]]")
>
> (I still, I'm ashamed to confess, find the traditional regexp syntax
> easier to read than rx notation, but could overcome this if you think
> rx is a better fit here.)
>
> What do you think?
>
> Best,  David.
>
> On Sat, 14 Sept 2024 at 18:26, Mattias Engdegård
> <[email-masked]> wrote:
> >
> > Thanks for the contribution, David! My electronic servant complained about some regexps:
> >
> > 3881    (re-search-backward (concat "[]["
> > 3882                                (mapconcat #'regexp-quote
> > 3883                                           (mapcar #'char-to-string
> > 3884                                                   tex-thingatpt-exclude-chars))
> > 3885                                "\"*`'#=&()%,|$[:cntrl:][:blank:]]"))
> >
> > This is not a correct way to build a regexp; `regexp-quote` can only be used to quote strings that should match literally, not characters inside [...], where backslashes have no escaping power.
> >
> > There are various ways of doing this properly. I would suggest something like
> >
> >   (rx-to-string `(or (in "\"#$%&'()*,=[]`|" cntrl blank)
> >                      ,@tex-thingatpt-exclude-chars)
> >                 t))
> >
> > but it also depends on what you want that `tex-thingatpt-exclude-chars` variable to be. Should it be a user option (defcustom)? The variable's doc string is a wall of text that basically says that it can be set to whatever you want but things will stop working so you'd better not.
> >
From 1c607543556a7b79615dbc5e8384e10d92d081d5 Mon Sep 17 00:00:00 2001
From: David Fussner <[email-masked]>
Date: Mon, 16 Sep 2024 14:05:01 +0100
Subject: [PATCH] Fix regexps for TeX xref backend (Bug#53749)

* lisp/textmodes/tex-mode.el (tex-thingatpt-exclude-chars): Remove
var.
(tex-thingatpt--beginning-of-symbol, tex-thingatpt--end-of-symbol):
Use hard-coded chars instead of var.
---
 lisp/textmodes/tex-mode.el | 43 ++------------------------------------
 1 file changed, 2 insertions(+), 41 deletions(-)

diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el
index ec0c0c47a2d..6fc49800018 100644
--- a/lisp/textmodes/tex-mode.el
+++ b/lisp/textmodes/tex-mode.el
@@ -3814,37 +3814,6 @@ tex-chktex
 ;; Emacs and `etags' this way aims to improve the user experience "out
 ;; of the box."
 
-(defvar tex-thingatpt-exclude-chars '(?\\ ?\{ ?\})
-  "Exclude these chars by default from TeX thing-at-point.
-
-The TeX `xref-backend-identifier-at-point' method uses the characters
-listed in this variable to decide on the default search string to
-present to the user who calls an `xref' command.  These characters
-become part of a regexp which always excludes them from that default
-string.  For the `xref' commands to function properly in TeX buffers, at
-least the TeX escape and the two TeX grouping characters should be
-listed here.  Should your TeX documents contain other characters which
-you want to exclude by default, then you can add them to the list,
-though you may wish to consult the functions
-`tex-thingatpt--beginning-of-symbol' and `tex-thingatpt--end-of-symbol'
-to see what the regexp already contains.  If your documents contain
-non-standard escape and grouping characters, then you can replace the
-three listed here with your own, thereby allowing the three standard
-characters to appear by default in search strings.  Please be aware,
-however, that the `etags' program only recognizes `\\' (92) and `!' (33)
-as escape characters in TeX documents, and if it detects the latter it
-also uses `<>' as the TeX grouping construct rather than `{}'.  Setting
-the escape and grouping chars to anything other than `\\=\\{}' or `!<>'
-will not be useful without changes to `etags', at least for commands
-that search tags tables, such as \\[xref-find-definitions] and \
-\\[xref-find-apropos].
-
-Should you wish to change the defaults, please also be aware that,
-without further modifications to tex-mode.el, the usual text-parsing
-routines for `font-lock' and the like won't work correctly, as the
-default escape and grouping characters are currently hard coded in many
-places.")
-
 ;; Populate `semantic-symref-filepattern-alist' for the in-tree modes;
 ;; AUCTeX is doing the same for its modes.
 (with-eval-after-load 'semantic/symref/grep
@@ -3878,20 +3847,12 @@ tex--thing-at-point
 
 (defun tex-thingatpt--beginning-of-symbol ()
   (and
-   (re-search-backward (concat "[]["
-                               (mapconcat #'regexp-quote
-                                          (mapcar #'char-to-string
-                                                  tex-thingatpt-exclude-chars))
-                               "\"*`'#=&()%,|$[:cntrl:][:blank:]]"))
+   (re-search-backward "[][\\{}\"*`'#=&()%,|$[:cntrl:][:blank:]]" nil t)
    (forward-char)))
 
 (defun tex-thingatpt--end-of-symbol ()
   (and
-   (re-search-forward (concat "[]["
-                              (mapconcat #'regexp-quote
-                                          (mapcar #'char-to-string
-                                                  tex-thingatpt-exclude-chars))
-                              "\"*`'#=&()%,|$[:cntrl:][:blank:]]"))
+   (re-search-forward "[][\\{}\"*`'#=&()%,|$[:cntrl:][:blank:]]" nil t)
    (backward-char)))
 
 (defun tex--bounds-of-symbol-at-point ()
-- 
2.39.4