Match regular expression as keyword in define-generic-mode

半世苍凉 提交于 2019-12-17 19:25:37

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/756666/match-regular-expression-as-keyword-in-define-generic-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!