在定义通用模式下匹配正则表达式作为关键字 [英] Match regular expression as keyword in define-generic-mode
问题描述
我正在使用定义通用模式为emacs写一个新模式。我发现了几个教程,显示如何添加关键字(如字符串),然后将其突出显示。可以给定义通用模式一个正则表达式,以便它可以突出显示匹配那个作为关键字的任何东西?
我想要一个模式其中以15/01/09形式匹配日期的任何内容将以不同的字体显示(最好带下划线,但我会接受不同的颜色)。
任何想法?
Robin
以下是 define-generic-mode
它设置正则表达式,使所有的日期都使用自定义的脸部,并选择一些属性作为示例:
(make-face'my-date-face)
。这可以通过auto-mode-alist(第五个参数
(set-face-attribute'my-date-face nil:underline t)
(set- face-attribute'my-date-face nil:familytimes)
(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 $ b $嗯,显然,设置模式为$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $日期模式define-generic-mode
)自动完成。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
解决方案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 todefine-generic-mode
).这篇关于在定义通用模式下匹配正则表达式作为关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!