2012-04-30 13 views
2

我經常使用的使用inputdialog使用執行命令:組合選項中使用inputdialog

let n = confirm({msg} [, {choices} [, {default} [, {type}]]]) 

體育搜索號碼
if n == 1 - > p.e.用'。'來搜索所有數字,
if n == 2 - > p.e.搜索所有指數
if n == 3 - > p.e.用3位數字搜索所有數字
etc

但是用這種方法我只能選擇一個參數。

在Vim中,您可以在inputdialog中一起選擇多個參數嗎?

回答

2

解決方法,使用input()函數,讓用戶選擇多個選項並將它們分成列表以處理它們。舉個例子:

下一個功能添加到vimrc或類似的文件:

func My_search() 
    let my_grouped_opts = input ("1.- Search one\n2.- Search two\n3.- Search three\n") 
    let my_list_opts = split(my_grouped_opts, '.\zs') 
    for opt in my_list_opts 
     echo "Option number " opt " selected" 
    endfor 
endfunction 

叫它:

:call My_search() 

即會出現您的選擇:

1.- Search one 
2.- Search two 
3.- Search three 

選擇他們喜歡的:

23 

而該功能將它們分成一個列表。

+0

謝謝birei,當我在命令行中插入第一個字符時,發生了一些奇怪的事情......光標向右移動了20個空格。我找不到它是什麼。你有好主意嗎? (順便說一下,我使用menu.vim中的函數) – Reman

+0

在幫助文件中找到它。我不得不在輸入行周圍放入':calls inputsave()'和':call inputrestore()'。 :) – Reman

+0

我做了一個檢查,看看輸入是否只有數字。如果沒有,我會返回一個錯誤輸出。你知道如何形象化嗎?使用'my_grouped_opts'項目的長列表不能看到錯誤輸出。 – Reman

3

你可以使用input()來提示用戶輸入一個字符串,然後檢查返回列表:

let string = input({msg}, {choices}, ...) 

例如,用戶可以輸入1,2,3,你可以做這個字符串的文本比較:

if (string =~ 1) 
    " do something 
endif 

if (string =~ 2) 
    " do something 
endif 

if (string =~ 3) 
    " do something 
endif 

一個更復雜的方法(例如,如果有超過9個選項更多)可能是將字符串分割成一個列表:

let choice_list = split(string, ',') 

for choice in choice_list 
    if choice == 1 
     " do something 
    endif 
    if choice == 2 
     " do something 
    endif 
    if choice == 3 
     " do something 
    endif 
endfor 

由於返回的字符串可能是用戶決定輸入的任何內容,因此您可能需要添加一些完整性檢查,以確定該字符串的確是整數列表。