[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Migrating from font-lock-syntactic-keywords to syntax-propertize-fun
From: |
Tassilo Horn |
Subject: |
Re: Migrating from font-lock-syntactic-keywords to syntax-propertize-function |
Date: |
Thu, 14 May 2020 11:56:31 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
Tassilo Horn <tsdh@gnu.org> writes:
> thank you both, I think I somehow made it.
>
> http://git.savannah.gnu.org/cgit/auctex.git/commit/?h=font-latex-update&id=e6076a4f8cf2f06e956ad1a60728ca3b2eb11a83
That was a bit too quick. It didn't work because we have a lot of
matchers with back-references in order to set syntax properties for
things like \verb|(setq foo t)| where the delimiter (here |) may be any
character, e.g., \verb/(setq foo t)/, \verb-(setq foo t)- would also be
ok.
So for now I this `syntax-propertize-function':
--8<---------------cut here---------------start------------->8---
(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))))
--8<---------------cut here---------------end--------------->8---
That works good but of course is still the obsolete mechanism, right?
However, I don't feel like implementing the search and syntax-property
setting myself would have a better outcome.
Why does `syntax-propertize-rules' not support back-references? It
already tracks which regexp group is what, so it could also renumber
back-references, no? Natively hacking away, it seems like this would do
the trick:
--8<---------------cut here---------------start------------->8---
modified lisp/emacs-lisp/syntax.el
@@ -140,13 +140,15 @@ syntax-propertize-multiline
(cons beg end))
(defun syntax-propertize--shift-groups (re n)
- (replace-regexp-in-string
- "\\\\(\\?\\([0-9]+\\):"
- (lambda (s)
- (replace-match
- (number-to-string (+ n (string-to-number (match-string 1 s))))
- t t s 1))
- re t t))
+ (let ((incr (lambda (s)
+ (replace-match
+ (number-to-string
+ (+ n (string-to-number (match-string 1 s))))
+ t t s 1))))
+ (replace-regexp-in-string
+ "[^\\]\\\\\\([0-9]+\\)" incr
+ (replace-regexp-in-string "\\\\(\\?\\([0-9]+\\):" incr re t t)
+ t t)))
(defmacro syntax-propertize-precompile-rules (&rest rules)
"Return a precompiled form of RULES to pass to `syntax-propertize-rules'.
--8<---------------cut here---------------end--------------->8---
Judging from the result of
--8<---------------cut here---------------start------------->8---
(syntax-propertize-rules
("\\(one\\)\\(two\\)\\(\\1\\)" (1 "|") (2 "."))
("\\(three\\)\\(four\\)\\(\\1\\)" (1 "|") (2 ".")))
--8<---------------cut here---------------end--------------->8---
I'd say it works (in this simple case).
Bye,
Tassilo