Mercurial > emacs
annotate lisp/server.el @ 832:f40ed8ebb09e
*** empty log message ***
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Tue, 21 Jul 1992 21:11:41 +0000 |
parents | 38b2499cb3e9 |
children | bf829a2d63b4 |
rev | line source |
---|---|
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
445
diff
changeset
|
1 ;;; server.el --- Lisp code for GNU Emacs running as server process. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
445
diff
changeset
|
2 |
787
3cece0106722
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
702
diff
changeset
|
3 ;; Author: William Sommerfeld <wesommer@athena.mit.edu> |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
787
diff
changeset
|
4 ;; Last-Modified: 05 Dec 1991 |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
5 ;; Keywords: processes |
787
3cece0106722
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
702
diff
changeset
|
6 |
3cece0106722
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
702
diff
changeset
|
7 ;; Changes by peck@sun.com and by rms. |
3cece0106722
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
702
diff
changeset
|
8 |
50 | 9 ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc. |
10 | |
11 ;; This file is part of GNU Emacs. | |
12 | |
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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
787
diff
changeset
|
15 ;; the Free Software Foundation; either version 2, or (at your option) |
50 | 16 ;; any later version. |
17 | |
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. | |
22 | |
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. | |
26 | |
787
3cece0106722
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
702
diff
changeset
|
27 ;;; Commentary: |
50 | 28 |
29 ;;; This Lisp code is run in Emacs when it is to operate as | |
30 ;;; a server for other processes. | |
31 | |
32 ;;; Load this library and do M-x server-edit to enable Emacs as a server. | |
445 | 33 ;;; Emacs runs the program ../arch-lib/emacsserver as a subprocess |
50 | 34 ;;; for communication with clients. If there are no client buffers to edit, |
35 ;;; server-edit acts like (switch-to-buffer (other-buffer)) | |
36 | |
37 ;;; When some other program runs "the editor" to edit a file, | |
445 | 38 ;;; "the editor" can be the Emacs client program ../lib-src/emacsclient. |
50 | 39 ;;; This program transmits the file names to Emacs through |
40 ;;; the server subprocess, and Emacs visits them and lets you edit them. | |
41 | |
42 ;;; Note that any number of clients may dispatch files to emacs to be edited. | |
43 | |
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. | |
49 | |
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: | |
54 | |
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. | |
57 | |
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. | |
60 | |
61 ;;; 3. When the program that wants to use "the editor" is running | |
62 ;;; as a subprocess of Emacs. | |
63 | |
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. | |
69 | |
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. | |
787
3cece0106722
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
702
diff
changeset
|
74 |
3cece0106722
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
702
diff
changeset
|
75 ;;; Code: |
50 | 76 |
77 (defvar server-program "emacsserver" | |
78 "*The program to use as the edit server") | |
79 | |
80 (defvar server-visit-hook nil | |
81 "*List of hooks to call when switching to a buffer for the Emacs server.") | |
82 | |
83 (defvar server-process nil | |
84 "the current server process") | |
85 | |
138 | 86 (defvar server-previous-string "") |
87 | |
50 | 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.") | |
93 | |
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) | |
98 | |
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.") | |
103 | |
104 (make-variable-buffer-local 'server-buffer-clients) | |
702
c89caa39704a
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
658
diff
changeset
|
105 (put 'server-buffer-clients 'permanent-local t) |
50 | 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))) | |
109 | |
110 ;; If a *server* buffer exists, | |
111 ;; write STRING to it for logging purposes. | |
112 (defun server-log (string) | |
113 (if (get-buffer "*server*") | |
114 (save-excursion | |
115 (set-buffer "*server*") | |
116 (goto-char (point-max)) | |
117 (insert string) | |
118 (or (bobp) (newline))))) | |
119 | |
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"))))) | |
125 | |
256 | 126 ;;;###autoload |
50 | 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\". | |
133 | |
134 Prefix arg means just kill any existing server communications subprocess." | |
135 (interactive "P") | |
136 ;; kill it dead! | |
137 (if server-process | |
138 (progn | |
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))) | |
146 (if leave-dead | |
147 nil | |
148 (if server-process | |
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))) | |
154 | |
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) | |
158 (server-log string) | |
138 | 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. | |
50 | 165 (setq string (substring string (match-end 0))) |
138 | 166 (setq server-previous-string "") |
50 | 167 (let ((client (list (substring string 0 (string-match " " string)))) |
168 (files nil) | |
169 (lineno 1)) | |
170 (setq string (substring string (match-end 0))) | |
171 (while (string-match "[^ ]+ " string) | |
172 (let ((arg | |
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))) | |
177 (setq files | |
178 (cons (list arg lineno) | |
179 files)) | |
180 (setq lineno 1)))) | |
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]."))))) | |
187 | |
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)." | |
191 (let (client-record) | |
192 (while files | |
193 (save-excursion | |
194 ;; If there is an existing buffer modified or the file is modified, | |
195 ;; revert it. | |
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)) | |
204 (if (y-or-n-p | |
205 (concat "File no longer exists: " | |
206 filen | |
207 ", write buffer to file? ")) | |
208 (write-file filen))) | |
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))) | |
216 | |
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)) | |
222 (next-buffer nil) | |
223 (old-clients server-clients)) | |
224 (while old-clients | |
225 (let ((client (car old-clients))) | |
226 (or next-buffer | |
227 (setq next-buffer (nth 1 (memq buffer client)))) | |
228 (delq buffer client) | |
229 ;; If client now has no pending buffers, | |
230 ;; tell it that it is done, and forget it entirely. | |
231 (if (cdr client) nil | |
232 (if running | |
233 (progn | |
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) | |
240 (save-excursion | |
241 (set-buffer buffer) | |
242 (setq server-buffer-clients nil))) | |
243 (bury-buffer buffer) | |
244 next-buffer)) | |
245 | |
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. | |
250 | |
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)))) | |
255 | |
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 | |
261 (progn | |
262 (if (server-temp-file-p buffer) | |
263 (progn (save-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))))) | |
271 | |
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. | |
276 | |
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. | |
280 | |
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]." | |
283 | |
284 (interactive "P") | |
285 (if (or arg | |
286 (not server-process) | |
287 (memq (process-status server-process) '(signal exit))) | |
288 (server-start nil) | |
289 (server-switch-buffer (server-done)))) | |
290 | |
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." | |
294 (if next-buffer | |
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))) | |
303 (if server-clients | |
304 (server-switch-buffer (nth 1 (car server-clients))) | |
305 (switch-to-buffer (other-buffer))))) | |
306 | |
307 (global-set-key "\C-x#" 'server-edit) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
445
diff
changeset
|
308 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
445
diff
changeset
|
309 ;;; server.el ends here |