tags:

views:

375

answers:

2

I'm trying to write a new mode for emacs, using define-generic-mode. I've found a few tutorials which show how you can add keywords (as strings) which will then be highlighted. Is it possible to give define-generic-mode a regular expression so that it can then highlight anything that matches that as a keyword?

I'd like to have a mode in which anything matching a date in the form 15/01/09 is displayed in a different font (preferably underlined, but I'll accept a different colour).

Any ideas?

Robin

+4  A: 

for example using font-lock-add-keywords in order to highlight FIXME, TODO and XXX as warning in major modes:

   (dolist (mode '(c-mode
 java-mode
 cperl-mode
 html-mode-hook
 css-mode-hook
 emacs-lisp-mode))
            (font-lock-add-keywords mode 
    '(("\\(XXX\\|FIXME\\|TODO\\)" 
       1 font-lock-warning-face prepend))))
dfa
+4  A: 

Here's an example of define-generic-mode which sets up the regexp to have all the dates fontified using a custom face with some attributes chosen as examples:

(make-face 'my-date-face)
(set-face-attribute 'my-date-face nil :underline t)
(set-face-attribute 'my-date-face nil :family "times")
(set-face-attribute 'my-date-face nil :slant 'normal)
(set-face-attribute 'my-date-face nil :height '340)

(define-generic-mode my-date-mode
  nil
  nil 
  '(("\\([0-9]+/[0-9]+/[0-9]+\\)"
     (1 'my-date-face)))
  nil
  nil)

Oh, and obviously, set the mode by M-x my-date-mode. This can be done automatically via the auto-mode-alist (5th argument to define-generic-mode).

Trey Jackson
Thanks for the reply. I can't seem to get this working though. I've copied in the code, and eval-buffer works fine, but then the right bits don't get underlined. Any idea what the problem is?
robintw
(Odd, my first comment was lost). I just edited the code to properly work. I thought I'd tested the previous form, but it fails for some unknown reason. This variant uses a sub expression in the regexp (requiring the `(1 'my-date-face)`). I don't know why the previous didn't work.
Trey Jackson
Thanks - it works fine now. :)
robintw
can we have this as a minor mode so that we can turn it off and on?
RamyenHead
@RamyenHead Check out `define-minor-mode`, similar arguments as `define-generic-mode`.
Trey Jackson