Skip to content

Commit

Permalink
Replace "fuzzy-snippets.vim".
Browse files Browse the repository at this point in the history
  - Replace the Vimscript implementation of the "fuzzy-snippets" shell
    script with a small wrapper over the aforementioned script.
  - Update the "fuzzy-snippets" shell script so that it can adjust its
    functionality for better Vim integration.
  - Update the help text for "fuzzy-snippets".
  • Loading branch information
damiendart committed May 4, 2024
1 parent 8b4aaa3 commit 1a21aa6
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 105 deletions.
91 changes: 91 additions & 0 deletions .vim/plugin/fuzzy-finders.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
" This file was written by Damien Dart, <[email protected]>. This is
" free and unencumbered software released into the public domain. For
" more information, please refer to the accompanying "UNLICENCE" file.

function! s:Fuzzy(command, select_cb) abort
let l:callback = {
\ 'select_cb': a:select_cb,
\ 'filename': tempname(),
\ 'window_id': win_getid(),
\ 'winrestcmd': winrestcmd(),
\ }

function l:callback.exit_cb(...) abort
call win_gotoid(l:self.window_id)
execute l:self.winrestcmd

if filereadable(l:self.filename)
try
call l:self.select_cb(readfile(l:self.filename))
catch /^Vim:Interrupt$/
catch /E684/
catch /E11/
echohl ErrorMsg
echom join(split(v:exception, ':')[1:2], ':')
echohl None
return
finally
call delete(l:self.filename)
endtry
endif

redraw!
endfunction

execute 'botright' 20 'new'

call term_start(
\ [
\ &shell,
\ &shellcmdflag,
\ "TERM=xterm-color256 " . a:command . '>' . l:callback.filename
\ ],
\ {
\ 'curwin': 1,
\ 'cwd': getcwd(),
\ 'exit_cb': l:callback.exit_cb,
\ 'term_kill': 'term',
\ }
\ )

setlocal nospell bufhidden=wipe nobuflisted nonumber
setfiletype fuzzyfinder
startinsert
endfunction

function! s:FuzzySnippets() abort
function! Handler(input) closure
if a:input[0] ==? 'ctrl-h'
execute "h :FS"

return
endif

let l:output = join(a:input[1:], "\n")

if a:input[0] ==? 'ctrl-y'
call setreg('"', l:output)

return
endif

try
let l:paste = &paste

set paste
execute "normal! a" . l:output . "\<Esc>"
finally
let &paste = l:paste
endtry
endfunction

call s:Fuzzy("fuzzy-snippets --vim", funcref("Handler"))
endfunction

autocmd FileType fuzzyfinder let b:laststatus = &laststatus
\| set laststatus=0 noshowmode noruler
\| autocmd BufLeave <buffer> set showmode ruler
\| let &laststatus = b:laststatus
\| autocmd WinLeave <buffer> close!

command! FS call s:FuzzySnippets()
83 changes: 0 additions & 83 deletions .vim/plugin/fuzzy-snippets.vim

This file was deleted.

80 changes: 58 additions & 22 deletions bin/fuzzy-snippets
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/bin/sh
#
# A simple fzf-powered snippet browser and selector script.
#
# See also "$HOME/.vim/plugin/fuzzy-snippets.vim".
# A snippet browser and selector.
#
# This file was written by Damien Dart, <[email protected]>. This is
# free and unencumbered software released into the public domain. For
Expand All @@ -12,42 +10,53 @@ set -e

help() {
cat << HELP
Usage: $(basename "$0") [FLAGS] [INITIAL-QUERY]
A snippet browser and selector.
Opens up a fzf-powered snippet browser.
Usage: $(basename "$0") [FLAGS] [INITIAL-QUERY]
Requires fzf, ripgrep, snippet-placeholder, and xsel, and that the
SNIPPET_PATH environment variable is set to a colon-seperated list of
directories containing files to use as snippets. If the GNU version of
getopt is available it will be used to parse command-line arguments,
adding support for grouped short options and other niceties.
directories containing files to use as snippets.
Placeholder content in snippets are expanded using snippet-placeholder.
For more information, see <https://github.com/damiendart/snippets>.
If the GNU version of getopt is available it will be used to preprocess
command-line arguments, enabling support for grouped short options and
other niceties.
FLAGS:
-h, --help
Displays this help text and exits.
Display this help text and exits.
-l, --list
Prints out a list of available snippet files and exits.
Print out a list of available snippet files and exits.
FZF COMMANDS:
--vim
Adjust the application's functionality for better Vim integration.
For more information, search for ":FS" in Vim's help.
ACTIONS:
ENTER
Pass the selected snippet through snippet-placeholder and copy the
output to the clipboard.
Copy the selected snippet to the clipboard.
CTRL-H
Display this help text and exits.
CTRL-P
Pass the selected snippet through snippet-placeholder and print the
output to standard input.
Print the selected snippet to standard output.
HELP
}

