[ Changed Cc: to auctex-devel. ]

>>>>> Tassilo Horn <[email protected]> writes:
> Stefan Monnier via bug-auctex via Bug reporting list for AUCTeX 
> <[email protected]> writes:
>> BTW, I noticed in the patch that `font-latex.el` appears to still make
>> use of `font-lock-syntactic-keywords`.  This var has been obsolete
>> since Emacs-24.1 and it's like to disappear in Emacs-29, so it would
>> be good to fix this issue (maybe using
>> `syntax-propertize-via-font-lock`).

> Thanks for the heads-up, I've done that just now.

>   
> https://git.savannah.gnu.org/cgit/auctex.git/commit/?id=ab3bfaf1033f0d6e0ecfe3172794a6363450b207

Unfortunately, this breaks regression test and results in:
SUMMARY OF TEST RESULTS
-----------------------
Files examined: 13
Ran 49 tests, 35 results as expected, 11 unexpected, 3 skipped
3 files contained unexpected results:
  latex/latex-test.log
  latex/font-latex-test.log
  context/context-test.log

I encountered the same problem when I tried similar approach before. The
reasons of these errors is that
1. `indent-according-to-mode' requires that syntax propertization is
   done.
2. `syntax-propertize-via-font-lock' needs font lock has been
   initialized in that buffer.
3. In batch mode, tests run before font lock is initialized because it
   doesn't enter command wait loop. For example,
(with-temp-buffer
  (LaTeX-mode)
  syntax-propertize-function)
   returns nil.

Attached is my preliminary attempt to avoid the errors by circumventing
`syntax-propertize-via-font-lock'. (Sorry, it involves some unrelated
changes.) Though I haven't finished it yet, I expect it works basically
and doesn't interfere with regression tests.

The problems is that it requires some changes in entries in
`font-latex-syntactic-keywords', sometimes rather non-trivial ones as
seen in the patch. It's possible to do required changes in files
distributed with AUCTeX, but if some user adds entries to
`font-latex-syntactic-keywords' in their private styles, this
incompatibility can be a non-easy problem.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine

diff --git a/font-latex.el b/font-latex.el
index 8b1fec60..1468edd6 100644
--- a/font-latex.el
+++ b/font-latex.el
@@ -1003,9 +1003,6 @@ The car is used for subscript, the cdr is used for superscripts."
 The form should be the same as in `font-lock-syntactic-keywords'.")
 (make-variable-buffer-local 'font-latex-syntactic-keywords-extra)
 
-;; Set and updated in `font-latex-set-syntactic-keywords'.
-(defvar font-latex-doctex-syntactic-keywords nil)
-
 (defun font-latex-set-syntactic-keywords ()
   "Set the variable `font-latex-syntactic-keywords'.
 This function can be used to refresh the variable in case other
@@ -1028,8 +1025,7 @@ have changed."
                                        (regexp-opt verb-macros-with-braces))
           font-latex-syntactic-keywords nil)
     (unless (= (length verb-envs) 0)
-      (add-to-list
-       'font-latex-syntactic-keywords
+      (push
        `(,(concat
            "^[ \t]*\\\\begin *{\\(?:" verb-envs "\\)}"
            ;; Some environments accept an optional and/or mandatory
@@ -1053,9 +1049,17 @@ have changed."
            ;; catches the case where verbatim content is written
            ;; immediately after the \begin{verbatim}.
            "\\(\n\\|.\\)")
-         (1 "|" t)))
-      (add-to-list
-       'font-latex-syntactic-keywords
+         (1 "|")
+         ;; Span non-nil `syntax-multiline' property over
+         ;; \begin{verbatim}[WHOLE OPTIONAL ARGUMENT] so that editing
+         ;; inside the multi-line optional arguments won't turn off
+         ;; verbatim faces inside the environments even when those
+         ;; contents are very large.
+         (0 (ignore
+             (put-text-property (match-beginning 0) (match-end 0)
+                                'syntax-multiline t))))
+       font-latex-syntactic-keywords)
+      (push
        ;; Using the newline character for the syntax property often
        ;; resulted in fontification problems when text was inserted at
        ;; the end of the verbatim environment.  That's why we now use
@@ -1063,10 +1067,10 @@ have changed."
        ;; `font-latex-make-user-keywords' to remove the spurious
        ;; fontification of the backslash.
        `(,(concat "\\(\\\\\\)end *{\\(?:" verb-envs "\\)}")
-         (1 "|" t))))
+         (1 "|"))
+       font-latex-syntactic-keywords))
     (unless (= (length verb-macros-with-delims) 0)
-      (add-to-list
-       'font-latex-syntactic-keywords
+      (push
        `(,(concat "\\\\\\(?:" verb-macros-with-delims "\\)"
                   ;; Some macros take an optional argument.  This is
                   ;; the same line as above for environments.
@@ -1074,29 +1078,46 @@ have changed."
                   ;; An opening curly brace as delimiter is valid, but
                   ;; allowing it might screw up fontification of stuff
                   ;; like "\url{...} foo \textbf{<--!...}".
-                  "\\([^a-z@*\n\f{]\\).*?"
+                  "\\([^a-z@*\n\f{]\\)")
+         (1 (prog1 "\""
+              (skip-chars-forward (string ?^ (preceding-char))
+                                  (line-end-position))
+              (if (eolp)
+                  nil
+                (let ((p (point)))
+                  (put-text-property p (1+ p) 'syntax-table
+                                     (string-to-syntax "\""))
                   ;; Give an escape char at the end of the verbatim
                   ;; construct punctuation syntax.  Prevents wrong
                   ;; fontification of stuff like "\verb|foo\|".
-                  "\\(" (regexp-quote TeX-esc) "*\\)\\(\\1\\)")
-         (1 "\"") (2 ".") (3 "\""))))
+                  (save-excursion
+                    (when (> 0 (skip-chars-backward "\\\\"))
+                      (put-text-property (point) p 'syntax-table
+                                         (string-to-syntax ".")))))))))
+       font-latex-syntactic-keywords))
     (unless (= (length verb-macros-with-braces) 0)
-      (add-to-list
-       'font-latex-syntactic-keywords
+      (push
        `(,(concat "\\\\\\(?:" verb-macros-with-braces "\\)"
                   ;; Some macros take an optional argument.  This is
                   ;; the same line as above for environments.
                   "\\(?:\\[[^][]*\\(?:\\[[^][]*\\][^][]*\\)*\\]\\)?"
                   "\\({\\).*?[^\\]\\(?:\\\\\\\\\\)*\\(}\\)")
-         (1 "|") (2 "|")))))
+         (1 "|") (2 "|"))
+       font-latex-syntactic-keywords)))
   (when font-latex-syntactic-keywords-extra
     (nconc font-latex-syntactic-keywords font-latex-syntactic-keywords-extra))
   ;; Cater for docTeX mode.
