On 14/04/09 21:11, Y. Hida wrote:
>
> Hi,
>
> Is there a way figure out how wide a string containing double-width
> characters are when displayed? I'm trying to right-justify some strings
> in foldtext.
>
> -Yozo
Hm, you could try playing with virtcol() at both ends of the string (and
beware of off-by-one errors), but beware that there are several kinds of
characters by virtual width:
- "ordinary" (narrow) characters: 1 column
- "wide" characters: 2 columns
- "control" characters in the range 0-0x1F: usually 2 columns
- tabs: between 1 and 'tabstop' columns, unless 'list' is set and
'listchars' does not include a "tab:" section, when it is 2 columns
- non-printable and invalid Unicode codepoints: between 4 and 10 columns
depending on value
etc.
This is, assuming that the string is displayed in a buffer.
There are so many possible "wide" characters out there, I don't think
it's possible to construct a regex to catch them all (IIRC, a range in a
regexp can be no more than 257 values), but maybe you know what kinds of
"narrow" characters there can be? Then (assuming no hard tabs, no
control characters, etc.) you can, let's say, replace every narrow
character by a dash, and in a second pass, every nondash by two dashes.
This gives you a line of dashes of the same "virtual length" as your
string. Maybe something like
let l = strlen(substitute(substitute(string,
\ "[\x20-\x7F\xA0-\xFF]","-","g"),
\ "[^-]","--","g"))
The above assumes that the string contains only printable Latin1
characters and "wide" characters. For a different set of "narrow"
characters, vary accordingly.
Best regards,
Tony.
--
God is Dead
-- Nietzsche
Nietzsche is Dead
-- God
Nietzsche is God
-- The Dead
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---