Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to split entry with keybinding #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
55 changes: 38 additions & 17 deletions autoload/fzy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ const findcmd: list<string> =<< trim END
| cut -b3-
END

export const vim_cmds_per_type: dict<dict<string>> = {
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
Expand Down Expand Up @@ -121,22 +138,25 @@ 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, 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)
Expand All @@ -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<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 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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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<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, ["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)
Expand All @@ -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<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))
Start(items, funcref(Open_tag_cb, ["tag", cmd]), Opts(stl))
enddef

export def Marks(bang: bool, ...args: list<string>)
Expand Down
35 changes: 33 additions & 2 deletions doc/fzy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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': '<c-t>',
'vertical': '<c-v>',
'split': '<c-s>',
'default': '<cr>',
'stop': '<c-c>'
}
<

For how to format keycodes check out |key-codes|. For examples see
|fzy-config-examples|.

==============================================================================
CONFIGURATION EXAMPLES *fzy-config-examples*

Expand Down Expand Up @@ -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: "<m-t>",
vertical: "<m-v>",
split: "<m-s>",
stop: "<m-c>"
}
}
<
12. Change default action from `<CR>` to `<Insert>`:
>
g:fzy = {
keymaps: {default: "<Insert>"}
}
<
==============================================================================
API *fzy-api*

Expand Down
21 changes: 20 additions & 1 deletion ftplugin/fzy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ vim9script
# License: Same as Vim itself (see :h license)
# ==============================================================================

tnoremap <silent> <buffer> <c-c> <c-w>:<c-u>call fzy#Stop()<cr>
import autoload 'fzy.vim'

def AcceptEntry(type: string)
fzy.last_key_pressed = type
bufnr()->term_sendkeys("\<cr>")
enddef

const cfg = {
tab: "<c-t>",
vertical: "<c-v>",
split: "<c-s>",
default: "<cr>",
stop: "<c-c>"
}->extend(get(g:, "fzy", {})->get("keymaps", {}))

exec $"tnoremap <silent> <buffer> {cfg.tab} <scriptcmd>AcceptEntry('tab')<cr>"
exec $"tnoremap <silent> <buffer> {cfg.vertical} <scriptcmd>AcceptEntry('vertical')<cr>"
exec $"tnoremap <silent> <buffer> {cfg.split} <scriptcmd>AcceptEntry('split')<cr>"
exec $"tnoremap <silent> <buffer> {cfg.default} <scriptcmd>AcceptEntry('default')<cr>"
exec $"tnoremap <silent> <buffer> {cfg.stop} <scriptcmd>fzy.Stop()<cr>"

b:undo_ftplugin = 'execute "tunmap <buffer> <c-c>"'
2 changes: 1 addition & 1 deletion plugin/fzy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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(<q-args>) ? getcwd() : <q-args>, 'edit', '')
command -nargs=? -complete=dir FzyFindSplit fzy.Find(empty(<q-args>) ? getcwd() : <q-args>, 'split', <q-mods>)
Expand Down