Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match regular expression as keyword in define-generic-mode

Tags:

emacs

elisp

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

like image 844
robintw Avatar asked Dec 13 '22 04:12

robintw


1 Answers

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

like image 176
Trey Jackson Avatar answered Feb 20 '23 19:02

Trey Jackson