Wednesday, January 27, 2010

Re: Vim script, not show path.

Hi,

Athunye wrote:
>> you can define your own custom completion function which can take care of
>> removing the paths, e.g.:
>>
>> let s:CompletionPath = ''
>>
>> function! CompleteFromPath(ArgLead, CmdLine, CursorPos)
>> let files = split(glob(s:CompletionPath . '*'), "\n")
>> call map(files, 'substitute(v:val, ".*/", "", "")')
>> return join(files, "\n")
>> endfunction
>>
>> function! VS()
>> let s:CompletionPath = '~/public_html/jeditux.home/vim_sessions/'
>> let ans = input('Session name: ', '', 'custom,CompleteFromPath')
>> execute ':source ' . s:CompletionPath . ans
>> endfunction
>
> Wow! That was really cool. Worked exactly as I expected.
> That was my first try to write something in Vim's language,
> and I don't understand your (GREAT) version of the script
> yet. (reading the extensible help)

as you already found out the last parameter of input() determines which
kind of completion is to be used. You used 'file', but this uses the
current directory or the path in the second argument to input().

With 'custom,...' (or 'customlist,...') you are much more flexible. For
both completion styles a function with three arguments must be defined.
They differ in their result type: one returns a string with the allowed
values separated by a newline character, the other returns a list of
strings. I chose the variant with a single string, because here Vim
takes care of the filtering.

There is no way to give additional information to the completion
function apart from the three mandatory arguments, so the path from
which the file is to be selected is passed through a script-global
variable (s:CompletionPath). Inside the completion function glob() is
used to get a string with all files from this directory. These files are
separated by newlines, so split() is used to put them in a list. The
resulting list is then passed to map() which uses the second argument as
a command to remove everything up to the last slash (i.e., the path)
from the file names. At the end the list of file names is join()ed with
newlines, so that we can return a string with "pure" file names.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: