Monday, February 28, 2011

Re: using the help system in split screen mode

On Mon, 28 Feb 2011, Chris Jones wrote:

> Another quick question.. in Vimscript, what is the natural idiom to
> code:
>
> if count == 0; do proc_0; fi
> if count == 1; do proc_1; fi
> if count > 1; do proc_n; fi

In what language is that a natural idiom? Is it...


... a for loop? VimL has :for loops if you're just trying to call a
function for 0 through 'count':

" +1 makes it [0,count] inclusive
for i in range(count+1)
call Proc(i)
endfor

See:
:help range()
:help :for

... something recursive?

fun! Factorial(count)
if a:count < 2
return 1
endif
return a:count * Factorial(a:count - 1)
endfun

:help function-argument

... a 'case' or 'select' statement?

Vim doesn't have that, so resort to if ... elseif ... elseif ... else ...
endif:

if count == 0
" do Proc_0 here
elseif count == 1
" do Proc_1 here
else
" do Proc_n here
endif

... some kind of "call a function with a user-specified name" or
dispatch table? Vim handles "symbolic references" or "variable
variables" (probably more names in other langs):

if exists('*Proc_'.count)
call Proc_{count}()
else
echo "Oh no, there's no Proc_".count."() defined"
endif

See:
:help exists()
:help :call
:help curly-braces-names


> Otherwise, is there a decent tutorial on how to write functions in
> Vim..?

:help script

--
Best,
Ben

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: