-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe
executable file
·50 lines (41 loc) · 1.13 KB
/
e
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
#!/usr/bin/env zsh
local editor=(${=EDITOR})
local running=0
if [ -n "$MSYSTEM" ]; then
if ps -f | awk '{print $6}' \
| grep -o '[^/]*$' | grep -qFx "${editor[1]}"; then
running=1
fi
elif ps -o comm | grep -qFx "${editor[1]}"; then
running=1
fi
if [ $running -eq 1 ]; then
printf "%s\n" "${editor[1]} is already running" >&2
read -q '?Continue? [y/N] ' _ || { echo; return; }
echo
fi
if [ $# -eq 0 ] || [ -n "$MSYSTEM" ]; then
$=EDITOR "$@"
return
fi
local f needroot=0
for f in "$@"; do
# easy: file exists, we have write permissions
[ -w "$f" ] && continue
# easy: file exists, but no write permissions
[ -e "$f" ] && { needroot=1; break }
# hard: file may be in a directory that we can't inspect
local d="$f"
while expr index "$d" / >/dev/null; do
d="${d%/*}"
# NOTE: this gets weird with the root directory, not sure how to handle
[ -e "$d" ] && [ ! -w "$d" ] && { needroot=1 && break; }
done
[ $needroot -eq 1 ] && break
# easy: file just doesn't exist
done
if [ $needroot -eq 0 ]; then
$=EDITOR "$@"
else
sudo -e "$@"
fi