How to understand these vim scripts

I have two questions about understanding these vim script. please, help,

Question 1: I download the a.vim plugin and I'm trying to read this plugin, how do I understand the variable definition below? The first line I can understand, but the second line, I don’t know for sure. g: alternateExtensions _ {'aspx.cs'} "means.

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

Question 2: how to understand the "SID" before the function name, using, as shown below, the function definition and function call.

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

Thank you for your kind help.

+4
source share
2 answers
 let g:alternateExtensions_{'aspx.cs'} = "aspx" 

This is a built-in extension of a Vimscript expression to a variable name, a rather hidden function that is rarely used with Vim version 7. See :help curly-braces-names for more information. It is usually used to interpolate a variable, rather than a string literal like here ( 'aspx.cs' ). In addition, this leads to an error because periods are not allowed in variable names. Newer plugins will use the List or Dictionary variable, but these data types were not available when a.vim was written.


To avoid contamination of the function namespace, the internal functions of the plugin should be script -local, that is, have the s: prefix. To call them from the comparison, instead of s: should use the special <SID> prefix, because <SID> internally converted to something that supports the script identifier, while pure s: when executed as part of the display, lost touch with the script that identified him.

Some plugin authors do not fully understand this unfortunate and random complexity of the Vim scoping implementation, and they put the <SID> prefix also in front of the function name (which also works). Although this is a little more correct and it is recommended to write it like this:

 " 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") 
+16
source

<SID> is explained in :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. 

This is the number you see on the left when you do :scriptnames , IIRC.

+3
source

Source: https://habr.com/ru/post/1482902/


All Articles