GNU bug report logs - #78587
[PATCH] TeX-find-macro-end

Previous Next

Package: auctex;

Reported by: "Paul D. Nelson" <ultrono <at> gmail.com>

Date: Sun, 25 May 2025 15:15:02 UTC

Severity: normal

Tags: patch

Done: Arash Esbati <arash <at> gnu.org>

To reply to this bug, email your comments to 78587 AT debbugs.gnu.org.
There is no need to reopen the bug first.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-auctex <at> gnu.org:
bug#78587; Package auctex. (Sun, 25 May 2025 15:15:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Paul D. Nelson" <ultrono <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-auctex <at> gnu.org. (Sun, 25 May 2025 15:15:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: bug-auctex <at> gnu.org
Subject: [PATCH] TeX-find-macro-end
Date: Sun, 25 May 2025 17:14:44 +0200
[Message part 1 (text/plain, inline)]
Hi all,

I noticed that TeX-find-macro-end doesn't work as expected way on, e.g.,

  \begin{enumerate}[a)]

The issue arose organically when folding such macros.

To see the basic issue, evaluate the following in scratch:

--8<---------------cut here---------------start------------->8---
(mapcar
 (lambda (str)
   (with-temp-buffer
     (LaTeX-mode)
     (insert str)
     (char-to-string (char-before (TeX-find-macro-end-helper (point-min))))))
 '("\begin{enumerate}[a]"
   "\begin{enumerate}[(a)]"
   "\begin{enumerate}[a)]"))
--8<---------------cut here---------------end--------------->8---

This returns ("]" "]" ")") but should return ("]" "]" "]").

The attached patch seems to address the issue by using a stripped syntax
table, which seems like a standard approach in tex.el.  Any feedback
would be welcome.

Paul

[0001-Fix-TeX-macro-end-detection-in-some-edge-cases.patch (text/x-patch, attachment)]

Information forwarded to bug-auctex <at> gnu.org:
bug#78587; Package auctex. (Mon, 26 May 2025 13:21:02 GMT) Full text and rfc822 format available.

Message #8 received at 78587 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: "Paul D. Nelson" <ultrono <at> gmail.com>
Cc: 78587 <at> debbugs.gnu.org
Subject: Re: bug#78587: [PATCH] TeX-find-macro-end
Date: Mon, 26 May 2025 15:20:28 +0200
Hi Paul,

"Paul D. Nelson" <ultrono <at> gmail.com> writes:

> I noticed that TeX-find-macro-end doesn't work as expected way on, e.g.,
>
>   \begin{enumerate}[a)]
>
> The issue arose organically when folding such macros.
>
> To see the basic issue, evaluate the following in scratch:
>
> (mapcar
>  (lambda (str)
>    (with-temp-buffer
>      (LaTeX-mode)
>      (insert str)
>      (char-to-string (char-before (TeX-find-macro-end-helper (point-min))))))
>  '("\begin{enumerate}[a]"
>    "\begin{enumerate}[(a)]"
>    "\begin{enumerate}[a)]"))
>
> This returns ("]" "]" ")") but should return ("]" "]" "]").
>
> The attached patch seems to address the issue by using a stripped syntax
> table, which seems like a standard approach in tex.el.  Any feedback
> would be welcome.

What do get with your patch when you eval this form:

--8<---------------cut here---------------start------------->8---
(mapcar
 (lambda (str)
   (with-temp-buffer
     (LaTeX-mode)
     (insert str)
     (char-to-string (char-before (TeX-find-macro-end-helper (point-min))))))
 '("\begin{enumerate}[a]"
   "\begin{enumerate}[(a)]"
   "\begin{enumerate}[a)]"
   "\begin{enumerate}[a{]}]"))
--8<---------------cut here---------------end--------------->8---

Best, Arash




Information forwarded to bug-auctex <at> gnu.org:
bug#78587; Package auctex. (Mon, 26 May 2025 16:18:02 GMT) Full text and rfc822 format available.

Message #11 received at 78587 <at> debbugs.gnu.org (full text, mbox):

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78587 <at> debbugs.gnu.org
Subject: Re: bug#78587: [PATCH] TeX-find-macro-end
Date: Mon, 26 May 2025 18:17:11 +0200
Hi Arash,