-  (setq font-latex-doctex-syntactic-keywords
-        (append font-latex-syntactic-keywords
-                ;; For docTeX comment-in-doc.
-                '(("\\(\\^\\)\\^A" (1 (font-latex-doctex-^^A)))))))
-
+  (if (derived-mode-p 'doctex-mode)
+      (setq font-latex-syntactic-keywords
+            (append font-latex-syntactic-keywords
+                    ;; For docTeX comment-in-doc.
+                    '(("\\(\\^\\)\\^A" (1 (font-latex-doctex-^^A)))))))
+  ;;
+  (setq syntax-propertize-function
+        (byte-compile
+         (eval '(syntax-propertize-rules
+                 font-latex-syntactic-keywords)
+               t))))
 
 ;;; Syntactic fontification
 
@@ -1258,15 +1279,6 @@ triggers Font Lock to recognize the change."
   (when (fboundp 'font-lock-flush)
     (font-lock-flush)))
 
-(defun font-latex-syntax-propertize-function (start end)
-  "The `syntax-propertize-function' for (La)TeX documents."
-  (with-no-warnings
-    (let ((font-lock-syntactic-keywords
-           (if (derived-mode-p 'doctex-mode)
-               font-latex-doctex-syntactic-keywords
-             font-latex-syntactic-keywords)))
-      (font-lock-fontify-syntactic-keywords-region start end))))
-
 ;;;###autoload
 (defun font-latex-setup ()
   "Setup this buffer for LaTeX font-lock.  Usually called from a hook."
@@ -1297,10 +1309,9 @@ triggers Font Lock to recognize the change."
             font-latex-extend-region-backwards-command-in-braces
             font-latex-extend-region-backwards-quotation
             font-latex-extend-region-backwards-math)
-           (syntax-propertize-function
-            . font-latex-syntax-propertize-function)
            (syntax-propertize-extend-region-functions
             syntax-propertize-wholelines
+            syntax-propertize-multiline
             font-latex-sp-extend-region-backwards-verb-env))))
     ;; Add the mode-dependent stuff to the basic variables defined above.
     (if (eq major-mode 'doctex-mode)
@@ -2232,8 +2243,6 @@ set to french, and >>german<< (and 8-bit) are used if set to german."
 
 (defface font-latex-doctex-preprocessor-face
   '((t (:inherit (font-latex-doctex-documentation-face
-                  font-lock-builtin-face ; Emacs 21 does not provide
-                                        ; the preprocessor face.
                   font-lock-preprocessor-face))))
   "Face used to highlight preprocessor directives in docTeX mode."
   :group 'font-latex-highlighting-faces)
@@ -2266,17 +2275,13 @@ set to french, and >>german<< (and 8-bit) are used if set to german."
              ;; syntax-table can't deal with.  We could turn it
              ;; into a non-comment, or use `\n%' or `%^' as the comment.
              ;; Instead, we include it in the ^^A comment.
-             (eval-when-compile (string-to-syntax "< b"))
-           ;; FIXME: Those `eval-when-compile' shouldn't be needed any
-           ;; more since the byte-compiler will precompute those calls
-           ;; anyway (because `string-to-syntax'  is marked as pure).
-           (eval-when-compile (string-to-syntax ">"))))
+             (string-to-syntax "< b")
+           (string-to-syntax ">")))
         (let ((end (line-end-position)))
           (if (< end (point-max))
               (put-text-property end (1+ end) 'syntax-table
-                                 (eval-when-compile
-                                   (string-to-syntax "> b")))))
-        (eval-when-compile (string-to-syntax "< b")))))
+                                 (string-to-syntax "> b"))))
+        (string-to-syntax "< b"))))
 
 ;; Copy and adaptation of `doctex-font-lock-syntactic-face-function'
 ;; in `tex-mode.el' of CVS Emacs (March 2004)
@@ -2298,16 +2303,6 @@ set to french, and >>german<< (and 8-bit) are used if set to german."
 ;; latex-mode, then please just add `font-latex-setup' to `latex-mode-hook'
 ;; yourself.
 
-;;; Byte-compilation of generated functions
-;; Not needed now that we generate the code via a macro.
-;; (when (byte-code-function-p
-;;        (symbol-function 'font-latex-make-built-in-keywords))
-;;   (dolist (elt font-latex-built-in-keyword-classes)
-;;     (let ((name (nth 0 elt)))
-;;       (byte-compile (intern (concat "font-latex-match-" name)))
-;;       (byte-compile (intern (concat "font-latex-match-" name "-make"))))))
-
-
 ;; Provide ourselves:
 (provide 'font-latex)
 

Reply via email to