Thursday, September 27, 2012

Re: character in statusline

On 26/09/12 21:34, Bee wrote:
> character in statusline
>
> I have added both decimal and hex representations
> of the char under the cursor to my statusline as follows:
> set statusline+=%b\ " decimal byte '98'
> set statusline+=x%02B " hex byte 'x62'
>
> I do NOT need this, but was curious how to show the char itself.
> The following does NOT work:
> set statusline+=\"%{nr2char(%b)}\" " char as "c"
>
> For testing I used a static value "c"=99 and it works.
> set statusline+=\"%{nr2char(99)}\" " char as "c"
>
> How to get %b to evaluate?
> Or is there a simpler way?
>
> Bill
>

%b should work if it is just part of your statusline, and _not_ inside a
%{ … } "Vim expression". Please read along…

My own statusline is as follows (4 lines beginning with ", if,
(indented)set, and endif):


" custom status line, see :help 'statusline' for details
if has("statusline")
set statusline=%<%f\ %h%m%r%=%k[%{(&fenc\ ==\
\"\"?&enc:&fenc).(&bomb?\",BOM\":\"\")}][U+%04B]\ %-12.(%l,%c%V%)\ %P
endif


where [U+%04B] displays the byte at the cursor in "Unicode hex" i.e.
[U+0000] to [U+FFFF] in the BMP, and [U+10000] to [U+10FFFF] above it
for the planes currently part of the Unicode Standard including the
noncharacters and illegal codepoints in them, or up to [U+7FFFFFFF] for
the earliest version of ISO-10646 UCS-4, which is still supported by Vim.

Of course, when the cursor is not on a character (e.g. in Insert mode
when appending at the end of the current line) there is no "character
value" to display, and in that case I get [U+0000]


To get the character at the cursor in alphanumeric, you could try (untested)

%{substitute(getline('.'), '^.*\(\%' . col('.') . 'c.\).*$', '\1', "")}

This roundabout way is meant to extract one "character" (which, in a
multi-byte 'encoding' like UTF-8, could be several bytes) at the current
"byte" position, because getline() returns a string of bytes. So we
substitute the full line by just the one "interesting" character.

You should test this, I'm not confident that it isn't off by one.


Best regards,
Tony.
--
Many years ago in a period commonly known as Next Friday Afternoon,
there lived a King who was very Gloomy on Tuesday mornings because he
was so Sad thinking about how Unhappy he had been on Monday and how
completely Mournful he would be on Wednesday ...
-- Walt Kelly

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