From: jan.synacek@posteo.org
To: help-gnu-emacs@gnu.org
Subject: Using face names as FACESPEC in font-lock-keywords does not work
Date: Wed, 22 Jun 2022 08:33:49 +0000 [thread overview]
Message-ID: <a7bf9753bf3dd458136b3d5b59c32449@posteo.de> (raw)
Hello!
I have the following piece of code that is supposed to add highlighting
for CPP-like conditionals in my mode:
(setq-local font-lock-defaults
(let* ((cpp-rx (rx line-start
"#" (or "if" "ifdef" "else" "elif"
"endif")
word-boundary))
(keywords
`((,cpp-rx . xref-file-header))))
(list keywords)))
This piece of code does not work and does not use the 'xref-file-header'
face.
In the Elisp manual (24.6.2 Search-based Fontification) it says:
"
-- Variable: font-lock-keywords
...
...
Each element of ‘font-lock-keywords’ should have one of these forms:
...
‘(MATCHER . FACESPEC)’
In this kind of element, FACESPEC is an expression whose value
specifies the face to use for highlighting. In the simplest case,
FACESPEC is a Lisp variable (a symbol) whose value is a face name.
...
"
I find the language here quite confusing. 'xref-file-header' is a symbol
that evaluates to itself, which also happens to be the face name, and is
technically an expression. But apparently, that's not enough, as it
seems that the symbol itself has to have a value cell in this particular
case.
Also, in 40.12.2 Defining Faces, it says:
"
People are sometimes tempted to create a variable whose value is a
face name. In the vast majority of cases, this is not necessary; the
usual procedure is to define a face with ‘defface’, and then use its
name directly.
"
This piece of documentation doesn't really help, too. It seems that my
case is exactly the minority case where it's actually needed. Using
faces like 'font-lock-string-face' works, because they are defined
exactly as the documentation says is not necessary. And evaluating
"(defvar xref-file-header 'xref-file-header)" fixes the highlighting,
too.
So my question is: Am I doing something wrong? Or does this case
(setting font-lock-keywords) really
require a variable which holds the face name instead of using the face
name directly?
In any case, I think that the documentation deserves a bit of
sharpening.
Regards,
Jan Synáček
next reply other threads:[~2022-06-22 8:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-22 8:33 jan.synacek [this message]
2022-06-22 20:42 ` Using face names as FACESPEC in font-lock-keywords does not work Emanuel Berg
2022-06-22 20:46 ` Emanuel Berg
2022-06-23 5:36 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a7bf9753bf3dd458136b3d5b59c32449@posteo.de \
--to=jan.synacek@posteo.org \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).