1 ;;; server.el --- Lisp code for GNU Emacs running as server process.
3 ;; Author: William Sommerfeld <wesommer@athena.mit.edu>
4 ;; Last-Modified: 05 Dec 1991
7 ;; Changes by peck@sun.com and by rms.
9 ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs 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 2, or (at your option)
18 ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;;; This Lisp code is run in Emacs when it is to operate as
30 ;;; a server for other processes.
32 ;;; Load this library and do M-x server-edit to enable Emacs as a server.
33 ;;; Emacs runs the program ../arch-lib/emacsserver as a subprocess
34 ;;; for communication with clients. If there are no client buffers to edit,
35 ;;; server-edit acts like (switch-to-buffer (other-buffer))
37 ;;; When some other program runs "the editor" to edit a file,
38 ;;; "the editor" can be the Emacs client program ../lib-src/emacsclient.
39 ;;; This program transmits the file names to Emacs through
40 ;;; the server subprocess, and Emacs visits them and lets you edit them.
42 ;;; Note that any number of clients may dispatch files to emacs to be edited.
44 ;;; When you finish editing a Server buffer, again call server-edit
45 ;;; to mark that buffer as done for the client and switch to the next
46 ;;; Server buffer. When all the buffers for a client have been edited
47 ;;; and exited with server-edit, the client "editor" will return
48 ;;; to the program that invoked it.
50 ;;; Your editing commands and Emacs's display output go to and from
51 ;;; the terminal in the usual way. Thus, server operation is possible
52 ;;; only when Emacs can talk to the terminal at the time you invoke
53 ;;; the client. This is possible in four cases:
55 ;;; 1. On a window system, where Emacs runs in one window and the
56 ;;; program that wants to use "the editor" runs in another.
58 ;;; 2. On a multi-terminal system, where Emacs runs on one terminal and the
59 ;;; program that wants to use "the editor" runs on another.
61 ;;; 3. When the program that wants to use "the editor" is running
62 ;;; as a subprocess of Emacs.
64 ;;; 4. On a system with job control, when Emacs is suspended, the program
65 ;;; that wants to use "the editor" will stop and display
66 ;;; "Waiting for Emacs...". It can then be suspended, and Emacs can be
67 ;;; brought into the foreground for editing. When done editing, Emacs is
68 ;;; suspended again, and the client program is brought into the foreground.
70 ;;; The buffer local variable "server-buffer-clients" lists
71 ;;; the clients who are waiting for this buffer to be edited.
72 ;;; The global variable "server-clients" lists all the waiting clients,
73 ;;; and which files are yet to be edited for each.
77 (defvar server-program
"emacsserver"
78 "*The program to use as the edit server")
80 (defvar server-visit-hook nil
81 "*List of hooks to call when switching to a buffer for the Emacs server.")
83 (defvar server-process nil
84 "the current server process")
86 (defvar server-previous-string
"")
88 (defvar server-clients nil
89 "List of current server clients.
90 Each element is (CLIENTID FILES...) where CLIENTID is a string
91 that can be given to the server process to identify a client.
92 When a buffer is marked as \"done\", it is removed from this list.")
94 (defvar server-buffer-clients nil
95 "List of clientids for clients requesting editing of current buffer.")
96 ;; Changing major modes should not erase this local.
97 (put 'server-buffer-clients
'permanent-local t
)
99 (defvar server-temp-file-regexp
"^/tmp/Re\\|/draft$"
100 "*Regexp which should match filenames of temporary files
101 which are deleted and reused after each edit
102 by the programs that invoke the emacs server.")
104 (make-variable-buffer-local 'server-buffer-clients
)
105 (put 'server-buffer-clients
'permanent-local t
)
106 (setq-default server-buffer-clients nil
)
107 (or (assq 'server-buffer-clients minor-mode-alist
)
108 (setq minor-mode-alist
(cons '(server-buffer-clients " Server") minor-mode-alist
)))
110 ;; If a *server* buffer exists,
111 ;; write STRING to it for logging purposes.
112 (defun server-log (string)
113 (if (get-buffer "*server*")
115 (set-buffer "*server*")
116 (goto-char (point-max))
118 (or (bobp) (newline)))))
120 (defun server-sentinel (proc msg
)
121 (cond ((eq (process-status proc
) 'exit
)
122 (server-log (message "Server subprocess exited")))
123 ((eq (process-status proc
) 'signal
)
124 (server-log (message "Server subprocess killed")))))
127 (defun server-start (&optional leave-dead
)
128 "Allow this Emacs process to be a server for client processes.
129 This starts a server communications subprocess through which
130 client \"editors\" can send your editing commands to this Emacs job.
131 To use the server, set up the program `etc/emacsclient' in the
132 Emacs distribution as your standard \"editor\".
134 Prefix arg means just kill any existing server communications subprocess."
139 (set-process-sentinel server-process nil
)
140 (condition-case () (delete-process server-process
) (error nil
))))
141 (condition-case () (delete-file "~/.emacs_server") (error nil
))
142 ;; If we already had a server, clear out associated status.
143 (while server-clients
144 (let ((buffer (nth 1 (car server-clients
))))
145 (server-buffer-done buffer
)))
149 (server-log (message "Restarting server")))
150 (setq server-process
(start-process "server" nil server-program
))
151 (set-process-sentinel server-process
'server-sentinel
)
152 (set-process-filter server-process
'server-process-filter
)
153 (process-kill-without-query server-process
)))
155 ;Process a request from the server to edit some files.
156 ;Format of STRING is "Client: CLIENTID PATH PATH PATH... \n"
157 (defun server-process-filter (proc string
)
159 (setq string
(concat server-previous-string string
))
160 (if (not (and (eq ?
\n (aref string
(1- (length string
))))
161 (eq 0 (string-match "Client: " string
))))
162 ;; If input is not complete, save it for later.
163 (setq server-previous-string string
)
164 ;; If it is complete, process it now, and discard what was saved.
165 (setq string
(substring string
(match-end 0)))
166 (setq server-previous-string
"")
167 (let ((client (list (substring string
0 (string-match " " string
))))
170 (setq string
(substring string
(match-end 0)))
171 (while (string-match "[^ ]+ " string
)
173 (substring string
(match-beginning 0) (1- (match-end 0)))))
174 (setq string
(substring string
(match-end 0)))
175 (if (string-match "\\`\\+[0-9]+\\'" arg
)
176 (setq lineno
(read (substring arg
1)))
178 (cons (list arg lineno
)
181 (server-visit-files files client
)
182 ;; CLIENT is now a list (CLIENTNUM BUFFERS...)
183 (setq server-clients
(cons client server-clients
))
184 (switch-to-buffer (nth 1 client
))
185 (message (substitute-command-keys
186 "When done with a buffer, type \\[server-edit].")))))
188 (defun server-visit-files (files client
)
189 "Finds FILES and returns the list CLIENT with the buffers nconc'd.
190 FILES is an alist whose elements are (FILENAME LINENUMBER)."
194 ;; If there is an existing buffer modified or the file is modified,
196 ;; If there is an existing buffer with deleted file, offer to write it.
197 (let* ((filen (car (car files
)))
198 (obuf (get-file-buffer filen
)))
199 (if (and obuf
(set-buffer obuf
))
200 (if (file-exists-p filen
)
201 (if (or (not (verify-visited-file-modtime obuf
))
202 (buffer-modified-p obuf
))
203 (revert-buffer t nil
))
205 (concat "File no longer exists: "
207 ", write buffer to file? "))
209 (set-buffer (find-file-noselect filen
))
210 (run-hooks 'server-visit-hook
)))
211 (goto-line (nth 1 (car files
)))
212 (setq server-buffer-clients
(cons (car client
) server-buffer-clients
))
213 (setq client-record
(cons (current-buffer) client-record
)))
214 (setq files
(cdr files
)))
215 (nconc client client-record
)))
217 (defun server-buffer-done (buffer)
218 "Mark BUFFER as \"done\" for its client(s).
219 Buries the buffer, and returns another server buffer
220 as a suggestion for what to select next."
221 (let ((running (eq (process-status server-process
) 'run
))
223 (old-clients server-clients
))
225 (let ((client (car old-clients
)))
227 (setq next-buffer
(nth 1 (memq buffer client
))))
229 ;; If client now has no pending buffers,
230 ;; tell it that it is done, and forget it entirely.
234 (send-string server-process
235 (format "Close: %s Done\n" (car client
)))
236 (server-log (format "Close: %s Done\n" (car client
)))))
237 (setq server-clients
(delq client server-clients
))))
238 (setq old-clients
(cdr old-clients
)))
239 (if (buffer-name buffer
)
242 (setq server-buffer-clients nil
)))
246 (defun server-temp-file-p (buffer)
247 "Return non-nil if BUFFER contains a file considered temporary.
248 These are files whose names suggest they are repeatedly
249 reused to pass information to another program.
251 The variable `server-temp-file-regexp' controls which filenames
252 are considered temporary."
253 (and (buffer-file-name buffer
)
254 (string-match server-temp-file-regexp
(buffer-file-name buffer
))))
256 (defun server-done ()
257 "Offer to save current buffer, mark it as \"done\" for clients,
258 bury it, and return a suggested buffer to select next."
259 (let ((buffer (current-buffer)))
260 (if server-buffer-clients
262 (if (server-temp-file-p buffer
)
264 (write-region (point-min) (point-max)
265 (concat buffer-file-name
"~"))
266 (kill-buffer buffer
))
267 (if (and (buffer-modified-p)
268 (y-or-n-p (concat "Save file " buffer-file-name
"? ")))
269 (save-buffer buffer
)))
270 (server-buffer-done buffer
)))))
272 (defun server-edit (&optional arg
)
273 "Switch to next server editing buffer; say \"Done\" for current buffer.
274 If a server buffer is current, it is marked \"done\" and optionally saved.
275 When all of a client's buffers are marked as \"done\", the client is notified.
277 Temporary files such as MH <draft> files are always saved and backed up,
278 no questions asked. The variable `server-temp-file-regexp' controls
279 which filenames are considered temporary.
281 If invoked with a prefix argument, or if there is no server process running,
282 starts server process and that is all. Invoked by \\[server-edit]."
287 (memq (process-status server-process
) '(signal exit
)))
289 (server-switch-buffer (server-done))))
291 (defun server-switch-buffer (next-buffer)
292 "Switch to another buffer, preferably one that has a client.
293 Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it."
295 (if (and (bufferp next-buffer
)
296 (buffer-name next-buffer
))
297 (switch-to-buffer next-buffer
)
298 ;; If NEXT-BUFFER is a dead buffer,
299 ;; remove the server records for it
300 ;; and try the next surviving server buffer.
301 (server-switch-buffer
302 (server-buffer-done next-buffer
)))
304 (server-switch-buffer (nth 1 (car server-clients
)))
305 (switch-to-buffer (other-buffer)))))
307 (global-set-key "\C-x#" 'server-edit
)
309 ;;; server.el ends here