This repository was archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspaces_emacs.sh
More file actions
343 lines (302 loc) · 10.3 KB
/
workspaces_emacs.sh
File metadata and controls
343 lines (302 loc) · 10.3 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#-------------------------------------------------------------------------
# workspaces_emacs.sh
#
# Functions to extend the workspaces stuff to make using emacs server
# and client easier. The basic idea is that each workspace runs its own
# emacs server. When all instances of a workspace are shutdown the
# associated emacs server is also shutdown.
#
# workspaces_emacs registers itself with the workspaces on_exit hook so
# that the shutdown check is always checked on exiting a workspace.
#
# Note on hackiness: I don't know how portable any of this is across
# other platforms.
#
# Some issues:
# The emacsclient -a "" doesn't seem to behave the way I would expect
# when a server name is specified. For example, I want to run a
# command like:
#
# emacsclient -c -n -a "" -s myserver
#
# What I would expect this to do is to try and connect to the emacs
# server daemon named "myserver" and if it is not running spawn a
# server with that name and connect to it. Instead, if the server is
# not running it spawns a randomly named server and then fails to
# connect to it because it has the wrong name.
#
# The wkspe_shutdown function needs work. It always prompts the user that
# there are "active clients". Searching google there are various discussions
# on what this means and how to get around it. Need to look into it.
#
#-------------------------------------------------------------------------
mbimport workspaces
mbimport prompts
mbimport logging
# name of emacs server when not in workspace
export GLOBAL_EMACS_SERVER_ID="${WORKSPACES_TMP_DIR}/eserver_global"
#------------------------------
# validate_ws - If a workspace is loaded then do nothing, otherwise
# set the WORKSPACE_ID to a global value
#------------------------------
_wkspe_get_server_name(){
if wksps_is_loaded ; then
echo "${WORKSPACE_TMP_DIR}/eserver_${WORKSPACE_ID}"
else
echo "${WORKSPACES_TMP_DIR}/eserver_global"
fi
return 0
}
#------------------------------
# Check if the running emacs server has a frame (ie. a window or terminal in non-emacs speak).
# Note: for some reason the frame-list returns 1 if there are no frames not 0. Is this
# a bug or a property of the emacs server?
#------------------------------
_wkspe_has_frame(){
local server_id=$(_wkspe_get_server_name)
local res=$(emacsclient -s "$server_id" -e '(let ((nfrms (length (frame-list)))) (if (<= nfrms 1) (message "NO")))' 2>&1)
if [[ "$res" =~ "NO" ]] ; then
return 1
fi
return 0
}
#------------------------------
# Start up the emacs server for the workspace
#------------------------------
_wkspe_run_server(){
local server_id=$(_wkspe_get_server_name)
# echo "RUNNING $server_id"
emacs --daemon="$server_id"
}
_wkspe_server_get_process(){
local server_id=$(_wkspe_get_server_name)
local match="daemon=$server_id"
local res=$(pgrep -a emacs | grep "$match" | awk '{print $1}')
echo "$res"
}
_wkspe_server_has_process(){
local res=$(_wkspe_server_get_process)
[ "$res" != "" ] && return 0
return 1
}
_wkspe_server_has_socket(){
local server_id=$(_wkspe_get_server_name)
[ -S "$server_id" ] && return 0
return 1
}
_wkspe_server_del_socket(){
local server_id=$(_wkspe_get_server_name)
if [ -S "$server_id" ] ; then
rm -f "$server_id"
fi
}
_wkspe_server_kill_process(){
local res=$(_wkspe_server_get_process)
[ "$res" == "" ] && return 0
kill "$res"
res=$(_wkspe_server_get_process)
if [ "$res" != "" ] ; then
log_error "Failed to delete emacs server process $res"
fi
}
#------------------------------
# Check if the workspace emacs server is running
#------------------------------
_wkspe_server_isrunning(){
local server_id=$(_wkspe_get_server_name)
if _wkspe_server_has_process && _wkspe_server_has_socket ; then
return 0
fi
if _wkspe_server_has_socket ; then
log_error "Emacs server ($server_id) has a socket but no process"
log_error "Deleting socket for server ($server_id)"
_wkspe_server_del_socket
fi
if _wkspe_server_has_process ; then
log_error "Emacs server ($server_id) has a process but no socket"
log_error "Killing process for server ($server_id)"
_wkspe_server_kill_process
fi
return 1
}
#------------------------------
# If a server is running then do return true (0).
# If in workspace and no server is running then run one.
# If not in workspace then prompt user.
# Returns the true (0) if a server is running.
#------------------------------
_wkspe_validate_running_server(){
local server_id=$(_wkspe_get_server_name)
_wkspe_server_isrunning && return 0
local server_id=$(_wkspe_get_server_name)
if [ "$server_id" == "$GLOBAL_EMACS_SERVER_ID" ] ; then
local res=$(prompt_yesno "Not in workspace. Start global emacs server: \"$server_id\" (y/N)?" n)
[ "$res" == "n" ] && return 1
fi
_wkspe_run_server
_wkspe_server_isrunning && return 0
log_error "Failed to run emacs server: \"$server_id\""
return 1
}
#------------------------------
# Intelligently shutdown all emacs servers.
#------------------------------
#wkspe_shutdown_all(){
# local servers=$(ps aux | grep "emacs --daemon="
# local server_id=$(_wkspe_get_server_name)
# ! _wkspe_server_isrunning && return 0
# # Always exit in terminal mode
# emacsclient -nw -s "$server_id" -e '(save-buffers-kill-emacs)'
#}
#------------------------------
# Intelligently shutdown the workspace emacs server (if it is running).
#------------------------------
wkspe_shutdown(){
local server_id=$(_wkspe_get_server_name)
local hasproc=$(_wkspe_server_has_process)
local hassocket=$(_wkspe_server_has_socket)
if _wkspe_server_has_process && _wkspe_server_has_socket ; then
# Always exit in terminal mode
emacsclient -nw -s "$server_id" -e '(save-buffers-kill-emacs)'
return 0
fi
if _wkspe_server_has_process ; then
_wkspe_server_kill_process
fi
if _wkspe_server_has_socket ; then
_wkspe_server_del_socket
fi
# if _wkspe_has_frame; then
# emacsclient -s "$server_id" -e '(save-buffers-kill-emacs)'
# else
# emacsclient -nw -s "$server_id" -e '(save-buffers-kill-emacs)'
# fi
}
#------------------------------
# On exit of every workspace call this function. If there are no more
# workspaces shells running then shutdown the workspace emacs server
# (if it is running).
#------------------------------
wkspe_on_exit(){
# if no more active pids for the current workspace
# then shutdown emacs server.
local npids=$(wksps_num_active_pids)
if [ $npids -eq 0 ] && _wkspe_server_isrunning ; then
log_info "Shutting down emacs server for workspace..."
wkspe_shutdown
fi
}
#------------------------------
# On enter of every workspace call this function. Simply
# sets up the EDITOR variable.
#------------------------------
wkspe_on_enter(){
local npids=$(wksps_num_active_pids)
local server_id=$(_wkspe_get_server_name)
# Check for a valid emacs server
if _wkspe_server_isrunning ; then
# If this was the first process then a previous server wasn't shutdown
# properly. But still seems to be valid
if [ $npids -eq 1 ]; then
log_warn "It appears that an Emacs server is already running for this workspace"
fi
fi
export EDITOR=wkspe_emacsclient
}
#------------------------------
# My emacs edit command
#
#------------------------------
wkspe_emacsclient(){
! _wkspe_validate_running_server && return 1 # make sure emacs server is running
local server_id=$(_wkspe_get_server_name)
local nw=""
if [ "$1" == "-nw" ] || [ "$1" == "-t" ] ; then
nw="Yes"
shift 1
fi
if [ "$DISPLAY" == "" ] || [ "$nw" != "" ] ; then
emacsclient -nw -s "$server_id" $@
elif _wkspe_has_frame ; then
emacsclient -n -s "$server_id" $@
else
emacsclient -c -n -s "$server_id" $@
fi
}
#------------------------------
# My emacs edit command that is terminal only
#------------------------------
wkspe_emacsclient_nw(){
wkspe_emacsclient -nw $@
}
#------------------------------
# Emacs workspace - run ediff from the command-line
#------------------------------
wkspe_emacsclient_ediff(){
local server_id=$(_wkspe_get_server_name)
local nw=""
if [ "$1" == "-nw" ] || [ "$1" == "-t" ] ; then
nw="Yes"
shift 1
fi
if [ "$1" == "" ] || [ "$2" == "" ] ; then
echo "error: no files specified"
return 1
fi
local cmd="(ediff-files \"$1\" \"$2\")"
# make sure emacs server is running
! _wkspe_validate_running_server && return 1
# Now run the ediff
if [ "$DISPLAY" == "" ] || [ "$nw" != "" ] ; then
emacsclient -nw -s "$server_id" -e "$cmd"
elif _wkspe_has_frame ; then
emacsclient -n -s "$server_id" -e "$cmd"
else
emacsclient -c -n -s "$server_id" -e "$cmd"
fi
}
wkspe_emacsclient_ediff_nw(){
wkspe_emacsclient_ediff -nw $@
}
_wkspe_emacsclient_ediff_autocomplete () {
local suggestions
local cur cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [ $COMP_CWORD -eq 1 ] || [ $COMP_CWORD -eq 2 ] ; then
suggestions=$(ls)
fi
COMPREPLY=( $(compgen -W "${suggestions}" -- ${cur}) )
return 0
}
#-----------------------------------------------------------------------
# Export the emacs server id
#-----------------------------------------------------------------------
export WORKSPACE_EMACS_SERVER_ID=$(_wkspe_get_server_name)
#-----------------------------------------------------------------------
# Main - Setup the workspace callback for on_enter and on_exit.
#-----------------------------------------------------------------------
wksps_hook_on_enter "wkspe_on_enter"
wksps_hook_on_exit "wkspe_on_exit"
#---------------------------------------------------------------
# Register the completion functions
#---------------------------------------------------------------
complete -F _wkspe_emacsclient_ediff_autocomplete wkspe_emacsclient_ediff
complete -F _wkspe_emacsclient_ediff_autocomplete wkspe_emacsclient_ediff_nw
export -f _wkspe_get_server_name
export -f _wkspe_has_frame
export -f _wkspe_run_server
export -f _wkspe_server_get_process
export -f _wkspe_server_has_process
export -f _wkspe_server_has_socket
export -f _wkspe_server_del_socket
export -f _wkspe_server_kill_process
export -f _wkspe_server_isrunning
export -f _wkspe_validate_running_server
export -f wkspe_shutdown
export -f wkspe_on_exit
export -f wkspe_on_enter
export -f wkspe_emacsclient
export -f wkspe_emacsclient_nw
export -f wkspe_emacsclient_ediff
export -f wkspe_emacsclient_ediff_nw