Mercurial > emacs
annotate lisp/server.el @ 731:5c6db33a9ef6
Initial revision
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Tue, 30 Jun 1992 13:49:39 +0000 |
parents | c89caa39704a |
children | 3cece0106722 |
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 |
50 | 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. | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
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. | |
13 | |
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. | |
18 | |
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. | |
22 | |
23 | |
24 ;;; This Lisp code is run in Emacs when it is to operate as | |
25 ;;; a server for other processes. | |
26 | |
27 ;;; Load this library and do M-x server-edit to enable Emacs as a server. | |
445 | 28 ;;; Emacs runs the program ../arch-lib/emacsserver as a subprocess |
50 | 29 ;;; for communication with clients. If there are no client buffers to edit, |
30 ;;; server-edit acts like (switch-to-buffer (other-buffer)) | |
31 | |
32 ;;; When some other program runs "the editor" to edit a file, | |
445 | 33 ;;; "the editor" can be the Emacs client program ../lib-src/emacsclient. |
50 | 34 ;;; This program transmits the file names to Emacs through |
35 ;;; the server subprocess, and Emacs visits them and lets you edit them. | |
36 | |
37 ;;; Note that any number of clients may dispatch files to emacs to be edited. | |
38 | |
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. | |
44 | |
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: | |
49 | |
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. | |
52 | |
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. | |
55 | |
56 ;;; 3. When the program that wants to use "the editor" is running | |
57 ;;; as a subprocess of Emacs. | |
58 | |
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. | |
64 | |
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. | |
69 | |
70 (defvar server-program "emacsserver" | |
71 "*The program to use as the edit server") | |
72 | |
73 (defvar server-visit-hook nil | |
74 "*List of hooks to call when switching to a buffer for the Emacs server.") | |
75 | |
76 (defvar server-process nil | |
77 "the current server process") | |
78 | |
138 | 79 (defvar server-previous-string "") |
80 | |
50 | 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.") | |
86 | |
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) | |
91 | |
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.") | |
96 | |
97 (make-variable-buffer-local 'server-buffer-clients) | |
702
c89caa39704a
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
658
diff
changeset
|
98 (put 'server-buffer-clients 'permanent-local t) |
50 | 99 (setq-default server-buffer-clients nil) |
100 (or (assq 'server-buffer-clients minor-mode-alist) | |
101 (setq minor-mode-alist (cons '(server-buffer-clients " Server") minor-mode-alist))) | |
102 | |
103 ;; If a *server* buffer exists, | |
104 ;; write STRING to it for logging purposes. | |
105 (defun server-log (string) | |
106 (if (get-buffer "*server*") | |
107 (save-excursion | |
108 (set-buffer "*server*") | |
109 (goto-char (point-max)) | |
110 (insert string) | |
111 (or (bobp) (newline))))) | |
112 | |
113 (defun server-sentinel (proc msg) | |
114 (cond ((eq (process-status proc) 'exit) | |
115 (server-log (message "Server subprocess exited"))) | |
116 ((eq (process-status proc) 'signal) | |
117 (server-log (message "Server subprocess killed"))))) | |
118 | |
256 | 119 ;;;###autoload |
50 | 120 (defun server-start (&optional leave-dead) |
121 "Allow this Emacs process to be a server for client processes. | |
122 This starts a server communications subprocess through which | |
123 client \"editors\" can send your editing commands to this Emacs job. | |
124 To use the server, set up the program `etc/emacsclient' in the | |
125 Emacs distribution as your standard \"editor\". | |
126 | |
127 Prefix arg means just kill any existing server communications subprocess." | |
128 (interactive "P") | |
129 ;; kill it dead! | |
130 (if server-process | |
131 (progn | |
132 (set-process-sentinel server-process nil) | |
133 (condition-case () (delete-process server-process) (error nil)))) | |
134 (condition-case () (delete-file "~/.emacs_server") (error nil)) | |
135 ;; If we already had a server, clear out associated status. | |
136 (while server-clients | |
137 (let ((buffer (nth 1 (car server-clients)))) | |
138 (server-buffer-done buffer))) | |
139 (if leave-dead | |
140 nil | |
141 (if server-process | |
142 (server-log (message "Restarting server"))) | |
143 (setq server-process (start-process "server" nil server-program)) | |
144 (set-process-sentinel server-process 'server-sentinel) | |
145 (set-process-filter server-process 'server-process-filter) | |
146 (process-kill-without-query server-process))) | |
147 | |
148 ;Process a request from the server to edit some files. | |
149 ;Format of STRING is "Client: CLIENTID PATH PATH PATH... \n" | |
150 (defun server-process-filter (proc string) | |
151 (server-log string) | |
138 | 152 (setq string (concat server-previous-string string)) |
153 (if (not (and (eq ?\n (aref string (1- (length string)))) | |
154 (eq 0 (string-match "Client: " string)))) | |
155 ;; If input is not complete, save it for later. | |
156 (setq server-previous-string string) | |
157 ;; If it is complete, process it now, and discard what was saved. | |
50 | 158 (setq string (substring string (match-end 0))) |
138 | 159 (setq server-previous-string "") |
50 | 160 (let ((client (list (substring string 0 (string-match " " string)))) |
161 (files nil) | |
162 (lineno 1)) | |
163 (setq string (substring string (match-end 0))) | |
164 (while (string-match "[^ ]+ " string) | |
165 (let ((arg | |
166 (substring string (match-beginning 0) (1- (match-end 0))))) | |
167 (setq string (substring string (match-end 0))) | |
168 (if (string-match "\\`\\+[0-9]+\\'" arg) | |
169 (setq lineno (read (substring arg 1))) | |
170 (setq files | |
171 (cons (list arg lineno) | |
172 files)) | |
173 (setq lineno 1)))) | |
174 (server-visit-files files client) | |
175 ;; CLIENT is now a list (CLIENTNUM BUFFERS...) | |
176 (setq server-clients (cons client server-clients)) | |
177 (switch-to-buffer (nth 1 client)) | |
178 (message (substitute-command-keys | |
179 "When done with a buffer, type \\[server-edit]."))))) | |
180 | |
181 (defun server-visit-files (files client) | |
182 "Finds FILES and returns the list CLIENT with the buffers nconc'd. | |
183 FILES is an alist whose elements are (FILENAME LINENUMBER)." | |
184 (let (client-record) | |
185 (while files | |
186 (save-excursion | |
187 ;; If there is an existing buffer modified or the file is modified, | |
188 ;; revert it. | |
189 ;; If there is an existing buffer with deleted file, offer to write it. | |
190 (let* ((filen (car (car files))) | |
191 (obuf (get-file-buffer filen))) | |
192 (if (and obuf (set-buffer obuf)) | |
193 (if (file-exists-p filen) | |
194 (if (or (not (verify-visited-file-modtime obuf)) | |
195 (buffer-modified-p obuf)) | |
196 (revert-buffer t nil)) | |
197 (if (y-or-n-p | |
198 (concat "File no longer exists: " | |
199 filen | |
200 ", write buffer to file? ")) | |
201 (write-file filen))) | |
202 (set-buffer (find-file-noselect filen)) | |
203 (run-hooks 'server-visit-hook))) | |
204 (goto-line (nth 1 (car files))) | |
205 (setq server-buffer-clients (cons (car client) server-buffer-clients)) | |
206 (setq client-record (cons (current-buffer) client-record))) | |
207 (setq files (cdr files))) | |
208 (nconc client client-record))) | |
209 | |
210 (defun server-buffer-done (buffer) | |
211 "Mark BUFFER as \"done\" for its client(s). | |
212 Buries the buffer, and returns another server buffer | |
213 as a suggestion for what to select next." | |
214 (let ((running (eq (process-status server-process) 'run)) | |
215 (next-buffer nil) | |
216 (old-clients server-clients)) | |
217 (while old-clients | |
218 (let ((client (car old-clients))) | |
219 (or next-buffer | |
220 (setq next-buffer (nth 1 (memq buffer client)))) | |
221 (delq buffer client) | |
222 ;; If client now has no pending buffers, | |
223 ;; tell it that it is done, and forget it entirely. | |
224 (if (cdr client) nil | |
225 (if running | |
226 (progn | |
227 (send-string server-process | |
228 (format "Close: %s Done\n" (car client))) | |
229 (server-log (format "Close: %s Done\n" (car client))))) | |
230 (setq server-clients (delq client server-clients)))) | |
231 (setq old-clients (cdr old-clients))) | |
232 (if (buffer-name buffer) | |
233 (save-excursion | |
234 (set-buffer buffer) | |
235 (setq server-buffer-clients nil))) | |
236 (bury-buffer buffer) | |
237 next-buffer)) | |
238 | |
239 (defun server-temp-file-p (buffer) | |
240 "Return non-nil if BUFFER contains a file considered temporary. | |
241 These are files whose names suggest they are repeatedly | |
242 reused to pass information to another program. | |
243 | |
244 The variable `server-temp-file-regexp' controls which filenames | |
245 are considered temporary." | |
246 (and (buffer-file-name buffer) | |
247 (string-match server-temp-file-regexp (buffer-file-name buffer)))) | |
248 | |
249 (defun server-done () | |
250 "Offer to save current buffer, mark it as \"done\" for clients, | |
251 bury it, and return a suggested buffer to select next." | |
252 (let ((buffer (current-buffer))) | |
253 (if server-buffer-clients | |
254 (progn | |
255 (if (server-temp-file-p buffer) | |
256 (progn (save-buffer) | |
257 (write-region (point-min) (point-max) | |
258 (concat buffer-file-name "~")) | |
259 (kill-buffer buffer)) | |
260 (if (and (buffer-modified-p) | |
261 (y-or-n-p (concat "Save file " buffer-file-name "? "))) | |
262 (save-buffer buffer))) | |
263 (server-buffer-done buffer))))) | |
264 | |
265 (defun server-edit (&optional arg) | |
266 "Switch to next server editing buffer; say \"Done\" for current buffer. | |
267 If a server buffer is current, it is marked \"done\" and optionally saved. | |
268 When all of a client's buffers are marked as \"done\", the client is notified. | |
269 | |
270 Temporary files such as MH <draft> files are always saved and backed up, | |
271 no questions asked. The variable `server-temp-file-regexp' controls | |
272 which filenames are considered temporary. | |
273 | |
274 If invoked with a prefix argument, or if there is no server process running, | |
275 starts server process and that is all. Invoked by \\[server-edit]." | |
276 | |
277 (interactive "P") | |
278 (if (or arg | |
279 (not server-process) | |
280 (memq (process-status server-process) '(signal exit))) | |
281 (server-start nil) | |
282 (server-switch-buffer (server-done)))) | |
283 | |
284 (defun server-switch-buffer (next-buffer) | |
285 "Switch to another buffer, preferably one that has a client. | |
286 Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it." | |
287 (if next-buffer | |
288 (if (and (bufferp next-buffer) | |
289 (buffer-name next-buffer)) | |
290 (switch-to-buffer next-buffer) | |
291 ;; If NEXT-BUFFER is a dead buffer, | |
292 ;; remove the server records for it | |
293 ;; and try the next surviving server buffer. | |
294 (server-switch-buffer | |
295 (server-buffer-done next-buffer))) | |
296 (if server-clients | |
297 (server-switch-buffer (nth 1 (car server-clients))) | |
298 (switch-to-buffer (other-buffer))))) | |
299 | |
300 (global-set-key "\C-x#" 'server-edit) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
445
diff
changeset
|
301 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
445
diff
changeset
|
302 ;;; server.el ends here |