From e91b76c51710da226a3bfb02f5a373bef0cf940b Mon Sep 17 00:00:00 2001 From: Sidharth Kshatriya Date: Sun, 16 Jan 2022 00:28:32 +0530 Subject: [PATCH] Feature to allow users to open a file in the same directory as the current file Let say you have a file open in a kakoune buffer and you are currently interested in opening another file _only_ in the _same_ directory as the current file. You could do this with the usual ` f` but that will list many files you are not interested in. Add functionality and shortcut ` F` that will list the files only in the directory of the currently displayed file. This is useful when your kakoune current directory is different from the directory of the currently displayed file. E.g. if you have /home/abc/xzy/lmn/hello.cpp open in kakoune and your current directory in kakoune is /home/abc/xyz then ` F` will list all files in /home/abc/xyz/lmn as candidates for opening. --- rc/modules/fzf-file.kak | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rc/modules/fzf-file.kak b/rc/modules/fzf-file.kak index 6b51a9c..2ebf563 100644 --- a/rc/modules/fzf-file.kak +++ b/rc/modules/fzf-file.kak @@ -4,6 +4,7 @@ hook global ModuleLoaded fzf %{ map global fzf -docstring "open file" 'f' ': require-module fzf-file; fzf-file' + map global fzf -docstring "open file in dir of currently displayed file" 'F' ': require-module fzf-file; fzf-file buffile-dir' } provide-module fzf-file %ยง @@ -31,7 +32,15 @@ Default value: bool fzf_file_preview true -define-command -hidden fzf-file %{ evaluate-commands %sh{ +define-command -hidden fzf-file -params 0..1 %{ evaluate-commands %sh{ + search_dir="." + if [ "$1" = "buffile-dir" ]; then + # dirname will return '.' if the file is non-existent. + # This value is a fail-safe default if we ever use buffile-dir functionality + # by mistake on files that do not have a directory e.g. *scratch* + search_dir=$(dirname "$kak_buffile") + fi + if [ -z "$(command -v "${kak_opt_fzf_file_command%% *}")" ]; then printf "%s\n" "echo -markup '{Information}''$kak_opt_fzf_file_command'' is not installed. Falling back to ''find'''" kak_opt_fzf_file_command="find" @@ -47,7 +56,7 @@ define-command -hidden fzf-file %{ evaluate-commands %sh{ cmd=$kak_opt_fzf_file_command ;; esac - cmd="$cmd 2>/dev/null" + cmd="cd $search_dir; $cmd 2>/dev/null" message="Open single or multiple files. : open file in new buffer. ${kak_opt_fzf_window_map:-ctrl-w}: open file in new terminal"