Tuesday, August 30, 2011

Re: Actual cursor position.

Reply to message «Actual cursor position.»,
sent 02:12:13 31 August 2011, Wednesday
by Gerardo Marset:

> I'm looking for a way to get the current cursor position in the line
> (that is, the column) in *characters*.
> col is no use, because it returns the position in bytes. I can't use
> virtcol either because it returns the position on the screen: a tab
> counts as 8 characters (or whatever tabstop is set to), the line can be
> wrapped, etc.
Why do you need it?

You can use
echo ((col('.')>1)?(strchars(getline(line('.'))[:col('.')-2])):(0))
or (if you need to count composing characters as a part of previous character)
replace `strchars(...)' with `len(split(..., '\v.@='))'.

Note that you solution does not work: you forgot that one character can occupy
two display cells if it either fullwidth (U+FF00..U+FF60, U+FFE0..U+FFE6,
U+3000) or with ambigious width and &ambiwidth is set.

> This could be a reasonably compatible solution:
>
> function! Column()
> let l:ot=&tabstop
> let l:ow=&wrap
> set tabstop=1
> set nowrap
>
> let l:ret = virtcol(".")
>
> let &tabstop=l:ot
> let &wrap=l:ow
>
> return l:ret
> endfunction
Original message:
> I'm looking for a way to get the current cursor position in the line
> (that is, the column) in *characters*.
> col is no use, because it returns the position in bytes. I can't use
> virtcol either because it returns the position on the screen: a tab
> counts as 8 characters (or whatever tabstop is set to), the line can be
> wrapped, etc.
>
> This could be a reasonably compatible solution:
>
> function! Column()
> let l:ot=&tabstop
> let l:ow=&wrap
> set tabstop=1
> set nowrap
>
> let l:ret = virtcol(".")
>
> let &tabstop=l:ot
> let &wrap=l:ow
>
> return l:ret
> endfunction
>
> But it looks sloppy and bogus. There's got to be a better way.
>
> Any suggestions or tips?
>
> Thanks!

No comments:

Post a Comment