如何理解这些vim脚本

gla*_*man 6 vim

我有两个关于理解那些vim脚本的问题.请给一些帮助,

问题1:我下载了a.vim插件,我尝试阅读这个插件,如何理解下面的变量定义?我能理解的第一行,但第二行,我不完全知道"g:alternateExtensions _ {'aspx.cs'}"的意思.

" E.g. let g:alternateExtensions_CPP = "inc,h,H,HPP,hpp" 
"      let g:alternateExtensions_{'aspx.cs'} = "aspx"
Run Code Online (Sandbox Code Playgroud)

问题2:如何在函数名之前理解"SID",使用如下函数定义和函数调用.

function! <SID>AddAlternateExtensionMapping(extension, alternates)
//omit define body

call <SID>AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")
call <SID>AddAlternateExtensionMapping('H',"C,CPP,CXX,CC")
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

Ing*_*kat 20

let g:alternateExtensions_{'aspx.cs'} = "aspx"
Run Code Online (Sandbox Code Playgroud)

这是Vimscript表达式的内联扩展为变量名称,这是一个相当模糊的功能,自Vim版本7以来很少使用.请参阅:help curly-braces-names详细信息.它通常用于插入变量,而不是像here('aspx.cs')那样的字符串文字.此外,这会产生错误,因为变量名中禁止使用句点.较新的插件将使用List或Dictionary变量,但是在编写a.vim时这些数据类型不可用.


为了避免污染函数名称空间,插件内部函数应该是脚本本地的,即具有前缀s:.要从映射调用这些,<SID>必须使用特殊前缀而不是s:,因为<SID>内部被转换为保留脚本ID的东西,而pure s:作为映射的一部分执行时,已失去与定义的脚本的关联它.

一些插件作者也不完全理解Vim的作用域实现的这种不幸和偶然的复杂性,并且他们也将<SID>前缀放在函数名称的前面(这也是有效的).虽然它稍微更正确,但建议像这样写:

" Define and invoke script-local function.
function! s:AddAlternateExtensionMapping(extension, alternates)
...
call s:AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")

" Only in a mapping, the special <SID> prefix is actually necessary.
nmap <Leader>a :call <SID>AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")
Run Code Online (Sandbox Code Playgroud)


rom*_*inl 7

<SID>解释在:help <SID>

When defining a function in a script, "s:" can be prepended to the name to
make it local to the script.  But when a mapping is executed from outside of
the script, it doesn't know in which script the function was defined.  To
avoid this problem, use "<SID>" instead of "s:".  The same translation is done
as for mappings.  This makes it possible to define a call to the function in
a mapping.

When a local function is executed, it runs in the context of the script it was
defined in.  This means that new functions and mappings it defines can also
use "s:" or "<SID>" and it will use the same unique number as when the
function itself was defined.  Also, the "s:var" local script variables can be
used.
Run Code Online (Sandbox Code Playgroud)

这个数字就是你在左边看到的那个数字:scriptnames,IIRC。