"exists('*mylib#func')" will return 0 in some cases even if it's installed

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

"exists('*mylib#func')" will return 0 in some cases even if it's installed

tyru-2
Hi list.

Place these scripts in ~/.vim
- autoload/mylib.vim
- autoload/mylib2.vim


--- autoload/mylib.vim ---

function! mylib#func()
    echo 'mylib#func()'
    echo exists('*mylib2#func')
endfunction


--- autoload/mylib2.vim ---

function! mylib2#func()
    echo 'mylib2#func()'
endfunction


Run vim and type this.
:call mylib#func()   " => 0


And, exists() also returns bogus value at plugin/mylib.vim like this.

--- plugin/mylib.vim ---

function! MylibFunc()
    echo 'MylibFunc()'
    echo exists('*mylib2#func')
endfunction


:call MylibFunc()
will also print "0".

--
You received this message from the "vim_dev" 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "exists('*mylib#func')" will return 0 in some cases even if it's installed

Bram Moolenaar

Tyru Exe wrote:

> Hi list.
>
> Place these scripts in ~/.vim
> - autoload/mylib.vim
> - autoload/mylib2.vim
>
>
> --- autoload/mylib.vim ---
>
> function! mylib#func()
>     echo 'mylib#func()'
>     echo exists('*mylib2#func')
> endfunction
>
>
> --- autoload/mylib2.vim ---
>
> function! mylib2#func()
>     echo 'mylib2#func()'
> endfunction
>
>
> Run vim and type this.
> :call mylib#func()   " => 0
>
>
> And, exists() also returns bogus value at plugin/mylib.vim like this.
>
> --- plugin/mylib.vim ---
>
> function! MylibFunc()
>     echo 'MylibFunc()'
>     echo exists('*mylib2#func')
> endfunction
>
>
> :call MylibFunc()
> will also print "0".

This works as intended.  Autoload scripts are only loaded when used.
Not when you check if something defined in it exists.

--
   An extraordinary TALL KNIGHT in all black (possibly John with Mike on his
   shoulders) walks out from the dark trees.  He is extremely fierce and
   gruesome countenance.  He walks towards KING ARTHUR and PATSY, who are
   wazzing like mad.  (Salopian slang, meaning very scared.  almost to the
   point of wetting oneself, e.g. before an important football match or
   prior to a postering.  Salopian slang meaning a beating by the school
   praeposters.  Sorry about the Salopian slant to this stage direction - Ed.)
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--
You received this message from the "vim_dev" 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "exists('*mylib#func')" will return 0 in some cases even if it's installed

Peter Odding-3
In reply to this post by tyru-2
Hi tyru,

I've run into this while adding integration between two plug-ins I was
developing. When you control both plug-ins you can add a dummy function
which the other plug-in can try to call (where catch /E117/ means the
plug-in isn't installed) or you can just use the g:loaded_plugin
convention and use exists('g:loaded_plugin') to detect the other
plug-in. Of course the g:loaded_plugin variable should be set from a
plug-in script in ~/.vim/plugin/ and not from a script in
~/.vim/autoload/, otherwise your back to the same problem :-). You also
probably shouldn't test for these g:loaded_* variables until all
plug-ins have been loaded (use the VimEnter autocmd or just test at
runtime).

If the above doesn't help because you really only want to check specific
functions, can't you just call them and catch any E117 errors?

  - Peter Odding

--
You received this message from the "vim_dev" 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "exists('*mylib#func')" will return 0 in some cases even if it's installed

tyru-2
In reply to this post by Bram Moolenaar
> Bram
> This works as intended.  Autoload scripts are only loaded when used.
> Not when you check if something defined in it exists.

This is expected behavior?
hmm, but checking this at command-line, exists() returns 1.
    :echo exists('*mylib#func')
and seeing :scriptnames, autoload/mylib.vim has been loaded.

This is also the same at "top level" of script.

------------------------
let s:is_exists = exists('*mylib#func')

function! s:foo()
    if s:is_exists
        call mylib#func()
    endif
endfunction

call s:foo()
------------------------

s:is_exists is 1 at this time, but
------------------------
function! s:foo()
    if exists('*mylib#func')
        call mylib#func()
    endif
endfunction

call s:foo()
------------------------
This does not call mylib#func() expectedly.


> Peter
Thanks.
I often add "mylib#load()" function to autoload script :-)

--
You received this message from the "vim_dev" 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "exists('*mylib#func')" will return 0 in some cases even if it's installed

Benjamin R. Haskell-8
On Sun, 11 Jul 2010, tyru wrote:

> > Bram
> > This works as intended.  Autoload scripts are only loaded when used.
> > Not when you check if something defined in it exists.
>
> This is expected behavior?
> hmm, but checking this at command-line, exists() returns 1.
>     :echo exists('*mylib#func')
> and seeing :scriptnames, autoload/mylib.vim has been loaded.

The expected behavior is for it to return 0.  But, as tyru is seeing,
I'm also getting the unexpected return of 1 with the following, simpler setup:

===> /tmp/just-autoload/vimrc <===
let &rtp='/tmp/just-autoload,'.&rtp
fun! TestExists()
    echo '(expected) in TestExists:' exists('*mylib#func')
endfun
call TestExists()
echo '(unexpected) in vimrc:' exists('*mylib#func')
quit

===> /tmp/just-autoload/autoload/mylib.vim <===
fun! mylib#func()
    " function never gets called
    echo "(not printed:) called mylib#func()"
endfun

$ vim -U NONE -N -u /tmp/just-autoload/vimrc
(expected) in TestExists: 0
(unexpected) in vimrc: 1

--
Best,
Ben

--
You received this message from the "vim_dev" 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "exists('*mylib#func')" will return 0 in some cases even if it's installed

Bram Moolenaar

Benjamin R. Haskell wrote:

> On Sun, 11 Jul 2010, tyru wrote:
>
> > > Bram
> > > This works as intended.  Autoload scripts are only loaded when used.
> > > Not when you check if something defined in it exists.
> >
> > This is expected behavior?
> > hmm, but checking this at command-line, exists() returns 1.
> >     :echo exists('*mylib#func')
> > and seeing :scriptnames, autoload/mylib.vim has been loaded.
>
> The expected behavior is for it to return 0.  But, as tyru is seeing,
> I'm also getting the unexpected return of 1 with the following, simpler setup:
>
> ===> /tmp/just-autoload/vimrc <===
> let &rtp='/tmp/just-autoload,'.&rtp
> fun! TestExists()
>     echo '(expected) in TestExists:' exists('*mylib#func')
> endfun
> call TestExists()
> echo '(unexpected) in vimrc:' exists('*mylib#func')
> quit
>
> ===> /tmp/just-autoload/autoload/mylib.vim <===
> fun! mylib#func()
>     " function never gets called
>     echo "(not printed:) called mylib#func()"
> endfun
>
> $ vim -U NONE -N -u /tmp/just-autoload/vimrc
> (expected) in TestExists: 0
> (unexpected) in vimrc: 1

Yeah, exists() should not load the autoload script.
It's a side effect of translating the function name.
I'll fix that.

--
You were lucky. We lived for three months in a brown paper bag in a
septic tank. We used to have to get up at six o'clock in the morning,
clean the bag, eat a crust of stale bread, go to work down mill for
fourteen hours a day week in-week out. When we got home, our Dad
would thrash us to sleep with his belt!

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--
You received this message from the "vim_dev" 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
Loading...