unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Tim X <timx@nospam.dev.null>
To: help-gnu-emacs@gnu.org
Subject: Re: Macro used for dynamic setting of font-lock-keywords
Date: Sat, 26 May 2007 18:20:27 +1000	[thread overview]
Message-ID: <874pm0573o.fsf@lion.rapttech.com.au> (raw)
In-Reply-To: mailman.1240.1180113798.32220.help-gnu-emacs@gnu.org

Sebastian Tennant <sebyte@smolny.plus.com> writes:

> Subject: Macro used for dynamic setting of font-lock-keywords
>
> Hi all,
>
> This works:
>
>   (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))

and use it like

(my-add-keyword "FIXME")

I can't see any reason for the macro.

I often use something like the following in my .emacs

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

which highlights FIXME: in the warning face. To be honest, I can't see any
benefit in defining a function just to define a simple additional word as you
have done and certainly cannot see any justification for a macro being defined
in that function.

If what you want to do is define a function you can call interactively to add
keywords, then look into the 'interactive' function to see how it can be used
to prompt the user (for lets say the word and the face to use. 

Tim



-- 
tcross (at) rapttech dot com dot au

       reply	other threads:[~2007-05-26  8:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1240.1180113798.32220.help-gnu-emacs@gnu.org>
2007-05-26  8:20 ` Tim X [this message]
2007-05-26 10:40   ` Macro used for dynamic setting of font-lock-keywords Sebastian Tennant
     [not found]   ` <mailman.1274.1180175941.32220.help-gnu-emacs@gnu.org>
2007-05-26 15:23     ` Tim X
2007-05-26 20:07       ` Sebastian Tennant
2007-06-02  3:29         ` Kevin Rodgers
     [not found]       ` <mailman.1303.1180209955.32220.help-gnu-emacs@gnu.org>
2007-05-27  3:30         ` Tim X
2007-05-27 21:45           ` Xavier Maillard
2007-05-28  9:22             ` Sebastian Tennant
2007-06-02  3:41               ` Kevin Rodgers
2007-06-02  7:26                 ` Eli Zaretskii
2007-06-03  9:39                 ` Sebastian Tennant
     [not found]           ` <mailman.1339.1180303291.32220.help-gnu-emacs@gnu.org>
2007-05-28  8:37             ` Tim X
2007-06-10  0:51 ` Stefan Monnier
2007-05-25 17:24 Sebastian Tennant

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874pm0573o.fsf@lion.rapttech.com.au \
    --to=timx@nospam.dev.null \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).