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-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
|
|
|
|
" }}}
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
" CommandLine:
|
2014-02-11 23:31:44 -05:00
|
|
|
let s:V = vital#of('easymotion')
|
|
|
|
let s:cmdline = s:V.import('Over.Commandline.Base')
|
|
|
|
let s:modules = s:V.import("Over.Commandline.Modules")
|
|
|
|
let s:search = s:cmdline.make()
|
|
|
|
let s:search.highlights.prompt = 'Question'
|
2014-01-30 01:31:25 -05:00
|
|
|
|
|
|
|
" Add Module: {{{
|
2014-02-23 23:43:16 -05:00
|
|
|
call s:search.connect('Exit')
|
2014-02-11 23:31:44 -05:00
|
|
|
call s:search.connect('Cancel')
|
|
|
|
call s:search.connect('Redraw')
|
2014-02-23 23:43:16 -05:00
|
|
|
call s:search.connect('DrawCommandline')
|
2014-02-05 00:20:50 -05:00
|
|
|
call s:search.connect('Delete')
|
|
|
|
call s:search.connect('CursorMove')
|
|
|
|
call s:search.connect('Paste')
|
|
|
|
call s:search.connect('BufferComplete')
|
|
|
|
call s:search.connect('InsertRegister')
|
2014-02-23 23:43:16 -05:00
|
|
|
call s:search.connect('ExceptionExit')
|
|
|
|
call s:search.connect(s:modules.get('ExceptionMessage').make('EasyMotion: ', 'echom'))
|
2014-02-11 23:31:44 -05:00
|
|
|
call s:search.connect(s:modules.get('History').make('/'))
|
|
|
|
call s:search.connect(s:modules.get('NoInsert').make_special_chars())
|
|
|
|
call s:search.connect(s:modules.get('KeyMapping').make_emacs())
|
|
|
|
call s:search.connect(s:modules.get('Doautocmd').make('EMCommandLine'))
|
2014-01-30 01:31:25 -05:00
|
|
|
|
|
|
|
let s:module = {
|
|
|
|
\ "name" : "EasyMotion",
|
|
|
|
\}
|
|
|
|
function! s:module.on_char_pre(cmdline)
|
|
|
|
if a:cmdline.is_input("<Over>(em-scroll-f)")
|
|
|
|
call s:scroll(0)
|
|
|
|
call a:cmdline.setchar('')
|
|
|
|
elseif a:cmdline.is_input("<Over>(em-scroll-b)")
|
|
|
|
call s:scroll(1)
|
|
|
|
call a:cmdline.setchar('')
|
|
|
|
elseif a:cmdline.is_input("<Over>(em-jumpback)")
|
|
|
|
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
|
|
|
|
call a:cmdline.setchar('')
|
|
|
|
elseif a:cmdline.is_input("<Over>(em-openallfold)")
|
|
|
|
" TODO: better solution
|
|
|
|
normal! zR
|
|
|
|
call a:cmdline.setchar('')
|
2014-01-26 04:15:26 -05:00
|
|
|
endif
|
2014-01-30 01:31:25 -05:00
|
|
|
endfunction
|
|
|
|
call s:search.connect(s:module)
|
2014-01-26 04:15:26 -05:00
|
|
|
"}}}
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
" CommandLine Keymap: {{{
|
2014-02-05 00:20:50 -05:00
|
|
|
function! s:search.keymapping() "{{{
|
|
|
|
return {
|
|
|
|
\ "\<C-l>" : {
|
|
|
|
\ "key" : "<Over>(buffer-complete)",
|
|
|
|
\ "noremap" : 1,
|
|
|
|
\ },
|
|
|
|
\ "\<Tab>" : {
|
|
|
|
\ "key" : "<Over>(em-scroll-f)",
|
|
|
|
\ "noremap" : 1,
|
|
|
|
\ },
|
|
|
|
\ "\<S-Tab>" : {
|
|
|
|
\ "key" : "<Over>(em-scroll-b)",
|
|
|
|
\ "noremap" : 1,
|
|
|
|
\ },
|
|
|
|
\ "\<C-o>" : {
|
|
|
|
\ "key" : "<Over>(em-jumpback)",
|
|
|
|
\ "noremap" : 1,
|
|
|
|
\ },
|
|
|
|
\ "\<C-z>" : {
|
|
|
|
\ "key" : "<Over>(em-openallfold)",
|
|
|
|
\ "noremap" : 1,
|
|
|
|
\ },
|
2014-02-23 23:43:16 -05:00
|
|
|
\ "\<CR>" : {
|
|
|
|
\ "key" : "<Over>(exit)",
|
|
|
|
\ "noremap" : 1,
|
|
|
|
\ "lock" : 1,
|
|
|
|
\ },
|
2014-02-05 00:20:50 -05:00
|
|
|
\ }
|
2014-01-26 04:15:26 -05:00
|
|
|
endfunction "}}}
|
2014-02-05 00:20:50 -05:00
|
|
|
|
|
|
|
" Fins Motion CommandLine Mapping Command: {{{
|
|
|
|
function! EasyMotion#command_line#cmap(args)
|
|
|
|
let lhs = s:as_keymapping(a:args[0])
|
|
|
|
let rhs = s:as_keymapping(a:args[1])
|
|
|
|
call s:search.cmap(lhs, rhs)
|
|
|
|
endfunction
|
|
|
|
function! EasyMotion#command_line#cnoremap(args)
|
|
|
|
let lhs = s:as_keymapping(a:args[0])
|
|
|
|
let rhs = s:as_keymapping(a:args[1])
|
|
|
|
call s:search.cnoremap(lhs, rhs)
|
|
|
|
endfunction
|
|
|
|
function! EasyMotion#command_line#cunmap(lhs)
|
|
|
|
let lhs = s:as_keymapping(a:lhs)
|
|
|
|
call s:search.cunmap(lhs)
|
|
|
|
endfunction
|
|
|
|
function! s:as_keymapping(key)
|
2014-02-23 23:43:16 -05:00
|
|
|
execute 'let result = "' . substitute(a:key, '\(<.\{-}>\)', '\\\1', 'g') . '"'
|
|
|
|
return result
|
2014-02-05 00:20:50 -05:00
|
|
|
endfunction
|
|
|
|
"}}}
|
2014-01-30 01:31:25 -05:00
|
|
|
"}}}
|
2014-01-26 04:15:26 -05:00
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
" Event: {{{
|
2014-02-09 20:34:41 -05:00
|
|
|
function! s:search.on_enter(cmdline) "{{{
|
2014-01-30 01:31:25 -05:00
|
|
|
if s:num_strokes == -1
|
2014-01-26 04:15:26 -05:00
|
|
|
call EasyMotion#highlight#delete_highlight()
|
2014-02-16 05:45:32 -05:00
|
|
|
call EasyMotion#helper#VarReset('&scrolloff', 0)
|
2014-02-05 01:15:05 -05:00
|
|
|
if g:EasyMotion_do_shade
|
|
|
|
call EasyMotion#highlight#add_highlight('\_.*',
|
|
|
|
\ g:EasyMotion_hl_group_shade)
|
|
|
|
endif
|
2014-03-21 04:02:26 -04:00
|
|
|
endif
|
|
|
|
if g:EasyMotion_cursor_highlight
|
2014-02-04 02:48:05 -05:00
|
|
|
call EasyMotion#highlight#add_highlight('\%#',
|
|
|
|
\ g:EasyMotion_hl_inc_cursor)
|
2014-01-26 04:15:26 -05:00
|
|
|
endif
|
|
|
|
endfunction "}}}
|
2014-02-09 20:34:41 -05:00
|
|
|
function! s:search.on_leave(cmdline) "{{{
|
2014-02-16 05:45:32 -05:00
|
|
|
if s:num_strokes == -1
|
|
|
|
call EasyMotion#highlight#delete_highlight(g:EasyMotion_hl_inc_search)
|
|
|
|
endif
|
2014-01-30 01:31:25 -05:00
|
|
|
endfunction "}}}
|
2014-02-09 20:34:41 -05:00
|
|
|
function! s:search.on_char(cmdline) "{{{
|
2014-01-30 01:31:25 -05:00
|
|
|
if s:num_strokes == -1
|
|
|
|
let re = s:search.getline()
|
2014-02-06 08:12:52 -05:00
|
|
|
if EasyMotion#helper#should_case_sensitive(re, 1)
|
|
|
|
let case_flag = '\c'
|
|
|
|
else
|
|
|
|
let case_flag = '\C'
|
|
|
|
endif
|
|
|
|
let re .= case_flag
|
2014-01-30 01:31:25 -05:00
|
|
|
if g:EasyMotion_inc_highlight
|
|
|
|
call s:inc_highlight(re)
|
|
|
|
endif
|
|
|
|
if g:EasyMotion_off_screen_search
|
|
|
|
call s:off_screen_search(re)
|
|
|
|
endif
|
|
|
|
elseif s:search.line.length() >= s:num_strokes
|
|
|
|
call s:search.exit()
|
|
|
|
endif
|
|
|
|
endfunction "}}}
|
2014-02-11 23:31:44 -05:00
|
|
|
"}}}
|
2014-01-26 04:15:26 -05:00
|
|
|
|
|
|
|
" Main:
|
|
|
|
function! EasyMotion#command_line#GetInput(num_strokes, prev, direction) "{{{
|
2014-01-30 01:31:25 -05:00
|
|
|
let s:num_strokes = a:num_strokes
|
2014-01-26 04:15:26 -05:00
|
|
|
|
2014-02-17 06:52:45 -05:00
|
|
|
let s:prompt_base = s:getPromptMessage(a:num_strokes)
|
2014-02-21 05:52:32 -05:00
|
|
|
call s:search.set_prompt(s:prompt_base)
|
2014-01-26 04:15:26 -05:00
|
|
|
|
|
|
|
" 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)
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
" Direction:
|
|
|
|
let s:direction = a:direction == 1 ? 'b' : ''
|
|
|
|
let s:save_direction = deepcopy(s:direction)
|
2014-01-26 04:15:26 -05:00
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
let input = s:search.get()
|
|
|
|
if input == '' && ! s:search.exit_code()
|
|
|
|
return a:prev
|
2014-02-23 23:43:16 -05:00
|
|
|
elseif s:search.exit_code() == 1 || s:search.exit_code() == -1
|
2014-02-06 05:35:16 -05:00
|
|
|
call s:Cancell()
|
|
|
|
return ''
|
2014-01-26 04:15:26 -05:00
|
|
|
else
|
2014-01-30 01:31:25 -05:00
|
|
|
return input
|
2014-01-26 04:15:26 -05:00
|
|
|
endif
|
|
|
|
endfunction "}}}
|
|
|
|
|
|
|
|
" Helper:
|
2014-01-13 18:48:26 -05:00
|
|
|
function! s:Cancell() " {{{
|
2014-01-30 01:31:25 -05:00
|
|
|
call EasyMotion#highlight#delete_highlight()
|
2014-02-16 05:45:32 -05:00
|
|
|
call EasyMotion#helper#VarReset('&scrolloff')
|
2014-01-23 08:53:58 -05:00
|
|
|
keepjumps call setpos('.', s:save_orig_pos)
|
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 "}}}
|
2014-01-26 04:15:26 -05:00
|
|
|
|
|
|
|
function! s:off_screen_search(re) "{{{
|
2014-01-22 21:47:31 -05:00
|
|
|
" First: search within visible screen range
|
|
|
|
call s:adjust_screen()
|
2014-01-30 01:31:25 -05:00
|
|
|
" Error occur when '\zs' without '!'
|
2014-01-22 21:47:31 -05:00
|
|
|
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
|
2014-02-23 23:43:16 -05:00
|
|
|
" redraw
|
2014-01-22 21:47:31 -05:00
|
|
|
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-26 04:15:26 -05:00
|
|
|
function! s:scroll(direction) "{{{
|
|
|
|
" direction: 0 -> forward, 1 -> backward
|
|
|
|
exec a:direction == 0 ? "normal! \<C-f>" : "normal! \<C-b>"
|
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-26 04:15:26 -05:00
|
|
|
let s:direction = a:direction == 0 ? '' : 'b'
|
2014-01-13 18:48:26 -05:00
|
|
|
endfunction "}}}
|
2014-01-26 04:15:26 -05:00
|
|
|
function! s:inc_highlight(re) "{{{
|
2014-01-30 01:31:25 -05:00
|
|
|
call EasyMotion#highlight#delete_highlight(g:EasyMotion_hl_inc_search)
|
|
|
|
if s:search.line.length() > 0
|
|
|
|
" Error occur when '\zs' without '!'
|
2014-01-26 04:15:26 -05:00
|
|
|
silent! call EasyMotion#highlight#add_highlight(a:re, g:EasyMotion_hl_inc_search)
|
|
|
|
endif
|
2014-01-13 18:48:26 -05:00
|
|
|
endfunction "}}}
|
2014-01-26 04:15:26 -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
|