感謝指針米沙Arefiev,它讓我找對了地方。這是我想出來的,它涵蓋了我所有的原始要求。我現在所知道的唯一的限制是,它會突出顯示無效的數字後綴,就好像它是正確的(例如「123ulu」)
(add-hook 'c-mode-common-hook (lambda()
(font-lock-add-keywords nil '(
; Valid hex number (will highlight invalid suffix though)
("\\b0x[[:xdigit:]]+[uUlL]*\\b" . font-lock-string-face)
; Invalid hex number
("\\b0x\\(\\w\\|\\.\\)+\\b" . font-lock-warning-face)
; Valid floating point number.
("\\(\\b[0-9]+\\|\\)\\(\\.\\)\\([0-9]+\\(e[-]?[0-9]+\\)?\\([lL]?\\|[dD]?[fF]?\\)\\)\\b" (1 font-lock-string-face) (3 font-lock-string-face))
; Invalid floating point number. Must be before valid decimal.
("\\b[0-9].*?\\..+?\\b" . font-lock-warning-face)
; Valid decimal number. Must be before octal regexes otherwise 0 and 0l
; will be highlighted as errors. Will highlight invalid suffix though.
("\\b\\(\\(0\\|[1-9][0-9]*\\)[uUlL]*\\)\\b" 1 font-lock-string-face)
; Valid octal number
("\\b0[0-7]+[uUlL]*\\b" . font-lock-string-face)
; Floating point number with no digits after the period. This must be
; after the invalid numbers, otherwise it will "steal" some invalid
; numbers and highlight them as valid.
("\\b\\([0-9]+\\)\\." (1 font-lock-string-face))
; Invalid number. Must be last so it only highlights anything not
; matched above.
("\\b[0-9]\\(\\w\\|\\.\\)+?\\b" . font-lock-warning-face)
))
))
任何建議/最佳化/修復的歡迎!
編輯:停止突出顯示評論內的數字。
Emacs有幾個不同的高亮包; 'font-lock-mode'和'hilit19'等等。你在用哪個? – 2012-01-14 04:15:28
我懷疑迄今爲止從未聽說過的標準C模式使用hilit19。字體鎖定更有可能。 – Tom 2012-01-14 06:51:02
我不知道如何說 - 我會想象我仍在使用默認設置。 – Malvineous 2012-01-14 07:18:08