-
Notifications
You must be signed in to change notification settings - Fork 0
/
is-vim
executable file
·76 lines (66 loc) · 1.92 KB
/
is-vim
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
#! /bin/sh
# Usage: is_vim.sh <tty>
# tty: Specify tty to check for vim process.
set -x
tty="$1"
alias /='eval'
list_length() {
local index=0
local item=
for item do
: $((index += 1))
done
printf "%s" $index
}
# Construct process tree.
# children=
# busybox does not support -t
# pids="$(ps -o pid= -t "$tty")"
pids="$(ps -o pid=)"
pid_list=
for pid in $pids; do
pid_list="${pid_list:+"${pid_list}|"}$pid"
done
children_build() {
local children_name=
while read -r pid ppid
do
[ ! -z "$pid" ] && [ "$pid" -ne "$ppid" ] &&
{
/ "children_name=\"children_$ppid\""
# / "children_$ppid=\"$pid\""
/ "children_name=\"${children_name:+\"${children_name}|\"}$pid\""
} || continue
done < <(ps -Ao pid=,ppid=)
}
children_build
# Get all descendant pids of processes in $tty with BFS
query_relative() {
local pid index=0
local ifs="$IFS"
local children_name=
local IFS=$'|'
for pid in $pid_list
# while [ "$(list_length pids)" -gt "$index" ]
do
[ "$(IFS=$'|' list_length $pid_list)" -gt "$index" ] || break
# pid=${pids[index++]}
: $((index += 1))
printf '%s %s\n' '$pid' "$pid" > "$TTY"
/ "children_name=\"children_$pid\""
/ "[ ! -z \"\${children_name+x}\" ]" || continue
# / "child_value=\"\$${children_$pid-}\""
/ "child_value=\"\$${children_name-}\""
printf '%s %s\n' '$child_value' "$child_value" > "$TTY"
# / "pid_list=\"${pid_list:+\"${pid_list}|\"}\$${children_$pid-}\""
/ "pid_list=\"${pid_list:+\"${pid_list}|\"}$child_value\""
done
}
query_relative
# Check whether any child pid_list are vim
# state -> stat, -p -> ''
# ps -o state=,comm= -p "$pid_list" | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'
ps -o pid=,stat=,comm= | egrep "^(PID| *$pid_list )" | awk '{print $(NF - 1) " " $NF}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'
set +x
exit $?
# [ is_vim does not detect vim running in a subshell #295 ](https://github.com/christoomey/vim-tmux-navigator/issues/295)