问题描述
我正在尝试使用define-generic-mode为emacs编写一个新模式.我找到了一些教程,它们展示了如何添加关键字(作为字符串),然后将其突出显示.是否可以为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?
我想要一种模式,在这种模式下,任何与 15/01/09 形式的日期匹配的内容都以不同的字体显示(最好加下划线,但我会接受不同的颜色).
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).
有什么想法吗?
罗宾
推荐答案
下面是 define-generic-mode
的一个例子,它设置了正则表达式,使所有的日期都使用带有一些字体的自定义字体字体化选择的属性作为示例:
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)
哦,显然,通过 M-x my-date-mode
设置模式.这可以通过 auto-mode-alist(define-generic-mode
的第 5 个参数)自动完成.
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
).
这篇关于在define-generic-mode中匹配正则表达式作为关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!