-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvim-anywhere.sh
More file actions
executable file
·178 lines (148 loc) · 5.39 KB
/
vim-anywhere.sh
File metadata and controls
executable file
·178 lines (148 loc) · 5.39 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
behaviour="classic"
TMP_DIR="/tmp/vim-anywhere"
DEF_FT="txt"
# char type delay in milliseconds
type_delay="6"
focus_delay="0.6"
class="GVim-Anywhere"
insertmode_start=""
config_dir="${XDG_CONFIG_HOME:-${HOME}/.config}/vim-anywhere"
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
# vim anywhere config
# vim anywhere behaviour: classic simple
# classic: the behaviour of the classic vim anywhere script, a tmp dir is created and every
# instance of vim-anywhere creates a new file inside that directory.
# simple: a simpler behaviour, every time an instance of vim-anywhere runs a tmp file is created
# with the same static name, once vim exits the tmp file is deleted.
behaviour="${behaviour}"
# the location for the tmp dir
TMP_DIR="${TMP_DIR}"
# default file type extension, we do no checking of this whatsoever, just pass your preferred file
# type extension for creating of the file, by default that is "txt" to create plain text files, but
# you may change it to whatever you want, like "md" for markdown or "py" for python, "c" for c, you
# get the idea.
DEF_FT="${DEF_FT}"
# delay in milliseconds between typing of characters, with the default of 6 milliseconds a word that
# is 5 characters long will take 30 milliseconds to be typed out.
type_delay="${type_delay}"
# the delay in seconds between gnvim being closed to the contents of the tmp file being typed out,
# this is the time you got to click into your desired text field window to ensure your text will be
# typed out in the correct place, the "0.6" default gives you slightly more than half second to
# react, also assumes that the sleep(1) utility in your system supports float values as all modern
# core unix utilities implementations do nowadays.
focus_delay="${focus_delay}"
# the class option for vim-anywhere, this is passed on to gnvim(1) which will set the terminal
# command class and title to this very same string, this should make it possible to add special
# rules for your window manager, given it has such functionality, to make the vim-anywhere terminal
# window into an "ontop" window with your desired dimensions and any other setting your window
# manager and compositor are able to apply based on the name title of the window alone.
class="${class}"
# this option controls werether or not to initiate the neovim instance of vim-anywhere in insert
# mode, leave this option empty to start in normal mode, write any text inside this to start in
# insert mode, whichever text is written is of no relevance as all that is checked is werether the
# variable is empty or not.
insertmode_start="${insertmode_start}"
__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"
TMP_FILE=""
case "$behaviour" in
classic)
TMP_FILE="${TMP_DIR}/doc-$(date +"%Y%m%d%H%M%S").${DEF_FT}"
;;
simple)
TMP_FILE="${TMP_DIR}/doc.${DEF_FT}"
;;
esac
lockfile="/tmp/vim-anywhere.lock"
vimopts=""
if [ -n "$insertmode_start" ]; then
# start vim in insert mode
vimopts="+star"
fi
clipboard_to_file () {
case "$XDG_SESSION_TYPE" in
x11)
xclip -o -selection clipboard > "$TMP_FILE"
;;
wayland)
wl-paste > "$TMP_FILE"
;;
esac
}
type_file () {
case "$XDG_SESSION_TYPE" in
x11)
xdotool type --delay "$type_delay" "$(cat $TMP_FILE)"
;;
wayland)
wtype -d "$type_delay" "$(cat $TMP_FILE)"
;;
esac
}
myname="${0##*/}"
mypid="${$}"
is_instance () {
ps ax -o'pid=,cmd=' \
| sed 's/^ *//' \
| awk \
-v pid="$1" \
-v name="$myname" \
'
BEGIN { found = 0 }
$1 == pid && $0 ~ name { found = 1 }
END { if (!found) exit 1 }
'
}
if [ -f "$lockfile" ] && is_instance "$(head $lockfile)" ; then
exit 1
else
printf '%s\n' "$mypid" > "$lockfile"
fi
if [ ! -d "$TMP_DIR" ]; then
mkdir -p "$TMP_DIR"
fi
case "$1" in
clipboard|-c|--clipboard)
shift
clipboard_to_file
;;
help|-h|--help)
printf '%s\n\t%s\n' "${myname}:" \
"nvim window from anywhere"
printf '%s\n\t%s\n' "Version:" "@VERSION@"
printf '%s\n\t%s\n' "Usage:" \
"${myname} -h | [ -c ]"
printf '%s\n\t%s\n' " -h" \
"print out this help message, 'help' and '--help' are supported too"
printf '%s\n\t%s\n' " -c" \
"copy clipboard contents to vim-anywhere tmp file"
printf '\n'
printf '%s\n\t%s\n' "Config:" "$config_file"
exit 0
;;
esac
# yes, vimopts without double quotes is intentional, that way if vimopts is an empty var gnvim will
# not try to open it as an argument that opens a nameless empty file
gnvim "--class" "$class" $vimopts "$TMP_FILE"
# don't even bother with the sleep and trying to type the TMP_FILE if it doesn't exist.
if [ -f "$TMP_FILE" ]; then
# yeh some delay for your window to recover focus...
sleep "$focus_delay"
type_file
fi
case "$behaviour" in
simple)
rm -f "$TMP_FILE"
;;
esac
rm "$lockfile"