2012-01-05 25 views
2

我想要做的是類似如下:如何將VIM中的語法高亮顏色設置爲語法腳本中局部變量的值?

"define our language struct variables with their default syntax color values 
let s:procedural_fg = "Red" 
let s:declarative_fg = "Blue" 
let s:string_fg = "DarkGray" 

... 

"Now the actual highlighting directives based on the groups defined elsewhere 
hi cyLoops guifg=s:procedural_fg 
hi cyConstants guifg=s:declarative_fg 
hi cyString guifg=s:string_fg 

但VIM不會讓我設置guifg值這種方式(「錯誤:無法分配顏色S:procedural_fg」 ......和每個變量名稱都是如此)。我想定義突出顯示這種方式的語法,以便通過更改局部變量值然後刷新緩衝區(或適用新顏色值所需的任何值)來動態更改它。

這可以在VIM語法腳本中完成嗎?如果是這樣,怎麼樣?

我已經嘗試了幾種變化:

"define our language struct variables with their default syntax color values 
let s:procedural_fg = Red 
let s:declarative_fg = Blue 
let s:string_fg = DarkGray 

... 

"Now the actual highlighting directives based on the groups defined elsewhere 
hi cyLoops guifg=s:procedural_fg 
hi cyConstants guifg=s:declarative_fg 
hi cyString guifg=s:string_fg 

"define our language struct variables with their default syntax color values 
let s:procedural_fg = v:Red 
let s:declarative_fg = v:Blue 
let s:string_fg = v:DarkGray 

... 

"Now the actual highlighting directives based on the groups defined elsewhere 
hi cyLoops guifg=s:procedural_fg 
hi cyConstants guifg=s:declarative_fg 
hi cyString guifg=s:string_fg 

從而導致錯誤抱怨,紅色,藍色等,或v:紅色,V:藍等都是未定義和/或無效的表達式。

感謝, CCJ

回答

2

使用:exec,這是Vim如何eval是Perl或bash shell中:

:exec 'hi cyLoops guifg=' . s:procedural_fg 
+0

啊哈,甜甜蜜編程的力量!謝謝! – CCJ 2012-01-06 20:23:02