[英]How to not highlight values in comments in Emacs?
我想在某些配置文件中突出显示 true 和 false 值。 我是这样做的:
(defun my/highlight-in-properties-files ()
"Highlight regexps in PROPERTIES files."
(when (string-match-p ".properties" (file-name-nondirectory buffer-file-name))
(highlight-regexp "true" 'hi-green)
(highlight-regexp "false" 'hi-pink)))
但它也在评论中强调了这些价值观:
有没有办法排除这些突出显示?
更新——highlight highlight-regexp
是“hi-lock.el”中“hi-lock-face-buffer”的别名。 而string-match-p
是 'subr.el' 中的编译后的 Lisp 函数。
您可以通过font-lock-add-keywords
添加正则表达式,这已经考虑了缓冲区中的注释语法,例如。
(defun my-font-lock-additions ()
(require 'hi-lock) ; fonts
(font-lock-add-keywords
nil
'(("\\btrue\\b" . 'hi-green)
("\\bfalse\\b" . 'hi-pink)))
(font-lock-flush))
并调用(font-lock-refresh-defaults)
恢复到 OG 设置。
坚持使用highlight-regexp
的纯正则表达式解决方案无疑会在奇怪的情况下产生一些错误,但是,我认为仅自定义您的正则表达式以检查注释前缀可能也能很好地工作,
(defun my/highlight-in-properties-files ()
"Highlight regexps in PROPERTIES files."
(when (string-match-p ".properties" (file-name-nondirectory buffer-file-name))
(comment-normalize-vars) ; ensure comment variables are setup
(let ((cmt-re (format "^[^%s]*" (regexp-quote (string-trim comment-start)))))
(highlight-regexp (format "%s\\(\\_<true\\_>\\)" cmt-re) 'hi-green 1)
(highlight-regexp (format "%s\\(\\_<false\\_>\\)" cmt-re) 'hi-pink 1))))
一种方法是简单地在正则表达式的末尾添加一个$
以不匹配注释,因为真/假总是在末尾,而注释中的那些总是位于句子的中间
(defun my/highlight-in-properties-files ()
"Highlight regexps in PROPERTIES files."
(when (string-match-p ".properties" (file-name-nondirectory buffer-file-name))
(highlight-regexp "true$" 'hi-green)
(highlight-regexp "false$" 'hi-pink)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.