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