unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Background colors in font-lock-keywords
@ 2003-05-27 11:53 Jesse Sheidlower
  2003-05-27 14:35 ` Stefan Monnier
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Jesse Sheidlower @ 2003-05-27 11:53 UTC (permalink / raw)



I'm developing a derived mode of psgml in order to set up my own
highlighting of particular tags and tag groups using font-lock,
and I'm having trouble with two things relating to the background
of faces I'm using.

First, I'd like certain faces to use as a background any other
element they happen to be on. For example, in my font-lock-keywords
section I define entity references for this application as:

  ("&[a-zA-z]+;" . (0 my-entity-face t))

I've defined my-entity-face with a red foreground color and a
weight of bold. What I would like is for the background to
match whatever it's on, so that if there's an entity reference
in text that happens to be white, it will be red with a white
background, but if, in another font-lock-keywords expression,
I have defined

  ("<title>\\(.*?\\)</title>" 1 my-title-face t)

, with my-title-face having a light-green background, then I
would like an entity reference used in a <title> to be red but
also with a light-green background. Is there any way of
accomplishing this? I've been experimenting to no avail.

Second, is there a way to associate a background color with a particular
buffer only? For this same mode, I would like to get a lighly shaded
background with white as the main background color for actual sgml (i.e.
at the end of a line, text will appear light gray after the last closing
tag). I have accomplished this by, in my mode definition,

  (set-background-color "gray90")

and then in my font-lock-keywords,

  ("^\\(<.*>\\)$" 1 my-background-face keep)

, with my-background-face defined as just having a white background,
and then (to solve the same problem as above) having other faces
inherit from this background. Is there a better way to do this? One
particular problem is that when I kill a buffer in this mode, I'm left
with a light-gray buffer wherever else I am, which is annoying, so
at the least I'd like a way to get the grayness to be associated only
with something in this mode.

Thanks.

Jesse Sheidlower

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

* Re: Background colors in font-lock-keywords
  2003-05-27 11:53 Background colors in font-lock-keywords Jesse Sheidlower
@ 2003-05-27 14:35 ` Stefan Monnier
  2003-05-28 16:09   ` Jesse Sheidlower
  2003-05-27 16:30 ` Kevin Rodgers
  2003-05-27 22:23 ` Oliver Scholz
  2 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier @ 2003-05-27 14:35 UTC (permalink / raw)


>>>>> "Jesse" == Jesse Sheidlower <jester@panix.com> writes:
>   ("&[a-zA-z]+;" . (0 my-entity-face t))
                                      ^^
This tells font-lock to override which ever face was there before.
See C-h v font-lock-keywords RET.
You want to use `append' or `prepend' instead.


        Stefan

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

* Re: Background colors in font-lock-keywords
  2003-05-27 11:53 Background colors in font-lock-keywords Jesse Sheidlower
  2003-05-27 14:35 ` Stefan Monnier
@ 2003-05-27 16:30 ` Kevin Rodgers
  2003-05-27 17:12   ` Jesse Sheidlower
  2003-05-27 22:23 ` Oliver Scholz
  2 siblings, 1 reply; 11+ messages in thread
From: Kevin Rodgers @ 2003-05-27 16:30 UTC (permalink / raw)


Jesse Sheidlower wrote:

> I'm developing a derived mode of psgml in order to set up my own
> highlighting of particular tags and tag groups using font-lock,
> and I'm having trouble with two things relating to the background
> of faces I'm using.


I'd like to hear how your project turns out.  Several years ago I implemented
something similar, but instead of using font-lock (which wasn't mature at the
time) I hacked my own (ELEMENT . OVERLAY-PROPERTY-LIST) association list.  It
had some nice features, like using overlay priorities to handled element nesting
and a special function property so you could compute overlay properties
dynamically, but I didn't maintain it to keep up with Emacs and PSGML.  Now I
think the right approach would be to implement XSL support in Emacs.


> First, I'd like certain faces to use as a background any other
> element they happen to be on. For example, in my font-lock-keywords
> section I define entity references for this application as:
> 
>   ("&[a-zA-z]+;" . (0 my-entity-face t))


Sorry I can't help you with your face questions.  But note that SGML's reference
concrete syntax (the default, used by PSGML) also allows digits, hyphen, and
period in entity names (just not as the first character); and XML additionally
allows underscore and colon (even as the first character) in entity names.  XML
also allows non-ASCII letters and other Unicode characters.  See

http://xml.coverpages.org/sgmlsyn/sgmlsyn.htm#P55
http://www.w3.org/TR/REC-xml#NT-Name


> I've defined my-entity-face with a red foreground color and a
> weight of bold. What I would like is for the background to
> match whatever it's on, so that if there's an entity reference
> in text that happens to be white, it will be red with a white
> background, but if, in another font-lock-keywords expression,
> I have defined
> 
>   ("<title>\\(.*?\\)</title>" 1 my-title-face t)


