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