]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/vim-interaction/vim-interaction.plugin.zsh
b73f9b4da5b2d9fe4d5c6600982901b89880f6d0
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / vim-interaction / vim-interaction.plugin.zsh
1 #
2 # See README.md
3 #
4 # Derek Wyatt (derek@{myfirstnamemylastname}.org
5
6
7 function callvim {
8   if [[ $# == 0 ]]; then
9     cat <<EOH
10 usage: callvim [-b cmd] [-a cmd] [-n name] [file ... fileN]
11
12   -b cmd     Run this command in GVIM before editing the first file
13   -a cmd     Run this command in GVIM after editing the first file
14   -n name    Name of the GVIM server to connect to
15   file       The file to edit
16   ... fileN  The other files to add to the argslist
17 EOH
18     return 0
19   fi
20
21   # Look up the newest instance or start one
22   local name="$(gvim --serverlist | tail -n 1)"
23   [[ -n "$name" ]] || {
24     # run gvim or exit if it fails
25     gvim || return $?
26
27     # wait for gvim instance to fully load
28     while name=$(gvim --serverlist) && [[ -z "$name" ]]; do
29       sleep 0.1
30     done
31   }
32
33   local before="<esc>" files after cmd
34
35   while getopts ":b:a:n:" option
36   do
37     case $option in
38       a) after="$OPTARG"
39          ;;
40       b) before="$OPTARG"
41          ;;
42       n) name="$OPTARG"
43          ;;
44     esac
45   done
46   shift $((OPTIND-1))
47
48   # If before or after commands begin with : and don't end with <cr>, append it
49   [[ ${after}  = :* && ${after}  != *\<cr\> ]] && after+="<cr>"
50   [[ ${before} = :* && ${before} != *\<cr\> ]] && before+="<cr>"
51   # Open files passed (:A means abs path resolving symlinks, :q means quoting special chars)
52   [[ $# -gt 0 ]] && files=':args! '"${@:A:q}<cr>"
53   # Pass the built vim command to gvim
54   cmd="$before$files$after"
55
56   # Run the gvim command
57   gvim --servername "$name" --remote-send "$cmd" || return $?
58
59   # Run postCallVim if defined (maybe to bring focus to gvim, see README)
60   (( ! $+functions[postCallVim] )) || postCallVim
61 }
62
63 alias v=callvim
64 alias vvsp="callvim -b':vsp'"
65 alias vhsp="callvim -b':sp'"
66 alias vk="callvim -b':wincmd k'"
67 alias vj="callvim -b':wincmd j'"
68 alias vl="callvim -b':wincmd l'"
69 alias vh="callvim -b':wincmd h'"