Merge branch 'refactoring' into master

This commit is contained in:
haya14busa 2014-01-15 22:59:52 +09:00
commit 762ad9cc17

View File

@ -12,9 +12,9 @@ set cpo&vim
" == Init {{{ " == Init {{{
function! EasyMotion#init() function! EasyMotion#init()
" Init Migemo Dictionary " Init Migemo Dictionary
let s:old = {} let s:previous = {}
call EasyMotion#reset()
let s:migemo_dicts = {} let s:migemo_dicts = {}
let s:line_flag = 0
" Anywhere regular expression: {{{ " Anywhere regular expression: {{{
let re = '\v' . let re = '\v' .
\ '(<.|^$)' . '|' . \ '(<.|^$)' . '|' .
@ -44,15 +44,21 @@ function! EasyMotion#init()
endfunction "}}} endfunction "}}}
" == Reset {{{ " == Reset {{{
function! EasyMotion#reset() function! EasyMotion#reset()
let s:line_flag = 0 let s:flag = {
\ 'within_line' : 0,
\ 'dot_repeat' : 0,
\ }
return "" return ""
endfunction "}}} endfunction "}}}
" == Motion functions {{{ " == Motion functions {{{
" -- Find Motion ------------------------- " -- Find Motion -------------------------
function! EasyMotion#S(num_strokes, visualmode, direction) " {{{ function! EasyMotion#S(num_strokes, visualmode, direction) " {{{
let s:old['input'] = get(s:old, 'input', '') let s:previous['input'] = get(s:previous, 'input', '')
let input = EasyMotion#command_line#GetInput(a:num_strokes, s:old.input) let input = EasyMotion#command_line#GetInput(a:num_strokes, s:previous.input)
let s:old['input'] = input let s:previous['input'] = input
let mode = mode(1)
let is_exclusive = mode ==# 'no' ? 1 : 0
" Check that we have an input char " Check that we have an input char
if empty(input) if empty(input)
@ -67,12 +73,15 @@ function! EasyMotion#S(num_strokes, visualmode, direction) " {{{
let re = s:findMotion(input) let re = s:findMotion(input)
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', mode(1)) call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
function! EasyMotion#T(num_strokes, visualmode, direction) " {{{ function! EasyMotion#T(num_strokes, visualmode, direction) " {{{
let s:old['input'] = get(s:old, 'input', '') let s:previous['input'] = get(s:previous, 'input', '')
let input = EasyMotion#command_line#GetInput(a:num_strokes, s:old.input) let input = EasyMotion#command_line#GetInput(a:num_strokes, s:previous.input)
let s:old['input'] = input let s:previous['input'] = input
let mode = mode(1)
let is_exclusive = mode ==# 'no' ? 1 : 0
" Check that we have an input char " Check that we have an input char
if empty(input) if empty(input)
@ -95,28 +104,31 @@ function! EasyMotion#T(num_strokes, visualmode, direction) " {{{
let re = '.\ze' . re let re = '.\ze' . re
endif endif
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', mode(1)) call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
" -- Word Motion ------------------------- " -- Word Motion -------------------------
function! EasyMotion#WB(visualmode, direction) " {{{ function! EasyMotion#WB(visualmode, direction) " {{{
call s:EasyMotion('\(\<.\|^$\)', a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion('\(\<.\|^$\)', a:direction, a:visualmode ? visualmode() : '', 0)
endfunction " }}} endfunction " }}}
function! EasyMotion#WBW(visualmode, direction) " {{{ function! EasyMotion#WBW(visualmode, direction) " {{{
call s:EasyMotion('\(\(^\|\s\)\@<=\S\|^$\)', a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion('\(\(^\|\s\)\@<=\S\|^$\)', a:direction, a:visualmode ? visualmode() : '', 0)
endfunction " }}} endfunction " }}}
function! EasyMotion#E(visualmode, direction) " {{{ function! EasyMotion#E(visualmode, direction) " {{{
call s:EasyMotion('\(.\>\|^$\)', a:direction, a:visualmode ? visualmode() : '', mode(1)) let is_exclusive = mode(1) ==# 'no' ? 1 : 0
call s:EasyMotion('\(.\>\|^$\)', a:direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
function! EasyMotion#EW(visualmode, direction) " {{{ function! EasyMotion#EW(visualmode, direction) " {{{
call s:EasyMotion('\(\S\(\s\|$\)\|^$\)', a:direction, a:visualmode ? visualmode() : '', mode(1)) let is_exclusive = mode(1) ==# 'no' ? 1 : 0
call s:EasyMotion('\(\S\(\s\|$\)\|^$\)', a:direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
" -- JK Motion --------------------------- " -- JK Motion ---------------------------
function! EasyMotion#JK(visualmode, direction) " {{{ function! EasyMotion#JK(visualmode, direction) " {{{
"FIXME: support exclusive
if g:EasyMotion_startofline if g:EasyMotion_startofline
call s:EasyMotion('^\(\w\|\s*\zs\|$\)', a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion('^\(\w\|\s*\zs\|$\)', a:direction, a:visualmode ? visualmode() : '', 0)
else else
let prev_column = getpos('.')[2] - 1 let prev_column = getpos('.')[2] - 1
call s:EasyMotion('^.\{,' . prev_column . '}\zs\(.\|$\)', a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion('^.\{,' . prev_column . '}\zs\(.\|$\)', a:direction, a:visualmode ? visualmode() : '', 0)
endif endif
endfunction " }}} endfunction " }}}
function! EasyMotion#Sol(visualmode, direction) " {{{ function! EasyMotion#Sol(visualmode, direction) " {{{
@ -127,17 +139,20 @@ function! EasyMotion#Eol(visualmode, direction) " {{{
endfunction " }}} endfunction " }}}
" -- Search Motion ----------------------- " -- Search Motion -----------------------
function! EasyMotion#Search(visualmode, direction) " {{{ function! EasyMotion#Search(visualmode, direction) " {{{
call s:EasyMotion(@/, a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion(@/, a:direction, a:visualmode ? visualmode() : '', 0)
endfunction " }}} endfunction " }}}
" -- JumpToAnywhere Motion --------------- " -- JumpToAnywhere Motion ---------------
function! EasyMotion#JumpToAnywhere(visualmode, direction) " {{{ function! EasyMotion#JumpToAnywhere(visualmode, direction) " {{{
call s:EasyMotion( g:EasyMotion_re_anywhere, a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion( g:EasyMotion_re_anywhere, a:direction, a:visualmode ? visualmode() : '', 0)
endfunction " }}} endfunction " }}}
" -- Line Motion ------------------------- " -- Line Motion -------------------------
function! EasyMotion#SL(num_strokes, visualmode, direction) " {{{ function! EasyMotion#SL(num_strokes, visualmode, direction) " {{{
let s:old['input'] = get(s:old, 'input', '') let s:previous['input'] = get(s:previous, 'input', '')
let input = EasyMotion#command_line#GetInput(a:num_strokes, s:old.input) let input = EasyMotion#command_line#GetInput(a:num_strokes, s:previous.input)
let s:old['input'] = input let s:previous['input'] = input
let is_exclusive = mode(1) ==# 'no' ? 1 : 0
" Check that we have an input char " Check that we have an input char
if empty(input) if empty(input)
@ -150,16 +165,18 @@ function! EasyMotion#SL(num_strokes, visualmode, direction) " {{{
return return
endif endif
let s:line_flag = 1 let s:flag.within_line = 1
let re = s:findMotion(input) let re = s:findMotion(input)
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', mode(1)) call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
function! EasyMotion#TL(num_strokes, visualmode, direction) " {{{ function! EasyMotion#TL(num_strokes, visualmode, direction) " {{{
let s:old['input'] = get(s:old, 'input', '') let s:previous['input'] = get(s:previous, 'input', '')
let input = EasyMotion#command_line#GetInput(a:num_strokes, s:old.input) let input = EasyMotion#command_line#GetInput(a:num_strokes, s:previous.input)
let s:old['input'] = input let s:previous['input'] = input
let is_exclusive = mode(1) ==# 'no' ? 1 : 0
" Check that we have an input char " Check that we have an input char
if empty(input) if empty(input)
@ -172,7 +189,7 @@ function! EasyMotion#TL(num_strokes, visualmode, direction) " {{{
return return
endif endif
let s:line_flag = 1 let s:flag.within_line = 1
let re = s:findMotion(input) let re = s:findMotion(input)
@ -184,33 +201,34 @@ function! EasyMotion#TL(num_strokes, visualmode, direction) " {{{
let re = '.\ze' . re let re = '.\ze' . re
endif endif
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', mode(1)) call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
function! EasyMotion#WBL(visualmode, direction) " {{{ function! EasyMotion#WBL(visualmode, direction) " {{{
let s:line_flag = 1 let s:flag.within_line = 1
call s:EasyMotion('\(\<.\|^$\)', a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion('\(\<.\|^$\)', a:direction, a:visualmode ? visualmode() : '', 0)
endfunction " }}} endfunction " }}}
function! EasyMotion#EL(visualmode, direction) " {{{ function! EasyMotion#EL(visualmode, direction) " {{{
let s:line_flag = 1 let s:flag.within_line = 1
call s:EasyMotion('\(.\>\|^$\)', a:direction, a:visualmode ? visualmode() : '', mode(1)) let is_exclusive = mode(1) ==# 'no' ? 1 : 0
call s:EasyMotion('\(.\>\|^$\)', a:direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
function! EasyMotion#LineAnywhere(visualmode, direction) " {{{ function! EasyMotion#LineAnywhere(visualmode, direction) " {{{
let s:line_flag = 1 let s:flag.within_line = 1
let re = g:EasyMotion_re_line_anywhere let re = g:EasyMotion_re_line_anywhere
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', '') call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', 0)
endfunction " }}} endfunction " }}}
" -- Special Motion ---------------------- " -- Special Motion ----------------------
function! EasyMotion#SelectLines() "{{{ function! EasyMotion#SelectLines() "{{{
let orig_pos = [line('.'), col('.')] let orig_pos = [line('.'), col('.')]
call s:EasyMotion('^\(\w\|\s*\zs\|$\)', 2, '', '', 0, 0, 1) call s:EasyMotion('^\(\w\|\s*\zs\|$\)', 2, '', 0, 0, 0, 1)
if s:EasyMotion_cancelled if s:EasyMotion_cancelled
keepjumps call cursor(orig_pos[0], orig_pos[1]) keepjumps call cursor(orig_pos[0], orig_pos[1])
return '' return ''
else else
let pos1 = [line('.'), col('.')] let pos1 = [line('.'), col('.')]
keepjumps call cursor(orig_pos[0], orig_pos[1]) keepjumps call cursor(orig_pos[0], orig_pos[1])
call s:EasyMotion('^\(\w\|\s*\zs\|$\)', 2, '', '', pos1[0], 1, 1) call s:EasyMotion('^\(\w\|\s*\zs\|$\)', 2, '', 0, pos1[0], 1, 1)
if s:EasyMotion_cancelled if s:EasyMotion_cancelled
keepjumps call cursor(orig_pos[0], orig_pos[1]) keepjumps call cursor(orig_pos[0], orig_pos[1])
return '' return ''
@ -285,7 +303,7 @@ function! EasyMotion#SelectPhrase() "{{{
let orig_pos = [line('.'), col('.')] let orig_pos = [line('.'), col('.')]
" First " First
call s:EasyMotion(re, 2, '', '', 0, 0, 0, 0) call s:EasyMotion(re, 2, '', 0, 0, 0, 0, 0)
if s:EasyMotion_cancelled if s:EasyMotion_cancelled
keepjumps call cursor(orig_pos[0], orig_pos[1]) keepjumps call cursor(orig_pos[0], orig_pos[1])
return '' return ''
@ -296,7 +314,7 @@ function! EasyMotion#SelectPhrase() "{{{
keepjumps call cursor(orig_pos[0], orig_pos[1]) keepjumps call cursor(orig_pos[0], orig_pos[1])
" Second " Second
call s:EasyMotion(re, 2, '', '', 0, 0, 0, pos1) call s:EasyMotion(re, 2, '', 0, 0, 0, 0, pos1)
if s:EasyMotion_cancelled if s:EasyMotion_cancelled
keepjumps call cursor(orig_pos[0], orig_pos[1]) keepjumps call cursor(orig_pos[0], orig_pos[1])
return '' return ''
@ -340,25 +358,21 @@ endfunction "}}}
function! EasyMotion#User(pattern, mode, direction) " {{{ function! EasyMotion#User(pattern, mode, direction) " {{{
let visualmode = match('\v([Vv])|(C-v)', a:mode) > 0 ? visualmode() : '' let visualmode = match('\v([Vv])|(C-v)', a:mode) > 0 ? visualmode() : ''
let re = escape(a:pattern, '|') let re = escape(a:pattern, '|')
call s:EasyMotion(re, a:direction, visualmode, a:mode) call s:EasyMotion(re, a:direction, visualmode, 0)
endfunction " }}}
function! EasyMotion#UserMapping(re, mapping, direction) " {{{
silent exec "nnoremap ".a:mapping." :call EasyMotion#User('".a:re."', 0, ".a:direction.")<CR>"
silent exec "onoremap ".a:mapping." :call EasyMotion#User('".a:re."', 0, ".a:direction.")<CR>"
silent exec "vnoremap ".a:mapping." :<C-u>call EasyMotion#User('".a:re."', 0,".a:direction.")<CR>"
endfunction " }}} endfunction " }}}
" -- Repeat Motion ----------------------- " -- Repeat Motion -----------------------
function! EasyMotion#Repeat(visualmode) " {{{ function! EasyMotion#Repeat(visualmode) " {{{
" Repeat previous motion with previous targets " Repeat previous motion with previous targets
if s:old ==# {} if s:previous ==# {}
call s:Message("Previous targets doesn't exist") call s:Message("Previous targets doesn't exist")
return return
endif endif
let re = s:old.regexp let re = s:previous.regexp
let direction = s:old.direction let direction = s:previous.direction
let s:line_flag = s:old.line_flag let s:flag.within_line = s:previous.line_flag
let is_exclusive = mode(1) ==# 'no' ? 1 : 0
call s:EasyMotion(re, direction, a:visualmode ? visualmode() : '', mode(1)) call s:EasyMotion(re, direction, a:visualmode ? visualmode() : '', is_exclusive)
endfunction " }}} endfunction " }}}
" }}} " }}}
@ -599,7 +613,7 @@ function! s:should_use_migemo(char) "{{{
endif endif
" TODO: use direction " TODO: use direction
if s:line_flag == 1 if s:flag.within_line == 1
let first_line = line('.') let first_line = line('.')
let end_line = line('.') let end_line = line('.')
else else
@ -1008,9 +1022,9 @@ function! s:PromptUser(groups, allows_repeat, fixed_column) "{{{
" -- Repeat EasyMotion ------------------- {{{ " -- Repeat EasyMotion ------------------- {{{
if a:allows_repeat && if a:allows_repeat &&
\ char == '.' && \ char == '.' &&
\ exists('s:old_target_coord') \ exists('s:previous_target_coord')
" For SelectLines " For SelectLines
return s:old_target_coord return s:previous_target_coord
endif "}}} endif "}}}
" -- Check if the input char is valid ---- {{{ " -- Check if the input char is valid ---- {{{
if ! has_key(a:groups, char) if ! has_key(a:groups, char)
@ -1028,7 +1042,7 @@ function! s:PromptUser(groups, allows_repeat, fixed_column) "{{{
return s:PromptUser(target, a:allows_repeat, a:fixed_column) return s:PromptUser(target, a:allows_repeat, a:fixed_column)
endif endif
endfunction "}}} endfunction "}}}
function! s:EasyMotion(regexp, direction, visualmode, mode, ...) " {{{ function! s:EasyMotion(regexp, direction, visualmode, is_exclusive, ...) " {{{
" For Special Function {{{ " For Special Function {{{
" For SelectLines(), to highlight previous selected line " For SelectLines(), to highlight previous selected line
let hlcurrent = a:0 >= 1 ? a:1 : 0 let hlcurrent = a:0 >= 1 ? a:1 : 0
@ -1050,9 +1064,9 @@ function! s:EasyMotion(regexp, direction, visualmode, mode, ...) " {{{
let targets = [] let targets = []
" Store Regular Expression " Store Regular Expression
let s:old['regexp'] = a:regexp let s:previous['regexp'] = a:regexp
let s:old['direction'] = a:direction let s:previous['direction'] = a:direction
let s:old['line_flag'] = s:line_flag == 1 ? 1 : 0 let s:previous['line_flag'] = s:flag.within_line == 1 ? 1 : 0
try try
" -- Reset properties -------------------- {{{ " -- Reset properties -------------------- {{{
@ -1065,7 +1079,7 @@ function! s:EasyMotion(regexp, direction, visualmode, mode, ...) " {{{
let search_stopline = a:direction >= 1 ? win_first_line : win_last_line let search_stopline = a:direction >= 1 ? win_first_line : win_last_line
let search_at_cursor = fixed_column ? 'c' : '' let search_at_cursor = fixed_column ? 'c' : ''
if s:line_flag == 1 if s:flag.within_line == 1
let search_stopline = orig_pos[0] let search_stopline = orig_pos[0]
endif endif
"}}} "}}}
@ -1130,7 +1144,7 @@ function! s:EasyMotion(regexp, direction, visualmode, mode, ...) " {{{
endif endif
let targets2 = [] let targets2 = []
if s:line_flag == 0 if s:flag.within_line == 0
let search_stopline = win_last_line let search_stopline = win_last_line
else else
let search_stopline = !empty(a:visualmode) ? c_pos[0] : orig_pos[0] let search_stopline = !empty(a:visualmode) ? c_pos[0] : orig_pos[0]
@ -1211,17 +1225,19 @@ function! s:EasyMotion(regexp, direction, visualmode, mode, ...) " {{{
endif endif
" }}} " }}}
" Jump back before prompt{{{ " -- Jump back before prompt for visual scroll {{{
" Because searchpos() change current cursor position and " Because searchpos() change current cursor position and
" if you just use cursor([orig_num, orig_pos]) to jump back, " if you just use cursor([orig_num, orig_pos]) to jump back,
" current line will bebecome center of window " current line will bebecome center of window
keepjumps call cursor(win_first_line,0) if ! empty(a:visualmode)
normal! zt keepjumps call cursor(win_first_line,0)
normal! zt
endif
"}}} "}}}
" -- Prompt user for target group/character {{{ " -- Prompt user for target group/character {{{
let coords = s:PromptUser(groups, allows_repeat, fixed_column) let coords = s:PromptUser(groups, allows_repeat, fixed_column)
let s:old_target_coord = coords let s:previous_target_coord = coords
"}}} "}}}
" -- Update selection -------------------- {{{ " -- Update selection -------------------- {{{
@ -1234,15 +1250,18 @@ function! s:EasyMotion(regexp, direction, visualmode, mode, ...) " {{{
" -- Update cursor position -------------- {{{ " -- Update cursor position -------------- {{{
call cursor(orig_pos[0], orig_pos[1]) call cursor(orig_pos[0], orig_pos[1])
" Handle operator-pending mode {{{ " Handle operator-pending mode {{{
if a:mode == 'no' if a:is_exclusive == 1
" This mode requires that we eat one more " This mode requires that we eat one more
" character to the right if we're using " character to the right if we're using
" a forward motion " a forward motion
normal! v normal! v
endif endif
" }}} " }}}
call cursor(win_first_line, 0) " Adjuast screen for visual scroll {{{
normal! zt if ! empty(a:visualmode)
keepjumps call cursor(win_first_line, 0)
normal! zt
endif "}}}
keepjumps call cursor(coords[0], coords[1]) keepjumps call cursor(coords[0], coords[1])
call s:Message('Jumping to [' . coords[0] . ', ' . coords[1] . ']') call s:Message('Jumping to [' . coords[0] . ', ' . coords[1] . ']')