From 120b0bab882f86b262dc3d1181de567d43d9be5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Mon, 28 Mar 2011 23:35:27 +0200 Subject: [PATCH] Improve exception handling for get_char() --- plugin/EasyMotion.vim | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/plugin/EasyMotion.vim b/plugin/EasyMotion.vim index 01074ee..277a0d8 100644 --- a/plugin/EasyMotion.vim +++ b/plugin/EasyMotion.vim @@ -95,9 +95,14 @@ function! EasyMotionF(visualmode, direction) call Prompt('Search for character') - let char = getchar() + let char = GetChar() - let re = '\C' . escape(nr2char(char), '.$^~') + " Check that we have an input char + if empty(char) + return + endif + + let re = '\C' . escape(char, '.$^~') call EasyMotion(re, a:direction, a:visualmode ? visualmode() : '') endfunction @@ -107,12 +112,17 @@ function! EasyMotionT(visualmode, direction) call Prompt('Search for character') - let char = getchar() + let char = GetChar() + + " Check that we have an input char + if empty(char) + return + endif if a:direction == 1 - let re = '\C' . escape(nr2char(char), '.$^~') . '\zs.' + let re = '\C' . escape(char, '.$^~') . '\zs.' else - let re = '\C.' . escape(nr2char(char), '.$^~') + let re = '\C.' . escape(char, '.$^~') endif call EasyMotion(re, a:direction, a:visualmode ? visualmode() : '') @@ -171,6 +181,20 @@ call setline(line_num, line[a:key]) endfor endfunction " }}} + function! s:GetChar() " {{{ + let char = getchar() + + if char == 27 + " Escape key pressed + redraw + + call Message('Cancelled') + + return '' + endif + + return nr2char(char) + endfunction " }}} " }}} " Core functions {{{ function! s:PromptUser(groups) "{{{ @@ -230,7 +254,7 @@ call Prompt('Group character') endif - let input_char = nr2char(getchar()) + let input_char = GetChar() redraw @@ -242,9 +266,14 @@ redraw + " Check that we have an input char + if empty(input_char) + throw 'Cancelled' + 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 - throw 'Cancelled' + throw 'Invalid target' endif if single_group