我正在尝试在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解释器中的基本正则表达式,但是无论什么原因,这只是不起作用。各个部分似乎都运行良好。
从好的方面来说,我昨天学会了一堆口齿不清......
对于它的价值,虽然代码修复肯定是足够的,但我对这种处理的心理模型是模糊的,所以我喜欢我错过的“为什么”。
<小时/> @sds刚刚得到它,并指出我正确的方向。最终工作:
(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))))))))))
答案 0 :(得分:4)
您有多个表单,其中只允许一个表单。 我想,你想要的是:
(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))))))))))