Thursday, November 3, 2011

Re: [Suggestion] New functions iskeywordchar(), isfnamechar(), isprintchar(), and isidentchar()

On 11/03/11 13:07, Axel Bender wrote:
> Sometimes it's necessary to determine whether a given character belongs to
> one of the options whose names are reflected by the above (suggested)
> function names. Though it's possible to write those functions in vim
> itself, one would either have to call such a function "manually" a) after a
> change to the respective option value (e.g. set iskeyword+=#), or b) before
> every check for containedness in the option value.
>
> As vim tracks the option values internally, it should be no big deal to
> provide such functions.

Those would be written as something like

function Iskeywordchar(c)
return a:c=~'\k'
endfunction

function Isfnamechar(c)
return a:c=~'\f'
endfunction

function Isprintchar(c)
return a:c=~'\p'
endfunction

function Isidentchar(c)
return a:c=~'\w'
endfunction

Since they're so trivial, I don't expect they'd make it into core
Vim as functions. The added advantage of using the "=~" notation
is that it can work for any pattern. Say you want to check for a
valid C identifier. If you just ask whether a *character* is a
valid C-identifier character with the "Isidentchar()" above, it
will return true for "4", and if you change it to "\h", then a
scan of characters in a valid identifier such as "Street2" will
fail on "2". Using the "=~", you can simply test

... my_var=~'\h\w*' ...

and it catches all the right cases.

So you'll want to read up at

:help /\i " and following
:h =~

and see how much more power Vim already puts in your hands :)

-tim


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