2011-08-27 116 views
1

我正在使用Vim的Utl插件,並且正在尋找一種創建自定義自動完成功能的方法,以生成指向文件中id標籤的鏈接。我想要使​​用的格式是:Vim的自定義自動完成中的文件列表

:CommandName <file> <id tag in file> 

我希望該函數的行爲像第一個參數的標準目錄完成。對於第二個參數,我希望它搜索第一個參數中指定的文件,以「id =」開頭的所有字符串並返回值。

我已經複製了類似的功能出主UTL包,但我還沒有親近使其工作,但目前看起來是這樣的:

fu! CompleteArgs(dummy_argLead, cmdLine, dummy_cursorPos) 

" Split cmdLine to figure out how many arguments have been provided by user 
" so far. If Using argument keepempty=1 for split will provide empty last 
" arg in case of a new arg is about to begin or an non empty last argument 
" in case of an incomplete last argument. So can just remove the last arg. 
exe "echo \"cmdline:\" \"".a:cmdLine."\"" 
let utlArgs=split(a:cmdLine, '\s\+', 1) 
execute "echo" string(utlArgs) 
echo "echo" "test complete" 
"remove the function name 
call remove(utlArgs, -1) 

" 1st arg to complete 
if len(utlArgs)==1 
return string(glob("*")) 
endi 

" 2nd arg to complete 
if len(utlArgs)==2 
    "insert code here 
endif 
endfun 

有沒有人有什麼想法?

回答

1

您可以試用frawor。如果你安裝它的代碼將如下:

execute frawor#Setup('0.0', {'@/fwc': '0.2', 
      \   '@/commands': '0.0',}) 
" No need to write bang here: above command will forbid script to be sourced 
" twice, see :h frawor#Reload for how it can be updated. 
function s:F.cmdfunc(file, tag) 
    " It will be called when the command launches. Alternatively you can replace 
    " `s:F.cmdfunc' in the below command.add call with a string you already had 
    " before. Note that you will have to replace s: in function names with <SID> 
    " and s:* variables will be just unaccessible. 
endfunction 
function s:F.gettags(file) 
    " This assumes that format is the same as used in ~/.vim/doc/tags. Note that 
    " if there may be any spaces, then you must escape them. 
    return map(readfile(a:file), 'matchstr(v:val, "\\v^.{-}\\t")[:-2]') 
endfunction 
" This replaces direct :command call, see :h frawor-f-command.add 
call s:_f.command.add('CommandName', s:F.cmdfunc, 
      \{ 'nargs': '+', 
      \'complete': ['path in*F.gettags(@<)']}) 
+0

謝謝,這幾乎是完美的。 – Sparky

1

Answering非常相似question我寫了一個完整的函數 ,它決定了要完成的命令參數的個數。以下是適合您的情況的 版本。

command! -nargs=* -complete=custom,FooComplete Foo echo [<f-args>] 
" Custom completion function for the command 'Foo' 
function! FooComplete(arg, line, pos) 
    let l = split(a:line[:a:pos-1], '\%(\%(\%(^\|[^\\]\)\\\)\@<!\s\)\+', 1) 
    let n = len(l) - index(l, 'Foo') - 1 
    if n == 1 
     return string(glob('*')) 
    endif 
    return "1\n2\n3" " Replace this with appropriate id-completion code 
endfunction 

功能正確處理轉義空白(這是一個 參數,而不是一個分離器的一部分)和空白命令名之前。請注意0​​建議候選中的空白字符應該被轉義,否則 一個參數將被Vim視爲兩個參數。