[英]Adding a created color-code to font-lock
我正在嘗試在Emacs中語法高亮顯示RGB顏色代碼。 我已經使#hex值起作用了,但是盡管昨天我為此煩惱了幾個小時,但還是無法正常工作:
(defvar rgb-color-keywords
'(("rgb([0-9]+,[0-9]+,[0-9]+)"
(0 (setq color-channels-rgb (substring (match-string-no-properties 0) 4 -1))
(setq color-channels (split-string color-channels-rgb ","))
(setq red-channel (format "%02X" (string-to-number (elt color-channels 0))))
(setq green-channel (format "%02X" (string-to-number (elt color-channels 1))))
(setq blue-channel (format "%02X" (string-to-number (elt color-channels 2))))
(put-text-property
(match-beginning 0)
(match-end 0)
'face (list :background (concat "#" red-channel green-channel blue-channel)))))))
以后我稱之為(font-lock-add-keywords nil rgb-color-keywords)
。 我知道這些基本知識是正確的,因為我對長短的十六進制值做了一些非常相似的工作(它們本身是基於我在網上潛伏的代碼),並且我已經在Emacs解釋器中測試了基本的正則表達式,但是對於無論出於什么原因,這都不起作用。 各個部分似乎也都可以正常工作。
從好的方面來說,我昨天學到了一堆口紅....
就其價值而言,盡管當然可以進行代碼修復就足夠了,但是我對這種處理方式的思維模型是模糊的,因此我很想在我錯過的內容上加一個“為什么”。
(defvar rgb-color-keywords '(("rgb([0-9]+, *[0-9]+, *[0-9]+)" (0 (put-text-property (match-beginning 0) (match-end 0) 'face (list :background (let ((color-channels (split-string (substring (match-string-no-properties 0) 4 -1) ","))) (format "#%02X%02X%02X" (string-to-number (nth 0 color-channels)) (string-to-number (nth 1 color-channels)) (string-to-number (nth 2 color-channels))))))))))
您有幾種形式,其中只允許一種形式。 我想您想要的是:
(defvar rgb-color-keywords
'(("rgb([0-9]+ *,[0-9]+ *,[0-9]+ *)"
(0
(let ((channels (split-string (substring (match-string-no-properties 0) 4 -1)
"," nil " *")))
(list 'face (list :background
(format "#%02X%02X%02X"
(string-to-number (nth 0 color-channels))
(string-to-number (nth 1 color-channels))
(string-to-number (nth 2 color-channels))))))))))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.