On 16-Oct-2012 04:54:43 +0200, Tony Mechelynck wrote:
> After upgrading my copy of the CSApprox plugin
> http://www.vim.org/scripts/script.php?script_id=2390 to version 4.00 I noticed
> that if a global Funcref is set to a script-local function in its defining
> script, the Funcref will be stored as pointing to s:<something>, not to
> <SNR>nn_<something>, i.e., the context is lost, and any attempt to invoke that
> Funcref from outside its defining script causes an error, because Vim tries to
> invoke the script-local function in the context of the caller.
>
> I admit that my colorscheme has one fancy feature: it alternates the
> StatusLine
> and WildMenu highlights by means of a CursorHold,CursorHoldI autocommand. In
> Console mode with CSApprox installed, in a terminal capable of (88 or) 256
> colours, this means setting highlights for these two highlight groups only,
> using a CSApprox function to get the ctermfg and ctermbg values without (for
> performance) running the whole plugin as the CursorScheme autocommand event
> would. A further complication is that the colorscheme is sourced early, by a
> :colorscheme statement in the vimrc, while the plugin/CSApprox.vim script is
> only sourced afterwards, together with other global plugins.
>
> The following doesn't work (the first :let statement triggers an error):
>
> if exists('g:CSApprox_approximator_function')
> let s:ctbg1 = g:CSApprox_approximator_function(0, 255, 0) " green
> let s:ctbg2 = g:CSApprox_approximator_function(255, 0, 0) " red
> let s:ctfg = g:CSApprox_approximator_function(0, 0, 0) " black
> else
> let s:ctbg1 = 'darkgreen'
> let s:ctbg2 = 'black'
> let s:ctfg = 'white'
> endif
>
> The following, which uses the contents of the contents of the Funcref, works
> (it
> causes an early sourcing of autoload/csapprox/per_component.vim and
> autoload/csapprox/common.vim, with no ill effects AFAICS):
>
> try
> let s:ctbg1 = csapprox#per_component#Approximate(0, 255, 0) " green
> let s:ctbg2 = csapprox#per_component#Approximate(255, 0, 0) " red
> let s:ctfg = csapprox#per_component#Approximate(0, 0, 0) " black
> catch
> let s:ctbg1 = 'darkgreen'
> let s:ctbg2 = 'black'
> let s:ctfg = 'white'
> endtry
>
>
> Best regards,
> Tony.
This is a known deficiency of Funcrefs. You can work around this by expanding
the <SID> on your own, via a function like this:
#v+
function! s:function(name)
return function(substitute(a:name, '^s:', matchstr(expand('<sfile>'),
'<SNR>\d\+_\zefunction$'),''))
endfunction
#v-
There recently was a discussion for an enhanced native call interface, where ZyX
suggested an implementation that would fix this problem as a side effect:
https://groups.google.com/group/vim_dev/msg/d767d19228f5a886?hl=en
-- regards, ingo
--
You received this message from the "vim_dev" 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