-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocus-emacs-frame
executable file
·82 lines (66 loc) · 2.41 KB
/
focus-emacs-frame
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
#!/bin/bash
#
# Uses wmctrl to focus an emacs frame, preferring one on the current
# desktop if it exists. Do not call this directly - instead use
# quick-emacs.
LOGFILE=$HOME/.log/focus-emacs-frame.log
me=`basename $0`
_dlog () { echo "`date` $*" >> "$LOGFILE" 2>&1; }
debug () { _dlog "$*"; }
progress () { _dlog "$*"; echo "$me: $*"; }
warn () { _dlog "$*"; echo >&2 "$me: $*"; }
error () { _dlog "ERROR: $*"; echo >&2 "$me: ERROR: $*"; }
fatal () { _dlog "FATAL: $*"; echo >&2 "$me: FATAL: $*"; exit 1; }
check_prereqs () {
if [ -z "$DISPLAY" ]; then
fatal "DISPLAY environment variable was not set"
fi
if ! which wmctrl >/dev/null 2>&1; then
fatal "wmctrl not found; aborting."
fi
if ! which xdotool >/dev/null 2>&1; then
fatal "xdotool not found; aborting."
fi
}
get_desktop_for_window () {
win_id="$1"
#xdotool get_desktop_for_window $win_id
wmctrl -lpx | grep "$win_id" | awk '{print $2}'
}
main () {
host="`hostname`"
host="${host%%.*}"
check_prereqs
if ! target_window_id=$( emacs-window-id ); then
fatal "emacs-window-id failed to get target window id"
fi
debug "Window name at start: $(current-window-name)"
if ! desktop=$( get_desktop_for_window $target_window_id ); then
fatal "Failed to get desktop number for emacs window $target_window_id"
fi
if [ "$desktop" = -1 ]; then
debug "Window is sticky across all desktops"
else
if ! xdotool set_desktop $desktop; then
fatal "Failed to switch to desktop $desktop"
fi
fi
sleep=0.1
while [ $( echo "$sleep < 30" | bc ) = 1 ]; do
#xdotool windowfocus --sync $target_window_id windowraise "$target_window_id"
debug "Switching to $target_window_id"
wmctrl -ia "$target_window_id"
debug "sleeping $sleep"
sleep $sleep
sleep=$( echo "$sleep * 2" | bc )
current_window_id="$(current-window-id)"
debug "Current window id is $current_window_id"
if [ "$current_window_id" = "$target_window_id" ]; then
debug "Successfully focused $target_window_id"
debug "Window name at end: $(current-window-name)"
exit 0
fi
done
fatal "Failed to focus emacs window $target_window_id"
}
main "$@"