refactor(vim-interaction): clean up code and open gvim instance if none open (#10209)

Co-authored-by: Kevin Bader <keb@visotech.at>
This commit is contained in:
Marc Cornellà 2021-09-23 12:33:37 +02:00 committed by GitHub
parent 5b3d2b2f0c
commit d87f29f564
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,8 +4,7 @@
# Derek Wyatt (derek@{myfirstnamemylastname}.org # Derek Wyatt (derek@{myfirstnamemylastname}.org
# #
function callvim function callvim {
{
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
cat <<EOH cat <<EOH
usage: callvim [-b cmd] [-a cmd] [-n name] [file ... fileN] usage: callvim [-b cmd] [-a cmd] [-n name] [file ... fileN]
@ -19,11 +18,20 @@ EOH
return 0 return 0
fi fi
local cmd="" # Look up the newest instance or start one
local before="<esc>"
local after=""
# Look up the newest instance
local name="$(gvim --serverlist | tail -n 1)" local name="$(gvim --serverlist | tail -n 1)"
[[ -n "$name" ]] || {
# run gvim or exit if it fails
gvim || return $?
# wait for gvim instance to fully load
while name=$(gvim --serverlist) && [[ -z "$name" ]]; do
sleep 0.1
done
}
local before="<esc>" files after cmd
while getopts ":b:a:n:" option while getopts ":b:a:n:" option
do do
case $option in case $option in
@ -36,22 +44,20 @@ EOH
esac esac
done done
shift $((OPTIND-1)) shift $((OPTIND-1))
if [[ ${after#:} != $after && ${after%<cr>} == $after ]]; then
after="$after<cr>" # If before or after commands begin with : and don't end with <cr>, append it
fi [[ ${after} = :* && ${after} != *\<cr\> ]] && after+="<cr>"
if [[ ${before#:} != $before && ${before%<cr>} == $before ]]; then [[ ${before} = :* && ${before} != *\<cr\> ]] && before+="<cr>"
before="$before<cr>" # Open files passed (:A means abs path resolving symlinks, :q means quoting special chars)
fi [[ $# -gt 0 ]] && files=':args! '"${@:A:q}<cr>"
local files # Pass the built vim command to gvim
if [[ $# -gt 0 ]]; then
# absolute path of files resolving symlinks (:A) and quoting special chars (:q)
files=':args! '"${@:A:q}<cr>"
fi
cmd="$before$files$after" cmd="$before$files$after"
gvim --servername "$name" --remote-send "$cmd"
if typeset -f postCallVim > /dev/null; then # Run the gvim command
postCallVim gvim --servername "$name" --remote-send "$cmd" || return $?
fi
# Run postCallVim if defined (maybe to bring focus to gvim, see README)
(( ! $+functions[postCallVim] )) || postCallVim
} }
alias v=callvim alias v=callvim