branch: master
commit 0cb158fd96ff8c1c7f0bf8c0dd152a301e82f562
Author: Arash Esbati <ar...@gnu.org>
Commit: Arash Esbati <ar...@gnu.org>

    Improve fontification of verbatim macros with braces
    
    * font-latex.el (font-latex-set-syntactic-keywords): Support one
    level of balanced braces inside the argument of verbatim macros
    with braces.  See related discussion:
    https://lists.gnu.org/archive/html/auctex-devel/2023-01/msg00023.html
    Also handle backslash better as last character in the argument.
    
    * tests/latex/font-latex-test.el
    (font-latex-verb-macros-with-braces): New test.
---
 font-latex.el                  | 12 +++++--
 tests/latex/font-latex-test.el | 73 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/font-latex.el b/font-latex.el
index 91714b32..7617b5cc 100644
--- a/font-latex.el
+++ b/font-latex.el
@@ -1097,8 +1097,16 @@ have changed."
                   ;; Some macros take an optional argument.  This is
                   ;; the same line as above for environments.
                   "\\(?:\\[[^][]*\\(?:\\[[^][]*\\][^][]*\\)*\\]\\)?"
-                  "\\({\\).*?[^\\]\\(?:\\\\\\\\\\)*\\(}\\)")
-         (1 "|") (2 "|")))))
+                  ;; Within verb macros with braces, only balanced
+                  ;; pairs of braces are allowed; so we respect this
+                  ;; and allow one level of balanced braces.  Give
+                  ;; escape char(s) at the end of the verbatim
+                  ;; construct punctuation syntax.
+                  "\\({\\)[^}{]*?"
+                  "\\(?:{[^}{]*}[^}{]*?\\)*"
+                  "\\(" (regexp-quote TeX-esc) "*\\)"
+                  "\\(}\\)")
+         (1 "|") (2 ".") (3 "|")))))
   (when font-latex-syntactic-keywords-extra
     (nconc font-latex-syntactic-keywords font-latex-syntactic-keywords-extra))
   ;; ;; Cater for docTeX mode.
diff --git a/tests/latex/font-latex-test.el b/tests/latex/font-latex-test.el
index 7ec3d6df..40f9633a 100644
--- a/tests/latex/font-latex-test.el
+++ b/tests/latex/font-latex-test.el
@@ -1,6 +1,6 @@
 ;;; font-latex-test.el --- tests for font-latex  -*- lexical-binding: t; -*-
 
-;; Copyright (C) 2020-2021  Free Software Foundation, Inc.
+;; Copyright (C) 2020-2023  Free Software Foundation, Inc.
 
 ;; This file is part of AUCTeX.
 
@@ -405,4 +405,75 @@ foo \"xyz\\\" bar
       (search-forward "ba" nil t)
       (should-not (get-text-property (point) 'face)))))
 
+(ert-deftest font-latex-verb-macros-with-braces ()
+  "Test fontification for verb macros with argument in braces."
+  (with-temp-buffer
+    (let ((TeX-install-font-lock #'font-latex-setup)
+          (TeX-parse-self t))
+      (insert "\
+\\documentclass{article}
+\\usepackage{fvextra}
+\\usepackage{hyperref}
+\\begin{document}
+foo \\Verb[commandchars=\\\\\\{\\}]{Pre \fbox{Middle} Post} bar
+foo \\Verb{w{o}r{k}s} bar
+foo \\Verb{b{r}eak{s}} bar
+foo \\href[ismap=false]{text \\cmd{test} text}{more text} bar
+foo \\path{C:\\path\\to\\} bar
+\\end{document}")
+      (LaTeX-mode)
+      (TeX-update-style t)
+      (syntax-ppss-flush-cache (point-min))
+      (font-lock-ensure)
+      (goto-char (point-min))
+
+      (re-search-forward "^f" nil t)
+      (should-not (get-text-property (point) 'face))
+      (search-forward "commandc")
+      (should (font-latex-faces-present-p 'font-lock-variable-name-face))
+      (search-forward "Mid")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "Po")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "ba")
+      (should-not (get-text-property (point) 'face))
+
+      (re-search-forward "^f" nil t)
+      (should-not (get-text-property (point) 'face))
+      (search-forward "k")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "ba")
+      (should-not (get-text-property (point) 'face))
+
+      (re-search-forward "^f" nil t)
+      (should-not (get-text-property (point) 'face))
+      (search-forward "s")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "ba")
+      (should-not (get-text-property (point) 'face))
+
+      (re-search-forward "^f" nil t)
+      (should-not (get-text-property (point) 'face))
+      (search-forward "ismap")
+      (should (font-latex-faces-present-p 'font-lock-variable-name-face))
+      (search-forward "text")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "cmd{t")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "text")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "more")
+      (should (font-latex-faces-present-p 'font-lock-constant-face))
+      (search-forward "ba")
+      (should-not (get-text-property (point) 'face))
+
+      (re-search-forward "^f" nil t)
+      (should-not (get-text-property (point) 'face))
+      (search-forward "C:")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "to")
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "ba")
+      (should-not (get-text-property (point) 'face)))))
+
 ;;; font-latex-test.el ends here

Reply via email to