Skip to content

Commit

Permalink
Simplify API as incsearch#go()
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Apr 30, 2015
1 parent ace87ac commit 32eccc4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
47 changes: 28 additions & 19 deletions autoload/incsearch.vim
Original file line number Diff line number Diff line change
Expand Up @@ -518,37 +518,46 @@ function! incsearch#cli() abort
endtry
endfunction

"" NOTE: this global variable is only for handling config from go_wrap func
" It avoids to make config string temporarily
let g:incsearch#_go_config = {}

"" This is main API
" ARGS:
" @config See autoload/incsearch/config.vim
" RETURN:
" Return primitive search commands (like `3/pattern<CR>`) if config.is_expr
" is TRUE, return excute command to call incsearch.vim's inner API.
" To handle dot repeat, make sure that config.is_expr is true. If you do not
" specify config.is_expr, it automatically set config.is_expr TRUE for
" operator-pending mode
" @api
function! incsearch#go(...) abort
let config = incsearch#config#make(get(a:, 1, {}))
let Search = function(config.is_stay ? 'incsearch#stay' : 'incsearch#search')
if s:U.is_visual(config.mode) && !config.is_expr
if config.is_expr
return incsearch#_go(config)
else
let g:incsearch#_go_config = config
let esc = s:U.is_visual(g:incsearch#_go_config.mode) ? "\<ESC>" : ''
return printf("%s:\<C-u>call incsearch#_go(g:incsearch#_go_config)\<CR>", esc)
endif
endfunction

" @return command: String to search
function! incsearch#_go(config) abort
let Search = function(a:config.is_stay ? 'incsearch#stay' : 'incsearch#search')
if s:U.is_visual(a:config.mode) && !a:config.is_expr
normal! gv
endif
let cli = s:make_cli(config)
let cli = s:make_cli(a:config)
let cmd = Search(cli)
if !config.is_expr
if !a:config.is_expr
let should_set_jumplist = (cli.flag !=# 'n')
call s:set_search_related_stuff(cli, cmd, should_set_jumplist)
endif
return cmd
endfunction

"" NOTE: this global variable is only for handling config from go_wrap func
" It avoids to make config string temporarily
let g:incsearch#_go_wrap_config = {}

" incsearch#go wrapper to call from non-expr state with mode and v:count1
" detection
" @api
" @return incsearch#go command to execute
function! incsearch#go_wrap(...) abort
let config = extend(get(a:, 1, {}), incsearch#config#lazy(), 'keep')
let g:incsearch#_go_wrap_config = config
let esc = s:U.is_visual(g:incsearch#_go_wrap_config.mode) ? "\<ESC>" : ''
return printf("%s:\<C-u>call incsearch#go(g:incsearch#_go_wrap_config)\<CR>", esc)
endfunction

" similar to incsearch#forward() but do not move the cursor unless explicitly
" move the cursor while searching
" @expr but sometimes called by non-<expr>
Expand Down
3 changes: 2 additions & 1 deletion autoload/incsearch/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ let s:config = {

" @return config for lazy value
function! incsearch#config#lazy() abort
return {'count1': v:count1, 'mode': mode(1)}
let m = mode(1)
return {'count1': v:count1, 'mode': m, 'is_expr': (m is# 'no')}
endfunction

" @return config with default value
Expand Down
13 changes: 3 additions & 10 deletions plugin/incsearch.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,9 @@ let s:save_cpo = &cpo
set cpo&vim
" }}}

" <expr> is just for handling mode(1) & v:count value, so basically called
" with non-<expr> state
noremap <silent><expr> <Plug>(incsearch-forward) incsearch#go_wrap({'command': '/'})
noremap <silent><expr> <Plug>(incsearch-backward) incsearch#go_wrap({'command': '?'})
noremap <silent><expr> <Plug>(incsearch-stay) incsearch#go_wrap({'command': '/', 'is_stay': 1})
" <expr> for dot repeat (`.`) in operator pending mode
onoremap <silent><expr> <Plug>(incsearch-forward) incsearch#go({'is_expr': 1, 'command': '/'})
onoremap <silent><expr> <Plug>(incsearch-backward) incsearch#go({'is_expr': 1, 'command': '?'})
onoremap <silent><expr> <Plug>(incsearch-stay) incsearch#go({'is_expr': 1, 'command': '/', 'is_stay': 1})
noremap <silent><expr> <Plug>(incsearch-forward) incsearch#go({'command': '/'})
noremap <silent><expr> <Plug>(incsearch-backward) incsearch#go({'command': '?'})
noremap <silent><expr> <Plug>(incsearch-stay) incsearch#go({'command': '/', 'is_stay': 1})
" Apply automatic :h :nohlsearch with :h :autocmd
" NOTE:
Expand Down

0 comments on commit 32eccc4

Please sign in to comment.