Re: [Vim-latex-devel] Some problems with command implementation
Brought to you by:
srinathava,
tmaas
From: Benji F. <be...@me...> - 2002-12-29 20:23:44
|
Srinath Avadhanula wrote: > Hello, > > I am facing a problem implementing some commands in templates.vim, > packages.vim and custommacros.vim... I have as an example, the following > setup: > > ---------------------%<--------------------- > fun! Foo(...) > if a:0 == 0 > let name = input('Whats your name?', 'bozo') > else > let name = a:1 > endif > return 'Your name is '.name > endfun > > inoremap <F3> <C-r>=Foo()<CR> > nnoremap <F3> i<C-r>=Foo()<CR> > inoremenu Help.foo <C-r>=Foo()<CR> > nnoremenu Help.foo i<C-r>=Foo()<CR> > > com! -nargs=? FooCom normal! i<C-r>=Foo()<CR> > ---------------------%<--------------------- > > With this setup, the insert and normal mode menu and mappings work as > expected. It also works when FooCom is called with 1 argument, such as > :FooCom bar > However, when FooCom is called without any arguments, we end up always > getting a value 'bozo' for name because the <CR> gets eaten up by the > input() immediately (I think). Whats a good way to make the command > work? I tried placing a ":call getchar()" at the beginning of the > function but even then the command without argument doesn't work... > > Also, I cannot use something like: > > com! -nargs=? FooCom put!=Foo() > > because in general Foo() will contain movement commands such as <Esc>3h > and stuff... Moreover, sometimes, we would like to end up in say select > mode after the function returns... So :put will not work... > > Any ideas? > > Srinath How about this? Note that I have changed the :if line in Foo() as well as the command. " === fun! Foo(...) if a:0 == 0 || a:1 == "" let name = input('Whats your name?', 'bozo') else let name = a:1 endif return 'Your name is '.name endfun inoremap <F3> <C-r>=Foo()<CR> nnoremap <F3> i<C-r>=Foo()<CR> inoremenu Help.foo <C-r>=Foo()<CR> nnoremenu Help.foo i<C-r>=Foo()<CR> com! -nargs=? FooCom let s:FooVal = Foo(<q-args>) <Bar> normal! i<C-r>=s:FooVal<CR> " === For consistency, you could always call Foo() with one argument: use "" instead of no arguments for the default behavior. HTH --Benji |