| 1 | ;;; server.el --- Lisp code for GNU Emacs running as server process -*- lexical-binding: t -*- |
| 2 | |
| 3 | ;; Copyright (C) 1986-1987, 1992, 1994-2014 Free Software Foundation, |
| 4 | ;; Inc. |
| 5 | |
| 6 | ;; Author: William Sommerfeld <wesommer@athena.mit.edu> |
| 7 | ;; Maintainer: emacs-devel@gnu.org |
| 8 | ;; Keywords: processes |
| 9 | |
| 10 | ;; Changes by peck@sun.com and by rms. |
| 11 | ;; Overhaul by Karoly Lorentey <lorentey@elte.hu> for multi-tty support. |
| 12 | |
| 13 | ;; This file is part of GNU Emacs. |
| 14 | |
| 15 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 16 | ;; it under the terms of the GNU General Public License as published by |
| 17 | ;; the Free Software Foundation, either version 3 of the License, or |
| 18 | ;; (at your option) any later version. |
| 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 |
| 26 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | ;;; Commentary: |
| 29 | |
| 30 | ;; This Lisp code is run in Emacs when it is to operate as |
| 31 | ;; a server for other processes. |
| 32 | |
| 33 | ;; Load this library and do M-x server-edit to enable Emacs as a server. |
| 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)) |
| 37 | |
| 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. |
| 42 | |
| 43 | ;; Note that any number of clients may dispatch files to Emacs to be edited. |
| 44 | |
| 45 | ;; When you finish editing a Server buffer, again call server-edit |
| 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 |
| 48 | ;; and exited with server-edit, the client "editor" will return |
| 49 | ;; to the program that invoked it. |
| 50 | |
| 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: |
| 55 | |
| 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. |
| 58 | |
| 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. |
| 61 | |
| 62 | ;; 3. When the program that wants to use "the editor" is running |
| 63 | ;; as a subprocess of Emacs. |
| 64 | |
| 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. |
| 70 | |
| 71 | ;; The buffer local variable "server-buffer-clients" lists |
| 72 | ;; the clients who are waiting for this buffer to be edited. |
| 73 | ;; The global variable "server-clients" lists all the waiting clients, |
| 74 | ;; and which files are yet to be edited for each. |
| 75 | |
| 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 | |
| 83 | ;;; Code: |
| 84 | |
| 85 | (eval-when-compile (require 'cl-lib)) |
| 86 | |
| 87 | (defgroup server nil |
| 88 | "Emacs running as a server process." |
| 89 | :group 'external) |
| 90 | |
| 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 | (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. |
| 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 doing! |
| 108 | On unsecured networks, accepting remote connections is very dangerous, |
| 109 | because server-client communication (including session authentication) |
| 110 | is not encrypted." |
| 111 | :group 'server |
| 112 | :type '(choice |
| 113 | (string :tag "Name or IP address") |
| 114 | (const :tag "Local" nil)) |
| 115 | :version "22.1") |
| 116 | ;;;###autoload |
| 117 | (put 'server-host 'risky-local-variable t) |
| 118 | |
| 119 | (defcustom server-port nil |
| 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." |
| 124 | :group 'server |
| 125 | :type '(choice |
| 126 | (string :tag "Port number") |
| 127 | (const :tag "Random" nil)) |
| 128 | :version "24.1") |
| 129 | ;;;###autoload |
| 130 | (put 'server-port 'risky-local-variable t) |
| 131 | |
| 132 | (defcustom server-auth-dir (locate-user-emacs-file "server/") |
| 133 | "Directory for server authentication files. |
| 134 | We only use this if `server-use-tcp' is non-nil. |
| 135 | Otherwise we use `server-socket-dir'. |
| 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." |
| 141 | :group 'server |
| 142 | :type 'directory |
| 143 | :version "22.1") |
| 144 | ;;;###autoload |
| 145 | (put 'server-auth-dir 'risky-local-variable t) |
| 146 | |
| 147 | (defcustom server-auth-key nil |
| 148 | "Server authentication key. |
| 149 | This is only used if `server-use-tcp' is non-nil. |
| 150 | |
| 151 | Normally, the authentication key is randomly generated when the |
| 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). |
| 155 | |
| 156 | In some situations however, it can be difficult to share randomly |
| 157 | generated passwords with remote hosts (e.g., no shared directory), |
| 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). |
| 161 | |
| 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. |
| 165 | |
| 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." |
| 169 | :group 'server |
| 170 | :type '(choice |
| 171 | (const :tag "Random" nil) |
| 172 | (string :tag "Password")) |
| 173 | :version "24.3") |
| 174 | |
| 175 | (defcustom server-raise-frame t |
| 176 | "If non-nil, raise frame when switching to a buffer." |
| 177 | :group 'server |
| 178 | :type 'boolean |
| 179 | :version "22.1") |
| 180 | |
| 181 | (defcustom server-visit-hook nil |
| 182 | "Hook run when visiting a file for the Emacs server." |
| 183 | :group 'server |
| 184 | :type 'hook) |
| 185 | |
| 186 | (defcustom server-switch-hook nil |
| 187 | "Hook run when switching to a buffer for the Emacs server." |
| 188 | :group 'server |
| 189 | :type 'hook) |
| 190 | |
| 191 | (defcustom server-done-hook nil |
| 192 | "Hook run when done editing a buffer for the Emacs server." |
| 193 | :group 'server |
| 194 | :type 'hook) |
| 195 | |
| 196 | (defvar server-process nil |
| 197 | "The current server process.") |
| 198 | |
| 199 | (defvar server-clients nil |
| 200 | "List of current server clients. |
| 201 | Each element is a process.") |
| 202 | |
| 203 | (defvar server-buffer-clients nil |
| 204 | "List of client processes requesting editing of current buffer.") |
| 205 | (make-variable-buffer-local 'server-buffer-clients) |
| 206 | ;; Changing major modes should not erase this local. |
| 207 | (put 'server-buffer-clients 'permanent-local t) |
| 208 | |
| 209 | (defcustom server-window nil |
| 210 | "Specification of the window to use for selecting Emacs server buffers. |
| 211 | If nil, use the selected window. |
| 212 | If it is a function, it should take one argument (a buffer) and |
| 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 |
| 220 | :version "22.1" |
| 221 | :type '(choice (const :tag "Use selected window" |
| 222 | :match (lambda (widget value) |
| 223 | (not (functionp value))) |
| 224 | nil) |
| 225 | (function-item :tag "Display in new frame" switch-to-buffer-other-frame) |
| 226 | (function-item :tag "Use pop-to-buffer" pop-to-buffer) |
| 227 | (function :tag "Other function"))) |
| 228 | |
| 229 | (defcustom server-temp-file-regexp "^/tmp/Re\\|/draft$" |
| 230 | "Regexp matching names of temporary files. |
| 231 | These are deleted and reused after each edit by the programs that |
| 232 | invoke the Emacs server." |
| 233 | :group 'server |
| 234 | :type 'regexp) |
| 235 | |
| 236 | (defcustom server-kill-new-buffers t |
| 237 | "Whether to kill buffers when done with them. |
| 238 | If non-nil, kill a buffer unless it already existed before editing |
| 239 | it with the Emacs server. If nil, kill only buffers as specified by |
| 240 | `server-temp-file-regexp'. |
| 241 | Please note that only buffers that still have a client are killed, |
| 242 | i.e. buffers visited with \"emacsclient --no-wait\" are never killed |
| 243 | in this way." |
| 244 | :group 'server |
| 245 | :type 'boolean |
| 246 | :version "21.1") |
| 247 | |
| 248 | (or (assq 'server-buffer-clients minor-mode-alist) |
| 249 | (push '(server-buffer-clients " Server") minor-mode-alist)) |
| 250 | |
| 251 | (defvar server-existing-buffer nil |
| 252 | "Non-nil means the buffer existed before the server was asked to visit it. |
| 253 | This means that the server should not kill the buffer when you say you |
| 254 | are done with it in the server.") |
| 255 | (make-variable-buffer-local 'server-existing-buffer) |
| 256 | |
| 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") |
| 264 | |
| 265 | ;; We do not use `temporary-file-directory' here, because emacsclient |
| 266 | ;; does not read the init file. |
| 267 | (defvar server-socket-dir |
| 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.") |
| 272 | |
| 273 | (defun server-clients-with (property value) |
| 274 | "Return a list of clients with PROPERTY set to VALUE." |
| 275 | (let (result) |
| 276 | (dolist (proc server-clients) |
| 277 | (when (equal value (process-get proc property)) |
| 278 | (push proc result))) |
| 279 | result)) |
| 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." |
| 284 | (add-to-list 'server-clients proc)) |
| 285 | |
| 286 | (defmacro server-with-environment (env vars &rest body) |
| 287 | "Evaluate BODY with environment variables VARS set to those in ENV. |
| 288 | The environment variables are then restored to their previous values. |
| 289 | |
| 290 | VARS should be a list of strings. |
| 291 | ENV should be in the same format as `process-environment'." |
| 292 | (declare (indent 2)) |
| 293 | (let ((var (make-symbol "var")) |
| 294 | (value (make-symbol "value"))) |
| 295 | `(let ((process-environment process-environment)) |
| 296 | (dolist (,var ,vars) |
| 297 | (let ((,value (getenv-internal ,var ,env))) |
| 298 | (push (if (stringp ,value) |
| 299 | (concat ,var "=" ,value) |
| 300 | ,var) |
| 301 | process-environment))) |
| 302 | (progn ,@body)))) |
| 303 | |
| 304 | (defun server-delete-client (proc &optional noframe) |
| 305 | "Delete PROC, including its buffers, terminals and frames. |
| 306 | If NOFRAME is non-nil, let the frames live. |
| 307 | Updates `server-clients'." |
| 308 | (server-log (concat "server-delete-client" (if noframe " noframe")) proc) |
| 309 | ;; Force a new lookup of client (prevents infinite recursion). |
| 310 | (when (memq proc server-clients) |
| 311 | (let ((buffers (process-get proc 'buffers))) |
| 312 | |
| 313 | ;; Kill the client's buffers. |
| 314 | (dolist (buf buffers) |
| 315 | (when (buffer-live-p buf) |
| 316 | (with-current-buffer buf |
| 317 | ;; Kill the buffer if necessary. |
| 318 | (when (and (equal server-buffer-clients |
| 319 | (list proc)) |
| 320 | (or (and server-kill-new-buffers |
| 321 | (not server-existing-buffer)) |
| 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))))))))) |
| 332 | |
| 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 | |
| 343 | (setq server-clients (delq proc server-clients)) |
| 344 | |
| 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)))) |
| 352 | |
| 353 | ;; Delete the client's process. |
| 354 | (if (eq (process-status proc) 'open) |
| 355 | (delete-process proc)) |
| 356 | |
| 357 | (server-log "Deleted" proc)))) |
| 358 | |
| 359 | (defvar server-log-time-function 'current-time-string |
| 360 | "Function to generate timestamps for `server-buffer'.") |
| 361 | |
| 362 | (defconst server-buffer " *server*" |
| 363 | "Buffer used internally by Emacs's server. |
| 364 | One use is to log the I/O for debugging purposes (see option `server-log'), |
| 365 | the other is to provide a current buffer in which the process filter can |
| 366 | safely let-bind buffer-local variables like `default-directory'.") |
| 367 | |
| 368 | (defvar server-log nil |
| 369 | "If non-nil, log the server's inputs and outputs in the `server-buffer'.") |
| 370 | |
| 371 | (defun server-log (string &optional client) |
| 372 | "If option `server-log' is non-nil, log STRING to `server-buffer'. |
| 373 | If CLIENT is non-nil, add a description of it to the logged message." |
| 374 | (when server-log |
| 375 | (with-current-buffer (get-buffer-create server-buffer) |
| 376 | (goto-char (point-max)) |
| 377 | (insert (funcall server-log-time-function) |
| 378 | (cond |
| 379 | ((null client) " ") |
| 380 | ((listp client) (format " %s: " (car client))) |
| 381 | (t (format " %s: " client))) |
| 382 | string) |
| 383 | (or (bolp) (newline))))) |
| 384 | |
| 385 | (defun server-sentinel (proc msg) |
| 386 | "The process sentinel for Emacs server connections." |
| 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)) |
| 392 | ;; Delete the associated connection file, if applicable. |
| 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) |
| 398 | (ignore-errors |
| 399 | (delete-file (process-get proc :server-file)))) |
| 400 | (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc) |
| 401 | (server-delete-client proc)) |
| 402 | |
| 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 | |
| 412 | (defun server-select-display (display) |
| 413 | ;; If the current frame is on `display' we're all set. |
| 414 | ;; Similarly if we are unable to open frames on other displays, there's |
| 415 | ;; nothing more we can do. |
| 416 | (unless (or (not (fboundp 'make-frame-on-display)) |
| 417 | (server--on-display-p (selected-frame) display)) |
| 418 | ;; Otherwise, look for an existing frame there and select it. |
| 419 | (dolist (frame (frame-list)) |
| 420 | (when (server--on-display-p frame display) |
| 421 | (select-frame frame))) |
| 422 | ;; If there's no frame on that display yet, create and select one. |
| 423 | (unless (server--on-display-p (selected-frame) display) |
| 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. |
| 429 | `((server-dummy-buffer . ,buffer) |
| 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. |
| 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))) |
| 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)) |
| 455 | (when (buffer-live-p buffer) |
| 456 | (kill-buffer buffer))))) |
| 457 | |
| 458 | (defun server-handle-delete-frame (frame) |
| 459 | "Delete the client connection when the emacsclient frame is deleted. |
| 460 | \(To be used from `delete-frame-functions'.)" |
| 461 | (let ((proc (frame-parameter frame 'client))) |
| 462 | (when (and (frame-live-p frame) |
| 463 | proc |
| 464 | ;; See if this is the last frame for this client. |
| 465 | (>= 1 (let ((frame-num 0)) |
| 466 | (dolist (f (frame-list)) |
| 467 | (when (eq proc (frame-parameter f 'client)) |
| 468 | (setq frame-num (1+ frame-num)))) |
| 469 | frame-num))) |
| 470 | (server-log (format "server-handle-delete-frame, frame %s" frame) proc) |
| 471 | (server-delete-client proc 'noframe)))) ; Let delete-frame delete the frame later. |
| 472 | |
| 473 | (defun server-handle-suspend-tty (terminal) |
| 474 | "Notify the client process that its tty device is suspended." |
| 475 | (dolist (proc (server-clients-with 'terminal terminal)) |
| 476 | (server-log (format "server-handle-suspend-tty, terminal %s" terminal) |
| 477 | proc) |
| 478 | (condition-case nil |
| 479 | (server-send-string proc "-suspend \n") |
| 480 | (file-error ;The pipe/socket was closed. |
| 481 | (ignore-errors (server-delete-client proc)))))) |
| 482 | |
| 483 | (defun server-unquote-arg (arg) |
| 484 | "Remove &-quotation from ARG. |
| 485 | See `server-quote-arg' and `server-process-filter'." |
| 486 | (replace-regexp-in-string |
| 487 | "&." (lambda (s) |
| 488 | (pcase (aref s 1) |
| 489 | (?& "&") |
| 490 | (?- "-") |
| 491 | (?n "\n") |
| 492 | (_ " "))) |
| 493 | arg t t)) |
| 494 | |
| 495 | (defun server-quote-arg (arg) |
| 496 | "In ARG, insert a & before each &, each space, each newline, and -. |
| 497 | Change spaces to underscores, too, so that the return value never |
| 498 | contains a space. |
| 499 | |
| 500 | See `server-unquote-arg' and `server-process-filter'." |
| 501 | (replace-regexp-in-string |
| 502 | "[-&\n ]" (lambda (s) |
| 503 | (pcase (aref s 0) |
| 504 | (?& "&&") |
| 505 | (?- "&-") |
| 506 | (?\n "&n") |
| 507 | (?\s "&_"))) |
| 508 | arg t t)) |
| 509 | |
| 510 | (defun server-send-string (proc string) |
| 511 | "A wrapper around `process-send-string' for logging." |
| 512 | (server-log (concat "Sent " string) proc) |
| 513 | (process-send-string proc string)) |
| 514 | |
| 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)) |
| 522 | (let ((attrs (file-attributes dir 'integer))) |
| 523 | (unless attrs |
| 524 | (cl-letf (((default-file-modes) ?\700)) (make-directory dir t)) |
| 525 | (setq attrs (file-attributes dir 'integer))) |
| 526 | |
| 527 | ;; Check that it's safe for use. |
| 528 | (let* ((uid (nth 2 attrs)) |
| 529 | (w32 (eq system-type 'windows-nt)) |
| 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. |
| 536 | Directories on FAT32 filesystems are NOT secure against tampering. |
| 537 | See variable `server-auth-dir' for details." |
| 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))))))) |
| 551 | (unless safe |
| 552 | (error "The directory `%s' is unsafe" dir))))) |
| 553 | |
| 554 | (defun server-generate-key () |
| 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." |
| 558 | (interactive) |
| 559 | (let ((auth-key |
| 560 | (cl-loop repeat 64 |
| 561 | collect (+ 33 (random 94)) into auth |
| 562 | finally return (concat auth)))) |
| 563 | (if (called-interactively-p 'interactive) |
| 564 | (insert auth-key)) |
| 565 | auth-key)) |
| 566 | |
| 567 | (defun server-get-auth-key () |
| 568 | "Return server's authentication key. |
| 569 | |
| 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." |
| 573 | (if server-auth-key |
| 574 | (if (string-match-p "^[!-~]\\{64\\}$" server-auth-key) |
| 575 | server-auth-key |
| 576 | (error "The key '%s' is invalid" server-auth-key)) |
| 577 | (server-generate-key))) |
| 578 | |
| 579 | ;;;###autoload |
| 580 | (defun server-start (&optional leave-dead inhibit-prompt) |
| 581 | "Allow this Emacs process to be a server for client processes. |
| 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\". |
| 586 | |
| 587 | Optional argument LEAVE-DEAD (interactively, a prefix arg) means just |
| 588 | kill any existing server communications subprocess. |
| 589 | |
| 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 | |
| 594 | To force-start a server, do \\[server-force-delete] and then |
| 595 | \\[server-start]." |
| 596 | (interactive "P") |
| 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. |
| 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? ")))) |
| 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 |
| 617 | (ignore-errors |
| 618 | (let (delete-by-moving-to-trash) |
| 619 | (delete-file server-file))) |
| 620 | (setq server-mode nil) ;; already set by the minor mode code |
| 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) |
| 629 | (setq leave-dead t)) |
| 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 |
| 636 | (unless (eq t leave-dead) (server-log (message "Server stopped"))) |
| 637 | (setq server-process nil)) |
| 638 | ;; Make sure there is a safe directory in which to place the socket. |
| 639 | (server-ensure-safe-dir server-dir) |
| 640 | (when server-process |
| 641 | (server-log (message "Restarting server"))) |
| 642 | (cl-letf (((default-file-modes) ?\700)) |
| 643 | (add-hook 'suspend-tty-functions 'server-handle-suspend-tty) |
| 644 | (add-hook 'delete-frame-functions 'server-handle-delete-frame) |
| 645 | (add-hook 'kill-emacs-query-functions |
| 646 | 'server-kill-emacs-query-function) |
| 647 | (add-hook 'kill-emacs-hook 'server-force-stop) ;Cleanup upon exit. |
| 648 | (setq server-process |
| 649 | (apply #'make-network-process |
| 650 | :name server-name |
| 651 | :server t |
| 652 | :noquery t |
| 653 | :sentinel 'server-sentinel |
| 654 | :filter 'server-process-filter |
| 655 | ;; We must receive file names without being decoded. |
| 656 | ;; Those are decoded by server-process-filter according |
| 657 | ;; to file-name-coding-system. Also don't get |
| 658 | ;; confused by CRs since we don't quote them. |
| 659 | :coding 'raw-text-unix |
| 660 | ;; The other args depend on the kind of socket used. |
| 661 | (if server-use-tcp |
| 662 | (list :family 'ipv4 ;; We're not ready for IPv6 yet |
| 663 | :service (or server-port t) |
| 664 | :host (or server-host 'local) |
| 665 | :plist '(:authenticated nil)) |
| 666 | (list :family 'local |
| 667 | :service server-file |
| 668 | :plist '(:authenticated t))))) |
| 669 | (unless server-process (error "Could not start server process")) |
| 670 | (process-put server-process :server-file server-file) |
| 671 | (when server-use-tcp |
| 672 | (let ((auth-key (server-get-auth-key))) |
| 673 | (process-put server-process :auth-key auth-key) |
| 674 | (with-temp-file server-file |
| 675 | (set-buffer-multibyte nil) |
| 676 | (setq buffer-file-coding-system 'no-conversion) |
| 677 | (insert (format-network-address |
| 678 | (process-contact server-process :local)) |
| 679 | " " (number-to-string (emacs-pid)) ; Kept for compatibility |
| 680 | "\n" auth-key))))))))) |
| 681 | |
| 682 | (defun server-force-stop () |
| 683 | "Kill all connections to the current server. |
| 684 | This function is meant to be called from `kill-emacs-hook'." |
| 685 | (server-start t t)) |
| 686 | |
| 687 | ;;;###autoload |
| 688 | (defun server-force-delete (&optional name) |
| 689 | "Unconditionally delete connection file for server NAME. |
| 690 | If server is running, it is first stopped. |
| 691 | NAME defaults to `server-name'. With argument, ask for NAME." |
| 692 | (interactive |
| 693 | (list (if current-prefix-arg |
| 694 | (read-string "Server name: " nil nil server-name)))) |
| 695 | (when server-mode (with-temp-message nil (server-mode -1))) |
| 696 | (let ((file (expand-file-name (or name server-name) |
| 697 | (if server-use-tcp |
| 698 | server-auth-dir |
| 699 | server-socket-dir)))) |
| 700 | (condition-case nil |
| 701 | (let (delete-by-moving-to-trash) |
| 702 | (delete-file file) |
| 703 | (message "Connection file %S deleted" file)) |
| 704 | (file-error |
| 705 | (message "No connection file %S" file))))) |
| 706 | |
| 707 | (defun server-running-p (&optional name) |
| 708 | "Test whether server NAME is running. |
| 709 | |
| 710 | Return values: |
| 711 | nil the server is definitely not running. |
| 712 | t the server seems to be running. |
| 713 | something else we cannot determine whether it's running without using |
| 714 | commands which may have to wait for a long time." |
| 715 | (unless name (setq name server-name)) |
| 716 | (condition-case nil |
| 717 | (if server-use-tcp |
| 718 | (with-temp-buffer |
| 719 | (insert-file-contents-literally (expand-file-name name server-auth-dir)) |
| 720 | (or (and (looking-at "127\\.0\\.0\\.1:[0-9]+ \\([0-9]+\\)") |
| 721 | (assq 'comm |
| 722 | (process-attributes |
| 723 | (string-to-number (match-string 1)))) |
| 724 | t) |
| 725 | :other)) |
| 726 | (delete-process |
| 727 | (make-network-process |
| 728 | :name "server-client-test" :family 'local :server nil :noquery t |
| 729 | :service (expand-file-name name server-socket-dir))) |
| 730 | t) |
| 731 | (file-error nil))) |
| 732 | |
| 733 | ;;;###autoload |
| 734 | (define-minor-mode server-mode |
| 735 | "Toggle Server mode. |
| 736 | With a prefix argument ARG, enable Server mode if ARG is |
| 737 | positive, and disable it otherwise. If called from Lisp, enable |
| 738 | Server mode if ARG is omitted or nil. |
| 739 | |
| 740 | Server mode runs a process that accepts commands from the |
| 741 | `emacsclient' program. See Info node `Emacs server' and |
| 742 | `server-start' for details." |
| 743 | :global t |
| 744 | :group 'server |
| 745 | :version "22.1" |
| 746 | ;; Fixme: Should this check for an existing server socket and do |
| 747 | ;; nothing if there is one (for multiple Emacs sessions)? |
| 748 | (server-start (not server-mode))) |
| 749 | \f |
| 750 | (defun server-eval-and-print (expr proc) |
| 751 | "Eval EXPR and send the result back to client PROC." |
| 752 | ;; While we're running asynchronously (from a process filter), it is likely |
| 753 | ;; that the emacsclient command was run in response to a user |
| 754 | ;; action, so the user probably knows that Emacs is processing this |
| 755 | ;; emacsclient request, so if we get a C-g it's likely that the user |
| 756 | ;; intended it to interrupt us rather than interrupt whatever Emacs |
| 757 | ;; was doing before it started handling the process filter. |
| 758 | ;; Hence `with-local-quit' (bug#6585). |
| 759 | (let ((v (with-local-quit (eval (car (read-from-string expr)))))) |
| 760 | (when proc |
| 761 | (with-temp-buffer |
| 762 | (let ((standard-output (current-buffer))) |
| 763 | (pp v) |
| 764 | (let ((text (buffer-substring-no-properties |
| 765 | (point-min) (point-max)))) |
| 766 | (server-reply-print (server-quote-arg text) proc))))))) |
| 767 | |
| 768 | (defconst server-msg-size 1024 |
| 769 | "Maximum size of a message sent to a client.") |
| 770 | |
| 771 | (defun server-reply-print (qtext proc) |
| 772 | "Send a `-print QTEXT' command to client PROC. |
| 773 | QTEXT must be already quoted. |
| 774 | This handles splitting the command if it would be bigger than |
| 775 | `server-msg-size'." |
| 776 | (let ((prefix "-print ") |
| 777 | part) |
| 778 | (while (> (+ (length qtext) (length prefix) 1) server-msg-size) |
| 779 | ;; We have to split the string |
| 780 | (setq part (substring qtext 0 (- server-msg-size (length prefix) 1))) |
| 781 | ;; Don't split in the middle of a quote sequence |
| 782 | (if (string-match "\\(^\\|[^&]\\)\\(&&\\)+$" part) |
| 783 | ;; There is an uneven number of & at the end |
| 784 | (setq part (substring part 0 -1))) |
| 785 | (setq qtext (substring qtext (length part))) |
| 786 | (server-send-string proc (concat prefix part "\n")) |
| 787 | (setq prefix "-print-nonl ")) |
| 788 | (server-send-string proc (concat prefix qtext "\n")))) |
| 789 | |
| 790 | (defun server-create-tty-frame (tty type proc) |
| 791 | (unless tty |
| 792 | (error "Invalid terminal device")) |
| 793 | (unless type |
| 794 | (error "Invalid terminal type")) |
| 795 | (add-to-list 'frame-inherited-parameters 'client) |
| 796 | (let ((frame |
| 797 | (server-with-environment (process-get proc 'env) |
| 798 | '("LANG" "LC_CTYPE" "LC_ALL" |
| 799 | ;; For tgetent(3); list according to ncurses(3). |
| 800 | "BAUDRATE" "COLUMNS" "ESCDELAY" "HOME" "LINES" |
| 801 | "NCURSES_ASSUMED_COLORS" "NCURSES_NO_PADDING" |
| 802 | "NCURSES_NO_SETBUF" "TERM" "TERMCAP" "TERMINFO" |
| 803 | "TERMINFO_DIRS" "TERMPATH" |
| 804 | ;; rxvt wants these |
| 805 | "COLORFGBG" "COLORTERM") |
| 806 | (make-frame `((window-system . nil) |
| 807 | (tty . ,tty) |
| 808 | (tty-type . ,type) |
| 809 | ;; Ignore nowait here; we always need to |
| 810 | ;; clean up opened ttys when the client dies. |
| 811 | (client . ,proc) |
| 812 | ;; This is a leftover from an earlier |
| 813 | ;; attempt at making it possible for process |
| 814 | ;; run in the server process to use the |
| 815 | ;; environment of the client process. |
| 816 | ;; It has no effect now and to make it work |
| 817 | ;; we'd need to decide how to make |
| 818 | ;; process-environment interact with client |
| 819 | ;; envvars, and then to change the |
| 820 | ;; C functions `child_setup' and |
| 821 | ;; `getenv_internal' accordingly. |
| 822 | (environment . ,(process-get proc 'env))))))) |
| 823 | |
| 824 | ;; ttys don't use the `display' parameter, but callproc.c does to set |
| 825 | ;; the DISPLAY environment on subprocesses. |
| 826 | (set-frame-parameter frame 'display |
| 827 | (getenv-internal "DISPLAY" (process-get proc 'env))) |
| 828 | (select-frame frame) |
| 829 | (process-put proc 'frame frame) |
| 830 | (process-put proc 'terminal (frame-terminal frame)) |
| 831 | frame)) |
| 832 | |
| 833 | (defun server-create-window-system-frame (display nowait proc parent-id |
| 834 | &optional parameters) |
| 835 | (let* ((display (or display |
| 836 | (frame-parameter nil 'display) |
| 837 | (error "Please specify display."))) |
| 838 | (w (or (cdr (assq 'window-system parameters)) |
| 839 | (window-system-for-display display)))) |
| 840 | |
| 841 | (unless (assq w window-system-initialization-alist) |
| 842 | (setq w nil)) |
| 843 | |
| 844 | ;; Special case for ns. This is because DISPLAY may not be set at all |
| 845 | ;; which in the ns case isn't an error. The variable display then becomes |
| 846 | ;; the fully qualified hostname, which make-frame-on-display below |
| 847 | ;; does not understand and throws an error. |
| 848 | ;; It may also be a valid X display, but if Emacs is compiled for ns, it |
| 849 | ;; can not make X frames. |
| 850 | (if (featurep 'ns-win) |
| 851 | (setq w 'ns display "ns")) |
| 852 | |
| 853 | (cond (w |
| 854 | ;; Flag frame as client-created, but use a dummy client. |
| 855 | ;; This will prevent the frame from being deleted when |
| 856 | ;; emacsclient quits while also preventing |
| 857 | ;; `server-save-buffers-kill-terminal' from unexpectedly |
| 858 | ;; killing emacs on that frame. |
| 859 | (let* ((params `((client . ,(if nowait 'nowait proc)) |
| 860 | ;; This is a leftover, see above. |
| 861 | (environment . ,(process-get proc 'env)) |
| 862 | ,@parameters)) |
| 863 | frame) |
| 864 | (if parent-id |
| 865 | (push (cons 'parent-id (string-to-number parent-id)) params)) |
| 866 | (add-to-list 'frame-inherited-parameters 'client) |
| 867 | (setq frame (make-frame-on-display display params)) |
| 868 | (server-log (format "%s created" frame) proc) |
| 869 | (select-frame frame) |
| 870 | (process-put proc 'frame frame) |
| 871 | (process-put proc 'terminal (frame-terminal frame)) |
| 872 | frame)) |
| 873 | |
| 874 | (t |
| 875 | (server-log "Window system unsupported" proc) |
| 876 | (server-send-string proc "-window-system-unsupported \n") |
| 877 | nil)))) |
| 878 | |
| 879 | (defun server-goto-toplevel (proc) |
| 880 | (condition-case nil |
| 881 | ;; If we're running isearch, we must abort it to allow Emacs to |
| 882 | ;; display the buffer and switch to it. |
| 883 | (dolist (buffer (buffer-list)) |
| 884 | (with-current-buffer buffer |
| 885 | (when (bound-and-true-p isearch-mode) |
| 886 | (isearch-cancel)))) |
| 887 | ;; Signaled by isearch-cancel. |
| 888 | (quit (message nil))) |
| 889 | (when (> (recursion-depth) 0) |
| 890 | ;; We're inside a minibuffer already, so if the emacs-client is trying |
| 891 | ;; to open a frame on a new display, we might end up with an unusable |
| 892 | ;; frame because input from that display will be blocked (until exiting |
| 893 | ;; the minibuffer). Better exit this minibuffer right away. |
| 894 | ;; Similarly with recursive-edits such as the splash screen. |
| 895 | (run-with-timer 0 nil (lambda () (server-execute-continuation proc))) |
| 896 | (top-level))) |
| 897 | |
| 898 | ;; We use various special properties on process objects: |
| 899 | ;; - `env' stores the info about the environment of the emacsclient process. |
| 900 | ;; - `continuation' is a no-arg function that we need to execute. It contains |
| 901 | ;; commands we wanted to execute in some earlier invocation of the process |
| 902 | ;; filter but that we somehow were unable to process at that time |
| 903 | ;; (e.g. because we first need to throw to the toplevel). |
| 904 | |
| 905 | (defun server-execute-continuation (proc) |
| 906 | (let ((continuation (process-get proc 'continuation))) |
| 907 | (process-put proc 'continuation nil) |
| 908 | (if continuation (ignore-errors (funcall continuation))))) |
| 909 | |
| 910 | (cl-defun server-process-filter (proc string) |
| 911 | "Process a request from the server to edit some files. |
| 912 | PROC is the server process. STRING consists of a sequence of |
| 913 | commands prefixed by a dash. Some commands have arguments; |
| 914 | these are &-quoted and need to be decoded by `server-unquote-arg'. |
| 915 | The filter parses and executes these commands. |
| 916 | |
| 917 | To illustrate the protocol, here is an example command that |
| 918 | emacsclient sends to create a new X frame (note that the whole |
| 919 | sequence is sent on a single line): |
| 920 | |
| 921 | -env HOME=/home/lorentey |
| 922 | -env DISPLAY=:0.0 |
| 923 | ... lots of other -env commands |
| 924 | -display :0.0 |
| 925 | -window-system |
| 926 | |
| 927 | The following commands are accepted by the server: |
| 928 | |
| 929 | `-auth AUTH-STRING' |
| 930 | Authenticate the client using the secret authentication string |
| 931 | AUTH-STRING. |
| 932 | |
| 933 | `-env NAME=VALUE' |
| 934 | An environment variable on the client side. |
| 935 | |
| 936 | `-dir DIRNAME' |
| 937 | The current working directory of the client process. |
| 938 | |
| 939 | `-current-frame' |
| 940 | Forbid the creation of new frames. |
| 941 | |
| 942 | `-frame-parameters ALIST' |
| 943 | Set the parameters of the created frame. |
| 944 | |
| 945 | `-nowait' |
| 946 | Request that the next frame created should not be |
| 947 | associated with this client. |
| 948 | |
| 949 | `-display DISPLAY' |
| 950 | Set the display name to open X frames on. |
| 951 | |
| 952 | `-position LINE[:COLUMN]' |
| 953 | Go to the given line and column number |
| 954 | in the next file opened. |
| 955 | |
| 956 | `-file FILENAME' |
| 957 | Load the given file in the current frame. |
| 958 | |
| 959 | `-eval EXPR' |
| 960 | Evaluate EXPR as a Lisp expression and return the |
| 961 | result in -print commands. |
| 962 | |
| 963 | `-window-system' |
| 964 | Open a new X frame. |
| 965 | |
| 966 | `-tty DEVICENAME TYPE' |
| 967 | Open a new tty frame at the client. |
| 968 | |
| 969 | `-suspend' |
| 970 | Suspend this tty frame. The client sends this string in |
| 971 | response to SIGTSTP and SIGTTOU. The server must cease all I/O |
| 972 | on this tty until it gets a -resume command. |
| 973 | |
| 974 | `-resume' |
| 975 | Resume this tty frame. The client sends this string when it |
| 976 | gets the SIGCONT signal and it is the foreground process on its |
| 977 | controlling tty. |
| 978 | |
| 979 | `-ignore COMMENT' |
| 980 | Do nothing, but put the comment in the server log. |
| 981 | Useful for debugging. |
| 982 | |
| 983 | |
| 984 | The following commands are accepted by the client: |
| 985 | |
| 986 | `-emacs-pid PID' |
| 987 | Describes the process id of the Emacs process; |
| 988 | used to forward window change signals to it. |
| 989 | |
| 990 | `-window-system-unsupported' |
| 991 | Signals that the server does not support creating X frames; |
| 992 | the client must try again with a tty frame. |
| 993 | |
| 994 | `-print STRING' |
| 995 | Print STRING on stdout. Used to send values |
| 996 | returned by -eval. |
| 997 | |
| 998 | `-print-nonl STRING' |
| 999 | Print STRING on stdout. Used to continue a |
| 1000 | preceding -print command that would be too big to send |
| 1001 | in a single message. |
| 1002 | |
| 1003 | `-error DESCRIPTION' |
| 1004 | Signal an error and delete process PROC. |
| 1005 | |
| 1006 | `-suspend' |
| 1007 | Suspend this terminal, i.e., stop the client process. |
| 1008 | Sent when the user presses C-z." |
| 1009 | (server-log (concat "Received " string) proc) |
| 1010 | ;; First things first: let's check the authentication |
| 1011 | (unless (process-get proc :authenticated) |
| 1012 | (if (and (string-match "-auth \\([!-~]+\\)\n?" string) |
| 1013 | (equal (match-string 1 string) (process-get proc :auth-key))) |
| 1014 | (progn |
| 1015 | (setq string (substring string (match-end 0))) |
| 1016 | (process-put proc :authenticated t) |
| 1017 | (server-log "Authentication successful" proc)) |
| 1018 | (server-log "Authentication failed" proc) |
| 1019 | (server-send-string |
| 1020 | proc (concat "-error " (server-quote-arg "Authentication failed"))) |
| 1021 | ;; Before calling `delete-process', give emacsclient time to |
| 1022 | ;; receive the error string and shut down on its own. |
| 1023 | (sit-for 1) |
| 1024 | (delete-process proc) |
| 1025 | ;; We return immediately. |
| 1026 | (cl-return-from server-process-filter))) |
| 1027 | (let ((prev (process-get proc 'previous-string))) |
| 1028 | (when prev |
| 1029 | (setq string (concat prev string)) |
| 1030 | (process-put proc 'previous-string nil))) |
| 1031 | (condition-case err |
| 1032 | (progn |
| 1033 | (server-add-client proc) |
| 1034 | ;; Send our pid |
| 1035 | (server-send-string proc (concat "-emacs-pid " |
| 1036 | (number-to-string (emacs-pid)) "\n")) |
| 1037 | (if (not (string-match "\n" string)) |
| 1038 | ;; Save for later any partial line that remains. |
| 1039 | (when (> (length string) 0) |
| 1040 | (process-put proc 'previous-string string)) |
| 1041 | |
| 1042 | ;; In earlier versions of server.el (where we used an `emacsserver' |
| 1043 | ;; process), there could be multiple lines. Nowadays this is not |
| 1044 | ;; supported any more. |
| 1045 | (cl-assert (eq (match-end 0) (length string))) |
| 1046 | (let ((request (substring string 0 (match-beginning 0))) |
| 1047 | (coding-system (and (default-value 'enable-multibyte-characters) |
| 1048 | (or file-name-coding-system |
| 1049 | default-file-name-coding-system))) |
| 1050 | nowait ; t if emacsclient does not want to wait for us. |
| 1051 | frame ; Frame opened for the client (if any). |
| 1052 | display ; Open frame on this display. |
| 1053 | parent-id ; Window ID for XEmbed |
| 1054 | dontkill ; t if client should not be killed. |
| 1055 | commands |
| 1056 | dir |
| 1057 | use-current-frame |
| 1058 | frame-parameters ;parameters for newly created frame |
| 1059 | tty-name ; nil, `window-system', or the tty name. |
| 1060 | tty-type ; string. |
| 1061 | files |
| 1062 | filepos |
| 1063 | args-left) |
| 1064 | ;; Remove this line from STRING. |
| 1065 | (setq string (substring string (match-end 0))) |
| 1066 | (setq args-left |
| 1067 | (mapcar 'server-unquote-arg (split-string request " " t))) |
| 1068 | (while args-left |
| 1069 | (pcase (pop args-left) |
| 1070 | ;; -version CLIENT-VERSION: obsolete at birth. |
| 1071 | (`"-version" (pop args-left)) |
| 1072 | |
| 1073 | ;; -nowait: Emacsclient won't wait for a result. |
| 1074 | (`"-nowait" (setq nowait t)) |
| 1075 | |
| 1076 | ;; -current-frame: Don't create frames. |
| 1077 | (`"-current-frame" (setq use-current-frame t)) |
| 1078 | |
| 1079 | ;; -frame-parameters: Set frame parameters |
| 1080 | (`"-frame-parameters" |
| 1081 | (let ((alist (pop args-left))) |
| 1082 | (if coding-system |
| 1083 | (setq alist (decode-coding-string alist coding-system))) |
| 1084 | (setq frame-parameters (car (read-from-string alist))))) |
| 1085 | |
| 1086 | ;; -display DISPLAY: |
| 1087 | ;; Open X frames on the given display instead of the default. |
| 1088 | (`"-display" |
| 1089 | (setq display (pop args-left)) |
| 1090 | (if (zerop (length display)) (setq display nil))) |
| 1091 | |
| 1092 | ;; -parent-id ID: |
| 1093 | ;; Open X frame within window ID, via XEmbed. |
| 1094 | (`"-parent-id" |
| 1095 | (setq parent-id (pop args-left)) |
| 1096 | (if (zerop (length parent-id)) (setq parent-id nil))) |
| 1097 | |
| 1098 | ;; -window-system: Open a new X frame. |
| 1099 | (`"-window-system" |
| 1100 | (if (fboundp 'x-create-frame) |
| 1101 | (setq dontkill t |
| 1102 | tty-name 'window-system))) |
| 1103 | |
| 1104 | ;; -resume: Resume a suspended tty frame. |
| 1105 | (`"-resume" |
| 1106 | (let ((terminal (process-get proc 'terminal))) |
| 1107 | (setq dontkill t) |
| 1108 | (push (lambda () |
| 1109 | (when (eq (terminal-live-p terminal) t) |
| 1110 | (resume-tty terminal))) |
| 1111 | commands))) |
| 1112 | |
| 1113 | ;; -suspend: Suspend the client's frame. (In case we |
| 1114 | ;; get out of sync, and a C-z sends a SIGTSTP to |
| 1115 | ;; emacsclient.) |
| 1116 | (`"-suspend" |
| 1117 | (let ((terminal (process-get proc 'terminal))) |
| 1118 | (setq dontkill t) |
| 1119 | (push (lambda () |
| 1120 | (when (eq (terminal-live-p terminal) t) |
| 1121 | (suspend-tty terminal))) |
| 1122 | commands))) |
| 1123 | |
| 1124 | ;; -ignore COMMENT: Noop; useful for debugging emacsclient. |
| 1125 | ;; (The given comment appears in the server log.) |
| 1126 | (`"-ignore" |
| 1127 | (setq dontkill t) |
| 1128 | (pop args-left)) |
| 1129 | |
| 1130 | ;; -tty DEVICE-NAME TYPE: Open a new tty frame. |
| 1131 | ;; (But if we see -window-system later, use that.) |
| 1132 | (`"-tty" |
| 1133 | (setq tty-name (pop args-left) |
| 1134 | tty-type (pop args-left) |
| 1135 | dontkill (or dontkill |
| 1136 | (not use-current-frame))) |
| 1137 | ;; On Windows, emacsclient always asks for a tty |
| 1138 | ;; frame. If running a GUI server, force the frame |
| 1139 | ;; type to GUI. (Cygwin is perfectly happy with |
| 1140 | ;; multi-tty support, so don't override the user's |
| 1141 | ;; choice there.) |
| 1142 | (when (and (eq system-type 'windows-nt) |
| 1143 | (eq window-system 'w32)) |
| 1144 | (push "-window-system" args-left))) |
| 1145 | |
| 1146 | ;; -position LINE[:COLUMN]: Set point to the given |
| 1147 | ;; position in the next file. |
| 1148 | (`"-position" |
| 1149 | (if (not (string-match "\\+\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?" |
| 1150 | (car args-left))) |
| 1151 | (error "Invalid -position command in client args")) |
| 1152 | (let ((arg (pop args-left))) |
| 1153 | (setq filepos |
| 1154 | (cons (string-to-number (match-string 1 arg)) |
| 1155 | (string-to-number (or (match-string 2 arg) |
| 1156 | "")))))) |
| 1157 | |
| 1158 | ;; -file FILENAME: Load the given file. |
| 1159 | (`"-file" |
| 1160 | (let ((file (pop args-left))) |
| 1161 | (if coding-system |
| 1162 | (setq file (decode-coding-string file coding-system))) |
| 1163 | (setq file (expand-file-name file dir)) |
| 1164 | (push (cons file filepos) files) |
| 1165 | (server-log (format "New file: %s %s" |
| 1166 | file (or filepos "")) proc)) |
| 1167 | (setq filepos nil)) |
| 1168 | |
| 1169 | ;; -eval EXPR: Evaluate a Lisp expression. |
| 1170 | (`"-eval" |
| 1171 | (if use-current-frame |
| 1172 | (setq use-current-frame 'always)) |
| 1173 | (let ((expr (pop args-left))) |
| 1174 | (if coding-system |
| 1175 | (setq expr (decode-coding-string expr coding-system))) |
| 1176 | (push (lambda () (server-eval-and-print expr proc)) |
| 1177 | commands) |
| 1178 | (setq filepos nil))) |
| 1179 | |
| 1180 | ;; -env NAME=VALUE: An environment variable. |
| 1181 | (`"-env" |
| 1182 | (let ((var (pop args-left))) |
| 1183 | ;; XXX Variables should be encoded as in getenv/setenv. |
| 1184 | (process-put proc 'env |
| 1185 | (cons var (process-get proc 'env))))) |
| 1186 | |
| 1187 | ;; -dir DIRNAME: The cwd of the emacsclient process. |
| 1188 | (`"-dir" |
| 1189 | (setq dir (pop args-left)) |
| 1190 | (if coding-system |
| 1191 | (setq dir (decode-coding-string dir coding-system))) |
| 1192 | (setq dir (command-line-normalize-file-name dir)) |
| 1193 | (process-put proc 'server-client-directory dir)) |
| 1194 | |
| 1195 | ;; Unknown command. |
| 1196 | (arg (error "Unknown command: %s" arg)))) |
| 1197 | |
| 1198 | ;; If both -no-wait and -tty are given with file or sexp |
| 1199 | ;; arguments, use an existing frame. |
| 1200 | (and nowait |
| 1201 | (not (eq tty-name 'window-system)) |
| 1202 | (or files commands) |
| 1203 | (setq use-current-frame t)) |
| 1204 | |
| 1205 | (setq frame |
| 1206 | (cond |
| 1207 | ((and use-current-frame |
| 1208 | (or (eq use-current-frame 'always) |
| 1209 | ;; We can't use the Emacs daemon's |
| 1210 | ;; terminal frame. |
| 1211 | (not (and (daemonp) |
| 1212 | (null (cdr (frame-list))) |
| 1213 | (eq (selected-frame) |
| 1214 | terminal-frame))))) |
| 1215 | (setq tty-name nil tty-type nil) |
| 1216 | (if display (server-select-display display))) |
| 1217 | ((eq tty-name 'window-system) |
| 1218 | (server-create-window-system-frame display nowait proc |
| 1219 | parent-id |
| 1220 | frame-parameters)) |
| 1221 | ;; When resuming on a tty, tty-name is nil. |
| 1222 | (tty-name |
| 1223 | (server-create-tty-frame tty-name tty-type proc)))) |
| 1224 | |
| 1225 | (process-put |
| 1226 | proc 'continuation |
| 1227 | (lambda () |
| 1228 | (with-current-buffer (get-buffer-create server-buffer) |
| 1229 | ;; Use the same cwd as the emacsclient, if possible, so |
| 1230 | ;; relative file names work correctly, even in `eval'. |
| 1231 | (let ((default-directory |
| 1232 | (if (and dir (file-directory-p dir)) |
| 1233 | dir default-directory))) |
| 1234 | (server-execute proc files nowait commands |
| 1235 | dontkill frame tty-name))))) |
| 1236 | |
| 1237 | (when (or frame files) |
| 1238 | (server-goto-toplevel proc)) |
| 1239 | |
| 1240 | (server-execute-continuation proc)))) |
| 1241 | ;; condition-case |
| 1242 | (error (server-return-error proc err)))) |
| 1243 | |
| 1244 | (defun server-execute (proc files nowait commands dontkill frame tty-name) |
| 1245 | ;; This is run from timers and process-filters, i.e. "asynchronously". |
| 1246 | ;; But w.r.t the user, this is not really asynchronous since the timer |
| 1247 | ;; is run after 0s and the process-filter is run in response to the |
| 1248 | ;; user running `emacsclient'. So it is OK to override the |
| 1249 | ;; inhibit-quit flag, which is good since `commands' (as well as |
| 1250 | ;; find-file-noselect via the major-mode) can run arbitrary code, |
| 1251 | ;; including code that needs to wait. |
| 1252 | (with-local-quit |
| 1253 | (condition-case err |
| 1254 | (let ((buffers (server-visit-files files proc nowait))) |
| 1255 | (mapc 'funcall (nreverse commands)) |
| 1256 | |
| 1257 | ;; If we were told only to open a new client, obey |
| 1258 | ;; `initial-buffer-choice' if it specifies a file |
| 1259 | ;; or a function. |
| 1260 | (unless (or files commands) |
| 1261 | (let ((buf |
| 1262 | (cond ((stringp initial-buffer-choice) |
| 1263 | (find-file-noselect initial-buffer-choice)) |
| 1264 | ((functionp initial-buffer-choice) |
| 1265 | (funcall initial-buffer-choice))))) |
| 1266 | (switch-to-buffer |
| 1267 | (if (buffer-live-p buf) buf (get-buffer-create "*scratch*")) |
| 1268 | 'norecord))) |
| 1269 | |
| 1270 | ;; Delete the client if necessary. |
| 1271 | (cond |
| 1272 | (nowait |
| 1273 | ;; Client requested nowait; return immediately. |
| 1274 | (server-log "Close nowait client" proc) |
| 1275 | (server-delete-client proc)) |
| 1276 | ((and (not dontkill) (null buffers)) |
| 1277 | ;; This client is empty; get rid of it immediately. |
| 1278 | (server-log "Close empty client" proc) |
| 1279 | (server-delete-client proc))) |
| 1280 | (cond |
| 1281 | ((or isearch-mode (minibufferp)) |
| 1282 | nil) |
| 1283 | ((and frame (null buffers)) |
| 1284 | (message "%s" (substitute-command-keys |
| 1285 | "When done with this frame, type \\[delete-frame]"))) |
| 1286 | ((not (null buffers)) |
| 1287 | (server-switch-buffer (car buffers) nil (cdr (car files))) |
| 1288 | (run-hooks 'server-switch-hook) |
| 1289 | (unless nowait |
| 1290 | (message "%s" (substitute-command-keys |
| 1291 | "When done with a buffer, type \\[server-edit]"))))) |
| 1292 | (when (and frame (null tty-name)) |
| 1293 | (server-unselect-display frame))) |
| 1294 | ((quit error) |
| 1295 | (when (eq (car err) 'quit) |
| 1296 | (message "Quit emacsclient request")) |
| 1297 | (server-return-error proc err))))) |
| 1298 | |
| 1299 | (defun server-return-error (proc err) |
| 1300 | (ignore-errors |
| 1301 | (server-send-string |
| 1302 | proc (concat "-error " (server-quote-arg |
| 1303 | (error-message-string err)))) |
| 1304 | (server-log (error-message-string err) proc) |
| 1305 | ;; Before calling `delete-process', give emacsclient time to |
| 1306 | ;; receive the error string and shut down on its own. |
| 1307 | (sit-for 5) |
| 1308 | (delete-process proc))) |
| 1309 | |
| 1310 | (defun server-goto-line-column (line-col) |
| 1311 | "Move point to the position indicated in LINE-COL. |
| 1312 | LINE-COL should be a pair (LINE . COL)." |
| 1313 | (when line-col |
| 1314 | (goto-char (point-min)) |
| 1315 | (forward-line (1- (car line-col))) |
| 1316 | (let ((column-number (cdr line-col))) |
| 1317 | (when (> column-number 0) |
| 1318 | (move-to-column (1- column-number)))))) |
| 1319 | |
| 1320 | (defun server-visit-files (files proc &optional nowait) |
| 1321 | "Find FILES and return a list of buffers created. |
| 1322 | FILES is an alist whose elements are (FILENAME . FILEPOS) |
| 1323 | where FILEPOS can be nil or a pair (LINENUMBER . COLUMNNUMBER). |
| 1324 | PROC is the client that requested this operation. |
| 1325 | NOWAIT non-nil means this client is not waiting for the results, |
| 1326 | so don't mark these buffers specially, just visit them normally." |
| 1327 | ;; Bind last-nonmenu-event to force use of keyboard, not mouse, for queries. |
| 1328 | (let ((last-nonmenu-event t) client-record) |
| 1329 | ;; Restore the current buffer afterward, but not using save-excursion, |
| 1330 | ;; because we don't want to save point in this buffer |
| 1331 | ;; if it happens to be one of those specified by the server. |
| 1332 | (save-current-buffer |
| 1333 | (dolist (file files) |
| 1334 | ;; If there is an existing buffer modified or the file is |
| 1335 | ;; modified, revert it. If there is an existing buffer with |
| 1336 | ;; deleted file, offer to write it. |
| 1337 | (let* ((minibuffer-auto-raise (or server-raise-frame |
| 1338 | minibuffer-auto-raise)) |
| 1339 | (filen (car file)) |
| 1340 | (obuf (get-file-buffer filen))) |
| 1341 | (add-to-history 'file-name-history filen) |
| 1342 | (if (null obuf) |
| 1343 | (progn |
| 1344 | (run-hooks 'pre-command-hook) |
| 1345 | (set-buffer (find-file-noselect filen))) |
| 1346 | (set-buffer obuf) |
| 1347 | ;; separately for each file, in sync with post-command hooks, |
| 1348 | ;; with the new buffer current: |
| 1349 | (run-hooks 'pre-command-hook) |
| 1350 | (cond ((file-exists-p filen) |
| 1351 | (when (not (verify-visited-file-modtime obuf)) |
| 1352 | (revert-buffer t nil))) |
| 1353 | (t |
| 1354 | (when (y-or-n-p |
| 1355 | (concat "File no longer exists: " filen |
| 1356 | ", write buffer to file? ")) |
| 1357 | (write-file filen)))) |
| 1358 | (unless server-buffer-clients |
| 1359 | (setq server-existing-buffer t))) |
| 1360 | (server-goto-line-column (cdr file)) |
| 1361 | (run-hooks 'server-visit-hook) |
| 1362 | ;; hooks may be specific to current buffer: |
| 1363 | (run-hooks 'post-command-hook)) |
| 1364 | (unless nowait |
| 1365 | ;; When the buffer is killed, inform the clients. |
| 1366 | (add-hook 'kill-buffer-hook 'server-kill-buffer nil t) |
| 1367 | (push proc server-buffer-clients)) |
| 1368 | (push (current-buffer) client-record))) |
| 1369 | (unless nowait |
| 1370 | (process-put proc 'buffers |
| 1371 | (nconc (process-get proc 'buffers) client-record))) |
| 1372 | client-record)) |
| 1373 | |
| 1374 | (defvar server-kill-buffer-running nil |
| 1375 | "Non-nil while `server-kill-buffer' or `server-buffer-done' is running.") |
| 1376 | |
| 1377 | (defun server-buffer-done (buffer &optional for-killing) |
| 1378 | "Mark BUFFER as \"done\" for its client(s). |
| 1379 | This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED). |
| 1380 | NEXT-BUFFER is another server buffer, as a suggestion for what to select next, |
| 1381 | or nil. KILLED is t if we killed BUFFER (typically, because it was visiting |
| 1382 | a temp file). |
| 1383 | FOR-KILLING if non-nil indicates that we are called from `kill-buffer'." |
| 1384 | (let ((next-buffer nil) |
| 1385 | (killed nil)) |
| 1386 | (dolist (proc server-clients) |
| 1387 | (let ((buffers (process-get proc 'buffers))) |
| 1388 | (or next-buffer |
| 1389 | (setq next-buffer (nth 1 (memq buffer buffers)))) |
| 1390 | (when buffers ; Ignore bufferless clients. |
| 1391 | (setq buffers (delq buffer buffers)) |
| 1392 | ;; Delete all dead buffers from PROC. |
| 1393 | (dolist (b buffers) |
| 1394 | (and (bufferp b) |
| 1395 | (not (buffer-live-p b)) |
| 1396 | (setq buffers (delq b buffers)))) |
| 1397 | (process-put proc 'buffers buffers) |
| 1398 | ;; If client now has no pending buffers, |
| 1399 | ;; tell it that it is done, and forget it entirely. |
| 1400 | (unless buffers |
| 1401 | (server-log "Close" proc) |
| 1402 | (if for-killing |
| 1403 | ;; `server-delete-client' might delete the client's |
| 1404 | ;; frames, which might change the current buffer. We |
| 1405 | ;; don't want that (bug#640). |
| 1406 | (save-current-buffer |
| 1407 | (server-delete-client proc)) |
| 1408 | (server-delete-client proc)))))) |
| 1409 | (when (and (bufferp buffer) (buffer-name buffer)) |
| 1410 | ;; We may or may not kill this buffer; |
| 1411 | ;; if we do, do not call server-buffer-done recursively |
| 1412 | ;; from kill-buffer-hook. |
| 1413 | (let ((server-kill-buffer-running t)) |
| 1414 | (with-current-buffer buffer |
| 1415 | (setq server-buffer-clients nil) |
| 1416 | (run-hooks 'server-done-hook)) |
| 1417 | ;; Notice whether server-done-hook killed the buffer. |
| 1418 | (if (null (buffer-name buffer)) |
| 1419 | (setq killed t) |
| 1420 | ;; Don't bother killing or burying the buffer |
| 1421 | ;; when we are called from kill-buffer. |
| 1422 | (unless for-killing |
| 1423 | (when (and (not killed) |
| 1424 | server-kill-new-buffers |
| 1425 | (with-current-buffer buffer |
| 1426 | (not server-existing-buffer))) |
| 1427 | (setq killed t) |
| 1428 | (bury-buffer buffer) |
| 1429 | ;; Prevent kill-buffer from prompting (Bug#3696). |
| 1430 | (with-current-buffer buffer |
| 1431 | (set-buffer-modified-p nil)) |
| 1432 | (kill-buffer buffer)) |
| 1433 | (unless killed |
| 1434 | (if (server-temp-file-p buffer) |
| 1435 | (progn |
| 1436 | (with-current-buffer buffer |
| 1437 | (set-buffer-modified-p nil)) |
| 1438 | (kill-buffer buffer) |
| 1439 | (setq killed t)) |
| 1440 | (bury-buffer buffer))))))) |
| 1441 | (list next-buffer killed))) |
| 1442 | |
| 1443 | (defun server-temp-file-p (&optional buffer) |
| 1444 | "Return non-nil if BUFFER contains a file considered temporary. |
| 1445 | These are files whose names suggest they are repeatedly |
| 1446 | reused to pass information to another program. |
| 1447 | |
| 1448 | The variable `server-temp-file-regexp' controls which filenames |
| 1449 | are considered temporary." |
| 1450 | (and (buffer-file-name buffer) |
| 1451 | (string-match-p server-temp-file-regexp (buffer-file-name buffer)))) |
| 1452 | |
| 1453 | (defun server-done () |
| 1454 | "Offer to save current buffer, mark it as \"done\" for clients. |
| 1455 | This kills or buries the buffer, then returns a list |
| 1456 | of the form (NEXT-BUFFER KILLED). NEXT-BUFFER is another server buffer, |
| 1457 | as a suggestion for what to select next, or nil. |
| 1458 | KILLED is t if we killed BUFFER, which happens if it was created |
| 1459 | specifically for the clients and did not exist before their request for it." |
| 1460 | (when server-buffer-clients |
| 1461 | (if (server-temp-file-p) |
| 1462 | ;; For a temp file, save, and do make a non-numeric backup |
| 1463 | ;; (unless make-backup-files is nil). |
| 1464 | (let ((version-control nil) |
| 1465 | (buffer-backed-up nil)) |
| 1466 | (save-buffer)) |
| 1467 | (when (and (buffer-modified-p) |
| 1468 | buffer-file-name |
| 1469 | (y-or-n-p (concat "Save file " buffer-file-name "? "))) |
| 1470 | (save-buffer))) |
| 1471 | (server-buffer-done (current-buffer)))) |
| 1472 | |
| 1473 | (defun server-kill-emacs-query-function () |
| 1474 | "Ask before exiting Emacs if it has live clients." |
| 1475 | (or (not server-clients) |
| 1476 | (let (live-client) |
| 1477 | (dolist (proc server-clients) |
| 1478 | (when (memq t (mapcar 'buffer-live-p (process-get |
| 1479 | proc 'buffers))) |
| 1480 | (setq live-client t))) |
| 1481 | live-client) |
| 1482 | (yes-or-no-p "This Emacs session has clients; exit anyway? "))) |
| 1483 | |
| 1484 | (defun server-kill-buffer () |
| 1485 | "Remove the current buffer from its clients' buffer list. |
| 1486 | Designed to be added to `kill-buffer-hook'." |
| 1487 | ;; Prevent infinite recursion if user has made server-done-hook |
| 1488 | ;; call kill-buffer. |
| 1489 | (or server-kill-buffer-running |
| 1490 | (and server-buffer-clients |
| 1491 | (let ((server-kill-buffer-running t)) |
| 1492 | (when server-process |
| 1493 | (server-buffer-done (current-buffer) t)))))) |
| 1494 | \f |
| 1495 | (defun server-edit (&optional arg) |
| 1496 | "Switch to next server editing buffer; say \"Done\" for current buffer. |
| 1497 | If a server buffer is current, it is marked \"done\" and optionally saved. |
| 1498 | The buffer is also killed if it did not exist before the clients asked for it. |
| 1499 | When all of a client's buffers are marked as \"done\", the client is notified. |
| 1500 | |
| 1501 | Temporary files such as MH <draft> files are always saved and backed up, |
| 1502 | no questions asked. (The variable `make-backup-files', if nil, still |
| 1503 | inhibits a backup; you can set it locally in a particular buffer to |
| 1504 | prevent a backup for it.) The variable `server-temp-file-regexp' controls |
| 1505 | which filenames are considered temporary. |
| 1506 | |
| 1507 | If invoked with a prefix argument, or if there is no server process running, |
| 1508 | starts server process and that is all. Invoked by \\[server-edit]." |
| 1509 | (interactive "P") |
| 1510 | (cond |
| 1511 | ((or arg |
| 1512 | (not server-process) |
| 1513 | (memq (process-status server-process) '(signal exit))) |
| 1514 | (server-mode 1)) |
| 1515 | (server-clients (apply 'server-switch-buffer (server-done))) |
| 1516 | (t (message "No server editing buffers exist")))) |
| 1517 | |
| 1518 | (defun server-switch-buffer (&optional next-buffer killed-one filepos) |
| 1519 | "Switch to another buffer, preferably one that has a client. |
| 1520 | Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it. |
| 1521 | |
| 1522 | KILLED-ONE is t in a recursive call if we have already killed one |
| 1523 | temp-file server buffer. This means we should avoid the final |
| 1524 | \"switch to some other buffer\" since we've already effectively |
| 1525 | done that. |
| 1526 | |
| 1527 | FILEPOS specifies a new buffer position for NEXT-BUFFER, if we |
| 1528 | visit NEXT-BUFFER in an existing window. If non-nil, it should |
| 1529 | be a cons cell (LINENUMBER . COLUMNNUMBER)." |
| 1530 | (if (null next-buffer) |
| 1531 | (progn |
| 1532 | (let ((rest server-clients)) |
| 1533 | (while (and rest (not next-buffer)) |
| 1534 | (let ((proc (car rest))) |
| 1535 | ;; Only look at frameless clients, or those in the selected |
| 1536 | ;; frame. |
| 1537 | (when (or (not (process-get proc 'frame)) |
| 1538 | (eq (process-get proc 'frame) (selected-frame))) |
| 1539 | (setq next-buffer (car (process-get proc 'buffers)))) |
| 1540 | (setq rest (cdr rest))))) |
| 1541 | (and next-buffer (server-switch-buffer next-buffer killed-one)) |
| 1542 | (unless (or next-buffer killed-one (window-dedicated-p)) |
| 1543 | ;; (switch-to-buffer (other-buffer)) |
| 1544 | (message "No server buffers remain to edit"))) |
| 1545 | (if (not (buffer-live-p next-buffer)) |
| 1546 | ;; If NEXT-BUFFER is a dead buffer, remove the server records for it |
| 1547 | ;; and try the next surviving server buffer. |
| 1548 | (apply 'server-switch-buffer (server-buffer-done next-buffer)) |
| 1549 | ;; OK, we know next-buffer is live, let's display and select it. |
| 1550 | (if (functionp server-window) |
| 1551 | (funcall server-window next-buffer) |
| 1552 | (let ((win (get-buffer-window next-buffer 0))) |
| 1553 | (if (and win (not server-window)) |
| 1554 | ;; The buffer is already displayed: just reuse the |
| 1555 | ;; window. If FILEPOS is non-nil, use it to replace the |
| 1556 | ;; window's own value of point. |
| 1557 | (progn |
| 1558 | (select-window win) |
| 1559 | (set-buffer next-buffer) |
| 1560 | (when filepos |
| 1561 | (server-goto-line-column filepos))) |
| 1562 | ;; Otherwise, let's find an appropriate window. |
| 1563 | (cond ((window-live-p server-window) |
| 1564 | (select-window server-window)) |
| 1565 | ((framep server-window) |
| 1566 | (unless (frame-live-p server-window) |
| 1567 | (setq server-window (make-frame))) |
| 1568 | (select-window (frame-selected-window server-window)))) |
| 1569 | (when (window-minibuffer-p) |
| 1570 | (select-window (next-window nil 'nomini 0))) |
| 1571 | ;; Move to a non-dedicated window, if we have one. |
| 1572 | (when (window-dedicated-p) |
| 1573 | (select-window |
| 1574 | (get-window-with-predicate |
| 1575 | (lambda (w) |
| 1576 | (and (not (window-dedicated-p w)) |
| 1577 | (equal (frame-terminal (window-frame w)) |
| 1578 | (frame-terminal)))) |
| 1579 | 'nomini 'visible (selected-window)))) |
| 1580 | (condition-case nil |
| 1581 | (switch-to-buffer next-buffer) |
| 1582 | ;; After all the above, we might still have ended up with |
| 1583 | ;; a minibuffer/dedicated-window (if there's no other). |
| 1584 | (error (pop-to-buffer next-buffer))))))) |
| 1585 | (when server-raise-frame |
| 1586 | (select-frame-set-input-focus (window-frame))))) |
| 1587 | |
| 1588 | ;;;###autoload |
| 1589 | (defun server-save-buffers-kill-terminal (arg) |
| 1590 | ;; Called from save-buffers-kill-terminal in files.el. |
| 1591 | "Offer to save each buffer, then kill the current client. |
| 1592 | With ARG non-nil, silently save all file-visiting buffers, then kill. |
| 1593 | |
| 1594 | If emacsclient was started with a list of filenames to edit, then |
| 1595 | only these files will be asked to be saved." |
| 1596 | (let ((proc (frame-parameter nil 'client))) |
| 1597 | (cond ((eq proc 'nowait) |
| 1598 | ;; Nowait frames have no client buffer list. |
| 1599 | (if (cdr (frame-list)) |
| 1600 | (progn (save-some-buffers arg) |
| 1601 | (delete-frame)) |
| 1602 | ;; If we're the last frame standing, kill Emacs. |
| 1603 | (save-buffers-kill-emacs arg))) |
| 1604 | ((processp proc) |
| 1605 | (let ((buffers (process-get proc 'buffers))) |
| 1606 | ;; If client is bufferless, emulate a normal Emacs exit |
| 1607 | ;; and offer to save all buffers. Otherwise, offer to |
| 1608 | ;; save only the buffers belonging to the client. |
| 1609 | (save-some-buffers |
| 1610 | arg (if buffers |
| 1611 | (lambda () (memq (current-buffer) buffers)) |
| 1612 | t)) |
| 1613 | (server-delete-client proc))) |
| 1614 | (t (error "Invalid client frame"))))) |
| 1615 | |
| 1616 | (define-key ctl-x-map "#" 'server-edit) |
| 1617 | |
| 1618 | (defun server-unload-function () |
| 1619 | "Unload the Server library." |
| 1620 | (server-mode -1) |
| 1621 | (substitute-key-definition 'server-edit nil ctl-x-map) |
| 1622 | (save-current-buffer |
| 1623 | (dolist (buffer (buffer-list)) |
| 1624 | (set-buffer buffer) |
| 1625 | (remove-hook 'kill-buffer-hook 'server-kill-buffer t))) |
| 1626 | ;; continue standard unloading |
| 1627 | nil) |
| 1628 | |
| 1629 | (defun server-eval-at (server form) |
| 1630 | "Contact the Emacs server named SERVER and evaluate FORM there. |
| 1631 | Returns the result of the evaluation, or signals an error if it |
| 1632 | cannot contact the specified server. For example: |
| 1633 | (server-eval-at \"server\" '(emacs-pid)) |
| 1634 | returns the process ID of the Emacs instance running \"server\"." |
| 1635 | (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir)) |
| 1636 | (server-file (expand-file-name server server-dir)) |
| 1637 | (coding-system-for-read 'binary) |
| 1638 | (coding-system-for-write 'binary) |
| 1639 | address port secret process) |
| 1640 | (unless (file-exists-p server-file) |
| 1641 | (error "No such server: %s" server)) |
| 1642 | (with-temp-buffer |
| 1643 | (when server-use-tcp |
| 1644 | (let ((coding-system-for-read 'no-conversion)) |
| 1645 | (insert-file-contents server-file) |
| 1646 | (unless (looking-at "\\([0-9.]+\\):\\([0-9]+\\)") |
| 1647 | (error "Invalid auth file")) |
| 1648 | (setq address (match-string 1) |
| 1649 | port (string-to-number (match-string 2))) |
| 1650 | (forward-line 1) |
| 1651 | (setq secret (buffer-substring (point) (line-end-position))) |
| 1652 | (erase-buffer))) |
| 1653 | (unless (setq process (make-network-process |
| 1654 | :name "eval-at" |
| 1655 | :buffer (current-buffer) |
| 1656 | :host address |
| 1657 | :service (if server-use-tcp port server-file) |
| 1658 | :family (if server-use-tcp 'ipv4 'local) |
| 1659 | :noquery t)) |
| 1660 | (error "Unable to contact the server")) |
| 1661 | (if server-use-tcp |
| 1662 | (process-send-string process (concat "-auth " secret "\n"))) |
| 1663 | (process-send-string process |
| 1664 | (concat "-eval " |
| 1665 | (server-quote-arg (format "%S" form)) |
| 1666 | "\n")) |
| 1667 | (while (memq (process-status process) '(open run)) |
| 1668 | (accept-process-output process 0 10)) |
| 1669 | (goto-char (point-min)) |
| 1670 | ;; If the result is nil, there's nothing in the buffer. If the |
| 1671 | ;; result is non-nil, it's after "-print ". |
| 1672 | (let ((answer "")) |
| 1673 | (while (re-search-forward "\n-print\\(-nonl\\)? " nil t) |
| 1674 | (setq answer |
| 1675 | (concat answer |
| 1676 | (buffer-substring (point) |
| 1677 | (progn (skip-chars-forward "^\n") |
| 1678 | (point)))))) |
| 1679 | (if (not (equal answer "")) |
| 1680 | (read (decode-coding-string (server-unquote-arg answer) |
| 1681 | 'emacs-internal))))))) |
| 1682 | |
| 1683 | \f |
| 1684 | (provide 'server) |
| 1685 | |
| 1686 | ;;; server.el ends here |