Merge branch 'feature/var-reset' into develop

* feature/var-reset:
  Wrap the main function in a try..catch..finally loop
  Create function for resetting/restoring variables
This commit is contained in:
Kim Silkebækken 2011-03-28 17:49:42 +02:00
commit 72dfd55cd1

View File

@ -66,6 +66,8 @@
let s:key_to_index[i] = index let s:key_to_index[i] = index
let index += 1 let index += 1
endfor endfor
let s:var_reset = {}
" }}} " }}}
" Motion functions {{{ " Motion functions {{{
" F key motions {{{ " F key motions {{{
@ -124,6 +126,20 @@
echo a:message . ': ' echo a:message . ': '
echohl None echohl None
endfunction " }}} endfunction " }}}
function! s:VarReset(var, ...) " {{{
if a:0 == 0 && has_key(s:var_reset, a:var)
" Reset var to original value
call setbufvar(bufname(0), a:var, s:var_reset[a:var])
elseif a:0 == 1
let new_value = a:0 == 1 ? a:1 : ''
" Store original value
let s:var_reset[a:var] = getbufvar(bufname(0), a:var)
" Set new var value
call setbufvar(bufname(0), a:var, new_value)
endif
endfunction " }}}
" }}} " }}}
" Core functions {{{ " Core functions {{{
function! s:PromptUser(groups) "{{{ function! s:PromptUser(groups) "{{{
@ -167,11 +183,6 @@
let lines_items = items(lines) let lines_items = items(lines)
" }}} " }}}
" Store original buffer properties {{{
let modified = &modified
let modifiable = &modifiable
let readonly = &readonly
" }}}
let input_char = '' let input_char = ''
@ -179,16 +190,6 @@
" Highlight source " Highlight source
let target_hl_id = matchadd(g:EasyMotion_target_hl, join(hl_coords, '\|'), 1) let target_hl_id = matchadd(g:EasyMotion_target_hl, join(hl_coords, '\|'), 1)
" Make sure we can change the buffer {{{
if modifiable == 0
silent setl modifiable
endif
if readonly == 1
silent setl noreadonly
endif
" }}}
" Set lines with markers " Set lines with markers
for [line_num, line] in lines_items for [line_num, line] in lines_items
try try
@ -226,20 +227,6 @@
redraw redraw
" Restore original properties {{{
if modified == 0
silent setl nomodified
endif
if modifiable == 0
silent setl nomodifiable
endif
if readonly == 1
silent setl readonly
endif
" }}}
" Check if the input char is valid " Check if the input char is valid
if ! has_key(s:key_to_index, input_char) || s:key_to_index[input_char] >= targets_len if ! has_key(s:key_to_index, input_char) || s:key_to_index[input_char] >= targets_len
" Invalid input char " Invalid input char
@ -260,94 +247,114 @@
let targets = [] let targets = []
let visualmode = a:0 > 0 ? a:1 : '' let visualmode = a:0 > 0 ? a:1 : ''
" Store original scrolloff value try
let scrolloff = &scrolloff " Reset properties
setl scrolloff=0 call <SID>VarReset('&scrolloff', 0)
call <SID>VarReset('&modified', 0)
call <SID>VarReset('&modifiable', 1)
call <SID>VarReset('&readonly', 0)
" Find motion targets " Find motion targets
while 1 while 1
let search_direction = (a:direction == 1 ? 'b' : '') let search_direction = (a:direction == 1 ? 'b' : '')
let search_stopline = line(a:direction == 1 ? 'w0' : 'w$') let search_stopline = line(a:direction == 1 ? 'w0' : 'w$')
let pos = searchpos(a:regexp, search_direction, search_stopline) let pos = searchpos(a:regexp, search_direction, search_stopline)
" Reached end of search range " Reached end of search range
if pos == [0, 0] if pos == [0, 0]
break break
endif
" Skip folded lines
if foldclosed(pos[0]) != -1
continue
endif
call add(targets, pos)
endwhile
let targets_len = len(targets)
let groups_len = len(s:index_to_key)
if targets_len == 0
throw 'No matches'
endif endif
" Skip folded lines
if foldclosed(pos[0]) != -1
continue
endif
call add(targets, pos)
endwhile
let targets_len = len(targets)
let groups_len = len(s:index_to_key)
if targets_len == 0
redraw
call <SID>Message('No matches')
" Restore cursor position " Restore cursor position
call setpos('.', [0, orig_pos[0], orig_pos[1]]) call setpos('.', [0, orig_pos[0], orig_pos[1]])
" Restore original scrolloff value " Split targets into key groups {{{
execute 'setl scrolloff=' . scrolloff let groups = []
let i = 0
return while i < targets_len
endif call add(groups, targets[i : i + groups_len - 1])
" Restore cursor position let i += groups_len
call setpos('.', [0, orig_pos[0], orig_pos[1]]) endwhile
" }}}
" Too many groups; only display the first ones {{{
if len(groups) > groups_len
call <SID>Message('Only displaying the first matches')
" Split targets into key groups {{{ let groups = groups[0 : groups_len - 1]
let groups = [] endif
let i = 0 " }}}
while i < targets_len " Shade inactive source
call add(groups, targets[i : i + groups_len - 1]) if g:EasyMotion_shade
let shade_hl_pos = '\%' . orig_pos[0] . 'l\%'. orig_pos[1] .'c'
let i += groups_len if a:direction == 1
endwhile " Backward
" }}} let shade_hl_re = '\%'. line('w0') .'l\_.*' . shade_hl_pos
" Too many groups; only display the first ones {{{ else
if len(groups) > groups_len " Forward
call <SID>Message('Only displaying the first matches') let shade_hl_re = shade_hl_pos . '\_.*\%'. line('w$') .'l'
endif
let groups = groups[0 : groups_len - 1] let shade_hl_id = matchadd(g:EasyMotion_shade_hl, shade_hl_re, 0)
endif endif
" }}}
" Shade inactive source " Prompt user for target group/character
if g:EasyMotion_shade let coords = <SID>PromptUser(groups)
let shade_hl_pos = '\%' . orig_pos[0] . 'l\%'. orig_pos[1] .'c'
if a:direction == 1 " Remove shading
" Backward if g:EasyMotion_shade
let shade_hl_re = '\%'. line('w0') .'l\_.*' . shade_hl_pos call matchdelete(shade_hl_id)
endif
if len(coords) != 2
throw 'Cancelled'
else else
" Forward if ! empty(visualmode)
let shade_hl_re = shade_hl_pos . '\_.*\%'. line('w$') .'l' " Store original marks
let m_a = getpos("'a")
let m_b = getpos("'b")
" Store start/end positions
call setpos("'a", [0, orig_pos[0], orig_pos[1]])
call setpos("'b", [0, coords[0], coords[1]])
" Update selection
silent exec 'normal! `a' . visualmode . '`b'
" Restore original marks
call setpos("'a", m_a)
call setpos("'b", m_b)
else
" Update cursor position
call setpos('.', [0, coords[0], coords[1]])
endif
call <SID>Message('Jumping to [' . coords[0] . ', ' . coords[1] . ']')
endif endif
catch /.*/
redraw
let shade_hl_id = matchadd(g:EasyMotion_shade_hl, shade_hl_re, 0) " Show exception message
endif call <SID>Message(v:exception)
" Prompt user for target group/character
let coords = <SID>PromptUser(groups)
" Remove shading
if g:EasyMotion_shade
call matchdelete(shade_hl_id)
endif
if len(coords) != 2
" Cancelled by user
call <SID>Message('Operation cancelled')
" Restore cursor position/selection " Restore cursor position/selection
if ! empty(visualmode) if ! empty(visualmode)
@ -355,36 +362,14 @@
else else
call setpos('.', [0, orig_pos[0], orig_pos[1]]) call setpos('.', [0, orig_pos[0], orig_pos[1]])
endif endif
finally
redraw
" Restore original scrolloff value " Restore properties
execute 'setl scrolloff=' . scrolloff call <SID>VarReset('&scrolloff')
call <SID>VarReset('&modified')
return call <SID>VarReset('&modifiable')
else call <SID>VarReset('&readonly')
if ! empty(visualmode) endtry
" Store original marks
let m_a = getpos("'a")
let m_b = getpos("'b")
" Store start/end positions
call setpos("'a", [0, orig_pos[0], orig_pos[1]])
call setpos("'b", [0, coords[0], coords[1]])
" Update selection
silent exec 'normal! `a' . visualmode . '`b'
" Restore original marks
call setpos("'a", m_a)
call setpos("'b", m_b)
else
" Update cursor position
call setpos('.', [0, coords[0], coords[1]])
endif
" Restore original scrolloff value
execute 'setl scrolloff=' . scrolloff
call <SID>Message('Jumping to [' . coords[0] . ', ' . coords[1] . ']')
endif
endfunction " }}} endfunction " }}}
" }}} " }}}