2011-12-29 84 views
4

我想Emacs的帮助我直观地识别那些没有被更改为unicode字符串字符串(Python版本< 3):emacs的蟒蛇模式:如何以不同的颜色突出显示STR与Unicode的STR

"display this string in color red" 

u"display this string in color orange" 

使用emacs 23和python模式

什么我需要添加到我的.emacs?谢谢。

+0

我还在寻找基于内容的高亮机制的解决方案。例如,我想突出显示一些用'#'标记的单词标记,例如#MPC3680#PQ37#MPC3680我希望我可以使用不同的颜色/脸部显示标记的单词,用相同的标签词相同的颜色/脸。我觉得emacs可能是最有可能的解决方案之一。提前致谢! – 2012-05-31 13:58:27

回答

1

在我的版本中,u未突出显示,但字符串为。关于面孔,你必须比我更有创意。我刚刚从font-lock中盗取了他们,并将几乎所有内容都更改为"red"

如果您还想突出显示u,则可以将文本属性放在下面的py-font-lock-sytactic-face-function中。 looking-back告诉你u的确切位置。我现在太懒惰了。已经很晚了。此外,这有点'hacky''。

 
(defface font-lock-ucs-string-face 
    '((((class grayscale) (background light)) :foreground "DarkGray" :slant italic) 
    (((class grayscale) (background dark)) :foreground "DarkGray" :slant italic) 
    (((class color) (min-colors 88) (background light)) :foreground "red") 
    (((class color) (min-colors 88) (background dark)) :foreground "red") 
    (((class color) (min-colors 16) (background light)) :foreground "red") 
    (((class color) (min-colors 16) (background dark)) :foreground "red") 
    (((class color) (min-colors 8)) :foreground "red") 
    (t :slant italic)) 
    "Font Lock mode face used to highlight strings." 
    :group 'font-lock-faces) 


(defun py-font-lock-syntactic-face-function (state) 
    "See variable `font-lock-syntactic-face-function'" 
    (message "Running py-font-lock-syntactic-face-function at %d." (point)) 
    (if (nth 3 state) 
     (if (looking-back "u\"") 
     'font-lock-ucs-string-face 
    'font-lock-string-face) 
    'font-lock-comment-face)) 

(add-hook 'python-mode-hook (lambda() 
        (setq font-lock-syntactic-face-function 
        'py-font-lock-syntactic-face-function))) 
2

我有一些像在.emacs如下:

(eval-after-load "python-mode" 
    (add-hook 'python-mode-hook 
    (lambda() 
     (font-lock-add-keywords nil 
     '(("[^\\w]\\(r\\|u\\)[\'\"]" (1 font-lock-keyword-face))))))) 

其中突出的 'U' 本身( 'R'),而不是整个字符串。也许这已经足够了,或者你可以看到适应它的方法。

+0

感谢您的回答。这部分有助于。但是,由于字符串中的文本对于unicode和常规stings都具有相同的颜色,所以它不足以吸引您的注意力(我希望常规字符串真正脱颖而出,以便当我看到他们在浏览大型代码库时,我可以轻松地将它们与unicode字符串区分开来)。 – 2012-01-14 16:20:09

0

喻慎在评论中讨论了一个完全不同的案例。所提及的语法不由简单句法分析器分析(参见例如syntax-ppss)。需要定义自己的字体锁处理程序(最好是jit-lock)。

给定的任务中存在一个特殊问题。需要检测用户何时完成符号的输入,否则该符号的每个部分都将被登记在字典中并且将获得其自己的颜色。下面的代码检查点是否在符号之外。如果您在符号之后键入空格,则符号高亮。

下面的代码只是一个可能的解决方案的粗略实现。其他更好的解决方案存在。

 
(defvar tag-font-lock-dict (make-hash-table :test 'equal) 
    "Dictionary that assigns colors to tags.") 
(make-variable-buffer-local 'tag-font-lock-dict) 

(defvar tag-font-lock-re "#[[:alnum:]]+\\>" 
    "Regular expression defining tags.") 

(defvar tag-font-lock-colors (apply 'vector (cdddr (defined-colors))) 
    "Vector of available colors. We should be more selective here.") 

(defvar tag-font-lock-num-used-colors 0 
    "Number of used colors.") 
(make-variable-buffer-local 'tag-font-lock-num-used-colors) 

(require 'cl) 

(defun tag-font-lock-next-color() 
    "Get the next color for a new tag." 
    (prog1 
     (aref tag-font-lock-colors tag-font-lock-num-used-colors) 
    (setq tag-font-lock-num-used-colors 
     (mod (1+ tag-font-lock-num-used-colors) 
      (length tag-font-lock-colors))))) 

(defun tag-font-lock-handler (b e) 
    "Colorize tags in region from b to e." 
    (let (col ol sym (pt (point))) 
    (save-excursion 
     (remove-overlays b e 'tag-font-lock t) ;; No danger of splitted overlays. We have always full lines. 
     (goto-char b) 
     (while (re-search-forward tag-font-lock-re e 'noErr) 
    (when (or (= pt (match-end 0))) 
     (setq sym (match-string-no-properties 0) 
     ol (make-overlay (match-beginning 0) (match-end 0)) 
     col (or (gethash sym tag-font-lock-dict) 
      (puthash sym (tag-font-lock-next-color) tag-font-lock-dict))) 
     (overlay-put ol 'face (list (list :foreground col))) 
     (overlay-put ol 'tag-font-lock t) 
    ))))) 

(defun tag-font-lock-clear() 
    "Remove color from tags in current buffer." 
    (interactive) 
    (remove-overlays 0 (buffer-size) 'tag-font-lock t) 
    (clrhash tag-font-lock-dict)) 

(define-minor-mode tag-font-lock-mode 
    "Highlight tags." 
    :lighter " TH" ;; stands for tag highlight 
    (if tag-font-lock-mode 
     (progn 
    (setq font-lock-extend-region-functions 'font-lock-extend-region-wholelines) 
    (font-lock-mode 1) 
    (jit-lock-register 'tag-font-lock-handler)) 
    (jit-lock-unregister 'tag-font-lock-handler) 
    (tag-font-lock-clear))) 
+0

谢谢! - 花了近2年的答案:) – 2013-11-05 20:04:32

相关问题