<SID> or s: - General questions about functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

<SID> or s: - General questions about functions

rameo
After a half year of use of VIM, I still don't understand what is, and
if there is a difference between <SID> and "s:". (even after reading
the help file)

Can they be exchanged?

Fun!, fun!, Function! and function!
is the same isn't?
Endfunction, endfunction, Endfun, endfun also?

I often Titlecase functionnames.
Is this the same?:
fun! s:Myfunction() --> :call s:Myfunction() --> call
<SID>Myfunction()
fun! s:myfunction() --> :call s:myfunction() --> call
<SID>myfunction()
fun! s:MYFUNCTION() --> :call s:MYFUNCTION() --> call
<SID>MYFUNCTION()

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: <SID> or s: - General questions about functions

Tony Mechelynck
On 12/04/11 08:41, rameo wrote:
> After a half year of use of VIM, I still don't understand what is, and
> if there is a difference between<SID>  and "s:". (even after reading
> the help file)
>
> Can they be exchanged?

Sometimes they can, sometimes they cannot. The idea is that you cannot
use s: when creating an object (such as a mapping) that will not
necessarily be run in the context of the current script. But you may
(IIUC) define a function as

        function s:foobar()
                ...
        endfunction

and in the same script define a mapping

        :map <Leader>FB :call <SID>foobar()

to call it. This macro would then be executed in the context of wherever
you hit \FB, probably at the command-line (in which case there is no
"current script", so you need the script number contained in <SID>,
which is stored as <SNR>nn_ where nn is the current "script number" [as
reported by the :scriptnames command] at the time the mapping is _defined_).

>
> Fun!, fun!, Function! and function!
> is the same isn't?

No. Vim ex-commands are case-sensitive. For instance :next and :previous
are different, but :Next is the same as :previous (and different from
:next). Similarly :cnext vs. :cNext aka :cprevious

:fun! and :function! are the same. :Fun! and :Function! are probably
undefined (and a syntax error).

> Endfunction, endfunction, Endfun, endfun also?

Also not, and for the same reason.

>
> I often Titlecase functionnames.
> Is this the same?:
> fun! s:Myfunction() -->  :call s:Myfunction() -->  call
> <SID>Myfunction()
> fun! s:myfunction() -->  :call s:myfunction() -->  call
> <SID>myfunction()
> fun! s:MYFUNCTION() -->  :call s:MYFUNCTION() -->  call
> <SID>MYFUNCTION()
>

Like ex-commands, function names (and variable names) are
case-sensitive. I would expect these three examples to mean three
different functions, none of which would override either of the others.


Best regards,
Tony.
--
The real problem with fucking a sheep is that you have to walk around
in front every time you want to kiss her.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: <SID> or s: - General questions about functions

Nikolay Aleksandrovich Pavlov
In reply to this post by rameo
Reply to message «<SID> or s: - General questions about functions»,
sent 10:41:25 12 April 2011, Tuesday
by rameo:

> After a half year of use of VIM, I still don't understand what is, and
> if there is a difference between <SID> and "s:". (even after reading
> the help file)
It is simple: if a function name (only *function* name!) starts with `s:' and
contains only letters, digits and underscores then `s:' will be translated to
`<SID>' (which is really `<SNR>{N}_'). There is a special case: anonymous
dictionary functions:
    function s:Foo()
will define function <SNR>{N}_Foo, but
    function s:.Foo()
(note the dot) will define function {M} and store reference to created function
in the value of the key Foo of dictionary s: (see :h internal-variables).

Both {N} and {M} are decimal string representations of arbitrary unsigned
integer numbers. First is the number of the script where function was defined,
second is the number of the function (incremented for each subsequent function
definitions).

`s:' -> `<SNR>{N}_' translation is valid for function names only. Script
variables cannot be accessed this way, in variables `s:' only designates the
scope of variable.

When you will see any function definition, don't forget that there are *no*
local functions, all function definitions are global. `s:' translation is just a
hack: you may define function `<SNR>1000_Foo' even if you don't have one
thousand script files. You may even define function `<SNR>Foo', or `b:Foo', or
`w:Foo', or even `_:foo': neither of these are really local functions. If you
write a script and want to make your functions unaccessible without a debugger,
use anonymous functions:
- you cannot call them without having a reference,
- you can obtain reference to them only by copying an existing reference,
- you cannot access script-local variable without a debugger (unless you posted
reference to s: dictionary somewhere).

