branch: externals/auctex
commit 33f9eb0709a47480ccd14342b983ded2cfa1c1ce
Author: Paul Nelson <ultr...@gmail.com>
Commit: Arash Esbati <ar...@gnu.org>

    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{]}].  (bug#78587)
    
    * 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                    | 15 ++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
index b68ea3b879..92b7c53063 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 e90252d830..5a3345aeb2 100644
--- a/tex.el
+++ b/tex.el
@@ -5866,9 +5866,18 @@ 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)))))
+            ;; Imitate `font-latex-find-matching-close', motivated by
+            ;; examples like \begin{enumerate}[a{]}].
+            (let ((syntax (TeX-search-syntax-table ?\[ ?\]))
+                  (parse-sexp-ignore-comments
+                   (not (derived-mode-p 'docTeX-mode))))
+              (modify-syntax-entry ?\{ "|" syntax)
+              (modify-syntax-entry ?\} "|" syntax)
+              (modify-syntax-entry ?\\ "/" syntax)
+              (condition-case nil
+                  (with-syntax-table syntax
+                    (forward-sexp))
+                (scan-error (throw 'found (point))))))
            ;; Skip over pairs of curly braces
            ((or (looking-at "[ \t]*\n?[ \t]*{") ; Be conservative: Consider
                                         ; only consecutive lines.

Reply via email to