> @Andy
>
> Thanks for the answer!
>
> The split on '\zs' was new to me. Made some speed tests and found
> that for lines with more than 180 chars the first solution is faster,
> and for shorter lines the solution below, which is based on the
> split() approach. Opted for the new solution as short lines are much
> more common.
>
> function! CWord(string, class_start, class_end)
>> let l:chars = split(a:string, '\zs')
>> let l:len = len(l:chars)
>> let l:pos = virtcol(".") - 1
Using virtcol() looks wrong
>> let l:start = l:pos
>> while l:start>= 0&& l:chars[l:start] =~ a:class_start
>> let l:start -= 1
>> endwhile
>>
>> let l:end = l:pos
>> while l:end< l:len&& l:chars[l:end] =~ a:class_end
>> let l:end += 1
>> endwhile
>>
>> return join(l:chars[l:start + 1:l:end - 1], '')
>> endfunction
A few more hints ...
Are you aware of
:echo expand("<cword>")
:echo expand("<cWORD>")
:h expand()
If you just expect 'iskeyword' characters for the identifier (or
whatever you try to match), then
:echo expand("<cword>")
will do.
You can distinguish between possible start and end characters:
:echo matchstr(expand("<cword>"), '\h\w*')
(not perfect, but will do in *most* cases)
Or use a custom 'isk' setting, just for the matching:
func! CWord1(custom_isk)
let sav_isk = &isk
let &l:isk = a:custom_isk
let word = expand("<cword>")
let &l:isk = sav_isk
return word
endfunc
Just a few hints ...
I'd never loop over characters in Vimscript if possible.
--
Andy
--
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
No comments:
Post a Comment