2014-01-13 18:48:26 -05:00
|
|
|
"=============================================================================
|
|
|
|
" FILE: autoload/EasyMotion/command_line.vim
|
|
|
|
" AUTHOR: haya14busa
|
2014-01-19 11:28:48 -05:00
|
|
|
" Reference: https://github.com/osyo-manga/vim-over
|
2014-01-23 11:03:48 -05:00
|
|
|
" Last Change: 24 Jan 2014.
|
2014-01-13 18:48:26 -05:00
|
|
|
" 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) "{{{
|
2014-01-17 03:39:26 -05:00
|
|
|
redraw
|
|
|
|
echohl Question | echon a:message | echohl None
|
|
|
|
echon a:input
|
2014-01-13 18:48:26 -05:00
|
|
|
endfunction "}}}
|
|
|
|
function! s:Cancell() " {{{
|
2014-01-23 11:06:21 -05:00
|
|
|
call s:after_input()
|
2014-01-23 08:53:58 -05:00
|
|
|
keepjumps call setpos('.', s:save_orig_pos)
|
2014-01-13 18:48:26 -05:00
|
|
|
redraw
|
2014-01-17 03:39:26 -05:00
|
|
|
echo 'EasyMotion: Cancelled'
|
2014-01-13 18:48:26 -05:00
|
|
|
return ''
|
|
|
|
endfunction " }}}
|
|
|
|
|
2014-01-19 11:28:48 -05:00
|
|
|
function! s:getPromptMessage(num_strokes) "{{{
|
2014-01-17 01:12:33 -05:00
|
|
|
if a:num_strokes == 1
|
|
|
|
let prompt = substitute(
|
|
|
|
\ substitute(g:EasyMotion_prompt,'{n}', a:num_strokes, 'g'),
|
|
|
|
\ '(s)', '', 'g')
|
|
|
|
elseif a:num_strokes == -1
|
|
|
|
let prompt = substitute(
|
|
|
|
\ substitute(g:EasyMotion_prompt, '{n}\s\{0,1}', '', 'g'),
|
|
|
|
\ '(s)', 's', 'g')
|
|
|
|
else
|
|
|
|
let prompt = substitute(
|
|
|
|
\ substitute(g:EasyMotion_prompt,'{n}', a:num_strokes, 'g'),
|
|
|
|
\ '(s)', 's', 'g')
|
|
|
|
endif
|
|
|
|
return prompt
|
2014-01-19 11:28:48 -05:00
|
|
|
endfunction "}}}
|
|
|
|
function! s:before_input(num_strokes) "{{{
|
|
|
|
if a:num_strokes == -1 && g:EasyMotion_inc_highlight
|
|
|
|
call EasyMotion#highlight#delete_highlight()
|
2014-01-23 11:03:48 -05:00
|
|
|
let shade_hl_re = '\_.*'
|
2014-01-19 11:28:48 -05:00
|
|
|
call EasyMotion#highlight#add_highlight(shade_hl_re, g:EasyMotion_hl_group_shade)
|
2014-01-23 11:03:48 -05:00
|
|
|
call EasyMotion#highlight#add_highlight('\%#', g:EasyMotion_hl_inc_cursor)
|
2014-01-19 11:28:48 -05:00
|
|
|
endif
|
|
|
|
endfunction "}}}
|
2014-01-23 11:06:21 -05:00
|
|
|
function! s:after_input() "{{{
|
2014-01-19 11:28:48 -05:00
|
|
|
call EasyMotion#highlight#delete_highlight()
|
|
|
|
endfunction "}}}
|
|
|
|
function! s:should_use_smartcase(input) "{{{
|
2014-01-22 21:47:31 -05:00
|
|
|
" TODO:
|
2014-01-19 11:28:48 -05:00
|
|
|
if g:EasyMotion_smartcase == 0
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
" return 1 if input didn't match upporcase letter
|
|
|
|
return match(a:input, '\u') == -1
|
|
|
|
endfunction "}}}
|
2014-01-22 21:47:31 -05:00
|
|
|
function! s:offscreen_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])
|
|
|
|
if pos != [0, 0]
|
|
|
|
" Restore cursor posision
|
|
|
|
keepjumps call setpos('.', s:orig_pos)
|
|
|
|
else
|
|
|
|
" Second: if there were no much, search off screen
|
|
|
|
silent! let pos = searchpos(a:re, s:direction)
|
|
|
|
if pos != [0, 0]
|
|
|
|
" Match
|
|
|
|
keepjumps call setpos('.', pos)
|
|
|
|
" Move cursor
|
2014-01-23 11:06:21 -05:00
|
|
|
if s:save_direction != 'b'
|
2014-01-22 21:47:31 -05:00
|
|
|
normal! zzH0
|
|
|
|
else
|
|
|
|
normal! zzL0
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
" No much
|
|
|
|
call s:adjust_screen()
|
|
|
|
keepjumps call setpos('.', s:orig_pos)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction "}}}
|
|
|
|
function! s:adjust_screen() "{{{
|
2014-01-23 11:06:21 -05:00
|
|
|
if s:save_direction != 'b'
|
2014-01-22 21:47:31 -05:00
|
|
|
" Forward
|
|
|
|
keepjumps call setpos('.', s:orig_line_start)
|
|
|
|
normal! zt
|
|
|
|
else
|
|
|
|
" Backward
|
|
|
|
keepjumps call setpos('.', s:orig_line_end)
|
|
|
|
normal! zb
|
|
|
|
endif
|
|
|
|
endfunction "}}}
|
2014-01-23 11:06:21 -05:00
|
|
|
function! s:search_histories() "{{{
|
|
|
|
return map(range(&history), 'histget("search", v:val * -1)')
|
|
|
|
endfunction "}}}
|
2014-01-22 21:47:31 -05:00
|
|
|
|
|
|
|
function! EasyMotion#command_line#GetInput(num_strokes, prev, direction) "{{{
|
|
|
|
let previous_input = a:prev
|
2014-01-23 11:06:21 -05:00
|
|
|
let s:save_direction = a:direction == 1 ? 'b' : ''
|
|
|
|
let s:direction = s:save_direction
|
2014-01-17 03:39:26 -05:00
|
|
|
let input = ''
|
|
|
|
let prompt = s:getPromptMessage(a:num_strokes)
|
2014-01-17 01:12:33 -05:00
|
|
|
|
2014-01-22 21:47:31 -05:00
|
|
|
let s:orig_pos = getpos('.')
|
|
|
|
let s:orig_line_start = getpos('w0')
|
|
|
|
let s:orig_line_end = getpos('w$')
|
|
|
|
|
2014-01-23 08:53:58 -05:00
|
|
|
let s:save_orig_pos = deepcopy(s:orig_pos)
|
|
|
|
|
2014-01-19 11:28:48 -05:00
|
|
|
call s:before_input(a:num_strokes)
|
|
|
|
|
2014-01-23 11:06:21 -05:00
|
|
|
" let s:search_hist = s:search_histories()
|
|
|
|
let s:search_hist = []
|
|
|
|
let s:search_cnt = 0
|
|
|
|
|
2014-01-17 03:39:26 -05:00
|
|
|
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
|
2014-01-19 11:28:48 -05:00
|
|
|
call s:Cancell() | let input = '' | break
|
2014-01-23 11:06:21 -05:00
|
|
|
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
|
2014-01-17 03:39:26 -05:00
|
|
|
elseif EasyMotion#command_line#is_input("\<C-c>")
|
2014-01-13 18:48:26 -05:00
|
|
|
" Cancel
|
2014-01-19 11:28:48 -05:00
|
|
|
call s:Cancell() | let input = '' | break
|
2014-01-17 03:39:26 -05:00
|
|
|
elseif EasyMotion#command_line#is_input("\<C-h>")
|
2014-01-13 18:48:26 -05:00
|
|
|
" Delete one character
|
2014-01-19 11:28:48 -05:00
|
|
|
if len(input) == 0
|
|
|
|
call s:Cancell() | let input = '' | break
|
|
|
|
endif
|
2014-01-17 03:39:26 -05:00
|
|
|
let input = substitute(input, '.$', '', '')
|
|
|
|
elseif EasyMotion#command_line#is_input("\<C-d>")
|
2014-01-13 18:48:26 -05:00
|
|
|
" Delete one character
|
2014-01-19 11:28:48 -05:00
|
|
|
if len(input) == 0
|
|
|
|
call s:Cancell() | let input = '' | break
|
|
|
|
endif
|
2014-01-17 03:39:26 -05:00
|
|
|
let input = substitute(input, '.$', '', '')
|
|
|
|
elseif EasyMotion#command_line#is_input("\<C-u>")
|
2014-01-13 18:48:26 -05:00
|
|
|
" Delete all
|
2014-01-19 11:28:48 -05:00
|
|
|
if len(input) == 0
|
|
|
|
call s:Cancell() | let input = '' | break
|
|
|
|
endif
|
2014-01-17 03:39:26 -05:00
|
|
|
let input = ''
|
|
|
|
elseif EasyMotion#command_line#is_input("\<C-w>")
|
2014-01-13 18:48:26 -05:00
|
|
|
" Delete word
|
2014-01-17 03:39:26 -05:00
|
|
|
let input = matchstr(input, '^\zs.\{-}\ze\(\(\w*\)\|\(.\)\)$')
|
2014-01-23 11:06:21 -05:00
|
|
|
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')
|
2014-01-17 03:39:26 -05:00
|
|
|
endif
|
2014-01-23 11:06:21 -05:00
|
|
|
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)
|
2014-01-23 08:53:58 -05:00
|
|
|
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$')
|
2014-01-23 11:06:21 -05:00
|
|
|
let s:direction = ''
|
2014-01-23 08:53:58 -05:00
|
|
|
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$')
|
2014-01-23 11:06:21 -05:00
|
|
|
let s:direction = 'b'
|
2014-01-23 08:53:58 -05:00
|
|
|
elseif EasyMotion#command_line#is_input("\<C-o>")
|
2014-01-23 11:06:21 -05:00
|
|
|
keepjumps call setpos('.', s:save_orig_pos)
|
2014-01-23 08:53:58 -05:00
|
|
|
let s:orig_pos = s:save_orig_pos
|
|
|
|
let s:orig_line_start = getpos('w0')
|
|
|
|
let s:orig_line_end = getpos('w$')
|
2014-01-24 03:04:30 -05:00
|
|
|
let s:direction = s:save_direction
|
2014-01-23 08:53:58 -05:00
|
|
|
elseif EasyMotion#command_line#is_input("\<C-z>")
|
|
|
|
normal! zR
|
2014-01-17 03:39:26 -05:00
|
|
|
elseif char2nr(s:char) == 128 || char2nr(s:char) < 27
|
2014-01-13 18:48:26 -05:00
|
|
|
" Do nothing for special key
|
2014-01-17 03:39:26 -05:00
|
|
|
continue
|
|
|
|
else
|
|
|
|
let input .= s:char
|
|
|
|
endif
|
2014-01-22 21:47:31 -05:00
|
|
|
|
|
|
|
" Incremental routine {{{
|
|
|
|
if a:num_strokes == -1
|
|
|
|
let re = input
|
|
|
|
let case_flag = s:should_use_smartcase(input) ? '\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 "}}}
|
2014-01-19 11:28:48 -05:00
|
|
|
endif
|
2014-01-22 21:47:31 -05:00
|
|
|
"}}}
|
2014-01-17 03:39:26 -05:00
|
|
|
endwhile
|
2014-01-23 11:06:21 -05:00
|
|
|
call s:after_input()
|
2014-01-17 03:39:26 -05:00
|
|
|
return input
|
2014-01-13 18:48:26 -05:00
|
|
|
endfunction "}}}
|
|
|
|
function! EasyMotion#command_line#char() "{{{
|
2014-01-17 03:39:26 -05:00
|
|
|
return s:char
|
2014-01-13 18:48:26 -05:00
|
|
|
endfunction "}}}
|
|
|
|
function! EasyMotion#command_line#is_input(key) "{{{
|
2014-01-17 03:39:26 -05:00
|
|
|
return EasyMotion#command_line#keymap(EasyMotion#command_line#char()) == a:key
|
2014-01-13 18:48:26 -05:00
|
|
|
endfunction "}}}
|
|
|
|
function! EasyMotion#command_line#keymap(key) "{{{
|
2014-01-17 03:39:26 -05:00
|
|
|
return get(extend(deepcopy(s:default_key_mapping), g:EasyMotion_command_line_key_mappings), a:key, a:key)
|
2014-01-13 18:48:26 -05:00
|
|
|
endfunction "}}}
|
|
|
|
" Default_key_mapping: {{{
|
|
|
|
let s:default_key_mapping = {
|
2014-01-17 03:39:26 -05:00
|
|
|
\ "\<Right>" : "\<C-f>",
|
|
|
|
\ "\<Left>" : "\<C-b>",
|
|
|
|
\ "\<Up>" : "\<C-p>",
|
|
|
|
\ "\<Down>" : "\<C-n>",
|
|
|
|
\ "\<BS>" : "\<C-h>",
|
|
|
|
\ "\<Del>" : "\<C-d>",
|
|
|
|
\ "\<Home>" : "\<C-a>",
|
|
|
|
\ "\<End>" : "\<C-e>",
|
2014-01-13 18:48:26 -05:00
|
|
|
\}
|
|
|
|
"}}}
|
2014-01-19 11:28:48 -05:00
|
|
|
|
2014-01-13 18:48:26 -05:00
|
|
|
" Restore 'cpoptions' {{{
|
|
|
|
let &cpo = s:save_cpo
|
|
|
|
unlet s:save_cpo
|
|
|
|
" }}}
|
2014-01-17 03:39:26 -05:00
|
|
|
" vim: fdm=marker:et:ts=4:sw=4:sts=4
|