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