[英]How to not highlight values in comments in Emacs?
I want to highlight true and false values in some configuration files.我想在某些配置文件中突出显示 true 和 false 值。 I've done it this way:
我是这样做的:
(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)))
But it also highlight those values in comments:但它也在评论中强调了这些价值观:
Is there a way to exclude those highlightings?有没有办法排除这些突出显示?
UPDATE -- highlight-regexp
is an alias for 'hi-lock-face-buffer' in 'hi-lock.el'.更新——highlight
highlight-regexp
是“hi-lock.el”中“hi-lock-face-buffer”的别名。 And string-match-p
is a compiled Lisp function in 'subr.el'.而
string-match-p
是 'subr.el' 中的编译后的 Lisp 函数。
You can just add the regexps via font-lock-add-keywords
, which will already account for the comment syntax in the buffer, eg.您可以通过
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))
And call (font-lock-refresh-defaults)
to revert back to OG settings.并调用
(font-lock-refresh-defaults)
恢复到 OG 设置。
Sticking to a purely regexp solution with highlight-regexp
will undoubtebly produce some mistakes in odd cases, but, I think just customizing your regex to check for a comment prefix would probably work well enough as well,坚持使用
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))))
A way would be to simply add a $
at the end of your regex to not match the comments, as the true/false are always at the end, while those in comments are always situated in the middle of a sentence一种方法是简单地在正则表达式的末尾添加一个
$
以不匹配注释,因为真/假总是在末尾,而注释中的那些总是位于句子的中间
(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.