sunaku / vim-unbundle

🎁 Fast, filetype-lazy loader of Vim scripts & plugins
http://www.vim.org/scripts/script.php?script_id=4744
96 stars 6 forks source link

Add FileType for load autocmd FileType settings. #7

Closed heavenshell closed 11 years ago

heavenshell commented 11 years ago

Hi, nice plugin I like it. But I've got a problem.

I use jedi.vim which is auto complete for Python. I've Downloaded jedi.vim to ~/.vim/ftbundle/python/ and then open python file. But jedi.vim did not work at GUI MacVim.app. (CUI MacVim works well)

unbundle.vim did not load autocmd FileType when ftbundle plugin loaded.

jedi.vim defined autocmd FileType python at https://github.com/davidhalter/jedi-vim/blob/master/plugin/jedi.vim#L49 (One of the example using autocmd FileType)

IMHO unbundle.vim is heavily related with filetype. A lot of FileType plugin defined autocmd FileType.

So, I add FileType to doautoall.

Please check and include.

Regards,

sunaku commented 11 years ago

Thanks for the patch.

But I have one concern: Unftbundle() ends up calling itself because the doautoall FileType inside Unftbundle() triggers the autocmd FileType call Unftbundle() directive near the bottom of unbundle.vim. Thankfully, the recursion is not infinite. :sweat_smile:

Also, the BufRead event should have caused set filetype=python when you opened a *.py file. I wonder why this does not occur in MacVim GUI? :apple:

heavenshell commented 11 years ago

Hi, thanks for quick reply.

Also, the BufRead event should have caused set filetype=python when you opened a *.py file. I wonder why this does not occur in MacVim GUI?

I reproduced in Windows GUI/CUI Vim following steps(I use old Windows XP).

  1. Startup Vim
  2. At this point, filetype does not set because nothing in buffer
  3. Type :set ft=python to change filetype
  4. Now unbundle.vim did not load autocmd FileType.

Sorry, I hava no idea why CUI MacVim works ok :-(

Regards,

heavenshell commented 11 years ago

Hi,

I continute to look into this problem. I think another plugin conflict to jedi.vim. Would you please wait to merge or it's ok to reject this patch. If this problem occuerd by unbundle.vim, I'll report you.

I applicate your kindness.

Regards,

sunaku commented 11 years ago

Sure, I will wait. There's no hurry. :innocent:

heavenshell commented 11 years ago

HI, I catch up this problem.

The reason why this problem occured by filetype plugin on before unbundle.vim load.

If filetype plugin on set before unbundle.vim, need to reload filetype for execute autocmd FileType. Because filetype plugin on execute autocmd FileType once.

Following code is a sample to check autocmd behavior.

autocmd!
set nocompatible

filetype indent plugin on
filetype off
filetype indent plugin on

autocmd FileType vim call s:Sample()

function! s:Sample()
  echomsg 'sample'
endfunction

This script echo sample once.

A lot of Japanese Vim user favor to use Vim-Kaoriya package which are included Original Vim binary + additional plugins + some patches. This package contains vimrc_example.vim and load before $HOME/.vimrc load. And vimrc_example.vim conatines filetype plugin indent on.

FYI Vim detect $VIM/{vimrc,.vimrc,_vimrc} load it and then load $HOME/.vimrc. Using this package, filetype indnt plugin always on.

So autocmd FileType already executed before unbundle.vim load.

Followings are use-cases of filetype state when unbundle.vim loaded.

  1. filetype plugin:OFF -> Did not set filetype plugin on before. This case is OK to load autocmd FileType because unbundle.vim set fietype plugin indent on at last.
  2. filetype plugin:ON -> autocmd FileType already executed. So need to reload autocmd FileType
  3. filetype plugin:(on) -> filetype is off but already executed filetype plugin on before. So need to reload autocmd FileType

I update my patch. Checking filetype loaded or not, if filetype is OFF, nothings to do. Otherwise set filetype again to execute autocmd FileType.

BTW I reported MacVim CUI works ok before, sorry that was my probrem. I aliased vim to vim -u $HOME/.vimrc to not load example_vimrc.vim :-p

Please check.

Regards,

sunaku commented 11 years ago

Thanks for the explanation and updated patch! :sparkles:

What if we turn off filetype detection before turning it on?

diff --git a/unbundle.vim b/unbundle.vim
index 09b47bd..b25b91f 100644
--- a/unbundle.vim
+++ b/unbundle.vim
@@ -49,5 +49,6 @@ augroup Unftbundle
   autocmd!
   autocmd FileType * call Unftbundle(expand('<amatch>'))
 augroup END
 runtime! ftbundle/*/*/ftdetect/*.vim
+filetype off
 filetype plugin indent on

Would that solve the problem?

heavenshell commented 11 years ago

Hi, thank you for comment.

Would that solve the problem?

No. If already filetype plugin on before unbundle.vim, 2nd filetype plugin on did not execute autocmd FileType. IMHO this is Vim's spec. Please see :h filetype-off.

You can see filetype info to type :filetype.

  1. start Vim $ vim -u NONE -U NONE -N
  2. type :filetype
  3. shows filetype detection:OFF plugin:OFF indent:OFF
  4. type :filetype plugin on and :filetype
  5. shows filetype detection:ON plugin:ON indent:OFF
  6. type :filetype off and :filetype 7 shows filetype detection:OFF plugin:(on) indent:OFF plugin:(on) means Vim caches filetype plugin flag. So autocmd FileType did not execute again.

Regards,

sunaku commented 11 years ago

Thanks for the detailed analysis! :+1:

Since Vim is caching the filetype plugin flag at step 7, let's try turning off all filetype flags:

diff --git a/unbundle.vim b/unbundle.vim
index 09b47bd..b25b91f 100644
--- a/unbundle.vim
+++ b/unbundle.vim
@@ -49,5 +49,6 @@ augroup Unftbundle
   autocmd!
   autocmd FileType * call Unftbundle(expand('<amatch>'))
 augroup END
 runtime! ftbundle/*/*/ftdetect/*.vim
+filetype plugin indent off
 filetype plugin indent on

Does that solve it?

By the way, I am asking you these additional questions because I think the solution can be made shorter (less lines of code). Thanks for your patience.

heavenshell commented 11 years ago

Hi,

Great, it's works! Very nice :+1:

By the way, I am asking you these additional questions because I think the solution can be made shorter (less lines of code). Thanks for your patience.

Totally agree with you.

Thank you for solving problem.

sunaku commented 11 years ago

Merged as c854f6a3d6d00fd57ba5ad120800d64439b608ee. Thanks for your contribution! :smiley_cat: