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