13

これらのvimスクリプトを理解することについて2つの質問があります。助けてください、

質問 1: a.vim プラグインをダウンロードし、このプラグインを読み込もうとしましたが、以下の変数定義を理解するにはどうすればよいですか? 最初の行は理解できますが、2行目、「g:alternateExtensions_{'aspx.cs'}」の意味が正確にはわかりません。

" E.g. let g:alternateExtensions_CPP = "inc,h,H,HPP,hpp" 
"      let g:alternateExtensions_{'aspx.cs'} = "aspx"

質問 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")

親切に助けてくれてありがとう。

4

2 に答える 2

28
let g:alternateExtensions_{'aspx.cs'} = "aspx"

これは、Vim スクリプトの式を変数名にインライン展開したものであり、Vim バージョン 7 以降ではほとんど使用されていない、ややあいまいな機能です。詳細については、を参照:help curly-braces-namesしてください。通常、ここのような文字列リテラルではなく、変数を補間するために使用されます ( 'aspx.cs')。さらに、変数名ではピリオドが禁止されているため、ここではエラーが発生します。新しいプラグインは List または Dictionary 変数を使用しますが、a.vimが作成された時点ではこれらのデータ型は使用できませんでした。


関数の名前空間の汚染を避けるために、プラグイン内部の関数はスクリプト ローカルにする必要があります。つまり、接頭辞s:. これらをmapping<SID>から呼び出すには、 の代わりに特別な接頭辞を使用する必要があります。これは、 はスクリプトの ID を保持するものに内部的に変換されるs:ためです。一方、純粋なは、マッピングの一部として実行されると、定義したスクリプトへの関連付けが失われます。それ。<SID>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")
于 2013-05-27T07:38:34.757 に答える
8

<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.

:scriptnamesその番号は、IIRCを実行したときに左側に表示される番号です。

于 2013-05-27T07:28:48.617 に答える