Hi Keita,

Ikumi Keita <[email protected]> writes:

> It seems OK. I'm fine with your proposal.

Thanks for your response.  I changed shortvrb.el and also tried to write
an ert-test, but the test breaks.

  (ert-test-failed
   ((should-not
     (get-text-property
      (point)
      'face))
    :form
    (get-text-property 77 face)
    :value font-latex-verbatim-face))

It seems that the Fontification is bleeding, but I can't see why and if
I open the test in a regular .tex file, it works as expected.  I'm
attaching the change, do you see what's going wrong?

TIA.  Best, Arash
diff --git a/style/shortvrb.el b/style/shortvrb.el
index e6de0608..2e0a7da3 100644
--- a/style/shortvrb.el
+++ b/style/shortvrb.el
@@ -1,6 +1,6 @@
 ;;; shortvrb.el --- AUCTeX style for `shortvrb.sty'  -*- lexical-binding: t; -*-
 
-;; Copyright (C) 2009--2022 Free Software Foundation, Inc.
+;; Copyright (C) 2009--2023 Free Software Foundation, Inc.
 
 ;; Author: Ralf Angeli <[email protected]>
 ;; Maintainer: [email protected]
@@ -45,12 +45,11 @@
 (require 'tex-style)
 
 ;; Silence the compiler:
-(declare-function font-latex-add-to-syntax-alist
-                  "font-latex"
-                  (list))
+(declare-function font-latex-set-syntactic-keywords
+                  "font-latex" ())
 (declare-function font-latex-add-keywords
-                  "font-latex"
-                  (keywords class))
+                  "font-latex" (keywords class))
+(defvar font-latex-syntactic-keywords-extra)
 
 (TeX-add-style-hook
  "shortvrb"
@@ -70,8 +69,6 @@
               LaTeX-shortvrb-chars)))
 
    ;; Syntax
-   ;; N.B. This doesn't handle backslash just before the closing
-   ;; delimiter like |xyz\| correctly.  We hope we can live with that.
    (when LaTeX-shortvrb-chars
      (let ((st (make-syntax-table (syntax-table))))
        (dolist (c LaTeX-shortvrb-chars)
@@ -79,16 +76,27 @@
        (set-syntax-table st)))
 
    ;; Fontification
-   (when (and LaTeX-shortvrb-chars
-              (featurep 'font-latex)
+   (when (and (featurep 'font-latex)
               (eq TeX-install-font-lock 'font-latex-setup))
-     (font-latex-add-to-syntax-alist
-      (mapcar (lambda (char) (cons char "\""))
-              LaTeX-shortvrb-chars))
-
      (font-latex-add-keywords '(("MakeShortVerb"   "*{")
                                 ("DeleteShortVerb" "{"))
-                              'function)))
+                              'function)
+
+     ;; Use `font-latex-syntactic-keywords-extra' instead of
+     ;; `font-latex-add-to-syntax-alist' so we can catch a backslash
+     ;; within the shortvrb delimiters and make things like |xyz\|
+     ;; work correctly:
+     (when LaTeX-shortvrb-chars
+       (dolist (c LaTeX-shortvrb-chars)
+         (let ((s (char-to-string c)))
+           (add-to-list 'font-latex-syntactic-keywords-extra
+                        `(,(concat "\\(" s "\\)"
+                                   ".*?"
+                                   "\\(" (regexp-quote TeX-esc) "*\\)"
+                                   "\\(" s "\\)")
+                          (1 "\"") (2 ".") (3 "\"")))))
+       ;; Tell font-lock about the update
+       (font-latex-set-syntactic-keywords))))
  TeX-dialect)
 
 ;;; shortvrb.el ends here
diff --git a/tests/latex/font-latex-test.el b/tests/latex/font-latex-test.el
index 0c81392a..1af14597 100644
--- a/tests/latex/font-latex-test.el
+++ b/tests/latex/font-latex-test.el
@@ -350,4 +350,45 @@ x
       (should (font-latex-faces-present-p 'font-lock-function-name-face
                                           (match-end 0)))  )))
 
+(ert-deftest font-latex-shortvrb-chars ()
+  "Test fontification within delimiters defined by `LaTeX-shortvrb-chars'."
+  (with-temp-buffer
+    (let* ((TeX-install-font-lock #'font-latex-setup)
+           (LaTeX-shortvrb-chars '(?|))
+           (TeX-parse-self t))
+      (insert "\
+\\documentclass{article}
+\\usepackage{shortvrb}
+\\begin{document}
+foo |xyz\\| bar
+\\end{document}
+%%% Local Variables:
+%%% mode: latex
+%%% TeX-master: t
+%%% End:")
+      (LaTeX-mode)
+      (TeX-update-style t)
+      (font-lock-ensure)
+      (goto-char (point-min))
+      (re-search-forward "^f" nil t)
+      (should-not (get-text-property (point) 'face))
+      (search-forward "|" nil t)
+      ;; This is the `|' char:
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face
+                                          (1- (point))))
+      ;; This is the `x' char:
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face))
+      (search-forward "|" nil t)
+      ;; This is the `\' char:
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face
+                                          (- (point) 2)))
+      ;; This is the `|' char:
+      (should (font-latex-faces-present-p 'font-latex-verbatim-face
+                                          (1- (point))))
+      (search-forward "ba" nil t)
+      ;;; Fontification seems to bleed?
+      (should-not (get-text-property (point) 'face))
+      ;;;
+      )))
+
 ;;; font-latex-test.el ends here

Reply via email to