How do you handle elements that span more than 1 line?  Does font-lock handle
nested elements correctly for you?


> , with my-title-face having a light-green background, then I
> would like an entity reference used in a <title> to be red but
> also with a light-green background. Is there any way of
> accomplishing this? I've been experimenting to no avail.

...

-- 
<a href="mailto:&lt;kevin.rodgers&#64;ihs.com&gt;">Kevin Rodgers</a>

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

* Re: Background colors in font-lock-keywords
  2003-05-27 16:30 ` Kevin Rodgers
@ 2003-05-27 17:12   ` Jesse Sheidlower
  0 siblings, 0 replies; 11+ messages in thread
From: Jesse Sheidlower @ 2003-05-27 17:12 UTC (permalink / raw)


In article <3ED392AF.2030607@yahoo.com>,
Kevin Rodgers  <ihs_4664@yahoo.com> wrote:
>Jesse Sheidlower wrote:
>
>> I'm developing a derived mode of psgml in order to set up my own
>> highlighting of particular tags and tag groups using font-lock,
>> and I'm having trouble with two things relating to the background
>> of faces I'm using.
>
>
>I'd like to hear how your project turns out.  Several years ago I implemented
>something similar, but instead of using font-lock (which wasn't mature at the
>time) I hacked my own (ELEMENT . OVERLAY-PROPERTY-LIST) association list.  It
>had some nice features, like using overlay priorities to handled element nesting
>and a special function property so you could compute overlay properties
>dynamically, but I didn't maintain it to keep up with Emacs and PSGML.  Now I
>think the right approach would be to implement XSL support in Emacs.

At least for now, I'm doing this in the most basic, kludgy way possible,
as I'm new to programming in Emacs Lisp. I'd be interested to see your
hacks as well!

>> First, I'd like certain faces to use as a background any other
>> element they happen to be on. For example, in my font-lock-keywords
>> section I define entity references for this application as:
>> 
>>   ("&[a-zA-z]+;" . (0 my-entity-face t))
>
>
>Sorry I can't help you with your face questions.  But note that SGML's reference
>concrete syntax (the default, used by PSGML) also allows digits, hyphen, and
>period in entity names (just not as the first character); and XML additionally
>allows underscore and colon (even as the first character) in entity names.  XML
>also allows non-ASCII letters and other Unicode characters.  See

Yes, but this project is designed for a particular application and I
know the content of all of my entities.

In fact, I'm using a flavor of SGML where the period, rather than the
semicolon, signals the end of an entity reference, but I silently
changed it above to avoid having to explain it! That didn't work.

>> I've defined my-entity-face with a red foreground color and a
>> weight of bold. What I would like is for the background to
>> match whatever it's on, so that if there's an entity reference
>> in text that happens to be white, it will be red with a white
>> background, but if, in another font-lock-keywords expression,
>> I have defined
>> 
>>   ("<title>\\(.*?\\)</title>" 1 my-title-face t)
>
>
>How do you handle elements that span more than 1 line?  Does font-lock handle
>nested elements correctly for you?

Again, for this application, the <title> tags only appear on a single
line; if it runs longer than 80 (or whatever) characters you just
keep going, without any newlines or returns.

Handling nested elements is what my question was about ;-). For
current purposes, the entity references are the only hard thing,
as the other elements I'm choosing to highlight aren't nested.
I was hoping that an answer to my original question would help
me solve the for-now-theoretical case of <title>This is a title
with <foo>something else</foo> inside</title>.

Best,

Jesse Sheidlower

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

* Re: Background colors in font-lock-keywords
  2003-05-27 11:53 Background colors in font-lock-keywords Jesse Sheidlower
  2003-05-27 14:35 ` Stefan Monnier
  2003-05-27 16:30 ` Kevin Rodgers
@ 2003-05-27 22:23 ` Oliver Scholz
  2003-05-27 22:43   ` Oliver Scholz
  2003-05-28  0:13   ` lawrence mitchell
  2 siblings, 2 replies; 11+ messages in thread
From: Oliver Scholz @ 2003-05-27 22:23 UTC (permalink / raw)


jester@panix.com (Jesse Sheidlower) writes:
[...]
> First, I'd like certain faces to use as a background any other
> element they happen to be on. For example, in my font-lock-keywords
> section I define entity references for this application as:
>
>   ("&[a-zA-z]+;" . (0 my-entity-face t))
>
> I've defined my-entity-face with a red foreground color and a
> weight of bold. What I would like is for the background to
> match whatever it's on, so that if there's an entity reference
> in text that happens to be white, it will be red with a white
> background, but if, in another font-lock-keywords expression,
> I have defined
>
>   ("<title>\\(.*?\\)</title>" 1 my-title-face t)
>
> , with my-title-face having a light-green background, then I
> would like an entity reference used in a <title> to be red but
> also with a light-green background. Is there any way of
> accomplishing this? I've been experimenting to no avail.

Use the `prepend' or `append' keyword. For example:

