[Top][All Lists]

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

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

From: Tim X
Subject: Re: Macro used for dynamic setting of font-lock-keywords
Date: Sun, 27 May 2007 01:23:11 +1000
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (gnu/linux)

Sebastian Tennant <address@hidden> writes:

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

My mistake for not checking the code and documentation closer. 

Your problem is passing the argument, its that the way you have the function
prevents the argument from being evaluated, so what is really happening is the
symbol word is getting added to the list. Try this

(defun my-add-keyword (word) 
    (font-lock-add-keywords nil (cons word font-lock-warning-face)) t))

Though you probably should define the function to take three arguments, the
word (or regexp), the match-group and the facename. You could make the last two
optional with defaults. 


reply via email to

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