Improve command_line and move helper function

This commit is contained in:
haya14busa 2014-01-14 08:48:26 +09:00
parent 7ab69a7dc2
commit 512ecf851a
4 changed files with 176 additions and 88 deletions

View File

@ -49,7 +49,7 @@ endfunction "}}}
" == Motion functions {{{
" -- Find Motion -------------------------
function! EasyMotion#S(num_strokes, visualmode, direction) " {{{
let input = s:GetInput(a:num_strokes)
let input = EasyMotion#command_line#GetInput(a:num_strokes)
" Check that we have an input char
if empty(input)
@ -65,7 +65,7 @@ function! EasyMotion#S(num_strokes, visualmode, direction) " {{{
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', mode(1))
endfunction " }}}
function! EasyMotion#T(num_strokes, visualmode, direction) " {{{
let input = s:GetInput(a:num_strokes)
let input = EasyMotion#command_line#GetInput(a:num_strokes)
" Check that we have an input char
if empty(input)
@ -120,7 +120,7 @@ function! EasyMotion#JumpToAnywhere(visualmode, direction) " {{{
endfunction " }}}
" -- Line Motion -------------------------
function! EasyMotion#SL(num_strokes, visualmode, direction) " {{{
let input = s:GetInput(a:num_strokes)
let input = EasyMotion#command_line#GetInput(a:num_strokes)
" Check that we have an input char
if empty(input)
@ -137,7 +137,7 @@ function! EasyMotion#SL(num_strokes, visualmode, direction) " {{{
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', mode(1))
endfunction " }}}
function! EasyMotion#TL(num_strokes, visualmode, direction) " {{{
let input = s:GetInput(a:num_strokes)
let input = EasyMotion#command_line#GetInput(a:num_strokes)
" Check that we have an input char
if empty(input)
@ -409,39 +409,6 @@ function! s:GetChar() " {{{
return nr2char(char)
endfunction " }}}
function! s:InputPrompt(message, input) "{{{
redraw
echohl Question | echon a:message | echohl None
echon a:input
endfunction "}}}
function! s:GetInput(num_strokes) "{{{
let input = ''
" repeat a:num_strokes times
while s:strchars(input) < a:num_strokes
" if a:num_strokes > 1 && g:EasyMotion_show_prompt
if g:EasyMotion_show_prompt
call s:InputPrompt(g:EasyMotion_prompt, input)
endif
let c = getchar()
let char = type(c) == type(0) ? nr2char(c) : c
if char ==# "\<Esc>" || char2nr(char) == 128
" cancel if escape or special character is input
" Escape key pressed
redraw
call s:Message('Cancelled')
return ''
elseif char ==# "\<C-h>"
let input = substitute(input, '.$', '', '')
continue
elseif char ==# "\<CR>"
" Return input charcters
" Enter key pressed
return input
endif
let input .= char
endwhile
return input
endfunction "}}}
function! s:GetSearchChar2(visualmode) " {{{
let chars = []
@ -505,7 +472,7 @@ endfunction "}}}
function! s:convertMigemo(re) "{{{
let re = a:re
if ! has_key(s:migemo_dicts, &l:encoding)
let s:migemo_dicts[&l:encoding] = s:load_migemo_dict()
let s:migemo_dicts[&l:encoding] = EasyMotion#helper#load_migemo_dict()
endif
if re =~# '^\a$'
let re = s:migemo_dicts[&l:encoding][re]
@ -549,55 +516,6 @@ function! s:load_smart_dict() "{{{
endif
endfunction "}}}
" Migemo {{{
function! s:should_use_migemo(char) "{{{
if ! g:EasyMotion_use_migemo || a:char !~# '^\a$'
return 0
endif
" TODO: use direction and support within line
let first_line = line('w0')
let end_line = line('w$')
for line in range(first_line, end_line)
if s:is_folded(line)
continue
endif
if s:include_multibyte_char(getline(line)) == 1
return 1
endif
endfor
return 0
endfunction "}}}
function! s:load_migemo_dict() "{{{
let enc = &l:encoding
if enc ==# 'utf-8'
return EasyMotion#migemo#utf8#load_dict()
elseif enc ==# 'cp932'
return EasyMotion#migemo#cp932#load_dict()
elseif enc ==# 'euc-jp'
return EasyMotion#migemo#eucjp#load_dict()
else
let g:EasyMotion_use_migemo = 0
throw "Error: ".enc." is not supported. Migemo is made disabled."
endif
endfunction "}}}
" s:strchars() {{{
if exists('*strchars')
function! s:strchars(str)
return strchars(a:str)
endfunction
else
function! s:strchars(str)
return strlen(substitute(str, ".", "x", "g"))
endfunction
endif "}}}
function! s:include_multibyte_char(str) "{{{
return strlen(a:str) != s:strchars(a:str)
endfunction "}}}
"}}}
" -- Handle Visual Mode ------------------
function! s:GetVisualStartPosition(c_pos, v_start, v_end, search_direction) "{{{
@ -644,6 +562,27 @@ function! s:GetVisualStartPosition(c_pos, v_start, v_end, search_direction) "{{{
endif
endfunction "}}}
" -- Others ------------------------------
function! s:should_use_migemo(char) "{{{
if ! g:EasyMotion_use_migemo || a:char !~# '^\a$'
return 0
endif
" TODO: use direction and support within line
let first_line = line('w0')
let end_line = line('w$')
for line in range(first_line, end_line)
if s:is_folded(line)
continue
endif
if EasyMotion#helper#include_multibyte_char(getline(line)) == 1
return 1
endif
endfor
return 0
endfunction "}}}
function! s:is_folded(line) "{{{
" Return false if g:EasyMotion_skipfoldedline == 1
" and line is start of folded lines

