簡體   English   中英

如何在 Emacs 的注釋中不突出顯示值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM