unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Using face names as FACESPEC in font-lock-keywords does not work
@ 2022-06-22  8:33 jan.synacek
  2022-06-22 20:42 ` Emanuel Berg
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: jan.synacek @ 2022-06-22  8:33 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using face names as FACESPEC in font-lock-keywords does not work
  2022-06-22  8:33 Using face names as FACESPEC in font-lock-keywords does not work jan.synacek
@ 2022-06-22 20:42 ` Emanuel Berg
  2022-06-22 20:46 ` Emanuel Berg
  2022-06-23  5:36 ` Eli Zaretskii
  2 siblings, 0 replies; 4+ messages in thread
From: Emanuel Berg @ 2022-06-22 20:42 UTC (permalink / raw)
  To: help-gnu-emacs

jan.synacek wrote:

> 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")

That's not C++ ... or what is it, the preprocessor?

>                                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 [...] In the simplest case [...]

Did you make it work "in the simplest case"?

Do that, so you know it works _at all_, if it does you can add
complexity all day every day.

I made it work two times:

  https://dataswamp.org/~incal/emacs-init/gnus/browse.el (simple)
  https://dataswamp.org/~incal/emacs-init/buffer-menu.el (???)

They work, 100% is all I'll say.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using face names as FACESPEC in font-lock-keywords does not work
  2022-06-22  8:33 Using face names as FACESPEC in font-lock-keywords does not work jan.synacek
  2022-06-22 20:42 ` Emanuel Berg
@ 2022-06-22 20:46 ` Emanuel Berg
  2022-06-23  5:36 ` Eli Zaretskii
  2 siblings, 0 replies; 4+ messages in thread
From: Emanuel Berg @ 2022-06-22 20:46 UTC (permalink / raw)
  To: help-gnu-emacs

> 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))

They are `font-lock-preprocessor-face' in `c++-mode'. No good?

Actually the need for a preprocessor is indicative of
a language that lacks in ... something?

> In any case, I think that the documentation deserves a bit
> of sharpening.

Do it yourself Jan :)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using face names as FACESPEC in font-lock-keywords does not work
  2022-06-22  8:33 Using face names as FACESPEC in font-lock-keywords does not work jan.synacek
  2022-06-22 20:42 ` Emanuel Berg
  2022-06-22 20:46 ` Emanuel Berg
@ 2022-06-23  5:36 ` Eli Zaretskii
  2 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2022-06-23  5:36 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Wed, 22 Jun 2022 08:33:49 +0000
> From: jan.synacek@posteo.org
> 
> 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.

The documentation you cite is of font-lock-keywords, but your code
uses font-lock-defaults, a variable whose value is supposed to be
something very different.  If that is a typo, then which of these two
is correct?

> 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.

Please note that it describes a different use case: how to _define_ a
face, not how to use faces in font-lock.  Faces can be used in Emacs
in contexts completely unrelated to font-lock.

> 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?

My suggestion is to use one of the font-lock faces, not something
unrelated.  I don't really understand how xref-file-header face could
be related to highlighting CPP-like conditionals in the first place.
If you like its colors, why not customize one of the rarely-used
font-lock faces to have those colors, and then use that?

But if you insist on using the above, and if you must understand how
to force Emacs to do what you want in that case, that's possibly a
separate discussion, so please do indicate your intentions.  (And yes,
faces are a tricky subject and can easily confuse in corner cases, and
your code uses some "advanced" techniques, so a little wonder you bump
into that.)



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-23  5:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-22  8:33 Using face names as FACESPEC in font-lock-keywords does not work jan.synacek
2022-06-22 20:42 ` Emanuel Berg
2022-06-22 20:46 ` Emanuel Berg
2022-06-23  5:36 ` Eli Zaretskii

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).