Commit | Line | Data |
---|---|---|
c530e1c2 | 1 | ;;; server.el --- Lisp code for GNU Emacs running as server process -*- lexical-binding: t -*- |
c88ab9ce | 2 | |
ba318903 | 3 | ;; Copyright (C) 1986-1987, 1992, 1994-2014 Free Software Foundation, |
ab422c4d | 4 | ;; 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 | 84 | |
a464a6c7 | 85 | (eval-when-compile (require 'cl-lib)) |
8b3e840e | 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"))) | |
337e3c70 JB |
98 | (set-default sym val)) |
99 | :group 'server | |
100 | :type 'boolean | |
101 | :version "22.1") | |
102 | ||
103 | (defcustom server-host nil | |
104 | "The name or IP address to use as host address of the server process. | |
48de8b12 CY |
105 | If set, the server accepts remote connections; otherwise it is local. |
106 | ||
107 | DO NOT give this a non-nil value unless you know what you are | |
108 | doing! On unsecured networks, accepting remote connections is | |
109 | very dangerous, because server-client communication (including | |
110 | session authentication) is not encrypted." | |
337e3c70 JB |
111 | :group 'server |
112 | :type '(choice | |
113 | (string :tag "Name or IP address") | |
114 | (const :tag "Local" nil)) | |
115 | :version "22.1") | |
7d3e3843 | 116 | ;;;###autoload |
337e3c70 JB |
117 | (put 'server-host 'risky-local-variable t) |
118 | ||
c79b0b1c | 119 | (defcustom server-port nil |
7a1ff57f CY |
120 | "The port number that the server process should listen on. |
121 | This variable only takes effect when the Emacs server is using | |
122 | TCP instead of local sockets. A nil value means to use a random | |
123 | port number." | |
c79b0b1c | 124 | :group 'server |
c79b0b1c PO |
125 | :type '(choice |
126 | (string :tag "Port number") | |
127 | (const :tag "Random" nil)) | |
128 | :version "24.1") | |
7d3e3843 GM |
129 | ;;;###autoload |
130 | (put 'server-port 'risky-local-variable t) | |
c79b0b1c | 131 | |
d6c180c4 | 132 | (defcustom server-auth-dir (locate-user-emacs-file "server/") |
3e70541a | 133 | "Directory for server authentication files. |
953cebf5 GM |
134 | We only use this if `server-use-tcp' is non-nil. |
135 | Otherwise we use `server-socket-dir'. | |
3e70541a JB |
136 | |
137 | NOTE: On FAT32 filesystems, directories are not secure; | |
138 | files can be read and modified by any user or process. | |
139 | It is strongly suggested to set `server-auth-dir' to a | |
140 | directory residing in a NTFS partition instead." | |
337e3c70 JB |
141 | :group 'server |
142 | :type 'directory | |
143 | :version "22.1") | |
7d3e3843 | 144 | ;;;###autoload |
337e3c70 JB |
145 | (put 'server-auth-dir 'risky-local-variable t) |
146 | ||
29734c21 MN |
147 | (defcustom server-auth-key nil |
148 | "Server authentication key. | |
48de8b12 | 149 | This is only used if `server-use-tcp' is non-nil. |
29734c21 | 150 | |
3603c3b1 | 151 | Normally, the authentication key is randomly generated when the |
48de8b12 CY |
152 | server starts. It is recommended to leave it that way. Using a |
153 | long-lived shared key will decrease security (especially since | |
154 | the key is transmitted as plain-text). | |
29734c21 MN |
155 | |
156 | In some situations however, it can be difficult to share randomly | |
65e7ca35 | 157 | generated passwords with remote hosts (e.g., no shared directory), |
3603c3b1 JB |
158 | so you can set the key with this variable and then copy the |
159 | server file to the remote host (with possible changes to IP | |
160 | address and/or port if that applies). | |
29734c21 | 161 | |
48de8b12 CY |
162 | Note that the usual security risks of using the server over |
163 | remote TCP, arising from the fact that client-server | |
164 | communications are unencrypted, still apply. | |
29734c21 | 165 | |
48de8b12 CY |
166 | The key must consist of 64 ASCII printable characters except for |
167 | space (this means characters from ! to ~; or from code 33 to | |
168 | 126). You can use \\[server-generate-key] to get a random key." | |
29734c21 MN |
169 | :group 'server |
170 | :type '(choice | |
171 | (const :tag "Random" nil) | |
172 | (string :tag "Password")) | |
2a1e2476 | 173 | :version "24.3") |
29734c21 | 174 | |
90caccca | 175 | (defcustom server-raise-frame t |
ff348fba | 176 | "If non-nil, raise frame when switching to a buffer." |
90caccca JB |
177 | :group 'server |
178 | :type 'boolean | |
179 | :version "22.1") | |
180 | ||
ab1c7f35 | 181 | (defcustom server-visit-hook nil |
ff348fba | 182 | "Hook run when visiting a file for the Emacs server." |
ab1c7f35 | 183 | :group 'server |
0c851d78 | 184 | :type 'hook) |
ab1c7f35 RS |
185 | |
186 | (defcustom server-switch-hook nil | |
ff348fba | 187 | "Hook run when switching to a buffer for the Emacs server." |
ab1c7f35 | 188 | :group 'server |
0c851d78 | 189 | :type 'hook) |
ab1c7f35 RS |
190 | |
191 | (defcustom server-done-hook nil | |
ff348fba | 192 | "Hook run when done editing a buffer for the Emacs server." |
ab1c7f35 | 193 | :group 'server |
0c851d78 | 194 | :type 'hook) |
f9b3ef88 | 195 | |
64f51134 JPW |
196 | (defvar server-process nil |
197 | "The current server process.") | |
9ae0f972 | 198 | |
199 | (defvar server-clients nil | |
200 | "List of current server clients. | |
448f754f | 201 | Each element is a process.") |
0a125897 | 202 | |
9ae0f972 | 203 | (defvar server-buffer-clients nil |
5f3c1a63 | 204 | "List of client processes requesting editing of current buffer.") |
faf931a8 | 205 | (make-variable-buffer-local 'server-buffer-clients) |
9ae0f972 | 206 | ;; Changing major modes should not erase this local. |
207 | (put 'server-buffer-clients 'permanent-local t) | |
208 | ||
33186f32 | 209 | (defcustom server-window nil |
ff348fba | 210 | "Specification of the window to use for selecting Emacs server buffers. |
ec40ed9f | 211 | If nil, use the selected window. |
408784a7 | 212 | If it is a function, it should take one argument (a buffer) and |
33186f32 DL |
213 | display and select it. A common value is `pop-to-buffer'. |
214 | If it is a window, use that. | |
215 | If it is a frame, use the frame's selected window. | |
216 | ||
217 | It is not meaningful to set this to a specific frame or window with Custom. | |
218 | Only programs can do so." | |
219 | :group 'server | |
bf247b6e | 220 | :version "22.1" |
33186f32 DL |
221 | :type '(choice (const :tag "Use selected window" |
222 | :match (lambda (widget value) | |
223 | (not (functionp value))) | |
224 | nil) | |
ee9272ff | 225 | (function-item :tag "Display in new frame" switch-to-buffer-other-frame) |
33186f32 DL |
226 | (function-item :tag "Use pop-to-buffer" pop-to-buffer) |
227 | (function :tag "Other function"))) | |
ec40ed9f | 228 | |
ab1c7f35 | 229 | (defcustom server-temp-file-regexp "^/tmp/Re\\|/draft$" |
ff348fba | 230 | "Regexp matching names of temporary files. |
33186f32 DL |
231 | These are deleted and reused after each edit by the programs that |
232 | invoke the Emacs server." | |
ab1c7f35 RS |
233 | :group 'server |
234 | :type 'regexp) | |
9ae0f972 | 235 | |
c6a117f0 | 236 | (defcustom server-kill-new-buffers t |
ff348fba | 237 | "Whether to kill buffers when done with them. |
c6a117f0 | 238 | If non-nil, kill a buffer unless it already existed before editing |
f258b525 | 239 | it with the Emacs server. If nil, kill only buffers as specified by |
c6a117f0 | 240 | `server-temp-file-regexp'. |
f258b525 | 241 | Please note that only buffers that still have a client are killed, |
a77ad240 JB |
242 | i.e. buffers visited with \"emacsclient --no-wait\" are never killed |
243 | in this way." | |
c6a117f0 GM |
244 | :group 'server |
245 | :type 'boolean | |
246 | :version "21.1") | |
247 | ||
9ae0f972 | 248 | (or (assq 'server-buffer-clients minor-mode-alist) |
b7621225 | 249 | (push '(server-buffer-clients " Server") minor-mode-alist)) |
9ae0f972 | 250 | |
c6a117f0 | 251 | (defvar server-existing-buffer nil |
ca0c7250 | 252 | "Non-nil means the buffer existed before the server was asked to visit it. |
4dd04714 | 253 | This means that the server should not kill the buffer when you say you |
ca0c7250 | 254 | are done with it in the server.") |
c6a117f0 GM |
255 | (make-variable-buffer-local 'server-existing-buffer) |
256 | ||
77c4d024 CY |
257 | (defcustom server-name "server" |
258 | "The name of the Emacs server, if this Emacs process creates one. | |
259 | The command `server-start' makes use of this. It should not be | |
260 | changed while a server is running." | |
261 | :group 'server | |
262 | :type 'string | |
263 | :version "23.1") | |
03ae35cf | 264 | |
03542605 CY |
265 | ;; We do not use `temporary-file-directory' here, because emacsclient |
266 | ;; does not read the init file. | |
f77b11a0 | 267 | (defvar server-socket-dir |
a77ad240 JB |
268 | (and (featurep 'make-network-process '(:family local)) |
269 | (format "%s/emacs%d" (or (getenv "TMPDIR") "/tmp") (user-uid))) | |
270 | "The directory in which to place the server socket. | |
271 | If local sockets are not supported, this is nil.") | |
0c851d78 | 272 | |
9002956f KL |
273 | (defun server-clients-with (property value) |
274 | "Return a list of clients with PROPERTY set to VALUE." | |
275 | (let (result) | |
2403c841 | 276 | (dolist (proc server-clients) |
448f754f | 277 | (when (equal value (process-get proc property)) |
2403c841 SM |
278 | (push proc result))) |
279 | result)) | |
9002956f KL |
280 | |
281 | (defun server-add-client (proc) | |
282 | "Create a client for process PROC, if it doesn't already have one. | |
283 | New clients have no properties." | |
448f754f | 284 | (add-to-list 'server-clients proc)) |
9002956f | 285 | |
59e085e0 KL |
286 | (defmacro server-with-environment (env vars &rest body) |
287 | "Evaluate BODY with environment variables VARS set to those in ENV. | |
65f64034 KL |
288 | The environment variables are then restored to their previous values. |
289 | ||
59e085e0 KL |
290 | VARS should be a list of strings. |
291 | ENV should be in the same format as `process-environment'." | |
65f64034 | 292 | (declare (indent 2)) |
13ba3740 SM |
293 | (let ((var (make-symbol "var")) |
294 | (value (make-symbol "value"))) | |
295 | `(let ((process-environment process-environment)) | |
59e085e0 | 296 | (dolist (,var ,vars) |
e159b869 | 297 | (let ((,value (getenv-internal ,var ,env))) |
abab01f2 JB |
298 | (push (if (stringp ,value) |
299 | (concat ,var "=" ,value) | |
300 | ,var) | |
13ba3740 SM |
301 | process-environment))) |
302 | (progn ,@body)))) | |
65f64034 | 303 | |
448f754f | 304 | (defun server-delete-client (proc &optional noframe) |
c48254fb | 305 | "Delete PROC, including its buffers, terminals and frames. |
a77ad240 JB |
306 | If NOFRAME is non-nil, let the frames live. |
307 | Updates `server-clients'." | |
ff91dc79 | 308 | (server-log (concat "server-delete-client" (if noframe " noframe")) proc) |
9002956f | 309 | ;; Force a new lookup of client (prevents infinite recursion). |
448f754f SM |
310 | (when (memq proc server-clients) |
311 | (let ((buffers (process-get proc 'buffers))) | |
9002956f | 312 | |
5f3c1a63 | 313 | ;; Kill the client's buffers. |
9002956f | 314 | (dolist (buf buffers) |
6ed9e43a KL |
315 | (when (buffer-live-p buf) |
316 | (with-current-buffer buf | |
6ed9e43a | 317 | ;; Kill the buffer if necessary. |
5f3c1a63 KL |
318 | (when (and (equal server-buffer-clients |
319 | (list proc)) | |
6ed9e43a KL |
320 | (or (and server-kill-new-buffers |
321 | (not server-existing-buffer)) | |
5f3c1a63 KL |
322 | (server-temp-file-p)) |
323 | (not (buffer-modified-p))) | |
324 | (let (flag) | |
325 | (unwind-protect | |
326 | (progn (setq server-buffer-clients nil) | |
327 | (kill-buffer (current-buffer)) | |
328 | (setq flag t)) | |
329 | (unless flag | |
330 | ;; Restore clients if user pressed C-g in `kill-buffer'. | |
331 | (setq server-buffer-clients (list proc))))))))) | |
9002956f | 332 | |
160f0817 KL |
333 | ;; Delete the client's frames. |
334 | (unless noframe | |
335 | (dolist (frame (frame-list)) | |
336 | (when (and (frame-live-p frame) | |
337 | (equal proc (frame-parameter frame 'client))) | |
338 | ;; Prevent `server-handle-delete-frame' from calling us | |
339 | ;; recursively. | |
340 | (set-frame-parameter frame 'client nil) | |
341 | (delete-frame frame)))) | |
342 | ||
448f754f | 343 | (setq server-clients (delq proc server-clients)) |
5f3c1a63 | 344 | |
520fca41 JB |
345 | ;; Delete the client's tty, except on Windows (both GUI and console), |
346 | ;; where there's only one terminal and does not make sense to delete it. | |
347 | (unless (eq system-type 'windows-nt) | |
348 | (let ((terminal (process-get proc 'terminal))) | |
349 | ;; Only delete the terminal if it is non-nil. | |
350 | (when (and terminal (eq (terminal-live-p terminal) t)) | |
351 | (delete-terminal terminal)))) | |
9002956f | 352 | |
9002956f | 353 | ;; Delete the client's process. |
448f754f SM |
354 | (if (eq (process-status proc) 'open) |
355 | (delete-process proc)) | |
9002956f KL |
356 | |
357 | (server-log "Deleted" proc)))) | |
358 | ||
88fd26a1 | 359 | (defvar server-log-time-function 'current-time-string |
f03ea9d9 | 360 | "Function to generate timestamps for `server-buffer'.") |
88fd26a1 | 361 | |
28cbade4 SM |
362 | (defconst server-buffer " *server*" |
363 | "Buffer used internally by Emacs's server. | |
9fc9a531 | 364 | One use is to log the I/O for debugging purposes (see option `server-log'), |
28cbade4 | 365 | the other is to provide a current buffer in which the process filter can |
f03ea9d9 | 366 | safely let-bind buffer-local variables like `default-directory'.") |
28cbade4 SM |
367 | |
368 | (defvar server-log nil | |
369 | "If non-nil, log the server's inputs and outputs in the `server-buffer'.") | |
370 | ||
8b3e840e | 371 | (defun server-log (string &optional client) |
9fc9a531 | 372 | "If option `server-log' is non-nil, log STRING to `server-buffer'. |
c48254fb | 373 | If CLIENT is non-nil, add a description of it to the logged message." |
28cbade4 SM |
374 | (when server-log |
375 | (with-current-buffer (get-buffer-create server-buffer) | |
337e3c70 | 376 | (goto-char (point-max)) |
88fd26a1 | 377 | (insert (funcall server-log-time-function) |
974b73e8 | 378 | (cond |
94d11cb5 IK |
379 | ((null client) " ") |
380 | ((listp client) (format " %s: " (car client))) | |
381 | (t (format " %s: " client))) | |
337e3c70 JB |
382 | string) |
383 | (or (bolp) (newline))))) | |
9ae0f972 | 384 | |
385 | (defun server-sentinel (proc msg) | |
9002956f | 386 | "The process sentinel for Emacs server connections." |
cbfc02e4 RF |
387 | ;; If this is a new client process, set the query-on-exit flag to nil |
388 | ;; for this process (it isn't inherited from the server process). | |
389 | (when (and (eq (process-status proc) 'open) | |
390 | (process-query-on-exit-flag proc)) | |
391 | (set-process-query-on-exit-flag proc nil)) | |
757e1681 | 392 | ;; Delete the associated connection file, if applicable. |
c63a334e JB |
393 | ;; Although there's no 100% guarantee that the file is owned by the |
394 | ;; running Emacs instance, server-start uses server-running-p to check | |
395 | ;; for possible servers before doing anything, so it *should* be ours. | |
396 | (and (process-contact proc :server) | |
397 | (eq (process-status proc) 'closed) | |
84716442 | 398 | (ignore-errors |
94d11cb5 | 399 | (delete-file (process-get proc :server-file)))) |
9002956f KL |
400 | (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc) |
401 | (server-delete-client proc)) | |
0c851d78 | 402 | |
e5248ac9 SM |
403 | (defun server--on-display-p (frame display) |
404 | (and (equal (frame-parameter frame 'display) display) | |
405 | ;; Note: TTY frames still get a `display' parameter set to the value of | |
406 | ;; $DISPLAY. This is useful when running from that tty frame | |
407 | ;; sub-processes that want to connect to the X server, but that means we | |
408 | ;; have to be careful here not to be tricked into thinking those frames | |
409 | ;; are on `display'. | |
410 | (not (eq (framep frame) t)))) | |
411 | ||
13ba3740 SM |
412 | (defun server-select-display (display) |
413 | ;; If the current frame is on `display' we're all set. | |
a77ad240 | 414 | ;; Similarly if we are unable to open frames on other displays, there's |
1a4a884c SM |
415 | ;; nothing more we can do. |
416 | (unless (or (not (fboundp 'make-frame-on-display)) | |
e5248ac9 | 417 | (server--on-display-p (selected-frame) display)) |
13ba3740 SM |
418 | ;; Otherwise, look for an existing frame there and select it. |
419 | (dolist (frame (frame-list)) | |
e5248ac9 | 420 | (when (server--on-display-p frame display) |
13ba3740 SM |
421 | (select-frame frame))) |
422 | ;; If there's no frame on that display yet, create and select one. | |
e5248ac9 | 423 | (unless (server--on-display-p (selected-frame) display) |
13ba3740 SM |
424 | (let* ((buffer (generate-new-buffer " *server-dummy*")) |
425 | (frame (make-frame-on-display | |
426 | display | |
427 | ;; Make it display (and remember) some dummy buffer, so | |
428 | ;; we can detect later if the frame is in use or not. | |
ff91dc79 | 429 | `((server-dummy-buffer . ,buffer) |
13ba3740 SM |
430 | ;; This frame may be deleted later (see |
431 | ;; server-unselect-display) so we want it to be as | |
432 | ;; unobtrusive as possible. | |
433 | (visibility . nil))))) | |
434 | (select-frame frame) | |
435 | (set-window-buffer (selected-window) buffer) | |
436 | frame)))) | |
437 | ||
438 | (defun server-unselect-display (frame) | |
439 | (when (frame-live-p frame) | |
440 | ;; If the temporary frame is in use (displays something real), make it | |
441 | ;; visible. If not (which can happen if the user's customizations call | |
442 | ;; pop-to-buffer etc.), delete it to avoid preserving the connection after | |
443 | ;; the last real frame is deleted. | |
ffb6157e MR |
444 | |
445 | ;; Rewritten to avoid inadvertently killing the current buffer after | |
446 | ;; `delete-frame' removed FRAME (Bug#10729). | |
447 | (let ((buffer (frame-parameter frame 'server-dummy-buffer))) | |
1dba6978 MR |
448 | (if (and (one-window-p 'nomini frame) |
449 | (eq (window-buffer (frame-first-window frame)) buffer)) | |
450 | ;; The temp frame still only shows one buffer, and that is the | |
451 | ;; internal temp buffer. | |
452 | (delete-frame frame) | |
453 | (set-frame-parameter frame 'visibility t) | |
454 | (set-frame-parameter frame 'server-dummy-buffer nil)) | |
451077ad | 455 | (when (buffer-live-p buffer) |
451077ad | 456 | (kill-buffer buffer))))) |
13ba3740 | 457 | |
77134727 | 458 | (defun server-handle-delete-frame (frame) |
a77ad240 JB |
459 | "Delete the client connection when the emacsclient frame is deleted. |
460 | \(To be used from `delete-frame-functions'.)" | |
9002956f | 461 | (let ((proc (frame-parameter frame 'client))) |
e519a50b KL |
462 | (when (and (frame-live-p frame) |
463 | proc | |
160f0817 | 464 | ;; See if this is the last frame for this client. |
d5381da2 | 465 | (>= 1 (let ((frame-num 0)) |
94d11cb5 IK |
466 | (dolist (f (frame-list)) |
467 | (when (eq proc (frame-parameter f 'client)) | |
468 | (setq frame-num (1+ frame-num)))) | |
469 | frame-num))) | |
9002956f | 470 | (server-log (format "server-handle-delete-frame, frame %s" frame) proc) |
de93c791 | 471 | (server-delete-client proc 'noframe)))) ; Let delete-frame delete the frame later. |
9002956f | 472 | |
6ed8eeff | 473 | (defun server-handle-suspend-tty (terminal) |
d032d5e7 | 474 | "Notify the client process that its tty device is suspended." |
6ed8eeff | 475 | (dolist (proc (server-clients-with 'terminal terminal)) |
d032d5e7 SM |
476 | (server-log (format "server-handle-suspend-tty, terminal %s" terminal) |
477 | proc) | |
478 | (condition-case nil | |
6afdd335 | 479 | (server-send-string proc "-suspend \n") |
44954c2f SM |
480 | (file-error ;The pipe/socket was closed. |
481 | (ignore-errors (server-delete-client proc)))))) | |
44a56b29 | 482 | |
0c851d78 | 483 | (defun server-unquote-arg (arg) |
6afdd335 KL |
484 | "Remove &-quotation from ARG. |
485 | See `server-quote-arg' and `server-process-filter'." | |
0c851d78 SM |
486 | (replace-regexp-in-string |
487 | "&." (lambda (s) | |
a464a6c7 | 488 | (pcase (aref s 1) |
0c851d78 SM |
489 | (?& "&") |
490 | (?- "-") | |
491 | (?n "\n") | |
a464a6c7 | 492 | (_ " "))) |
0c851d78 | 493 | arg t t)) |
9ae0f972 | 494 | |
0b0d3e0b | 495 | (defun server-quote-arg (arg) |
9002956f | 496 | "In ARG, insert a & before each &, each space, each newline, and -. |
0b0d3e0b | 497 | Change spaces to underscores, too, so that the return value never |
6afdd335 KL |
498 | contains a space. |
499 | ||
500 | See `server-unquote-arg' and `server-process-filter'." | |
0b0d3e0b KL |
501 | (replace-regexp-in-string |
502 | "[-&\n ]" (lambda (s) | |
a464a6c7 | 503 | (pcase (aref s 0) |
0b0d3e0b KL |
504 | (?& "&&") |
505 | (?- "&-") | |
506 | (?\n "&n") | |
507 | (?\s "&_"))) | |
508 | arg t t)) | |
509 | ||
6afdd335 | 510 | (defun server-send-string (proc string) |
a77ad240 | 511 | "A wrapper around `process-send-string' for logging." |
6afdd335 KL |
512 | (server-log (concat "Sent " string) proc) |
513 | (process-send-string proc string)) | |
514 | ||
724629d2 SM |
515 | (defun server-ensure-safe-dir (dir) |
516 | "Make sure DIR is a directory with no race-condition issues. | |
517 | Creates the directory if necessary and makes sure: | |
518 | - there's no symlink involved | |
519 | - it's owned by us | |
520 | - it's not readable/writable by anybody else." | |
521 | (setq dir (directory-file-name dir)) | |
d7554167 | 522 | (let ((attrs (file-attributes dir 'integer))) |
724629d2 | 523 | (unless attrs |
a464a6c7 | 524 | (cl-letf (((default-file-modes) ?\700)) (make-directory dir t)) |
d7554167 | 525 | (setq attrs (file-attributes dir 'integer))) |
3e70541a | 526 | |
724629d2 | 527 | ;; Check that it's safe for use. |
3e70541a JB |
528 | (let* ((uid (nth 2 attrs)) |
529 | (w32 (eq system-type 'windows-nt)) | |
197b6f3c JB |
530 | (safe (cond |
531 | ((not (eq t (car attrs))) nil) ; is a dir? | |
532 | ((and w32 (zerop uid)) ; on FAT32? | |
533 | (display-warning | |
534 | 'server | |
535 | (format "Using `%s' to store Emacs-server authentication files. | |
3e70541a JB |
536 | Directories on FAT32 filesystems are NOT secure against tampering. |
537 | See variable `server-auth-dir' for details." | |
197b6f3c JB |
538 | (file-name-as-directory dir)) |
539 | :warning) | |
540 | t) | |
541 | ((and (/= uid (user-uid)) ; is the dir ours? | |
542 | (or (not w32) | |
543 | ;; Files created on Windows by Administrator | |
544 | ;; (RID=500) have the Administrators (RID=544) | |
545 | ;; group recorded as the owner. | |
546 | (/= uid 544) (/= (user-uid) 500))) | |
547 | nil) | |
548 | (w32 t) ; on NTFS? | |
549 | (t ; else, check permissions | |
550 | (zerop (logand ?\077 (file-modes dir))))))) | |
3e70541a JB |
551 | (unless safe |
552 | (error "The directory `%s' is unsafe" dir))))) | |
724629d2 | 553 | |
29734c21 | 554 | (defun server-generate-key () |
3603c3b1 JB |
555 | "Generate and return a random authentication key. |
556 | The key is a 64-byte string of random chars in the range `!'..`~'. | |
557 | If called interactively, also inserts it into current buffer." | |
29734c21 MN |
558 | (interactive) |
559 | (let ((auth-key | |
a464a6c7 SM |
560 | (cl-loop repeat 64 |
561 | collect (+ 33 (random 94)) into auth | |
562 | finally return (concat auth)))) | |
e6de100c | 563 | (if (called-interactively-p 'interactive) |
29734c21 MN |
564 | (insert auth-key)) |
565 | auth-key)) | |
566 | ||
567 | (defun server-get-auth-key () | |
3603c3b1 | 568 | "Return server's authentication key. |
29734c21 | 569 | |
3603c3b1 JB |
570 | If `server-auth-key' is nil, just call `server-generate-key'. |
571 | Otherwise, if `server-auth-key' is a valid key, return it. | |
572 | If the key is not valid, signal an error." | |
29734c21 | 573 | (if server-auth-key |
3603c3b1 | 574 | (if (string-match-p "^[!-~]\\{64\\}$" server-auth-key) |
29734c21 MN |
575 | server-auth-key |
576 | (error "The key '%s' is invalid" server-auth-key)) | |
577 | (server-generate-key))) | |
578 | ||
7229064d | 579 | ;;;###autoload |
381d186f | 580 | (defun server-start (&optional leave-dead inhibit-prompt) |
9ae0f972 | 581 | "Allow this Emacs process to be a server for client processes. |
3603c3b1 JB |
582 | This starts a server communications subprocess through which client |
583 | \"editors\" can send your editing commands to this Emacs job. | |
584 | To use the server, set up the program `emacsclient' in the Emacs | |
585 | distribution as your standard \"editor\". | |
9ae0f972 | 586 | |
bd410bb0 | 587 | Optional argument LEAVE-DEAD (interactively, a prefix arg) means just |
c63a334e JB |
588 | kill any existing server communications subprocess. |
589 | ||
381d186f CY |
590 | If a server is already running, restart it. If clients are |
591 | running, ask the user for confirmation first, unless optional | |
592 | argument INHIBIT-PROMPT is non-nil. | |
593 | ||
c63a334e JB |
594 | To force-start a server, do \\[server-force-delete] and then |
595 | \\[server-start]." | |
9ae0f972 | 596 | (interactive "P") |
97e121cc CY |
597 | (when (or (not server-clients) |
598 | ;; Ask the user before deleting existing clients---except | |
599 | ;; when we can't get user input, which may happen when | |
600 | ;; doing emacsclient --eval "(kill-emacs)" in daemon mode. | |
381d186f CY |
601 | (cond |
602 | ((and (daemonp) | |
603 | (null (cdr (frame-list))) | |
604 | (eq (selected-frame) terminal-frame)) | |
605 | leave-dead) | |
606 | (inhibit-prompt t) | |
607 | (t (yes-or-no-p | |
608 | "The current server still has clients; delete them? ")))) | |
c63a334e JB |
609 | (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir)) |
610 | (server-file (expand-file-name server-name server-dir))) | |
611 | (when server-process | |
612 | ;; kill it dead! | |
613 | (ignore-errors (delete-process server-process))) | |
614 | ;; Delete the socket files made by previous server invocations. | |
615 | (if (not (eq t (server-running-p server-name))) | |
616 | ;; Remove any leftover socket or authentication file | |
84716442 | 617 | (ignore-errors |
94d11cb5 IK |
618 | (let (delete-by-moving-to-trash) |
619 | (delete-file server-file))) | |
c63a334e | 620 | (setq server-mode nil) ;; already set by the minor mode code |
c31ee003 CY |
621 | (display-warning |
622 | 'server | |
623 | (concat "Unable to start the Emacs server.\n" | |
624 | (format "There is an existing Emacs server, named %S.\n" | |
625 | server-name) | |
626 | "To start the server in this Emacs process, stop the existing | |
627 | server or call `M-x server-force-delete' to forcibly disconnect it.") | |
628 | :warning) | |
35f372ca | 629 | (setq leave-dead t)) |
c63a334e JB |
630 | ;; If this Emacs already had a server, clear out associated status. |
631 | (while server-clients | |
632 | (server-delete-client (car server-clients))) | |
633 | ;; Now any previous server is properly stopped. | |
634 | (if leave-dead | |
635 | (progn | |
35f372ca | 636 | (unless (eq t leave-dead) (server-log (message "Server stopped"))) |
c63a334e | 637 | (setq server-process nil)) |
974b73e8 KL |
638 | ;; Make sure there is a safe directory in which to place the socket. |
639 | (server-ensure-safe-dir server-dir) | |
974b73e8 KL |
640 | (when server-process |
641 | (server-log (message "Restarting server"))) | |
a464a6c7 | 642 | (cl-letf (((default-file-modes) ?\700)) |
e4019195 | 643 | (add-hook 'suspend-tty-functions 'server-handle-suspend-tty) |
974b73e8 | 644 | (add-hook 'delete-frame-functions 'server-handle-delete-frame) |
a464a6c7 SM |
645 | (add-hook 'kill-buffer-query-functions |
646 | 'server-kill-buffer-query-function) | |
647 | (add-hook 'kill-emacs-query-functions | |
648 | 'server-kill-emacs-query-function) | |
381d186f | 649 | (add-hook 'kill-emacs-hook 'server-force-stop) ;Cleanup upon exit. |
974b73e8 KL |
650 | (setq server-process |
651 | (apply #'make-network-process | |
652 | :name server-name | |
653 | :server t | |
654 | :noquery t | |
655 | :sentinel 'server-sentinel | |
656 | :filter 'server-process-filter | |
657 | ;; We must receive file names without being decoded. | |
658 | ;; Those are decoded by server-process-filter according | |
1b0a6c68 SM |
659 | ;; to file-name-coding-system. Also don't get |
660 | ;; confused by CRs since we don't quote them. | |
661 | :coding 'raw-text-unix | |
28cbade4 | 662 | ;; The other args depend on the kind of socket used. |
974b73e8 | 663 | (if server-use-tcp |
d5b8058f | 664 | (list :family 'ipv4 ;; We're not ready for IPv6 yet |
c79b0b1c | 665 | :service (or server-port t) |
ba3033ee | 666 | :host (or server-host 'local) |
974b73e8 KL |
667 | :plist '(:authenticated nil)) |
668 | (list :family 'local | |
669 | :service server-file | |
670 | :plist '(:authenticated t))))) | |
671 | (unless server-process (error "Could not start server process")) | |
c63a334e | 672 | (process-put server-process :server-file server-file) |
974b73e8 | 673 | (when server-use-tcp |
29734c21 | 674 | (let ((auth-key (server-get-auth-key))) |
974b73e8 KL |
675 | (process-put server-process :auth-key auth-key) |
676 | (with-temp-file server-file | |
677 | (set-buffer-multibyte nil) | |
678 | (setq buffer-file-coding-system 'no-conversion) | |
679 | (insert (format-network-address | |
680 | (process-contact server-process :local)) | |
a16f5f64 | 681 | " " (number-to-string (emacs-pid)) ; Kept for compatibility |
974b73e8 | 682 | "\n" auth-key))))))))) |
33186f32 | 683 | |
381d186f CY |
684 | (defun server-force-stop () |
685 | "Kill all connections to the current server. | |
686 | This function is meant to be called from `kill-emacs-hook'." | |
bf6442c3 | 687 | (server-start t t)) |
381d186f | 688 | |
c63a334e JB |
689 | ;;;###autoload |
690 | (defun server-force-delete (&optional name) | |
691 | "Unconditionally delete connection file for server NAME. | |
692 | If server is running, it is first stopped. | |
693 | NAME defaults to `server-name'. With argument, ask for NAME." | |
694 | (interactive | |
695 | (list (if current-prefix-arg | |
696 | (read-string "Server name: " nil nil server-name)))) | |
697 | (when server-mode (with-temp-message nil (server-mode -1))) | |
698 | (let ((file (expand-file-name (or name server-name) | |
699 | (if server-use-tcp | |
700 | server-auth-dir | |
701 | server-socket-dir)))) | |
702 | (condition-case nil | |
84716442 | 703 | (let (delete-by-moving-to-trash) |
c63a334e JB |
704 | (delete-file file) |
705 | (message "Connection file %S deleted" file)) | |
706 | (file-error | |
707 | (message "No connection file %S" file))))) | |
708 | ||
44954c2f | 709 | (defun server-running-p (&optional name) |
c63a334e JB |
710 | "Test whether server NAME is running. |
711 | ||
712 | Return values: | |
713 | nil the server is definitely not running. | |
714 | t the server seems to be running. | |
715 | something else we cannot determine whether it's running without using | |
716 | commands which may have to wait for a long time." | |
44954c2f SM |
717 | (unless name (setq name server-name)) |
718 | (condition-case nil | |
c63a334e JB |
719 | (if server-use-tcp |
720 | (with-temp-buffer | |
721 | (insert-file-contents-literally (expand-file-name name server-auth-dir)) | |
d9569a55 | 722 | (or (and (looking-at "127\\.0\\.0\\.1:[0-9]+ \\([0-9]+\\)") |
c63a334e | 723 | (assq 'comm |
a20878b6 | 724 | (process-attributes |
c63a334e JB |
725 | (string-to-number (match-string 1)))) |
726 | t) | |
727 | :other)) | |
44954c2f SM |
728 | (delete-process |
729 | (make-network-process | |
730 | :name "server-client-test" :family 'local :server nil :noquery t | |
731 | :service (expand-file-name name server-socket-dir))) | |
732 | t) | |
733 | (file-error nil))) | |
734 | ||
33186f32 DL |
735 | ;;;###autoload |
736 | (define-minor-mode server-mode | |
737 | "Toggle Server mode. | |
06e21633 CY |
738 | With a prefix argument ARG, enable Server mode if ARG is |
739 | positive, and disable it otherwise. If called from Lisp, enable | |
740 | Server mode if ARG is omitted or nil. | |
741 | ||
33186f32 | 742 | Server mode runs a process that accepts commands from the |
06e21633 CY |
743 | `emacsclient' program. See Info node `Emacs server' and |
744 | `server-start' for details." | |
33186f32 DL |
745 | :global t |
746 | :group 'server | |
bf247b6e | 747 | :version "22.1" |
33186f32 DL |
748 | ;; Fixme: Should this check for an existing server socket and do |
749 | ;; nothing if there is one (for multiple Emacs sessions)? | |
750 | (server-start (not server-mode))) | |
9ae0f972 | 751 | \f |
13ba3740 SM |
752 | (defun server-eval-and-print (expr proc) |
753 | "Eval EXPR and send the result back to client PROC." | |
28109f49 SM |
754 | ;; While we're running asynchronously (from a process filter), it is likely |
755 | ;; that the emacsclient command was run in response to a user | |
756 | ;; action, so the user probably knows that Emacs is processing this | |
757 | ;; emacsclient request, so if we get a C-g it's likely that the user | |
758 | ;; intended it to interrupt us rather than interrupt whatever Emacs | |
759 | ;; was doing before it started handling the process filter. | |
760 | ;; Hence `with-local-quit' (bug#6585). | |
761 | (let ((v (with-local-quit (eval (car (read-from-string expr)))))) | |
56e6cc31 | 762 | (when proc |
13ba3740 SM |
763 | (with-temp-buffer |
764 | (let ((standard-output (current-buffer))) | |
765 | (pp v) | |
766 | (let ((text (buffer-substring-no-properties | |
767 | (point-min) (point-max)))) | |
e29ab36b AS |
768 | (server-reply-print (server-quote-arg text) proc))))))) |
769 | ||
770 | (defconst server-msg-size 1024 | |
771 | "Maximum size of a message sent to a client.") | |
772 | ||
773 | (defun server-reply-print (qtext proc) | |
774 | "Send a `-print QTEXT' command to client PROC. | |
775 | QTEXT must be already quoted. | |
776 | This handles splitting the command if it would be bigger than | |
777 | `server-msg-size'." | |
778 | (let ((prefix "-print ") | |
779 | part) | |
780 | (while (> (+ (length qtext) (length prefix) 1) server-msg-size) | |
781 | ;; We have to split the string | |
782 | (setq part (substring qtext 0 (- server-msg-size (length prefix) 1))) | |
783 | ;; Don't split in the middle of a quote sequence | |
784 | (if (string-match "\\(^\\|[^&]\\)\\(&&\\)+$" part) | |
785 | ;; There is an uneven number of & at the end | |
786 | (setq part (substring part 0 -1))) | |
787 | (setq qtext (substring qtext (length part))) | |
788 | (server-send-string proc (concat prefix part "\n")) | |
789 | (setq prefix "-print-nonl ")) | |
790 | (server-send-string proc (concat prefix qtext "\n")))) | |
13ba3740 SM |
791 | |
792 | (defun server-create-tty-frame (tty type proc) | |
f79b43b2 DN |
793 | (unless tty |
794 | (error "Invalid terminal device")) | |
795 | (unless type | |
796 | (error "Invalid terminal type")) | |
4419b755 | 797 | (add-to-list 'frame-inherited-parameters 'client) |
13ba3740 SM |
798 | (let ((frame |
799 | (server-with-environment (process-get proc 'env) | |
94d11cb5 IK |
800 | '("LANG" "LC_CTYPE" "LC_ALL" |
801 | ;; For tgetent(3); list according to ncurses(3). | |
802 | "BAUDRATE" "COLUMNS" "ESCDELAY" "HOME" "LINES" | |
803 | "NCURSES_ASSUMED_COLORS" "NCURSES_NO_PADDING" | |
804 | "NCURSES_NO_SETBUF" "TERM" "TERMCAP" "TERMINFO" | |
805 | "TERMINFO_DIRS" "TERMPATH" | |
806 | ;; rxvt wants these | |
807 | "COLORFGBG" "COLORTERM") | |
808 | (make-frame `((window-system . nil) | |
809 | (tty . ,tty) | |
810 | (tty-type . ,type) | |
811 | ;; Ignore nowait here; we always need to | |
812 | ;; clean up opened ttys when the client dies. | |
813 | (client . ,proc) | |
814 | ;; This is a leftover from an earlier | |
815 | ;; attempt at making it possible for process | |
816 | ;; run in the server process to use the | |
817 | ;; environment of the client process. | |
818 | ;; It has no effect now and to make it work | |
819 | ;; we'd need to decide how to make | |
820 | ;; process-environment interact with client | |
821 | ;; envvars, and then to change the | |
822 | ;; C functions `child_setup' and | |
823 | ;; `getenv_internal' accordingly. | |
824 | (environment . ,(process-get proc 'env))))))) | |
052056a9 | 825 | |
e159b869 SM |
826 | ;; ttys don't use the `display' parameter, but callproc.c does to set |
827 | ;; the DISPLAY environment on subprocesses. | |
828 | (set-frame-parameter frame 'display | |
829 | (getenv-internal "DISPLAY" (process-get proc 'env))) | |
13ba3740 | 830 | (select-frame frame) |
448f754f | 831 | (process-put proc 'frame frame) |
448f754f | 832 | (process-put proc 'terminal (frame-terminal frame)) |
13ba3740 SM |
833 | frame)) |
834 | ||
18a4ce5e AR |
835 | (defun server-create-window-system-frame (display nowait proc parent-id |
836 | &optional parameters) | |
efc3dd3c DC |
837 | (let* ((display (or display |
838 | (frame-parameter nil 'display) | |
839 | (error "Please specify display."))) | |
840 | (w (or (cdr (assq 'window-system parameters)) | |
841 | (window-system-for-display display)))) | |
842 | ||
843 | (unless (assq w window-system-initialization-alist) | |
844 | (setq w nil)) | |
845 | ||
d743088d JD |
846 | ;; Special case for ns. This is because DISPLAY may not be set at all |
847 | ;; which in the ns case isn't an error. The variable display then becomes | |
848 | ;; the fully qualified hostname, which make-frame-on-display below | |
849 | ;; does not understand and throws an error. | |
850 | ;; It may also be a valid X display, but if Emacs is compiled for ns, it | |
851 | ;; can not make X frames. | |
531bdce6 JD |
852 | (if (featurep 'ns-win) |
853 | (setq w 'ns display "ns")) | |
854 | ||
efc3dd3c DC |
855 | (cond (w |
856 | ;; Flag frame as client-created, but use a dummy client. | |
857 | ;; This will prevent the frame from being deleted when | |
858 | ;; emacsclient quits while also preventing | |
859 | ;; `server-save-buffers-kill-terminal' from unexpectedly | |
860 | ;; killing emacs on that frame. | |
861 | (let* ((params `((client . ,(if nowait 'nowait proc)) | |
862 | ;; This is a leftover, see above. | |
863 | (environment . ,(process-get proc 'env)) | |
864 | ,@parameters)) | |
865 | frame) | |
866 | (if parent-id | |
867 | (push (cons 'parent-id (string-to-number parent-id)) params)) | |
868 | (add-to-list 'frame-inherited-parameters 'client) | |
869 | (setq frame (make-frame-on-display display params)) | |
870 | (server-log (format "%s created" frame) proc) | |
871 | (select-frame frame) | |
872 | (process-put proc 'frame frame) | |
873 | (process-put proc 'terminal (frame-terminal frame)) | |
874 | frame)) | |
875 | ||
876 | (t | |
877 | (server-log "Window system unsupported" proc) | |
878 | (server-send-string proc "-window-system-unsupported \n") | |
879 | nil)))) | |
13ba3740 | 880 | |
13ba3740 SM |
881 | (defun server-goto-toplevel (proc) |
882 | (condition-case nil | |
883 | ;; If we're running isearch, we must abort it to allow Emacs to | |
884 | ;; display the buffer and switch to it. | |
885 | (dolist (buffer (buffer-list)) | |
886 | (with-current-buffer buffer | |
887 | (when (bound-and-true-p isearch-mode) | |
888 | (isearch-cancel)))) | |
889 | ;; Signaled by isearch-cancel. | |
890 | (quit (message nil))) | |
891 | (when (> (recursion-depth) 0) | |
892 | ;; We're inside a minibuffer already, so if the emacs-client is trying | |
893 | ;; to open a frame on a new display, we might end up with an unusable | |
894 | ;; frame because input from that display will be blocked (until exiting | |
895 | ;; the minibuffer). Better exit this minibuffer right away. | |
896 | ;; Similarly with recursive-edits such as the splash screen. | |
94d11cb5 | 897 | (run-with-timer 0 nil (lambda () (server-execute-continuation proc))) |
13ba3740 SM |
898 | (top-level))) |
899 | ||
900 | ;; We use various special properties on process objects: | |
901 | ;; - `env' stores the info about the environment of the emacsclient process. | |
902 | ;; - `continuation' is a no-arg function that we need to execute. It contains | |
903 | ;; commands we wanted to execute in some earlier invocation of the process | |
904 | ;; filter but that we somehow were unable to process at that time | |
905 | ;; (e.g. because we first need to throw to the toplevel). | |
906 | ||
907 | (defun server-execute-continuation (proc) | |
908 | (let ((continuation (process-get proc 'continuation))) | |
909 | (process-put proc 'continuation nil) | |
910 | (if continuation (ignore-errors (funcall continuation))))) | |
911 | ||
a464a6c7 | 912 | (cl-defun server-process-filter (proc string) |
33186f32 | 913 | "Process a request from the server to edit some files. |
6afdd335 | 914 | PROC is the server process. STRING consists of a sequence of |
909049cb JB |
915 | commands prefixed by a dash. Some commands have arguments; |
916 | these are &-quoted and need to be decoded by `server-unquote-arg'. | |
917 | The filter parses and executes these commands. | |
6afdd335 KL |
918 | |
919 | To illustrate the protocol, here is an example command that | |
920 | emacsclient sends to create a new X frame (note that the whole | |
921 | sequence is sent on a single line): | |
922 | ||
909049cb JB |
923 | -env HOME=/home/lorentey |
924 | -env DISPLAY=:0.0 | |
6afdd335 KL |
925 | ... lots of other -env commands |
926 | -display :0.0 | |
927 | -window-system | |
928 | ||
6afdd335 KL |
929 | The following commands are accepted by the server: |
930 | ||
974b73e8 KL |
931 | `-auth AUTH-STRING' |
932 | Authenticate the client using the secret authentication string | |
a38daa0a | 933 | AUTH-STRING. |
974b73e8 | 934 | |
59e085e0 | 935 | `-env NAME=VALUE' |
6afdd335 KL |
936 | An environment variable on the client side. |
937 | ||
2828d5f9 KL |
938 | `-dir DIRNAME' |
939 | The current working directory of the client process. | |
940 | ||
92071250 KL |
941 | `-current-frame' |
942 | Forbid the creation of new frames. | |
943 | ||
18a4ce5e AR |
944 | `-frame-parameters ALIST' |
945 | Set the parameters of the created frame. | |
946 | ||
6afdd335 KL |
947 | `-nowait' |
948 | Request that the next frame created should not be | |
949 | associated with this client. | |
950 | ||
951 | `-display DISPLAY' | |
952 | Set the display name to open X frames on. | |
953 | ||
954 | `-position LINE[:COLUMN]' | |
955 | Go to the given line and column number | |
956 | in the next file opened. | |
957 | ||
958 | `-file FILENAME' | |
959 | Load the given file in the current frame. | |
960 | ||
961 | `-eval EXPR' | |
962 | Evaluate EXPR as a Lisp expression and return the | |
963 | result in -print commands. | |
964 | ||
965 | `-window-system' | |
966 | Open a new X frame. | |
967 | ||
968 | `-tty DEVICENAME TYPE' | |
969 | Open a new tty frame at the client. | |
970 | ||
6afdd335 KL |
971 | `-suspend' |
972 | Suspend this tty frame. The client sends this string in | |
973 | response to SIGTSTP and SIGTTOU. The server must cease all I/O | |
974 | on this tty until it gets a -resume command. | |
975 | ||
2828d5f9 | 976 | `-resume' |
c48254fb | 977 | Resume this tty frame. The client sends this string when it |
2828d5f9 KL |
978 | gets the SIGCONT signal and it is the foreground process on its |
979 | controlling tty. | |
980 | ||
6afdd335 | 981 | `-ignore COMMENT' |
909049cb JB |
982 | Do nothing, but put the comment in the server log. |
983 | Useful for debugging. | |
6afdd335 KL |
984 | |
985 | ||
986 | The following commands are accepted by the client: | |
987 | ||
6afdd335 KL |
988 | `-emacs-pid PID' |
989 | Describes the process id of the Emacs process; | |
990 | used to forward window change signals to it. | |
991 | ||
992 | `-window-system-unsupported' | |
c48254fb JB |
993 | Signals that the server does not support creating X frames; |
994 | the client must try again with a tty frame. | |
6afdd335 KL |
995 | |
996 | `-print STRING' | |
997 | Print STRING on stdout. Used to send values | |
998 | returned by -eval. | |
999 | ||
e29ab36b AS |
1000 | `-print-nonl STRING' |
1001 | Print STRING on stdout. Used to continue a | |
1002 | preceding -print command that would be too big to send | |
1003 | in a single message. | |
1004 | ||
6afdd335 | 1005 | `-error DESCRIPTION' |
2b7ba565 | 1006 | Signal an error and delete process PROC. |
6afdd335 KL |
1007 | |
1008 | `-suspend' | |
c48254fb JB |
1009 | Suspend this terminal, i.e., stop the client process. |
1010 | Sent when the user presses C-z." | |
6afdd335 | 1011 | (server-log (concat "Received " string) proc) |
337e3c70 JB |
1012 | ;; First things first: let's check the authentication |
1013 | (unless (process-get proc :authenticated) | |
38b9f0f3 | 1014 | (if (and (string-match "-auth \\([!-~]+\\)\n?" string) |
974b73e8 KL |
1015 | (equal (match-string 1 string) (process-get proc :auth-key))) |
1016 | (progn | |
1017 | (setq string (substring string (match-end 0))) | |
1018 | (process-put proc :authenticated t) | |
1019 | (server-log "Authentication successful" proc)) | |
337e3c70 | 1020 | (server-log "Authentication failed" proc) |
974b73e8 KL |
1021 | (server-send-string |
1022 | proc (concat "-error " (server-quote-arg "Authentication failed"))) | |
2a847524 CY |
1023 | ;; Before calling `delete-process', give emacsclient time to |
1024 | ;; receive the error string and shut down on its own. | |
1025 | (sit-for 1) | |
337e3c70 | 1026 | (delete-process proc) |
a464a6c7 SM |
1027 | ;; We return immediately. |
1028 | (cl-return-from server-process-filter))) | |
3f7ef08e SM |
1029 | (let ((prev (process-get proc 'previous-string))) |
1030 | (when prev | |
1031 | (setq string (concat prev string)) | |
1032 | (process-put proc 'previous-string nil))) | |
a9298135 | 1033 | (condition-case err |
0b0d3e0b | 1034 | (progn |
9002956f | 1035 | (server-add-client proc) |
968ef9b4 JB |
1036 | ;; Send our pid |
1037 | (server-send-string proc (concat "-emacs-pid " | |
1038 | (number-to-string (emacs-pid)) "\n")) | |
13ba3740 SM |
1039 | (if (not (string-match "\n" string)) |
1040 | ;; Save for later any partial line that remains. | |
1041 | (when (> (length string) 0) | |
1042 | (process-put proc 'previous-string string)) | |
c48254fb | 1043 | |
13ba3740 SM |
1044 | ;; In earlier versions of server.el (where we used an `emacsserver' |
1045 | ;; process), there could be multiple lines. Nowadays this is not | |
1046 | ;; supported any more. | |
a464a6c7 | 1047 | (cl-assert (eq (match-end 0) (length string))) |
0b0d3e0b | 1048 | (let ((request (substring string 0 (match-beginning 0))) |
597e2240 | 1049 | (coding-system (and (default-value 'enable-multibyte-characters) |
0b0d3e0b KL |
1050 | (or file-name-coding-system |
1051 | default-file-name-coding-system))) | |
0191e222 CY |
1052 | nowait ; t if emacsclient does not want to wait for us. |
1053 | frame ; Frame opened for the client (if any). | |
1054 | display ; Open frame on this display. | |
1055 | parent-id ; Window ID for XEmbed | |
1056 | dontkill ; t if client should not be killed. | |
650d0dbc | 1057 | commands |
2828d5f9 | 1058 | dir |
cd9c54eb | 1059 | use-current-frame |
18a4ce5e | 1060 | frame-parameters ;parameters for newly created frame |
0191e222 CY |
1061 | tty-name ; nil, `window-system', or the tty name. |
1062 | tty-type ; string. | |
650d0dbc CY |
1063 | files |
1064 | filepos | |
59003be9 | 1065 | args-left) |
0b0d3e0b KL |
1066 | ;; Remove this line from STRING. |
1067 | (setq string (substring string (match-end 0))) | |
59003be9 | 1068 | (setq args-left |
bfb74e75 | 1069 | (mapcar 'server-unquote-arg (split-string request " " t))) |
59003be9 SM |
1070 | (while args-left |
1071 | (pcase (pop args-left) | |
1072 | ;; -version CLIENT-VERSION: obsolete at birth. | |
1073 | (`"-version" (pop args-left)) | |
1074 | ||
1075 | ;; -nowait: Emacsclient won't wait for a result. | |
1076 | (`"-nowait" (setq nowait t)) | |
1077 | ||
1078 | ;; -current-frame: Don't create frames. | |
1079 | (`"-current-frame" (setq use-current-frame t)) | |
1080 | ||
18a4ce5e AR |
1081 | ;; -frame-parameters: Set frame parameters |
1082 | (`"-frame-parameters" | |
1083 | (let ((alist (pop args-left))) | |
1084 | (if coding-system | |
1085 | (setq alist (decode-coding-string alist coding-system))) | |
1086 | (setq frame-parameters (car (read-from-string alist))))) | |
1087 | ||
59003be9 SM |
1088 | ;; -display DISPLAY: |
1089 | ;; Open X frames on the given display instead of the default. | |
1090 | (`"-display" | |
1091 | (setq display (pop args-left)) | |
1092 | (if (zerop (length display)) (setq display nil))) | |
1093 | ||
1094 | ;; -parent-id ID: | |
1095 | ;; Open X frame within window ID, via XEmbed. | |
1096 | (`"-parent-id" | |
1097 | (setq parent-id (pop args-left)) | |
1098 | (if (zerop (length parent-id)) (setq parent-id nil))) | |
1099 | ||
1100 | ;; -window-system: Open a new X frame. | |
1101 | (`"-window-system" | |
2d0e8e61 CY |
1102 | (if (fboundp 'x-create-frame) |
1103 | (setq dontkill t | |
1104 | tty-name 'window-system))) | |
59003be9 SM |
1105 | |
1106 | ;; -resume: Resume a suspended tty frame. | |
1107 | (`"-resume" | |
c530e1c2 | 1108 | (let ((terminal (process-get proc 'terminal))) |
59003be9 SM |
1109 | (setq dontkill t) |
1110 | (push (lambda () | |
1111 | (when (eq (terminal-live-p terminal) t) | |
1112 | (resume-tty terminal))) | |
1113 | commands))) | |
1114 | ||
1115 | ;; -suspend: Suspend the client's frame. (In case we | |
1116 | ;; get out of sync, and a C-z sends a SIGTSTP to | |
1117 | ;; emacsclient.) | |
1118 | (`"-suspend" | |
c530e1c2 | 1119 | (let ((terminal (process-get proc 'terminal))) |
59003be9 SM |
1120 | (setq dontkill t) |
1121 | (push (lambda () | |
1122 | (when (eq (terminal-live-p terminal) t) | |
1123 | (suspend-tty terminal))) | |
1124 | commands))) | |
1125 | ||
1126 | ;; -ignore COMMENT: Noop; useful for debugging emacsclient. | |
1127 | ;; (The given comment appears in the server log.) | |
1128 | (`"-ignore" | |
1129 | (setq dontkill t) | |
1130 | (pop args-left)) | |
1131 | ||
2d0e8e61 CY |
1132 | ;; -tty DEVICE-NAME TYPE: Open a new tty frame. |
1133 | ;; (But if we see -window-system later, use that.) | |
59003be9 SM |
1134 | (`"-tty" |
1135 | (setq tty-name (pop args-left) | |
1136 | tty-type (pop args-left) | |
1137 | dontkill (or dontkill | |
520fca41 | 1138 | (not use-current-frame))) |
5e0944c6 DC |
1139 | ;; On Windows, emacsclient always asks for a tty |
1140 | ;; frame. If running a GUI server, force the frame | |
1141 | ;; type to GUI. (Cygwin is perfectly happy with | |
1142 | ;; multi-tty support, so don't override the user's | |
1143 | ;; choice there.) | |
1144 | (when (and (eq system-type 'windows-nt) | |
1145 | (eq window-system 'w32)) | |
520fca41 | 1146 | (push "-window-system" args-left))) |
59003be9 SM |
1147 | |
1148 | ;; -position LINE[:COLUMN]: Set point to the given | |
1149 | ;; position in the next file. | |
1150 | (`"-position" | |
1151 | (if (not (string-match "\\+\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?" | |
1152 | (car args-left))) | |
1153 | (error "Invalid -position command in client args")) | |
1154 | (let ((arg (pop args-left))) | |
1155 | (setq filepos | |
1156 | (cons (string-to-number (match-string 1 arg)) | |
1157 | (string-to-number (or (match-string 2 arg) | |
1158 | "")))))) | |
1159 | ||
1160 | ;; -file FILENAME: Load the given file. | |
1161 | (`"-file" | |
1162 | (let ((file (pop args-left))) | |
1163 | (if coding-system | |
1164 | (setq file (decode-coding-string file coding-system))) | |
1165 | (setq file (expand-file-name file dir)) | |
1166 | (push (cons file filepos) files) | |
1167 | (server-log (format "New file: %s %s" | |
1168 | file (or filepos "")) proc)) | |
1169 | (setq filepos nil)) | |
1170 | ||
1171 | ;; -eval EXPR: Evaluate a Lisp expression. | |
1172 | (`"-eval" | |
1173 | (if use-current-frame | |
1174 | (setq use-current-frame 'always)) | |
c530e1c2 | 1175 | (let ((expr (pop args-left))) |
59003be9 SM |
1176 | (if coding-system |
1177 | (setq expr (decode-coding-string expr coding-system))) | |
1178 | (push (lambda () (server-eval-and-print expr proc)) | |
1179 | commands) | |
1180 | (setq filepos nil))) | |
1181 | ||
1182 | ;; -env NAME=VALUE: An environment variable. | |
1183 | (`"-env" | |
1184 | (let ((var (pop args-left))) | |
1185 | ;; XXX Variables should be encoded as in getenv/setenv. | |
1186 | (process-put proc 'env | |
1187 | (cons var (process-get proc 'env))))) | |
1188 | ||
1189 | ;; -dir DIRNAME: The cwd of the emacsclient process. | |
1190 | (`"-dir" | |
1191 | (setq dir (pop args-left)) | |
1192 | (if coding-system | |
1193 | (setq dir (decode-coding-string dir coding-system))) | |
8c4f2952 JD |
1194 | (setq dir (command-line-normalize-file-name dir)) |
1195 | (process-put proc 'server-client-directory dir)) | |
59003be9 SM |
1196 | |
1197 | ;; Unknown command. | |
1198 | (arg (error "Unknown command: %s" arg)))) | |
c48254fb | 1199 | |
9a864fa2 CY |
1200 | ;; If both -no-wait and -tty are given with file or sexp |
1201 | ;; arguments, use an existing frame. | |
1202 | (and nowait | |
1203 | (not (eq tty-name 'window-system)) | |
1204 | (or files commands) | |
1205 | (setq use-current-frame t)) | |
1206 | ||
cd9c54eb CY |
1207 | (setq frame |
1208 | (cond | |
1209 | ((and use-current-frame | |
1210 | (or (eq use-current-frame 'always) | |
1211 | ;; We can't use the Emacs daemon's | |
1212 | ;; terminal frame. | |
d9bf544c | 1213 | (not (and (daemonp) |
97e121cc | 1214 | (null (cdr (frame-list))) |
cd9c54eb CY |
1215 | (eq (selected-frame) |
1216 | terminal-frame))))) | |
d9bf544c | 1217 | (setq tty-name nil tty-type nil) |
cd9c54eb CY |
1218 | (if display (server-select-display display))) |
1219 | ((eq tty-name 'window-system) | |
0191e222 | 1220 | (server-create-window-system-frame display nowait proc |
18a4ce5e AR |
1221 | parent-id |
1222 | frame-parameters)) | |
650d0dbc CY |
1223 | ;; When resuming on a tty, tty-name is nil. |
1224 | (tty-name | |
1225 | (server-create-tty-frame tty-name tty-type proc)))) | |
13ba3740 | 1226 | |
28cbade4 SM |
1227 | (process-put |
1228 | proc 'continuation | |
94d11cb5 IK |
1229 | (lambda () |
1230 | (with-current-buffer (get-buffer-create server-buffer) | |
1231 | ;; Use the same cwd as the emacsclient, if possible, so | |
1232 | ;; relative file names work correctly, even in `eval'. | |
1233 | (let ((default-directory | |
1234 | (if (and dir (file-directory-p dir)) | |
1235 | dir default-directory))) | |
1236 | (server-execute proc files nowait commands | |
1237 | dontkill frame tty-name))))) | |
13ba3740 SM |
1238 | |
1239 | (when (or frame files) | |
1240 | (server-goto-toplevel proc)) | |
1241 | ||
1242 | (server-execute-continuation proc)))) | |
a9298135 | 1243 | ;; condition-case |
13ba3740 SM |
1244 | (error (server-return-error proc err)))) |
1245 | ||
1246 | (defun server-execute (proc files nowait commands dontkill frame tty-name) | |
7197f5de SM |
1247 | ;; This is run from timers and process-filters, i.e. "asynchronously". |
1248 | ;; But w.r.t the user, this is not really asynchronous since the timer | |
1249 | ;; is run after 0s and the process-filter is run in response to the | |
1250 | ;; user running `emacsclient'. So it is OK to override the | |
1251 | ;; inhibit-quit flag, which is good since `commands' (as well as | |
1252 | ;; find-file-noselect via the major-mode) can run arbitrary code, | |
1253 | ;; including code that needs to wait. | |
1254 | (with-local-quit | |
1255 | (condition-case err | |
de6ff46d | 1256 | (let ((buffers (server-visit-files files proc nowait))) |
7197f5de SM |
1257 | (mapc 'funcall (nreverse commands)) |
1258 | ||
de6ff46d | 1259 | ;; If we were told only to open a new client, obey |
dc646358 CK |
1260 | ;; `initial-buffer-choice' if it specifies a file |
1261 | ;; or a function. | |
1262 | (unless (or files commands) | |
1263 | (let ((buf | |
1264 | (cond ((stringp initial-buffer-choice) | |
1265 | (find-file-noselect initial-buffer-choice)) | |
1266 | ((functionp initial-buffer-choice) | |
1267 | (funcall initial-buffer-choice))))) | |
1268 | (switch-to-buffer | |
1269 | (if (buffer-live-p buf) buf (get-buffer-create "*scratch*")) | |
1270 | 'norecord))) | |
de6ff46d | 1271 | |
7197f5de SM |
1272 | ;; Delete the client if necessary. |
1273 | (cond | |
1274 | (nowait | |
1275 | ;; Client requested nowait; return immediately. | |
1276 | (server-log "Close nowait client" proc) | |
1277 | (server-delete-client proc)) | |
1278 | ((and (not dontkill) (null buffers)) | |
1279 | ;; This client is empty; get rid of it immediately. | |
1280 | (server-log "Close empty client" proc) | |
1281 | (server-delete-client proc))) | |
1282 | (cond | |
1283 | ((or isearch-mode (minibufferp)) | |
1284 | nil) | |
1285 | ((and frame (null buffers)) | |
13ba3740 | 1286 | (message "%s" (substitute-command-keys |
7197f5de SM |
1287 | "When done with this frame, type \\[delete-frame]"))) |
1288 | ((not (null buffers)) | |
1289 | (server-switch-buffer (car buffers) nil (cdr (car files))) | |
1290 | (run-hooks 'server-switch-hook) | |
1291 | (unless nowait | |
1292 | (message "%s" (substitute-command-keys | |
1293 | "When done with a buffer, type \\[server-edit]"))))) | |
1294 | (when (and frame (null tty-name)) | |
1295 | (server-unselect-display frame))) | |
b768cdcd JB |
1296 | ((quit error) |
1297 | (when (eq (car err) 'quit) | |
1298 | (message "Quit emacsclient request")) | |
1299 | (server-return-error proc err))))) | |
13ba3740 SM |
1300 | |
1301 | (defun server-return-error (proc err) | |
1302 | (ignore-errors | |
1303 | (server-send-string | |
1304 | proc (concat "-error " (server-quote-arg | |
1305 | (error-message-string err)))) | |
1306 | (server-log (error-message-string err) proc) | |
2a847524 CY |
1307 | ;; Before calling `delete-process', give emacsclient time to |
1308 | ;; receive the error string and shut down on its own. | |
1309 | (sit-for 5) | |
13ba3740 | 1310 | (delete-process proc))) |
9ae0f972 | 1311 | |
656d4706 SM |
1312 | (defun server-goto-line-column (line-col) |
1313 | "Move point to the position indicated in LINE-COL. | |
1314 | LINE-COL should be a pair (LINE . COL)." | |
1315 | (when line-col | |
e6ce8c42 GM |
1316 | (goto-char (point-min)) |
1317 | (forward-line (1- (car line-col))) | |
656d4706 SM |
1318 | (let ((column-number (cdr line-col))) |
1319 | (when (> column-number 0) | |
1320 | (move-to-column (1- column-number)))))) | |
6b98185f | 1321 | |
448f754f | 1322 | (defun server-visit-files (files proc &optional nowait) |
c5b0a355 | 1323 | "Find FILES and return a list of buffers created. |
656d4706 SM |
1324 | FILES is an alist whose elements are (FILENAME . FILEPOS) |
1325 | where FILEPOS can be nil or a pair (LINENUMBER . COLUMNNUMBER). | |
448f754f | 1326 | PROC is the client that requested this operation. |
dfa35e6b RS |
1327 | NOWAIT non-nil means this client is not waiting for the results, |
1328 | so don't mark these buffers specially, just visit them normally." | |
e82e73c2 | 1329 | ;; Bind last-nonmenu-event to force use of keyboard, not mouse, for queries. |
44a56b29 | 1330 | (let ((last-nonmenu-event t) client-record) |
3a0ce849 RS |
1331 | ;; Restore the current buffer afterward, but not using save-excursion, |
1332 | ;; because we don't want to save point in this buffer | |
1333 | ;; if it happens to be one of those specified by the server. | |
44a56b29 SM |
1334 | (save-current-buffer |
1335 | (dolist (file files) | |
1336 | ;; If there is an existing buffer modified or the file is | |
1337 | ;; modified, revert it. If there is an existing buffer with | |
1338 | ;; deleted file, offer to write it. | |
cd7320d4 | 1339 | (let* ((minibuffer-auto-raise (or server-raise-frame |
974b73e8 | 1340 | minibuffer-auto-raise)) |
c2d0d432 | 1341 | (filen (car file)) |
44a56b29 | 1342 | (obuf (get-file-buffer filen))) |
c398358a | 1343 | (add-to-history 'file-name-history filen) |
656d4706 | 1344 | (if (null obuf) |
38dbc4d8 | 1345 | (progn |
56e6cc31 | 1346 | (run-hooks 'pre-command-hook) |
38dbc4d8 | 1347 | (set-buffer (find-file-noselect filen))) |
656d4706 | 1348 | (set-buffer obuf) |
38dbc4d8 DR |
1349 | ;; separately for each file, in sync with post-command hooks, |
1350 | ;; with the new buffer current: | |
56e6cc31 | 1351 | (run-hooks 'pre-command-hook) |
656d4706 SM |
1352 | (cond ((file-exists-p filen) |
1353 | (when (not (verify-visited-file-modtime obuf)) | |
1354 | (revert-buffer t nil))) | |
1355 | (t | |
1356 | (when (y-or-n-p | |
1357 | (concat "File no longer exists: " filen | |
1358 | ", write buffer to file? ")) | |
1359 | (write-file filen)))) | |
1360 | (unless server-buffer-clients | |
1361 | (setq server-existing-buffer t))) | |
1362 | (server-goto-line-column (cdr file)) | |
38dbc4d8 DR |
1363 | (run-hooks 'server-visit-hook) |
1364 | ;; hooks may be specific to current buffer: | |
56e6cc31 | 1365 | (run-hooks 'post-command-hook)) |
44a56b29 SM |
1366 | (unless nowait |
1367 | ;; When the buffer is killed, inform the clients. | |
1368 | (add-hook 'kill-buffer-hook 'server-kill-buffer nil t) | |
448f754f | 1369 | (push proc server-buffer-clients)) |
c5b0a355 KL |
1370 | (push (current-buffer) client-record))) |
1371 | (unless nowait | |
448f754f SM |
1372 | (process-put proc 'buffers |
1373 | (nconc (process-get proc 'buffers) client-record))) | |
c5b0a355 | 1374 | client-record)) |
d032d5e7 SM |
1375 | |
1376 | (defvar server-kill-buffer-running nil | |
1377 | "Non-nil while `server-kill-buffer' or `server-buffer-done' is running.") | |
1378 | ||
b392bac9 | 1379 | (defun server-buffer-done (buffer &optional for-killing) |
9ae0f972 | 1380 | "Mark BUFFER as \"done\" for its client(s). |
9184aafb RS |
1381 | This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED). |
1382 | NEXT-BUFFER is another server buffer, as a suggestion for what to select next, | |
8b3e840e SM |
1383 | or nil. KILLED is t if we killed BUFFER (typically, because it was visiting |
1384 | a temp file). | |
1385 | FOR-KILLING if non-nil indicates that we are called from `kill-buffer'." | |
0c851d78 | 1386 | (let ((next-buffer nil) |
9002956f | 1387 | (killed nil)) |
448f754f SM |
1388 | (dolist (proc server-clients) |
1389 | (let ((buffers (process-get proc 'buffers))) | |
8b3e840e | 1390 | (or next-buffer |
9002956f KL |
1391 | (setq next-buffer (nth 1 (memq buffer buffers)))) |
1392 | (when buffers ; Ignore bufferless clients. | |
1393 | (setq buffers (delq buffer buffers)) | |
448f754f | 1394 | ;; Delete all dead buffers from PROC. |
9002956f KL |
1395 | (dolist (b buffers) |
1396 | (and (bufferp b) | |
1397 | (not (buffer-live-p b)) | |
1398 | (setq buffers (delq b buffers)))) | |
448f754f | 1399 | (process-put proc 'buffers buffers) |
9002956f KL |
1400 | ;; If client now has no pending buffers, |
1401 | ;; tell it that it is done, and forget it entirely. | |
1402 | (unless buffers | |
448f754f | 1403 | (server-log "Close" proc) |
737e5c83 CY |
1404 | (if for-killing |
1405 | ;; `server-delete-client' might delete the client's | |
1406 | ;; frames, which might change the current buffer. We | |
1407 | ;; don't want that (bug#640). | |
1408 | (save-current-buffer | |
1409 | (server-delete-client proc)) | |
1410 | (server-delete-client proc)))))) | |
337e3c70 JB |
1411 | (when (and (bufferp buffer) (buffer-name buffer)) |
1412 | ;; We may or may not kill this buffer; | |
1413 | ;; if we do, do not call server-buffer-done recursively | |
1414 | ;; from kill-buffer-hook. | |
1415 | (let ((server-kill-buffer-running t)) | |
1416 | (with-current-buffer buffer | |
1417 | (setq server-buffer-clients nil) | |
1418 | (run-hooks 'server-done-hook)) | |
1419 | ;; Notice whether server-done-hook killed the buffer. | |
1420 | (if (null (buffer-name buffer)) | |
1421 | (setq killed t) | |
1422 | ;; Don't bother killing or burying the buffer | |
1423 | ;; when we are called from kill-buffer. | |
1424 | (unless for-killing | |
1425 | (when (and (not killed) | |
1426 | server-kill-new-buffers | |
1427 | (with-current-buffer buffer | |
1428 | (not server-existing-buffer))) | |
599f9a5c | 1429 | (setq killed t) |
337e3c70 | 1430 | (bury-buffer buffer) |
2d25aa5a CY |
1431 | ;; Prevent kill-buffer from prompting (Bug#3696). |
1432 | (with-current-buffer buffer | |
1433 | (set-buffer-modified-p nil)) | |
337e3c70 JB |
1434 | (kill-buffer buffer)) |
1435 | (unless killed | |
1436 | (if (server-temp-file-p buffer) | |
1437 | (progn | |
2d25aa5a CY |
1438 | (with-current-buffer buffer |
1439 | (set-buffer-modified-p nil)) | |
337e3c70 JB |
1440 | (kill-buffer buffer) |
1441 | (setq killed t)) | |
1442 | (bury-buffer buffer))))))) | |
9184aafb | 1443 | (list next-buffer killed))) |
9ae0f972 | 1444 | |
408784a7 | 1445 | (defun server-temp-file-p (&optional buffer) |
9ae0f972 | 1446 | "Return non-nil if BUFFER contains a file considered temporary. |
1447 | These are files whose names suggest they are repeatedly | |
1448 | reused to pass information to another program. | |
1449 | ||
1450 | The variable `server-temp-file-regexp' controls which filenames | |
1451 | are considered temporary." | |
1452 | (and (buffer-file-name buffer) | |
a77ad240 | 1453 | (string-match-p server-temp-file-regexp (buffer-file-name buffer)))) |
9ae0f972 | 1454 | |
1455 | (defun server-done () | |
cc9875f9 | 1456 | "Offer to save current buffer, mark it as \"done\" for clients. |
ed9ae328 RS |
1457 | This kills or buries the buffer, then returns a list |
1458 | of the form (NEXT-BUFFER KILLED). NEXT-BUFFER is another server buffer, | |
1459 | as a suggestion for what to select next, or nil. | |
1460 | KILLED is t if we killed BUFFER, which happens if it was created | |
1461 | specifically for the clients and did not exist before their request for it." | |
408784a7 SM |
1462 | (when server-buffer-clients |
1463 | (if (server-temp-file-p) | |
1464 | ;; For a temp file, save, and do make a non-numeric backup | |
1465 | ;; (unless make-backup-files is nil). | |
1466 | (let ((version-control nil) | |
1467 | (buffer-backed-up nil)) | |
1468 | (save-buffer)) | |
337e3c70 JB |
1469 | (when (and (buffer-modified-p) |
1470 | buffer-file-name | |
1471 | (y-or-n-p (concat "Save file " buffer-file-name "? "))) | |
1472 | (save-buffer))) | |
408784a7 | 1473 | (server-buffer-done (current-buffer)))) |
faf931a8 | 1474 | |
71207de2 RS |
1475 | ;; Ask before killing a server buffer. |
1476 | ;; It was suggested to release its client instead, | |
1477 | ;; but I think that is dangerous--the client would proceed | |
1478 | ;; using whatever is on disk in that file. -- rms. | |
03d78665 | 1479 | (defun server-kill-buffer-query-function () |
9002956f | 1480 | "Ask before killing a server buffer." |
03d78665 | 1481 | (or (not server-buffer-clients) |
114a8b8c | 1482 | (let ((res t)) |
2403c841 | 1483 | (dolist (proc server-buffer-clients) |
448f754f SM |
1484 | (when (and (memq proc server-clients) |
1485 | (eq (process-status proc) 'open)) | |
2403c841 SM |
1486 | (setq res nil))) |
1487 | res) | |
03d78665 RS |
1488 | (yes-or-no-p (format "Buffer `%s' still has clients; kill it? " |
1489 | (buffer-name (current-buffer)))))) | |
1490 | ||
03d78665 | 1491 | (defun server-kill-emacs-query-function () |
c48254fb | 1492 | "Ask before exiting Emacs if it has live clients." |
9002956f KL |
1493 | (or (not server-clients) |
1494 | (let (live-client) | |
2403c841 | 1495 | (dolist (proc server-clients) |
448f754f SM |
1496 | (when (memq t (mapcar 'buffer-live-p (process-get |
1497 | proc 'buffers))) | |
2403c841 SM |
1498 | (setq live-client t))) |
1499 | live-client) | |
9002956f | 1500 | (yes-or-no-p "This Emacs session has clients; exit anyway? "))) |
b392bac9 | 1501 | |
b392bac9 | 1502 | (defun server-kill-buffer () |
6d3a46f7 KL |
1503 | "Remove the current buffer from its clients' buffer list. |
1504 | Designed to be added to `kill-buffer-hook'." | |
fb873cfc RS |
1505 | ;; Prevent infinite recursion if user has made server-done-hook |
1506 | ;; call kill-buffer. | |
1507 | (or server-kill-buffer-running | |
599f9a5c RS |
1508 | (and server-buffer-clients |
1509 | (let ((server-kill-buffer-running t)) | |
1510 | (when server-process | |
1511 | (server-buffer-done (current-buffer) t)))))) | |
9ae0f972 | 1512 | \f |
1513 | (defun server-edit (&optional arg) | |
1514 | "Switch to next server editing buffer; say \"Done\" for current buffer. | |
1515 | If a server buffer is current, it is marked \"done\" and optionally saved. | |
ed9ae328 | 1516 | The buffer is also killed if it did not exist before the clients asked for it. |
9ae0f972 | 1517 | When all of a client's buffers are marked as \"done\", the client is notified. |
1518 | ||
1519 | Temporary files such as MH <draft> files are always saved and backed up, | |
991298c3 RS |
1520 | no questions asked. (The variable `make-backup-files', if nil, still |
1521 | inhibits a backup; you can set it locally in a particular buffer to | |
1522 | prevent a backup for it.) The variable `server-temp-file-regexp' controls | |
9ae0f972 | 1523 | which filenames are considered temporary. |
1524 | ||
64f51134 | 1525 | If invoked with a prefix argument, or if there is no server process running, |
9ae0f972 | 1526 | starts server process and that is all. Invoked by \\[server-edit]." |
9ae0f972 | 1527 | (interactive "P") |
6b519504 | 1528 | (cond |
94d11cb5 IK |
1529 | ((or arg |
1530 | (not server-process) | |
1531 | (memq (process-status server-process) '(signal exit))) | |
1532 | (server-mode 1)) | |
1533 | (server-clients (apply 'server-switch-buffer (server-done))) | |
1534 | (t (message "No server editing buffers exist")))) | |
9ae0f972 | 1535 | |
ee0aed46 | 1536 | (defun server-switch-buffer (&optional next-buffer killed-one filepos) |
9ae0f972 | 1537 | "Switch to another buffer, preferably one that has a client. |
6d3a46f7 KL |
1538 | Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it. |
1539 | ||
1540 | KILLED-ONE is t in a recursive call if we have already killed one | |
1541 | temp-file server buffer. This means we should avoid the final | |
1542 | \"switch to some other buffer\" since we've already effectively | |
ee0aed46 CY |
1543 | done that. |
1544 | ||
1545 | FILEPOS specifies a new buffer position for NEXT-BUFFER, if we | |
1546 | visit NEXT-BUFFER in an existing window. If non-nil, it should | |
1547 | be a cons cell (LINENUMBER . COLUMNNUMBER)." | |
ca0c7250 | 1548 | (if (null next-buffer) |
9002956f KL |
1549 | (progn |
1550 | (let ((rest server-clients)) | |
1551 | (while (and rest (not next-buffer)) | |
448f754f | 1552 | (let ((proc (car rest))) |
a7ce6c7f AS |
1553 | ;; Only look at frameless clients, or those in the selected |
1554 | ;; frame. | |
1555 | (when (or (not (process-get proc 'frame)) | |
1556 | (eq (process-get proc 'frame) (selected-frame))) | |
448f754f | 1557 | (setq next-buffer (car (process-get proc 'buffers)))) |
9002956f KL |
1558 | (setq rest (cdr rest))))) |
1559 | (and next-buffer (server-switch-buffer next-buffer killed-one)) | |
290d5b58 | 1560 | (unless (or next-buffer killed-one (window-dedicated-p)) |
9002956f | 1561 | ;; (switch-to-buffer (other-buffer)) |
90ee5627 | 1562 | (message "No server buffers remain to edit"))) |
9002956f | 1563 | (if (not (buffer-live-p next-buffer)) |
ca0c7250 | 1564 | ;; If NEXT-BUFFER is a dead buffer, remove the server records for it |
9ae0f972 | 1565 | ;; and try the next surviving server buffer. |
ca0c7250 SM |
1566 | (apply 'server-switch-buffer (server-buffer-done next-buffer)) |
1567 | ;; OK, we know next-buffer is live, let's display and select it. | |
408784a7 SM |
1568 | (if (functionp server-window) |
1569 | (funcall server-window next-buffer) | |
1570 | (let ((win (get-buffer-window next-buffer 0))) | |
1571 | (if (and win (not server-window)) | |
ee0aed46 CY |
1572 | ;; The buffer is already displayed: just reuse the |
1573 | ;; window. If FILEPOS is non-nil, use it to replace the | |
1574 | ;; window's own value of point. | |
90caccca JB |
1575 | (progn |
1576 | (select-window win) | |
ee0aed46 CY |
1577 | (set-buffer next-buffer) |
1578 | (when filepos | |
1579 | (server-goto-line-column filepos))) | |
408784a7 | 1580 | ;; Otherwise, let's find an appropriate window. |
60b4b298 | 1581 | (cond ((window-live-p server-window) |
408784a7 SM |
1582 | (select-window server-window)) |
1583 | ((framep server-window) | |
337e3c70 JB |
1584 | (unless (frame-live-p server-window) |
1585 | (setq server-window (make-frame))) | |
408784a7 | 1586 | (select-window (frame-selected-window server-window)))) |
290d5b58 | 1587 | (when (window-minibuffer-p) |
337e3c70 | 1588 | (select-window (next-window nil 'nomini 0))) |
408784a7 | 1589 | ;; Move to a non-dedicated window, if we have one. |
290d5b58 | 1590 | (when (window-dedicated-p) |
408784a7 SM |
1591 | (select-window |
1592 | (get-window-with-predicate | |
1593 | (lambda (w) | |
1594 | (and (not (window-dedicated-p w)) | |
6ed8eeff | 1595 | (equal (frame-terminal (window-frame w)) |
12b4c0ea | 1596 | (frame-terminal)))) |
408784a7 SM |
1597 | 'nomini 'visible (selected-window)))) |
1598 | (condition-case nil | |
1599 | (switch-to-buffer next-buffer) | |
1600 | ;; After all the above, we might still have ended up with | |
1601 | ;; a minibuffer/dedicated-window (if there's no other). | |
90caccca JB |
1602 | (error (pop-to-buffer next-buffer))))))) |
1603 | (when server-raise-frame | |
290d5b58 | 1604 | (select-frame-set-input-focus (window-frame))))) |
9ae0f972 | 1605 | |
59e085e0 | 1606 | ;;;###autoload |
ac088d51 | 1607 | (defun server-save-buffers-kill-terminal (arg) |
c61a4448 | 1608 | ;; Called from save-buffers-kill-terminal in files.el. |
ac088d51 | 1609 | "Offer to save each buffer, then kill the current client. |
a77ad240 | 1610 | With ARG non-nil, silently save all file-visiting buffers, then kill. |
7540c1e0 KL |
1611 | |
1612 | If emacsclient was started with a list of filenames to edit, then | |
1613 | only these files will be asked to be saved." | |
3b734d1c | 1614 | (let ((proc (frame-parameter nil 'client))) |
ac088d51 CY |
1615 | (cond ((eq proc 'nowait) |
1616 | ;; Nowait frames have no client buffer list. | |
1617 | (if (cdr (frame-list)) | |
1618 | (progn (save-some-buffers arg) | |
1619 | (delete-frame)) | |
1620 | ;; If we're the last frame standing, kill Emacs. | |
1621 | (save-buffers-kill-emacs arg))) | |
1622 | ((processp proc) | |
1623 | (let ((buffers (process-get proc 'buffers))) | |
1624 | ;; If client is bufferless, emulate a normal Emacs exit | |
1625 | ;; and offer to save all buffers. Otherwise, offer to | |
1626 | ;; save only the buffers belonging to the client. | |
1627 | (save-some-buffers | |
1628 | arg (if buffers | |
1629 | (lambda () (memq (current-buffer) buffers)) | |
1630 | t)) | |
1631 | (server-delete-client proc))) | |
1632 | (t (error "Invalid client frame"))))) | |
b4ca0271 | 1633 | |
772c5eb7 | 1634 | (define-key ctl-x-map "#" 'server-edit) |
df4e8a11 | 1635 | |
08446d5e | 1636 | (defun server-unload-function () |
6d3a46f7 | 1637 | "Unload the server library." |
2e8457a0 | 1638 | (server-mode -1) |
a8e0c053 | 1639 | (substitute-key-definition 'server-edit nil ctl-x-map) |
08446d5e JB |
1640 | (save-current-buffer |
1641 | (dolist (buffer (buffer-list)) | |
1642 | (set-buffer buffer) | |
1643 | (remove-hook 'kill-buffer-hook 'server-kill-buffer t))) | |
1644 | ;; continue standard unloading | |
1645 | nil) | |
3bb38bc2 | 1646 | |
e793a940 | 1647 | (defun server-eval-at (server form) |
953cebf5 GM |
1648 | "Contact the Emacs server named SERVER and evaluate FORM there. |
1649 | Returns the result of the evaluation, or signals an error if it | |
1650 | cannot contact the specified server. For example: | |
1651 | \(server-eval-at \"server\" '(emacs-pid)) | |
ad0bf5b6 AS |
1652 | returns the process ID of the Emacs instance running \"server\"." |
1653 | (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir)) | |
1654 | (server-file (expand-file-name server server-dir)) | |
1655 | (coding-system-for-read 'binary) | |
1656 | (coding-system-for-write 'binary) | |
1657 | address port secret process) | |
1658 | (unless (file-exists-p server-file) | |
1659 | (error "No such server: %s" server)) | |
e793a940 | 1660 | (with-temp-buffer |
ad0bf5b6 AS |
1661 | (when server-use-tcp |
1662 | (let ((coding-system-for-read 'no-conversion)) | |
1663 | (insert-file-contents server-file) | |
1664 | (unless (looking-at "\\([0-9.]+\\):\\([0-9]+\\)") | |
1665 | (error "Invalid auth file")) | |
1666 | (setq address (match-string 1) | |
1667 | port (string-to-number (match-string 2))) | |
1668 | (forward-line 1) | |
1669 | (setq secret (buffer-substring (point) (line-end-position))) | |
1670 | (erase-buffer))) | |
1671 | (unless (setq process (make-network-process | |
1672 | :name "eval-at" | |
1673 | :buffer (current-buffer) | |
1674 | :host address | |
1675 | :service (if server-use-tcp port server-file) | |
1676 | :family (if server-use-tcp 'ipv4 'local) | |
1677 | :noquery t)) | |
1678 | (error "Unable to contact the server")) | |
1679 | (if server-use-tcp | |
1680 | (process-send-string process (concat "-auth " secret "\n"))) | |
1681 | (process-send-string process | |
1682 | (concat "-eval " | |
1683 | (server-quote-arg (format "%S" form)) | |
1684 | "\n")) | |
e793a940 LMI |
1685 | (while (memq (process-status process) '(open run)) |
1686 | (accept-process-output process 0 10)) | |
1687 | (goto-char (point-min)) | |
1688 | ;; If the result is nil, there's nothing in the buffer. If the | |
1689 | ;; result is non-nil, it's after "-print ". | |
e29ab36b AS |
1690 | (let ((answer "")) |
1691 | (while (re-search-forward "\n-print\\(-nonl\\)? " nil t) | |
1692 | (setq answer | |
1693 | (concat answer | |
1694 | (buffer-substring (point) | |
1695 | (progn (skip-chars-forward "^\n") | |
1696 | (point)))))) | |
1697 | (if (not (equal answer "")) | |
ad0bf5b6 AS |
1698 | (read (decode-coding-string (server-unquote-arg answer) |
1699 | 'emacs-internal))))))) | |
e793a940 | 1700 | |
16c15321 RM |
1701 | \f |
1702 | (provide 'server) | |
c88ab9ce ER |
1703 | |
1704 | ;;; server.el ends here |