Skip to content

Commit

Permalink
Merge branch 'before_open_trigger'
Browse files Browse the repository at this point in the history
  • Loading branch information
hecal3 committed Oct 6, 2018
2 parents 54fe7e3 + ca5d53f commit 8dff63f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 13 deletions.
71 changes: 58 additions & 13 deletions autoload/leaderGuide.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
let s:save_cpo = &cpo
set cpo&vim

function! leaderGuide#add_trigger(name, fun) " {{{
if !exists('s:triggers')
let s:triggers = {}
endif

if type(a:fun) ==? type(function("tr"))
let s:triggers[a:name] = a:fun
elseif has_key(s:triggers, a:name)
unlet! s:triggers[a:name]
endif
endfunction " }}}
function! leaderGuide#has_configuration() "{{{
return exists('s:desc_lookup')
endfunction "}}}
Expand Down Expand Up @@ -293,10 +304,36 @@ function! s:create_string(layout) " {{{
endfunction " }}}


function! s:trigger_before_open() " {{{
let trigger_name = 'before_open'
if !exists("s:triggers") || !has_key(s:triggers, trigger_name)
return
endif

let g:leaderGuide#context = {
\ 'type' : 'trigger',
\ 'name' : trigger_name,
\ 'display': s:lmap,
\ 'level': s:current_level,
\ 'register': s:reg,
\ 'visual': s:vis == '',
\ 'count': s:count,
\ 'winv': s:winv,
\ 'winnr': s:winnr,
\ 'winres': s:winres
\ }

let Fun = s:triggers[trigger_name]
call Fun()
endfunction " }}}

function! s:start_buffer() " {{{
let s:winv = winsaveview()
let s:winnr = winnr()
let s:winres = winrestcmd()

call s:trigger_before_open()

call s:winopen()
let layout = s:calc_layout()
let string = s:create_string(layout)
Expand All @@ -319,11 +356,16 @@ endfunction " }}}
function! s:handle_input(input) " {{{
call s:winclose()
if type(a:input) ==? type({})
let s:current_level += 1
let s:lmap = a:input
call s:start_buffer()
else
call feedkeys(s:vis.s:reg.s:count, 'ti')
if type(a:input) !=? type([])
let last = strpart(s:last_inp[-1], strchars(s:last_inp[-1]) - 1)
if s:last_inp[0] !=? last
execute s:escape_mappings({'rhs': join(s:last_inp, ""), 'noremap': 0})
endif
return
endif
redraw
Expand All @@ -336,13 +378,15 @@ function! s:handle_input(input) " {{{
endfunction " }}}
function! s:wait_for_input() " {{{
redraw
let inp = input("")
if inp ==? ''
"let inp = input("")
let curr_inp = input("")
if curr_inp ==? ''
call s:winclose()
elseif match(inp, "^<LGCMD>submode") == 0
elseif match(curr_inp, "^<LGCMD>submode") == 0
call s:submode_mappings()
else
let fsel = get(s:lmap, inp)
call add(s:last_inp, curr_inp)
let fsel = get(s:lmap, curr_inp)
call s:handle_input(fsel)
endif
endfunction " }}}
Expand Down Expand Up @@ -441,17 +485,24 @@ function! s:get_register() "{{{
endif
return clip
endfunction "}}}
function! leaderGuide#start_by_prefix(vis, key) " {{{
function! s:init_on_call(vis) " {{{
let s:vis = a:vis ? 'gv' : ''
let s:count = v:count != 0 ? v:count : ''
let s:toplevel = a:key ==? ' '
let s:current_level = 1
let s:last_inp = []

if has('nvim') && !exists('s:reg')
let s:reg = ''
else
let s:reg = v:register != s:get_register() ? '"'.v:register : ''
endif
endfunction " }}}
function! leaderGuide#start_by_prefix(vis, key) " {{{

call s:init_on_call(a:vis)
call add(s:last_inp, a:key)

let s:toplevel = a:key ==? ' '
if !has_key(s:cached_dicts, a:key) || g:leaderGuide_run_map_on_popup
"first run
let s:cached_dicts[a:key] = {}
Expand All @@ -469,14 +520,8 @@ function! leaderGuide#start_by_prefix(vis, key) " {{{
endfunction " }}}
function! leaderGuide#start(vis, dict) " {{{

if has('nvim') && !exists('s:reg')
let s:reg = ''
else
let s:reg = v:register != s:get_register() ? '"'.v:register : ''
endif
call s:init_on_call(a:vis)

let s:count = v:count != 0 ? v:count : ''
let s:vis = a:vis ? 'gv' : 0
let s:lmap = a:dict
call s:start_buffer()
endfunction " }}}
Expand Down
47 changes: 47 additions & 0 deletions doc/vim-leader-guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,51 @@ leaderGuide#parse_mappings()
Note: Only call this function if |g:leaderGuide_run_map_on_popup| is unset
and the current mappings have changed (switched to a buffer with other
mappings, new filetype, manual change, etc.)



*leaderGuide#add_trigger()*
leaderGuide#add_trigger("{name}", {funcref})
Adds the specified function as a trigger to be called a a specific
point during execution.

Currently available triggers:
- before_open: called before the guide-buffer opens.


Inside of the function |g:leaderGuide#context| is available. It contains
a context dictionary with the following information

'type' : fix: "trigger"
'name' : Current trigger name (eg. 'before_open')
'display': Current guide dictionary (can be modified)
'level': Current call level (starting at on at first key-press)
'register': Currently selected register
'visual': visual mode (y/n)
'count': Range given
'winv': return value of winsaveview()
'winnr': Current window number

The display-dictionary 'display' can still be modified at this point.

Example:
Removes the menu or mapping 'g' from level 1 (Just the output, the
underlying mappings will still work)
>
function! s:leaderGuide_before_open()
" echom string(g:leaderGuide#context)
let context = g:leaderGuide#context
let guide_dictionary = get(g:leaderGuide#context, 'display')
let level = get(g:leaderGuide#context, 'level')
if level == 1 && has_key(guide_dictionary, 'g')
call remove(guide_dictionary, 'g')
endif
endfunction
call leaderGuide#add_trigger('before_open', function("s:leaderGuide_before_open"))
<


==============================================================================
GLOBAL VARIABLES *leader-guide-globals*
Expand Down Expand Up @@ -556,6 +600,9 @@ g:leaderGuide_submode_mappings~
==============================================================================
CHANGELOG *leader-guide-changelog*

2018-10-06: - Add before_open trigger.
(See |leaderGuide#add_trigger()|)

2016-11-06: - Add paging function.
(See |g:leaderGuide_max_size| and
|g:leaderGuide_submode_mappings|)
Expand Down

0 comments on commit 8dff63f

Please sign in to comment.