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