2012-11-25 24 views
8

的Vimscript中的幫助文件指出,定義用戶功能時:爲什麼我用磅符號(#)打破VIM用戶函數的命名規則

函數名必須以大寫字母開頭,來避免 與內建函數混淆。

除了以下通過查看其他代碼發現的情況,這是強制執行的。

"This should not work. 
"But it does as long as the function is in a file called 'overrides.vim'. 
function! overrides#name() abort 
    echo 'Test overrides\name' 
endfunction 

"This should not work either. 
"But it does as long as the file above is in a folder called 'plugin'. 
function! plugin#overrides#name() abort 
    echo 'Test plugin\overrides\name' 
endfunction 

let stupid = {} 
"This should not work. 
"But it does aslong as the stupid Dictionary is defined. 
function! stupid.name() abort 
    echo 'Test stupidname' 
endfunction 


call overrides#name() 
call plugin#overrides#name() 
call stupid.name() 

我到處尋找任何可以解釋這種語法的東西。我知道現在這個工作。我非常好奇的是,對於那些你使用過這種語法的人,你從哪裏瞭解到它?

還有其他的vimscript功能在幫助文件的任何地方都沒有提到嗎?

+0

由於內建函數不會包含'#'字符,所以不存在混淆的可能性,因此關於以大寫字母啓動函數名稱的規則不適用於自動加載函數。 – qqx

+0

@qqx不能這樣,因爲我可以命名一個函數'crazy&name'或者'crazy * name',腳本將會失敗,並且E128:函數名必須以大寫開頭或者包含冒號。「 – dkinzer

+1

@dkinzer It只是說解析器首先使用dump來確定函數名在哪裏結束,然後開始檢查。如果你修正了它('Crazy&name'),你會得到不同的錯誤,但是在函數名中不允許使用'&' E124:Missing'(':Crazy&name()'。有更多的信息:'fu abc()'的作品,就像'fu :()'和'fu a :::::::::不同於自動加載和字典功能,這在任何地方都沒有提及 – ZyX

回答

9

此命名語法用於autoload函數。請輸入:help autoload-functions尋求幫助。

AUTOMATICALLY LOADING FUNCTIONS ~ 
                  *autoload-functions* 
When using many or large functions, it's possible to automatically define them 
only when they are used. There are two methods: with an autocommand and with 
the "autoload" directory in 'runtimepath'. 
+0

哇,對於我的生活我無法找到。謝謝! – dkinzer

+0

Graat和:help字典功能回答了我的問題的第二部分。畢竟這都在那裏。 – dkinzer

相關問題