From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: Macro used for dynamic setting of font-lock-keywords Date: Sun, 27 May 2007 01:23:11 +1000 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <87lkfb4nj4.fsf@lion.rapttech.com.au> References: <874pm0573o.fsf@lion.rapttech.com.au> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1180194035 14680 80.91.229.12 (26 May 2007 15:40:35 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 26 May 2007 15:40:35 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat May 26 17:40:33 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1HryNd-0000sk-Ia for geh-help-gnu-emacs@m.gmane.org; Sat, 26 May 2007 17:40:33 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HryNd-0005pA-02 for geh-help-gnu-emacs@m.gmane.org; Sat, 26 May 2007 11:40:33 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!newsfeed.berkeley.edu!ucberkeley!sn-xt-sjc-02!sn-xt-sjc-09!sn-post-sjc-01!supernews.com!corp.supernews.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (gnu/linux) Cancel-Lock: sha1:5gBrvLOgE1BwD9MzBJF0ykTggeA= Original-X-Complaints-To: abuse@supernews.com Original-Lines: 65 Original-Xref: shelby.stanford.edu gnu.emacs.help:148874 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:44462 Archived-At: Sebastian Tennant writes: > Quoth Tim X : >>> (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. Tim