Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; server.el --- Lisp code for GNU Emacs running as server process |
c88ab9ce | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1986, 1987, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
f86228e3 | 4 | ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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. |
35dfa9b6 | 11 | ;; Overhaul by Karoly Lorentey <lorentey@elte.hu> for multi-tty support. |
9ae0f972 | 12 | |
13 | ;; This file is part of GNU Emacs. | |
14 | ||
eb3fa2cf | 15 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
9ae0f972 | 16 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
17 | ;; the Free Software Foundation, either version 3 of the License, or |
18 | ;; (at your option) any later version. | |
9ae0f972 | 19 | |
20 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
21 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | ;; GNU General Public License for more details. | |
24 | ||
25 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 26 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
9ae0f972 | 27 | |
630cc463 | 28 | ;;; Commentary: |
9ae0f972 | 29 | |
b578f267 EN |
30 | ;; This Lisp code is run in Emacs when it is to operate as |
31 | ;; a server for other processes. | |
9ae0f972 | 32 | |
b578f267 | 33 | ;; Load this library and do M-x server-edit to enable Emacs as a server. |
44a56b29 SM |
34 | ;; Emacs opens up a socket for communication with clients. If there are no |
35 | ;; client buffers to edit, server-edit acts like (switch-to-buffer | |
36 | ;; (other-buffer)) | |
9ae0f972 | 37 | |
b578f267 EN |
38 | ;; When some other program runs "the editor" to edit a file, |
39 | ;; "the editor" can be the Emacs client program ../lib-src/emacsclient. | |
40 | ;; This program transmits the file names to Emacs through | |
41 | ;; the server subprocess, and Emacs visits them and lets you edit them. | |
9ae0f972 | 42 | |
6d3a46f7 | 43 | ;; Note that any number of clients may dispatch files to Emacs to be edited. |
9ae0f972 | 44 | |
b578f267 | 45 | ;; When you finish editing a Server buffer, again call server-edit |
64f51134 JPW |
46 | ;; to mark that buffer as done for the client and switch to the next |
47 | ;; Server buffer. When all the buffers for a client have been edited | |
b578f267 | 48 | ;; and exited with server-edit, the client "editor" will return |
64f51134 | 49 | ;; to the program that invoked it. |
9ae0f972 | 50 | |
b578f267 EN |
51 | ;; Your editing commands and Emacs's display output go to and from |
52 | ;; the terminal in the usual way. Thus, server operation is possible | |
53 | ;; only when Emacs can talk to the terminal at the time you invoke | |
54 | ;; the client. This is possible in four cases: | |
9ae0f972 | 55 | |
b578f267 EN |
56 | ;; 1. On a window system, where Emacs runs in one window and the |
57 | ;; program that wants to use "the editor" runs in another. | |
9ae0f972 | 58 | |
b578f267 EN |
59 | ;; 2. On a multi-terminal system, where Emacs runs on one terminal and the |
60 | ;; program that wants to use "the editor" runs on another. | |
9ae0f972 | 61 | |
b578f267 EN |
62 | ;; 3. When the program that wants to use "the editor" is running |
63 | ;; as a subprocess of Emacs. | |
9ae0f972 | 64 | |
b578f267 EN |
65 | ;; 4. On a system with job control, when Emacs is suspended, the program |
66 | ;; that wants to use "the editor" will stop and display | |
67 | ;; "Waiting for Emacs...". It can then be suspended, and Emacs can be | |
68 | ;; brought into the foreground for editing. When done editing, Emacs is | |
69 | ;; suspended again, and the client program is brought into the foreground. | |
9ae0f972 | 70 | |
64f51134 JPW |
71 | ;; The buffer local variable "server-buffer-clients" lists |
72 | ;; the clients who are waiting for this buffer to be edited. | |
b578f267 EN |
73 | ;; The global variable "server-clients" lists all the waiting clients, |
74 | ;; and which files are yet to be edited for each. | |
630cc463 | 75 | |
0acb1916 SM |
76 | ;; Todo: |
77 | ||
78 | ;; - handle command-line-args-left. | |
79 | ;; - move most of the args processing and decision making from emacsclient.c | |
80 | ;; to here. | |
81 | ;; - fix up handling of the client's environment (place it in the terminal?). | |
82 | ||
630cc463 | 83 | ;;; Code: |
8b3e840e SM |
84 | |
85 | (eval-when-compile (require 'cl)) | |
86 | ||
ab1c7f35 RS |
87 | (defgroup server nil |
88 | "Emacs running as a server process." | |
89 | :group 'external) | |
90 | ||
337e3c70 JB |
91 | (defcustom server-use-tcp nil |
92 | "If non-nil, use TCP sockets instead of local sockets." | |
93 | :set #'(lambda (sym val) | |
94 | (unless (featurep 'make-network-process '(:family local)) | |
95 | (setq val t) | |
96 | (unless load-in-progress | |
97 | (message "Local sockets unsupported, using TCP sockets"))) | |
98 | (when val (random t)) | |
99 | (set-default sym val)) | |
100 | :group 'server | |
101 | :type 'boolean | |
102 | :version "22.1") | |
103 | ||
104 | (defcustom server-host nil | |
105 | "The name or IP address to use as host address of the server process. | |
106 | If set, the server accepts remote connections; otherwise it is local." | |
107 | :group 'server | |
108 | :type '(choice | |
109 | (string :tag "Name or IP address") | |
110 | (const :tag "Local" nil)) | |
111 | :version "22.1") | |
112 | (put 'server-host 'risky-local-variable t) | |
113 | ||
0eee7a1d | 114 | (defcustom server-auth-dir (concat user-emacs-directory "server/") |
337e3c70 JB |
115 | "Directory for server authentication files." |
116 | :group 'server | |
117 | :type 'directory | |
118 | :version "22.1") | |
119 | (put 'server-auth-dir 'risky-local-variable t) | |
120 | ||
90caccca | 121 | (defcustom server-raise-frame t |
ff348fba | 122 | "If non-nil, raise frame when switching to a buffer." |
90caccca JB |
123 | :group 'server |
124 | :type 'boolean | |
125 | :version "22.1") | |
126 | ||
ab1c7f35 | 127 | (defcustom server-visit-hook nil |
ff348fba | 128 | "Hook run when visiting a file for the Emacs server." |
ab1c7f35 | 129 | :group 'server |
0c851d78 | 130 | :type 'hook) |
ab1c7f35 RS |
131 | |
132 | (defcustom server-switch-hook nil | |
ff348fba | 133 | "Hook run when switching to a buffer for the Emacs server." |
ab1c7f35 | 134 | :group 'server |
0c851d78 | 135 | :type 'hook) |
ab1c7f35 RS |
136 | |
137 | (defcustom server-done-hook nil | |
ff348fba | 138 | "Hook run when done editing a buffer for the Emacs server." |
ab1c7f35 | 139 | :group 'server |
0c851d78 | 140 | :type 'hook) |
f9b3ef88 | 141 | |
64f51134 JPW |
142 | (defvar server-process nil |
143 | "The current server process.") | |
9ae0f972 | 144 | |
145 | (defvar server-clients nil | |
146 | "List of current server clients. | |
448f754f | 147 | Each element is a process.") |
0a125897 | 148 | |
9ae0f972 | 149 | (defvar server-buffer-clients nil |
5f3c1a63 | 150 | "List of client processes requesting editing of current buffer.") |
faf931a8 | 151 | (make-variable-buffer-local 'server-buffer-clients) |
9ae0f972 | 152 | ;; Changing major modes should not erase this local. |
153 | (put 'server-buffer-clients 'permanent-local t) | |
154 | ||
33186f32 | 155 | (defcustom server-window nil |
ff348fba | 156 | "Specification of the window to use for selecting Emacs server buffers. |
ec40ed9f | 157 | If nil, use the selected window. |
408784a7 | 158 | If it is a function, it should take one argument (a buffer) and |
33186f32 DL |
159 | display and select it. A common value is `pop-to-buffer'. |
160 | If it is a window, use that. | |
161 | If it is a frame, use the frame's selected window. | |
162 | ||
163 | It is not meaningful to set this to a specific frame or window with Custom. | |
164 | Only programs can do so." | |
165 | :group 'server | |
bf247b6e | 166 | :version "22.1" |
33186f32 DL |
167 | :type '(choice (const :tag "Use selected window" |
168 | :match (lambda (widget value) | |
169 | (not (functionp value))) | |
170 | nil) | |
ee9272ff | 171 | (function-item :tag "Display in new frame" switch-to-buffer-other-frame) |
33186f32 DL |
172 | (function-item :tag "Use pop-to-buffer" pop-to-buffer) |
173 | (function :tag "Other function"))) | |
ec40ed9f | 174 | |
ab1c7f35 | 175 | (defcustom server-temp-file-regexp "^/tmp/Re\\|/draft$" |
ff348fba | 176 | "Regexp matching names of temporary files. |
33186f32 DL |
177 | These are deleted and reused after each edit by the programs that |
178 | invoke the Emacs server." | |
ab1c7f35 RS |
179 | :group 'server |
180 | :type 'regexp) | |
9ae0f972 | 181 | |
c6a117f0 | 182 | (defcustom server-kill-new-buffers t |
ff348fba | 183 | "Whether to kill buffers when done with them. |
c6a117f0 | 184 | If non-nil, kill a buffer unless it already existed before editing |
f258b525 | 185 | it with the Emacs server. If nil, kill only buffers as specified by |
c6a117f0 | 186 | `server-temp-file-regexp'. |
f258b525 GM |
187 | Please note that only buffers that still have a client are killed, |
188 | i.e. buffers visited with \"emacsclient --no-wait\" are never killed in | |
c6a117f0 GM |
189 | this way." |
190 | :group 'server | |
191 | :type 'boolean | |
192 | :version "21.1") | |
193 | ||
9ae0f972 | 194 | (or (assq 'server-buffer-clients minor-mode-alist) |
b7621225 | 195 | (push '(server-buffer-clients " Server") minor-mode-alist)) |
9ae0f972 | 196 | |
c6a117f0 | 197 | (defvar server-existing-buffer nil |
ca0c7250 | 198 | "Non-nil means the buffer existed before the server was asked to visit it. |
4dd04714 | 199 | This means that the server should not kill the buffer when you say you |
ca0c7250 | 200 | are done with it in the server.") |
c6a117f0 GM |
201 | (make-variable-buffer-local 'server-existing-buffer) |
202 | ||
03ae35cf SM |
203 | (defvar server-name "server") |
204 | ||
6afdf6e6 SM |
205 | (defvar server-socket-dir (format "/tmp/emacs%d" (user-uid)) |
206 | "The directory in which to place the server socket.") | |
0c851d78 | 207 | |
9002956f KL |
208 | (defun server-clients-with (property value) |
209 | "Return a list of clients with PROPERTY set to VALUE." | |
210 | (let (result) | |
448f754f SM |
211 | (dolist (proc server-clients result) |
212 | (when (equal value (process-get proc property)) | |
213 | (push proc result))))) | |
9002956f KL |
214 | |
215 | (defun server-add-client (proc) | |
216 | "Create a client for process PROC, if it doesn't already have one. | |
217 | New clients have no properties." | |
448f754f | 218 | (add-to-list 'server-clients proc)) |
9002956f | 219 | |
59e085e0 KL |
220 | (defmacro server-with-environment (env vars &rest body) |
221 | "Evaluate BODY with environment variables VARS set to those in ENV. | |
65f64034 KL |
222 | The environment variables are then restored to their previous values. |
223 | ||
59e085e0 KL |
224 | VARS should be a list of strings. |
225 | ENV should be in the same format as `process-environment'." | |
65f64034 | 226 | (declare (indent 2)) |
13ba3740 SM |
227 | (let ((var (make-symbol "var")) |
228 | (value (make-symbol "value"))) | |
229 | `(let ((process-environment process-environment)) | |
59e085e0 | 230 | (dolist (,var ,vars) |
e159b869 | 231 | (let ((,value (getenv-internal ,var ,env))) |
13ba3740 SM |
232 | (push (if (null ,value) |
233 | ,var | |
234 | (concat ,var "=" ,value)) | |
235 | process-environment))) | |
236 | (progn ,@body)))) | |
65f64034 | 237 | |
448f754f | 238 | (defun server-delete-client (proc &optional noframe) |
c48254fb | 239 | "Delete PROC, including its buffers, terminals and frames. |
de93c791 | 240 | If NOFRAME is non-nil, let the frames live. (To be used from |
5f3c1a63 | 241 | `delete-frame-functions'.)" |
ff91dc79 | 242 | (server-log (concat "server-delete-client" (if noframe " noframe")) proc) |
9002956f | 243 | ;; Force a new lookup of client (prevents infinite recursion). |
448f754f SM |
244 | (when (memq proc server-clients) |
245 | (let ((buffers (process-get proc 'buffers))) | |
9002956f | 246 | |
5f3c1a63 | 247 | ;; Kill the client's buffers. |
9002956f | 248 | (dolist (buf buffers) |
6ed9e43a KL |
249 | (when (buffer-live-p buf) |
250 | (with-current-buffer buf | |
6ed9e43a | 251 | ;; Kill the buffer if necessary. |
5f3c1a63 KL |
252 | (when (and (equal server-buffer-clients |
253 | (list proc)) | |
6ed9e43a KL |
254 | (or (and server-kill-new-buffers |
255 | (not server-existing-buffer)) | |
5f3c1a63 KL |
256 | (server-temp-file-p)) |
257 | (not (buffer-modified-p))) | |
258 | (let (flag) | |
259 | (unwind-protect | |
260 | (progn (setq server-buffer-clients nil) | |
261 | (kill-buffer (current-buffer)) | |
262 | (setq flag t)) | |
263 | (unless flag | |
264 | ;; Restore clients if user pressed C-g in `kill-buffer'. | |
265 | (setq server-buffer-clients (list proc))))))))) | |
9002956f | 266 | |
160f0817 KL |
267 | ;; Delete the client's frames. |
268 | (unless noframe | |
269 | (dolist (frame (frame-list)) | |
270 | (when (and (frame-live-p frame) | |
271 | (equal proc (frame-parameter frame 'client))) | |
272 | ;; Prevent `server-handle-delete-frame' from calling us | |
273 | ;; recursively. | |
274 | (set-frame-parameter frame 'client nil) | |
275 | (delete-frame frame)))) | |
276 | ||
448f754f | 277 | (setq server-clients (delq proc server-clients)) |
5f3c1a63 | 278 | |
9002956f | 279 | ;; Delete the client's tty. |
448f754f | 280 | (let ((terminal (process-get proc 'terminal))) |
977ede64 DN |
281 | ;; Only delete the terminal if it is non-nil. |
282 | (when (and terminal (eq (terminal-live-p terminal) t)) | |
6ed8eeff | 283 | (delete-terminal terminal))) |
9002956f | 284 | |
9002956f | 285 | ;; Delete the client's process. |
448f754f SM |
286 | (if (eq (process-status proc) 'open) |
287 | (delete-process proc)) | |
9002956f KL |
288 | |
289 | (server-log "Deleted" proc)))) | |
290 | ||
88fd26a1 | 291 | (defvar server-log-time-function 'current-time-string |
f03ea9d9 | 292 | "Function to generate timestamps for `server-buffer'.") |
88fd26a1 | 293 | |
28cbade4 SM |
294 | (defconst server-buffer " *server*" |
295 | "Buffer used internally by Emacs's server. | |
296 | One use is to log the I/O for debugging purposes (see `server-log'), | |
297 | the other is to provide a current buffer in which the process filter can | |
f03ea9d9 | 298 | safely let-bind buffer-local variables like `default-directory'.") |
28cbade4 SM |
299 | |
300 | (defvar server-log nil | |
301 | "If non-nil, log the server's inputs and outputs in the `server-buffer'.") | |
302 | ||
8b3e840e | 303 | (defun server-log (string &optional client) |
28cbade4 | 304 | "If `server-log' is non-nil, log STRING to `server-buffer'. |
c48254fb | 305 | If CLIENT is non-nil, add a description of it to the logged message." |
28cbade4 SM |
306 | (when server-log |
307 | (with-current-buffer (get-buffer-create server-buffer) | |
337e3c70 | 308 | (goto-char (point-max)) |
88fd26a1 | 309 | (insert (funcall server-log-time-function) |
974b73e8 | 310 | (cond |
88fd26a1 JB |
311 | ((null client) " ") |
312 | ((listp client) (format " %s: " (car client))) | |
313 | (t (format " %s: " client))) | |
337e3c70 JB |
314 | string) |
315 | (or (bolp) (newline))))) | |
9ae0f972 | 316 | |
317 | (defun server-sentinel (proc msg) | |
9002956f | 318 | "The process sentinel for Emacs server connections." |
cbfc02e4 RF |
319 | ;; If this is a new client process, set the query-on-exit flag to nil |
320 | ;; for this process (it isn't inherited from the server process). | |
321 | (when (and (eq (process-status proc) 'open) | |
322 | (process-query-on-exit-flag proc)) | |
323 | (set-process-query-on-exit-flag proc nil)) | |
757e1681 SM |
324 | ;; Delete the associated connection file, if applicable. |
325 | ;; This is actually problematic: the file may have been overwritten by | |
326 | ;; another Emacs server in the mean time, so it's not ours any more. | |
327 | ;; (and (process-contact proc :server) | |
328 | ;; (eq (process-status proc) 'closed) | |
329 | ;; (ignore-errors (delete-file (process-get proc :server-file)))) | |
9002956f KL |
330 | (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc) |
331 | (server-delete-client proc)) | |
0c851d78 | 332 | |
13ba3740 SM |
333 | (defun server-select-display (display) |
334 | ;; If the current frame is on `display' we're all set. | |
1a4a884c SM |
335 | ;; Similarly if we are unable to open a frames on other displays, there's |
336 | ;; nothing more we can do. | |
337 | (unless (or (not (fboundp 'make-frame-on-display)) | |
338 | (equal (frame-parameter (selected-frame) 'display) display)) | |
13ba3740 SM |
339 | ;; Otherwise, look for an existing frame there and select it. |
340 | (dolist (frame (frame-list)) | |
341 | (when (equal (frame-parameter frame 'display) display) | |
342 | (select-frame frame))) | |
343 | ;; If there's no frame on that display yet, create and select one. | |
344 | (unless (equal (frame-parameter (selected-frame) 'display) display) | |
345 | (let* ((buffer (generate-new-buffer " *server-dummy*")) | |
346 | (frame (make-frame-on-display | |
347 | display | |
348 | ;; Make it display (and remember) some dummy buffer, so | |
349 | ;; we can detect later if the frame is in use or not. | |
ff91dc79 | 350 | `((server-dummy-buffer . ,buffer) |
13ba3740 SM |
351 | ;; This frame may be deleted later (see |
352 | ;; server-unselect-display) so we want it to be as | |
353 | ;; unobtrusive as possible. | |
354 | (visibility . nil))))) | |
355 | (select-frame frame) | |
356 | (set-window-buffer (selected-window) buffer) | |
357 | frame)))) | |
358 | ||
359 | (defun server-unselect-display (frame) | |
360 | (when (frame-live-p frame) | |
361 | ;; If the temporary frame is in use (displays something real), make it | |
362 | ;; visible. If not (which can happen if the user's customizations call | |
363 | ;; pop-to-buffer etc.), delete it to avoid preserving the connection after | |
364 | ;; the last real frame is deleted. | |
365 | (if (and (eq (frame-first-window frame) | |
366 | (next-window (frame-first-window frame) 'nomini)) | |
367 | (eq (window-buffer (frame-first-window frame)) | |
368 | (frame-parameter frame 'server-dummy-buffer))) | |
369 | ;; The temp frame still only shows one buffer, and that is the | |
370 | ;; internal temp buffer. | |
371 | (delete-frame frame) | |
372 | (set-frame-parameter frame 'visibility t)) | |
373 | (kill-buffer (frame-parameter frame 'server-dummy-buffer)) | |
374 | (set-frame-parameter frame 'server-dummy-buffer nil))) | |
375 | ||
77134727 KL |
376 | (defun server-handle-delete-frame (frame) |
377 | "Delete the client connection when the emacsclient frame is deleted." | |
9002956f | 378 | (let ((proc (frame-parameter frame 'client))) |
e519a50b KL |
379 | (when (and (frame-live-p frame) |
380 | proc | |
160f0817 | 381 | ;; See if this is the last frame for this client. |
d5381da2 | 382 | (>= 1 (let ((frame-num 0)) |
aabf0cb9 | 383 | (dolist (f (frame-list)) |
160f0817 KL |
384 | (when (eq proc (frame-parameter f 'client)) |
385 | (setq frame-num (1+ frame-num)))) | |
386 | frame-num))) | |
9002956f | 387 | (server-log (format "server-handle-delete-frame, frame %s" frame) proc) |
de93c791 | 388 | (server-delete-client proc 'noframe)))) ; Let delete-frame delete the frame later. |
9002956f | 389 | |
6ed8eeff | 390 | (defun server-handle-suspend-tty (terminal) |
9002956f | 391 | "Notify the emacsclient process to suspend itself when its tty device is suspended." |
6ed8eeff KL |
392 | (dolist (proc (server-clients-with 'terminal terminal)) |
393 | (server-log (format "server-handle-suspend-tty, terminal %s" terminal) proc) | |
6d3a46f7 | 394 | (condition-case err |
6afdd335 | 395 | (server-send-string proc "-suspend \n") |
44954c2f SM |
396 | (file-error ;The pipe/socket was closed. |
397 | (ignore-errors (server-delete-client proc)))))) | |
44a56b29 | 398 | |
0c851d78 | 399 | (defun server-unquote-arg (arg) |
6afdd335 KL |
400 | "Remove &-quotation from ARG. |
401 | See `server-quote-arg' and `server-process-filter'." | |
0c851d78 SM |
402 | (replace-regexp-in-string |
403 | "&." (lambda (s) | |
404 | (case (aref s 1) | |
405 | (?& "&") | |
406 | (?- "-") | |
407 | (?n "\n") | |
408 | (t " "))) | |
409 | arg t t)) | |
9ae0f972 | 410 | |
0b0d3e0b | 411 | (defun server-quote-arg (arg) |
9002956f | 412 | "In ARG, insert a & before each &, each space, each newline, and -. |
0b0d3e0b | 413 | Change spaces to underscores, too, so that the return value never |
6afdd335 KL |
414 | contains a space. |
415 | ||
416 | See `server-unquote-arg' and `server-process-filter'." | |
0b0d3e0b KL |
417 | (replace-regexp-in-string |
418 | "[-&\n ]" (lambda (s) | |
419 | (case (aref s 0) | |
420 | (?& "&&") | |
421 | (?- "&-") | |
422 | (?\n "&n") | |
423 | (?\s "&_"))) | |
424 | arg t t)) | |
425 | ||
6afdd335 KL |
426 | (defun server-send-string (proc string) |
427 | "A wrapper around `proc-send-string' for logging." | |
428 | (server-log (concat "Sent " string) proc) | |
429 | (process-send-string proc string)) | |
430 | ||
724629d2 SM |
431 | (defun server-ensure-safe-dir (dir) |
432 | "Make sure DIR is a directory with no race-condition issues. | |
433 | Creates the directory if necessary and makes sure: | |
434 | - there's no symlink involved | |
435 | - it's owned by us | |
436 | - it's not readable/writable by anybody else." | |
437 | (setq dir (directory-file-name dir)) | |
438 | (let ((attrs (file-attributes dir))) | |
439 | (unless attrs | |
337e3c70 | 440 | (letf (((default-file-modes) ?\700)) (make-directory dir t)) |
724629d2 SM |
441 | (setq attrs (file-attributes dir))) |
442 | ;; Check that it's safe for use. | |
da6657b7 | 443 | (unless (and (eq t (car attrs)) (eql (nth 2 attrs) (user-uid)) |
337e3c70 JB |
444 | (or (eq system-type 'windows-nt) |
445 | (zerop (logand ?\077 (file-modes dir))))) | |
724629d2 SM |
446 | (error "The directory %s is unsafe" dir)))) |
447 | ||
7229064d | 448 | ;;;###autoload |
9ae0f972 | 449 | (defun server-start (&optional leave-dead) |
450 | "Allow this Emacs process to be a server for client processes. | |
451 | This starts a server communications subprocess through which | |
6d3a46f7 KL |
452 | client \"editors\" can send your editing commands to this Emacs |
453 | job. To use the server, set up the program `emacsclient' in the | |
9ae0f972 | 454 | Emacs distribution as your standard \"editor\". |
455 | ||
bd410bb0 EZ |
456 | Optional argument LEAVE-DEAD (interactively, a prefix arg) means just |
457 | kill any existing server communications subprocess." | |
9ae0f972 | 458 | (interactive "P") |
d6b4b3cf KL |
459 | (when (or |
460 | (not server-clients) | |
461 | (yes-or-no-p | |
462 | "The current server still has clients; delete them? ")) | |
974b73e8 KL |
463 | (when server-process |
464 | ;; kill it dead! | |
465 | (ignore-errors (delete-process server-process))) | |
d6b4b3cf KL |
466 | ;; Delete the socket files made by previous server invocations. |
467 | (condition-case () | |
468 | (delete-file (expand-file-name server-name server-socket-dir)) | |
469 | (error nil)) | |
470 | ;; If this Emacs already had a server, clear out associated status. | |
471 | (while server-clients | |
472 | (server-delete-client (car server-clients))) | |
9f97e26d | 473 | ;; Now any previous server is properly stopped. |
d6b4b3cf KL |
474 | (if leave-dead |
475 | (progn | |
476 | (server-log (message "Server stopped")) | |
477 | (setq server-process nil)) | |
974b73e8 KL |
478 | (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir)) |
479 | (server-file (expand-file-name server-name server-dir))) | |
480 | ;; Make sure there is a safe directory in which to place the socket. | |
481 | (server-ensure-safe-dir server-dir) | |
482 | ;; Remove any leftover socket or authentication file. | |
483 | (ignore-errors (delete-file server-file)) | |
484 | (when server-process | |
485 | (server-log (message "Restarting server"))) | |
486 | (letf (((default-file-modes) ?\700)) | |
e4019195 | 487 | (add-hook 'suspend-tty-functions 'server-handle-suspend-tty) |
974b73e8 KL |
488 | (add-hook 'delete-frame-functions 'server-handle-delete-frame) |
489 | (add-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function) | |
490 | (add-hook 'kill-emacs-query-functions 'server-kill-emacs-query-function) | |
1d515b42 | 491 | (add-hook 'kill-emacs-hook (lambda () (server-mode -1))) ;Cleanup upon exit. |
974b73e8 KL |
492 | (setq server-process |
493 | (apply #'make-network-process | |
494 | :name server-name | |
495 | :server t | |
496 | :noquery t | |
497 | :sentinel 'server-sentinel | |
498 | :filter 'server-process-filter | |
499 | ;; We must receive file names without being decoded. | |
500 | ;; Those are decoded by server-process-filter according | |
1b0a6c68 SM |
501 | ;; to file-name-coding-system. Also don't get |
502 | ;; confused by CRs since we don't quote them. | |
503 | :coding 'raw-text-unix | |
28cbade4 | 504 | ;; The other args depend on the kind of socket used. |
974b73e8 KL |
505 | (if server-use-tcp |
506 | (list :family nil | |
507 | :service t | |
508 | :host (or server-host 'local) | |
509 | :plist '(:authenticated nil)) | |
510 | (list :family 'local | |
511 | :service server-file | |
512 | :plist '(:authenticated t))))) | |
513 | (unless server-process (error "Could not start server process")) | |
514 | (when server-use-tcp | |
515 | (let ((auth-key | |
516 | (loop | |
d76e2046 JB |
517 | ;; The auth key is a 64-byte string of random chars in the |
518 | ;; range `!'..`~'. | |
519 | for i below 64 | |
520 | collect (+ 33 (random 94)) into auth | |
521 | finally return (concat auth)))) | |
974b73e8 KL |
522 | (process-put server-process :auth-key auth-key) |
523 | (with-temp-file server-file | |
524 | (set-buffer-multibyte nil) | |
525 | (setq buffer-file-coding-system 'no-conversion) | |
526 | (insert (format-network-address | |
527 | (process-contact server-process :local)) | |
528 | " " (int-to-string (emacs-pid)) | |
529 | "\n" auth-key))))))))) | |
33186f32 | 530 | |
44954c2f SM |
531 | (defun server-running-p (&optional name) |
532 | "Test whether server NAME is running." | |
533 | (interactive | |
534 | (list (if current-prefix-arg | |
535 | (read-string "Server name: " nil nil server-name)))) | |
536 | (unless name (setq name server-name)) | |
537 | (condition-case nil | |
538 | (progn | |
539 | (delete-process | |
540 | (make-network-process | |
541 | :name "server-client-test" :family 'local :server nil :noquery t | |
542 | :service (expand-file-name name server-socket-dir))) | |
543 | t) | |
544 | (file-error nil))) | |
545 | ||
33186f32 DL |
546 | ;;;###autoload |
547 | (define-minor-mode server-mode | |
548 | "Toggle Server mode. | |
549 | With ARG, turn Server mode on if ARG is positive, off otherwise. | |
550 | Server mode runs a process that accepts commands from the | |
551 | `emacsclient' program. See `server-start' and Info node `Emacs server'." | |
552 | :global t | |
553 | :group 'server | |
bf247b6e | 554 | :version "22.1" |
33186f32 DL |
555 | ;; Fixme: Should this check for an existing server socket and do |
556 | ;; nothing if there is one (for multiple Emacs sessions)? | |
557 | (server-start (not server-mode))) | |
9ae0f972 | 558 | \f |
13ba3740 SM |
559 | (defun server-eval-and-print (expr proc) |
560 | "Eval EXPR and send the result back to client PROC." | |
561 | (let ((v (eval (car (read-from-string expr))))) | |
562 | (when (and v proc) | |
563 | (with-temp-buffer | |
564 | (let ((standard-output (current-buffer))) | |
565 | (pp v) | |
566 | (let ((text (buffer-substring-no-properties | |
567 | (point-min) (point-max)))) | |
568 | (server-send-string | |
569 | proc (format "-print %s\n" | |
570 | (server-quote-arg text))))))))) | |
571 | ||
572 | (defun server-create-tty-frame (tty type proc) | |
4419b755 | 573 | (add-to-list 'frame-inherited-parameters 'client) |
13ba3740 SM |
574 | (let ((frame |
575 | (server-with-environment (process-get proc 'env) | |
576 | '("LANG" "LC_CTYPE" "LC_ALL" | |
577 | ;; For tgetent(3); list according to ncurses(3). | |
578 | "BAUDRATE" "COLUMNS" "ESCDELAY" "HOME" "LINES" | |
579 | "NCURSES_ASSUMED_COLORS" "NCURSES_NO_PADDING" | |
580 | "NCURSES_NO_SETBUF" "TERM" "TERMCAP" "TERMINFO" | |
c48254fb | 581 | "TERMINFO_DIRS" "TERMPATH" |
13ba3740 SM |
582 | ;; rxvt wants these |
583 | "COLORFGBG" "COLORTERM") | |
584 | (make-frame-on-tty tty type | |
585 | ;; Ignore nowait here; we always need to | |
586 | ;; clean up opened ttys when the client dies. | |
587 | `((client . ,proc) | |
4419b755 SM |
588 | ;; This is a leftover from an earlier |
589 | ;; attempt at making it possible for process | |
590 | ;; run in the server process to use the | |
591 | ;; environment of the client process. | |
592 | ;; It has no effect now and to make it work | |
593 | ;; we'd need to decide how to make | |
594 | ;; process-environment interact with client | |
595 | ;; envvars, and then to change the | |
596 | ;; C functions `child_setup' and | |
597 | ;; `getenv_internal' accordingly. | |
448f754f | 598 | (environment . ,(process-get proc 'env))))))) |
c48254fb | 599 | |
e159b869 SM |
600 | ;; ttys don't use the `display' parameter, but callproc.c does to set |
601 | ;; the DISPLAY environment on subprocesses. | |
602 | (set-frame-parameter frame 'display | |
603 | (getenv-internal "DISPLAY" (process-get proc 'env))) | |
13ba3740 | 604 | (select-frame frame) |
448f754f | 605 | (process-put proc 'frame frame) |
448f754f | 606 | (process-put proc 'terminal (frame-terminal frame)) |
13ba3740 SM |
607 | |
608 | ;; Display *scratch* by default. | |
609 | (switch-to-buffer (get-buffer-create "*scratch*") 'norecord) | |
610 | ||
611 | ;; Reply with our pid. | |
612 | (server-send-string proc (concat "-emacs-pid " | |
613 | (number-to-string (emacs-pid)) "\n")) | |
614 | frame)) | |
615 | ||
616 | (defun server-create-window-system-frame (display nowait proc) | |
4419b755 | 617 | (add-to-list 'frame-inherited-parameters 'client) |
e159b869 | 618 | (if (not (fboundp 'make-frame-on-display)) |
13ba3740 SM |
619 | (progn |
620 | ;; This emacs does not support X. | |
621 | (server-log "Window system unsupported" proc) | |
622 | (server-send-string proc "-window-system-unsupported \n") | |
623 | nil) | |
624 | ;; Flag frame as client-created, but use a dummy client. | |
625 | ;; This will prevent the frame from being deleted when | |
626 | ;; emacsclient quits while also preventing | |
627 | ;; `server-save-buffers-kill-terminal' from unexpectedly | |
628 | ;; killing emacs on that frame. | |
629 | (let* ((params `((client . ,(if nowait 'nowait proc)) | |
4419b755 | 630 | ;; This is a leftover, see above. |
13ba3740 SM |
631 | (environment . ,(process-get proc 'env)))) |
632 | (frame (make-frame-on-display | |
633 | (or display | |
634 | (frame-parameter nil 'display) | |
635 | (getenv "DISPLAY") | |
636 | (error "Please specify display")) | |
448f754f | 637 | params))) |
13ba3740 | 638 | (server-log (format "%s created" frame) proc) |
13ba3740 | 639 | (select-frame frame) |
448f754f SM |
640 | (process-put proc 'frame frame) |
641 | (process-put proc 'terminal (frame-terminal frame)) | |
13ba3740 SM |
642 | |
643 | ;; Display *scratch* by default. | |
644 | (switch-to-buffer (get-buffer-create "*scratch*") 'norecord) | |
645 | frame))) | |
646 | ||
647 | ||
648 | (defun server-goto-toplevel (proc) | |
649 | (condition-case nil | |
650 | ;; If we're running isearch, we must abort it to allow Emacs to | |
651 | ;; display the buffer and switch to it. | |
652 | (dolist (buffer (buffer-list)) | |
653 | (with-current-buffer buffer | |
654 | (when (bound-and-true-p isearch-mode) | |
655 | (isearch-cancel)))) | |
656 | ;; Signaled by isearch-cancel. | |
657 | (quit (message nil))) | |
658 | (when (> (recursion-depth) 0) | |
659 | ;; We're inside a minibuffer already, so if the emacs-client is trying | |
660 | ;; to open a frame on a new display, we might end up with an unusable | |
661 | ;; frame because input from that display will be blocked (until exiting | |
662 | ;; the minibuffer). Better exit this minibuffer right away. | |
663 | ;; Similarly with recursive-edits such as the splash screen. | |
664 | (run-with-timer 0 nil (lexical-let ((proc proc)) | |
665 | (lambda () (server-execute-continuation proc)))) | |
666 | (top-level))) | |
667 | ||
668 | ;; We use various special properties on process objects: | |
669 | ;; - `env' stores the info about the environment of the emacsclient process. | |
670 | ;; - `continuation' is a no-arg function that we need to execute. It contains | |
671 | ;; commands we wanted to execute in some earlier invocation of the process | |
672 | ;; filter but that we somehow were unable to process at that time | |
673 | ;; (e.g. because we first need to throw to the toplevel). | |
674 | ||
675 | (defun server-execute-continuation (proc) | |
676 | (let ((continuation (process-get proc 'continuation))) | |
677 | (process-put proc 'continuation nil) | |
678 | (if continuation (ignore-errors (funcall continuation))))) | |
679 | ||
337e3c70 | 680 | (defun* server-process-filter (proc string) |
33186f32 | 681 | "Process a request from the server to edit some files. |
6afdd335 KL |
682 | PROC is the server process. STRING consists of a sequence of |
683 | commands prefixed by a dash. Some commands have arguments; these | |
684 | are &-quoted and need to be decoded by `server-unquote-arg'. The | |
685 | filter parses and executes these commands. | |
686 | ||
687 | To illustrate the protocol, here is an example command that | |
688 | emacsclient sends to create a new X frame (note that the whole | |
689 | sequence is sent on a single line): | |
690 | ||
6afdd335 KL |
691 | -env HOME /home/lorentey |
692 | -env DISPLAY :0.0 | |
693 | ... lots of other -env commands | |
694 | -display :0.0 | |
695 | -window-system | |
696 | ||
6afdd335 KL |
697 | The following commands are accepted by the server: |
698 | ||
974b73e8 KL |
699 | `-auth AUTH-STRING' |
700 | Authenticate the client using the secret authentication string | |
a38daa0a | 701 | AUTH-STRING. |
974b73e8 | 702 | |
59e085e0 | 703 | `-env NAME=VALUE' |
6afdd335 KL |
704 | An environment variable on the client side. |
705 | ||
2828d5f9 KL |
706 | `-dir DIRNAME' |
707 | The current working directory of the client process. | |
708 | ||
92071250 KL |
709 | `-current-frame' |
710 | Forbid the creation of new frames. | |
711 | ||
6afdd335 KL |
712 | `-nowait' |
713 | Request that the next frame created should not be | |
714 | associated with this client. | |
715 | ||
716 | `-display DISPLAY' | |
717 | Set the display name to open X frames on. | |
718 | ||
719 | `-position LINE[:COLUMN]' | |
720 | Go to the given line and column number | |
721 | in the next file opened. | |
722 | ||
723 | `-file FILENAME' | |
724 | Load the given file in the current frame. | |
725 | ||
726 | `-eval EXPR' | |
727 | Evaluate EXPR as a Lisp expression and return the | |
728 | result in -print commands. | |
729 | ||
730 | `-window-system' | |
731 | Open a new X frame. | |
732 | ||
733 | `-tty DEVICENAME TYPE' | |
734 | Open a new tty frame at the client. | |
735 | ||
6afdd335 KL |
736 | `-suspend' |
737 | Suspend this tty frame. The client sends this string in | |
738 | response to SIGTSTP and SIGTTOU. The server must cease all I/O | |
739 | on this tty until it gets a -resume command. | |
740 | ||
2828d5f9 | 741 | `-resume' |
c48254fb | 742 | Resume this tty frame. The client sends this string when it |
2828d5f9 KL |
743 | gets the SIGCONT signal and it is the foreground process on its |
744 | controlling tty. | |
745 | ||
6afdd335 KL |
746 | `-ignore COMMENT' |
747 | Do nothing, but put the comment in the server | |
748 | log. Useful for debugging. | |
749 | ||
750 | ||
751 | The following commands are accepted by the client: | |
752 | ||
6afdd335 KL |
753 | `-emacs-pid PID' |
754 | Describes the process id of the Emacs process; | |
755 | used to forward window change signals to it. | |
756 | ||
757 | `-window-system-unsupported' | |
c48254fb JB |
758 | Signals that the server does not support creating X frames; |
759 | the client must try again with a tty frame. | |
6afdd335 KL |
760 | |
761 | `-print STRING' | |
762 | Print STRING on stdout. Used to send values | |
763 | returned by -eval. | |
764 | ||
765 | `-error DESCRIPTION' | |
766 | Signal an error (but continue processing). | |
767 | ||
768 | `-suspend' | |
c48254fb JB |
769 | Suspend this terminal, i.e., stop the client process. |
770 | Sent when the user presses C-z." | |
6afdd335 | 771 | (server-log (concat "Received " string) proc) |
337e3c70 JB |
772 | ;; First things first: let's check the authentication |
773 | (unless (process-get proc :authenticated) | |
38b9f0f3 | 774 | (if (and (string-match "-auth \\([!-~]+\\)\n?" string) |
974b73e8 KL |
775 | (equal (match-string 1 string) (process-get proc :auth-key))) |
776 | (progn | |
777 | (setq string (substring string (match-end 0))) | |
778 | (process-put proc :authenticated t) | |
779 | (server-log "Authentication successful" proc)) | |
337e3c70 | 780 | (server-log "Authentication failed" proc) |
974b73e8 KL |
781 | (server-send-string |
782 | proc (concat "-error " (server-quote-arg "Authentication failed"))) | |
337e3c70 JB |
783 | (delete-process proc) |
784 | ;; We return immediately | |
785 | (return-from server-process-filter))) | |
3f7ef08e SM |
786 | (let ((prev (process-get proc 'previous-string))) |
787 | (when prev | |
788 | (setq string (concat prev string)) | |
789 | (process-put proc 'previous-string nil))) | |
a9298135 | 790 | (condition-case err |
0b0d3e0b | 791 | (progn |
9002956f | 792 | (server-add-client proc) |
13ba3740 SM |
793 | (if (not (string-match "\n" string)) |
794 | ;; Save for later any partial line that remains. | |
795 | (when (> (length string) 0) | |
796 | (process-put proc 'previous-string string)) | |
c48254fb | 797 | |
13ba3740 SM |
798 | ;; In earlier versions of server.el (where we used an `emacsserver' |
799 | ;; process), there could be multiple lines. Nowadays this is not | |
800 | ;; supported any more. | |
801 | (assert (eq (match-end 0) (length string))) | |
0b0d3e0b KL |
802 | (let ((request (substring string 0 (match-beginning 0))) |
803 | (coding-system (and default-enable-multibyte-characters | |
804 | (or file-name-coding-system | |
805 | default-file-name-coding-system))) | |
9002956f KL |
806 | nowait ; t if emacsclient does not want to wait for us. |
807 | frame ; The frame that was opened for the client (if any). | |
59e085e0 | 808 | display ; Open the frame on this display. |
0b0d3e0b | 809 | dontkill ; t if the client should not be killed. |
13ba3740 | 810 | (commands ()) |
2828d5f9 | 811 | dir |
13ba3740 SM |
812 | (tty-name nil) ;nil, `window-system', or the tty name. |
813 | tty-type ;string. | |
0b0d3e0b | 814 | (files nil) |
656d4706 | 815 | (filepos nil) |
bfb74e75 DK |
816 | command-line-args-left |
817 | arg) | |
0b0d3e0b KL |
818 | ;; Remove this line from STRING. |
819 | (setq string (substring string (match-end 0))) | |
bfb74e75 DK |
820 | (setq command-line-args-left |
821 | (mapcar 'server-unquote-arg (split-string request " " t))) | |
822 | (while (setq arg (pop command-line-args-left)) | |
0b0d3e0b | 823 | (cond |
13ba3740 | 824 | ;; -version CLIENT-VERSION: obsolete at birth. |
bfb74e75 DK |
825 | ((and (equal "-version" arg) command-line-args-left) |
826 | (pop command-line-args-left)) | |
0b0d3e0b | 827 | |
9002956f | 828 | ;; -nowait: Emacsclient won't wait for a result. |
0b0d3e0b KL |
829 | ((equal "-nowait" arg) (setq nowait t)) |
830 | ||
92071250 | 831 | ;; -current-frame: Don't create frames. |
13ba3740 | 832 | ((equal "-current-frame" arg) (setq tty-name nil)) |
92071250 | 833 | |
9002956f | 834 | ;; -display DISPLAY: |
59e085e0 | 835 | ;; Open X frames on the given display instead of the default. |
bfb74e75 | 836 | ((and (equal "-display" arg) command-line-args-left) |
ff91dc79 SM |
837 | (setq display (pop command-line-args-left)) |
838 | (if (zerop (length display)) (setq display nil))) | |
0b0d3e0b | 839 | |
9002956f | 840 | ;; -window-system: Open a new X frame. |
0b0d3e0b | 841 | ((equal "-window-system" arg) |
13ba3740 SM |
842 | (setq dontkill t) |
843 | (setq tty-name 'window-system)) | |
9002956f KL |
844 | |
845 | ;; -resume: Resume a suspended tty frame. | |
0b0d3e0b | 846 | ((equal "-resume" arg) |
448f754f | 847 | (lexical-let ((terminal (process-get proc 'terminal))) |
0b0d3e0b | 848 | (setq dontkill t) |
13ba3740 SM |
849 | (push (lambda () |
850 | (when (eq (terminal-live-p terminal) t) | |
851 | (resume-tty terminal))) | |
852 | commands))) | |
0b0d3e0b | 853 | |
9002956f KL |
854 | ;; -suspend: Suspend the client's frame. (In case we |
855 | ;; get out of sync, and a C-z sends a SIGTSTP to | |
856 | ;; emacsclient.) | |
0b0d3e0b | 857 | ((equal "-suspend" arg) |
448f754f | 858 | (lexical-let ((terminal (process-get proc 'terminal))) |
0b0d3e0b | 859 | (setq dontkill t) |
13ba3740 SM |
860 | (push (lambda () |
861 | (when (eq (terminal-live-p terminal) t) | |
862 | (suspend-tty terminal))) | |
863 | commands))) | |
0b0d3e0b | 864 | |
9002956f KL |
865 | ;; -ignore COMMENT: Noop; useful for debugging emacsclient. |
866 | ;; (The given comment appears in the server log.) | |
bfb74e75 DK |
867 | ((and (equal "-ignore" arg) command-line-args-left |
868 | (setq dontkill t) | |
869 | (pop command-line-args-left))) | |
0b0d3e0b | 870 | |
9002956f | 871 | ;; -tty DEVICE-NAME TYPE: Open a new tty frame at the client. |
13ba3740 | 872 | ((and (equal "-tty" arg) |
bfb74e75 DK |
873 | (cdr command-line-args-left)) |
874 | (setq tty-name (pop command-line-args-left) | |
875 | tty-type (pop command-line-args-left) | |
876 | dontkill t)) | |
13ba3740 SM |
877 | |
878 | ;; -position LINE[:COLUMN]: Set point to the given | |
879 | ;; position in the next file. | |
880 | ((and (equal "-position" arg) | |
bfb74e75 DK |
881 | command-line-args-left |
882 | (string-match "\\+\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?" | |
883 | (car command-line-args-left))) | |
884 | (setq arg (pop command-line-args-left)) | |
656d4706 SM |
885 | (setq filepos |
886 | (cons (string-to-number (match-string 1 arg)) | |
887 | (string-to-number (or (match-string 2 arg) ""))))) | |
0b0d3e0b | 888 | |
9002956f | 889 | ;; -file FILENAME: Load the given file. |
13ba3740 | 890 | ((and (equal "-file" arg) |
bfb74e75 DK |
891 | command-line-args-left) |
892 | (let ((file (pop command-line-args-left))) | |
0b0d3e0b KL |
893 | (if coding-system |
894 | (setq file (decode-coding-string file coding-system))) | |
895 | (setq file (command-line-normalize-file-name file)) | |
656d4706 SM |
896 | (push (cons file filepos) files) |
897 | (server-log (format "New file: %s %s" | |
898 | file (or filepos "")) proc)) | |
899 | (setq filepos nil)) | |
0b0d3e0b | 900 | |
9002956f | 901 | ;; -eval EXPR: Evaluate a Lisp expression. |
13ba3740 | 902 | ((and (equal "-eval" arg) |
bfb74e75 DK |
903 | command-line-args-left) |
904 | (lexical-let ((expr (pop command-line-args-left))) | |
0b0d3e0b KL |
905 | (if coding-system |
906 | (setq expr (decode-coding-string expr coding-system))) | |
13ba3740 SM |
907 | (push (lambda () (server-eval-and-print expr proc)) |
908 | commands) | |
656d4706 | 909 | (setq filepos nil))) |
0b0d3e0b | 910 | |
59e085e0 | 911 | ;; -env NAME=VALUE: An environment variable. |
bfb74e75 DK |
912 | ((and (equal "-env" arg) command-line-args-left) |
913 | (let ((var (pop command-line-args-left))) | |
da8e8fc1 | 914 | ;; XXX Variables should be encoded as in getenv/setenv. |
13ba3740 SM |
915 | (process-put proc 'env |
916 | (cons var (process-get proc 'env))))) | |
9002956f | 917 | |
2828d5f9 | 918 | ;; -dir DIRNAME: The cwd of the emacsclient process. |
bfb74e75 DK |
919 | ((and (equal "-dir" arg) command-line-args-left) |
920 | (setq dir (pop command-line-args-left)) | |
2828d5f9 KL |
921 | (if coding-system |
922 | (setq dir (decode-coding-string dir coding-system))) | |
923 | (setq dir (command-line-normalize-file-name dir))) | |
924 | ||
0b0d3e0b | 925 | ;; Unknown command. |
bfb74e75 | 926 | (t (error "Unknown command: %s" arg)))) |
c48254fb | 927 | |
13ba3740 SM |
928 | (setq frame |
929 | (case tty-name | |
930 | ((nil) (if display (server-select-display display))) | |
931 | ((window-system) | |
932 | (server-create-window-system-frame display nowait proc)) | |
933 | (t (server-create-tty-frame tty-name tty-type proc)))) | |
934 | ||
28cbade4 SM |
935 | (process-put |
936 | proc 'continuation | |
937 | (lexical-let ((proc proc) | |
938 | (files files) | |
939 | (nowait nowait) | |
940 | (commands commands) | |
941 | (dontkill dontkill) | |
942 | (frame frame) | |
943 | (dir dir) | |
944 | (tty-name tty-name)) | |
945 | (lambda () | |
946 | (with-current-buffer (get-buffer-create server-buffer) | |
947 | ;; Use the same cwd as the emacsclient, if possible, so | |
948 | ;; relative file names work correctly, even in `eval'. | |
949 | (let ((default-directory | |
f03ea9d9 JB |
950 | (if (and dir (file-directory-p dir)) |
951 | dir default-directory))) | |
28cbade4 SM |
952 | (server-execute proc files nowait commands |
953 | dontkill frame tty-name)))))) | |
13ba3740 SM |
954 | |
955 | (when (or frame files) | |
956 | (server-goto-toplevel proc)) | |
957 | ||
958 | (server-execute-continuation proc)))) | |
a9298135 | 959 | ;; condition-case |
13ba3740 SM |
960 | (error (server-return-error proc err)))) |
961 | ||
962 | (defun server-execute (proc files nowait commands dontkill frame tty-name) | |
963 | (condition-case err | |
448f754f | 964 | (let* ((buffers |
13ba3740 SM |
965 | (when files |
966 | (run-hooks 'pre-command-hook) | |
448f754f | 967 | (prog1 (server-visit-files files proc nowait) |
13ba3740 SM |
968 | (run-hooks 'post-command-hook))))) |
969 | ||
970 | (mapc 'funcall (nreverse commands)) | |
c48254fb | 971 | |
13ba3740 SM |
972 | ;; Delete the client if necessary. |
973 | (cond | |
974 | (nowait | |
975 | ;; Client requested nowait; return immediately. | |
976 | (server-log "Close nowait client" proc) | |
977 | (server-delete-client proc)) | |
978 | ((and (not dontkill) (null buffers)) | |
979 | ;; This client is empty; get rid of it immediately. | |
980 | (server-log "Close empty client" proc) | |
981 | (server-delete-client proc))) | |
982 | (cond | |
983 | ((or isearch-mode (minibufferp)) | |
984 | nil) | |
985 | ((and frame (null buffers)) | |
986 | (message "%s" (substitute-command-keys | |
987 | "When done with this frame, type \\[delete-frame]"))) | |
988 | ((not (null buffers)) | |
989 | (server-switch-buffer (car buffers)) | |
990 | (run-hooks 'server-switch-hook) | |
991 | (unless nowait | |
992 | (message "%s" (substitute-command-keys | |
993 | "When done with a buffer, type \\[server-edit]"))))) | |
994 | (when (and frame (null tty-name)) | |
995 | (server-unselect-display frame))) | |
996 | (error (server-return-error proc err)))) | |
997 | ||
998 | (defun server-return-error (proc err) | |
999 | (ignore-errors | |
1000 | (server-send-string | |
1001 | proc (concat "-error " (server-quote-arg | |
1002 | (error-message-string err)))) | |
1003 | (server-log (error-message-string err) proc) | |
1004 | (delete-process proc))) | |
9ae0f972 | 1005 | |
656d4706 SM |
1006 | (defun server-goto-line-column (line-col) |
1007 | "Move point to the position indicated in LINE-COL. | |
1008 | LINE-COL should be a pair (LINE . COL)." | |
1009 | (when line-col | |
1010 | (goto-line (car line-col)) | |
1011 | (let ((column-number (cdr line-col))) | |
1012 | (when (> column-number 0) | |
1013 | (move-to-column (1- column-number)))))) | |
6b98185f | 1014 | |
448f754f | 1015 | (defun server-visit-files (files proc &optional nowait) |
c5b0a355 | 1016 | "Find FILES and return a list of buffers created. |
656d4706 SM |
1017 | FILES is an alist whose elements are (FILENAME . FILEPOS) |
1018 | where FILEPOS can be nil or a pair (LINENUMBER . COLUMNNUMBER). | |
448f754f | 1019 | PROC is the client that requested this operation. |
dfa35e6b RS |
1020 | NOWAIT non-nil means this client is not waiting for the results, |
1021 | so don't mark these buffers specially, just visit them normally." | |
e82e73c2 | 1022 | ;; Bind last-nonmenu-event to force use of keyboard, not mouse, for queries. |
44a56b29 | 1023 | (let ((last-nonmenu-event t) client-record) |
3a0ce849 RS |
1024 | ;; Restore the current buffer afterward, but not using save-excursion, |
1025 | ;; because we don't want to save point in this buffer | |
1026 | ;; if it happens to be one of those specified by the server. | |
44a56b29 SM |
1027 | (save-current-buffer |
1028 | (dolist (file files) | |
1029 | ;; If there is an existing buffer modified or the file is | |
1030 | ;; modified, revert it. If there is an existing buffer with | |
1031 | ;; deleted file, offer to write it. | |
cd7320d4 | 1032 | (let* ((minibuffer-auto-raise (or server-raise-frame |
974b73e8 | 1033 | minibuffer-auto-raise)) |
c2d0d432 | 1034 | (filen (car file)) |
44a56b29 | 1035 | (obuf (get-file-buffer filen))) |
c398358a | 1036 | (add-to-history 'file-name-history filen) |
656d4706 SM |
1037 | (if (null obuf) |
1038 | (set-buffer (find-file-noselect filen)) | |
1039 | (set-buffer obuf) | |
1040 | (cond ((file-exists-p filen) | |
1041 | (when (not (verify-visited-file-modtime obuf)) | |
1042 | (revert-buffer t nil))) | |
1043 | (t | |
1044 | (when (y-or-n-p | |
1045 | (concat "File no longer exists: " filen | |
1046 | ", write buffer to file? ")) | |
1047 | (write-file filen)))) | |
1048 | (unless server-buffer-clients | |
1049 | (setq server-existing-buffer t))) | |
1050 | (server-goto-line-column (cdr file)) | |
1051 | (run-hooks 'server-visit-hook)) | |
44a56b29 SM |
1052 | (unless nowait |
1053 | ;; When the buffer is killed, inform the clients. | |
1054 | (add-hook 'kill-buffer-hook 'server-kill-buffer nil t) | |
448f754f | 1055 | (push proc server-buffer-clients)) |
c5b0a355 KL |
1056 | (push (current-buffer) client-record))) |
1057 | (unless nowait | |
448f754f SM |
1058 | (process-put proc 'buffers |
1059 | (nconc (process-get proc 'buffers) client-record))) | |
c5b0a355 | 1060 | client-record)) |
9ae0f972 | 1061 | \f |
b392bac9 | 1062 | (defun server-buffer-done (buffer &optional for-killing) |
9ae0f972 | 1063 | "Mark BUFFER as \"done\" for its client(s). |
9184aafb RS |
1064 | This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED). |
1065 | NEXT-BUFFER is another server buffer, as a suggestion for what to select next, | |
8b3e840e SM |
1066 | or nil. KILLED is t if we killed BUFFER (typically, because it was visiting |
1067 | a temp file). | |
1068 | FOR-KILLING if non-nil indicates that we are called from `kill-buffer'." | |
0c851d78 | 1069 | (let ((next-buffer nil) |
9002956f | 1070 | (killed nil)) |
448f754f SM |
1071 | (dolist (proc server-clients) |
1072 | (let ((buffers (process-get proc 'buffers))) | |
8b3e840e | 1073 | (or next-buffer |
9002956f KL |
1074 | (setq next-buffer (nth 1 (memq buffer buffers)))) |
1075 | (when buffers ; Ignore bufferless clients. | |
1076 | (setq buffers (delq buffer buffers)) | |
448f754f | 1077 | ;; Delete all dead buffers from PROC. |
9002956f KL |
1078 | (dolist (b buffers) |
1079 | (and (bufferp b) | |
1080 | (not (buffer-live-p b)) | |
1081 | (setq buffers (delq b buffers)))) | |
448f754f | 1082 | (process-put proc 'buffers buffers) |
9002956f KL |
1083 | ;; If client now has no pending buffers, |
1084 | ;; tell it that it is done, and forget it entirely. | |
1085 | (unless buffers | |
448f754f SM |
1086 | (server-log "Close" proc) |
1087 | (server-delete-client proc))))) | |
337e3c70 JB |
1088 | (when (and (bufferp buffer) (buffer-name buffer)) |
1089 | ;; We may or may not kill this buffer; | |
1090 | ;; if we do, do not call server-buffer-done recursively | |
1091 | ;; from kill-buffer-hook. | |
1092 | (let ((server-kill-buffer-running t)) | |
1093 | (with-current-buffer buffer | |
1094 | (setq server-buffer-clients nil) | |
1095 | (run-hooks 'server-done-hook)) | |
1096 | ;; Notice whether server-done-hook killed the buffer. | |
1097 | (if (null (buffer-name buffer)) | |
1098 | (setq killed t) | |
1099 | ;; Don't bother killing or burying the buffer | |
1100 | ;; when we are called from kill-buffer. | |
1101 | (unless for-killing | |
1102 | (when (and (not killed) | |
1103 | server-kill-new-buffers | |
1104 | (with-current-buffer buffer | |
1105 | (not server-existing-buffer))) | |
599f9a5c | 1106 | (setq killed t) |
337e3c70 JB |
1107 | (bury-buffer buffer) |
1108 | (kill-buffer buffer)) | |
1109 | (unless killed | |
1110 | (if (server-temp-file-p buffer) | |
1111 | (progn | |
1112 | (kill-buffer buffer) | |
1113 | (setq killed t)) | |
1114 | (bury-buffer buffer))))))) | |
9184aafb | 1115 | (list next-buffer killed))) |
9ae0f972 | 1116 | |
408784a7 | 1117 | (defun server-temp-file-p (&optional buffer) |
9ae0f972 | 1118 | "Return non-nil if BUFFER contains a file considered temporary. |
1119 | These are files whose names suggest they are repeatedly | |
1120 | reused to pass information to another program. | |
1121 | ||
1122 | The variable `server-temp-file-regexp' controls which filenames | |
1123 | are considered temporary." | |
1124 | (and (buffer-file-name buffer) | |
1125 | (string-match server-temp-file-regexp (buffer-file-name buffer)))) | |
1126 | ||
1127 | (defun server-done () | |
cc9875f9 | 1128 | "Offer to save current buffer, mark it as \"done\" for clients. |
ed9ae328 RS |
1129 | This kills or buries the buffer, then returns a list |
1130 | of the form (NEXT-BUFFER KILLED). NEXT-BUFFER is another server buffer, | |
1131 | as a suggestion for what to select next, or nil. | |
1132 | KILLED is t if we killed BUFFER, which happens if it was created | |
1133 | specifically for the clients and did not exist before their request for it." | |
408784a7 SM |
1134 | (when server-buffer-clients |
1135 | (if (server-temp-file-p) | |
1136 | ;; For a temp file, save, and do make a non-numeric backup | |
1137 | ;; (unless make-backup-files is nil). | |
1138 | (let ((version-control nil) | |
1139 | (buffer-backed-up nil)) | |
1140 | (save-buffer)) | |
337e3c70 JB |
1141 | (when (and (buffer-modified-p) |
1142 | buffer-file-name | |
1143 | (y-or-n-p (concat "Save file " buffer-file-name "? "))) | |
1144 | (save-buffer))) | |
408784a7 | 1145 | (server-buffer-done (current-buffer)))) |
faf931a8 | 1146 | |
71207de2 RS |
1147 | ;; Ask before killing a server buffer. |
1148 | ;; It was suggested to release its client instead, | |
1149 | ;; but I think that is dangerous--the client would proceed | |
1150 | ;; using whatever is on disk in that file. -- rms. | |
03d78665 | 1151 | (defun server-kill-buffer-query-function () |
9002956f | 1152 | "Ask before killing a server buffer." |
03d78665 | 1153 | (or (not server-buffer-clients) |
114a8b8c KL |
1154 | (let ((res t)) |
1155 | (dolist (proc server-buffer-clients res) | |
448f754f SM |
1156 | (when (and (memq proc server-clients) |
1157 | (eq (process-status proc) 'open)) | |
1158 | (setq res nil)))) | |
03d78665 RS |
1159 | (yes-or-no-p (format "Buffer `%s' still has clients; kill it? " |
1160 | (buffer-name (current-buffer)))))) | |
1161 | ||
03d78665 | 1162 | (defun server-kill-emacs-query-function () |
c48254fb | 1163 | "Ask before exiting Emacs if it has live clients." |
9002956f KL |
1164 | (or (not server-clients) |
1165 | (let (live-client) | |
448f754f SM |
1166 | (dolist (proc server-clients live-client) |
1167 | (when (memq t (mapcar 'buffer-live-p (process-get | |
1168 | proc 'buffers))) | |
974b73e8 | 1169 | (setq live-client t)))) |
9002956f | 1170 | (yes-or-no-p "This Emacs session has clients; exit anyway? "))) |
b392bac9 | 1171 | |
fb873cfc | 1172 | (defvar server-kill-buffer-running nil |
599f9a5c | 1173 | "Non-nil while `server-kill-buffer' or `server-buffer-done' is running.") |
fb873cfc | 1174 | |
b392bac9 | 1175 | (defun server-kill-buffer () |
6d3a46f7 KL |
1176 | "Remove the current buffer from its clients' buffer list. |
1177 | Designed to be added to `kill-buffer-hook'." | |
fb873cfc RS |
1178 | ;; Prevent infinite recursion if user has made server-done-hook |
1179 | ;; call kill-buffer. | |
1180 | (or server-kill-buffer-running | |
599f9a5c RS |
1181 | (and server-buffer-clients |
1182 | (let ((server-kill-buffer-running t)) | |
1183 | (when server-process | |
1184 | (server-buffer-done (current-buffer) t)))))) | |
9ae0f972 | 1185 | \f |
1186 | (defun server-edit (&optional arg) | |
1187 | "Switch to next server editing buffer; say \"Done\" for current buffer. | |
1188 | If a server buffer is current, it is marked \"done\" and optionally saved. | |
ed9ae328 | 1189 | The buffer is also killed if it did not exist before the clients asked for it. |
9ae0f972 | 1190 | When all of a client's buffers are marked as \"done\", the client is notified. |
1191 | ||
1192 | Temporary files such as MH <draft> files are always saved and backed up, | |
991298c3 RS |
1193 | no questions asked. (The variable `make-backup-files', if nil, still |
1194 | inhibits a backup; you can set it locally in a particular buffer to | |
1195 | prevent a backup for it.) The variable `server-temp-file-regexp' controls | |
9ae0f972 | 1196 | which filenames are considered temporary. |
1197 | ||
64f51134 | 1198 | If invoked with a prefix argument, or if there is no server process running, |
9ae0f972 | 1199 | starts server process and that is all. Invoked by \\[server-edit]." |
9ae0f972 | 1200 | (interactive "P") |
6b519504 | 1201 | (cond |
95eefb35 JB |
1202 | ((or arg |
1203 | (not server-process) | |
1204 | (memq (process-status server-process) '(signal exit))) | |
1205 | (server-mode 1)) | |
1206 | (server-clients (apply 'server-switch-buffer (server-done))) | |
1207 | (t (message "No server editing buffers exist")))) | |
9ae0f972 | 1208 | |
98a4349c | 1209 | (defun server-switch-buffer (&optional next-buffer killed-one) |
9ae0f972 | 1210 | "Switch to another buffer, preferably one that has a client. |
6d3a46f7 KL |
1211 | Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it. |
1212 | ||
1213 | KILLED-ONE is t in a recursive call if we have already killed one | |
1214 | temp-file server buffer. This means we should avoid the final | |
1215 | \"switch to some other buffer\" since we've already effectively | |
1216 | done that." | |
ca0c7250 | 1217 | (if (null next-buffer) |
9002956f KL |
1218 | (progn |
1219 | (let ((rest server-clients)) | |
1220 | (while (and rest (not next-buffer)) | |
448f754f | 1221 | (let ((proc (car rest))) |
a7ce6c7f AS |
1222 | ;; Only look at frameless clients, or those in the selected |
1223 | ;; frame. | |
1224 | (when (or (not (process-get proc 'frame)) | |
1225 | (eq (process-get proc 'frame) (selected-frame))) | |
448f754f | 1226 | (setq next-buffer (car (process-get proc 'buffers)))) |
9002956f KL |
1227 | (setq rest (cdr rest))))) |
1228 | (and next-buffer (server-switch-buffer next-buffer killed-one)) | |
1229 | (unless (or next-buffer killed-one (window-dedicated-p (selected-window))) | |
1230 | ;; (switch-to-buffer (other-buffer)) | |
90ee5627 | 1231 | (message "No server buffers remain to edit"))) |
9002956f | 1232 | (if (not (buffer-live-p next-buffer)) |
ca0c7250 | 1233 | ;; If NEXT-BUFFER is a dead buffer, remove the server records for it |
9ae0f972 | 1234 | ;; and try the next surviving server buffer. |
ca0c7250 SM |
1235 | (apply 'server-switch-buffer (server-buffer-done next-buffer)) |
1236 | ;; OK, we know next-buffer is live, let's display and select it. | |
408784a7 SM |
1237 | (if (functionp server-window) |
1238 | (funcall server-window next-buffer) | |
1239 | (let ((win (get-buffer-window next-buffer 0))) | |
1240 | (if (and win (not server-window)) | |
1241 | ;; The buffer is already displayed: just reuse the window. | |
90caccca JB |
1242 | (progn |
1243 | (select-window win) | |
1244 | (set-buffer next-buffer)) | |
408784a7 | 1245 | ;; Otherwise, let's find an appropriate window. |
60b4b298 | 1246 | (cond ((window-live-p server-window) |
408784a7 SM |
1247 | (select-window server-window)) |
1248 | ((framep server-window) | |
337e3c70 JB |
1249 | (unless (frame-live-p server-window) |
1250 | (setq server-window (make-frame))) | |
408784a7 | 1251 | (select-window (frame-selected-window server-window)))) |
337e3c70 JB |
1252 | (when (window-minibuffer-p (selected-window)) |
1253 | (select-window (next-window nil 'nomini 0))) | |
408784a7 SM |
1254 | ;; Move to a non-dedicated window, if we have one. |
1255 | (when (window-dedicated-p (selected-window)) | |
1256 | (select-window | |
1257 | (get-window-with-predicate | |
1258 | (lambda (w) | |
1259 | (and (not (window-dedicated-p w)) | |
6ed8eeff KL |
1260 | (equal (frame-terminal (window-frame w)) |
1261 | (frame-terminal (selected-frame))))) | |
408784a7 SM |
1262 | 'nomini 'visible (selected-window)))) |
1263 | (condition-case nil | |
1264 | (switch-to-buffer next-buffer) | |
1265 | ;; After all the above, we might still have ended up with | |
1266 | ;; a minibuffer/dedicated-window (if there's no other). | |
90caccca JB |
1267 | (error (pop-to-buffer next-buffer))))))) |
1268 | (when server-raise-frame | |
1269 | (select-frame-set-input-focus (window-frame (selected-window)))))) | |
9ae0f972 | 1270 | |
59e085e0 | 1271 | ;;;###autoload |
6ed8eeff | 1272 | (defun server-save-buffers-kill-terminal (proc &optional arg) |
c61a4448 | 1273 | ;; Called from save-buffers-kill-terminal in files.el. |
59e085e0 | 1274 | "Offer to save each buffer, then kill PROC. |
b4ca0271 | 1275 | |
7540c1e0 KL |
1276 | With prefix arg, silently save all file-visiting buffers, then kill. |
1277 | ||
1278 | If emacsclient was started with a list of filenames to edit, then | |
1279 | only these files will be asked to be saved." | |
c61a4448 SM |
1280 | ;; save-buffers-kill-terminal occasionally calls us with proc set |
1281 | ;; to `nowait' (comes from the value of the `client' frame parameter). | |
1282 | (when (processp proc) | |
1283 | (let ((buffers (process-get proc 'buffers))) | |
1284 | ;; If client is bufferless, emulate a normal Emacs session | |
1285 | ;; exit and offer to save all buffers. Otherwise, offer to | |
1286 | ;; save only the buffers belonging to the client. | |
1287 | (save-some-buffers arg | |
1288 | (if buffers | |
1289 | (lambda () (memq (current-buffer) buffers)) | |
1290 | t)) | |
1291 | (server-delete-client proc)))) | |
b4ca0271 | 1292 | |
772c5eb7 | 1293 | (define-key ctl-x-map "#" 'server-edit) |
df4e8a11 | 1294 | |
08446d5e | 1295 | (defun server-unload-function () |
6d3a46f7 | 1296 | "Unload the server library." |
2e8457a0 | 1297 | (server-mode -1) |
a8e0c053 | 1298 | (substitute-key-definition 'server-edit nil ctl-x-map) |
08446d5e JB |
1299 | (save-current-buffer |
1300 | (dolist (buffer (buffer-list)) | |
1301 | (set-buffer buffer) | |
1302 | (remove-hook 'kill-buffer-hook 'server-kill-buffer t))) | |
1303 | ;; continue standard unloading | |
1304 | nil) | |
3bb38bc2 | 1305 | |
16c15321 RM |
1306 | \f |
1307 | (provide 'server) | |
c88ab9ce | 1308 | |
2e8457a0 | 1309 | ;; arch-tag: 1f7ecb42-f00a-49f8-906d-61995d84c8d6 |
c88ab9ce | 1310 | ;;; server.el ends here |