Created
June 18, 2010 18:30
Get one character under cursor; with also multibyte character.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" For test (Do quickrun on the target character!): | |
" aiu | |
" あいう | |
function! s:char2bits(char) | |
let bits = map(range(8), '0') | |
let cur_keta = len(bits) - 1 | |
let nr = char2nr(a:char) | |
while cur_keta >= 0 | |
let cur_num = float2nr(pow(2, cur_keta)) | |
if nr >= cur_num | |
let nr -= cur_num | |
let bits[cur_keta] = 1 | |
endif | |
let cur_keta -= 1 | |
endwhile | |
return reverse(bits) | |
endfunction | |
function! s:is_trail_char(char) | |
let bits = s:char2bits(a:char) | |
return bits[0] && !bits[1] | |
endfunction | |
function! s:get_current_and_next_index(str, cur_idx) | |
let cur_idx = a:cur_idx | |
let str = a:str | |
let end_idx = cur_idx + 1 | |
while s:is_trail_char(str[end_idx]) | |
let end_idx += 1 | |
endwhile | |
" NOTE: `cur_idx` is always first index. | |
return [cur_idx, end_idx] | |
endfunction | |
function! s:get_current_character() | |
if mode() ==# 'c' | |
let str = getcmdline() | |
let cur_idx = getcmdpos() - 1 | |
elseif mode() ==# 'n' | |
let str = getline('.') | |
let cur_idx = col('.') - 1 | |
else | |
echoerr 'not supported' | |
endif | |
let [begin, end] = s:get_current_and_next_index(str, cur_idx) | |
return strpart(str, begin, end - begin) | |
endfunction | |
echom s:get_current_character() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment