Loading ...
Sorry, an error occurred while loading the content.

Re: Loading plugin only once

Expand Messages
  • Teemu Likonen
    I added vim-dev as well because this may concern developers too. This is not a bug report; this only touches an issue which might be good idea to clarify in
    Message 1 of 1 , Aug 6, 2008
    • 0 Attachment
      I added vim-dev as well because this may concern developers too. This is
      not a bug report; this only touches an issue which might be good idea to
      clarify in the reference manual. I'm talking about the subject of adding
      "load the script only once" mechanism to autoload scripts.


      Teemu Likonen wrote (2008-08-06 09:40 +0300):

      > I'm still not sure I understand _how_ this applies to autoload
      > scripts. Let's be concrete: I command
      >
      > call myplugin#Function()
      >
      > for the first time. What files get loaded? Does Vim load all the
      > autoload/myplugin.vim files which are found from the runtimepath and
      > after that try to call the function again? Or does Vim try to call the
      > function after loading each occurence of autoload/myplugin.vim (in which
      > case it probably stops searching runtimepath after the wanted function
      > is found)?

      Well, I tested. It seems that I was correct in assuming that autoload
      scripts are loaded only once in any case. I did two autoload scripts:
      one which is in the beginning of runtimepath and one which is in the
      end.

      ~/.vim/autoload/test.vim:

      function! test#TestFunction()
      let g:vim_test = 'The first'
      endfunction

      ~/.vim/after/autoload/test.vim:

      function! test#TestFunction()
      let g:vim_test = 'The second'
      endfunction
      function! test#SomethingElse()
      let g:vim_test = 'Something'
      endfunction

      Then I did

      :call test#TestFunction()

      which resulted in g:vim_test with the value "The first". The function
      test#SomethingElse() didn't get defined. This must mean that autoload
      scripts are sourced only to the point when the wanted function is found.
      But I also tried

      :call test#SomethingElse()

      which resulted in error "E117: Unknown function: test#SomethingElse".
      This means that the searching of autoload scripts ended at the point
      where the first autoload/test.vim was found. Since it didn't define the
      function test#SomethingElse() Vim printed an error message. The
      ~/.vim/after/autoload/test.vim is loaded only when there are no other
      autoload/test.vim files before in the runtimepath.

      So I guess this kind of proves that autoload scripts are loaded only
      once in any case. I even consider it harmful to add "let
      g:loaded_myplugin = 1" kind of things to autoload scripts. If user
      deletes (maybe by accident) one of the functions with :delfunction then
      the function isn't there and when trying to call it again the autoload
      mechanism isn't able to redefine it. It's because the "if
      exists('g:autoload_myplugin') | finish | endif" prevents it.

      I think the best thing is to not add "only load once" mechanism to
      autoload scripts. How about clarifying this in the Vim manual?

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_dev" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    Your message has been successfully submitted and would be delivered to recipients shortly.