Re: Function To Search Directory And Display File List

2006-07-14 Thread Tim Chase

Well, as an ugly first-pass hack:

:let s = expand("*template")
:echo substitute("\n".s."\n",
'^\%([^[:cntrl:]]*[[:cntrl:]]\)\{'.(confirm('Which file?',
s)).'}\([^[:cntrl:]]*\).*', '\1', 'g')


Thanks a *ton* Tim!  This looks very succinct (if not a bit
frightening).  I'll definitely try this one out too.


The 10k foot view is "offer the user the list of files (the 
confirm() call) extract the Nth position from that list of files 
as determined by the index returned from the confirm() call".



Wow, I'm glad that I started Vim scripting after version 7 was released,
because it looks like it was much more difficult to script using version
6.


Vim7 added lists (among other more advanced datatypes, IIRC) 
which makes this hack considerably less taxing.  The above hack 
is a way of treating a string like a list, using the newline 
characters (which happen to be control-characters, and thus match 
the POSIX-type [[:cntrl:]] notation) as demarcations between list 
items.


You could probably change the

"\n".s."\n"

to just

"\n".s

as, looking over it, it doesn't seem to require something 
following the item of interest.


Hope this helps make a bit more sense of the chaos. :)

-tim






Re: Function To Search Directory And Display File List

2006-07-14 Thread stri ker

Try
:set wildmenu

I put the above line in my .vimrc file and now can do the following:

:e *s*#then hit tab and any file in the current directory that  
contains an 's' is displayed in a menu.


Good luck,
Kevin


On Jul 13, 2006, at 1:58 PM, Tom Purl wrote:


Hi, I'm a Vim scripting newbie, and had a pretty general question.

I'm would like to write a function that does the following:

1. Searches a directory for all files that end in the string  
"Template"

2. Return those file names to the user as chooseable list, similar to
the way that the spell checker functionality works when you use the z=
command.

Would anyone know how to do this?  I checked the functions help in  
Vim,

but couldn't find what I was looking for.

Thanks in advance for any guidance!

Tom Purl





Re: Function To Search Directory And Display File List

2006-07-13 Thread Tom Purl
>> 1. Searches a directory for all files that end in the string "Template"
>> 2. Return those file names to the user as chooseable list, similar to
>> the way that the spell checker functionality works when you use the z=
>> command.
>
> Well, as an ugly first-pass hack:
>
> :let s = expand("*template")
> :echo substitute("\n".s."\n",
> '^\%([^[:cntrl:]]*[[:cntrl:]]\)\{'.(confirm('Which file?',
> s)).'}\([^[:cntrl:]]*\).*', '\1', 'g')
>
> Those two lines create a prompt with available matches for
> whatever is stashed in "s".
>
> Instead of an :echo you can do an assignment, or make it the
> return value of a function so you don't have to recreate this
> beast repeatedly.

Thanks a *ton* Tim!  This looks very succinct (if not a bit
frightening).  I'll definitely try this one out too.

> It can be wrapped in a function if desired.  Hari's suggestion
> was a good one, but requires Vim7 (at least it didn't work on 6.3
> when I tried it because of the need for the inputlist() and
> split() functions).  As you reference the "like spell-checker",
> you're likely running 7.0 and thus are safe with his much clearer
> method.

Wow, I'm glad that I started Vim scripting after version 7 was released,
because it looks like it was much more difficult to script using version
6.

> The above two-liner, despite its brutish opacity, should work at
> least back to 6.0.

Thanks again!

Tom Purl



Re: Function To Search Directory And Display File List

2006-07-13 Thread Tom Purl
> From your description, it is not clear if this will be part of a larger
> script that you are developing. If all that you want to do is ability to
> search and open the file, one of the existing plugins might be able to
> help you.

I'm adding a function to the potwiki script so that I can create a new
wiki page based on a template.

> But if you want to ask the user to select one and do something
> with that selection, you can use the glob() and inputlist() functions to
> do this, something like this:
>
> let files = glob(dir.'*Template')
> if files != ''
>   let filelist = split(files, "\n")
>   let sel = inputlist(['Select file:']+filelist)
>   if sel != -1
> let selectedFile = filelist[sel]
>   endif
> endif

This example worked very well for me.  Thanks a ton!

Tom Purl



Re: Function To Search Directory And Display File List

2006-07-13 Thread Tim Chase

1. Searches a directory for all files that end in the string "Template"
2. Return those file names to the user as chooseable list, similar to
the way that the spell checker functionality works when you use the z=
command.



Well, as an ugly first-pass hack:

:let s = expand("*template")
:echo substitute("\n".s."\n", 
'^\%([^[:cntrl:]]*[[:cntrl:]]\)\{'.(confirm('Which file?', 
s)).'}\([^[:cntrl:]]*\).*', '\1', 'g')


Those two lines create a prompt with available matches for 
whatever is stashed in "s".


Instead of an :echo you can do an assignment, or make it the 
return value of a function so you don't have to recreate this 
beast repeatedly.


It can be wrapped in a function if desired.  Hari's suggestion 
was a good one, but requires Vim7 (at least it didn't work on 6.3 
when I tried it because of the need for the inputlist() and 
split() functions).  As you reference the "like spell-checker", 
you're likely running 7.0 and thus are safe with his much clearer 
method.


The above two-liner, despite its brutish opacity, should work at 
least back to 6.0.


-tim





Re: Function To Search Directory And Display File List

2006-07-13 Thread Hari Krishna Dara

On Thu, 13 Jul 2006 at 12:58pm, Tom Purl wrote:

> Hi, I'm a Vim scripting newbie, and had a pretty general question.
>
> I'm would like to write a function that does the following:
>
> 1. Searches a directory for all files that end in the string "Template"
> 2. Return those file names to the user as chooseable list, similar to
> the way that the spell checker functionality works when you use the z=
> command.
>
> Would anyone know how to do this?  I checked the functions help in Vim,
> but couldn't find what I was looking for.
>
> Thanks in advance for any guidance!
>
> Tom Purl
>

>From your description, it is not clear if this will be part of a larger
script that you are developing. If all that you want to do is ability to
search and open the file, one of the existing plugins might be able to
help you. But if you want to ask the user to select one and do something
with that selection, you can use the glob() and inputlist() functions to
do this, something like this:

let files = glob(dir.'*Template')
if files != ''
  let filelist = split(files, "\n")
  let sel = inputlist(['Select file:']+filelist)
  if sel != -1
let selectedFile = filelist[sel]
  endif
endif

-- 
HTH,
Hari

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Function To Search Directory And Display File List

2006-07-13 Thread Tom Purl
Hi, I'm a Vim scripting newbie, and had a pretty general question.

I'm would like to write a function that does the following:

1. Searches a directory for all files that end in the string "Template"
2. Return those file names to the user as chooseable list, similar to
the way that the spell checker functionality works when you use the z=
command.

Would anyone know how to do this?  I checked the functions help in Vim,
but couldn't find what I was looking for.

Thanks in advance for any guidance!

Tom Purl