LIST_FILES=0
VIM_MODE=0

# Using the GNU version of getopt to parse command-line arguments adds
# support for grouped short options and other niceties.
if test "$(getopt --test >/dev/null 2>&1 && echo "$?" || echo "$?")" -eq 4; then
if ! ARGS=$(getopt -l help,list -n "$(basename "$0")" -o hl -- "$@"); then
if ! ARGS=$(getopt -l help,list,vim -n "$(basename "$0")" -o hl -- "$@"); then
printf 'See "%s --help" for available options\n' "$(basename "$0")" >&2
exit 2
fi
Expand All @@ -57,9 +66,10 @@ fi

if ! type 'fzf' 1>/dev/null 2>&1 || \
! type 'rg' 1>/dev/null 2>&1 || \
! type 'snippet-placeholder' 1>/dev/null 2>&1 || \
! type 'xsel' 1>/dev/null 2>&1;
then
printf 'ERROR: "%s" requires fzf, ripgrep, and xsel\n' "$(basename "$0")" >&2
printf 'ERROR: "%s" requires fzf, ripgrep, snippet-placeholder, and xsel\n' "$(basename "$0")" >&2
exit 2
fi

Expand All @@ -82,6 +92,10 @@ while :; do
shift
break
;;
--vim)
VIM_MODE=1
shift
;;
--)
shift
break
Expand Down Expand Up @@ -118,30 +132,52 @@ if [ "$LIST_FILES" = 1 ]; then
exit
fi

FZF_EXPECT='ctrl-h,ctrl-p'
FZF_LABEL='CTRL+H: Help ╱ CTRL+P: Print snippet ╱ ENTER: Copy snippet'

if [ "$VIM_MODE" = 1 ]; then
FZF_EXPECT='ctrl-h,ctrl-y'
FZF_LABEL='CTRL+H: help ╱ CTRL+Y: yank ╱ ENTER: insert'
fi

# CTRL+Z process suspension is suppressed as it doesn't work properly
# (see <https://github.com/junegunn/fzf/issues/2289>).
FZF_OUTPUT=$(\
echo "$SNIPPET_FILES" | sort | fzf \
--bind='ctrl-z:ignore' \
--border-label="$FZF_LABEL" \
--border-label-pos='-3:bottom' \
--delimiter='/' \
--expect='ctrl-p' \
--header='CTRL+P: Print snippet ╱ ENTER: Copy snippet' \
--with-nth='-1' \
--expect="$FZF_EXPECT" \
--info='inline-right' \
--preview='cat {} 2>/dev/null' \
--prompt='--%<-- ' \
--query="$1" \
--scheme='path' \
--with-nth='-1' \
)
SELECTED_ACTION=$(echo "$FZF_OUTPUT" | head -1)
SELECTED_FILE=$(echo "$FZF_OUTPUT" | tail -1)

case $SELECTED_ACTION in
ctrl-h)
if [ "$VIM_MODE" = 1 ]; then
echo "$SELECTED_ACTION"
else
help
fi
;;
ctrl-p)
snippet-placeholder "$SELECTED_FILE"
;;
*)
SNIPPET="$(snippet-placeholder "$SELECTED_FILE")"
printf "%s" "$SNIPPET" | xsel -i --clipboard
echo "[✔] Copied snippet \"$SELECTED_FILE\" to the clipboard!"

if [ "$VIM_MODE" = 1 ]; then
printf "%s\n%s" "$SELECTED_ACTION" "$SNIPPET"
else
printf "%s" "$SNIPPET" | xsel -i --clipboard
echo "[✔] Copied snippet \"$SELECTED_FILE\" to the clipboard!"
fi
;;
esac

0 comments on commit 1a21aa6

Please sign in to comment.