Skip to content

Commit

Permalink
Add ability to split with a keybinding
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
saccarosium committed Sep 25, 2024
1 parent 8ba5d59 commit c4f525e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
82 changes: 46 additions & 36 deletions autoload/fzy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ const findcmd: list<string> =<< 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
Expand Down Expand Up @@ -121,40 +133,44 @@ def Opts(title: string, space: bool = false): dict<any>
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
const cmd: string = bang ? "g`" : "`"
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<any> = 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
Expand Down Expand Up @@ -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
Expand All @@ -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<any> = 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<string> = 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<string> = 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<any>): 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<string>)
Expand Down
3 changes: 3 additions & 0 deletions ftplugin/fzy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ vim9script
# ==============================================================================

tnoremap <silent> <buffer> <c-c> <c-w>:<c-u>call fzy#Stop()<cr>
tnoremap <silent> <buffer> <c-v> <cr><c-w>:<c-u>let g:fzy_last_key_pressed = 'C-v'<cr>
tnoremap <silent> <buffer> <c-s> <cr><c-w>:<c-u>let g:fzy_last_key_pressed = 'C-s'<cr>
tnoremap <silent> <buffer> <cr> <cr><c-w>:<c-u>let g:fzy_last_key_pressed = 'CR'<cr>
b:undo_ftplugin = 'execute "tunmap <buffer> <c-c>"'
30 changes: 15 additions & 15 deletions plugin/fzy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ vim9script

import autoload '../autoload/fzy.vim'

command -nargs=? -complete=dir FzyFind fzy.Find(empty(<q-args>) ? getcwd() : <q-args>, 'edit', '')
command -nargs=? -complete=dir FzyFindSplit fzy.Find(empty(<q-args>) ? getcwd() : <q-args>, 'split', <q-mods>)
command -nargs=? -complete=dir FzyFind fzy.Find(empty(<q-args>) ? getcwd() : <q-args>, '')
command -nargs=? -complete=dir FzyFindSplit fzy.Find(empty(<q-args>) ? getcwd() : <q-args>, <q-mods>)

command -nargs=+ -complete=file FzyGrep fzy.Grep('buffer', '', <q-args>)
command -nargs=+ -complete=file FzyGrepSplit fzy.Grep('sbuffer', <q-mods>, <q-args>)
command -nargs=+ -complete=file FzyGrep fzy.Grep('', <q-args>)
command -nargs=+ -complete=file FzyGrepSplit fzy.Grep(<q-mods>, <q-args>)

command -bar -bang FzyBuffer fzy.Buffers('buffer', <bang>0, '')
command -bar -bang FzyBufferSplit fzy.Buffers('sbuffer', <bang>0, <q-mods>)
command -bar -bang FzyBuffer fzy.Buffers(<bang>0, '')
command -bar -bang FzyBufferSplit fzy.Buffers(<bang>0, <q-mods>)

command -bar -bang FzyMarks fzy.Marks(<bang>0)
command -bar -bang FzyMarksSplit fzy.Marks(<bang>0, <q-mods>)

command -bar FzyOldfiles fzy.Oldfiles('edit', '')
command -bar FzyOldfilesSplit fzy.Oldfiles('split', <q-mods>)
command -bar FzyOldfiles fzy.Oldfiles('')
command -bar FzyOldfilesSplit fzy.Oldfiles(<q-mods>)

command -bar FzyArgs fzy.Arg('edit', false, '')
command -bar FzyArgsSplit fzy.Arg('split', false, <q-mods>)
command -bar FzyArgs fzy.Arg(false, '')
command -bar FzyArgsSplit fzy.Arg(false, <q-mods>)

command -bar FzyLargs fzy.Arg('edit', true, '')
command -bar FzyLargsSplit fzy.Arg('split', true, <q-mods>)
command -bar FzyLargs fzy.Arg(true, '')
command -bar FzyLargsSplit fzy.Arg(true, <q-mods>)

command -bar FzyTjump fzy.Tags('tjump', '')
command -bar FzyTjumpSplit fzy.Tags('stjump', <q-mods>)
command -bar FzyTjump fzy.Tags()
command -bar FzyTjumpSplit fzy.Tags()

command -bar FzyHelp fzy.Help('help', <q-mods>)
command -bar FzyHelp fzy.Help(<q-mods>)

0 comments on commit c4f525e

Please sign in to comment.