unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* font-lock-keywords uses only facename
@ 2004-06-03 23:48 Alex Schroeder
  2004-06-03 23:55 ` Miles Bader
  0 siblings, 1 reply; 10+ messages in thread
From: Alex Schroeder @ 2004-06-03 23:48 UTC (permalink / raw)


Today I wrote a very small generic mode for lists of emails.
define-generic-mode's parameter font-lock-list is used to set
font-lock-keywords, eventually.

My problem was that I didn't know that font-lock-keywords doesn't
take faces.  It only takes expressions that evaluate to faces.
Therefore, the following font-lock-list doesn't work.  Only the
keyword regexp is shown:

(defface mail-list-whitespace '((t (:background "orange"))) "For whitespace.")
(defface mail-list-empty-lines '((t (:background "red"))) "For empty lines.")

(define-generic-mode 'mail-list-mode
  nil
  nil
  '(("^[a-z]+@[a-z.]+\\.[a-z][a-z]+$" . font-lock-keyword-face)
    ("[ \t]+" . mail-list-whitespace)
    ("^\n" . mail-list-empty-lines))
  '("\\blist\\'")
  nil
  "Mode used for Minimalist lists: One email per line.")

I must either use variables:

(setq mail-list-whitespace 'mail-list-whitespace)
(setq mail-list-empty-lines 'mail-list-empty-lines)

Or I must use quotes inside the list:

(define-generic-mode 'mail-list-mode
  nil
  nil
  '(("^[a-z]+@[a-z.]+\\.[a-z][a-z]+$" . font-lock-keyword-face)
    ("[ \t]+" . 'mail-list-whitespace)
    ("^\n" . 'mail-list-empty-lines))
  '("\\blist\\'")
  nil
  "Mode used for Minimalist lists: One email per line.")

Is this on purpose?  Would it break anything if using faces were
allowed directly?

Alex.
-- 
.O.  http://www.emacswiki.org/alex/
..O  Schroeder's fourth law:
OOO  None of your friends and coworkers share your taste in music.

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

* Re: font-lock-keywords uses only facename
  2004-06-03 23:48 font-lock-keywords uses only facename Alex Schroeder
@ 2004-06-03 23:55 ` Miles Bader
  2004-06-04  3:33   ` Stefan Monnier
  0 siblings, 1 reply; 10+ messages in thread
From: Miles Bader @ 2004-06-03 23:55 UTC (permalink / raw)
  Cc: emacs-devel

On Fri, Jun 04, 2004 at 01:48:10AM +0200, Alex Schroeder wrote:
> Is this on purpose?  Would it break anything if using faces were
> allowed directly?

It's just historical stupidity.  If it were being designed today, it would
probably just use face names directly, but font-lock is _really_ old...

I presume you're asking whether it could do something like:

   (if (and (symbolp expr) (not (boundp expr)))
       expr	   ; use face name directly
     (eval expr))  ; evaluate EXPR to get face name

?

It seems like it would work, though I wonder if it could cause more
confusion...

-Miles
-- 
Occam's razor split hairs so well, I bought the whole argument!

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

* Re: font-lock-keywords uses only facename
  2004-06-03 23:55 ` Miles Bader
@ 2004-06-04  3:33   ` Stefan Monnier
  2004-06-04  3:41     ` Miles Bader
  2004-06-05 13:47     ` Richard Stallman
  0 siblings, 2 replies; 10+ messages in thread
From: Stefan Monnier @ 2004-06-04  3:33 UTC (permalink / raw)
  Cc: emacs-devel, Alex Schroeder

> It's just historical stupidity.

Well, I wouldn't put it qute like this.  Given the fact that any expression
is allowed, and that quoting can be used to get the "unevaluated" behavior,
the current behavior has the advantage of being general and simple.

> If it were being designed today, it would probably just use face names
> directly,

If I were writing it today, I'd just do an `eval' as did Simon Marshall:
it's simpler and there is no loss of generality.  And adding a quote is
really not that difficult.

> but font-lock is _really_ old...

Indeed.  A complete overhaul is long overdue.

> I presume you're asking whether it could do something like:

>    (if (and (symbolp expr) (not (boundp expr)))
>        expr	   ; use face name directly
>      (eval expr))  ; evaluate EXPR to get face name

