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