Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; server.el --- Lisp code for GNU Emacs running as server process |
c88ab9ce | 2 | |
92c7831b | 3 | ;; Copyright (C) 1986,87,92,94,95,96,97,98,99,2000,01,02,03,2004 |
0f5f7c3e | 4 | ;; Free Software Foundation, Inc. |
6d74b528 | 5 | |
630cc463 | 6 | ;; Author: William Sommerfeld <wesommer@athena.mit.edu> |
e4874521 | 7 | ;; Maintainer: FSF |
d7b4d18f | 8 | ;; Keywords: processes |
630cc463 | 9 | |
9ae0f972 | 10 | ;; Changes by peck@sun.com and by rms. |
11 | ||
12 | ;; This file is part of GNU Emacs. | |
13 | ||
14 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
15 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 16 | ;; the Free Software Foundation; either version 2, or (at your option) |
9ae0f972 | 17 | ;; any later version. |
18 | ||
19 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
23 | ||
24 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
25 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
26 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
27 | ;; Boston, MA 02111-1307, USA. | |
9ae0f972 | 28 | |
630cc463 | 29 | ;;; Commentary: |
9ae0f972 | 30 | |
b578f267 EN |
31 | ;; This Lisp code is run in Emacs when it is to operate as |
32 | ;; a server for other processes. | |
9ae0f972 | 33 | |
b578f267 | 34 | ;; Load this library and do M-x server-edit to enable Emacs as a server. |
44a56b29 SM |
35 | ;; Emacs opens up a socket for communication with clients. If there are no |
36 | ;; client buffers to edit, server-edit acts like (switch-to-buffer | |
37 | ;; (other-buffer)) | |
9ae0f972 | 38 | |
b578f267 EN |
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. | |
9ae0f972 | 43 | |
b578f267 | 44 | ;; Note that any number of clients may dispatch files to emacs to be edited. |
9ae0f972 | 45 | |
b578f267 | 46 | ;; When you finish editing a Server buffer, again call server-edit |
64f51134 JPW |
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 | |
b578f267 | 49 | ;; and exited with server-edit, the client "editor" will return |
64f51134 | 50 | ;; to the program that invoked it. |
9ae0f972 | 51 | |
b578f267 EN |
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: | |
9ae0f972 | 56 | |
b578f267 EN |
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. | |
9ae0f972 | 59 | |
b578f267 EN |
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. | |
9ae0f972 | 62 | |
b578f267 EN |
63 | ;; 3. When the program that wants to use "the editor" is running |
64 | ;; as a subprocess of Emacs. | |
9ae0f972 | 65 | |
b578f267 EN |
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. | |
9ae0f972 | 71 | |
64f51134 JPW |
72 | ;; The buffer local variable "server-buffer-clients" lists |
73 | ;; the clients who are waiting for this buffer to be edited. | |
b578f267 EN |
74 | ;; The global variable "server-clients" lists all the waiting clients, |
75 | ;; and which files are yet to be edited for each. | |
630cc463 ER |
76 | |
77 | ;;; Code: | |
8b3e840e SM |
78 | |
79 | (eval-when-compile (require 'cl)) | |
80 | ||
ab1c7f35 RS |
81 | (defgroup server nil |
82 | "Emacs running as a server process." | |
83 | :group 'external) | |
84 | ||
ab1c7f35 | 85 | (defcustom server-visit-hook nil |
0c851d78 | 86 | "*Hook run when visiting a file for the Emacs server." |
ab1c7f35 | 87 | :group 'server |
0c851d78 | 88 | :type 'hook) |
ab1c7f35 RS |
89 | |
90 | (defcustom server-switch-hook nil | |
0c851d78 | 91 | "*Hook run when switching to a buffer for the Emacs server." |
ab1c7f35 | 92 | :group 'server |
0c851d78 | 93 | :type 'hook) |
ab1c7f35 RS |
94 | |
95 | (defcustom server-done-hook nil | |
0c851d78 | 96 | "*Hook run when done editing a buffer for the Emacs server." |
ab1c7f35 | 97 | :group 'server |
0c851d78 | 98 | :type 'hook) |
f9b3ef88 | 99 | |
64f51134 JPW |
100 | (defvar server-process nil |
101 | "The current server process.") | |
9ae0f972 | 102 | |
103 | (defvar server-clients nil | |
104 | "List of current server clients. | |
9002956f KL |
105 | Each element is (PROC PROPERTIES...) where PROC is a process object, |
106 | and PROPERTIES is an association list of client properties.") | |
0a125897 | 107 | |
9ae0f972 | 108 | (defvar server-buffer-clients nil |
b5a9911c | 109 | "List of client ids for clients requesting editing of current buffer.") |
faf931a8 | 110 | (make-variable-buffer-local 'server-buffer-clients) |
9ae0f972 | 111 | ;; Changing major modes should not erase this local. |
112 | (put 'server-buffer-clients 'permanent-local t) | |
113 | ||
33186f32 DL |
114 | (defcustom server-window nil |
115 | "*Specification of the window to use for selecting Emacs server buffers. | |
ec40ed9f | 116 | If nil, use the selected window. |
408784a7 | 117 | If it is a function, it should take one argument (a buffer) and |
33186f32 DL |
118 | display and select it. A common value is `pop-to-buffer'. |
119 | If it is a window, use that. | |
120 | If it is a frame, use the frame's selected window. | |
121 | ||
122 | It is not meaningful to set this to a specific frame or window with Custom. | |
123 | Only programs can do so." | |
124 | :group 'server | |
125 | :version "21.4" | |
126 | :type '(choice (const :tag "Use selected window" | |
127 | :match (lambda (widget value) | |
128 | (not (functionp value))) | |
129 | nil) | |
130 | (function-item :tag "Use pop-to-buffer" pop-to-buffer) | |
131 | (function :tag "Other function"))) | |
ec40ed9f | 132 | |
ab1c7f35 | 133 | (defcustom server-temp-file-regexp "^/tmp/Re\\|/draft$" |
33186f32 DL |
134 | "*Regexp matching names of temporary files. |
135 | These are deleted and reused after each edit by the programs that | |
136 | invoke the Emacs server." | |
ab1c7f35 RS |
137 | :group 'server |
138 | :type 'regexp) | |
9ae0f972 | 139 | |
c6a117f0 GM |
140 | (defcustom server-kill-new-buffers t |
141 | "*Whether to kill buffers when done with them. | |
142 | If non-nil, kill a buffer unless it already existed before editing | |
64f51134 | 143 | it with Emacs server. If nil, kill only buffers as specified by |
c6a117f0 GM |
144 | `server-temp-file-regexp'. |
145 | Please note that only buffers are killed that still have a client, | |
146 | i.e. buffers visited which \"emacsclient --no-wait\" are never killed in | |
147 | this way." | |
148 | :group 'server | |
149 | :type 'boolean | |
150 | :version "21.1") | |
151 | ||
9ae0f972 | 152 | (or (assq 'server-buffer-clients minor-mode-alist) |
153 | (setq minor-mode-alist (cons '(server-buffer-clients " Server") minor-mode-alist))) | |
154 | ||
c6a117f0 | 155 | (defvar server-existing-buffer nil |
ca0c7250 | 156 | "Non-nil means the buffer existed before the server was asked to visit it. |
4dd04714 | 157 | This means that the server should not kill the buffer when you say you |
ca0c7250 | 158 | are done with it in the server.") |
c6a117f0 GM |
159 | (make-variable-buffer-local 'server-existing-buffer) |
160 | ||
03ae35cf SM |
161 | (defvar server-name "server") |
162 | ||
163 | (defvar server-socket-dir | |
164 | (format "/tmp/emacs%d" (user-uid))) | |
0c851d78 | 165 | |
9002956f KL |
166 | (defun server-client (proc) |
167 | "Return the Emacs client corresponding to PROC. | |
168 | PROC must be a process object. | |
169 | The car of the result is PROC; the cdr is an association list. | |
170 | See `server-client-get' and `server-client-set'." | |
171 | (assq proc server-clients)) | |
172 | ||
173 | (defun server-client-get (client property) | |
174 | "Get the value of PROPERTY in CLIENT. | |
175 | CLIENT may be a process object, or a client returned by `server-client'. | |
176 | Return nil if CLIENT has no such property." | |
177 | (or (listp client) (setq client (server-client client))) | |
178 | (cdr (assq property (cdr client)))) | |
179 | ||
180 | (defun server-client-set (client property value) | |
181 | "Set the PROPERTY to VALUE in CLIENT, and return VALUE. | |
182 | CLIENT may be a process object, or a client returned by `server-client'." | |
183 | (let (p proc) | |
184 | (if (listp client) | |
185 | (setq proc (car client)) | |
186 | (setq proc client | |
187 | client (server-client client))) | |
188 | (setq p (assq property client)) | |
189 | (cond | |
190 | (p (setcdr p value)) | |
191 | (client (setcdr client (cons (cons property value) (cdr client)))) | |
192 | (t (setq server-clients | |
193 | `((,proc (,property . ,value)) . ,server-clients)))) | |
194 | value)) | |
195 | ||
196 | (defun server-clients-with (property value) | |
197 | "Return a list of clients with PROPERTY set to VALUE." | |
198 | (let (result) | |
199 | (dolist (client server-clients result) | |
200 | (when (equal value (server-client-get client property)) | |
201 | (setq result (cons (car client) result)))))) | |
202 | ||
203 | (defun server-add-client (proc) | |
204 | "Create a client for process PROC, if it doesn't already have one. | |
205 | New clients have no properties." | |
206 | (unless (server-client proc) | |
207 | (setq server-clients (cons (cons proc nil) | |
208 | server-clients)))) | |
209 | ||
210 | (defun server-delete-client (client) | |
211 | "Delete CLIENT, including its buffers, displays and frames." | |
212 | ;; Force a new lookup of client (prevents infinite recursion). | |
213 | (setq client (server-client | |
214 | (if (listp client) (car client) client))) | |
215 | (let ((proc (car client)) | |
216 | (buffers (server-client-get client 'buffers))) | |
217 | (when client | |
218 | (setq server-clients (delq client server-clients)) | |
219 | ||
220 | (dolist (buf buffers) | |
6ed9e43a KL |
221 | (when (buffer-live-p buf) |
222 | (with-current-buffer buf | |
223 | ;; Remove PROC from the clients of each buffer. | |
224 | (setq server-buffer-clients (delq proc server-buffer-clients)) | |
225 | ;; Kill the buffer if necessary. | |
226 | (when (and (null server-buffer-clients) | |
227 | (or (and server-kill-new-buffers | |
228 | (not server-existing-buffer)) | |
229 | (server-temp-file-p))) | |
230 | (kill-buffer (current-buffer)))))) | |
9002956f KL |
231 | |
232 | ;; Delete the client's tty. | |
233 | (let ((tty (server-client-get client 'tty))) | |
6ed9e43a KL |
234 | (when (and tty (server-tty-live-p tty)) |
235 | (delete-tty tty))) | |
9002956f KL |
236 | |
237 | ;; Delete the client's frames. | |
238 | (dolist (frame (frame-list)) | |
239 | (if (and (frame-live-p frame) | |
240 | (equal (car client) (frame-parameter frame 'client))) | |
241 | (delete-frame frame))) | |
242 | ||
243 | ;; Delete the client's process. | |
244 | (if (eq (process-status (car client)) 'open) | |
245 | (delete-process (car client))) | |
246 | ||
247 | (server-log "Deleted" proc)))) | |
248 | ||
8b3e840e | 249 | (defun server-log (string &optional client) |
33186f32 | 250 | "If a *server* buffer exists, write STRING to it for logging purposes." |
9ae0f972 | 251 | (if (get-buffer "*server*") |
8b3e840e | 252 | (with-current-buffer "*server*" |
9ae0f972 | 253 | (goto-char (point-max)) |
8b3e840e | 254 | (insert (current-time-string) |
9002956f KL |
255 | (cond |
256 | ((null client) " ") | |
257 | ((listp client) (format " %s: " (car client))) | |
258 | (t (format " %s: " client))) | |
8b3e840e | 259 | string) |
74c20cd3 | 260 | (or (bolp) (newline))))) |
9ae0f972 | 261 | |
4d553a13 KL |
262 | (defun server-tty-live-p (tty) |
263 | "Return non-nil if the tty device named TTY has a live frame." | |
264 | (let (result) | |
265 | (dolist (frame (frame-list) result) | |
266 | (when (and (eq (frame-live-p frame) t) | |
267 | (equal (frame-tty-name frame) tty)) | |
268 | (setq result t))))) | |
269 | ||
9ae0f972 | 270 | (defun server-sentinel (proc msg) |
9002956f KL |
271 | "The process sentinel for Emacs server connections." |
272 | (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc) | |
273 | (server-delete-client proc)) | |
0c851d78 | 274 | |
4d553a13 | 275 | (defun server-handle-delete-tty (tty) |
77134727 | 276 | "Delete the client connection when the emacsclient terminal device is closed." |
9002956f KL |
277 | (dolist (proc (server-clients-with 'tty tty)) |
278 | (server-log (format "server-handle-delete-tty, tty %s" tty) proc) | |
279 | (server-delete-client proc))) | |
0b0d3e0b | 280 | |
77134727 KL |
281 | (defun server-handle-delete-frame (frame) |
282 | "Delete the client connection when the emacsclient frame is deleted." | |
9002956f | 283 | (let ((proc (frame-parameter frame 'client))) |
6ed9e43a KL |
284 | (when (and proc (window-system frame)) |
285 | ;; (Closing a terminal frame must not trigger a delete; | |
286 | ;; we must wait for delete-tty-after-functions.) | |
9002956f KL |
287 | (server-log (format "server-handle-delete-frame, frame %s" frame) proc) |
288 | (server-delete-client proc)))) | |
289 | ||
290 | (defun server-handle-suspend-tty (tty) | |
291 | "Notify the emacsclient process to suspend itself when its tty device is suspended." | |
292 | (dolist (proc (server-clients-with 'tty tty)) | |
293 | (server-log (format "server-handle-suspend-tty, tty %s" tty) proc) | |
294 | (process-send-string proc "-suspend \n"))) | |
77134727 | 295 | |
44a56b29 SM |
296 | (defun server-select-display (display) |
297 | ;; If the current frame is on `display' we're all set. | |
298 | (unless (equal (frame-parameter (selected-frame) 'display) display) | |
299 | ;; Otherwise, look for an existing frame there and select it. | |
300 | (dolist (frame (frame-list)) | |
301 | (when (equal (frame-parameter frame 'display) display) | |
302 | (select-frame frame))) | |
303 | ;; If there's no frame on that display yet, create a dummy one | |
304 | ;; and select it. | |
305 | (unless (equal (frame-parameter (selected-frame) 'display) display) | |
306 | (select-frame | |
77134727 | 307 | (make-frame-on-display display))))) |
44a56b29 SM |
308 | ;; This frame is only there in place of an actual "current display" |
309 | ;; setting, so we want it to be as unobtrusive as possible. That's | |
310 | ;; what the invisibility is for. The minibuffer setting is so that | |
311 | ;; we don't end up displaying a buffer in it (which noone would | |
312 | ;; notice). | |
77134727 KL |
313 | ;; XXX I have found this behaviour to be surprising and annoying. -- Lorentey |
314 | ;; '((visibility . nil) (minibuffer . only))))))) | |
44a56b29 | 315 | |
0c851d78 | 316 | (defun server-unquote-arg (arg) |
9002956f | 317 | "Remove &-quotation from ARG." |
0c851d78 SM |
318 | (replace-regexp-in-string |
319 | "&." (lambda (s) | |
320 | (case (aref s 1) | |
321 | (?& "&") | |
322 | (?- "-") | |
323 | (?n "\n") | |
324 | (t " "))) | |
325 | arg t t)) | |
9ae0f972 | 326 | |
0b0d3e0b | 327 | (defun server-quote-arg (arg) |
9002956f | 328 | "In ARG, insert a & before each &, each space, each newline, and -. |
0b0d3e0b KL |
329 | Change spaces to underscores, too, so that the return value never |
330 | contains a space." | |
331 | (replace-regexp-in-string | |
332 | "[-&\n ]" (lambda (s) | |
333 | (case (aref s 0) | |
334 | (?& "&&") | |
335 | (?- "&-") | |
336 | (?\n "&n") | |
337 | (?\s "&_"))) | |
338 | arg t t)) | |
339 | ||
724629d2 SM |
340 | (defun server-ensure-safe-dir (dir) |
341 | "Make sure DIR is a directory with no race-condition issues. | |
342 | Creates the directory if necessary and makes sure: | |
343 | - there's no symlink involved | |
344 | - it's owned by us | |
345 | - it's not readable/writable by anybody else." | |
346 | (setq dir (directory-file-name dir)) | |
347 | (let ((attrs (file-attributes dir))) | |
348 | (unless attrs | |
349 | (letf (((default-file-modes) ?\700)) (make-directory dir)) | |
350 | (setq attrs (file-attributes dir))) | |
351 | ;; Check that it's safe for use. | |
352 | (unless (and (eq t (car attrs)) (eq (nth 2 attrs) (user-uid)) | |
353 | (zerop (logand ?\077 (file-modes dir)))) | |
354 | (error "The directory %s is unsafe" dir)))) | |
355 | ||
7229064d | 356 | ;;;###autoload |
9ae0f972 | 357 | (defun server-start (&optional leave-dead) |
358 | "Allow this Emacs process to be a server for client processes. | |
359 | This starts a server communications subprocess through which | |
360 | client \"editors\" can send your editing commands to this Emacs job. | |
42bbacdf | 361 | To use the server, set up the program `emacsclient' in the |
9ae0f972 | 362 | Emacs distribution as your standard \"editor\". |
363 | ||
364 | Prefix arg means just kill any existing server communications subprocess." | |
365 | (interactive "P") | |
724629d2 | 366 | ;; Make sure there is a safe directory in which to place the socket. |
03ae35cf | 367 | (server-ensure-safe-dir server-socket-dir) |
9ae0f972 | 368 | ;; kill it dead! |
60f8ea53 JPW |
369 | (if server-process |
370 | (condition-case () (delete-process server-process) (error nil))) | |
c1148e39 | 371 | ;; Delete the socket files made by previous server invocations. |
03ae35cf SM |
372 | (condition-case () |
373 | (delete-file (expand-file-name server-name server-socket-dir)) | |
374 | (error nil)) | |
c1148e39 | 375 | ;; If this Emacs already had a server, clear out associated status. |
9ae0f972 | 376 | (while server-clients |
9002956f | 377 | (server-delete-client (car server-clients))) |
8b3e840e | 378 | (unless leave-dead |
9ae0f972 | 379 | (if server-process |
380 | (server-log (message "Restarting server"))) | |
e5576936 | 381 | (letf (((default-file-modes) ?\700)) |
9002956f KL |
382 | (add-hook 'delete-tty-after-functions 'server-handle-delete-tty) |
383 | (add-hook 'suspend-tty-functions 'server-handle-suspend-tty) | |
384 | (add-hook 'delete-frame-functions 'server-handle-delete-frame) | |
385 | (add-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function) | |
386 | (add-hook 'kill-emacs-query-functions 'server-kill-emacs-query-function) | |
e5576936 SM |
387 | (setq server-process |
388 | (make-network-process | |
389 | :name "server" :family 'local :server t :noquery t | |
03ae35cf | 390 | :service (expand-file-name server-name server-socket-dir) |
e5576936 SM |
391 | :sentinel 'server-sentinel :filter 'server-process-filter |
392 | ;; We must receive file names without being decoded. | |
393 | ;; Those are decoded by server-process-filter according | |
394 | ;; to file-name-coding-system. | |
395 | :coding 'raw-text))))) | |
33186f32 DL |
396 | |
397 | ;;;###autoload | |
398 | (define-minor-mode server-mode | |
399 | "Toggle Server mode. | |
400 | With ARG, turn Server mode on if ARG is positive, off otherwise. | |
401 | Server mode runs a process that accepts commands from the | |
402 | `emacsclient' program. See `server-start' and Info node `Emacs server'." | |
403 | :global t | |
404 | :group 'server | |
405 | :version "21.4" | |
406 | ;; Fixme: Should this check for an existing server socket and do | |
407 | ;; nothing if there is one (for multiple Emacs sessions)? | |
408 | (server-start (not server-mode))) | |
9ae0f972 | 409 | \f |
9ae0f972 | 410 | (defun server-process-filter (proc string) |
33186f32 DL |
411 | "Process a request from the server to edit some files. |
412 | PROC is the server process. Format of STRING is \"PATH PATH PATH... \\n\"." | |
0c851d78 | 413 | (server-log string proc) |
3f7ef08e SM |
414 | (let ((prev (process-get proc 'previous-string))) |
415 | (when prev | |
416 | (setq string (concat prev string)) | |
417 | (process-put proc 'previous-string nil))) | |
a9298135 | 418 | (condition-case err |
0b0d3e0b | 419 | (progn |
9002956f | 420 | (server-add-client proc) |
0b0d3e0b KL |
421 | ;; If the input is multiple lines, |
422 | ;; process each line individually. | |
423 | (while (string-match "\n" string) | |
424 | (let ((request (substring string 0 (match-beginning 0))) | |
425 | (coding-system (and default-enable-multibyte-characters | |
426 | (or file-name-coding-system | |
427 | default-file-name-coding-system))) | |
9002956f KL |
428 | (client (server-client proc)) |
429 | nowait ; t if emacsclient does not want to wait for us. | |
430 | frame ; The frame that was opened for the client (if any). | |
431 | display ; Open the frame on this display. | |
0b0d3e0b | 432 | dontkill ; t if the client should not be killed. |
0b0d3e0b KL |
433 | (files nil) |
434 | (lineno 1) | |
435 | (columnno 0)) | |
436 | ;; Remove this line from STRING. | |
437 | (setq string (substring string (match-end 0))) | |
9002956f | 438 | (while (string-match " *[^ ]* " request) |
0b0d3e0b KL |
439 | (let ((arg (substring request (match-beginning 0) (1- (match-end 0))))) |
440 | (setq request (substring request (match-end 0))) | |
441 | (cond | |
9002956f KL |
442 | ;; -version CLIENT-VERSION: |
443 | ;; Check version numbers, signal an error if there is a mismatch. | |
444 | ((and (equal "-version" arg) | |
445 | (string-match "\\([0-9.]+\\) " request)) | |
0b0d3e0b | 446 | (let* ((client-version (match-string 1 request)) |
9002956f KL |
447 | (truncated-emacs-version |
448 | (substring emacs-version 0 (length client-version)))) | |
0b0d3e0b KL |
449 | (setq request (substring request (match-end 0))) |
450 | (if (equal client-version truncated-emacs-version) | |
451 | (progn | |
452 | (process-send-string proc "-good-version \n") | |
9002956f KL |
453 | (server-client-set client 'version client-version)) |
454 | (error (concat "Version mismatch: Emacs is " | |
455 | truncated-emacs-version | |
456 | ", emacsclient is " client-version))))) | |
0b0d3e0b | 457 | |
9002956f | 458 | ;; -nowait: Emacsclient won't wait for a result. |
0b0d3e0b KL |
459 | ((equal "-nowait" arg) (setq nowait t)) |
460 | ||
9002956f KL |
461 | ;; -display DISPLAY: |
462 | ;; Open X frames on the given instead of the default. | |
0b0d3e0b KL |
463 | ((and (equal "-display" arg) (string-match "\\([^ ]*\\) " request)) |
464 | (setq display (match-string 1 request) | |
465 | request (substring request (match-end 0)))) | |
466 | ||
9002956f | 467 | ;; -window-system: Open a new X frame. |
0b0d3e0b | 468 | ((equal "-window-system" arg) |
9002956f | 469 | (unless (server-client-get client 'version) |
a9298135 | 470 | (error "Protocol error; make sure to use the correct version of emacsclient")) |
9002956f KL |
471 | (setq frame (make-frame-on-display |
472 | (or display | |
473 | (frame-parameter nil 'display) | |
474 | (getenv "DISPLAY") | |
54021676 KL |
475 | (error "Please specify display")))) |
476 | (modify-frame-parameters frame (list (cons 'client proc))) | |
9002956f KL |
477 | (select-frame frame) |
478 | (server-client-set client 'frame frame) | |
479 | (setq dontkill t)) | |
480 | ||
481 | ;; -resume: Resume a suspended tty frame. | |
0b0d3e0b | 482 | ((equal "-resume" arg) |
9002956f | 483 | (let ((tty (server-client-get client 'tty))) |
0b0d3e0b KL |
484 | (setq dontkill t) |
485 | (when tty (resume-tty tty)))) | |
486 | ||
9002956f KL |
487 | ;; -suspend: Suspend the client's frame. (In case we |
488 | ;; get out of sync, and a C-z sends a SIGTSTP to | |
489 | ;; emacsclient.) | |
0b0d3e0b | 490 | ((equal "-suspend" arg) |
9002956f | 491 | (let ((tty (server-client-get client 'tty))) |
0b0d3e0b KL |
492 | (setq dontkill t) |
493 | (when tty (suspend-tty tty)))) | |
494 | ||
9002956f KL |
495 | ;; -ignore COMMENT: Noop; useful for debugging emacsclient. |
496 | ;; (The given comment appears in the server log.) | |
0b0d3e0b KL |
497 | ((and (equal "-ignore" arg) (string-match "\\([^ ]*\\) " request)) |
498 | (setq dontkill t | |
499 | request (substring request (match-end 0)))) | |
500 | ||
9002956f | 501 | ;; -tty DEVICE-NAME TYPE: Open a new tty frame at the client. |
0b0d3e0b KL |
502 | ((and (equal "-tty" arg) (string-match "\\([^ ]*\\) \\([^ ]*\\) " request)) |
503 | (let ((tty (server-unquote-arg (match-string 1 request))) | |
504 | (type (server-unquote-arg (match-string 2 request)))) | |
505 | (setq request (substring request (match-end 0))) | |
9002956f KL |
506 | (unless (server-client-get client 'version) |
507 | (error "Protocol error; make sure you use the correct version of emacsclient")) | |
54021676 KL |
508 | (setq frame (make-frame-on-tty tty type)) |
509 | (modify-frame-parameters frame (list (cons 'client proc))) | |
9002956f KL |
510 | (select-frame frame) |
511 | (server-client-set client 'frame frame) | |
512 | (server-client-set client 'tty (frame-tty-name frame)) | |
513 | ;; Reply with our pid. | |
514 | (process-send-string proc (concat "-emacs-pid " (number-to-string (emacs-pid)) "\n")) | |
515 | (setq dontkill t))) | |
516 | ||
517 | ;; -position LINE: Go to the given line in the next file. | |
0b0d3e0b KL |
518 | ((and (equal "-position" arg) (string-match "\\(\\+[0-9]+\\) " request)) |
519 | (setq request (substring request (match-end 0)) | |
520 | lineno (string-to-int (substring (match-string 1 request) 1)))) | |
521 | ||
9002956f | 522 | ;; -position LINE:COLUMN: Set point to the given position in the next file. |
0b0d3e0b KL |
523 | ((and (equal "-position" arg) (string-match "\\+\\([0-9]+\\):\\([0-9]+\\) " request)) |
524 | (setq request (substring request (match-end 0)) | |
525 | lineno (string-to-int (match-string 1 request)) | |
526 | columnno (string-to-int (match-string 2 request)))) | |
527 | ||
9002956f | 528 | ;; -file FILENAME: Load the given file. |
0b0d3e0b KL |
529 | ((and (equal "-file" arg) (string-match "\\([^ ]+\\) " request)) |
530 | (let ((file (server-unquote-arg (match-string 1 request)))) | |
531 | (setq request (substring request (match-end 0))) | |
532 | (if coding-system | |
533 | (setq file (decode-coding-string file coding-system))) | |
534 | (setq file (command-line-normalize-file-name file)) | |
535 | (push (list file lineno columnno) files)) | |
536 | (setq lineno 1 | |
537 | columnno 0)) | |
538 | ||
9002956f | 539 | ;; -eval EXPR: Evaluate a Lisp expression. |
0b0d3e0b KL |
540 | ((and (equal "-eval" arg) (string-match "\\([^ ]+\\) " request)) |
541 | (let ((expr (server-unquote-arg (match-string 1 request)))) | |
542 | (setq request (substring request (match-end 0))) | |
543 | (if coding-system | |
544 | (setq expr (decode-coding-string expr coding-system))) | |
545 | (let ((v (eval (car (read-from-string expr))))) | |
9002956f | 546 | (when (and (not frame) v) |
a9298135 KL |
547 | (with-temp-buffer |
548 | (let ((standard-output (current-buffer))) | |
549 | (pp v) | |
550 | (process-send-string proc "-print ") | |
0b0d3e0b KL |
551 | (process-send-string |
552 | proc (server-quote-arg | |
553 | (buffer-substring-no-properties (point-min) | |
554 | (point-max)))) | |
555 | (process-send-string proc "\n"))))) | |
556 | (setq lineno 1 | |
557 | columnno 0))) | |
558 | ||
9002956f KL |
559 | ;; -env NAME VALUE: An environment variable. |
560 | ((and (equal "-env" arg) (string-match "\\([^ ]+\\) \\([^ ]+\\) " request)) | |
561 | (let ((name (server-unquote-arg (match-string 1 request))) | |
562 | (value (server-unquote-arg (match-string 2 request)))) | |
563 | (when coding-system | |
564 | (setq name (decode-coding-string name coding-system)) | |
565 | (setq value (decode-coding-string value coding-system))) | |
566 | (setq request (substring request (match-end 0))) | |
567 | (server-client-set | |
568 | client 'environment | |
569 | (cons (cons name value) | |
570 | (server-client-get client 'environment))))) | |
571 | ||
0b0d3e0b KL |
572 | ;; Unknown command. |
573 | (t (error "Unknown command: %s" arg))))) | |
574 | ||
a9298135 KL |
575 | (when files |
576 | (run-hooks 'pre-command-hook) | |
577 | (server-visit-files files client nowait) | |
578 | (run-hooks 'post-command-hook)) | |
0b0d3e0b | 579 | |
0b0d3e0b KL |
580 | ;; Delete the client if necessary. |
581 | (cond | |
0b0d3e0b | 582 | (nowait |
9002956f KL |
583 | ;; Client requested nowait; return immediately. |
584 | (server-log "Close nowait client" proc) | |
585 | (server-delete-client proc)) | |
586 | ((and (not dontkill) | |
587 | (null (server-client-get client 'buffers))) | |
588 | ;; This client is empty; get rid of it immediately. | |
589 | (server-log "Close empty client" proc) | |
590 | (server-delete-client proc)) | |
591 | (t | |
592 | (let ((buffers (server-client-get client 'buffers))) | |
593 | (when buffers | |
594 | ;; We visited some buffer for this client. | |
595 | (cond | |
596 | ((or isearch-mode (minibufferp)) | |
597 | nil) | |
598 | ((and frame (null buffers)) | |
599 | (message (substitute-command-keys | |
600 | "When done with this frame, type \\[delete-frame]"))) | |
601 | ((not (null buffers)) | |
602 | (server-switch-buffer (car buffers)) | |
603 | (run-hooks 'server-switch-hook) | |
604 | (unless nowait | |
605 | (message (substitute-command-keys | |
606 | "When done with a buffer, type \\[server-edit]"))))))))))) | |
0b0d3e0b | 607 | |
a9298135 KL |
608 | ;; Save for later any partial line that remains. |
609 | (when (> (length string) 0) | |
610 | (process-put proc 'previous-string string))) | |
611 | ;; condition-case | |
612 | (error (ignore-errors | |
613 | (process-send-string | |
0b0d3e0b | 614 | proc (concat "-error " (server-quote-arg (error-message-string err)))) |
a9298135 KL |
615 | (setq string "") |
616 | (server-log (error-message-string err) proc) | |
617 | (delete-process proc))))) | |
9ae0f972 | 618 | |
6b98185f RS |
619 | (defun server-goto-line-column (file-line-col) |
620 | (goto-line (nth 1 file-line-col)) | |
621 | (let ((column-number (nth 2 file-line-col))) | |
622 | (if (> column-number 0) | |
623 | (move-to-column (1- column-number))))) | |
624 | ||
dfa35e6b | 625 | (defun server-visit-files (files client &optional nowait) |
44a56b29 | 626 | "Find FILES and return the list CLIENT with the buffers nconc'd. |
8c493570 | 627 | FILES is an alist whose elements are (FILENAME LINENUMBER COLUMNNUMBER). |
dfa35e6b RS |
628 | NOWAIT non-nil means this client is not waiting for the results, |
629 | so don't mark these buffers specially, just visit them normally." | |
e82e73c2 | 630 | ;; Bind last-nonmenu-event to force use of keyboard, not mouse, for queries. |
44a56b29 | 631 | (let ((last-nonmenu-event t) client-record) |
3a0ce849 RS |
632 | ;; Restore the current buffer afterward, but not using save-excursion, |
633 | ;; because we don't want to save point in this buffer | |
634 | ;; if it happens to be one of those specified by the server. | |
44a56b29 SM |
635 | (save-current-buffer |
636 | (dolist (file files) | |
637 | ;; If there is an existing buffer modified or the file is | |
638 | ;; modified, revert it. If there is an existing buffer with | |
639 | ;; deleted file, offer to write it. | |
640 | (let* ((filen (car file)) | |
641 | (obuf (get-file-buffer filen))) | |
642 | (push filen file-name-history) | |
643 | (if (and obuf (set-buffer obuf)) | |
644 | (progn | |
645 | (cond ((file-exists-p filen) | |
646 | (if (not (verify-visited-file-modtime obuf)) | |
647 | (revert-buffer t nil))) | |
648 | (t | |
649 | (if (y-or-n-p | |
9002956f | 650 | (concat "File no longer exists: " filen |
44a56b29 SM |
651 | ", write buffer to file? ")) |
652 | (write-file filen)))) | |
653 | (setq server-existing-buffer t) | |
654 | (server-goto-line-column file)) | |
655 | (set-buffer (find-file-noselect filen)) | |
656 | (server-goto-line-column file) | |
657 | (run-hooks 'server-visit-hook))) | |
658 | (unless nowait | |
659 | ;; When the buffer is killed, inform the clients. | |
660 | (add-hook 'kill-buffer-hook 'server-kill-buffer nil t) | |
661 | (push (car client) server-buffer-clients)) | |
662 | (push (current-buffer) client-record))) | |
9002956f KL |
663 | (server-client-set |
664 | client 'buffers | |
665 | (nconc (server-client-get client 'buffers) client-record)))) | |
9ae0f972 | 666 | \f |
b392bac9 | 667 | (defun server-buffer-done (buffer &optional for-killing) |
9ae0f972 | 668 | "Mark BUFFER as \"done\" for its client(s). |
9184aafb RS |
669 | This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED). |
670 | NEXT-BUFFER is another server buffer, as a suggestion for what to select next, | |
8b3e840e SM |
671 | or nil. KILLED is t if we killed BUFFER (typically, because it was visiting |
672 | a temp file). | |
673 | FOR-KILLING if non-nil indicates that we are called from `kill-buffer'." | |
0c851d78 | 674 | (let ((next-buffer nil) |
9002956f KL |
675 | (killed nil)) |
676 | (dolist (client server-clients) | |
677 | (let ((buffers (server-client-get client 'buffers))) | |
8b3e840e | 678 | (or next-buffer |
9002956f KL |
679 | (setq next-buffer (nth 1 (memq buffer buffers)))) |
680 | (when buffers ; Ignore bufferless clients. | |
681 | (setq buffers (delq buffer buffers)) | |
682 | ;; Delete all dead buffers from CLIENT. | |
683 | (dolist (b buffers) | |
684 | (and (bufferp b) | |
685 | (not (buffer-live-p b)) | |
686 | (setq buffers (delq b buffers)))) | |
687 | (server-client-set client 'buffers buffers) | |
688 | ;; If client now has no pending buffers, | |
689 | ;; tell it that it is done, and forget it entirely. | |
690 | (unless buffers | |
691 | (server-log "Close" client) | |
692 | (server-delete-client client))))) | |
68469f74 | 693 | (if (and (bufferp buffer) (buffer-name buffer)) |
599f9a5c RS |
694 | ;; We may or may not kill this buffer; |
695 | ;; if we do, do not call server-buffer-done recursively | |
696 | ;; from kill-buffer-hook. | |
697 | (let ((server-kill-buffer-running t)) | |
408784a7 | 698 | (with-current-buffer buffer |
f9b3ef88 RS |
699 | (setq server-buffer-clients nil) |
700 | (run-hooks 'server-done-hook)) | |
599f9a5c RS |
701 | ;; Notice whether server-done-hook killed the buffer. |
702 | (if (null (buffer-name buffer)) | |
703 | (setq killed t) | |
704 | ;; Don't bother killing or burying the buffer | |
705 | ;; when we are called from kill-buffer. | |
706 | (unless for-killing | |
c6a117f0 GM |
707 | (when (and (not killed) |
708 | server-kill-new-buffers | |
4dd04714 RS |
709 | (with-current-buffer buffer |
710 | (not server-existing-buffer))) | |
c6a117f0 | 711 | (setq killed t) |
3d2a0e0b | 712 | (bury-buffer buffer) |
c6a117f0 GM |
713 | (kill-buffer buffer)) |
714 | (unless killed | |
715 | (if (server-temp-file-p buffer) | |
716 | (progn | |
717 | (kill-buffer buffer) | |
718 | (setq killed t)) | |
719 | (bury-buffer buffer))))))) | |
9184aafb | 720 | (list next-buffer killed))) |
9ae0f972 | 721 | |
408784a7 | 722 | (defun server-temp-file-p (&optional buffer) |
9ae0f972 | 723 | "Return non-nil if BUFFER contains a file considered temporary. |
724 | These are files whose names suggest they are repeatedly | |
725 | reused to pass information to another program. | |
726 | ||
727 | The variable `server-temp-file-regexp' controls which filenames | |
728 | are considered temporary." | |
729 | (and (buffer-file-name buffer) | |
730 | (string-match server-temp-file-regexp (buffer-file-name buffer)))) | |
731 | ||
732 | (defun server-done () | |
cc9875f9 | 733 | "Offer to save current buffer, mark it as \"done\" for clients. |
ed9ae328 RS |
734 | This kills or buries the buffer, then returns a list |
735 | of the form (NEXT-BUFFER KILLED). NEXT-BUFFER is another server buffer, | |
736 | as a suggestion for what to select next, or nil. | |
737 | KILLED is t if we killed BUFFER, which happens if it was created | |
738 | specifically for the clients and did not exist before their request for it." | |
408784a7 SM |
739 | (when server-buffer-clients |
740 | (if (server-temp-file-p) | |
741 | ;; For a temp file, save, and do make a non-numeric backup | |
742 | ;; (unless make-backup-files is nil). | |
743 | (let ((version-control nil) | |
744 | (buffer-backed-up nil)) | |
745 | (save-buffer)) | |
746 | (if (and (buffer-modified-p) | |
747 | buffer-file-name | |
748 | (y-or-n-p (concat "Save file " buffer-file-name "? "))) | |
749 | (save-buffer))) | |
750 | (server-buffer-done (current-buffer)))) | |
faf931a8 | 751 | |
71207de2 RS |
752 | ;; Ask before killing a server buffer. |
753 | ;; It was suggested to release its client instead, | |
754 | ;; but I think that is dangerous--the client would proceed | |
755 | ;; using whatever is on disk in that file. -- rms. | |
03d78665 | 756 | (defun server-kill-buffer-query-function () |
9002956f | 757 | "Ask before killing a server buffer." |
03d78665 | 758 | (or (not server-buffer-clients) |
114a8b8c KL |
759 | (let ((res t)) |
760 | (dolist (proc server-buffer-clients res) | |
9002956f KL |
761 | (let ((client (server-client proc))) |
762 | (when (and client (eq (process-status proc) 'open)) | |
763 | (setq res nil))))) | |
03d78665 RS |
764 | (yes-or-no-p (format "Buffer `%s' still has clients; kill it? " |
765 | (buffer-name (current-buffer)))))) | |
766 | ||
03d78665 | 767 | (defun server-kill-emacs-query-function () |
9002956f KL |
768 | "Ask before exiting Emacs it has are live clients." |
769 | (or (not server-clients) | |
770 | (let (live-client) | |
771 | (dolist (client server-clients live-client) | |
772 | (if (memq t (mapcar 'buffer-live-p (server-client-get | |
773 | client 'buffers))) | |
774 | (setq live-client t)))) | |
775 | (yes-or-no-p "This Emacs session has clients; exit anyway? "))) | |
b392bac9 | 776 | |
fb873cfc | 777 | (defvar server-kill-buffer-running nil |
599f9a5c | 778 | "Non-nil while `server-kill-buffer' or `server-buffer-done' is running.") |
fb873cfc | 779 | |
b392bac9 | 780 | (defun server-kill-buffer () |
fb873cfc RS |
781 | ;; Prevent infinite recursion if user has made server-done-hook |
782 | ;; call kill-buffer. | |
783 | (or server-kill-buffer-running | |
599f9a5c RS |
784 | (and server-buffer-clients |
785 | (let ((server-kill-buffer-running t)) | |
786 | (when server-process | |
787 | (server-buffer-done (current-buffer) t)))))) | |
9ae0f972 | 788 | \f |
789 | (defun server-edit (&optional arg) | |
790 | "Switch to next server editing buffer; say \"Done\" for current buffer. | |
791 | If a server buffer is current, it is marked \"done\" and optionally saved. | |
ed9ae328 | 792 | The buffer is also killed if it did not exist before the clients asked for it. |
9ae0f972 | 793 | When all of a client's buffers are marked as \"done\", the client is notified. |
794 | ||
795 | Temporary files such as MH <draft> files are always saved and backed up, | |
991298c3 RS |
796 | no questions asked. (The variable `make-backup-files', if nil, still |
797 | inhibits a backup; you can set it locally in a particular buffer to | |
798 | prevent a backup for it.) The variable `server-temp-file-regexp' controls | |
9ae0f972 | 799 | which filenames are considered temporary. |
800 | ||
64f51134 | 801 | If invoked with a prefix argument, or if there is no server process running, |
9ae0f972 | 802 | starts server process and that is all. Invoked by \\[server-edit]." |
9ae0f972 | 803 | (interactive "P") |
804 | (if (or arg | |
805 | (not server-process) | |
806 | (memq (process-status server-process) '(signal exit))) | |
807 | (server-start nil) | |
9184aafb | 808 | (apply 'server-switch-buffer (server-done)))) |
9ae0f972 | 809 | |
98a4349c | 810 | (defun server-switch-buffer (&optional next-buffer killed-one) |
9ae0f972 | 811 | "Switch to another buffer, preferably one that has a client. |
812 | Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it." | |
9184aafb RS |
813 | ;; KILLED-ONE is t in a recursive call |
814 | ;; if we have already killed one temp-file server buffer. | |
815 | ;; This means we should avoid the final "switch to some other buffer" | |
816 | ;; since we've already effectively done that. | |
ca0c7250 | 817 | (if (null next-buffer) |
9002956f KL |
818 | (progn |
819 | (let ((rest server-clients)) | |
820 | (while (and rest (not next-buffer)) | |
821 | (let ((client (car rest))) | |
822 | ;; Only look at frameless clients. | |
823 | (when (not (server-client-get client 'frame)) | |
824 | (setq next-buffer (car (server-client-get client 'buffers)))) | |
825 | (setq rest (cdr rest))))) | |
826 | (and next-buffer (server-switch-buffer next-buffer killed-one)) | |
827 | (unless (or next-buffer killed-one (window-dedicated-p (selected-window))) | |
828 | ;; (switch-to-buffer (other-buffer)) | |
90ee5627 | 829 | (message "No server buffers remain to edit"))) |
9002956f | 830 | (if (not (buffer-live-p next-buffer)) |
ca0c7250 | 831 | ;; If NEXT-BUFFER is a dead buffer, remove the server records for it |
9ae0f972 | 832 | ;; and try the next surviving server buffer. |
ca0c7250 SM |
833 | (apply 'server-switch-buffer (server-buffer-done next-buffer)) |
834 | ;; OK, we know next-buffer is live, let's display and select it. | |
408784a7 SM |
835 | (if (functionp server-window) |
836 | (funcall server-window next-buffer) | |
837 | (let ((win (get-buffer-window next-buffer 0))) | |
838 | (if (and win (not server-window)) | |
839 | ;; The buffer is already displayed: just reuse the window. | |
840 | (let ((frame (window-frame win))) | |
841 | (if (eq (frame-visible-p frame) 'icon) | |
842 | (raise-frame frame)) | |
843 | (select-window win) | |
844 | (set-buffer next-buffer)) | |
845 | ;; Otherwise, let's find an appropriate window. | |
846 | (cond ((and (windowp server-window) | |
847 | (window-live-p server-window)) | |
848 | (select-window server-window)) | |
849 | ((framep server-window) | |
850 | (if (not (frame-live-p server-window)) | |
851 | (setq server-window (make-frame))) | |
852 | (select-window (frame-selected-window server-window)))) | |
853 | (if (window-minibuffer-p (selected-window)) | |
854 | (select-window (next-window nil 'nomini 0))) | |
855 | ;; Move to a non-dedicated window, if we have one. | |
856 | (when (window-dedicated-p (selected-window)) | |
857 | (select-window | |
858 | (get-window-with-predicate | |
859 | (lambda (w) | |
860 | (and (not (window-dedicated-p w)) | |
861 | (equal (frame-parameter (window-frame w) 'display) | |
862 | (frame-parameter (selected-frame) 'display)))) | |
863 | 'nomini 'visible (selected-window)))) | |
864 | (condition-case nil | |
865 | (switch-to-buffer next-buffer) | |
866 | ;; After all the above, we might still have ended up with | |
867 | ;; a minibuffer/dedicated-window (if there's no other). | |
868 | (error (pop-to-buffer next-buffer))))))))) | |
9ae0f972 | 869 | |
870 | (global-set-key "\C-x#" 'server-edit) | |
df4e8a11 DL |
871 | |
872 | (defun server-unload-hook () | |
33186f32 | 873 | (server-start t) |
9002956f KL |
874 | (remove-hook 'delete-tty-after-functions 'server-handle-delete-tty) |
875 | (remove-hook 'suspend-tty-functions 'server-handle-suspend-tty) | |
876 | (remove-hook 'delete-frame-functions 'server-handle-delete-frame) | |
df4e8a11 DL |
877 | (remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function) |
878 | (remove-hook 'kill-emacs-query-functions 'server-kill-emacs-query-function) | |
879 | (remove-hook 'kill-buffer-hook 'server-kill-buffer)) | |
16c15321 RM |
880 | \f |
881 | (provide 'server) | |
c88ab9ce | 882 | |
ab5796a9 | 883 | ;;; arch-tag: 1f7ecb42-f00a-49f8-906d-61995d84c8d6 |
c88ab9ce | 884 | ;;; server.el ends here |