help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Macro used for dynamic setting of font-lock-keywords


From: Sebastian Tennant
Subject: Re: Macro used for dynamic setting of font-lock-keywords
Date: Sat, 26 May 2007 13:40:34 +0300
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.95 (gnu/linux)

Quoth Tim X <address@hidden>:
>>   (defun foo (word)
>>     (display-buffer (set-buffer (get-buffer-create "*bar*")))
>>     (insert (format "baz\n"))
>>     (unless (fboundp 'foo-dynamic-add-keyword) ;only define it once
>>       (defmacro foo-dynamic-add-keyword ()
>>         `(font-lock-add-keywords nil '((,word . font-lock-warning-face)) 
>> 'set)))
>>     (foo-dynamic-add-keyword) ;call the macro
>>     (font-lock-mode 1))
>>
>>   (foo "baz")
>>
>> in that the string argument to foo is added to the buffer's
>> font-lock-keywords and the string is highlighted wherever it occurs,
>> but it seems like something of a kludge to me.
>>
>> Just out of interest really, is there a better way of passing the
>> value of a variable as an argument to the function
>> font-lock-add-keywords?
>
> I must be missing something - I don't understand why you are using a macro or
> what the issue is with passing an argument. Doesn't something like (not 
> tested)
>
(defun my-add-keyword (word) 
  (font-lock-add-keywords nil '((word . font-lock-warning-face)) 'set))
[C-x C-e]
my-add-keyword

(my-add-keyword "keyword")
[C-x C-e]
(t ((word . font-lock-warning-face)) (word (0 font-lock-warning-face)))
                                        ^
                                        |
 This doesn't do the job ---------------+

As you can see, passing arguments that make it into font-lock-keywords
is not so straightforward as you think.

> (add-hook xxx-mode-hook 
>             (lambda ()
>                .... ; various mode specific struff
>                (font-lock-add-keywords nil
>                   '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend)))))

Of course this works.  The pattern you want to match is being added
directly to the list.

Sebastian





reply via email to

[Prev in Thread] Current Thread [Next in Thread]