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