Wrap the main function in a try..catch..finally loop

The script throws exceptions when cancelled or when there's no matches.
The catch block displays the message and restores the cursor position.
The finally block restores buffer properties.
This commit is contained in:
Kim Silkebækken 2011-03-28 17:48:09 +02:00
parent 99afbc1b88
commit a5119b9981

View File

@ -183,11 +183,6 @@
let lines_items = items(lines)
" }}}
" Store original buffer properties {{{
let modified = &modified
let modifiable = &modifiable
let readonly = &readonly
" }}}
let input_char = ''
@ -195,16 +190,6 @@
" Highlight source
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
for [line_num, line] in lines_items
try
@ -242,20 +227,6 @@
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
if ! has_key(s:key_to_index, input_char) || s:key_to_index[input_char] >= targets_len
" Invalid input char
@ -276,9 +247,12 @@
let targets = []
let visualmode = a:0 > 0 ? a:1 : ''
" Store original scrolloff value
let scrolloff = &scrolloff
setl scrolloff=0
try
" Reset properties
call <SID>VarReset('&scrolloff', 0)
call <SID>VarReset('&modified', 0)
call <SID>VarReset('&modifiable', 1)
call <SID>VarReset('&readonly', 0)
" Find motion targets
while 1
@ -304,17 +278,7 @@
let groups_len = len(s:index_to_key)
if targets_len == 0
redraw
call <SID>Message('No matches')
" Restore cursor position
call setpos('.', [0, orig_pos[0], orig_pos[1]])
" Restore original scrolloff value
execute 'setl scrolloff=' . scrolloff
return
throw 'No matches'
endif
" Restore cursor position
@ -362,20 +326,7 @@
endif
if len(coords) != 2
" Cancelled by user
call <SID>Message('Operation cancelled')
" Restore cursor position/selection
if ! empty(visualmode)
silent exec 'normal! `<' . visualmode . '`>'
else
call setpos('.', [0, orig_pos[0], orig_pos[1]])
endif
" Restore original scrolloff value
execute 'setl scrolloff=' . scrolloff
return
throw 'Cancelled'
else
if ! empty(visualmode)
" Store original marks
@ -397,10 +348,28 @@
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
catch /.*/
redraw
" Show exception message
call <SID>Message(v:exception)
" Restore cursor position/selection
if ! empty(visualmode)
silent exec 'normal! `<' . visualmode . '`>'
else
call setpos('.', [0, orig_pos[0], orig_pos[1]])
endif
finally
redraw
" Restore properties
call <SID>VarReset('&scrolloff')
call <SID>VarReset('&modified')
call <SID>VarReset('&modifiable')
call <SID>VarReset('&readonly')
endtry
endfunction " }}}
" }}}