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

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

Re: highlighting keywords in comments


From: Tim X
Subject: Re: highlighting keywords in comments
Date: Mon, 07 Mar 2011 08:22:18 +1100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Nathaniel Flath <flat0103@gmail.com> writes:

> In java-mode (and other cc-modes), I have the following code 
>
>
>                    (font-lock-add-keywords
>                     nil
>                     '(("\\(FIXME\\|TODO\\)" 1 font-lock-warning-face)))
>
> However, this doesn't work on FIXME or TODO words in comments, making it much
> less useful.  Is there a way for these keywords to be highlighted in
> font-lock-warning-face, even in comments?
>

What you want to do is described in the help for font-lock-add-keywords
(at least in emacs 24 it is) - see the example. 

,----[ C-h f font-lock-add-keywords RET ]
| font-lock-add-keywords is a compiled Lisp function in `font-lock.el'.
| 
| (font-lock-add-keywords MODE KEYWORDS &optional HOW)
| 
| Add highlighting KEYWORDS for MODE.
| 
| MODE should be a symbol, the major mode command name, such as `c-mode'
| or nil.  If nil, highlighting keywords are added for the current buffer.
| KEYWORDS should be a list; see the variable `font-lock-keywords'.
| By default they are added at the beginning of the current highlighting list.
| If optional argument HOW is `set', they are used to replace the current
| highlighting list.  If HOW is any other non-nil value, they are added at the
| end of the current highlighting list.
| 
| For example:
| 
|  (font-lock-add-keywords 'c-mode
|   '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend)
|     ("\\<\\(and\\|or\\|not\\)\\>" . font-lock-keyword-face)))
| 
| adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
| comments, and to fontify `and', `or' and `not' words as keywords.
| 
| The above procedure will only add the keywords for C mode, not
| for modes derived from C mode.  To add them for derived modes too,
| pass nil for MODE and add the call to c-mode-hook.
| 
| For example:
| 
|  (add-hook 'c-mode-hook
|   (lambda ()
|    (font-lock-add-keywords nil
|     '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend)
|       ("\\<\\(and\\|or\\|not\\)\\>" .
|        font-lock-keyword-face)))))
| 
| The above procedure may fail to add keywords to derived modes if
| some involved major mode does not follow the standard conventions.
| File a bug report if this happens, so the major mode can be corrected.
| 
| Note that some modes have specialized support for additional patterns, e.g.,
| see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
| `objc-font-lock-extra-types' and `java-font-lock-extra-types'.
| 
| [back]
`----

Tim

-- 
tcross (at) rapttech dot com dot au


reply via email to

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