Implement cursor move to n key motion command line

Heavily using code from vim-over
Author : osyo-manga
URL    : https://github.com/osyo-manga/vim-over
This commit is contained in:
haya14busa 2014-01-26 18:15:26 +09:00
parent 2f9147321b
commit f26a4597db

View File

@ -30,6 +30,399 @@ let s:save_cpo = &cpo
set cpo&vim
" }}}
" Autocmd: {{{
augroup plugin-easymotion-dummy
autocmd!
augroup END
let s:cache_command = {}
function! s:doautocmd_user(command) "{{{
if !has_key(s:cache_command, a:command)
execute "autocmd plugin-easymotion-dummy"
\ . " User " . a:command." silent! execute ''"
if v:version > 703 || v:version == 703 && has("patch438")
" :helpgrep 7.4.438
let s:cache_command[a:command] = "doautocmd <nomodeline> User " . a:command
else
let s:cache_command[a:command] = "doautocmd User " . a:command
endif
endif
execute s:cache_command[a:command]
endfunction "}}}
augroup easymotion-cmdline
autocmd!
autocmd User EasyMotionCmdLineEnter call s:init()
autocmd User EasyMotionCmdLineLeave call s:finish()
augroup END
"}}}
" Activate:
function! s:init() "{{{
" Cursor
let hl_cursor = EasyMotion#command_line#hl_cursor_off()
if !hlexists("EasyMotionCommandLineCursor")
execute "highlight EasyMotionCommandLineCursor " . hl_cursor . " term=underline gui=underline"
endif
" Save cursor visible
let s:old_t_ve = &t_ve
set t_ve=
endfunction "}}}
function! s:finish() "{{{
" Cursor
call EasyMotion#command_line#hl_cursor_on()
let &t_ve = s:old_t_ve
endfunction "}}}
function! EasyMotion#command_line#hl_cursor_off() "{{{
if exists("s:old_hi_cursor")
return s:old_hi_cursor
endif
let s:old_hi_cursor = 'cterm=reverse'
if hlexists('Cursor')
redir => cursor
silent highlight Cursor
redir END
let hl = substitute(matchstr(cursor, 'xxx \zs.*'), '[ \t\n]\+\|cleared', ' ', 'g')
if mode(1) == 'ce'
" TODO: this section exists only for vim-vspec test...
let hl = substitute(hl, '\sLast\sset\sfrom.*', '', '')
endif
if !empty(substitute(hl, '\s', '', 'g'))
let s:old_hi_cursor = hl
endif
highlight Cursor NONE
endif
return s:old_hi_cursor
endfunction "}}}
function! EasyMotion#command_line#hl_cursor_on() "{{{
if exists('s:old_hi_cursor')
silent execute 'highlight Cursor ' . s:old_hi_cursor
unlet s:old_hi_cursor
endif
endfunction "}}}
function! s:before_input(num_strokes) "{{{
if a:num_strokes == -1 && g:EasyMotion_inc_highlight
call EasyMotion#highlight#delete_highlight()
let shade_hl_re = '\_.*'
call EasyMotion#highlight#add_highlight(shade_hl_re, g:EasyMotion_hl_group_shade)
call EasyMotion#highlight#add_highlight('\%#', g:EasyMotion_hl_inc_cursor)
endif
endfunction "}}}
function! s:after_input() "{{{
call EasyMotion#highlight#delete_highlight()
endfunction "}}}
" Main:
function! EasyMotion#command_line#GetInput(num_strokes, prev, direction) "{{{
call s:doautocmd_user("EasyMotionCmdLineEnter")
let previous_input = a:prev
let s:save_direction = a:direction == 1 ? 'b' : ''
let s:direction = s:save_direction
" CommandLine: Input string and cursor position
let s:command_line = s:string_with_pos('')
let prompt = s:getPromptMessage(a:num_strokes)
" Screen: cursor position, first and last line
let s:orig_pos = getpos('.')
let s:orig_line_start = getpos('w0')
let s:orig_line_end = getpos('w$')
let s:save_orig_pos = deepcopy(s:orig_pos)
call s:before_input(a:num_strokes)
let s:search_hist = []
let s:search_cnt = 0
try
while s:command_line.length() < a:num_strokes ||
\ a:num_strokes == -1
if g:EasyMotion_show_prompt
call s:echo_cmdline(prompt, s:command_line)
endif
let c = getchar()
let s:char = type(c) == type(0) ? nr2char(c) : c
if EasyMotion#command_line#is_input("\<Esc>") ||
\ EasyMotion#command_line#is_input("\<C-c>")
" Cancel if Escape key pressed
call s:Cancell()
call s:command_line.set('')
break
elseif EasyMotion#command_line#is_input("\<CR>")
if s:command_line.length() == 0
call s:command_line.set(previous_input)
break
endif
break
elseif EasyMotion#command_line#is_input("\<C-j>")
break
elseif EasyMotion#command_line#is_input("\<C-h>")
" Delete one character
if s:command_line.length() == 0
call s:Cancell() | break
endif
call s:command_line.remove_prev()
elseif EasyMotion#command_line#is_input("\<C-d>")
" Delete one character
if s:command_line.length() == 0
call s:Cancell() | break
endif
call s:command_line.remove_pos()
elseif EasyMotion#command_line#is_input("\<C-u>")
" Delete all
if s:command_line.length() == 0
call s:Cancell() | break
endif
call s:command_line.set(s:command_line.pos_word() .
\ s:command_line.forward()) "string
call s:command_line.set(0) " cursor
elseif EasyMotion#command_line#is_input("\<C-w>")
" Delete word
let backward = matchstr(s:command_line.backward(),
\ '\v^\zs.\{-}\ze((\w*)|(.))$')
call s:command_line.set(backward . s:command_line.pos_word() . s:command_line.forward())
call s:command_line.set(strchars(backward))
elseif EasyMotion#command_line#is_input("\<C-p>") ||
\ EasyMotion#command_line#is_input("\<C-n>")
" History
if s:search_cnt == 0 && empty(s:search_hist)
let cmdline = '^' . EasyMotion#command_line#getline()
let s:search_hist = filter(s:search_histories(), 'v:val =~ cmdline')
endif
if EasyMotion#command_line#is_input("\<C-n>")
let s:search_cnt = max([s:search_cnt - 1, 0])
endif
if EasyMotion#command_line#is_input("\<C-p>")
let s:search_cnt = min([s:search_cnt + 1, len(s:search_hist)])
endif
call EasyMotion#command_line#setline(
\ get(s:search_hist, s:search_cnt,
\ EasyMotion#command_line#getline()))
elseif EasyMotion#command_line#is_input("\<Tab>")
call s:scroll(0)
elseif EasyMotion#command_line#is_input("\<S-Tab>")
call s:scroll(1)
elseif EasyMotion#command_line#is_input("\<C-o>")
keepjumps call setpos('.', s:save_orig_pos)
let s:orig_pos = s:save_orig_pos
let s:orig_line_start = getpos('w0')
let s:orig_line_end = getpos('w$')
let s:direction = s:save_direction
elseif EasyMotion#command_line#is_input("\<C-z>")
" TODO: better solution
normal! zR
elseif EasyMotion#command_line#is_input("\<C-f>")
call s:command_line.next()
elseif EasyMotion#command_line#is_input("\<C-b>")
call s:command_line.prev()
elseif EasyMotion#command_line#is_input("\<C-a>")
call s:command_line.set(0)
elseif EasyMotion#command_line#is_input("\<C-e>")
call s:command_line.set(s:command_line.length())
elseif char2nr(s:char) == 128 || char2nr(s:char) < 27
" Do nothing for special key
continue
else
call s:command_line.input(s:char)
endif
" Incremental routine {{{
if a:num_strokes == -1
let re = s:command_line.str()
let case_flag = EasyMotion#helper#should_use_smartcase(re) ?
\ '\c' : '\C'
let re .= case_flag
if g:EasyMotion_inc_highlight "{{{
call s:inc_highlight(re)
endif "}}}
if g:EasyMotion_off_screen_search "{{{
call s:off_screen_search(re)
endif "}}}
endif
"}}}
endwhile
finally
call s:after_input()
call s:doautocmd_user("EasyMotionCmdLineLeave")
endtry
return s:command_line.str()
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>",
\}
"}}}
" CommandLine:
function! s:echo_cmdline(prompt, pstr) "{{{
" pstr -> cursor pos & string
redraw
echohl Question | echon a:prompt | echohl NONE
echon a:pstr.backward()
echohl EasyMotionCommandLineCursor
if empty(a:pstr.pos_word())
echon ' '
else
echon a:pstr.pos_word()
endif
echohl NONE
echon a:pstr.forward()
endfunction "}}}
function! s:string_with_pos(...) "{{{
" string with cursor position
" emulate cursor using list
let default = get(a:, 1, '') " placeholder
let self = {} " OOP
" NOTE: {{{
" 'V!m!s{cursor}hment'
" self.list = ['V', '!', 'm', '!', 's', 'h', 'm', 'e', 'n', 't']
" 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
" self.col = 5 -> cursor
" self.length() = 10
" self.backward() = 'V!m!s'
" self.pos_word() = 'h'
" self.forward() = 'ment'
" self.str() = 'V!m!s{cursor}hment'
"}}}
" Set: {{{
function! self.set(item)
return type(a:item) == type('') ? self.set_str(a:item)
\ : type(a:item) == type(0) ? self.set_pos(a:item)
\ : self
endfunction
function! self.set_str(str)
" set string
let self.list = split(a:str, '\zs')
let self.col = EasyMotion#helper#strchars(a:str)
return self
endfunction
function! self.set_pos(pos)
" set cursor position
let self.col = s:clamp(a:pos, 0, self.length())
return self
endfunction
"}}}
" Get Input: {{{
function! self.str()
" to string
return join(self.list, '')
endfunction
function! self.backward()
" get string backward from cursor pos
return self.col > 0 ? join(self.list[ : self.col-1], '') : ''
endfunction
function! self.forward()
" get string forward from cursor pos
return join(self.list[self.col+1 : ], '')
endfunction
function! self.pos_word()
" get cursor position character (word)
return get(self.list, self.col, '')
endfunction
"}}}
" Input: {{{
function! self.input(str)
call extend(self.list, split(a:str, '\zs'), self.col)
let self.col += len(split(a:str, '\zs'))
return self
endfunction
"}}}
" Move Cursor: {{{
function! self.next()
return self.set_pos(self.col + 1)
endfunction
function! self.prev()
return self.set_pos(self.col - 1)
endfunction
"}}}
" Remove: {{{
function! self.remove(index)
" Remove character
if a:index < 0 || self.length() <= a:index
return self
endif
unlet self.list[a:index]
if a:index < self.col
call self.set(self.col - 1)
endif
return self
endfunction
function! self.remove_pos()
return self.remove(self.col)
endfunction
function! self.remove_prev()
return self.remove(self.col - 1)
endfunction
function! self.remove_next()
return self.remove(self.col + 1)
endfunction
"}}}
" Helper: {{{
function! self.pos()
" describe cursor position as column number
return self.col
endfunction
function! self.length()
return len(self.list)
endfunction
"}}}
call self.set(default)
return self
endfunction "}}}
function! s:clamp(value, min, max) "{{{
return min([max([a:value, a:min]), a:max])
endfunction "}}}
function! EasyMotion#command_line#getline() "{{{
return s:command_line.str()
endfunction "}}}
function! EasyMotion#command_line#setline(line) "{{{
call s:command_line.set(a:line)
endfunction "}}}
" Helper:
function! s:InputPrompt(message, input) "{{{
redraw
echohl Question | echon a:message | echohl None
@ -42,7 +435,6 @@ function! s:Cancell() " {{{
echo 'EasyMotion: Cancelled'
return ''
endfunction " }}}
function! s:getPromptMessage(num_strokes) "{{{
if a:num_strokes == 1
let prompt = substitute(
@ -59,18 +451,8 @@ function! s:getPromptMessage(num_strokes) "{{{
endif
return prompt
endfunction "}}}
function! s:before_input(num_strokes) "{{{
if a:num_strokes == -1 && g:EasyMotion_inc_highlight
call EasyMotion#highlight#delete_highlight()
let shade_hl_re = '\_.*'
call EasyMotion#highlight#add_highlight(shade_hl_re, g:EasyMotion_hl_group_shade)
call EasyMotion#highlight#add_highlight('\%#', g:EasyMotion_hl_inc_cursor)
endif
endfunction "}}}
function! s:after_input() "{{{
call EasyMotion#highlight#delete_highlight()
endfunction "}}}
function! s:offscreen_search(re) "{{{
function! s:off_screen_search(re) "{{{
" First: search within visible screen range
call s:adjust_screen()
silent! let pos = searchpos(a:re, s:direction . 'n', s:orig_line_end[1])
@ -107,150 +489,25 @@ function! s:adjust_screen() "{{{
normal! zb
endif
endfunction "}}}
function! s:search_histories() "{{{
return map(range(&history), 'histget("search", v:val * -1)')
endfunction "}}}
function! EasyMotion#command_line#GetInput(num_strokes, prev, direction) "{{{
let previous_input = a:prev
let s:save_direction = a:direction == 1 ? 'b' : ''
let s:direction = s:save_direction
let input = ''
let prompt = s:getPromptMessage(a:num_strokes)
function! s:scroll(direction) "{{{
" direction: 0 -> forward, 1 -> backward
exec a:direction == 0 ? "normal! \<C-f>" : "normal! \<C-b>"
let s:orig_pos = getpos('.')
let s:orig_line_start = getpos('w0')
let s:orig_line_end = getpos('w$')
let s:save_orig_pos = deepcopy(s:orig_pos)
call s:before_input(a:num_strokes)
" let s:search_hist = s:search_histories()
let s:search_hist = []
let s:search_cnt = 0
while EasyMotion#helper#strchars(input) < a:num_strokes ||
\ a:num_strokes == -1
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() | let input = '' | break
elseif EasyMotion#command_line#is_input("\<CR>")
if len(input) == 0
let input = previous_input | break
endif
break
elseif EasyMotion#command_line#is_input("\<C-j>")
break
elseif EasyMotion#command_line#is_input("\<C-c>")
" Cancel
call s:Cancell() | let input = '' | break
elseif EasyMotion#command_line#is_input("\<C-h>")
" Delete one character
if len(input) == 0
call s:Cancell() | let input = '' | break
endif
let input = substitute(input, '.$', '', '')
elseif EasyMotion#command_line#is_input("\<C-d>")
" Delete one character
if len(input) == 0
call s:Cancell() | let input = '' | break
endif
let input = substitute(input, '.$', '', '')
elseif EasyMotion#command_line#is_input("\<C-u>")
" Delete all
if len(input) == 0
call s:Cancell() | let input = '' | break
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("\<C-p>") || EasyMotion#command_line#is_input("\<C-n>")
if s:search_cnt == 0 && empty(s:search_hist)
let cmdline = '^' . input
let s:search_hist = filter(s:search_histories(), 'v:val =~ cmdline')
endif
if EasyMotion#command_line#is_input("\<C-n>")
let s:search_cnt = max([s:search_cnt - 1, 0])
endif
if EasyMotion#command_line#is_input("\<C-p>")
let s:search_cnt = min([s:search_cnt + 1, len(s:search_hist)])
endif
let input = get(s:search_hist, s:search_cnt, input)
elseif EasyMotion#command_line#is_input("\<Tab>")
exec "normal! \<C-f>"
let s:orig_pos = getpos('.')
let s:orig_line_start = getpos('w0')
let s:orig_line_end = getpos('w$')
let s:direction = ''
elseif EasyMotion#command_line#is_input("\<S-Tab>")
exec "normal! \<C-b>"
let s:orig_pos = getpos('.')
let s:orig_line_start = getpos('w0')
let s:orig_line_end = getpos('w$')
let s:direction = 'b'
elseif EasyMotion#command_line#is_input("\<C-o>")
keepjumps call setpos('.', s:save_orig_pos)
let s:orig_pos = s:save_orig_pos
let s:orig_line_start = getpos('w0')
let s:orig_line_end = getpos('w$')
let s:direction = s:save_direction
elseif EasyMotion#command_line#is_input("\<C-z>")
normal! zR
elseif char2nr(s:char) == 128 || char2nr(s:char) < 27
" Do nothing for special key
continue
else
let input .= s:char
endif
" Incremental routine {{{
if a:num_strokes == -1
let re = input
let case_flag = EasyMotion#helper#should_use_smartcase(re) ? '\c' : '\C'
let re .= case_flag
if g:EasyMotion_inc_highlight "{{{
call EasyMotion#highlight#delete_highlight('EasyMotionIncSearch')
if len(input) > 0
silent! call EasyMotion#highlight#add_highlight(re, g:EasyMotion_hl_inc_search)
endif
endif "}}}
if g:EasyMotion_off_screen_search "{{{
call s:offscreen_search(re)
endif "}}}
endif
"}}}
endwhile
call s:after_input()
return input
let s:direction = a:direction == 0 ? '' : 'b'
endfunction "}}}
function! EasyMotion#command_line#char() "{{{
return s:char
function! s:search_histories() "{{{
return map(range(&history), 'histget("search", v:val * -1)')
endfunction "}}}
function! EasyMotion#command_line#is_input(key) "{{{
return EasyMotion#command_line#keymap(EasyMotion#command_line#char()) == a:key
function! s:inc_highlight(re) "{{{
call EasyMotion#highlight#delete_highlight('EasyMotionIncSearch')
if s:command_line.length() > 0
silent! call EasyMotion#highlight#add_highlight(a:re, g:EasyMotion_hl_inc_search)
endif
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