Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should every `ftplugin/name.vim` need to define `b:undo_ftplugin`?

Tags:

vim

Some scripts in $VIMRUNTIME/ftplugin/(for example python.vim and ada.vim) don't define b:undo_ftplugin. The cpo option default value is aABceFs.

When I set ft=python, then set ft=css. $VIMRUNTIME/ftplugin/css.vim finish immediately. And omnifunc=pythoncomplete#Complete all the time.

Should every ftplugin/name.vim need to define b:undo_ftplugin?


This is /usr/share/vim/vim73/ftplugin.vim:

" Vim support file to switch on loading plugins for file types
"
" Maintainer:   Bram Moolenaar <[email protected]>
" Last change:  2006 Apr 30

if exists("did_load_ftplugin")
  finish
endif
let did_load_ftplugin = 1

augroup filetypeplugin
  au FileType * call s:LoadFTPlugin()

  func! s:LoadFTPlugin()
    if exists("b:undo_ftplugin")
      exe b:undo_ftplugin
      unlet! b:undo_ftplugin b:did_ftplugin
    endif

    let s = expand("<amatch>")
    if s != ""
      if &cpo =~# "S" && exists("b:did_ftplugin")
        " In compatible mode options are reset to the global values, need to
        " set the local values also when a plugin was already used.
        unlet b:did_ftplugin
      endif

      " When there is a dot it is used to separate filetype names.  Thus for
      " "aaa.bbb" load "aaa" and then "bbb".
      for name in split(s, '\.')
        exe 'runtime! ftplugin/' . name . '.vim ftplugin/' . name . '_*.vim ftplugin/' . name . '/*.vim'
      endfor
    endif
  endfunc
augroup END

This is /usr/share/vim/vim73/ftplugin/css.vim:

" Vim filetype plugin file
" Language:         CSS
" Maintainer:       Nikolai Weibull <[email protected]>
" Latest Revision:  2008-07-09

if exists("b:did_ftplugin")
  finish
endif
let b:did_ftplugin = 1

let s:cpo_save = &cpo
set cpo&vim

let b:undo_ftplugin = "setl com< cms< inc< fo< ofu<"

setlocal comments=s1:/*,mb:*,ex:*/ commentstring&
setlocal formatoptions-=t formatoptions+=croql
setlocal omnifunc=csscomplete#CompleteCSS

let &l:include = '^\s*@import\s\+\%(url(\)\='

let &cpo = s:cpo_save
unlet s:cpo_save

If I set ft=python, then set ft=css. Vim cannot pass this test:

if &cpo =~# "S" && exists("b:did_ftplugin")

b:did_ftplugin doesn't get deleted, so ftplugin/css.vim finish immediately.

like image 610
kev Avatar asked Jul 11 '12 05:07

kev


1 Answers

:help undo_ftplugin mentions:

When the user does ":setfiletype xyz" the effect of the previous filetype should be undone.

Note that it says "should", not "must". But, according to the implementation

func! s:LoadFTPlugin()
    if exists("b:undo_ftplugin")
        exe b:undo_ftplugin
        unlet! b:undo_ftplugin b:did_ftplugin
    endif

a ftplugin must define b:undo_ftplugin, or its filetype settings cannot be changed any more via :setf. I think the documentation should point that out, and all ftplugins should indeed set b:undo_ftplugin (if only to an empty, no-op value).

like image 145
Ingo Karkat Avatar answered Oct 10 '22 07:10

Ingo Karkat