(defface example-l-word-face
  '((t
     (:background "Seagreen4")))
  "Face used for words beginning with \"l\".")

(defvar example-font-lock-keywords
  '(("\\<lirum\\>" . 'font-lock-warning-face)
    ("\\<larum\\>" . 'font-lock-keyword-face)
    ("\\<l\\w+" (0 'example-l-word-face append))))

(define-derived-mode example-mode text-mode "EXAMPLE"
  "Example mode for testing font lock keywords."
  (setq font-lock-defaults '(example-font-lock-keywords)))


> Second, is there a way to associate a background color with a particular
> buffer only? 
[...]

Unfortunately there isn't a way to do this. The thing that comes
closest to this is something like:

(set (make-local-variable 'default-text-properties)
     '(face example-l-word-face))

The disadvantages are obvious.

    Oliver
-- 
9 Prairial an 211 de la Révolution
Liberté, Egalité, Fraternité!

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

* Re: Background colors in font-lock-keywords
  2003-05-27 22:23 ` Oliver Scholz
@ 2003-05-27 22:43   ` Oliver Scholz
  2003-05-28  0:13   ` lawrence mitchell
  1 sibling, 0 replies; 11+ messages in thread
From: Oliver Scholz @ 2003-05-27 22:43 UTC (permalink / raw)


Oliver Scholz <alkibiades@gmx.de> writes:

[...]
> Unfortunately there isn't a way to do this. The thing that comes
> closest to this is something like:
>
> (set (make-local-variable 'default-text-properties)
>      '(face example-l-word-face))
>
> The disadvantages are obvious.
[...]

I forgot to add: you can also do something like:

(overlay-put (make-overlay (point-min)
			   (point-max))
	     'face
	     'example-l-word-face)

Still not exactly what you want and not a canonical thing to do in a
major mode. But you don't have to add `prepend' keywords to all your
font-lock directives.

    Oliver
-- 
9 Prairial an 211 de la Révolution
Liberté, Egalité, Fraternité!

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

* Re: Background colors in font-lock-keywords
  2003-05-27 22:23 ` Oliver Scholz
  2003-05-27 22:43   ` Oliver Scholz
@ 2003-05-28  0:13   ` lawrence mitchell
  2003-05-28  7:32     ` Oliver Scholz
  1 sibling, 1 reply; 11+ messages in thread
From: lawrence mitchell @ 2003-05-28  0:13 UTC (permalink / raw)


Oliver Scholz wrote:

[...]

> (defvar example-font-lock-keywords
>   '(("\\<lirum\\>" . 'font-lock-warning-face)
>     ("\\<larum\\>" . 'font-lock-keyword-face)
>     ("\\<l\\w+" (0 'example-l-word-face append))))
                                          ^^^^^^
Surely this wants to be prepend.  At least, my reading of the
font-lock-keywords docstring, and some testing would indicate
so.

[...]

-- 
lawrence mitchell <wence@gmx.li>

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

* Re: Background colors in font-lock-keywords
  2003-05-28  0:13   ` lawrence mitchell
@ 2003-05-28  7:32     ` Oliver Scholz
  2003-05-28 13:15       ` lawrence mitchell
  0 siblings, 1 reply; 11+ messages in thread
From: Oliver Scholz @ 2003-05-28  7:32 UTC (permalink / raw)


lawrence mitchell <wence@gmx.li> writes:

> Oliver Scholz wrote:
>
> [...]
>
>> (defvar example-font-lock-keywords
>>   '(("\\<lirum\\>" . 'font-lock-warning-face)
>>     ("\\<larum\\>" . 'font-lock-keyword-face)
>>     ("\\<l\\w+" (0 'example-l-word-face append))))
>                                           ^^^^^^
> Surely this wants to be prepend.  At least, my reading of the
> font-lock-keywords docstring, and some testing would indicate
> so.
[...]

Erm, this code was just example code, so I am personally not so sure
which one should take precedence, the old fontification or the new
one. It's hard for me to make an educated guess, because the major
mode has no meaning, and so the fontification has no meaning.

But maybe you have a more general reason to say this. Could you
please elaborate?

    Oliver
-- 
9 Prairial an 211 de la Révolution
Liberté, Egalité, Fraternité!

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

* Re: Background colors in font-lock-keywords
  2003-05-28  7:32     ` Oliver Scholz
@ 2003-05-28 13:15       ` lawrence mitchell
  2003-05-28 16:13         ` Oliver Scholz
  0 siblings, 1 reply; 11+ messages in thread
From: lawrence mitchell @ 2003-05-28 13:15 UTC (permalink / raw)


Oliver Scholz wrote:

[...] PREPEND versus APPEND in font-lock-keywords.

> Erm, this code was just example code, so I am personally not so sure
> which one should take precedence, the old fontification or the new
> one. It's hard for me to make an educated guess, because the major
> mode has no meaning, and so the fontification has no meaning.

> But maybe you have a more general reason to say this. Could you
> please elaborate?

The way I read it, if using `append', rather than `prepend',
in the example above, EXAMPLE-L-WORD-FACE would change the
background iff FONT-LOCK-WARNING-FACE and
FONT-LOCK-KEYWORD-FACE, did not set the background colour.
Certainly in my Emacs setup, said faces have had their
backgrounds set explicitly.

Maybe a font-lock wizard can jump in with a better and/or
correct explanation.

-- 
lawrence mitchell <wence@gmx.li>

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

* Re: Background colors in font-lock-keywords
  2003-05-27 14:35 ` Stefan Monnier
@ 2003-05-28 16:09   ` Jesse Sheidlower
  0 siblings, 0 replies; 11+ messages in thread
From: Jesse Sheidlower @ 2003-05-28 16:09 UTC (permalink / raw)


In article <5lllwscv9f.fsf@rum.cs.yale.edu>,
Stefan Monnier <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> wrote:
>>>>>> "Jesse" == Jesse Sheidlower <jester@panix.com> writes:
>>   ("&[a-zA-z]+;" . (0 my-entity-face t))
>                                      ^^
>This tells font-lock to override which ever face was there before.
>See C-h v font-lock-keywords RET.
>You want to use `append' or `prepend' instead.

Thanks to you and the other poster who pointed this out. I did 
look this up, but in the Reference Manual, which discussed it
in more confusing terms, and my experiment with it didn't work.
It is much more clear in the regular doc string, which I should
have checked first, and it seems it was an error in my experiment
that kept it from working.

Jesse Sheidlower

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

* Re: Background colors in font-lock-keywords
  2003-05-28 13:15       ` lawrence mitchell
@ 2003-05-28 16:13         ` Oliver Scholz
  0 siblings, 0 replies; 11+ messages in thread
From: Oliver Scholz @ 2003-05-28 16:13 UTC (permalink / raw)


lawrence mitchell <wence@gmx.li> writes:

> Oliver Scholz wrote:
>
> [...] PREPEND versus APPEND in font-lock-keywords.
>
>> Erm, this code was just example code, so I am personally not so sure
>> which one should take precedence, the old fontification or the new
>> one. It's hard for me to make an educated guess, because the major
>> mode has no meaning, and so the fontification has no meaning.
>
>> But maybe you have a more general reason to say this. Could you
>> please elaborate?
>
> The way I read it, if using `append', rather than `prepend',
> in the example above, EXAMPLE-L-WORD-FACE would change the
> background iff FONT-LOCK-WARNING-FACE and
> FONT-LOCK-KEYWORD-FACE, did not set the background colour.
> Certainly in my Emacs setup, said faces have had their
> backgrounds set explicitly.
[...]

That's true; this is what the "precedence" is about. However, if a
user sets the background colour of `font-lock-warning-face'
etc. explicitely, she probably attaches some intensional significance
with it. Maybe the background colour even is the only face attribute
of `font-lock-warning-face' in her configuration?

Whether it is a bug or a feature to override this or to not override
this, depends entirely on what is more important. Is it more important
to highlight "lirum" and "larum" as special words or is it more
important to highlight words beginning with "l"? Maybe it's nice but
not necessary to highlight "lambda" and "lalala", but "lirum" and
"larum" are *the* important keywords to watch out for? In this case it
is a bug to highlight "lirum" and "larum" as `words beginning with l'.

I have no idea what is the proper thing to do for `example-mode'. And
I am the author of it. :-|

    Oliver
-- 
9 Prairial an 211 de la Révolution
Liberté, Egalité, Fraternité!

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

end of thread, other threads:[~2003-05-28 16:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-27 11:53 Background colors in font-lock-keywords Jesse Sheidlower
2003-05-27 14:35 ` Stefan Monnier
2003-05-28 16:09   ` Jesse Sheidlower
2003-05-27 16:30 ` Kevin Rodgers
2003-05-27 17:12   ` Jesse Sheidlower
2003-05-27 22:23 ` Oliver Scholz
2003-05-27 22:43   ` Oliver Scholz
2003-05-28  0:13   ` lawrence mitchell
2003-05-28  7:32     ` Oliver Scholz
2003-05-28 13:15       ` lawrence mitchell
2003-05-28 16:13         ` Oliver Scholz

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