1 ;;; gnuserv.el --- Lisp interface code between Emacs and gnuserv
2 ;; Copyright (C) 1989-1997 Free Software Foundation, Inc.
5 ;; Author: Andy Norman (ange@hplb.hpl.hp.com), originally based on server.el
6 ;; Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Maintainer: Jan Vroonhof <vroonhof@math.ethz.ch>,
8 ;; Hrvoje Niksic <hniksic@xemacs.org>
9 ;; Keywords: environment, processes, terminals
11 ;; This file is part of SXEmacs.
13 ;; SXEmacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; SXEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Synched up with: Not in FSF.
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.
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
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.
48 ;; To evaluate a Lisp form in a running Emacs, use the `-eval'
49 ;; argument of gnuclient. To simplify this, we provide the `gnudoit'
50 ;; shell script. For example `gnudoit "(+ 2 3)"' will print `5',
51 ;; whereas `gnudoit "(gnus)"' will fire up your favorite newsreader.
52 ;; Like gnuclient, `gnudoit' requires the server to be started prior
55 ;; For more information you can refer to man pages of gnuclient,
56 ;; gnudoit and gnuserv, distributed with XEmacs.
58 ;; gnuserv.el was originally written by Andy Norman as an improvement
59 ;; over William Sommerfeld's server.el. Since then, a number of
60 ;; people have worked on it, including Bob Weiner, Darell Kindred,
61 ;; Arup Mukherjee, Ben Wing and Jan Vroonhof. It was completely
62 ;; rewritten (labeled as version 3) by Hrvoje Niksic in May 1997. The
63 ;; new code will not run on GNU Emacs.
65 ;; Jan Vroonhof <vroonhof@math.ethz.ch> July/1996
66 ;; ported the server-temp-file-regexp feature from server.el
67 ;; ported server hooks from server.el
68 ;; ported kill-*-query functions from server.el (and made it optional)
69 ;; synced other behavior with server.el
74 ;; Hrvoje Niksic <hniksic@xemacs.org> May/1997
75 ;; Completely rewritten. Now uses `defstruct' and other CL stuff
76 ;; to define clients cleanly. Many thanks to Dave Gillespie!
78 ;; Mike Scheidler <c23mts@eng.delcoelect.com> July, 1997
79 ;; Added 'Done' button to the menubar.
85 "The gnuserv suite of programs to talk to Emacs from outside."
91 (defcustom gnuserv-mode-line-string " Server"
92 "*String to display in the modeline when Gnuserv is active.
93 Set this to nil if you don't want a modeline indicator."
95 (const :tag "none" nil))
99 ;; Provide the old variables as aliases, to avoid breaking .emacs
100 ;; files. However, they are obsolete and should be converted to the
101 ;; new forms. This ugly crock must be before the variable
102 ;; declaration, or the scheme fails.
104 (define-obsolete-variable-alias 'server-frame 'gnuserv-frame)
105 (define-obsolete-variable-alias 'server-done-function
106 'gnuserv-done-function)
107 (define-obsolete-variable-alias 'server-done-temp-file-function
108 'gnuserv-done-temp-file-function)
109 (define-obsolete-variable-alias 'server-find-file-function
110 'gnuserv-find-file-function)
111 (define-obsolete-variable-alias 'server-program
113 (define-obsolete-variable-alias 'server-visit-hook
115 (define-obsolete-variable-alias 'server-done-hook
117 (define-obsolete-variable-alias 'server-kill-quietly
118 'gnuserv-kill-quietly)
119 (define-obsolete-variable-alias 'server-temp-file-regexp
120 'gnuserv-temp-file-regexp)
121 (define-obsolete-variable-alias 'server-make-temp-file-backup
122 'gnuserv-make-temp-file-backup)
125 (defcustom gnuserv-frame nil
126 "*The frame to be used to display all edited files.
127 If nil, then a new frame is created for each file edited.
128 If t, then the currently selected frame will be used.
129 If a function, then this will be called with a symbol `x' or `tty' as the
130 only argument, and its return value will be interpreted as above."
132 :type '(radio (const :tag "Create new frame each time" nil)
133 (const :tag "Use selected frame" t)
134 (function-item :tag "Use main Emacs frame"
135 gnuserv-main-frame-function)
136 (function-item :tag "Use visible frame, otherwise create new"
137 gnuserv-visible-frame-function)
138 (function-item :tag "Create special Gnuserv frame and use it"
139 gnuserv-special-frame-function)
140 (function :tag "Other"))
144 (defcustom gnuserv-frame-plist nil
145 "*Plist of frame properties for creating a gnuserv frame."
150 (defcustom gnuserv-done-function 'kill-buffer
151 "*Function used to remove a buffer after editing.
152 It is called with one BUFFER argument. Functions such as `kill-buffer' and
153 `bury-buffer' are good values. See also `gnuserv-done-temp-file-function'."
154 :type '(radio (function-item kill-buffer)
155 (function-item bury-buffer)
156 (function :tag "Other"))
159 (defcustom gnuserv-done-temp-file-function 'kill-buffer
160 "*Function used to remove a temporary buffer after editing.
161 It is called with one BUFFER argument. Functions such as `kill-buffer' and
162 `bury-buffer' are good values. See also `gnuserv-done-temp-file-function'."
163 :type '(radio (function-item kill-buffer)
164 (function-item bury-buffer)
165 (function :tag "Other"))
168 (defcustom gnuserv-find-file-function 'find-file
169 "*Function to visit a file with.
170 It takes one argument, a file name to visit."
174 (defcustom gnuserv-view-file-function 'view-file
175 "*Function to view a file with.
176 It takes one argument, a file name to view."
177 :type '(radio (function-item view-file)
178 (function-item find-file-read-only)
179 (function :tag "Other"))
182 (defcustom gnuserv-program "gnuserv"
183 "*Program to use as the editing server."
187 (defcustom gnuserv-visit-hook nil
188 "*Hook run after visiting a file."
192 (defcustom gnuserv-done-hook nil
193 "*Hook run when done editing a buffer for the Emacs server.
194 The hook functions are called after the file has been visited, with the
195 current buffer set to the visiting buffer."
199 (defcustom gnuserv-init-hook nil
200 "*Hook run after the server is started."
204 (defcustom gnuserv-shutdown-hook nil
205 "*Hook run before the server exits."
209 (defcustom gnuserv-kill-quietly nil
210 "*Non-nil means to kill buffers with clients attached without requiring confirmation."
214 (defcustom gnuserv-temp-file-regexp
215 (concat "^" (temp-directory) "/Re\\|/draft$")
216 "*Regexp which should match filenames of temporary files deleted
217 and reused by the programs that invoke the Emacs server."
221 (defcustom gnuserv-make-temp-file-backup nil
222 "*Non-nil makes the server backup temporary files also."
227 ;;; Internal variables:
230 "An object that encompasses several buffers in one.
231 Normally, a client connecting to Emacs will be assigned an id, and
232 will request editing of several files.
234 ID - Client id (integer).
235 BUFFERS - List of buffers that \"belong\" to the client.
236 NOTE: one buffer can belong to several clients.
237 DEVICE - The device this client is on. If the device was also created.
238 by a client, it will be placed to `gnuserv-devices' list.
239 FRAME - Frame created by the client, or nil if the client didn't
242 All the slots default to nil."
248 (defvar gnuserv-process nil
249 "The current gnuserv process.")
251 (defvar gnuserv-string ""
252 "The last input string from the server.")
254 (defvar gnuserv-current-client nil
255 "The client we are currently talking to.")
257 (defvar gnuserv-clients nil
258 "List of current gnuserv clients.
259 Each element is a gnuclient structure that identifies a client.")
261 (defvar gnuserv-devices nil
262 "List of devices created by clients.")
264 (defvar gnuserv-special-frame nil
265 "Frame created specially for Server.")
267 ;; We want the client-infested buffers to have some modeline
268 ;; identification, so we'll make a "minor mode".
269 (defvar gnuserv-minor-mode nil)
270 (make-variable-buffer-local 'gnuserv-minor-mode)
271 ;;(pushnew '(gnuserv-minor-mode "Server") minor-mode-alist
273 (add-minor-mode 'gnuserv-minor-mode 'gnuserv-mode-line-string)
276 ;; Sample gnuserv-frame functions
278 (defun gnuserv-main-frame-function (type)
279 "Return a sensible value for the main Emacs frame."
284 (defun gnuserv-visible-frame-function (type)
285 "Return a frame if there is a frame that is truly visible, nil otherwise.
286 This is meant in the X sense, so it will not return frames that are on another
287 visual screen. Totally visible frames are preferred. If none found, return nil."
289 (cond ((car (filtered-frame-list 'frame-totally-visible-p
291 ((car (filtered-frame-list (lambda (frame)
292 ;; eq t as in not 'hidden
293 (eq t (frame-visible-p frame)))
294 (selected-device)))))
297 (defun gnuserv-special-frame-function (type)
298 "Create a special frame for Gnuserv and return it on later invocations."
299 (unless (frame-live-p gnuserv-special-frame)
300 (setq gnuserv-special-frame (make-frame gnuserv-frame-plist)))
301 gnuserv-special-frame)
304 ;;; Communication functions
306 ;; We used to restart the server here, but it's too risky -- if
307 ;; something goes awry, it's too easy to wind up in a loop.
308 (defun gnuserv-sentinel (proc msg)
309 (let ((msgstring (concat "Gnuserv process %s; restart with `%s'"))
310 (keystring (substitute-command-keys "\\[gnuserv-start]")))
311 (case (process-status proc)
313 (message msgstring "exited" keystring)
314 (gnuserv-prepare-shutdown))
316 (message msgstring "killed" keystring)
317 (gnuserv-prepare-shutdown))
319 (message msgstring "closed" keystring))
320 (gnuserv-prepare-shutdown))))
322 ;; This function reads client requests from our current server. Every
323 ;; client is identified by a unique ID within the server
324 ;; (incidentally, the same ID is the file descriptor the server uses
325 ;; to communicate to client).
327 ;; The request string can arrive in several chunks. As the request
328 ;; ends with \C-d, we check for that character at the end of string.
329 ;; If not found, keep reading, and concatenating to former strings.
330 ;; So, if at first read we receive "5 (gn", that text will be stored
331 ;; to gnuserv-string. If we then receive "us)\C-d", the two will be
332 ;; concatenated, `current-client' will be set to 5, and `(gnus)' form
333 ;; will be evaluated.
335 ;; Server will send the following:
337 ;; "ID <text>\C-d" (no quotes)
339 ;; ID - file descriptor of the given client;
340 ;; <text> - the actual contents of the request.
341 (defun gnuserv-process-filter (proc string)
342 "Process gnuserv client requests to execute Emacs commands."
343 (setq gnuserv-string (concat gnuserv-string string))
344 ;; C-d means end of request.
345 (when (string-match "\C-d\n?\\'" gnuserv-string)
346 (cond ((string-match "\\`[0-9]+" gnuserv-string) ; client request id
347 (let ((header (read-from-string gnuserv-string)))
348 ;; Set the client we are talking to.
349 (setq gnuserv-current-client (car header))
350 ;; Evaluate the expression
352 (eval (car (read-from-string gnuserv-string (cdr header))))
353 ;; In case of an error, write the description to the
354 ;; client, and then signal it.
355 (error (setq gnuserv-string "")
356 (when gnuserv-current-client
357 (gnuserv-write-to-client gnuserv-current-client oops))
358 (setq gnuserv-current-client nil)
359 (signal (car oops) (cdr oops)))
360 (quit (setq gnuserv-string "")
361 (when gnuserv-current-client
362 (gnuserv-write-to-client gnuserv-current-client oops))
363 (setq gnuserv-current-client nil)
365 (setq gnuserv-string "")))
367 (let ((response (car (split-string gnuserv-string "\C-d"))))
368 (setq gnuserv-string "")
369 (error "%s: invalid response from gnuserv" response))))))
371 ;; This function is somewhat of a misnomer. Actually, we write to the
372 ;; server (using `process-send-string' to gnuserv-process), which
373 ;; interprets what we say and forwards it to the client. The
374 ;; incantation server understands is (from gnuserv.c):
376 ;; "FD/LEN:<text>\n" (no quotes)
377 ;; FD - file descriptor of the given client (which we obtained from
378 ;; the server earlier);
379 ;; LEN - length of the stuff we are about to send;
380 ;; <text> - the actual contents of the request.
381 (defun gnuserv-write-to-client (client-id form)
382 "Write the given form to the given client via the gnuserv process."
383 (when (eq (process-status gnuserv-process) 'run)
384 (let* ((result (format "%s" form))
385 (s (format "%s/%d:%s\n" client-id
386 (length result) result)))
387 (process-send-string gnuserv-process s))))
389 ;; The following two functions are helper functions, used by
392 (defun gnuserv-eval (form)
393 "Evaluate form and return result to client."
394 (gnuserv-write-to-client gnuserv-current-client (eval form))
395 (setq gnuserv-current-client nil))
397 (defun gnuserv-eval-quickly (form)
398 "Let client know that we've received the request, and then eval the form.
399 This order is important as not to keep the client waiting."
400 (gnuserv-write-to-client gnuserv-current-client nil)
401 (setq gnuserv-current-client nil)
406 ;; "Execute" a client connection, called by gnuclient. This is the
407 ;; backbone of gnuserv.el.
408 (defun gnuserv-edit-files (type list &rest flags)
409 "For each (line-number . file) pair in LIST, edit the file at line-number.
410 The visited buffers are memorized, so that when \\[gnuserv-edit] is invoked
411 in such a buffer, or when it is killed, or the client's device deleted, the
412 client will be invoked that the edit is finished.
414 TYPE should either be a (tty TTY TERM PID) list, or (x DISPLAY) list.
415 If a flag is `quick', just edit the files in Emacs.
416 If a flag is `view', view the files read-only."
420 (quick (setq quick t))
422 (t (error "Invalid flag %s" flag))))
424 (let* ((old-device-num (length (device-list)))
426 (dest-frame (if (functionp gnuserv-frame)
427 (funcall gnuserv-frame (car type))
429 ;; The gnuserv-frame dependencies are ugly, but it's
430 ;; extremely hard to make that stuff cleaner without
431 ;; breaking everything in sight.
432 (device (cond ((frame-live-p dest-frame)
433 (frame-device dest-frame))
436 (tty (apply 'make-tty-device (cdr type)))
437 (x (make-x-device (cadr type)))
438 (t (error "Invalid device type"))))
441 (frame (cond ((frame-live-p dest-frame)
444 (setq new-frame (make-frame gnuserv-frame-plist
447 (t (selected-frame))))
448 (client (make-gnuclient :id gnuserv-current-client
452 (setq gnuserv-current-client nil)
453 ;; If the device was created by this client, push it to the list.
454 (and (/= old-device-num (length (device-list)))
455 (push device gnuserv-devices))
456 (and (frame-iconified-p frame)
457 (deiconify-frame frame))
458 ;; Visit all the listed files.
460 (let ((line (caar list)) (path (cdar list)))
464 gnuserv-view-file-function
465 gnuserv-find-file-function)
468 ;; Don't memorize the quick and view buffers.
469 (unless (or quick view)
470 (pushnew (current-buffer) (gnuclient-buffers client))
471 (setq gnuserv-minor-mode t)
472 ;; Add the "Done" button to the menubar, only in this buffer.
473 (if (and (featurep 'menubar) current-menubar)
474 (progn (set-buffer-menubar current-menubar)
475 (add-menu-button nil ["Done" gnuserv-edit]))
477 (run-hooks 'gnuserv-visit-hook)
480 ((and (or quick view)
481 (device-on-window-system-p device))
482 ;; Exit if on X device, and quick or view. NOTE: if the
483 ;; client is to finish now, it must absolutely /not/ be
484 ;; included to the list of clients. This way the client-ids
486 (gnuserv-write-to-client (gnuclient-id client) nil))
488 ;; Else, the client gets a vote.
489 (push client gnuserv-clients)
490 ;; Explain buffer exit options. If dest-frame is nil, the
491 ;; user can exit via `delete-frame'. OTOH, if FLAGS are nil
492 ;; and there are some buffers, the user can exit via
494 (if (and (not (or quick view))
495 (gnuclient-buffers client))
497 (substitute-command-keys
498 "Type `\\[gnuserv-edit]' to finish editing"))
501 (substitute-command-keys
502 "Type `\\[delete-frame]' to finish editing")))))))))
505 ;;; Functions that hook into Emacs in various way to enable operation
508 (add-hook 'kill-emacs-hook 'gnuserv-kill-all-clients t)
510 ;; A helper function; used by others. Try avoiding it whenever
511 ;; possible, because it is slow, and conses a list. Use
512 ;; `gnuserv-buffer-p' when appropriate, for instance.
513 (defun gnuserv-buffer-clients (buffer)
514 "Return a list of clients to which BUFFER belongs."
516 (dolist (client gnuserv-clients)
517 (when (memq buffer (gnuclient-buffers client))
521 ;; Like `gnuserv-buffer-clients', but returns a boolean; doesn't
523 (defun gnuserv-buffer-p (buffer)
524 (member* buffer gnuserv-clients
526 :key 'gnuclient-buffers))
528 ;; This function makes sure that a killed buffer is deleted off the
529 ;; list for the particular client.
531 ;; This hooks into `kill-buffer-hook'. It is *not* a replacement for
532 ;; `kill-buffer' (thanks God).
533 (defun gnuserv-kill-buffer-function ()
534 "Remove the buffer from the buffer lists of all the clients it belongs to.
535 Any client that remains \"empty\" after the removal is informed that the
537 (let* ((buf (current-buffer)))
538 (dolist (client (gnuserv-buffer-clients buf))
539 (callf2 delq buf (gnuclient-buffers client))
540 ;; If no more buffers, kill the client.
541 (when (null (gnuclient-buffers client))
542 (gnuserv-kill-client client)))))
544 (add-hook 'kill-buffer-hook 'gnuserv-kill-buffer-function)
546 ;; Ask for confirmation before killing a buffer that belongs to a
548 (defun gnuserv-kill-buffer-query-function ()
549 (or gnuserv-kill-quietly
550 (not (gnuserv-buffer-p (current-buffer)))
552 (format "Buffer %s belongs to gnuserv client(s); kill anyway? "
555 (add-hook 'kill-buffer-query-functions
556 'gnuserv-kill-buffer-query-function)
558 (defun gnuserv-kill-emacs-query-function ()
559 (or gnuserv-kill-quietly
560 (not (some 'gnuclient-buffers gnuserv-clients))
561 (yes-or-no-p "Gnuserv buffers still have clients; exit anyway? ")))
563 (add-hook 'kill-emacs-query-functions
564 'gnuserv-kill-emacs-query-function)
566 ;; If the device of a client is to be deleted, the client should die
567 ;; as well. This is why we hook into `delete-device-hook'.
568 (defun gnuserv-check-device (device)
569 (when (memq device gnuserv-devices)
570 (dolist (client gnuserv-clients)
571 (when (eq device (gnuclient-device client))
572 ;; we must make sure that the server kill doesn't result in
573 ;; killing the device, because it would cause a device-dead
574 ;; error when `delete-device' tries to do the job later.
575 (gnuserv-kill-client client t))))
576 (callf2 delq device gnuserv-devices))
578 (add-hook 'delete-device-hook 'gnuserv-check-device)
580 (defun gnuserv-temp-file-p (buffer)
581 "Return non-nil if BUFFER contains a file considered temporary.
582 These are files whose names suggest they are repeatedly
583 reused to pass information to another program.
585 The variable `gnuserv-temp-file-regexp' controls which filenames
586 are considered temporary."
587 (and (buffer-file-name buffer)
588 (string-match gnuserv-temp-file-regexp (buffer-file-name buffer))))
590 (defun gnuserv-kill-client (client &optional leave-frame)
591 "Kill the gnuclient CLIENT.
592 This will do away with all the associated buffers. If LEAVE-FRAME,
593 the function will not remove the frames associated with the client."
594 ;; Order is important: first delete client from gnuserv-clients, to
595 ;; prevent gnuserv-buffer-done-1 calling us recursively.
596 (callf2 delq client gnuserv-clients)
597 ;; Process the buffers.
598 (mapc 'gnuserv-buffer-done-1 (gnuclient-buffers client))
600 (let ((device (gnuclient-device client)))
601 ;; kill frame created by this client (if any), unless
602 ;; specifically requested otherwise.
604 ;; note: last frame on a device will not be deleted here.
605 (when (and (gnuclient-frame client)
606 (frame-live-p (gnuclient-frame client))
607 (second (device-frame-list device)))
608 (delete-frame (gnuclient-frame client)))
609 ;; If the device is live, created by a client, and no longer used
610 ;; by any client, delete it.
611 (when (and (device-live-p device)
612 (memq device gnuserv-devices)
613 (second (device-list))
614 (not (member* device gnuserv-clients
615 :key 'gnuclient-device)))
616 ;; `gnuserv-check-device' will remove it from `gnuserv-devices'.
617 (delete-device device))))
618 ;; Notify the client.
619 (gnuserv-write-to-client (gnuclient-id client) nil))
621 ;; Do away with the buffer.
622 (defun gnuserv-buffer-done-1 (buffer)
623 (dolist (client (gnuserv-buffer-clients buffer))
624 (callf2 delq buffer (gnuclient-buffers client))
625 (when (null (gnuclient-buffers client))
626 (gnuserv-kill-client client)))
627 ;; Get rid of the buffer.
630 (run-hooks 'gnuserv-done-hook)
631 (setq gnuserv-minor-mode nil)
632 ;; Delete the menu button.
633 (if (and (featurep 'menubar) current-menubar)
634 (delete-menu-item '("Done")))
635 (funcall (if (gnuserv-temp-file-p buffer)
636 gnuserv-done-temp-file-function
637 gnuserv-done-function)
641 ;;; Higher-level functions
643 ;; Choose a `next' server buffer, according to several criteria, and
644 ;; return it. If none are found, return nil.
645 (defun gnuserv-next-buffer ()
646 (let* ((frame (selected-frame))
647 (device (selected-device))
650 ;; If we have a client belonging to this frame, return
651 ;; the first buffer from it.
653 (car (member* frame gnuserv-clients :key 'gnuclient-frame)))
654 (car (gnuclient-buffers client)))
655 ;; Else, look for a device.
657 (memq (selected-device) gnuserv-devices)
659 (car (member* device gnuserv-clients :key 'gnuclient-device))))
660 (car (gnuclient-buffers client)))
661 ;; Else, try to find any client with at least one buffer, and
662 ;; return its first buffer.
664 (car (member-if-not #'null gnuserv-clients
665 :key 'gnuclient-buffers)))
666 (car (gnuclient-buffers client)))
670 (defun gnuserv-buffer-done (buffer)
671 "Mark BUFFER as \"done\" for its client(s).
672 Does the save/backup queries first, and calls `gnuserv-done-function'."
673 ;; Check whether this is the real thing.
674 (unless (gnuserv-buffer-p buffer)
675 (error "%s does not belong to a gnuserv client" buffer))
677 (if (gnuserv-temp-file-p buffer)
678 ;; For a temp file, save, and do NOT make a non-numeric backup
679 ;; Why does server.el explicitly back up temporary files?
680 (let ((version-control nil)
681 (buffer-backed-up (not gnuserv-make-temp-file-backup)))
683 (if (and (buffer-modified-p)
684 (y-or-n-p (concat "Save file " buffer-file-name "? ")))
685 (save-buffer buffer)))
686 (gnuserv-buffer-done-1 buffer))
688 ;; Called by `gnuserv-start-1' to clean everything. Hooked into
689 ;; `kill-emacs-hook', too.
690 (defun gnuserv-kill-all-clients ()
691 "Kill all the gnuserv clients. Ruthlessly."
692 (mapc 'gnuserv-kill-client gnuserv-clients))
694 ;; This serves to run the hook and reset
695 ;; `allow-deletion-of-last-visible-frame'.
696 (defun gnuserv-prepare-shutdown ()
697 (setq allow-deletion-of-last-visible-frame nil)
698 (run-hooks 'gnuserv-shutdown-hook))
700 ;; This is a user-callable function, too.
701 (defun gnuserv-shutdown ()
702 "Shutdown the gnuserv server, if one is currently running.
703 All the clients will be disposed of via the normal methods."
705 (gnuserv-kill-all-clients)
706 (when gnuserv-process
707 (set-process-sentinel gnuserv-process nil)
708 (gnuserv-prepare-shutdown)
710 (delete-process gnuserv-process)
712 (setq gnuserv-process nil)))
714 ;; Actually start the process. Kills all the clients before-hand.
715 (defun gnuserv-start-1 (&optional leave-dead)
716 ;; Shutdown the existing server, if any.
718 ;; If we already had a server, clear out associated status.
720 (setq gnuserv-string ""
721 gnuserv-current-client nil)
722 (let ((process-connection-type t))
723 (setq gnuserv-process
724 (start-process "gnuserv" nil gnuserv-program)))
725 (set-process-sentinel gnuserv-process 'gnuserv-sentinel)
726 (set-process-filter gnuserv-process 'gnuserv-process-filter)
727 (process-kill-without-query gnuserv-process)
728 (setq allow-deletion-of-last-visible-frame t)
729 (run-hooks 'gnuserv-init-hook)))
732 ;;; User-callable functions:
735 (defun gnuserv-running-p ()
736 "Return non-nil if a gnuserv process is running from this XEmacs session."
737 (not (not gnuserv-process)))
740 (defun gnuserv-start (&optional leave-dead)
741 "Allow this Emacs process to be a server for client processes.
742 This starts a gnuserv communications subprocess through which
743 client \"editors\" (gnuclient and gnudoit) can send editing commands to
744 this Emacs job. See the gnuserv(1) manual page for more details.
746 Prefix arg means just kill any existing server communications subprocess."
750 (message "Restarting gnuserv"))
751 (gnuserv-start-1 leave-dead))
753 (defun gnuserv-edit (&optional count)
754 "Mark the current gnuserv editing buffer as \"done\", and switch to next one.
756 Run with a numeric prefix argument, repeat the operation that number
757 of times. If given a universal prefix argument, close all the buffers
758 of this buffer's clients.
760 The `gnuserv-done-function' (bound to `kill-buffer' by default) is
761 called to dispose of the buffer after marking it as done.
763 Files that match `gnuserv-temp-file-regexp' are considered temporary and
764 are saved unconditionally and backed up if `gnuserv-make-temp-file-backup'
765 is non-nil. They are disposed of using `gnuserv-done-temp-file-function'
766 \(also bound to `kill-buffer' by default).
768 When all of a client's buffers are marked as \"done\", the client is notified."
772 (cond ((numberp count)
773 (while (natnump (decf count))
774 (let ((frame (selected-frame)))
775 (gnuserv-buffer-done (current-buffer))
776 (when (eq frame (selected-frame))
777 ;; Switch to the next gnuserv buffer. However, do this
778 ;; only if we remain in the same frame.
779 (let ((next (gnuserv-next-buffer)))
781 (switch-to-buffer next)))))))
783 (let* ((buf (current-buffer))
784 (clients (gnuserv-buffer-clients buf)))
786 (error "%s does not belong to a gnuserv client" buf))
787 (mapc 'gnuserv-kill-client (gnuserv-buffer-clients buf))))))
789 (global-set-key "\C-x#" 'gnuserv-edit)
793 ;;; gnuserv.el ends here