From c4f525e45be3a1daf57ec83912039d5a47e38897 Mon Sep 17 00:00:00 2001 From: Luca Saccarola Date: Wed, 25 Sep 2024 14:51:55 +0200 Subject: [PATCH] Add ability to split with a keybinding fixes #8 --- autoload/fzy.vim | 82 +++++++++++++++++++++++++++--------------------- ftplugin/fzy.vim | 3 ++ plugin/fzy.vim | 30 +++++++++--------- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/autoload/fzy.vim b/autoload/fzy.vim index 0f6e9c1..a92f3c6 100644 --- a/autoload/fzy.vim +++ b/autoload/fzy.vim @@ -20,6 +20,18 @@ const findcmd: list =<< trim END | cut -b3- END +export def Dispatch_vim_cmd(input_kind: string): string + const last_key = g:fzy_last_key_pressed + g:fzy_last_key_pressed = '' + const dispatch = { + file: { 'C-v': 'vsplit', 'C-s': 'split', 'CR': 'edit' }, + buffer: { 'C-v': 'vertical sbuffer', 'C-s': 'sbuffer', 'CR': 'buffer' }, + tag: { 'C-v': 'vertical stjump', 'C-s': 'stjump', 'CR': 'tjump' }, + help: { 'C-v': 'vertical help', 'C-s': 'help', 'CR': 'help' }, + } + return dispatch[input_kind][last_key] +enddef + def Error(msg: string) echohl ErrorMsg | echomsg msg | echohl None enddef @@ -121,25 +133,28 @@ 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, choice: string) var fpath: string = fnamemodify(dir, ':p:s?/$??') .. '/' .. choice + const cmd = Dispatch_vim_cmd('file') 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(choice: string) const fname: string = fnameescape(choice) - Update_cmd_history($'{vim_cmd} {fname}') - Tryexe($'{vim_cmd} {fname}') + const cmd: string = Dispatch_vim_cmd('buffer') + 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(tag_type: string, choice: string) + const cmd = Dispatch_vim_cmd(tag_type) + Update_cmd_history(cmd .. ' ' .. choice) + Tryexe(cmd .. ' ' .. escape(choice, '"')) enddef -def Marks_cb(split_cmd: string, bang: bool, item: string) +def Marks_cb(bang: bool, item: string) if !empty(split_cmd) execute split_cmd endif @@ -147,14 +162,15 @@ 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, 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 cmd: string = $'{vim_cmd} {items[0].bufnr} | call cursor({items[0].lnum}, {items[0].col})' + var cmd = Dispatch_vim_cmd('buffer') + cmd = $'{cmd} {items[0].bufnr} | call cursor({items[0].lnum}, {items[0].col})' Update_cmd_history(cmd) Tryexe(cmd) enddef @@ -236,7 +252,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, mods: string) if !isdirectory(expand(dir, true)) Error($'fzy-find: Directory "{expand(dir, true)}" does not exist') return @@ -247,59 +263,53 @@ 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 stl: string = $':{editcmd} [directory: {path}]' - Start(cmd, funcref(Find_cb, [path, editcmd]), Opts(stl)) + const stl: string = $':[directory: {path}]' + Start(cmd, funcref(Find_cb, [path]), Opts(stl)) enddef -export def Buffers(edit_cmd: string, bang: bool, mods: string) - const cmd: string = empty(mods) ? edit_cmd : (mods .. ' ' .. edit_cmd) +export def Buffers(bang: bool, mods: string) const items: list = range(1, bufnr('$')) ->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)) + const stl: string = printf(':%s (%s buffers)', "", bang ? 'all' : 'listed') + Start(items, funcref(Open_file_cb), Opts(stl)) enddef -export def Oldfiles(edit_cmd: string, mods: string) - const cmd: string = empty(mods) ? edit_cmd : (mods .. ' ' .. edit_cmd) +export def Oldfiles(mods: string) const items: list = v:oldfiles ->copy() ->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), Opts(stl)) enddef -export def Arg(edit_cmd: string, local: bool, mods: string) +export def Arg(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), Opts($':({str})')) enddef -export def Help(help_cmd: string, mods: string) - const cmd: string = empty(mods) ? help_cmd : (mods .. ' ' .. help_cmd) +export def Help(mods: string) 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)) + const stl: string = $'(helptags)' + Start(items, funcref(Open_tag_cb, ['help']), Opts(stl)) enddef -export def Grep(edit_cmd: string, mods: string, args: string) +export def Grep(mods: string, args: string) const cmd: string = empty(mods) ? edit_cmd : (mods .. ' ' .. edit_cmd) const grep_cmd: string = get(g:, 'fzy', {})->get('grepcmd', &grepprg) .. ' ' .. args const grep_efm: string = get(g:, 'fzy', {})->get('grepformat', &grepformat) const stl: string = $':{cmd} ({grep_cmd})' - Start(grep_cmd, funcref(Grep_cb, [grep_efm, cmd]), Opts(stl)) + Start(grep_cmd, funcref(Grep_cb, [grep_efm]), Opts(stl)) enddef -export def Tags(tags_cmd: string, mods: string) - const cmd: string = empty(mods) ? tags_cmd : (mods .. ' ' .. tags_cmd) +export def Tags() const items: any = executable('sed') && executable('cut') && executable('sort') && executable('uniq') ? 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)) + const stl: string = printf(':%s [%s]', tagfiles()->map((_, i: string): string => fnamemodify(i, ':~:.'))->join(', ')) + Start(items, funcref(Open_tag_cb, ['tag']), Opts(stl)) enddef export def Marks(bang: bool, ...args: list) diff --git a/ftplugin/fzy.vim b/ftplugin/fzy.vim index 0245235..7a155d7 100644 --- a/ftplugin/fzy.vim +++ b/ftplugin/fzy.vim @@ -9,5 +9,8 @@ vim9script # ============================================================================== tnoremap :call fzy#Stop() +tnoremap :let g:fzy_last_key_pressed = 'C-v' +tnoremap :let g:fzy_last_key_pressed = 'C-s' +tnoremap :let g:fzy_last_key_pressed = 'CR' b:undo_ftplugin = 'execute "tunmap "' diff --git a/plugin/fzy.vim b/plugin/fzy.vim index 4a19f17..aff2f78 100644 --- a/plugin/fzy.vim +++ b/plugin/fzy.vim @@ -10,28 +10,28 @@ vim9script import autoload '../autoload/fzy.vim' -command -nargs=? -complete=dir FzyFind fzy.Find(empty() ? getcwd() : , 'edit', '') -command -nargs=? -complete=dir FzyFindSplit fzy.Find(empty() ? getcwd() : , 'split', ) +command -nargs=? -complete=dir FzyFind fzy.Find(empty() ? getcwd() : , '') +command -nargs=? -complete=dir FzyFindSplit fzy.Find(empty() ? getcwd() : , ) -command -nargs=+ -complete=file FzyGrep fzy.Grep('buffer', '', ) -command -nargs=+ -complete=file FzyGrepSplit fzy.Grep('sbuffer', , ) +command -nargs=+ -complete=file FzyGrep fzy.Grep('', ) +command -nargs=+ -complete=file FzyGrepSplit fzy.Grep(, ) -command -bar -bang FzyBuffer fzy.Buffers('buffer', 0, '') -command -bar -bang FzyBufferSplit fzy.Buffers('sbuffer', 0, ) +command -bar -bang FzyBuffer fzy.Buffers(0, '') +command -bar -bang FzyBufferSplit fzy.Buffers(0, ) command -bar -bang FzyMarks fzy.Marks(0) command -bar -bang FzyMarksSplit fzy.Marks(0, ) -command -bar FzyOldfiles fzy.Oldfiles('edit', '') -command -bar FzyOldfilesSplit fzy.Oldfiles('split', ) +command -bar FzyOldfiles fzy.Oldfiles('') +command -bar FzyOldfilesSplit fzy.Oldfiles() -command -bar FzyArgs fzy.Arg('edit', false, '') -command -bar FzyArgsSplit fzy.Arg('split', false, ) +command -bar FzyArgs fzy.Arg(false, '') +command -bar FzyArgsSplit fzy.Arg(false, ) -command -bar FzyLargs fzy.Arg('edit', true, '') -command -bar FzyLargsSplit fzy.Arg('split', true, ) +command -bar FzyLargs fzy.Arg(true, '') +command -bar FzyLargsSplit fzy.Arg(true, ) -command -bar FzyTjump fzy.Tags('tjump', '') -command -bar FzyTjumpSplit fzy.Tags('stjump', ) +command -bar FzyTjump fzy.Tags() +command -bar FzyTjumpSplit fzy.Tags() -command -bar FzyHelp fzy.Help('help', ) +command -bar FzyHelp fzy.Help()