[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Toning down font-lock
From: |
Stefan Monnier |
Subject: |
Re: Toning down font-lock |
Date: |
Tue, 08 Mar 2022 10:18:36 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) |
>> Nice. I'd suggest you merge `font-lock-ignore--test` and
>> `font-lock-ignore--test-one` into a single (recursive) function.
> The exclamation marks are not a recursive thing.
> But I added `and' and `not' rules that use recursion.
Ah, I see I had misunderstood the syntax and semantics of your toplevel,
so indeed to bring it into the recursion, you'd need a new thingy which
might be called maybe `or` with a syntax like (or RULES .. [!] RULE ..).
See some nitpicks below.
Stefan
> + - A symbol, say a face name. It matches any font-lock rule
> + mentioning that symbol anywhere. Asterisks are treated as
> + wildcards.
I suggest we call it a glob pattern so it's a known pattern
matching thingy and we don't need to reinvent and re-document how to
make it match an actual `*`.
> @@ -1810,9 +1842,11 @@ font-lock-compile-keywords
> (error "Font-lock trying to use keywords before setting them up"))
> (if (eq (car-safe keywords) t)
> keywords
> - (setq keywords
> - (cons t (cons keywords
> - (mapcar #'font-lock-compile-keyword keywords))))
> + (let ((compiled (mapcar #'font-lock-compile-keyword keywords)))
> + (setq keywords `(t ,keywords ,@(if (and font-lock-ignore
> + (not syntactic-keywords))
> + (font-lock--filter-keywords
> compiled)
> + compiled))))
I think I'd move the test of `font-lock-ignore` into
`font-lock--filter-keywords` so it's the only function which consults it.
> @@ -1883,6 +1917,56 @@ font-lock-choose-keywords
> (t
> (car keywords))))
>
> +(defun font-lock--test-keyword (rule keyword)
That sadly doesn't say whether it return nil when it matches or whether
it returns non-nil when it matches. I suggest to rename it to something
like `font-lock--matches-keyword` so the name clearly say when we return
nil and when we return non-nil.
> + "Test whether font-lock KEYWORD matches a RULE.
> +See `font-lock-ignore' for the possible rules."
Same comment ;-)
> + (pcase-exhaustive rule
> + ('* t)
> + ((pred symbolp)
> + (let* ((name (symbol-name rule))
> + (regexp (when (string-match-p "\\*" name)
> + (let* ((words (mapcar #'regexp-quote
> + (split-string name "\\*")))
> + (joined (string-join words ".*")))
> + (concat "\\`" joined "\\'")))))
We can use `wildcard-to-regexp` here.
> + (if regexp
> + (seq-some (lambda (obj)
> + (when (symbolp obj)
> + (string-match-p regexp (symbol-name obj))))
> + (flatten-tree keyword))
> + (memq rule (flatten-tree keyword)))))
Performance likely doesn't matter, but I suspect it'd be faster if we
recursed over the data-structure rather than flattening it.
E.g. something like
(named-let search ((obj keyword))
(cond
((consp obj) (or (search (car obj)) (search (cdr obj))))
((and obj (symbolp obj))
(string-match-p regexp (symbol-name obj)))))