Function behaviour problem

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Function behaviour problem

Bugzilla from tomasz2k@poczta.onet.pl
Hey

I don't understand why does this happends. Cursor is in the middle of documentWhen I map a key like this:

nmap <F12> :color morning<CR>

it works ok. Changes my colorscheme. But when I do it like this:

nmap <F12> :exec ToggleColors()<CR>

function! ToggleColors()
        color adaryn
endfunction

it changes colorscheme... and moves cursor to first line. Why is that?

Thanks in advance
Tomasz Kalkosinski
Reply | Threaded
Open this post in threaded view
|

Re: Function behaviour problem

A.J.Mechelynck
----- Original Message -----
From: <[hidden email]>
To: <[hidden email]>
Sent: Thursday, September 22, 2005 10:22 AM
Subject: Function behaviour problem


> Hey
>
> I don't understand why does this happends. Cursor is in the middle of
> documentWhen I map a key like this:
>
> nmap <F12> :color morning<CR>
>
> it works ok. Changes my colorscheme. But when I do it like this:
>
> nmap <F12> :exec ToggleColors()<CR>
>
> function! ToggleColors()
> color adaryn
> endfunction
>
> it changes colorscheme... and moves cursor to first line. Why is that?
>
> Thanks in advance
> Tomasz Kalkosinski

":exec" takes as argument an Ex-command, not a function. To run a function
from the command-line, use ":call functionname()" or ":echo functionname()".
The latter displays the return value of the function, or 0 if ":return"
wasn't used or was used without an argument.

Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Function behaviour problem

Charles E Campbell Jr
In reply to this post by Bugzilla from tomasz2k@poczta.onet.pl
[hidden email] wrote:

>nmap <F12> :exec ToggleColors()<CR>
>
>function! ToggleColors()
> color adaryn
>endfunction
>
>it changes colorscheme... and moves cursor to first line. Why is that?
>  
>

Hmm. The default return value of a function is 0.
So, you're doing an "exe 0"; as a side effect, ToggleColors() also
changes the colorscheme.

Typing
:0
will give you the same line-changing effect. To do what you want:

nmap <F12> :call ToggleColors()<cr>

However, is there a reason why you don't just have

nmap <F12> :color adaryn<cr>

?

Regards,
Chip Campbell