*** empty log message ***
[emacs.git] / lisp / server.el
blob064a3165a0b7e8ddd1095744ba05a1e9c432d28a
1 ;;; server.el --- Lisp code for GNU Emacs running as server process.
3 ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 ;; Author William Sommerfeld, wesommer@athena.mit.edu.
5 ;; Changes by peck@sun.com and by rms.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; This Lisp code is run in Emacs when it is to operate as
25 ;;; a server for other processes.
27 ;;; Load this library and do M-x server-edit to enable Emacs as a server.
28 ;;; Emacs runs the program ../arch-lib/emacsserver as a subprocess
29 ;;; for communication with clients. If there are no client buffers to edit,
30 ;;; server-edit acts like (switch-to-buffer (other-buffer))
32 ;;; When some other program runs "the editor" to edit a file,
33 ;;; "the editor" can be the Emacs client program ../lib-src/emacsclient.
34 ;;; This program transmits the file names to Emacs through
35 ;;; the server subprocess, and Emacs visits them and lets you edit them.
37 ;;; Note that any number of clients may dispatch files to emacs to be edited.
39 ;;; When you finish editing a Server buffer, again call server-edit
40 ;;; to mark that buffer as done for the client and switch to the next
41 ;;; Server buffer. When all the buffers for a client have been edited
42 ;;; and exited with server-edit, the client "editor" will return
43 ;;; to the program that invoked it.
45 ;;; Your editing commands and Emacs's display output go to and from
46 ;;; the terminal in the usual way. Thus, server operation is possible
47 ;;; only when Emacs can talk to the terminal at the time you invoke
48 ;;; the client. This is possible in four cases:
50 ;;; 1. On a window system, where Emacs runs in one window and the
51 ;;; program that wants to use "the editor" runs in another.
53 ;;; 2. On a multi-terminal system, where Emacs runs on one terminal and the
54 ;;; program that wants to use "the editor" runs on another.
56 ;;; 3. When the program that wants to use "the editor" is running
57 ;;; as a subprocess of Emacs.
59 ;;; 4. On a system with job control, when Emacs is suspended, the program
60 ;;; that wants to use "the editor" will stop and display
61 ;;; "Waiting for Emacs...". It can then be suspended, and Emacs can be
62 ;;; brought into the foreground for editing. When done editing, Emacs is
63 ;;; suspended again, and the client program is brought into the foreground.
65 ;;; The buffer local variable "server-buffer-clients" lists
66 ;;; the clients who are waiting for this buffer to be edited.
67 ;;; The global variable "server-clients" lists all the waiting clients,
68 ;;; and which files are yet to be edited for each.
70 (defvar server-program "emacsserver"
71 "*The program to use as the edit server")
73 (defvar server-visit-hook nil
74 "*List of hooks to call when switching to a buffer for the Emacs server.")
76 (defvar server-process nil
77 "the current server process")
79 (defvar server-previous-string "")
81 (defvar server-clients nil
82 "List of current server clients.
83 Each element is (CLIENTID FILES...) where CLIENTID is a string
84 that can be given to the server process to identify a client.
85 When a buffer is marked as \"done\", it is removed from this list.")
87 (defvar server-buffer-clients nil
88 "List of clientids for clients requesting editing of current buffer.")
89 ;; Changing major modes should not erase this local.
90 (put 'server-buffer-clients 'permanent-local t)
92 (defvar server-temp-file-regexp "^/tmp/Re\\|/draft$"
93 "*Regexp which should match filenames of temporary files
94 which are deleted and reused after each edit
95 by the programs that invoke the emacs server.")
97 (make-variable-buffer-local 'server-buffer-clients)
98 (setq-default server-buffer-clients nil)
99 (or (assq 'server-buffer-clients minor-mode-alist)
100 (setq minor-mode-alist (cons '(server-buffer-clients " Server") minor-mode-alist)))
102 ;; If a *server* buffer exists,
103 ;; write STRING to it for logging purposes.
104 (defun server-log (string)
105 (if (get-buffer "*server*")
106 (save-excursion
107 (set-buffer "*server*")
108 (goto-char (point-max))
109 (insert string)
110 (or (bobp) (newline)))))
112 (defun server-sentinel (proc msg)
113 (cond ((eq (process-status proc) 'exit)
114 (server-log (message "Server subprocess exited")))
115 ((eq (process-status proc) 'signal)
116 (server-log (message "Server subprocess killed")))))
118 ;;;###autoload
119 (defun server-start (&optional leave-dead)
120 "Allow this Emacs process to be a server for client processes.
121 This starts a server communications subprocess through which
122 client \"editors\" can send your editing commands to this Emacs job.
123 To use the server, set up the program `etc/emacsclient' in the
124 Emacs distribution as your standard \"editor\".
126 Prefix arg means just kill any existing server communications subprocess."
127 (interactive "P")
128 ;; kill it dead!
129 (if server-process
130 (progn
131 (set-process-sentinel server-process nil)
132 (condition-case () (delete-process server-process) (error nil))))
133 (condition-case () (delete-file "~/.emacs_server") (error nil))
134 ;; If we already had a server, clear out associated status.
135 (while server-clients
136 (let ((buffer (nth 1 (car server-clients))))
137 (server-buffer-done buffer)))
138 (if leave-dead
140 (if server-process
141 (server-log (message "Restarting server")))
142 (setq server-process (start-process "server" nil server-program))
143 (set-process-sentinel server-process 'server-sentinel)
144 (set-process-filter server-process 'server-process-filter)
145 (process-kill-without-query server-process)))
147 ;Process a request from the server to edit some files.
148 ;Format of STRING is "Client: CLIENTID PATH PATH PATH... \n"
149 (defun server-process-filter (proc string)
150 (server-log string)
151 (setq string (concat server-previous-string string))
152 (if (not (and (eq ?\n (aref string (1- (length string))))
153 (eq 0 (string-match "Client: " string))))
154 ;; If input is not complete, save it for later.
155 (setq server-previous-string string)
156 ;; If it is complete, process it now, and discard what was saved.
157 (setq string (substring string (match-end 0)))
158 (setq server-previous-string "")
159 (let ((client (list (substring string 0 (string-match " " string))))
160 (files nil)
161 (lineno 1))
162 (setq string (substring string (match-end 0)))
163 (while (string-match "[^ ]+ " string)
164 (let ((arg
165 (substring string (match-beginning 0) (1- (match-end 0)))))
166 (setq string (substring string (match-end 0)))
167 (if (string-match "\\`\\+[0-9]+\\'" arg)
168 (setq lineno (read (substring arg 1)))
169 (setq files
170 (cons (list arg lineno)
171 files))
172 (setq lineno 1))))
173 (server-visit-files files client)
174 ;; CLIENT is now a list (CLIENTNUM BUFFERS...)
175 (setq server-clients (cons client server-clients))
176 (switch-to-buffer (nth 1 client))
177 (message (substitute-command-keys
178 "When done with a buffer, type \\[server-edit].")))))
180 (defun server-visit-files (files client)
181 "Finds FILES and returns the list CLIENT with the buffers nconc'd.
182 FILES is an alist whose elements are (FILENAME LINENUMBER)."
183 (let (client-record)
184 (while files
185 (save-excursion
186 ;; If there is an existing buffer modified or the file is modified,
187 ;; revert it.
188 ;; If there is an existing buffer with deleted file, offer to write it.
189 (let* ((filen (car (car files)))
190 (obuf (get-file-buffer filen)))
191 (if (and obuf (set-buffer obuf))
192 (if (file-exists-p filen)
193 (if (or (not (verify-visited-file-modtime obuf))
194 (buffer-modified-p obuf))
195 (revert-buffer t nil))
196 (if (y-or-n-p
197 (concat "File no longer exists: "
198 filen
199 ", write buffer to file? "))
200 (write-file filen)))
201 (set-buffer (find-file-noselect filen))
202 (run-hooks 'server-visit-hook)))
203 (goto-line (nth 1 (car files)))
204 (setq server-buffer-clients (cons (car client) server-buffer-clients))
205 (setq client-record (cons (current-buffer) client-record)))
206 (setq files (cdr files)))
207 (nconc client client-record)))
209 (defun server-buffer-done (buffer)
210 "Mark BUFFER as \"done\" for its client(s).
211 Buries the buffer, and returns another server buffer
212 as a suggestion for what to select next."
213 (let ((running (eq (process-status server-process) 'run))
214 (next-buffer nil)
215 (old-clients server-clients))
216 (while old-clients
217 (let ((client (car old-clients)))
218 (or next-buffer
219 (setq next-buffer (nth 1 (memq buffer client))))
220 (delq buffer client)
221 ;; If client now has no pending buffers,
222 ;; tell it that it is done, and forget it entirely.
223 (if (cdr client) nil
224 (if running
225 (progn
226 (send-string server-process
227 (format "Close: %s Done\n" (car client)))
228 (server-log (format "Close: %s Done\n" (car client)))))
229 (setq server-clients (delq client server-clients))))
230 (setq old-clients (cdr old-clients)))
231 (if (buffer-name buffer)
232 (save-excursion
233 (set-buffer buffer)
234 (setq server-buffer-clients nil)))
235 (bury-buffer buffer)
236 next-buffer))
238 (defun server-temp-file-p (buffer)
239 "Return non-nil if BUFFER contains a file considered temporary.
240 These are files whose names suggest they are repeatedly
241 reused to pass information to another program.
243 The variable `server-temp-file-regexp' controls which filenames
244 are considered temporary."
245 (and (buffer-file-name buffer)
246 (string-match server-temp-file-regexp (buffer-file-name buffer))))
248 (defun server-done ()
249 "Offer to save current buffer, mark it as \"done\" for clients,
250 bury it, and return a suggested buffer to select next."
251 (let ((buffer (current-buffer)))
252 (if server-buffer-clients
253 (progn
254 (if (server-temp-file-p buffer)
255 (progn (save-buffer)
256 (write-region (point-min) (point-max)
257 (concat buffer-file-name "~"))
258 (kill-buffer buffer))
259 (if (and (buffer-modified-p)
260 (y-or-n-p (concat "Save file " buffer-file-name "? ")))
261 (save-buffer buffer)))
262 (server-buffer-done buffer)))))
264 (defun server-edit (&optional arg)
265 "Switch to next server editing buffer; say \"Done\" for current buffer.
266 If a server buffer is current, it is marked \"done\" and optionally saved.
267 When all of a client's buffers are marked as \"done\", the client is notified.
269 Temporary files such as MH <draft> files are always saved and backed up,
270 no questions asked. The variable `server-temp-file-regexp' controls
271 which filenames are considered temporary.
273 If invoked with a prefix argument, or if there is no server process running,
274 starts server process and that is all. Invoked by \\[server-edit]."
276 (interactive "P")
277 (if (or arg
278 (not server-process)
279 (memq (process-status server-process) '(signal exit)))
280 (server-start nil)
281 (server-switch-buffer (server-done))))
283 (defun server-switch-buffer (next-buffer)
284 "Switch to another buffer, preferably one that has a client.
285 Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it."
286 (if next-buffer
287 (if (and (bufferp next-buffer)
288 (buffer-name next-buffer))
289 (switch-to-buffer next-buffer)
290 ;; If NEXT-BUFFER is a dead buffer,
291 ;; remove the server records for it
292 ;; and try the next surviving server buffer.
293 (server-switch-buffer
294 (server-buffer-done next-buffer)))
295 (if server-clients
296 (server-switch-buffer (nth 1 (car server-clients)))
297 (switch-to-buffer (other-buffer)))))
299 (global-set-key "\C-x#" 'server-edit)
301 ;;; server.el ends here