-
Notifications
You must be signed in to change notification settings - Fork 0
/
group
executable file
·156 lines (130 loc) · 3.64 KB
/
group
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
#!/bin/sh
#
# group
usage() {
base="$(basename "$0")"
cat >&2 << EOF
Usage:
$ $base -a | add \$group \$wid : Add \$wid to \$group.
$ $base -A | scr \$group : Add all windows on screen to \$group.
$ $base -d | del \$wid : Delete \$wid from all groups.
$ $base -t | tog \$group : Toggle \$group visibility.
$ $base -s | smart \$group : Toggle \$group visibility smartly.
$ $base -l | list : List all windows in groups.
$ $base -h | help : Show this help.
EOF
[ $# -eq 0 ] || exit "$1"
}
list_groups() {
for wid in $(lsw -a); do
wg="$(atomx GROUP "$wid")"
[ -n "$wg" ] && printf 'GROUP_%s %s %s\n' \
"$wg" "$wid" "$(atomx WM_CLASS "$wid")"
done
}
sort_groups() {
list_groups | sort
}
show_group() {
# focus window in group that is at the top
PFW="$(list_groups | grep "GROUP_$1" | tail -n 1 | cut -d\ -f 2)"
for wid in $(lsw -u); do
wg=$(atomx GROUP "$wid")
[ -z "$wg" ] && continue
[ "$wg" -eq "$1" ] && {
mapw -m "$wid"
chwso -r "$wid"
}
done
focus -w "$PFW"
# set group to active
atomx GROUP_"$1"=on "$ROOT" > /dev/null
}
hide_group() {
for wid in $(lsw); do
wg=$(atomx GROUP "$wid")
[ -z "$wg" ] && continue
[ "$wg" -eq "$1" ] && {
mapw -u "$wid"
chwb -s "$BW" -c "$INACTIVE" "$wid"
}
done
# set group to inactive
atomx GROUP_"$1"=off "$ROOT" > /dev/null
}
toggle_group() {
case $(atomx GROUP_"$1" "$ROOT") in
on) hide_group "$1" ;;
off) show_group "$1" ;;
*) atomx GROUP_"$1"=on "$ROOT" > /dev/null ;;
esac
}
del_wid() {
grp="$(atomx GROUP "$1") $(lsw -r)"
list_groups | grep -q "$grp" || {
atomx -d GROUP_"$grp" "$(lsw -r)"
}
atomx -d GROUP "$1"
}
smart_toggle_group() {
case $(atomx GROUP_"$1" "$ROOT") in
on)
windows="$(list_groups | grep "GROUP_$1" | cut -d\ -f 2)"
for wid in $windows; do
if [ "$wid" = "$PFW" ]; then
# if [ "$PFW" != "$(lsw | head -n 1)" ]; then
hide_group "$1"
return
# fi
fi
done
focus -w "$(printf '%s\n' "$windows" | tail -n 1)"
;;
off)
show_group "$1"
;;
*)
smart_toggle_group "$1"
;;
esac
}
add_group() {
atomx GROUP="$1" "$2" > /dev/null
# automatically hide window if group is hidden
case "$(atomx GROUP_"$1" "$ROOT")" in
off)
mapw -u "$2"
chwb -s "$BW" -c "$INACTIVE" "$2"
;;
*)
# set group to active
atomx GROUP_"$1"=on "$ROOT" > /dev/null
;;
esac
}
add_screen() {
for wid in $(listwindows); do
add_group "$1" "$wid"
done
# set group to active
atomx GROUP_"$1"=on "$ROOT" > /dev/null
}
main() {
. fwmrc
wmenv
wmcolours
# store group information inside root window properties
ROOT="$(lsw -r)"
case "$1" in
-a|--add|add) add_group "$2" "$3" ;;
-A|--scr|scr) add_screen "$2" ;;
-d|--del|del) del_wid "$2" ;;
-u|--hide|hide) hide_group "$2" ;;
-t|--tog|tog) toggle_group "$2" ;;
-s|--smart|smart) smart_toggle_group "$2" ;;
-l|--list|list) sort_groups ;;
-h|--help|help) usage 0 ;;
*) usage 1 ;;
esac
}
main "$@"