Thread: [Vim-latex-devel] Some problems with command implementation
Brought to you by:
srinathava,
tmaas
From: Srinath A. <sr...@fa...> - 2002-12-27 09:06:58
|
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 |
From: Luc H. <her...@fr...> - 2002-12-27 23:27:21
|
* On Fri, Dec 27, 2002 at 01:06:32AM -0800, Srinath Avadhanula <sr...@fa...> wrote: > I am facing a problem implementing some commands in templates.vim, > packages.vim and custommacros.vim... I have as an example, the > following setup: > > ---------------------%<--------------------- > inoremap <F3> <C-r>=Foo()<CR> > nnoremap <F3> i<C-r>=Foo()<CR> > com! -nargs=? FooCom normal! i<C-r>=Foo()<CR> > ---------------------%<--------------------- > > 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... I remember I add a similar problem sometimes ago. Unfortunately, I don't remember how I solved it, or if I solved it. I wonder if I didn't give up to finally call :normal from the function... And I guess that: command! -nargs=? FooCom :normal <F3> does not solve your problem ? -- Luc Hermitte http://hermitte.free.fr/vim/ |
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 |
From: Srinath A. <sr...@fa...> - 2002-12-29 23:40:34
|
On Sun, 29 Dec 2002, Benji Fisher wrote: > 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> > " === Hello! This works nicely! Thanks! I shall use this idea in packages.vim, templates.vim and custommacros.vim. BTW, please check the latest version of latex-suite (from CVS) for bugs etc. I want this to be the next stable release before we work on any further enhancements. Thanks, Srinath |
From: Srinath A. <sr...@fa...> - 2002-12-30 00:38:08
|
On Sun, 29 Dec 2002, Srinath Avadhanula wrote: > This works nicely! Thanks! > > I shall use this idea in packages.vim, templates.vim and > custommacros.vim. > I see that there's still a problem: The command: com! Foo normal! i doesn't leave the user in insert mode, whereas com! Foo normal! 0v$<C-g> does leave the user in select mode. This seems to be a vim inconsistency... Any workarounds? Doing a startinsert after the command is not going to cut it because sometimes we want to end up in insert mode, sometimes in select mode. Srinath |
From: Benji F. <be...@me...> - 2002-12-30 01:59:30
|
Srinath Avadhanula wrote: > On Sun, 29 Dec 2002, Srinath Avadhanula wrote: > > > >This works nicely! Thanks! > > > >I shall use this idea in packages.vim, templates.vim and > >custommacros.vim. > > > > > I see that there's still a problem: > > The command: > > com! Foo normal! i > > doesn't leave the user in insert mode, whereas > > com! Foo normal! 0v$ > > does leave the user in select mode. > > This seems to be a vim inconsistency... Any workarounds? Doing a > startinsert after the command is not going to cut it because sometimes > we want to end up in insert mode, sometimes in select mode. > > Srinath I think the idea of "complete command" under :help :normal is that you are not allowed to end in Insert mode. It is inconvenient, but I have always been able to work around it. Of course, I have never tried to do much with commands... I guess my general approach is to keep the command simple: mostly, it just calls a function. The function can end with something like if a:endMode == "i" startinsert elseif a:endMode == "s" execute "normal! 0v$\<C-G>" elseif a:endMode == "v" normal! 0v$ endif (I was going to say that your command leaves me in Visual mode, not Select mode, until I remembered the 'selectmode' option. Let's wait until someone complains, and then modify all our functions to respect this one!) HTH --Benji |
From: Srinath A. <sr...@fa...> - 2002-12-30 03:11:39
|
On Sun, 29 Dec 2002, Benji Fisher wrote: > (I was going to say that your command leaves me in Visual mode, not > Select mode, until I remembered the 'selectmode' option. Let's wait > until someone complains, and then modify all our functions to respect > this one!) ... or we could just document this as a 'feature' ;) In any case, I guess it works reasonably well now. The easiest way to insert a \usepackage is just pressing <F5> on an empty line in the preamble anyway... Srinath |
From: Carl M. <cm...@ma...> - 2002-12-31 16:52:37
|
I forgot to mention, it might also be good to have a macro for conditional statements: f(x) = { 3 if x>5 2x if x<5 Here is my macro for this (following Lamport's book). inoremap <buffer> <F6> \left\{\begin{array}{ll}<CR>&\mbox{$$} \\<CR>&\mbox{}<CR>\end{array}<CR>\right.<Up><Up><Up><Home> Amslatex has \begin{cases}...\end{cases}. Best wishes, Carl |