font-lock-keywords中的progn表达式如何工作? [英] How does a progn form in font-lock-keywords work?

查看:167
本文介绍了font-lock-keywords中的progn表达式如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将通过将一个progn表单传递给字体锁关键字,直观地将hello world替换为HW。

  (font-lock-add-keywords 
nil'((\\(hello world \\)
(0(progn(put-text-property(match-beginning 1) (匹配结束1)
'显示HW)
nil)))))

我查看了 Ch v font-lock-keywords ,看看这是否是字体锁定的记录功能。你好,世界元素似乎是这样的:

 (MATCHER HIGHLIGHT ...)

这意味着(0 ...)是HIGHLIGHT,文档说

  HIGHLIGHT应该是MATCH-HIGHLIGHT或MATCH-ANCHORED。 

  MATCH-HIGHLIGHT应该是这样的形式:

(SUBEXP FACENAME [OVERRIDE [LAXMATCH]])

所以我猜是0是SUBEXP和(progn ...)是FACENAME。但是如果(progn ..)是一个有效的FACENAME,以下代码将会起作用但不起作用。

  ;; (MATCHER。FACENAME)
(font-lock-add-keywords
nil'((goodbye lenin
。(progn(put-text-property(match-begin 1) -end $)



$ b

这让我看到第一个代码的工作原理以及是否依赖于一个无证的功能。






更新



旁注:没有字体锁定错误的更简单的视觉替换方式

 (font-lock-add-keywords 
nil'((my llama
(0(progn(put-text-property(match-beginning 0) (match-end 0)
'显示ML)
nil)))))


解决方案

它有效 - 但是您的 MATCHER 不正确 - 匹配结果未被存储。这个例子不起作用:

 (font-lock-add-keywords 
nil'((再见lenin
(0(progn(put-text-property(match-beginning 1)(match-end 1)
'显示GL)
nil)))))

而这样做:

 code>(font-lock-add-keywords 
nil'((\\(goodbye lenin\\\)
。(progn(put-text-property -bginning 1)(match-end 1)
'显示GL)
nil))))

文档说: FACENAME 是一个表达式,其值是要使用的面部名称
而不是一张脸, FACENAME 可以评估一个属性列表
,格式为(face FACE PROP1 VAL1 PROP2 VAL2 ...)
在这种情况下,所有列出的文本属性将被设置而不是
只是 FACE



这里, FACENAME 表达式( progn )的计算结果为 nil ,所以没有设置属性或面孔 - 由 put-text-property 导致的唯一效果。


Following code will visually replace "hello world" with "HW" by passing a progn form to font lock keywords.

(font-lock-add-keywords
 nil '(("\\(hello world\\)"
        (0 (progn (put-text-property (match-beginning 1) (match-end 1)
                                     'display "HW")
                  nil)))))

I've look into C-h v font-lock-keywords to see if this is a documented feature of font lock. The hello world element seemed to be of this form:

(MATCHER HIGHLIGHT ...)

which would mean that (0 ...) is HIGHLIGHT and the doc says

HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.

and

MATCH-HIGHLIGHT should be of the form:

 (SUBEXP FACENAME [OVERRIDE [LAXMATCH]])

So I guessed 0 was SUBEXP and (progn ...) was FACENAME. But if (progn ..) were a valid FACENAME, the following code would work but it doesn't work.

;;  (MATCHER . FACENAME)
(font-lock-add-keywords
 nil '(("goodbye lenin"
        . (progn (put-text-property (match-beginning 1) (match-end 1)
                                    'display "GL")
                 nil))))

That brings me to the question of how the first code works and whether it is relying on an undocumented feature.


Update:

Side note: simpler way of visual replacement without font lock errors

(font-lock-add-keywords
 nil '(("my llama"
        (0 (progn (put-text-property (match-beginning 0) (match-end 0)
                                     'display "ML")
                  nil)))))

解决方案

It does work - but your MATCHER is not correct - the result of the match is not stored. This, for example does not work:

(font-lock-add-keywords
 nil '(("goodbye lenin"
        (0 (progn (put-text-property (match-beginning 1) (match-end 1)
                                    'display "GL")
                  nil)))))

while this does:

(font-lock-add-keywords
 nil '(("\\(goodbye lenin\\)"
        . (progn (put-text-property (match-beginning 1) (match-end 1)
                                    'display "GL")
                 nil))))

The documentation says: "FACENAME is an expression whose value is the face name to use. Instead of a face, FACENAME can evaluate to a property list of the form (face FACE PROP1 VAL1 PROP2 VAL2 ...) in which case all the listed text-properties will be set rather than just FACE."

Here, the FACENAME expression (progn) evaluates to nil, so no properties or faces are being set - the only effect that caused by put-text-property.

这篇关于font-lock-keywords中的progn表达式如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