> Fun!, fun!, Function! and function!
> is the same isn't?
> Endfunction, endfunction, Endfun, endfun also?
Case matters, `command' and `Command' are never the same. Truncations are
different: `fu', `fun' and `function' are the same as well as `endf', `endfun'
and `endfunction'. For user-defined commands truncations must be unambigious
(you can't have commands `FooBar' and `FoBar' and truncate one of them to `Fo'),
but this is not true for built-ins (`f' is a short form of `file', for example).

> Is this the same?:
> fun! s:Myfunction() --> :call s:Myfunction() --> call
> <SID>Myfunction()
> fun! s:myfunction() --> :call s:myfunction() --> call
> <SID>myfunction()
> fun! s:MYFUNCTION() --> :call s:MYFUNCTION() --> call
> <SID>MYFUNCTION()
Case matters, but `s:' and `<SID>' are the same.

Original message:

> After a half year of use of VIM, I still don't understand what is, and
> if there is a difference between <SID> and "s:". (even after reading
> the help file)
>
> Can they be exchanged?
>
> Fun!, fun!, Function! and function!
> is the same isn't?
> Endfunction, endfunction, Endfun, endfun also?
>
> I often Titlecase functionnames.
> Is this the same?:
> fun! s:Myfunction() --> :call s:Myfunction() --> call
> <SID>Myfunction()
> fun! s:myfunction() --> :call s:myfunction() --> call
> <SID>myfunction()
> fun! s:MYFUNCTION() --> :call s:MYFUNCTION() --> call
> <SID>MYFUNCTION()

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: <SID> or s: - General questions about functions

rameo
Thank you very much Tony and ZyX.

My functions names do only contains letters, digits and underscores.
If I've understood you well, I can now unify all my functions and

change s: ---> <SID>
change function! ---> fun!
change endfunction! --> endfun!

Is that correct?

and can I change also this:
s/\\d\\{2,}/\\=s:MyFunctionName(submatch(0))/g
in:
s/\\d\\{2,}/\\=<SID>MyFunctionName(submatch(0))/g

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: <SID> or s: - General questions about functions

Nikolay Aleksandrovich Pavlov
Reply to message «Re: <SID> or s: - General questions about functions»,
sent 10:52:37 13 April 2011, Wednesday
by rameo:

> My functions names do only contains letters, digits and underscores.
> If I've understood you well, I can now unify all my functions and
>
> change s: ---> <SID>
> change function! ---> fun!
> change endfunction! --> endfun!
>
> Is that correct?
1. Yes (for functions only).
2. Yes (though I would prefer to change fun! -> function!, not the opposite).
3. Yes (both will throw E477 error because `endfunction' does not accept bang.
You may even change this to, for example, `call!' for this reason).

> and can I change also this:
> s/\\d\\{2,}/\\=s:MyFunctionName(submatch(0))/g
> in:
> s/\\d\\{2,}/\\=<SID>MyFunctionName(submatch(0))/g
No (first will replace string `\d\{2,}' with `\=s:MyFunctionName(submatch(0))',
second will replace it with `\=<SID>MyFunctionName(submatch(0))'. You should
half the number of backslashes to make it mean the same).

Original message:

> Thank you very much Tony and ZyX.
>
> My functions names do only contains letters, digits and underscores.
> If I've understood you well, I can now unify all my functions and
>
> change s: ---> <SID>
> change function! ---> fun!
> change endfunction! --> endfun!
>
> Is that correct?
>
> and can I change also this:
> s/\\d\\{2,}/\\=s:MyFunctionName(submatch(0))/g
> in:
> s/\\d\\{2,}/\\=<SID>MyFunctionName(submatch(0))/g

signature.asc (205 bytes) Download Attachment