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)
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 (put 'server-buffer-clients
'permanent-local t
)
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
)))
103 ;; If a *server* buffer exists,
104 ;; write STRING to it for logging purposes.
105 (defun server-log (string)
106 (if (get-buffer "*server*")
108 (set-buffer "*server*")
109 (goto-char (point-max))
111 (or (bobp) (newline)))))
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")))))
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\".
127 Prefix arg means just kill any existing server communications subprocess."
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
)))
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
)))
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
)
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.
158 (setq string
(substring string
(match-end 0)))
159 (setq server-previous-string
"")
160 (let ((client (list (substring string
0 (string-match " " string
))))
163 (setq string
(substring string
(match-end 0)))
164 (while (string-match "[^ ]+ " string
)
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)))
171 (cons (list arg lineno
)
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].")))))
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)."
187 ;; If there is an existing buffer modified or the file is modified,
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
))
198 (concat "File no longer exists: "
200 ", write buffer to file? "))
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
)))
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
))
216 (old-clients server-clients
))
218 (let ((client (car old-clients
)))
220 (setq next-buffer
(nth 1 (memq buffer client
))))
222 ;; If client now has no pending buffers,
223 ;; tell it that it is done, and forget it entirely.
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
)
235 (setq server-buffer-clients nil
)))
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.
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
))))
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
255 (if (server-temp-file-p 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
)))))
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.
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.
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]."
280 (memq (process-status server-process
) '(signal exit
)))
282 (server-switch-buffer (server-done))))
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."
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
)))
297 (server-switch-buffer (nth 1 (car server-clients
)))
298 (switch-to-buffer (other-buffer)))))
300 (global-set-key "\C-x#" 'server-edit
)
302 ;;; server.el ends here