> What do get with your patch when you eval this form:
>
> --8<---------------cut here---------------start------------->8---
> (mapcar
>  (lambda (str)
>    (with-temp-buffer
>      (LaTeX-mode)
>      (insert str)
>      (char-to-string (char-before (TeX-find-macro-end-helper (point-min))))))
>  '("\begin{enumerate}[a]"
>    "\begin{enumerate}[(a)]"
>    "\begin{enumerate}[a)]"
>    "\begin{enumerate}[a{]}]"))
> --8<---------------cut here---------------end--------------->8---

I get the right answer ("]" "]" "]" "]"), but for the wrong reason:
evaluating

--8<---------------cut here---------------start------------->8---
(mapcar
 (lambda (str)
   (with-temp-buffer
     (LaTeX-mode)
     (insert str)
     (char-to-string (char-before (1- (TeX-find-macro-end-helper (point-min)))))))
 '("\begin{enumerate}[a{]}]"))
--8<---------------cut here---------------end--------------->8---

yields ("{"), which shows that it's picking up the wrong "]".

I guess this case could be covered by adding "{" and "}" to the syntax
table.  Can you imagine any further issues with that approach?

Thanks, best,

Paul




Information forwarded to bug-auctex <at> gnu.org:
bug#78587; Package auctex. (Tue, 27 May 2025 12:33:01 GMT) Full text and rfc822 format available.

Message #14 received at 78587 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: "Paul D. Nelson" <ultrono <at> gmail.com>
Cc: 78587 <at> debbugs.gnu.org
Subject: Re: bug#78587: [PATCH] TeX-find-macro-end
Date: Tue, 27 May 2025 14:32:11 +0200
Hi Paul,

"Paul D. Nelson" <ultrono <at> gmail.com> writes:

> I get the right answer ("]" "]" "]" "]"), but for the wrong reason:
> evaluating
>
> (mapcar
>  (lambda (str)
>    (with-temp-buffer
>      (LaTeX-mode)
>      (insert str)
>      (char-to-string (char-before (1- (TeX-find-macro-end-helper (point-min)))))))
>  '("\begin{enumerate}[a{]}]"))
>
> yields ("{"), which shows that it's picking up the wrong "]".

This is also what I thought, sorry for not being explicit about it in my
last message, was a bit in hurry.

> I guess this case could be covered by adding "{" and "}" to the syntax
> table.  Can you imagine any further issues with that approach?

My answer to that exercise is basically this:

https://cgit.git.savannah.gnu.org/cgit/auctex.git/commit/font-latex.el?id=7de5f1bbdd186bc84a8bd764e843a308f798210f

This was the best I could come up with as I tried to fix this
fontification bug:

https://debbugs.gnu.org/cgi/bugreport.cgi?bug=57516

I'm not sure if that solution is applicable to the issue at hand.

Best, Arash




Information forwarded to bug-auctex <at> gnu.org:
bug#78587; Package auctex. (Tue, 27 May 2025 14:58:02 GMT) Full text and rfc822 format available.

Message #17 received at 78587 <at> debbugs.gnu.org (full text, mbox):

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78587 <at> debbugs.gnu.org
Subject: Re: bug#78587: [PATCH] TeX-find-macro-end
Date: Tue, 27 May 2025 16:56:53 +0200
[Message part 1 (text/plain, inline)]
Hi Arash,

Thanks, that suggestion is helpful, and seems to take care of the issue
you noted.  Please find attached an updated patch, to which I also added
a test.

thanks, best,

Paul

[0001-Fix-TeX-macro-end-detection-in-some-edge-cases.patch (text/x-patch, attachment)]

Information forwarded to bug-auctex <at> gnu.org:
bug#78587; Package auctex. (Wed, 28 May 2025 21:54:02 GMT) Full text and rfc822 format available.

Message #20 received at 78587 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: "Paul D. Nelson" <ultrono <at> gmail.com>
Cc: 78587 <at> debbugs.gnu.org
Subject: Re: bug#78587: [PATCH] TeX-find-macro-end
Date: Wed, 28 May 2025 23:53:00 +0200
Hi Paul,

"Paul D. Nelson" <ultrono <at> gmail.com> writes:

> Thanks, that suggestion is helpful, and seems to take care of the issue
> you noted.

Great, glad it helped.

> Please find attached an updated patch, to which I also added a test.

Thanks, I have 2 minor question below.

> From 65906577241bda8d7321d0ef33f0808944d06bed Mon Sep 17 00:00:00 2001
> From: Paul Nelson <ultrono <at> gmail.com>
> Date: Sun, 25 May 2025 17:11:49 +0200
> Subject: [PATCH] Fix TeX macro end detection in some edge cases
>
> * tex.el (TeX-find-macro-end-helper): Use
> TeX-search-syntax-table with square brackets when calling
> forward-sexp.  This fixes parsing of optional arguments
> containing other unmatched delimiters, such as
> \begin{enumerate}[a)], where the search would stop at ')'
> instead of ']'.  We take further care to treat examples like
> \begin{enumerate}[a{]}].
>
> * tests/latex/latex-test.el
> (TeX-find-macro-end-with-complicated-optional-args): New test
> concerning the above issue.
> ---
>  tests/latex/latex-test.el | 13 +++++++++++++
>  tex.el                    | 16 +++++++++++++---
>  2 files changed, 26 insertions(+), 3 deletions(-)
>
> diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
> index b68ea3b8..92b7c530 100644
> --- a/tests/latex/latex-test.el
> +++ b/tests/latex/latex-test.el
> @@ -761,4 +761,17 @@ check the indentation for optional argument of \\usepackage."
>                 (buffer-string)
>                 "sab\n")))))
>  
> +(ert-deftest TeX-find-macro-end-with-complicated-optional-args ()
> +  "Check that end of macro is determined correctly."
> +  ;; (bug#78587)
> +  (dolist (str '("\begin{enumerate}[a]"
> +                 "\begin{enumerate}[(a)]"
> +                 "\begin{enumerate}[a)]"
> +                 "\begin{enumerate}[a{]}]"))
> +    (with-temp-buffer
> +      (insert str)
> +      (LaTeX-mode)
> +      (should (equal (point-max)
> +                     (TeX-find-macro-end-helper (point-min)))))))
> +
>  ;;; latex-test.el ends here
> diff --git a/tex.el b/tex.el
> index e90252d8..62fc217b 100644
> --- a/tex.el
> +++ b/tex.el
> @@ -5866,9 +5866,19 @@ those will be considered part of it."
>                         (forward-line 1)
>                         (looking-at "[ \t]*\\(\\[\\)"))))
>              (goto-char (match-beginning 1))
> -            (condition-case nil
> -                (forward-sexp)
> -              (scan-error (throw 'found (point)))))
> +            (goto-char (match-beginning 1))
> +            ;; Imitate `font-latex-find-matching-close', motivated by
> +            ;; examples like \begin{enumerate}[a{]}].
> +            (let* ((syntax (TeX-search-syntax-table ?\[ ?\]))

Why do you need `let*' here?  `let' seems to do the job, or am I missing
something?

> +                   (parse-sexp-ignore-comments
> +                    (not (eq major-mode 'docTeX-mode))))

Can we do (not (derived-mode-p 'docTeX-mode))?

Let me know what you think about the above, I can add them when I
install the patch.

Best, Arash




Information forwarded to bug-auctex <at> gnu.org:
bug#78587; Package auctex. (Thu, 29 May 2025 07:49:01 GMT) Full text and rfc822 format available.

Message #23 received at 78587 <at> debbugs.gnu.org (full text, mbox):

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78587 <at> debbugs.gnu.org
Subject: Re: bug#78587: [PATCH] TeX-find-macro-end
Date: Thu, 29 May 2025 09:47:57 +0200
Hi Arash,

I think the answer to both of your questions is "yes".  Thanks for your
feedback and best,

Paul




Reply sent to Arash Esbati <arash <at> gnu.org>:
You have taken responsibility. (Fri, 30 May 2025 15:07:02 GMT) Full text and rfc822 format available.

Notification sent to "Paul D. Nelson" <ultrono <at> gmail.com>:
bug acknowledged by developer. (Fri, 30 May 2025 15:07:02 GMT) Full text and rfc822 format available.

Message #28 received at 78587-done <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: "Paul D. Nelson" <ultrono <at> gmail.com>
Cc: 78587-done <at> debbugs.gnu.org
Subject: Re: bug#78587: [PATCH] TeX-find-macro-end
Date: Fri, 30 May 2025 17:06:30 +0200
Hi Paul,

"Paul D. Nelson" <ultrono <at> gmail.com> writes:

> I think the answer to both of your questions is "yes".  Thanks for your
> feedback and best,

I massaged that change a little and installed it.  Many thanks for the
change set, closing.

Best, Arash




This bug report was last modified 14 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.