每个`ftplugin/name.vim`都需要定义`b:undo_ftplugin`吗?

kev*_*kev 7 vim

$VIMRUNTIME/ftplugin/(例如python.vimada.vim)中的某些脚本没有定义b:undo_ftplugin.该cpo选项的默认值是aABceFs.

当我set ft=python,然后set ft=css.$VIMRUNTIME/ftplugin/css.vim立即完成.而且omnifunc=pythoncomplete#Complete一直都是.

每个人都ftplugin/name.vim需要定义b:undo_ftplugin吗?


这是 /usr/share/vim/vim73/ftplugin.vim:

" Vim support file to switch on loading plugins for file types
"
" Maintainer:   Bram Moolenaar <Bram@vim.org>
" 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
Run Code Online (Sandbox Code Playgroud)

这是/usr/share/vim/vim73/ftplugin/css.vim:

" Vim filetype plugin file
" Language:         CSS
" Maintainer:       Nikolai Weibull <now@bitwi.se>
" 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
Run Code Online (Sandbox Code Playgroud)

如果我set ft=python,那么set ft=css.Vim无法通过此测试:

if &cpo =~# "S" && exists("b:did_ftplugin")
Run Code Online (Sandbox Code Playgroud)

b:did_ftplugin不会被删除,所以ftplugin/css.vim立即完成.

Ing*_*kat 9

:help undo_ftplugin 提到:

当用户执行":setfiletype xyz"时,应撤消先前文件类型的效果.

注意它说"应该",而不是"必须".但是,根据实施情况而定

func! s:LoadFTPlugin()
    if exists("b:undo_ftplugin")
        exe b:undo_ftplugin
        unlet! b:undo_ftplugin b:did_ftplugin
    endif
Run Code Online (Sandbox Code Playgroud)

ftplugin 必须定义b:undo_ftplugin,或者其文件类型设置不能再通过更改:setf.我认为文档应该指出,并且所有ftplugins确实应该设置b:undo_ftplugin(如果只是一个空的,无操作值).