This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vopen
executable file
·166 lines (142 loc) · 4.58 KB
/
vopen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
# vopen v1.3.0
#
# Automatically determine whether to use vim or gvim and use a single instance
# Git commit messages will use a separate vim server called GIT
#
__vopen() {
local _editor="${VOPEN_EDITOR:-vim}"
local _visual="${VOPEN_VISUAL:-gvim}"
local _default_command="${VOPEN_DEFAULT_COMMAND}"
local _reuse_command="${VOPEN_REUSE_COMMAND:--remote-send \":call foreground()<CR>\"}"
local _has_gui=false
local _use_gui=true
local _has_server=false
local _use_server="${VOPEN_USE_SERVER:-true}"
local _existing_server=''
local _has_fork=true
local _servername="${VOPEN_SERVERNAME:-VOPEN}"
local _exe
local _args
local _server_arg=""
local _files
# which executable -----------------------------------------------------------
if command -v "$_visual" >/dev/null 2>&1; then
_has_gui=true
fi
if [ -z "$DISPLAY" ] || [[ $OSTYPE == darwin* ]]; then
_has_gui=false
fi
if [ "$_has_gui" = "false" ] || [ "$VOPEN_USE_GUI" = "false" ]; then
_use_gui=false
fi
# Override editor if env var provides, otherwise use vim/gvim
if [ "$_use_gui" = true ] && [ "$_has_gui" = true ]; then
_exe="$_visual"
else
_exe="$_editor"
fi
if [[ "$_exe" = *"vim" ]] || [[ "$_exe" = "nvr" ]]; then
_has_server=true
fi
if [[ "$_exe" == *"nvim"* ]] || [ "$_exe" = "nyaovim" ] ; then
_has_server=false
_use_server=false
_has_fork=false
fi
# --------------------------------------------------------------------------
# arg parsing
# --------------------------------------------------------------------------
_args=""
while [[ "$#" -gt 0 ]] && [[ ."$1" = .--* ]]; do
case "$1" in
"--") # file separator
break # break out of while loop, continue to files
;;
"--version")
# pass through nvr to nvim
[[ "$_exe" = "nvr" ]] && _exe="nvim"
# vim ignores all else if version is specified, only need to send that
eval "$_exe --version"
exit # stop processing vopen
break # known dead code path
;;
"--nofork") # never use server if nofork
_use_server=false
if [ "$_has_fork" = "true" ]; then
_args="$_args --nofork"
fi
;;
"--noserver") # don't use server at all, useful for sudoeditor
_use_server=false
;;
"--servername") # custom servername
shift # get next arg
_servername="$1"
;;
"--"*) # take any other args as they are
_args="$_args $1"
;;
esac
shift
done
# --------------------------------------------------------------------------
# file parsing
# If there are still _args after the above...
# --------------------------------------------------------------------------
_files=""
while [[ "$#" -gt 0 ]]; do
_files="$_files \"$1\""
case "$1" in
*"MERGE_MSG"|*"COMMIT_EDITMSG")
_use_server=false
;;
"/etc/sudoers")
echo "$0"
;;
esac
shift
done
# --------------------------------------------------------------------------
# existing server
# --------------------------------------------------------------------------
if [ "$_servername" ] \
&& [ "$_has_server" = true ] \
&& [ "$_use_server" = true ]; then
# always exists for nvr if you have NVIM_LISTEN_ADDRESS
_existing_server=$( $_exe --serverlist | grep "$_servername" 2>/dev/null )
_server_arg="--servername $_servername"
# reuse server if existing server
if [ "$_existing_server" != "" ]; then
if [ "$_files" = "" ]; then
_args="$_args $_reuse_command"
else
_args="$_args --remote-silent"
fi
# new server
else
if [ "$_files" = "" ]; then
_args="$_args $_default_command"
elif [[ "$_exe" = "nvr" ]]; then
_args="$_args $_reuse_command"
fi
fi
fi
# --------------------------------------------------------------------------
# execute
# --------------------------------------------------------------------------
if [ -n "$VOPEN_DEBUG" ]; then
echo "_servername: ${_servername}"
echo "_server_arg: ${_server_arg}"
echo "_reuse_command: ${_reuse_command}"
echo "_existing_server: ${_existing_server}"
echo "_default_command: ${_default_command}"
echo "_exe: ${_exe}"
echo "_args: ${_args}"
echo "_files: ${_files}"
echo "$_exe $_server_arg $_args $_files"
else
eval "$_exe $_server_arg $_args $_files"
fi
}
__vopen "$@"