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