diff --git a/README.md b/README.md index 8472b63..6bcd13d 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ typing more shortcut keys or parts of shortcut descriptions shown in the menu. ## Requirements * [fzf.vim] plugin. +* [denite.nvim](https://github.com/Shougo/denite.nvim) optional plugin. ## Usage @@ -21,10 +22,14 @@ typing more shortcut keys or parts of shortcut descriptions shown in the menu. * Use the `:Shortcuts` command to display a searchable menu of shortcuts. +* Use the `:Denite shortcut` command to display a searcheable menu of shourtcuts through the denite interface + * Use the `g:shortcuts` variable to access shortcuts keys and descriptions. * Use the `g:shortcuts_overwrite_warning` variable to detect any conflicts. +* Use the `g:shortcut_expand_leader_keys` variable to indicate whether to expand leader keys in the Shortcuts list. + ### Discovery & fallback shortcuts I recommend that you define these two shortcuts for discovery and fallback diff --git a/doc/shortcut.txt b/doc/shortcut.txt index a80fbd8..445ccf3 100644 --- a/doc/shortcut.txt +++ b/doc/shortcut.txt @@ -30,6 +30,10 @@ USAGE *shortcut-usage* * Use the |g:shortcuts_overwrite_warning| variable to detect any conflicts. +* Use the |g:shortcut_expand_leader_keys| variable to indicate whether to + expand leader keys in the Shortcuts list. + + ------------------------------------------------------------------------------ SETUP *shortcut-setup* ------------------------------------------------------------------------------ @@ -67,6 +71,15 @@ g:shortcuts_overwrite_warning *g:shortcuts_overwrite_warning* let g:shortcuts_overwrite_warning = 1 " enable this warning < +g:g:shortcut_expand_leader_keys *g:shortcuts_expand_leader_keys* + Option to expand leader keys in list.If you have the leader key + set to it is useful to not expand. + This is enabled by default. Assign 0 to disable this feature. +> + let g:shortcut_expand_leader_keys = 0 " disables expansion +< + + ------------------------------------------------------------------------------ COMMANDS *shortcut-cmd* ------------------------------------------------------------------------------ @@ -96,6 +109,11 @@ COMMANDS *shortcut-cmd* To avoid this limitation, just trigger the shortcut directly (without choosing through a menu) or use |:Shortcuts| instead. +:Denite shortcut *:Denite shortcut* + Display a |denite.nvim| menu of shortcuts described in |g:shortcuts|. + When an item is chosen from the menu, its shortcut {keys} are + synthetically replayed (as if you typed them) via |feedkeys()|. + :Shortcut! {keys} {description} *:Shortcut!* Associate an existing shortcut's {keys} to its {description} in the |g:shortcuts| dictionary. Extra whitespace is ignored. diff --git a/plugin/shortcut.vim b/plugin/shortcut.vim index 8d4b8a1..7c702c3 100644 --- a/plugin/shortcut.vim +++ b/plugin/shortcut.vim @@ -7,8 +7,13 @@ if !exists('g:shortcuts') let g:shortcuts = {} endif +if !exists('g:shortcut_expand_leader_keys') + let g:shortcut_expand_leader_keys = 1 +endif + command! -range -bang Shortcuts ,call s:shortcut_menu_command(0) command! -range -bang ShortcutsRangeless call s:shortcut_menu_command(0) +let s:is_from_visual = '' function! s:shortcut_menu_command(fullscreen) range abort let s:is_from_visual = a:firstline == line("'<") && a:lastline == line("'>") @@ -20,11 +25,20 @@ function! s:shortcut_menu_command(fullscreen) range abort endfunction function! s:shortcut_menu_items() abort - let labels = map(copy(g:shortcuts), 'ShortcutLeaderKeys(v:key)') + let apply = 'v:key' + if g:shortcut_expand_leader_keys + let apply = 'ShortcutLeaderKeys(v:key)' + endif + let labels = map(copy(g:shortcuts), apply) let width = max(map(values(labels), 'len(v:val)')) + 4 return values(map(labels, "printf('%-".width."S%s', v:val, g:shortcuts[v:key])")) endfunction +function! ShortcutFeedKeys(choice) range abort + let s:is_from_visual = a:firstline == line("'<") && a:lastline == line("'>") + call s:shortcut_menu_item_action(a:choice) +endfunction + function! s:shortcut_menu_item_action(choice) abort let shortcut = substitute(a:choice, '\s.*', '', '') let keystrokes = ShortcutKeystrokes(shortcut) diff --git a/rplugin/python3/denite/source/shortcut.py b/rplugin/python3/denite/source/shortcut.py new file mode 100644 index 0000000..fe9c260 --- /dev/null +++ b/rplugin/python3/denite/source/shortcut.py @@ -0,0 +1,49 @@ +# ============================================================================ +# FILE: shortcut.py +# ============================================================================ + +from os import path + +from denite.base.source import Base +from denite.kind.command import Kind as Command + +from denite.util import globruntime, Nvim, UserContext, Candidates + + +class Source(Base): + + def __init__(self, vim: Nvim) -> None: + super().__init__(vim) + + self.name = 'shortcut' + self.kind = Kind(vim) + + def gather_candidates(self, context: UserContext) -> Candidates: + shortcuts = {} + + for shortcut, description in self.vim.vars["shortcuts"].items(): + if self.vim.vars['shortcut_expand_leader_keys'] == 1: + shortcut = self.vim.eval(f'ShortcutLeaderKeys("{shortcut}")') + + shortcuts[shortcut] = { + 'word': '{0:<12} -- {1}'.format(shortcut, description), + 'action__command': shortcut + } + + return sorted(shortcuts.values(), key=lambda value: value['word']) + + +class Kind(Command): + + def __init__(self, vim: Nvim) -> None: + super().__init__(vim) + + self.name = 'shortcut' + + def action_execute(self, context: UserContext) -> None: + target = context['targets'][0] + command = target['action__command'] + self.vim.command(f'call ShortcutFeedKeys("{command}")') + + def action_edit(self, context: UserContext) -> None: + return super().action_execute(context)