View File

@ -0,0 +1,116 @@
"=============================================================================
" FILE: autoload/EasyMotion/command_line.vim
" AUTHOR: haya14busa
" Last Change: 14 Jan 2014.
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
" "Software"), to deal in the Software without restriction, including
" without limitation the rights to use, copy, modify, merge, publish,
" distribute, sublicense, and/or sell copies of the Software, and to
" permit persons to whom the Software is furnished to do so, subject to
" the following conditions:
"
" The above copyright notice and this permission notice shall be included
" in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
"=============================================================================
scriptencoding utf-8
" Saving 'cpoptions' {{{
let s:save_cpo = &cpo
set cpo&vim
" }}}
function! s:InputPrompt(message, input) "{{{
redraw
echohl Question | echon a:message | echohl None
echon a:input
endfunction "}}}
function! s:Cancell() " {{{
redraw
echo 'EasyMotion: Cancelled'
return ''
endfunction " }}}
function! EasyMotion#command_line#GetInput(num_strokes) "{{{
let input = ''
" repeat a:num_strokes times
let prompt_num = a:num_strokes < 50 ? a:num_strokes : ''
let prompt = prompt_num . g:EasyMotion_prompt
while EasyMotion#helper#strchars(input) < a:num_strokes
if g:EasyMotion_show_prompt
call s:InputPrompt(prompt, input)
endif
let c = getchar()
let s:char = type(c) == type(0) ? nr2char(c) : c
if EasyMotion#command_line#is_input("\<Esc>")
" Cancel if Escape key pressed
call s:Cancell() | return ''
elseif EasyMotion#command_line#is_input("\<C-c>")
" Cancel
call s:Cancell() | return ''
elseif EasyMotion#command_line#is_input("\<C-h>")
" Delete one character
if len(input) == 0 | call s:Cancell() | return '' | endif
let input = substitute(input, '.$', '', '')
elseif EasyMotion#command_line#is_input("\<C-d>")
" Delete one character
if len(input) == 0 | call s:Cancell() | return '' | endif
let input = substitute(input, '.$', '', '')
elseif EasyMotion#command_line#is_input("\<C-u>")
" Delete all
if len(input) == 0 | call s:Cancell() | return '' | endif
let input = ''
elseif EasyMotion#command_line#is_input("\<C-w>")
" Delete word
let input = matchstr(input, '^\zs.\{-}\ze\(\(\w*\)\|\(.\)\)$')
elseif EasyMotion#command_line#is_input("\<CR>")
" Return input charcters
return input
elseif EasyMotion#command_line#is_input("\<C-j>")
" Return input charcters
return input
elseif char2nr(s:char) == 128 || char2nr(s:char) < 27
" Do nothing for special key
continue
else
let input .= s:char
endif
endwhile
return input
endfunction "}}}
function! EasyMotion#command_line#char() "{{{
return s:char
endfunction "}}}
function! EasyMotion#command_line#is_input(key) "{{{
return EasyMotion#command_line#keymap(EasyMotion#command_line#char()) == a:key
endfunction "}}}
function! EasyMotion#command_line#keymap(key) "{{{
return get(extend(deepcopy(s:default_key_mapping), g:EasyMotion_command_line_key_mappings), a:key, a:key)
endfunction "}}}
" Default_key_mapping: {{{
let s:default_key_mapping = {
\ "\<Right>" : "\<C-f>",
\ "\<Left>" : "\<C-b>",
\ "\<Up>" : "\<C-p>",
\ "\<Down>" : "\<C-n>",
\ "\<BS>" : "\<C-h>",
\ "\<Del>" : "\<C-d>",
\ "\<Home>" : "\<C-a>",
\ "\<End>" : "\<C-e>",
\}
"}}}
"
" Restore 'cpoptions' {{{
let &cpo = s:save_cpo
unlet s:save_cpo
" }}}

