75504364edce000c054495f1b394bea8ed51ea45

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #1022 from derekwyatt/master

A plugin that makes it easier to interact with the (single) running instance of gvim

Diff

  1diff --git a/plugins/vim-interaction/README.md b/plugins/vim-interaction/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..681648018e69ccb077909e9fcd6346be9d55d4d1
  4--- /dev/null
  5+++ b/plugins/vim-interaction/README.md
  6@@ -0,0 +1,82 @@
  7+# Vim Interaction #
  8+
  9+The plugin presents a function called `callvim` whose usage is:
 10+
 11+    usage: callvim [-b cmd] [-a cmd] [file ... fileN]
 12+    
 13+      -b cmd     Run this command in GVIM before editing the first file
 14+      -a cmd     Run this command in GVIM after editing the first file
 15+      file       The file to edit
 16+      ... fileN  The other files to add to the argslist
 17+
 18+## Rationale ##
 19+
 20+The idea for this script is to give you some decent interaction with a running
 21+GVim session.  Normally you'll be running around your filesystem doing any
 22+number of amazing things and you'll need to load some files into GVim for
 23+editing, inspecting, destruction, or other bits of mayhem.  This script lets you
 24+do that.
 25+
 26+## Aliases ##
 27+
 28+There are a few aliases presented as well:
 29+
 30+* `v` A shorthand for `callvim`
 31+* `vvsp` Edits the passed in file but first makes a vertical split
 32+* `vhsp` Edits the passed in file but first makes a horizontal split
 33+
 34+## Post Callout ##
 35+
 36+At the end of the `callvim` function we invoke the `postCallVim` function if it
 37+exists.  If you're using MacVim, for example, you could define a function that
 38+brings window focus to it after the file is loaded:
 39+
 40+    function postCallVim
 41+    {
 42+      osascript -e 'tell application "MacVim" to activate'
 43+    }
 44+
 45+This'll be different depending on your OS / Window Manager.
 46+
 47+## Examples ##
 48+
 49+This will load `/tmp/myfile.scala` into the running GVim session:
 50+
 51+    > v /tmp/myfile.scala
 52+
 53+This will load it after first doing a vertical split:
 54+
 55+    > vvsp /tmp/myfile.scala
 56+    or
 57+    > v -b':vsp' /tmp/myfile.scala
 58+
 59+This will load it after doing a horizontal split, then moving to the bottom of
 60+the file:
 61+
 62+    > vhsp -aG /tmp/myfile.scala
 63+    or
 64+    > v -b':sp' -aG /tmp/myfile.scala
 65+
 66+This will load the file and then copy the first line to the end (Why you would
 67+ever want to do this... I dunno):
 68+
 69+    > v -a':1t$' /tmp/myfile.scala
 70+
 71+And this will load all of the `*.txt` files into the args list:
 72+
 73+    > v *.txt
 74+
 75+If you want to load files into areas that are already split, use one of the
 76+aliases for that:
 77+
 78+    # Do a ':wincmd h' first
 79+    > vh /tmp/myfile.scala
 80+
 81+    # Do a ':wincmd j' first
 82+    > vj /tmp/myfile.scala
 83+
 84+    # Do a ':wincmd k' first
 85+    > vk /tmp/myfile.scala
 86+
 87+    # Do a ':wincmd l' first
 88+    > vl /tmp/myfile.scala
 89diff --git a/plugins/vim-interaction/vim-interaction.plugin.zsh b/plugins/vim-interaction/vim-interaction.plugin.zsh
 90new file mode 100644
 91index 0000000000000000000000000000000000000000..5142f1f9b586607ec17355c33161ef99c9c486f8
 92--- /dev/null
 93+++ b/plugins/vim-interaction/vim-interaction.plugin.zsh
 94@@ -0,0 +1,72 @@
 95+#
 96+# See README.md
 97+#
 98+# Derek Wyatt (derek@{myfirstnamemylastname}.org
 99+# 
100+
101+function resolveFile
102+{
103+  if [ -f "$1" ]; then
104+    echo $(readlink -f "$1")
105+  elif [[ "${1#/}" == "$1" ]]; then
106+    echo "$(pwd)/$1"
107+  else
108+    echo $1
109+  fi
110+}
111+
112+function callvim
113+{
114+  if [[ $# == 0 ]]; then
115+    cat <<EOH
116+usage: callvim [-b cmd] [-a cmd] [file ... fileN]
117+
118+  -b cmd     Run this command in GVIM before editing the first file
119+  -a cmd     Run this command in GVIM after editing the first file
120+  file       The file to edit
121+  ... fileN  The other files to add to the argslist
122+EOH
123+    return 0
124+  fi
125+
126+  local cmd=""
127+  local before="<esc>"
128+  local after=""
129+  while getopts ":b:a:" option
130+  do
131+    case $option in
132+      a) after="$OPTARG"
133+         ;;
134+      b) before="$OPTARG"
135+         ;;
136+    esac
137+  done
138+  shift $((OPTIND-1))
139+  if [[ ${after#:} != $after && ${after%<cr>} == $after ]]; then
140+    after="$after<cr>"
141+  fi
142+  if [[ ${before#:} != $before && ${before%<cr>} == $before ]]; then
143+    before="$before<cr>"
144+  fi
145+  local files=""
146+  for f in $@
147+  do
148+    files="$files $(resolveFile $f)"
149+  done
150+  if [[ -n $files ]]; then
151+    files=':args! '"$files<cr>"
152+  fi
153+  cmd="$before$files$after"
154+  gvim --remote-send "$cmd"
155+  if typeset -f postCallVim > /dev/null; then
156+    postCallVim
157+  fi
158+}
159+
160+alias v=callvim
161+alias vvsp="callvim -b':vsp'"
162+alias vhsp="callvim -b':sp'"
163+alias vk="callvim -b':wincmd k'"
164+alias vj="callvim -b':wincmd j'"
165+alias vl="callvim -b':wincmd l'"
166+alias vh="callvim -b':wincmd h'"