149
|
1 ;;; gnuserv.el --- Lisp interface code between Emacs and gnuserv
|
|
2 ;; Copyright (C) 1989-1997 Free Software Foundation, Inc.
|
|
3
|
173
|
4 ;; Version: 3.9
|
149
|
5 ;; Author: Andy Norman (ange@hplb.hpl.hp.com), originally based on server.el
|
|
6 ;; Hrvoje Niksic <hniksic@srce.hr>
|
153
|
7 ;; Maintainer: Jan Vroonhof <vroonhof@math.ethz.ch>,
|
|
8 ;; Hrvoje Niksic <hniksic@srce.hr>
|
149
|
9 ;; Keywords: environment, processes, terminals
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ;; under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
0
|
28 ;;; Synched up with: Not in FSF.
|
|
29
|
149
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; Gnuserv is run when Emacs needs to operate as a server for other
|
|
33 ;; processes. Specifically, any number of files can be attached for
|
|
34 ;; editing to a running XEmacs process using the `gnuclient' program.
|
|
35
|
|
36 ;; Use `M-x gnuserv-start' to start the server and `gnuclient files'
|
|
37 ;; to load them to XEmacs. When you are done with a buffer, press
|
|
38 ;; `C-x #' (`M-x gnuserv-edit'). You can put (gnuserv-start) to your
|
|
39 ;; .emacs, and enable `gnuclient' as your Unix "editor". When all the
|
|
40 ;; buffers for a client have been edited and exited with
|
|
41 ;; `gnuserv-edit', the client "editor" will return to the program that
|
|
42 ;; invoked it.
|
|
43
|
|
44 ;; Your editing commands and Emacs' display output go to and from the
|
|
45 ;; terminal or X display in the usual way. If you are running under
|
|
46 ;; X, a new X frame will be open for each gnuclient. If you are on a
|
|
47 ;; TTY, this TTY will be attached as a new device to the running
|
|
48 ;; XEmacs, and will be removed once you are done with the buffer.
|
|
49
|
153
|
50 ;; To evaluate a Lisp form in a running Emacs, use the `-eval'
|
|
51 ;; argument of gnuclient. To simplify this, we provide the `gnudoit'
|
|
52 ;; shell script. For example `gnudoit "(+ 2 3)"' will print `5',
|
|
53 ;; whereas `gnudoit "(gnus)"' will fire up your favorite newsreader.
|
|
54 ;; Like gnuclient, `gnudoit' requires the server to be started prior
|
|
55 ;; to using it.
|
149
|
56
|
|
57 ;; For more information you can refer to man pages of gnuclient,
|
|
58 ;; gnudoit and gnuserv, distributed with XEmacs.
|
|
59
|
|
60 ;; gnuserv.el was originally written by Andy Norman as an improvement
|
|
61 ;; ver William Sommerfeld's server.el. Since then, a number of people
|
|
62 ;; have worked on it, including Bob Weiner, Darell Kindred, Arup
|
|
63 ;; Mukherjee, Ben Wing and Jan Vroonhof. It was completely rewritten
|
|
64 ;; (labeled as version 3) by Hrvoje Niksic in May 1997.
|
|
65
|
|
66 ;; Jan Vroonhof <vroonhof@math.ethz.ch> July/1996
|
|
67 ;; ported the server-temp-file-regexp feature from server.el
|
|
68 ;; ported server hooks from server.el
|
|
69 ;; ported kill-*-query functions from server.el (and made it optional)
|
|
70 ;; synced other behaviour with server.el
|
|
71 ;;
|
|
72 ;; Jan Vroonhof
|
|
73 ;; Customized.
|
|
74 ;;
|
|
75 ;; Hrvoje Niksic <hniksic@srce.hr> May/1997
|
|
76 ;; Completely rewritten. Now uses `defstruct' and other CL stuff
|
165
|
77 ;; to define clients cleanly. Many thanks to Dave Gillespie!
|
173
|
78 ;;
|
|
79 ;; Mike Scheidler <c23mts@eng.delcoelect.com> July, 1997
|
|
80 ;; Added 'Done' button to the menubar.
|
0
|
81
|
|
82
|
149
|
83 ;;; Code:
|
|
84
|
120
|
85 (defgroup gnuserv nil
|
149
|
86 "The gnuserv suite of programs to talk to Emacs from outside."
|
|
87 :group 'environment
|
120
|
88 :group 'processes
|
|
89 :group 'terminals)
|
|
90
|
|
91
|
167
|
92 ;; Provide the old variables as aliases, to avoid breaking .emacs
|
|
93 ;; files. However, they are obsolete and should be converted to the
|
|
94 ;; new forms. This ugly crock must be before the variable
|
|
95 ;; declaration, or the scheme fails.
|
|
96
|
|
97 (define-obsolete-variable-alias 'server-frame 'gnuserv-frame)
|
|
98 (define-obsolete-variable-alias 'server-done-function
|
|
99 'gnuserv-done-function)
|
|
100 (define-obsolete-variable-alias 'server-done-temp-file-function
|
|
101 'gnuserv-done-temp-file-function)
|
|
102 (define-obsolete-variable-alias 'server-find-file-function
|
|
103 'gnuserv-find-file-function)
|
|
104 (define-obsolete-variable-alias 'server-program
|
|
105 'gnuserv-program)
|
|
106 (define-obsolete-variable-alias 'server-visit-hook
|
|
107 'gnuserv-visit-hook)
|
|
108 (define-obsolete-variable-alias 'server-done-hook
|
|
109 'gnuserv-done-hook)
|
|
110 (define-obsolete-variable-alias 'server-kill-quietly
|
|
111 'gnuserv-kill-quietly)
|
|
112 (define-obsolete-variable-alias 'server-temp-file-regexp
|
|
113 'gnuserv-temp-file-regexp)
|
|
114 (define-obsolete-variable-alias 'server-make-temp-file-backup
|
|
115 'gnuserv-make-temp-file-backup)
|
|
116
|
149
|
117 ;;;###autoload
|
|
118 (defcustom gnuserv-frame nil
|
|
119 "*The frame to be used to display all edited files.
|
|
120 If nil, then a new frame is created for each file edited.
|
|
121 If t, then the currently selected frame will be used.
|
|
122 If a function, then this will be called with a symbol `x' or `tty' as the
|
|
123 only argument, and its return value will be interpreted as above."
|
|
124 :tag "Gnuserv Frame"
|
|
125 :type '(radio (const :tag "Create new frame each time" nil)
|
|
126 (const :tag "Use selected frame" t)
|
|
127 (function-item :tag "Use main Emacs frame"
|
|
128 gnuserv-main-frame-function)
|
|
129 (function-item :tag "Use visible frame, otherwise create new"
|
|
130 gnuserv-visible-frame-function)
|
|
131 (function-item :tag "Create special Gnuserv frame and use it"
|
|
132 gnuserv-special-frame-function)
|
|
133 (function :tag "Other"))
|
|
134 :group 'gnuserv)
|
0
|
135
|
149
|
136 (defcustom gnuserv-done-function 'kill-buffer
|
|
137 "*Function used to remove a buffer after editing.
|
173
|
138 It is called with one BUFFER argument. Functions such as `kill-buffer' and
|
|
139 `bury-buffer' are good values. See also `gnuserv-done-temp-file-function'."
|
149
|
140 :type '(radio (function-item kill-buffer)
|
|
141 (function-item bury-buffer)
|
|
142 (function :tag "Other"))
|
|
143 :group 'gnuserv)
|
0
|
144
|
149
|
145 (defcustom gnuserv-done-temp-file-function 'kill-buffer
|
|
146 "*Function used to remove a temporary buffer after editing.
|
173
|
147 It is called with one BUFFER argument. Functions such as `kill-buffer' and
|
|
148 `bury-buffer' are good values. See also `gnuserv-done-temp-file-function'."
|
149
|
149 :type '(radio (function-item kill-buffer)
|
|
150 (function-item bury-buffer)
|
|
151 (function :tag "Other"))
|
|
152 :group 'gnuserv)
|
|
153
|
|
154 (defcustom gnuserv-find-file-function 'find-file
|
|
155 "*Function to visit a file with.
|
|
156 It takes one argument, a file name to visit."
|
120
|
157 :type 'function
|
|
158 :group 'gnuserv)
|
114
|
159
|
149
|
160 (defcustom gnuserv-view-file-function 'view-file
|
|
161 "*Function to view a file with.
|
|
162 It takes one argument, a file name to view."
|
|
163 :type '(radio (function-item view-file)
|
|
164 (function-item find-file-read-only)
|
|
165 (function :tag "Other"))
|
120
|
166 :group 'gnuserv)
|
0
|
167
|
149
|
168 (defcustom gnuserv-program "gnuserv"
|
|
169 "*Program to use as the editing server."
|
120
|
170 :type 'string
|
|
171 :group 'gnuserv)
|
0
|
172
|
149
|
173 (defcustom gnuserv-visit-hook nil
|
|
174 "*Hook run after visiting a file."
|
|
175 :type 'hook
|
|
176 :group 'gnuserv)
|
114
|
177
|
149
|
178 (defcustom gnuserv-done-hook nil
|
|
179 "*Hook run when done editing a buffer for the Emacs server.
|
|
180 The hook functions are called after the file has been visited, with the
|
|
181 current buffer set to the visiting buffer."
|
120
|
182 :type 'hook
|
|
183 :group 'gnuserv)
|
114
|
184
|
151
|
185 (defcustom gnuserv-init-hook nil
|
|
186 "*Hook run after the server is started."
|
|
187 :type 'hook
|
|
188 :group 'gnuserv)
|
|
189
|
|
190 (defcustom gnuserv-shutdown-hook nil
|
|
191 "*Hook run before the server exits."
|
|
192 :type 'hook
|
|
193 :group 'gnuserv)
|
|
194
|
149
|
195 (defcustom gnuserv-kill-quietly nil
|
|
196 "*Non-nil means to kill buffers with clients attached without requiring confirmation."
|
|
197 :type 'boolean
|
120
|
198 :group 'gnuserv)
|
114
|
199
|
149
|
200 (defcustom gnuserv-temp-file-regexp "^/tmp/Re\\|/draft$"
|
|
201 "*Regexp which should match filenames of temporary files deleted
|
|
202 and reused by the programs that invoke the Emacs server."
|
|
203 :type 'regexp
|
|
204 :group 'gnuserv)
|
78
|
205
|
149
|
206 (defcustom gnuserv-make-temp-file-backup nil
|
|
207 "*Non-nil makes the server backup temporary files also."
|
120
|
208 :type 'boolean
|
|
209 :group 'gnuserv)
|
114
|
210
|
149
|
211
|
|
212 ;;; Internal variables:
|
|
213
|
|
214 (defstruct gnuclient
|
|
215 "An object that encompasses several buffers in one.
|
|
216 Normally, a client connecting to Emacs will be assigned an id, and
|
|
217 will request editing of several files.
|
|
218
|
|
219 ID - Client id (integer).
|
|
220 BUFFERS - List of buffers that \"belong\" to the client.
|
|
221 NOTE: one buffer can belong to several clients.
|
|
222 DEVICE - The device this client is on. If the device was also created.
|
|
223 by a client, it will be placed to `gnuserv-devices' list.
|
|
224 FRAME - Frame created by the client, or nil if the client didn't
|
|
225 create a frame.
|
|
226
|
|
227 All the slots default to nil."
|
|
228 (id nil)
|
|
229 (buffers nil)
|
|
230 (device nil)
|
|
231 (frame nil))
|
|
232
|
|
233 (defvar gnuserv-process nil
|
|
234 "The current gnuserv process.")
|
|
235
|
|
236 (defvar gnuserv-string ""
|
|
237 "The last input string from the server.")
|
|
238
|
|
239 (defvar gnuserv-current-client nil
|
|
240 "The client we are currently talking to.")
|
|
241
|
|
242 (defvar gnuserv-clients nil
|
|
243 "List of current gnuserv clients.
|
|
244 Each element is a gnuclient structure that identifies a client.")
|
|
245
|
|
246 (defvar gnuserv-devices nil
|
|
247 "List of devices created by clients.")
|
|
248
|
|
249 (defvar gnuserv-special-frame nil
|
|
250 "Frame created specially for Server.")
|
|
251
|
|
252 ;; We want the client-infested buffers to have some modeline
|
|
253 ;; identification, so we'll make a "minor mode".
|
|
254 (defvar gnuserv-minor-mode nil)
|
|
255 (make-variable-buffer-local 'gnuserv-mode)
|
151
|
256 (pushnew '(gnuserv-minor-mode " Server") minor-mode-alist
|
|
257 :test 'equal)
|
149
|
258
|
|
259
|
|
260 ;; Sample gnuserv-frame functions
|
|
261
|
|
262 (defun gnuserv-main-frame-function (type)
|
|
263 "Returns a sensible value for the main Emacs frame."
|
|
264 (if (eq type 'x)
|
|
265 (car (frame-list))
|
|
266 nil))
|
0
|
267
|
149
|
268 (defun gnuserv-visible-frame-function (type)
|
|
269 "Returns a frame if there is a frame that is truly visible, nil otherwise.
|
|
270 This is meant in the X sense, so it will not return frames that are on another
|
|
271 visual screen. Totally visible frames are preferred. If none found, return nil."
|
|
272 (if (eq type 'x)
|
|
273 (cond ((car (filtered-frame-list 'frame-totally-visible-p
|
|
274 (selected-device))))
|
|
275 ((car (filtered-frame-list (lambda (frame)
|
|
276 ;; eq t as in not 'hidden
|
|
277 (eq t (frame-visible-p frame)))
|
|
278 (selected-device)))))
|
|
279 nil))
|
|
280
|
|
281 (defun gnuserv-special-frame-function (type)
|
|
282 "Creates a special frame for Gnuserv and returns it on later invocations."
|
|
283 (unless (frame-live-p gnuserv-special-frame)
|
|
284 (setq gnuserv-special-frame (make-frame)))
|
|
285 gnuserv-special-frame)
|
|
286
|
|
287
|
|
288 ;;; Communication functions
|
|
289
|
151
|
290 ;; We used to restart the server here, but it's too risky -- if
|
|
291 ;; something goes awry, it's too easy to wind up in a loop.
|
149
|
292 (defun gnuserv-sentinel (proc msg)
|
167
|
293 (let ((msgstring (concat "Gnuserv process %s; restart with `%s'"))
|
|
294 (keystring (substitute-command-keys "\\[gnuserv-start]")))
|
149
|
295 (case (process-status proc)
|
151
|
296 (exit
|
167
|
297 (message msgstring "exited" keystring)
|
151
|
298 (gnuserv-prepare-shutdown))
|
|
299 (signal
|
167
|
300 (message msgstring "killed" keystring)
|
151
|
301 (gnuserv-prepare-shutdown))
|
|
302 (closed
|
167
|
303 (message msgstring "closed" keystring))
|
151
|
304 (gnuserv-prepare-shutdown))))
|
149
|
305
|
151
|
306 ;; This function reads client requests from our current server. Every
|
|
307 ;; client is identified by a unique ID within the server
|
|
308 ;; (incidentally, the same ID is the file descriptor the server uses
|
|
309 ;; to communicate to client).
|
|
310 ;;
|
|
311 ;; The request string can arrive in several chunks. As the request
|
|
312 ;; ends with \C-d, we check for that character at the end of string.
|
|
313 ;; If not found, keep reading, and concatenating to former strings.
|
|
314 ;; So, if at first read we receive "5 (gn", that text will be stored
|
|
315 ;; to gnuserv-string. If we then receive "us)\C-d", the two will be
|
|
316 ;; concatenated, `current-client' will be set to 5, and `(gnus)' form
|
|
317 ;; will be evaluated.
|
|
318 ;;
|
|
319 ;; Server will send the following:
|
|
320 ;;
|
|
321 ;; "ID <text>\C-d" (no quotes)
|
|
322 ;;
|
|
323 ;; ID - file descriptor of the given client;
|
|
324 ;; <text> - the actual contents of the request.
|
149
|
325 (defun gnuserv-process-filter (proc string)
|
|
326 "Process gnuserv client requests to execute Emacs commands."
|
|
327 (setq gnuserv-string (concat gnuserv-string string))
|
|
328 ;; C-d means end of request.
|
153
|
329 (when (string-match "\C-d\\'" gnuserv-string)
|
149
|
330 (cond ((string-match "^[0-9]+" gnuserv-string) ; client request id
|
|
331 (let ((header (read-from-string gnuserv-string)))
|
|
332 ;; Set the client we are talking to.
|
|
333 (setq gnuserv-current-client (car header))
|
|
334 ;; Evaluate the expression
|
|
335 (condition-case oops
|
|
336 (eval (car (read-from-string gnuserv-string (cdr header))))
|
|
337 ;; In case of an error, write the description to the
|
|
338 ;; client, and then signal it.
|
|
339 (error (setq gnuserv-string "")
|
|
340 (gnuserv-write-to-client gnuserv-current-client oops)
|
|
341 (setq gnuserv-current-client nil)
|
|
342 (signal (car oops) (cdr oops)))
|
|
343 (quit (setq gnuserv-string "")
|
|
344 (gnuserv-write-to-client gnuserv-current-client oops)
|
|
345 (setq gnuserv-current-client nil)
|
|
346 (signal 'quit nil)))
|
|
347 (setq gnuserv-string "")))
|
|
348 (t
|
|
349 (error "%s: invalid response from gnuserv" gnuserv-string)
|
|
350 (setq gnuserv-string "")))))
|
|
351
|
151
|
352 ;; This function is somewhat of a misnomer. Actually, we write to the
|
|
353 ;; server (using `process-send-string' to gnuserv-process), which
|
|
354 ;; interprets what we say and forwards it to the client. The
|
|
355 ;; incantation server understands is (from gnuserv.c):
|
|
356 ;;
|
|
357 ;; "FD/LEN:<text>\n" (no quotes)
|
|
358 ;; FD - file descriptor of the given client (which we obtained from
|
|
359 ;; the server earlier);
|
|
360 ;; LEN - length of the stuff we are about to send;
|
|
361 ;; <text> - the actual contents of the request.
|
149
|
362 (defun gnuserv-write-to-client (client-id form)
|
|
363 "Write the given form to the given client via the gnuserv process."
|
|
364 (when (eq (process-status gnuserv-process) 'run)
|
|
365 (let* ((result (format "%s" form))
|
|
366 (s (format "%s/%d:%s\n" client-id
|
|
367 (length result) result)))
|
|
368 (process-send-string gnuserv-process s))))
|
|
369
|
|
370 ;; The following two functions are helper functions, used by
|
|
371 ;; gnuclient.
|
|
372
|
|
373 (defun gnuserv-eval (form)
|
|
374 "Evaluate form and return result to client."
|
|
375 (gnuserv-write-to-client gnuserv-current-client (eval form))
|
|
376 (setq gnuserv-current-client nil))
|
|
377
|
|
378 (defun gnuserv-eval-quickly (form)
|
|
379 "Let client know that we've received the request, and then eval the form.
|
|
380 This order is important as not to keep the client waiting."
|
|
381 (gnuserv-write-to-client gnuserv-current-client nil)
|
|
382 (setq gnuserv-current-client nil)
|
|
383 (eval form))
|
114
|
384
|
149
|
385
|
|
386 ;; "Execute" a client connection, called by gnuclient. This is the
|
|
387 ;; backbone of gnuserv.el.
|
151
|
388 (defun gnuserv-edit-files (type list &rest flags)
|
149
|
389 "For each (line-number . file) pair in LIST, edit the file at line-number.
|
|
390 The visited buffers are memorized, so that when \\[gnuserv-edit] is invoked
|
|
391 in such a buffer, or when it is killed, or the client's device deleted, the
|
|
392 client will be invoked that the edit is finished.
|
114
|
393
|
149
|
394 TYPE should either be a (tty TTY TERM PID) list, or (x DISPLAY) list.
|
151
|
395 If a flag is `quick', just edit the files in Emacs.
|
|
396 If a flag is `view', view the files read-only."
|
|
397 (let (quick view)
|
|
398 (mapc (lambda (flag)
|
|
399 (case flag
|
|
400 (quick (setq quick t))
|
|
401 (view (setq view t))
|
|
402 (t (error "Invalid flag %s" flag))))
|
|
403 flags)
|
|
404 (let* ((old-device-num (length (device-list)))
|
|
405 (new-frame nil)
|
|
406 (dest-frame (if (functionp gnuserv-frame)
|
|
407 (funcall gnuserv-frame (car type))
|
|
408 gnuserv-frame))
|
|
409 ;; The gnuserv-frame dependencies are ugly.
|
|
410 (device (cond ((frame-live-p dest-frame)
|
|
411 (frame-device dest-frame))
|
|
412 ((null dest-frame)
|
|
413 (case (car type)
|
|
414 (tty (apply 'make-tty-device (cdr type)))
|
|
415 (x (make-x-device (cadr type)))
|
|
416 (t (error "Invalid device type"))))
|
|
417 (t
|
|
418 (selected-device))))
|
|
419 (frame (cond ((frame-live-p dest-frame)
|
|
420 dest-frame)
|
|
421 ((null dest-frame)
|
|
422 (setq new-frame (make-frame nil device))
|
|
423 new-frame)
|
|
424 (t (selected-frame))))
|
|
425 (client (make-gnuclient :id gnuserv-current-client
|
|
426 :device device
|
|
427 :frame new-frame)))
|
|
428 (setq gnuserv-current-client nil)
|
|
429 ;; If the device was created by this client, push it to the list.
|
|
430 (and (/= old-device-num (length (device-list)))
|
|
431 (push device gnuserv-devices))
|
|
432 (and (frame-iconified-p frame)
|
|
433 (deiconify-frame frame))
|
|
434 ;; Visit all the listed files.
|
|
435 (while list
|
|
436 (let ((line (caar list)) (path (cdar list)))
|
|
437 (select-frame frame)
|
|
438 ;; Visit the file.
|
|
439 (funcall (if view
|
|
440 gnuserv-view-file-function
|
|
441 gnuserv-find-file-function)
|
|
442 path)
|
|
443 (goto-line line)
|
|
444 ;; Don't memorize the quick and view buffers.
|
|
445 (unless (or quick view)
|
|
446 (pushnew (current-buffer) (gnuclient-buffers client))
|
173
|
447 (setq gnuserv-minor-mode t)
|
|
448 ;; Add the "Done" button to the menubar, only in this buffer.
|
|
449 (set-buffer-menubar current-menubar)
|
|
450 (add-menu-button nil ["Done" gnuserv-edit t]))
|
|
451 (run-hooks 'gnuserv-visit-hook)
|
151
|
452 (pop list)))
|
|
453 (cond
|
|
454 ((and (or quick view)
|
|
455 (device-on-window-system-p device))
|
|
456 ;; Exit if on X device, and quick or view. NOTE: if the
|
|
457 ;; client is to finish now, it must absolutely /not/ be
|
|
458 ;; included to the list of clients. This way the client-ids
|
|
459 ;; should be unique.
|
|
460 (gnuserv-write-to-client (gnuclient-id client) nil))
|
|
461 (t
|
|
462 ;; Else, the client gets a vote.
|
|
463 (push client gnuserv-clients)
|
|
464 ;; Explain buffer exit options. If dest-frame is nil, the
|
|
465 ;; user can exit via `delete-frame'. OTOH, if FLAGS are nil
|
|
466 ;; and there are some buffers, the user can exit via
|
|
467 ;; `gnuserv-edit'.
|
|
468 (if (and (not (or quick view))
|
|
469 (gnuclient-buffers client))
|
167
|
470 (message "%s"
|
|
471 (substitute-command-keys
|
151
|
472 "Type `\\[gnuserv-edit]' to finish editing"))
|
|
473 (or dest-frame
|
167
|
474 (message "%s"
|
|
475 (substitute-command-keys
|
151
|
476 "Type `\\[delete-frame]' to finish editing")))))))))
|
0
|
477
|
149
|
478
|
|
479 ;;; Functions that hook into Emacs in various way to enable operation
|
|
480
|
|
481 ;; Defined later.
|
|
482 (add-hook 'kill-emacs-hook 'gnuserv-kill-all-clients t)
|
|
483
|
151
|
484 ;; A helper function; used by others. Try avoiding it whenever
|
|
485 ;; possible, because it is slow, and conses a list. Use
|
|
486 ;; `gnuserv-buffer-p' when appropriate, for instance.
|
149
|
487 (defun gnuserv-buffer-clients (buffer)
|
|
488 "Returns a list of clients to which BUFFER belongs."
|
165
|
489 (let (res)
|
|
490 (dolist (client gnuserv-clients)
|
|
491 (when (memq buffer (gnuclient-buffers client))
|
|
492 (push client res)))
|
149
|
493 res))
|
|
494
|
151
|
495 ;; Like `gnuserv-buffer-clients', but returns a boolean; doesn't
|
|
496 ;; collect a list.
|
|
497 (defun gnuserv-buffer-p (buffer)
|
|
498 (member* buffer gnuserv-clients
|
|
499 :test 'memq
|
|
500 :key 'gnuclient-buffers))
|
|
501
|
149
|
502 ;; This function makes sure that a killed buffer is deleted off the
|
|
503 ;; list for the particular client.
|
|
504 ;;
|
|
505 ;; This hooks into `kill-buffer-hook'. It is *not* a replacement for
|
|
506 ;; `kill-buffer' (thanks God).
|
|
507 (defun gnuserv-kill-buffer-function ()
|
|
508 "Remove the buffer from the buffer lists of all the clients it belongs to.
|
|
509 Any client that remains \"empty\" after the removal is informed that the
|
|
510 editing has ended."
|
165
|
511 (let* ((buf (current-buffer)))
|
|
512 (dolist (client (gnuserv-buffer-clients buf))
|
|
513 (callf2 delq buf (gnuclient-buffers client))
|
149
|
514 ;; If no more buffers, kill the client.
|
165
|
515 (when (null (gnuclient-buffers client))
|
|
516 (gnuserv-kill-client client)))))
|
149
|
517
|
|
518 (add-hook 'kill-buffer-hook 'gnuserv-kill-buffer-function)
|
|
519
|
|
520 ;; Ask for confirmation before killing a buffer that belongs to a
|
|
521 ;; living client.
|
|
522 (defun gnuserv-kill-buffer-query-function ()
|
|
523 (or gnuserv-kill-quietly
|
151
|
524 (not (gnuserv-buffer-p (current-buffer)))
|
149
|
525 (yes-or-no-p
|
|
526 (format "Buffer %s belongs to gnuserv client(s); kill anyway? "
|
|
527 (current-buffer)))))
|
|
528
|
|
529 (add-hook 'kill-buffer-query-functions
|
|
530 'gnuserv-kill-buffer-query-function)
|
|
531
|
|
532 (defun gnuserv-kill-emacs-query-function ()
|
|
533 (or gnuserv-kill-quietly
|
|
534 (not (some 'gnuclient-buffers gnuserv-clients))
|
|
535 (yes-or-no-p "Gnuserv buffers still have clients; exit anyway? ")))
|
|
536
|
|
537 (add-hook 'kill-emacs-query-functions
|
|
538 'gnuserv-kill-emacs-query-function)
|
|
539
|
|
540 ;; If the device of a client is to be deleted, the client should die
|
|
541 ;; as well. This is why we hook into `delete-device-hook'.
|
|
542 (defun gnuserv-check-device (device)
|
|
543 (when (memq device gnuserv-devices)
|
165
|
544 (dolist (client gnuserv-clients)
|
|
545 (when (eq device (gnuclient-device client))
|
|
546 ;; we must make sure that the server kill doesn't result in
|
|
547 ;; killing the device, because it would cause a device-dead
|
|
548 ;; error when `delete-device' tries to do the job later.
|
167
|
549 (gnuserv-kill-client client t))))
|
165
|
550 (callf2 delq device gnuserv-devices))
|
149
|
551
|
|
552 (add-hook 'delete-device-hook 'gnuserv-check-device)
|
|
553
|
|
554 (defun gnuserv-temp-file-p (buffer)
|
114
|
555 "Return non-nil if BUFFER contains a file considered temporary.
|
|
556 These are files whose names suggest they are repeatedly
|
|
557 reused to pass information to another program.
|
|
558
|
149
|
559 The variable `gnuserv-temp-file-regexp' controls which filenames
|
114
|
560 are considered temporary."
|
|
561 (and (buffer-file-name buffer)
|
149
|
562 (string-match gnuserv-temp-file-regexp (buffer-file-name buffer))))
|
0
|
563
|
149
|
564 (defun gnuserv-kill-client (client &optional leave-frame)
|
|
565 "Kill the gnuclient CLIENT.
|
|
566 This will do away with all the associated buffers. If LEAVE-FRAME,
|
|
567 the function will not remove the frames associated with the client."
|
|
568 ;; Order is important: first delete client from gnuserv-clients, to
|
|
569 ;; prevent gnuserv-buffer-done-1 calling us recursively.
|
|
570 (callf2 delq client gnuserv-clients)
|
|
571 ;; Process the buffers.
|
|
572 (mapc 'gnuserv-buffer-done-1 (gnuclient-buffers client))
|
|
573 (unless leave-frame
|
|
574 (let ((device (gnuclient-device client)))
|
|
575 ;; kill frame created by this client (if any), unless
|
|
576 ;; specifically requested otherwise.
|
|
577 ;;
|
|
578 ;; note: last frame on a device will not be deleted here.
|
|
579 (when (and (gnuclient-frame client)
|
|
580 (frame-live-p (gnuclient-frame client))
|
|
581 (second (device-frame-list device)))
|
|
582 (delete-frame (gnuclient-frame client)))
|
|
583 ;; If the device is live, created by a client, and no longer used
|
|
584 ;; by any client, delete it.
|
|
585 (when (and (device-live-p device)
|
|
586 (memq device gnuserv-devices)
|
|
587 (second (device-list))
|
|
588 (not (member* device gnuserv-clients
|
|
589 :key 'gnuclient-device)))
|
|
590 ;; `gnuserv-check-device' will remove it from `gnuserv-devices'.
|
|
591 (delete-device device))))
|
|
592 ;; Notify the client.
|
|
593 (gnuserv-write-to-client (gnuclient-id client) nil))
|
0
|
594
|
149
|
595 ;; Do away with the buffer.
|
|
596 (defun gnuserv-buffer-done-1 (buffer)
|
165
|
597 (dolist (client (gnuserv-buffer-clients buffer))
|
|
598 (callf2 delq buffer (gnuclient-buffers client))
|
|
599 (when (null (gnuclient-buffers client))
|
|
600 (gnuserv-kill-client client)))
|
173
|
601 ;; Get rid of the buffer.
|
165
|
602 (save-excursion
|
|
603 (set-buffer buffer)
|
|
604 (run-hooks 'gnuserv-done-hook)
|
|
605 (setq gnuserv-minor-mode nil)
|
173
|
606 ;; Delete the menu button.
|
|
607 (delete-menu-item '("Done"))
|
165
|
608 (funcall (if (gnuserv-temp-file-p buffer)
|
|
609 gnuserv-done-temp-file-function
|
|
610 gnuserv-done-function)
|
|
611 buffer)))
|
0
|
612
|
149
|
613
|
|
614 ;;; Higher-level functions
|
0
|
615
|
149
|
616 ;; Choose a `next' server buffer, according to several criteria, and
|
151
|
617 ;; return it. If none are found, return nil.
|
149
|
618 (defun gnuserv-next-buffer ()
|
|
619 (let* ((frame (selected-frame))
|
|
620 (device (selected-device))
|
|
621 client)
|
|
622 (cond
|
|
623 ;; If we have a client belonging to this frame, return
|
|
624 ;; the first buffer from it.
|
|
625 ((setq client
|
|
626 (car (member* frame gnuserv-clients :key 'gnuclient-frame)))
|
|
627 (car (gnuclient-buffers client)))
|
|
628 ;; Else, look for a device.
|
|
629 ((and
|
|
630 (memq (selected-device) gnuserv-devices)
|
|
631 (setq client
|
|
632 (car (member* device gnuserv-clients :key 'gnuclient-device))))
|
|
633 (car (gnuclient-buffers client)))
|
151
|
634 ;; Else, try to find any client with at least one buffer, and
|
|
635 ;; return its first buffer.
|
|
636 ((setq client
|
165
|
637 (car (member-if-not #'null gnuserv-clients
|
153
|
638 :key 'gnuclient-buffers)))
|
151
|
639 (car (gnuclient-buffers client)))
|
|
640 ;; Oh, give up.
|
149
|
641 (t nil))))
|
0
|
642
|
149
|
643 (defun gnuserv-buffer-done (buffer)
|
|
644 "Mark BUFFER as \"done\" for its client(s).
|
151
|
645 Does the save/backup queries first, and calls `gnuserv-done-function'."
|
149
|
646 ;; Check whether this is the real thing.
|
151
|
647 (unless (gnuserv-buffer-p buffer)
|
149
|
648 (error "%s does not belong to a gnuserv client" buffer))
|
|
649 ;; Backup/ask query.
|
|
650 (if (gnuserv-temp-file-p buffer)
|
|
651 ;; For a temp file, save, and do NOT make a non-numeric backup
|
|
652 ;; Why does server.el explicitly back up temporary files?
|
|
653 (let ((version-control nil)
|
|
654 (buffer-backed-up (not gnuserv-make-temp-file-backup)))
|
|
655 (save-buffer))
|
|
656 (if (and (buffer-modified-p)
|
|
657 (y-or-n-p (concat "Save file " buffer-file-name "? ")))
|
|
658 (save-buffer buffer)))
|
151
|
659 (gnuserv-buffer-done-1 buffer))
|
0
|
660
|
149
|
661 ;; Called by `gnuserv-start-1' to clean everything. Hooked into
|
|
662 ;; `kill-emacs-hook', too.
|
|
663 (defun gnuserv-kill-all-clients ()
|
|
664 "Kill all the gnuserv clients. Ruthlessly."
|
|
665 (mapc 'gnuserv-kill-client gnuserv-clients))
|
|
666
|
151
|
667 ;; This serves to run the hook and reset
|
|
668 ;; `allow-deletion-of-last-visible-frame'.
|
|
669 (defun gnuserv-prepare-shutdown ()
|
|
670 (setq allow-deletion-of-last-visible-frame nil)
|
|
671 (run-hooks 'gnuserv-shutdown-hook))
|
|
672
|
|
673 ;; This is a user-callable function, too.
|
|
674 (defun gnuserv-shutdown ()
|
|
675 "Shutdown the gnuserv server, if one is currently running.
|
|
676 All the clients will be disposed of via the normal methods."
|
|
677 (interactive)
|
149
|
678 (gnuserv-kill-all-clients)
|
|
679 (when gnuserv-process
|
|
680 (set-process-sentinel gnuserv-process nil)
|
151
|
681 (gnuserv-prepare-shutdown)
|
149
|
682 (condition-case ()
|
|
683 (delete-process gnuserv-process)
|
151
|
684 (error nil))
|
153
|
685 (setq gnuserv-process nil)))
|
151
|
686
|
|
687 ;; Actually start the process. Kills all the clients before-hand.
|
|
688 (defun gnuserv-start-1 (&optional leave-dead)
|
|
689 ;; Shutdown the existing server, if any.
|
|
690 (gnuserv-shutdown)
|
149
|
691 ;; If we already had a server, clear out associated status.
|
|
692 (unless leave-dead
|
151
|
693 (setq gnuserv-string ""
|
|
694 gnuserv-current-client nil)
|
149
|
695 (let ((process-connection-type t))
|
151
|
696 (setq gnuserv-process
|
149
|
697 (start-process "gnuserv" nil gnuserv-program)))
|
|
698 (set-process-sentinel gnuserv-process 'gnuserv-sentinel)
|
|
699 (set-process-filter gnuserv-process 'gnuserv-process-filter)
|
151
|
700 (process-kill-without-query gnuserv-process)
|
|
701 (setq allow-deletion-of-last-visible-frame t)
|
|
702 (run-hooks 'gnuserv-init-hook)))
|
149
|
703
|
|
704
|
|
705 ;;; User-callable functions:
|
0
|
706
|
|
707 ;;;###autoload
|
151
|
708 (defun gnuserv-running-p ()
|
|
709 "Return non-nil if a gnuserv process is running from this XEmacs session."
|
|
710 (not (not gnuserv-process)))
|
|
711
|
|
712 ;;;###autoload
|
0
|
713 (defun gnuserv-start (&optional leave-dead)
|
|
714 "Allow this Emacs process to be a server for client processes.
|
149
|
715 This starts a gnuserv communications subprocess through which
|
0
|
716 client \"editors\" (gnuclient and gnudoit) can send editing commands to
|
149
|
717 this Emacs job. See the gnuserv(1) manual page for more details.
|
0
|
718
|
|
719 Prefix arg means just kill any existing server communications subprocess."
|
|
720 (interactive "P")
|
149
|
721 (and gnuserv-process
|
|
722 (not leave-dead)
|
|
723 (message "Restarting gnuserv"))
|
|
724 (gnuserv-start-1 leave-dead))
|
0
|
725
|
151
|
726 (defun gnuserv-edit (&optional count)
|
149
|
727 "Mark the current gnuserv editing buffer as \"done\", and switch to next one.
|
0
|
728
|
151
|
729 Run with a numeric prefix argument, repeat the operation that number
|
|
730 of times. If given a universal prefix argument, close all the buffers
|
|
731 of this buffer's clients.
|
|
732
|
|
733 The `gnuserv-done-function' (bound to `kill-buffer' by default) is
|
|
734 called to dispose of the buffer after marking it as done.
|
0
|
735
|
149
|
736 Files that match `gnuserv-temp-file-regexp' are considered temporary and
|
|
737 are saved unconditionally and backed up if `gnuserv-make-temp-file-backup'
|
151
|
738 is non-nil. They are disposed of using `gnuserv-done-temp-file-function'
|
|
739 (also bound to `kill-buffer' by default).
|
114
|
740
|
151
|
741 When all of a client's buffers are marked as \"done\", the client is notified."
|
0
|
742 (interactive "P")
|
151
|
743 (when (null count)
|
|
744 (setq count 1))
|
|
745 (cond ((numberp count)
|
|
746 (let (next)
|
|
747 (while (natnump (decf count))
|
|
748 (gnuserv-buffer-done (current-buffer))
|
|
749 (setq next (gnuserv-next-buffer))
|
|
750 (when next
|
|
751 (switch-to-buffer next)))))
|
|
752 (count
|
|
753 (let* ((buf (current-buffer))
|
|
754 (clients (gnuserv-buffer-clients buf)))
|
|
755 (unless clients
|
|
756 (error "%s does not belong to a gnuserv client" buf))
|
|
757 (mapc 'gnuserv-kill-client (gnuserv-buffer-clients buf))))))
|
0
|
758
|
149
|
759 (global-set-key "\C-x#" 'gnuserv-edit)
|
0
|
760
|
|
761 (provide 'gnuserv)
|
|
762
|
78
|
763 ;;; gnuserv.el ends here
|