unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* highlighting keywords in comments
@ 2011-03-06 19:03 Nathaniel Flath
  2011-03-11 17:32 ` Andreas Röhler
  0 siblings, 1 reply; 5+ messages in thread
From: Nathaniel Flath @ 2011-03-06 19:03 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 416 bytes --]

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?

Thanks,
Nathaniel Flath

[-- Attachment #2: Type: text/html, Size: 539 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: highlighting keywords in comments
       [not found] <mailman.3.1299438195.5045.help-gnu-emacs@gnu.org>
@ 2011-03-06 21:22 ` Tim X
  2011-03-06 21:31 ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Tim X @ 2011-03-06 21:22 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: highlighting keywords in comments
       [not found] <mailman.3.1299438195.5045.help-gnu-emacs@gnu.org>
  2011-03-06 21:22 ` highlighting keywords in comments Tim X
@ 2011-03-06 21:31 ` Stefan Monnier
  2011-03-07 16:18   ` jpkotta
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2011-03-06 21:31 UTC (permalink / raw)
  To: help-gnu-emacs

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

Check the doc of `font-lock-keywords', especially the OVERRIDE part.


        Stefan


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: highlighting keywords in comments
  2011-03-06 21:31 ` Stefan Monnier
@ 2011-03-07 16:18   ` jpkotta
  0 siblings, 0 replies; 5+ messages in thread
From: jpkotta @ 2011-03-07 16:18 UTC (permalink / raw)
  To: help-gnu-emacs

On Mar 6, 3:31 pm, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> > 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?
>
> Check the doc of `font-lock-keywords', especially the OVERRIDE part.
>
>         Stefan

(defvar warning-words-regexp
  (regexp-opt '("FIXME" "TODO" "BUG" "XXX" "DEBUG") 'words)
  "Regexp matching words that commonly denote something that
  warrants attention in programs.")

(setq warning-words-font-lock-spec
      (cons warning-words-regexp
            (list
             0 ;; use whole match
             'font-lock-warning-face
             t ;; OVERRIDE
             )))

(font-lock-add-keywords
 'c-mode ;; or nil for current buffer
 (list
  warning-words-font-lock-spec))


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: highlighting keywords in comments
  2011-03-06 19:03 Nathaniel Flath
@ 2011-03-11 17:32 ` Andreas Röhler
  0 siblings, 0 replies; 5+ messages in thread
From: Andreas Röhler @ 2011-03-11 17:32 UTC (permalink / raw)
  To: help-gnu-emacs

Am 06.03.2011 20:03, schrieb Nathaniel Flath:
> 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?
>
> Thanks,
> Nathaniel Flath
>

maybe a "prepend" helps here?

C-h f font-lock-add-keywords says:

...


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.




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-03-11 17:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.3.1299438195.5045.help-gnu-emacs@gnu.org>
2011-03-06 21:22 ` highlighting keywords in comments Tim X
2011-03-06 21:31 ` Stefan Monnier
2011-03-07 16:18   ` jpkotta
2011-03-06 19:03 Nathaniel Flath
2011-03-11 17:32 ` Andreas Röhler

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