0
|
1 ;;; server.el --- Lisp code for GNU Emacs running as server process.
|
|
2
|
|
3 ;; Copyright (C) 1986, 1987, 1992, 1994, 1995 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: William Sommerfeld <wesommer@athena.mit.edu>
|
|
6 ;; Keywords: processes
|
|
7
|
|
8 ;; Changes by peck@sun.com and by rms.
|
|
9
|
|
10 ;; This file is part of XEmacs.
|
|
11
|
|
12 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
13 ;; under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
25 ;; 02111-1307, USA.
|
0
|
26
|
2
|
27 ;;; Synched up with FSF 19.34.
|
0
|
28
|
|
29 ;;; Commentary:
|
|
30
|
2
|
31 ;; This Lisp code is run in Emacs when it is to operate as
|
|
32 ;; a server for other processes.
|
0
|
33
|
2
|
34 ;; Load this library and do M-x server-edit to enable Emacs as a server.
|
|
35 ;; Emacs runs the program ../../lib-src/emacsserver as a subprocess
|
|
36 ;; for communication with clients. If there are no client buffers to edit,
|
|
37 ;; server-edit acts like (switch-to-buffer (other-buffer))
|
0
|
38
|
2
|
39 ;; When some other program runs "the editor" to edit a file,
|
|
40 ;; "the editor" can be the Emacs client program ../../lib-src/emacsclient.
|
|
41 ;; This program transmits the file names to Emacs through
|
|
42 ;; the server subprocess, and Emacs visits them and lets you edit them.
|
0
|
43
|
2
|
44 ;; Note that any number of clients may dispatch files to emacs to be edited.
|
0
|
45
|
2
|
46 ;; When you finish editing a Server buffer, again call server-edit
|
|
47 ;; to mark that buffer as done for the client and switch to the next
|
|
48 ;; Server buffer. When all the buffers for a client have been edited
|
|
49 ;; and exited with server-edit, the client "editor" will return
|
|
50 ;; to the program that invoked it.
|
0
|
51
|
2
|
52 ;; Your editing commands and Emacs's display output go to and from
|
|
53 ;; the terminal in the usual way. Thus, server operation is possible
|
|
54 ;; only when Emacs can talk to the terminal at the time you invoke
|
|
55 ;; the client. This is possible in four cases:
|
0
|
56
|
2
|
57 ;; 1. On a window system, where Emacs runs in one window and the
|
|
58 ;; program that wants to use "the editor" runs in another.
|
0
|
59
|
2
|
60 ;; 2. On a multi-terminal system, where Emacs runs on one terminal and the
|
|
61 ;; program that wants to use "the editor" runs on another.
|
0
|
62
|
2
|
63 ;; 3. When the program that wants to use "the editor" is running
|
|
64 ;; as a subprocess of Emacs.
|
0
|
65
|
2
|
66 ;; 4. On a system with job control, when Emacs is suspended, the program
|
|
67 ;; that wants to use "the editor" will stop and display
|
|
68 ;; "Waiting for Emacs...". It can then be suspended, and Emacs can be
|
|
69 ;; brought into the foreground for editing. When done editing, Emacs is
|
|
70 ;; suspended again, and the client program is brought into the foreground.
|
0
|
71
|
2
|
72 ;; The buffer local variable "server-buffer-clients" lists
|
|
73 ;; the clients who are waiting for this buffer to be edited.
|
|
74 ;; The global variable "server-clients" lists all the waiting clients,
|
|
75 ;; and which files are yet to be edited for each.
|
0
|
76
|
|
77 ;;; Code:
|
|
78
|
|
79 (defvar server-program (expand-file-name "emacsserver" exec-directory)
|
|
80 "*The program to use as the edit server.")
|
|
81
|
|
82 (defvar server-visit-hook nil
|
|
83 "*List of hooks to call when visiting a file for the Emacs server.")
|
|
84
|
|
85 (defvar server-switch-hook nil
|
|
86 "*List of hooks to call when switching to a buffer for the Emacs server.")
|
|
87
|
|
88 (defvar server-done-hook nil
|
|
89 "*List of hooks to call when done editing a buffer for the Emacs server.")
|
|
90
|
|
91 (defvar server-process nil
|
|
92 "the current server process")
|
|
93
|
|
94 (defvar server-previous-string "")
|
|
95
|
|
96 (defvar server-clients nil
|
|
97 "List of current server clients.
|
|
98 Each element is (CLIENTID BUFFERS...) where CLIENTID is a string
|
|
99 that can be given to the server process to identify a client.
|
|
100 When a buffer is marked as \"done\", it is removed from this list.")
|
|
101
|
|
102 (defvar server-buffer-clients nil
|
|
103 "List of clientids for clients requesting editing of current buffer.")
|
|
104 (make-variable-buffer-local 'server-buffer-clients)
|
|
105 ;; Changing major modes should not erase this local.
|
|
106 (put 'server-buffer-clients 'permanent-local t)
|
|
107
|
|
108 (defvar server-window nil
|
|
109 "*The window to use for selecting Emacs server buffers.
|
|
110 If nil, use the selected window.
|
|
111 If it is a frame, use the frame's selected window.")
|
|
112
|
|
113 (defvar server-temp-file-regexp "^/tmp/Re\\|/draft$"
|
|
114 "*Regexp which should match filenames of temporary files
|
|
115 which are deleted and reused after each edit
|
|
116 by the programs that invoke the emacs server.")
|
|
117
|
|
118 (or (assq 'server-buffer-clients minor-mode-alist)
|
|
119 (setq minor-mode-alist (cons '(server-buffer-clients " Server") minor-mode-alist)))
|
|
120
|
|
121 ;; If a *server* buffer exists,
|
|
122 ;; write STRING to it for logging purposes.
|
|
123 (defun server-log (string)
|
|
124 (if (get-buffer "*server*")
|
|
125 (save-excursion
|
|
126 (set-buffer "*server*")
|
|
127 (goto-char (point-max))
|
|
128 (insert (current-time-string) " " string)
|
|
129 (or (bolp) (newline)))))
|
|
130
|
|
131 (defun server-sentinel (proc msg)
|
|
132 (cond ((eq (process-status proc) 'exit)
|
|
133 (server-log (message "Server subprocess exited")))
|
|
134 ((eq (process-status proc) 'signal)
|
|
135 (server-log (message "Server subprocess killed")))))
|
|
136
|
|
137 ;;;###autoload
|
|
138 (make-obsolete 'server-start 'gnuserv-start)
|
|
139 (defun server-start (&optional leave-dead)
|
|
140 "Use `gnuserv-start' instead of this function.
|
|
141 Allow this Emacs process to be a server for client processes.
|
|
142 This starts a server communications subprocess through which
|
|
143 client \"editors\" can send your editing commands to this Emacs job.
|
|
144 To use the server, set up the program `emacsclient' in the
|
|
145 Emacs distribution as your standard \"editor\".
|
|
146
|
|
147 Prefix arg means just kill any existing server communications subprocess."
|
|
148 (interactive "P")
|
|
149 ;; kill it dead!
|
|
150 (if server-process
|
|
151 (progn
|
|
152 (set-process-sentinel server-process nil)
|
|
153 (condition-case () (delete-process server-process) (error nil))))
|
|
154 (condition-case () (delete-file "~/.emacs_server") (error nil))
|
|
155 (let* ((sysname (system-name))
|
|
156 (dot-index (string-match "\\." sysname)))
|
|
157 (condition-case ()
|
|
158 (delete-file (format "/tmp/esrv%d-%s" (user-uid) sysname))
|
|
159 (error nil))
|
|
160 ;; In case the server file name was made with a domainless hostname,
|
|
161 ;; try deleting that name too.
|
|
162 (if dot-index
|
|
163 (condition-case ()
|
|
164 (delete-file (format "/tmp/esrv%d-%s" (user-uid)
|
|
165 (substring sysname 0 dot-index)))
|
|
166 (error nil))))
|
|
167 ;; If we already had a server, clear out associated status.
|
|
168 (while server-clients
|
|
169 (let ((buffer (nth 1 (car server-clients))))
|
|
170 (server-buffer-done buffer)))
|
|
171 (if leave-dead
|
|
172 nil
|
|
173 (if server-process
|
|
174 (server-log (message "Restarting server")))
|
|
175 ;; Using a pty is wasteful, and the separate session causes
|
|
176 ;; annoyance sometimes (some systems kill idle sessions).
|
|
177 (let ((process-connection-type nil))
|
|
178 (setq server-process (start-process "server" nil server-program)))
|
|
179 (set-process-sentinel server-process 'server-sentinel)
|
|
180 (set-process-filter server-process 'server-process-filter)
|
|
181 (process-kill-without-query server-process)))
|
|
182
|
|
183 ;Process a request from the server to edit some files.
|
|
184 ;Format of STRING is "Client: CLIENTID PATH PATH PATH... \n"
|
|
185 (defun server-process-filter (proc string)
|
|
186 (server-log string)
|
|
187 (setq string (concat server-previous-string string))
|
|
188 ;; If the input is multiple lines,
|
|
189 ;; process each line individually.
|
|
190 (while (string-match "\n" string)
|
|
191 (let ((request (substring string 0 (match-beginning 0)))
|
|
192 client
|
|
193 (files nil)
|
|
194 (lineno 1))
|
|
195 ;; Remove this line from STRING.
|
|
196 (setq string (substring string (match-end 0)))
|
|
197 (if (string-match "^Error: " request)
|
2
|
198 (message "Server error: %s" (substring request (match-end 0)))
|
0
|
199 (if (string-match "^Client: " request)
|
|
200 (progn
|
|
201 (setq request (substring request (match-end 0)))
|
|
202 (setq client (list (substring request 0 (string-match " " request))))
|
|
203 (setq request (substring request (match-end 0)))
|
|
204 (while (string-match "[^ ]+ " request)
|
|
205 (let ((arg
|
|
206 (substring request (match-beginning 0) (1- (match-end 0)))))
|
|
207 (setq request (substring request (match-end 0)))
|
|
208 (if (string-match "\\`\\+[0-9]+\\'" arg)
|
2
|
209 ;; ARG is a line number option.
|
0
|
210 (setq lineno (read (substring arg 1)))
|
2
|
211 ;; ARG is a file name.
|
|
212 ;; Collapse multiple slashes to single slashes.
|
|
213 (setq arg (command-line-normalize-file-name arg))
|
0
|
214 (setq files
|
|
215 (cons (list arg lineno)
|
|
216 files))
|
|
217 (setq lineno 1))))
|
|
218 (server-visit-files files client)
|
|
219 ;; CLIENT is now a list (CLIENTNUM BUFFERS...)
|
|
220 (setq server-clients (cons client server-clients))
|
|
221 (server-switch-buffer (nth 1 client))
|
|
222 (run-hooks 'server-switch-hook)
|
|
223 (message (substitute-command-keys
|
|
224 "When done with a buffer, type \\[server-edit]")))))))
|
|
225 ;; Save for later any partial line that remains.
|
|
226 (setq server-previous-string string))
|
|
227
|
|
228 (defun server-visit-files (files client)
|
|
229 "Finds FILES and returns the list CLIENT with the buffers nconc'd.
|
|
230 FILES is an alist whose elements are (FILENAME LINENUMBER)."
|
2
|
231 ;; Bind last-nonmenu-event to force use of keyboard, not mouse, for queries.
|
|
232 ;; XEmacs: this doesn't work as far as I can tell. -sb
|
|
233 (let (client-record (last-nonmenu-event t) (obuf (current-buffer)))
|
0
|
234 ;; Restore the current buffer afterward, but not using save-excursion,
|
|
235 ;; because we don't want to save point in this buffer
|
|
236 ;; if it happens to be one of those specified by the server.
|
|
237 (unwind-protect
|
|
238 (while files
|
|
239 ;; If there is an existing buffer modified or the file is modified,
|
|
240 ;; revert it.
|
|
241 ;; If there is an existing buffer with deleted file, offer to write it.
|
|
242 (let* ((filen (car (car files)))
|
|
243 (obuf (get-file-buffer filen)))
|
|
244 (if (and obuf (set-buffer obuf))
|
|
245 (if (file-exists-p filen)
|
|
246 (if (or (not (verify-visited-file-modtime obuf))
|
|
247 (buffer-modified-p obuf))
|
|
248 (revert-buffer t nil))
|
|
249 (if (y-or-n-p
|
|
250 (concat "File no longer exists: "
|
|
251 filen
|
|
252 ", write buffer to file? "))
|
|
253 (write-file filen)))
|
|
254 (set-buffer (find-file-noselect filen))
|
|
255 (run-hooks 'server-visit-hook)))
|
|
256 (goto-line (nth 1 (car files)))
|
|
257 (setq server-buffer-clients (cons (car client) server-buffer-clients))
|
|
258 (setq client-record (cons (current-buffer) client-record))
|
|
259 (setq files (cdr files)))
|
|
260 (set-buffer obuf))
|
|
261 (nconc client client-record)))
|
|
262
|
|
263 (defun server-buffer-done (buffer)
|
|
264 "Mark BUFFER as \"done\" for its client(s).
|
|
265 This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED).
|
|
266 NEXT-BUFFER is another server buffer, as a suggestion for what to select next,
|
|
267 or nil. KILLED is t if we killed BUFFER (because it was a temp file)."
|
|
268 (let ((running (eq (process-status server-process) 'run))
|
|
269 (next-buffer nil)
|
|
270 (killed nil)
|
|
271 (first t)
|
|
272 (old-clients server-clients))
|
|
273 (while old-clients
|
|
274 (let ((client (car old-clients)))
|
|
275 (or next-buffer
|
|
276 (setq next-buffer (nth 1 (memq buffer client))))
|
|
277 (delq buffer client)
|
|
278 ;; Delete all dead buffers from CLIENT.
|
|
279 (let ((tail client))
|
|
280 (while tail
|
|
281 (and (bufferp (car tail))
|
|
282 (null (buffer-name (car tail)))
|
|
283 (delq (car tail) client))
|
|
284 (setq tail (cdr tail))))
|
|
285 ;; If client now has no pending buffers,
|
|
286 ;; tell it that it is done, and forget it entirely.
|
|
287 (if (cdr client) nil
|
|
288 (if running
|
|
289 (progn
|
|
290 ;; Don't send emacsserver two commands in close succession.
|
|
291 ;; It cannot handle that.
|
|
292 (or first (sit-for 1))
|
|
293 (setq first nil)
|
2
|
294 ;; XEmacs: FSF uses obsolete send-string
|
0
|
295 (process-send-string server-process
|
|
296 (format "Close: %s Done\n" (car client)))
|
|
297 (server-log (format "Close: %s Done\n" (car client)))))
|
|
298 (setq server-clients (delq client server-clients))))
|
|
299 (setq old-clients (cdr old-clients)))
|
|
300 (if (and (bufferp buffer) (buffer-name buffer))
|
|
301 (progn
|
|
302 (save-excursion
|
|
303 (set-buffer buffer)
|
|
304 (setq server-buffer-clients nil)
|
|
305 (run-hooks 'server-done-hook))
|
|
306 (if (server-temp-file-p buffer)
|
|
307 (progn (kill-buffer buffer)
|
|
308 (setq killed t))
|
|
309 (bury-buffer buffer))))
|
|
310 (list next-buffer killed)))
|
|
311
|
|
312 (defun server-temp-file-p (buffer)
|
|
313 "Return non-nil if BUFFER contains a file considered temporary.
|
|
314 These are files whose names suggest they are repeatedly
|
|
315 reused to pass information to another program.
|
|
316
|
|
317 The variable `server-temp-file-regexp' controls which filenames
|
|
318 are considered temporary."
|
|
319 (and (buffer-file-name buffer)
|
|
320 (string-match server-temp-file-regexp (buffer-file-name buffer))))
|
|
321
|
|
322 (defun server-done ()
|
|
323 "Offer to save current buffer, mark it as \"done\" for clients.
|
|
324 This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED).
|
|
325 NEXT-BUFFER is another server buffer, as a suggestion for what to select next,
|
|
326 or nil. KILLED is t if we killed the BUFFER (because it was a temp file)."
|
|
327 (let ((buffer (current-buffer)))
|
|
328 (if server-buffer-clients
|
|
329 (progn
|
|
330 (if (server-temp-file-p buffer)
|
|
331 ;; For a temp file, save, and do make a non-numeric backup
|
|
332 ;; (unless make-backup-files is nil).
|
|
333 (let ((version-control nil)
|
|
334 (buffer-backed-up nil))
|
|
335 (save-buffer))
|
|
336 (if (and (buffer-modified-p)
|
|
337 (y-or-n-p (concat "Save file " buffer-file-name "? ")))
|
|
338 (save-buffer buffer)))
|
|
339 (server-buffer-done buffer)))))
|
|
340
|
|
341 ;; Ask before killing a server buffer.
|
|
342 ;; It was suggested to release its client instead,
|
|
343 ;; but I think that is dangerous--the client would proceed
|
|
344 ;; using whatever is on disk in that file. -- rms.
|
|
345 (defun server-kill-buffer-query-function ()
|
|
346 (or (not server-buffer-clients)
|
|
347 (yes-or-no-p (format "Buffer `%s' still has clients; kill it? "
|
|
348 (buffer-name (current-buffer))))))
|
|
349
|
|
350 (add-hook 'kill-buffer-query-functions
|
|
351 'server-kill-buffer-query-function)
|
|
352
|
|
353 (defun server-kill-emacs-query-function ()
|
|
354 (let (live-client
|
|
355 (tail server-clients))
|
|
356 ;; See if any clients have any buffers that are still alive.
|
|
357 (while tail
|
|
358 (if (memq t (mapcar 'stringp (mapcar 'buffer-name (cdr (car tail)))))
|
|
359 (setq live-client t))
|
|
360 (setq tail (cdr tail)))
|
|
361 (or (not live-client)
|
|
362 (yes-or-no-p "Server buffers still have clients; exit anyway? "))))
|
|
363
|
|
364 (add-hook 'kill-emacs-query-functions 'server-kill-emacs-query-function)
|
|
365
|
|
366 (defun server-edit (&optional arg)
|
|
367 "Switch to next server editing buffer; say \"Done\" for current buffer.
|
|
368 If a server buffer is current, it is marked \"done\" and optionally saved.
|
|
369 When all of a client's buffers are marked as \"done\", the client is notified.
|
|
370
|
|
371 Temporary files such as MH <draft> files are always saved and backed up,
|
|
372 no questions asked. (The variable `make-backup-files', if nil, still
|
|
373 inhibits a backup; you can set it locally in a particular buffer to
|
|
374 prevent a backup for it.) The variable `server-temp-file-regexp' controls
|
|
375 which filenames are considered temporary.
|
|
376
|
|
377 If invoked with a prefix argument, or if there is no server process running,
|
|
378 starts server process and that is all. Invoked by \\[server-edit]."
|
|
379 (interactive "P")
|
|
380 (if (or arg
|
|
381 (not server-process)
|
|
382 (memq (process-status server-process) '(signal exit)))
|
|
383 (server-start nil)
|
|
384 (apply 'server-switch-buffer (server-done))))
|
|
385
|
|
386 (defun server-switch-buffer (&optional next-buffer killed-one)
|
|
387 "Switch to another buffer, preferably one that has a client.
|
|
388 Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it."
|
|
389 ;; KILLED-ONE is t in a recursive call
|
|
390 ;; if we have already killed one temp-file server buffer.
|
|
391 ;; This means we should avoid the final "switch to some other buffer"
|
|
392 ;; since we've already effectively done that.
|
|
393 (cond ((and (windowp server-window)
|
|
394 (window-live-p server-window))
|
|
395 (select-window server-window))
|
|
396 ((framep server-window)
|
|
397 (if (not (frame-live-p server-window))
|
|
398 (setq server-window (make-frame)))
|
|
399 (select-window (frame-selected-window server-window))))
|
|
400 (if (window-minibuffer-p (selected-window))
|
|
401 (select-window (next-window nil 'nomini 0)))
|
|
402 ;; Move to a non-dedicated window, if we have one.
|
|
403 (let ((last-window (previous-window nil 'nomini 0)))
|
|
404 (while (and (window-dedicated-p (selected-window))
|
|
405 (not (eq last-window (selected-window))))
|
|
406 (select-window (next-window nil 'nomini 0))))
|
|
407 (set-window-dedicated-p (selected-window) nil)
|
|
408 (if next-buffer
|
|
409 (if (and (bufferp next-buffer)
|
|
410 (buffer-name next-buffer))
|
|
411 (switch-to-buffer next-buffer)
|
|
412 ;; If NEXT-BUFFER is a dead buffer,
|
|
413 ;; remove the server records for it
|
|
414 ;; and try the next surviving server buffer.
|
|
415 (apply 'server-switch-buffer
|
|
416 (server-buffer-done next-buffer)))
|
|
417 (if server-clients
|
|
418 (server-switch-buffer (nth 1 (car server-clients)) killed-one)
|
|
419 (if (not killed-one)
|
|
420 (switch-to-buffer (other-buffer))))))
|
|
421
|
|
422 (global-set-key "\C-x#" 'server-edit)
|
|
423
|
|
424 (provide 'server)
|
|
425
|
|
426 ;;; server.el ends here
|