> ?

> It seems like it would work, though I wonder if it could cause more
> confusion...

I think if we really care about it (although it hasn't bothered us that
much for the last however many years, so I doubt it's worth the trouble),
we could do something like:

- when "compiling" the keywords, do something like

    (if (and (symbolp expr) (not (boundp expr)) (facep expr))
        (list 'quote expr)
      expr)

- put a condition-case around font-lock-fontify-keywords-region like

    (condition-case unbound-err
        ...blabla...
      (void-variable
        (let ((var (cadr unbound-err)))
          (if (and (symbolp var) (not (boundp var)) (facep var))
              (error "Face %s needs to be quoted in font-lock-keywords" var)
            (signal (car unbound-err) (cdr unbound-err))))))


-- Stefan

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

* Re: font-lock-keywords uses only facename
  2004-06-04  3:33   ` Stefan Monnier
@ 2004-06-04  3:41     ` Miles Bader
  2004-06-04  3:58       ` Stefan Monnier
  2004-06-05 13:47     ` Richard Stallman
  1 sibling, 1 reply; 10+ messages in thread
From: Miles Bader @ 2004-06-04  3:41 UTC (permalink / raw)
  Cc: emacs-devel, Alex Schroeder

Stefan Monnier <monnier@iro.umontreal.ca> writes:
> If I were writing it today, I'd just do an `eval' as did Simon Marshall:
> it's simpler and there is no loss of generality.  And adding a quote is
> really not that difficult.

Does anyone ever use the eval feature, except old code that has `face'
variables (a practice which seems out of favor these days)?

What's certainly the case is that it often causes confusion, although
admittedly some of this is due to font-lock's `standard' faces using
variables and faces with exactly the same name.

-Miles
-- 
"Whatever you do will be insignificant, but it is very important that
 you do it."  Mahatma Gandhi

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

* Re: font-lock-keywords uses only facename
  2004-06-04  3:41     ` Miles Bader
@ 2004-06-04  3:58       ` Stefan Monnier
  2004-06-04  4:08         ` Miles Bader
  0 siblings, 1 reply; 10+ messages in thread
From: Stefan Monnier @ 2004-06-04  3:58 UTC (permalink / raw)
  Cc: emacs-devel, Alex Schroeder

>> If I were writing it today, I'd just do an `eval' as did Simon Marshall:
>> it's simpler and there is no loss of generality.  And adding a quote is
>> really not that difficult.

> Does anyone ever use the eval feature, except old code that has `face'
> variables (a practice which seems out of favor these days)?

Well, the "eval feature" is used extensively at many places, yes.

If you mean "use `eval' even for trivial expressions composed of nothing
more than a symbol", I've used it as a poor man's buffer-local faces
a few times.  But admittedly, it's only rarely used.

> What's certainly the case is that it often causes confusion, although
> admittedly some of this is due to font-lock's `standard' faces using
> variables and faces with exactly the same name.

Right, I think if variables had names like `font-lock-foo-face' while their
content was `font-lock-foo' it would have helped a bit.

Another way to avoid the confusion would have been to introduce a whole new
self-evaluating datatype for faces rather than use symbols.


        Stefan

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

* Re: font-lock-keywords uses only facename
  2004-06-04  3:58       ` Stefan Monnier
@ 2004-06-04  4:08         ` Miles Bader
  2004-06-05 15:58           ` Stefan
  0 siblings, 1 reply; 10+ messages in thread
From: Miles Bader @ 2004-06-04  4:08 UTC (permalink / raw)
  Cc: emacs-devel, Alex Schroeder

Stefan Monnier <monnier@iro.umontreal.ca> writes:
> Well, the "eval feature" is used extensively at many places, yes.

Can you explain a typical usage?  Does font-lock make guarantees about
what context the eval will be done in?  I can understand perhaps wanting
some sort of evaluation to compute the regexp, but it seems like
computing the actual face name would be of limited utility unless it can
use context information.

-Miles
-- 
((lambda (x) (list x x)) (lambda (x) (list x x)))

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

* Re: font-lock-keywords uses only facename
  2004-06-04  3:33   ` Stefan Monnier
  2004-06-04  3:41     ` Miles Bader
@ 2004-06-05 13:47     ` Richard Stallman
  2004-06-05 23:03       ` Miles Bader
  1 sibling, 1 reply; 10+ messages in thread
From: Richard Stallman @ 2004-06-05 13:47 UTC (permalink / raw)
  Cc: emacs-devel, alex, miles

    - when "compiling" the keywords, do something like

	(if (and (symbolp expr) (not (boundp expr)) (facep expr))
	    (list 'quote expr)
	  expr)

That sounds like a good idea to me.  It is simple and does the job.

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

* Re: font-lock-keywords uses only facename
  2004-06-04  4:08         ` Miles Bader
@ 2004-06-05 15:58           ` Stefan
  0 siblings, 0 replies; 10+ messages in thread
From: Stefan @ 2004-06-05 15:58 UTC (permalink / raw)
  Cc: emacs-devel, Alex Schroeder

>> Well, the "eval feature" is used extensively at many places, yes.
> Can you explain a typical usage?

I think a typical usage is when you want to supplement the regexp by
some elisp condition.
Another is when you want to not only add a face but also do a few more
things like put an overlay.

The code is evaluated right after the call to re-search-forward (thus the
match-data is available, ...).


        Stefan

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

* Re: font-lock-keywords uses only facename
  2004-06-05 13:47     ` Richard Stallman
@ 2004-06-05 23:03       ` Miles Bader
  2004-06-06 22:33         ` Richard Stallman
  0 siblings, 1 reply; 10+ messages in thread
From: Miles Bader @ 2004-06-05 23:03 UTC (permalink / raw)


Richard Stallman <rms@gnu.org> writes:
>     - when "compiling" the keywords, do something like
>
> 	(if (and (symbolp expr) (not (boundp expr)) (facep expr))
> 	    (list 'quote expr)
> 	  expr)
>
> That sounds like a good idea to me.  It is simple and does the job.

I wonder if the `boundp' check could result in fragile behavior though
-- if someone writes a font-lock expression depending on the above
behavior, and then later a variable gets added that uses the same name,
suddenly the font-lock expression will start trying to use the variable
value, which might be a very confusing bug to find.

Except for the standard `font-lock-...-face' variables, do we have any
idea how widespread the use of variables-pointing-to-face-names in
font-lock expressions is?

If it's extremely rare, I'd be um, less nervous (don't want to say "more
happy" :-), simply making a rule that says "you can't use variable names
there except for the standard ones", i.e., use this code instead:

   (if (and (symbolp expr) (not (memq expr standard-face-variables)))
       (list 'quote expr)
     expr)

It would be kinda ugly, less backward compatible, and less `simple', but
maybe safer and more maintainable in the long run...

-Miles
-- 
P.S.  All information contained in the above letter is false,
      for reasons of military security.

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

* Re: font-lock-keywords uses only facename
  2004-06-05 23:03       ` Miles Bader
@ 2004-06-06 22:33         ` Richard Stallman
  0 siblings, 0 replies; 10+ messages in thread
From: Richard Stallman @ 2004-06-06 22:33 UTC (permalink / raw)
  Cc: emacs-devel

    Except for the standard `font-lock-...-face' variables, do we have any
    idea how widespread the use of variables-pointing-to-face-names in
    font-lock expressions is?

    If it's extremely rare, I'd be um, less nervous (don't want to say "more
    happy" :-), simply making a rule that says "you can't use variable names
    there except for the standard ones", i.e., use this code instead:

       (if (and (symbolp expr) (not (memq expr standard-face-variables)))
	   (list 'quote expr)
	 expr)

If that is rare, I think this would be a good solution.
Does someone want to check?

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

end of thread, other threads:[~2004-06-06 22:33 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-06-03 23:48 font-lock-keywords uses only facename Alex Schroeder
2004-06-03 23:55 ` Miles Bader
2004-06-04  3:33   ` Stefan Monnier
2004-06-04  3:41     ` Miles Bader
2004-06-04  3:58       ` Stefan Monnier
2004-06-04  4:08         ` Miles Bader
2004-06-05 15:58           ` Stefan
2004-06-05 13:47     ` Richard Stallman
2004-06-05 23:03       ` Miles Bader
2004-06-06 22:33         ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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