简体   繁体   English

将创建的颜色代码添加到字体锁定

[英]Adding a created color-code to font-lock

I'm trying to syntax-highlight RGB color codes in Emacs. 我正在尝试在Emacs中语法高亮显示RGB颜色代码。 I've got the #hex values working, but though I was bashing my head against this for a few hours yesterday I couldn't get it to work right: 我已经使#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)))))))

where I later call (font-lock-add-keywords nil rgb-color-keywords) . 以后我称之为(font-lock-add-keywords nil rgb-color-keywords) I know the basics are right since I do something very similar for short and long hex values that works (which are themselves based on code I found lurking on the net), and I've tested the basic regex in the Emacs interpreter, but for whatever reason this is just not working. 我知道这些基本知识是正确的,因为我对长短的十六进制值做了一些非常相似的工作(它们本身是基于我在网上潜伏的代码),并且我已经在Emacs解释器中测试了基本的正则表达式,但是对于无论出于什么原因,这都不起作用。 The individual parts all seem to work fine also. 各个部分似乎也都可以正常工作。

On the plus side, I learned a bunch of lisp yesterday .... 从好的方面来说,我昨天学到了一堆口红....

For what it's worth, though certainly a code fix would be sufficient, my mental model of this handling is fuzzy so I'd love a "why" on what I missed. 就其价值而言,尽管当然可以进行代码修复就足够了,但是我对这种处理方式的思维模型是模糊的,因此我很想在我错过的内容上加一个“为什么”。


@sds just about got it, and pointed me in the right direction. @sds差不多得到了,并向我指出了正确的方向。 This ended up working: 最终工作:

 (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)))))))))) 

You have several forms where only one is permitted. 您有几种形式,其中只允许一种形式。 What you want, I think, is this: 我想您想要的是:

(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM