-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnvim.sh
More file actions
executable file
·153 lines (133 loc) · 3.79 KB
/
gnvim.sh
File metadata and controls
executable file
·153 lines (133 loc) · 3.79 KB
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
#!/bin/sh
term_cmd="x-terminal-emulator"
no_tmux=""
nv_cmd="/usr/bin/nvim"
class="gnvim"
opencmd="edit"
config_dir="${XDG_CONFIG_HOME:-${HOME}/.config}/gnvim"
config_file="${config_dir}/configrc"
if [ ! -f "$config_file" ]; then
if [ ! -d "$config_dir" ]; then
mkdir -p "$config_dir"
fi
cat << __HEREDOC__ > "$config_file"
# vim: ft=sh
# gnvim wrapper config file
# terminal command
term_cmd="${term_cmd}"
# neovim command
nv_cmd="${nv_cmd}"
# open command
# used internally by neovim to open a file if not using tmux
# supported values: edit, tabedit, split, vsplit, drop
opencmd="${opencmd}"
# gnvim class
class="${class}"
# do not use tmux for regular operation
# setting this to anything will take effect
no_tmux="${no_tmux}"
__HEREDOC__
fi
# loading the config here means the default config is already present and the user is not able to
# overwrite anything defined after this point.
. "$config_file"
myname="${0##*/}"
# cuz $UID is not POSIX ¯\_(ツ)_/¯
# but it may be defined in the environment
if [ -z "$UID" ]; then
# result of: id -u $USER
UserID=$(id -u "$USER")
else
UserID="$UID"
fi
case "$1" in
class|-c|--class)
shift
class="$1"
shift
;;
help|-h|--help)
printf '%s\n\t%s\n' "${myname}:" \
"nvim terminal wrapper"
printf '%s\n\t%s\n' "Version:" "@VERSION@"
printf '%s\n\t%s\n' "Usage:" \
"${myname} -h | [ --class NAME ] [ nvim flags and files ]"
printf '%s\n\t%s\n' " -h" \
"print out this help message, 'help' and '--help' are supported too"
printf '%s\n\t%s\n\t%s%s\n' \
" --class NAME" \
"sets terminal '-name' and '-T' flags to NAME, sets tmux-nvim" \
"--session-id to NAME, sets runfile as " \
"'/var/run/user/ID/NAME/NAME.lock'"
printf '\n'
printf '%s\n\t%s\n' "Config:" "$config_file"
exit 0
;;
esac
run_dir="${XDG_RUNTIME_DIR:-/run/user/${UserID}}/${class}"
run_file="${run_dir}/${class}.lock"
p_file="${run_dir}/${class}.pipe"
# unix command line like booleans
# Type: int
# value: 0
b_true=0
# Type: int
# value: 1
b_false=1
# check if directory is empty
# return type: int
# return values:
# empty b_true
# non empty b_false
# taken from:
# https://superuser.com/questions/352289/bash-scripting-test-for-empty-directory/830902#830902
is_empty() {
test -e "$1/"* 2>/dev/null
case $? in
1) return "$b_true" ;;
*) return "$b_false" ;;
esac
}
cleanup() {
if [ -r "$run_file" ]; then
rm "$run_file" 2>/dev/null
fi
if is_empty "$run_dir"; then
rmdir "$run_dir" 2>/dev/null
fi
}
trap cleanup EXIT
if [ -f "$run_file" ]; then
hasterm="$b_true"
date +"[%F %T]: ${myname} called." >> "$run_file"
else
hasterm="$b_false"
if [ ! -d "$run_dir" ]; then
mkdir -p "$run_dir"
fi
date +"[%F %T]: ${myname} called." > "$run_file"
fi
if [ ! -f "$run_file" ]; then
exit 1
fi
if [ "$hasterm" -eq "$b_true" ]; then
if [ -z "$no_tmux" ]; then
exec tmux-nvim --session-id "$class" "$@"
else
case "$opencmd" in
edit|tabedit|split|vsplit|drop)
: # do nothing, we just accept those values
;;
*) # fallback to default if not a supported command
opencmd="edit"
;;
esac
exec nvim --server "$p_file" --remote-send "<C-\\><C-N>:${opencmd} ${*}<CR>"
fi
else
if [ -z "$no_tmux" ]; then
$term_cmd -name "$class" -title "$class" -e tmux-nvim --session-id "$class" "$@"
else
$term_cmd -name "$class" -title "$class" -e $nv_cmd --listen "$p_file" "$@"
fi
fi