* (add-hook 'font-lock-mode-hook ...) keeps adding to font-lock-keywords
@ 2005-09-02 9:04 Klaus Zeitler
2005-09-02 14:34 ` Stefan Monnier
0 siblings, 1 reply; 4+ messages in thread
From: Klaus Zeitler @ 2005-09-02 9:04 UTC (permalink / raw)
For a long time I've been using the simple show-tabs function below. With CVS
emacs I've noticed now that the same element gets appended to global
font-lock-keywords over and over again.
1. emacs -Q&
2. M-x global-font-lock-mode
3. in scratch buffer evaluate the following 2 expressions:
(defun show-tabs () "Show tabs with a slightly changed background"
(setq font-lock-keywords
(append font-lock-keywords '(("[\t]+" (0 'tab-face t))))))
(add-hook 'font-lock-mode-hook 'show-tabs)
4. in scratch buffer: C-h v font-lock-keywords
Local in buffer *scratch*; global value is
(("[ ]+"
(0 'tab-face t)))
5. every C-h v font-lock-keywords adds one more entry to global
font-lock-keywords
Value:
(t
(("[ ]+"
(0 'tab-face t)))
("[ ]+"
(0 'tab-face t)))
Moreover if I load e.g. a text file, then C-h v in that buffer will give me
something like:
Value:
(t
(("[ ]+"
(0 'tab-face t)))
("[ ]+"
(0 'tab-face t)))
Local in buffer x.txt; global value is
(("[ ]+"
(0 'tab-face t))
("[ ]+"
(0 'tab-face t))
("[ ]+"
(0 'tab-face t))
("[ ]+"
(0 'tab-face t))
("[ ]+"
(0 'tab-face t)))
--
------------------------------------------
| Klaus Zeitler Lucent Technologies |
| Email: kzeitler@lucent.com |
------------------------------------------
---
Why bother building any more nuclear warheads until we use the ones we have?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: (add-hook 'font-lock-mode-hook ...) keeps adding to font-lock-keywords
2005-09-02 9:04 (add-hook 'font-lock-mode-hook ...) keeps adding to font-lock-keywords Klaus Zeitler
@ 2005-09-02 14:34 ` Stefan Monnier
2005-09-05 8:58 ` Klaus Zeitler
0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2005-09-02 14:34 UTC (permalink / raw)
Cc: emacs-devel
> (defun show-tabs () "Show tabs with a slightly changed background"
> (setq font-lock-keywords
> (append font-lock-keywords '(("[\t]+" (0 'tab-face t))))))
> (add-hook 'font-lock-mode-hook 'show-tabs)
font-lock-mode-hook is called both when turning the mode ON and when turning
the mode OFF.
font-lock-keywords should not be modified as you do. Instead you should use
font-lock-add-keywords, which knows about font-lock-compile-keywords.
Stefan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: (add-hook 'font-lock-mode-hook ...) keeps adding to font-lock-keywords
2005-09-02 14:34 ` Stefan Monnier
@ 2005-09-05 8:58 ` Klaus Zeitler
2005-09-05 15:54 ` Stefan Monnier
0 siblings, 1 reply; 4+ messages in thread
From: Klaus Zeitler @ 2005-09-05 8:58 UTC (permalink / raw)
Cc: emacs-devel
>>>>> "Stefan" == Stefan Monnier <monnier@iro.umontreal.ca> writes:
Stefan>
Stefan> font-lock-mode-hook is called both when turning the mode ON and
Stefan> when turning the mode OFF.
But I didn't turn it off.
Stefan> font-lock-keywords should not be modified as you do. Instead you
Is that documented somewhere. I've seen similar constructs used quite a few
times (e.g. emacs wiki page).
Stefan> should use font-lock-add-keywords, which knows about
Stefan> font-lock-compile-keywords.
ok, changed it to:
(defun show-tabs () "Show tabs with a slightly changed background"
(font-lock-add-keywords nil '(("[\t]+" (0 'tab-face t)))))
(add-hook 'font-lock-mode-hook 'show-tabs)
and now it looks better, but it still looks a bit odd to me:
(t
(("[ ]+"
(0 'tab-face t)))
("[ ]+"
(0 'tab-face t)))
But at least it doesn't get added over and over again.
--
------------------------------------------
| Klaus Zeitler Lucent Technologies |
| Email: kzeitler@lucent.com |
------------------------------------------
---
If a listener nods his head when you're
explaining your program, wake him up.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: (add-hook 'font-lock-mode-hook ...) keeps adding to font-lock-keywords
2005-09-05 8:58 ` Klaus Zeitler
@ 2005-09-05 15:54 ` Stefan Monnier
0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2005-09-05 15:54 UTC (permalink / raw)
Cc: emacs-devel
Stefan> font-lock-mode-hook is called both when turning the mode ON and
Stefan> when turning the mode OFF.
> But I didn't turn it off.
No, but it still means your hook code is wrong: you should check that
font-lock-mode is non-nil before calling your `show-tabs'.
Stefan> font-lock-keywords should not be modified as you do. Instead you
> Is that documented somewhere. I've seen similar constructs used quite a few
> times (e.g. emacs wiki page).
Not really, but it's always been that way. Take a look at what
font-lock-compile-keywords does. It has partly changed over time, but some
form of it has existed for "ever".
Also the docstring suggests using font-lock-add-keywords:
[...]
contained expressions. You can also alter it by calling
`font-lock-add-keywords' or `font-lock-remove-keywords' with MODE = nil.
[...]
> and now it looks better, but it still looks a bit odd to me:
Only to you. Not to Emacs.
Stefan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2005-09-05 15:54 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-02 9:04 (add-hook 'font-lock-mode-hook ...) keeps adding to font-lock-keywords Klaus Zeitler
2005-09-02 14:34 ` Stefan Monnier
2005-09-05 8:58 ` Klaus Zeitler
2005-09-05 15:54 ` Stefan Monnier
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.