diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dd7450e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +# Unix-style newlines with a newline ending every file +[*] +trim_trailing_whitespace = true + +[*.vim] +indent_style = space +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false diff --git a/autoload/fzy.vim b/autoload/fzy.vim index 0f6e9c1..7a89184 100644 --- a/autoload/fzy.vim +++ b/autoload/fzy.vim @@ -20,6 +20,23 @@ const findcmd: list =<< trim END | cut -b3- END +export const vim_cmds_per_type: dict> = { + file: { tab: 'tabedit', vertical: 'vsplit', split: 'split' }, + buffer: { tab: 'tabnew | buffer', vertical: 'vertical sbuffer', split: 'sbuffer' }, + tag: { tab: 'tabnew | tjump', vertical: 'vertical stjump', split: 'stjump' }, + help: { tab: 'tabnew | help', vertical: 'vertical help', split: 'help' }, +} + +export var last_key_pressed: string + +def Get_cmd(type: string, default: string): string + var last_key = last_key_pressed + last_key_pressed = "" + return last_key == "default" + ? default + : vim_cmds_per_type[type][last_key] +enddef + def Error(msg: string) echohl ErrorMsg | echomsg msg | echohl None enddef @@ -121,22 +138,25 @@ def Opts(title: string, space: bool = false): dict return opts enddef -def Find_cb(dir: string, vim_cmd: string, choice: string) +def Find_cb(dir: string, default_cmd: string, choice: string) + const cmd = Get_cmd("file", default_cmd) var fpath: string = fnamemodify(dir, ':p:s?/$??') .. '/' .. choice fpath = fpath->resolve()->fnamemodify(':.')->fnameescape() - Update_cmd_history($'{vim_cmd} {fpath}') - Tryexe($'{vim_cmd} {fpath}') + Update_cmd_history($'{cmd} {fpath}') + Tryexe($'{cmd} {fpath}') enddef -def Open_file_cb(vim_cmd: string, choice: string) +def Open_file_cb(type: string, default_cmd: string, choice: string) + const cmd = Get_cmd(type, default_cmd) const fname: string = fnameescape(choice) - Update_cmd_history($'{vim_cmd} {fname}') - Tryexe($'{vim_cmd} {fname}') + Update_cmd_history($'{cmd} {fname}') + Tryexe($'{cmd} {fname}') enddef -def Open_tag_cb(vim_cmd: string, choice: string) - Update_cmd_history(vim_cmd .. ' ' .. choice) - Tryexe(vim_cmd .. ' ' .. escape(choice, '"')) +def Open_tag_cb(type: string, default_cmd: string, choice: string) + const cmd = Get_cmd(type, default_cmd) + Update_cmd_history(cmd .. ' ' .. choice) + Tryexe(cmd .. ' ' .. escape(choice, '"')) enddef def Marks_cb(split_cmd: string, bang: bool, item: string) @@ -147,13 +167,14 @@ def Marks_cb(split_cmd: string, bang: bool, item: string) Tryexe($'normal! {cmd}{item[1]}') enddef -def Grep_cb(efm: string, vim_cmd: string, choice: string) +def Grep_cb(efm: string, default_cmd: string, choice: string) const items: list = getqflist({lines: [choice], efm: efm})->get('items', []) if empty(items) || !items[0].bufnr Error('fzy: no valid item selected') return endif setbufvar(items[0].bufnr, '&buflisted', 1) + const vim_cmd = Get_cmd("buffer", default_cmd) const cmd: string = $'{vim_cmd} {items[0].bufnr} | call cursor({items[0].lnum}, {items[0].col})' Update_cmd_history(cmd) Tryexe(cmd) @@ -236,7 +257,7 @@ export def Stop() bufnr()->term_getjob()->job_stop() enddef -export def Find(dir: string, vim_cmd: string, mods: string) +export def Find(dir: string, default_cmd: string, mods: string) if !isdirectory(expand(dir, true)) Error($'fzy-find: Directory "{expand(dir, true)}" does not exist') return @@ -247,7 +268,7 @@ export def Find(dir: string, vim_cmd: string, mods: string) expand(path, true)->shellescape(), get(g:, 'fzy', {})->get('findcmd', join(findcmd)) ) - const editcmd: string = empty(mods) ? vim_cmd : (mods .. ' ' .. vim_cmd) + const editcmd: string = empty(mods) ? default_cmd : $"{mods} {default_cmd}" const stl: string = $':{editcmd} [directory: {path}]' Start(cmd, funcref(Find_cb, [path, editcmd]), Opts(stl)) enddef @@ -258,7 +279,7 @@ export def Buffers(edit_cmd: string, bang: bool, mods: string) ->filter(bang ? (_, i: number): bool => bufexists(i) : (_, i: number): bool => buflisted(i)) ->mapnew((_, i: number): any => i->bufname()->empty() ? i : i->bufname()->fnamemodify(':~:.')) const stl: string = printf(':%s (%s buffers)', cmd, bang ? 'all' : 'listed') - Start(items, funcref(Open_file_cb, [cmd]), Opts(stl)) + Start(items, funcref(Open_file_cb, ["buffer", cmd]), Opts(stl)) enddef export def Oldfiles(edit_cmd: string, mods: string) @@ -268,21 +289,21 @@ export def Oldfiles(edit_cmd: string, mods: string) ->filter((_, i: string): bool => i->fnamemodify(':p')->filereadable()) ->map((_, i: string): string => fnamemodify(i, ':~:.')) const stl: string = $':{cmd} (oldfiles)' - Start(items, funcref(Open_file_cb, [cmd]), Opts(stl)) + Start(items, funcref(Open_file_cb, ["file", cmd]), Opts(stl)) enddef export def Arg(edit_cmd: string, local: bool, mods: string) const items: list = local ? argv() : argv(-1, -1) const str: string = local ? 'local arglist' : 'global arglist' const cmd: string = empty(mods) ? edit_cmd : (mods .. ' ' .. edit_cmd) - Start(items, funcref(Open_file_cb, [cmd]), Opts($':{cmd} ({str})')) + Start(items, funcref(Open_file_cb, ["file", cmd]), Opts($':{cmd} ({str})')) enddef export def Help(help_cmd: string, mods: string) const cmd: string = empty(mods) ? help_cmd : (mods .. ' ' .. help_cmd) const items: string = 'cut -f 1 ' .. findfile('doc/tags', &runtimepath, -1)->join() const stl: string = $':{cmd} (helptags)' - Start(items, funcref(Open_tag_cb, [cmd]), Opts(stl)) + Start(items, funcref(Open_tag_cb, ["help", cmd]), Opts(stl)) enddef export def Grep(edit_cmd: string, mods: string, args: string) @@ -299,7 +320,7 @@ export def Tags(tags_cmd: string, mods: string) ? printf("sed '/^!_TAG_/ d' %s | cut -f 1 | sort | uniq", tagfiles()->join()) : taglist('.*')->mapnew((_, i: dict): string => i.name)->sort()->uniq() const stl: string = printf(':%s [%s]', cmd, tagfiles()->map((_, i: string): string => fnamemodify(i, ':~:.'))->join(', ')) - Start(items, funcref(Open_tag_cb, [cmd]), Opts(stl)) + Start(items, funcref(Open_tag_cb, ["tag", cmd]), Opts(stl)) enddef export def Marks(bang: bool, ...args: list) diff --git a/doc/fzy.txt b/doc/fzy.txt index b769dcf..49f603a 100644 --- a/doc/fzy.txt +++ b/doc/fzy.txt @@ -191,8 +191,7 @@ following options are supported: grepformat *g:fzy.grepformat* Set the format string for parsing the fuzzy-selected grep-output - line. This is a scanf-like string that uses the same format as the - 'grepformat' option. Example: > + line. This is a scanf-like string that uses the same format as the 'grepformat' option. Example: > g:fzy = { grepcmd: 'rg --vimgrep', grepformat: '%f:%l:%c:%m' @@ -204,6 +203,21 @@ following options are supported: Add the Ex command to Vim's command-line history. Default: |v:false| + keymaps *g:fzy.keymaps* + Dictionary for customizing action keybindings in the fzy window. + Default: > + { + 'tab': '', + 'vertical': '', + 'split': '', + 'default': '', + 'stop': '' + } +< + + For how to format keycodes check out |key-codes|. For examples see + |fzy-config-examples|. + ============================================================================== CONFIGURATION EXAMPLES *fzy-config-examples* @@ -290,6 +304,23 @@ CONFIGURATION EXAMPLES *fzy-config-examples* grepformat: '%f:%l:%c:%m' } < +11. Change action keymaps from CTRL to ALT: +> + g:fzy = { + keymaps: { + tab: "", + vertical: "", + split: "", + stop: "" + } + } +< +12. Change default action from `` to ``: +> + g:fzy = { + keymaps: {default: ""} + } +< ============================================================================== API *fzy-api* diff --git a/ftplugin/fzy.vim b/ftplugin/fzy.vim index 0245235..d13ce7a 100644 --- a/ftplugin/fzy.vim +++ b/ftplugin/fzy.vim @@ -8,6 +8,25 @@ vim9script # License: Same as Vim itself (see :h license) # ============================================================================== -tnoremap :call fzy#Stop() +import autoload 'fzy.vim' + +def AcceptEntry(type: string) + fzy.last_key_pressed = type + bufnr()->term_sendkeys("\") +enddef + +const cfg = { + tab: "", + vertical: "", + split: "", + default: "", + stop: "" +}->extend(get(g:, "fzy", {})->get("keymaps", {})) + +exec $"tnoremap {cfg.tab} AcceptEntry('tab')" +exec $"tnoremap {cfg.vertical} AcceptEntry('vertical')" +exec $"tnoremap {cfg.split} AcceptEntry('split')" +exec $"tnoremap {cfg.default} AcceptEntry('default')" +exec $"tnoremap {cfg.stop} fzy.Stop()" b:undo_ftplugin = 'execute "tunmap "' diff --git a/plugin/fzy.vim b/plugin/fzy.vim index 4a19f17..c27ec87 100644 --- a/plugin/fzy.vim +++ b/plugin/fzy.vim @@ -8,7 +8,7 @@ vim9script # License: Same as Vim itself (see :h license) # ============================================================================== -import autoload '../autoload/fzy.vim' +import autoload 'fzy.vim' command -nargs=? -complete=dir FzyFind fzy.Find(empty() ? getcwd() : , 'edit', '') command -nargs=? -complete=dir FzyFindSplit fzy.Find(empty() ? getcwd() : , 'split', )