Skip to content

Instantly share code, notes, and snippets.

@tyru
Created June 18, 2010 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tyru/444017 to your computer and use it in GitHub Desktop.
Save tyru/444017 to your computer and use it in GitHub Desktop.
Get one character under cursor; with also multibyte character.
" 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