views:

45

answers:

2

I have a number of scripts (Ruby as it happens) I run from VIM, by setting up the startup file to contain (for instance):

amenu Ruby.script1 :%!ruby C:\ruby_scripts\script1.rb<cr><cr>
amenu Ruby.script2 :%!ruby C:\ruby_scripts\script2.rb<cr><cr>
...

What I would like to do, is to have VIM automatically check the C:\ruby_scripts directory and assign menu items automatically - can this be done?

+1  A: 

You can combine glob() and exe with something like this:

let dirname = 'c:/ruby_scripts'
for script in split(glob(dirname . '/*.rb'))
    " Get the script name
    let scriptname = fnamemodify(script, ':t:r')
    let scriptfile = fnamemodify(script, ':p')
    " Add the item to the menu
    exe 'amenu Ruby.' . scriptname . ' :%!ruby ' . scriptfile . '<cr><cr>'
endfor

For more information, see:

:help glob()
:help split()
:help fnamemodify()
:help expand()
:help :exe
Al
work great - thanks !
monojohnny
+1  A: 

Try:

function! s:AddScript(dir, menuname)
  let files = lh#path#GlobAsList(a:dir, "*.rb")
  for f in files
    let n = fnamemodify(f, ":t:r")
    exe "anoremenu ".a:menuname.".".n." :%!ruby ".f."<cr><cr>"
  endfor
endfunction

call s:AddScript("c:/ruby_scripts", "Ruby")

NB: lh#path#GlobAsList comes from lh-vim-lib. With a substitute(), you shall be able to transform globpath() result to the string you'll then need to :execute.

Luc Hermitte
Couldn't get this to work on my Vim install - I guess due to the lh#path#GlobAsList stuff you mentioned: but still, thankyou very much!
monojohnny
Yes, this function needs to be installed with its script in the right directory. As it offers some more services you don't seem to need, Al solution is more than enough.
Luc Hermitte