2011-12-29 86 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

相關問題