-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-emacs
executable file
·149 lines (133 loc) · 4.12 KB
/
quick-emacs
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
#!/bin/bash
#
# Provide an emacs window in the quickest way possible, using wmctrl
# to focus it if necessary. This favours the use of emacsclient if
# there is a running emacs server, and intelligently takes advantage
# of newer emacs multi-tty support where present.
#
# Invoke this rather than focus-emacs-frame.
tty_emacs=fe
# This has to be optional since invocations from mutt must not be
# backgrounded. It cannot be unconditional (hence the '-if-poss'
# bit) since backgrounding is impossible if we have to fallback
# to tty emacs
create_frame=
while [ $# != 0 ]; do
case "$1" in
--bg-if-poss)
bg_if_poss=y
shift
;;
-c|--create-frame)
create_frame=$1
shift
;;
*)
break
;;
esac
done
if echo "$*" | egrep -q '(^| )-e( |$)'; then
echo "-e would only work with emacsclient, not with new-emacs; aborting." >&2
exit 1
fi
debug () {
:
echo "$*" >&2
}
if fast-X11-display; then
EMACSCLIENT_TTY=
if [ -n "$SSH_CONNECTION" ] || [[ "$DISPLAY" == localhost:* ]]; then
xauth-emacs
fi
else
DISPLAY=
EMACSCLIENT_TTY=--tty
fi
# If we're root, we don't want emacsclient to fall back to using
# a server socket for an emacs running non-root .
LOGNAME=
# Ideally we want to return control to the caller immediately, but at
# the same time wait until emacsclient completes before running
# wmctrl. But you can't background emacsclient and then re-foreground
# it so it's connected to the tty's STDIN upon fallback to the
# alternative editor, since there's no way of knowing whether the
# fallback occurred until it's too late. So we need to know in
# advance whether emacsclient will connect to the emacs server, and
# only background if it will:
if emacs_version=$( emacsclient -e 'emacs-version' 2>&1 ); then
server_up=y
emacs_version="${emacs_version%\"}"
emacs_version="${emacs_version#\"}"
else
server_up=
emacs_version=$( emacs --batch --no-site-file --eval '(message emacs-version)' 2>&1 )
fi
multi_tty_support=
case "$emacs_version" in
1[0-9].*|2[012].*)
;;
*)
multi_tty_support=y
;;
esac
focus_emacs_frame () {
# Save the current window id so we know what to switch back to from emacs.
if ! client_winid=`current-window-id`; then
echo "WARNING: couldn't establish current window; will not be able to switch back" >&2
fi
# Focus existing emacs frame
if ! focus-emacs-frame; then
echo "focus-emacs-frame failed: is the emacs server running on a different X11 display?" >&2
fi
}
do_editing () {
# Wait for editing to be completed
emacsclient $EMACSCLIENT_TTY "$@" >/dev/null
if [ -n "$client_winid" ]; then
# If we can, switch back to window from which emacsclient was invoked.
wmctrl -ia "$client_winid"
fi
}
maybe_bg () {
if [ -n "$bg_if_poss" ] && [ -n "$DISPLAY" ]; then
"$@" >/dev/null &
else
"$@"
fi
}
# Work around loathsome exo-open URI-encoding bug. Best to just upgrade
# exo-tools to >= 0.12.0 though.
# case "$1" in
# org-protocol://store-link%3F'*)
# set -- "${1/store-link%3F/store-link?}" "${@:2}"
# ;;
# esac
if [ -n "$server_up" ]; then
if [ -n "$multi_tty_support" ]; then
if [ -z "$create_frame" ]; then
# useful with org-protocol
focus_emacs_frame
fi
# newer emacsen do all the hard work for us, since its clients
# and servers can be tty- or Xorg-based!
maybe_bg emacsclient $EMACSCLIENT_TTY $create_frame "$@"
else
# Looks like we found a working server
if [ -n "$DISPLAY" ]; then
focus_emacs_frame
maybe_bg do_editing "$@"
else
# But we can't reach it, so fallback to fast-loading tty
# emacs in foreground, in favour of speed.
$tty_emacs "$@"
fi
fi
else
if [ -n "$DISPLAY" ] && [ "$USER" != root ]; then
maybe_bg new-emacs "$@"
else
# Fallback to fast-loading tty emacs in foreground, in favour of speed
$tty_emacs "$@"
fi
fi