View File

@ -1,7 +1,7 @@
"=============================================================================
" FILE: autoload/EasyMotion/helper.vim
" AUTHOR: haya14busa
" Last Change: 13 Jan 2014.
" Last Change: 14 Jan 2014.
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
@ -55,6 +55,36 @@ function! EasyMotion#helper#is_greater_coords(coords1, coords2) "{{{
endif
endfunction "}}}
" Migemo {{{
function! EasyMotion#helper#load_migemo_dict() "{{{
let enc = &l:encoding
if enc ==# 'utf-8'
return EasyMotion#migemo#utf8#load_dict()
elseif enc ==# 'cp932'
return EasyMotion#migemo#cp932#load_dict()
elseif enc ==# 'euc-jp'
return EasyMotion#migemo#eucjp#load_dict()
else
let g:EasyMotion_use_migemo = 0
throw "Error: ".enc." is not supported. Migemo is made disabled."
endif
endfunction "}}}
" EasyMotion#helper#strchars() {{{
if exists('*strchars')
function! EasyMotion#helper#strchars(str)
return strchars(a:str)
endfunction
else
function! EasyMotion#helper#strchars(str)
return strlen(substitute(str, ".", "x", "g"))
endfunction
endif "}}}
function! EasyMotion#helper#include_multibyte_char(str) "{{{
return strlen(a:str) != EasyMotion#helper#strchars(a:str)
endfunction "}}}
"}}}
" Restore 'cpoptions' {{{
let &cpo = s:save_cpo
unlet s:save_cpo

View File

@ -33,6 +33,9 @@ let g:EasyMotion_use_upper = get(g: , 'EasyMotion_use_upper' ,
let g:EasyMotion_enter_jump_first = get(g: , 'EasyMotion_enter_jump_first' , 0)
let g:EasyMotion_show_prompt = get(g: , 'EasyMotion_show_prompt' , 1)
let g:EasyMotion_prompt = get(g: , 'EasyMotion_prompt' , '> ')
let g:EasyMotion_command_line_key_mappings =
\ get(g: , 'EasyMotion_command_line_key_mappings' , {})
"}}}
" -- Default highlighting ---------------- {{{