Add arch tagline
[emacs.git] / lisp / server.el
bloba1d34eb81284c7a6b54f1440d24fc76e86715b02
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 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 nil
208 "The directory in which to place the server socket.
209 Initialized by `server-start'.")
211 (defun server-clients-with (property value)
212 "Return a list of clients with PROPERTY set to VALUE."
213 (let (result)
214 (dolist (proc server-clients result)
215 (when (equal value (process-get proc property))
216 (push proc result)))))
218 (defun server-add-client (proc)
219 "Create a client for process PROC, if it doesn't already have one.
220 New clients have no properties."
221 (add-to-list 'server-clients proc))
223 (defmacro server-with-environment (env vars &rest body)
224 "Evaluate BODY with environment variables VARS set to those in ENV.
225 The environment variables are then restored to their previous values.
227 VARS should be a list of strings.
228 ENV should be in the same format as `process-environment'."
229 (declare (indent 2))
230 (let ((var (make-symbol "var"))
231 (value (make-symbol "value")))
232 `(let ((process-environment process-environment))
233 (dolist (,var ,vars)
234 (let ((,value (getenv-internal ,var ,env)))
235 (push (if (null ,value)
236 ,var
237 (concat ,var "=" ,value))
238 process-environment)))
239 (progn ,@body))))
241 (defun server-delete-client (proc &optional noframe)
242 "Delete PROC, including its buffers, terminals and frames.
243 If NOFRAME is non-nil, let the frames live. (To be used from
244 `delete-frame-functions'.)"
245 (server-log (concat "server-delete-client" (if noframe " noframe"))
246 proc)
247 ;; Force a new lookup of client (prevents infinite recursion).
248 (when (memq proc server-clients)
249 (let ((buffers (process-get proc 'buffers)))
251 ;; Kill the client's buffers.
252 (dolist (buf buffers)
253 (when (buffer-live-p buf)
254 (with-current-buffer buf
255 ;; Kill the buffer if necessary.
256 (when (and (equal server-buffer-clients
257 (list proc))
258 (or (and server-kill-new-buffers
259 (not server-existing-buffer))
260 (server-temp-file-p))
261 (not (buffer-modified-p)))
262 (let (flag)
263 (unwind-protect
264 (progn (setq server-buffer-clients nil)
265 (kill-buffer (current-buffer))
266 (setq flag t))
267 (unless flag
268 ;; Restore clients if user pressed C-g in `kill-buffer'.
269 (setq server-buffer-clients (list proc)))))))))
271 ;; Delete the client's frames.
272 (unless noframe
273 (dolist (frame (frame-list))
274 (when (and (frame-live-p frame)
275 (equal proc (frame-parameter frame 'client)))
276 ;; Prevent `server-handle-delete-frame' from calling us
277 ;; recursively.
278 (set-frame-parameter frame 'client nil)
279 (delete-frame frame))))
281 (setq server-clients (delq proc server-clients))
283 ;; Delete the client's tty.
284 (let ((terminal (process-get proc 'terminal)))
285 ;; Only delete the terminal if it is non-nil.
286 (when (and terminal (eq (terminal-live-p terminal) t))
287 (delete-terminal terminal)))
289 ;; Delete the client's process.
290 (if (eq (process-status proc) 'open)
291 (delete-process proc))
293 (server-log "Deleted" proc))))
295 (defun server-log (string &optional client)
296 "If a *server* buffer exists, write STRING to it for logging purposes.
297 If CLIENT is non-nil, add a description of it to the logged message."
298 (when (get-buffer "*server*")
299 (with-current-buffer "*server*"
300 (goto-char (point-max))
301 (insert (current-time-string)
302 (cond
303 ((null client) " ")
304 ((listp client) (format " %s: " (car client)))
305 (t (format " %s: " client)))
306 string)
307 (or (bolp) (newline)))))
309 (defun server-sentinel (proc msg)
310 "The process sentinel for Emacs server connections."
311 ;; If this is a new client process, set the query-on-exit flag to nil
312 ;; for this process (it isn't inherited from the server process).
313 (when (and (eq (process-status proc) 'open)
314 (process-query-on-exit-flag proc))
315 (set-process-query-on-exit-flag proc nil))
316 ;; Delete the associated connection file, if applicable.
317 ;; This is actually problematic: the file may have been overwritten by
318 ;; another Emacs server in the mean time, so it's not ours any more.
319 ;; (and (process-contact proc :server)
320 ;; (eq (process-status proc) 'closed)
321 ;; (ignore-errors (delete-file (process-get proc :server-file))))
322 (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc)
323 (server-delete-client proc))
325 (defun server-select-display (display)
326 ;; If the current frame is on `display' we're all set.
327 ;; Similarly if we are unable to open a frames on other displays, there's
328 ;; nothing more we can do.
329 (unless (or (not (fboundp 'make-frame-on-display))
330 (equal (frame-parameter (selected-frame) 'display) display))
331 ;; Otherwise, look for an existing frame there and select it.
332 (dolist (frame (frame-list))
333 (when (equal (frame-parameter frame 'display) display)
334 (select-frame frame)))
335 ;; If there's no frame on that display yet, create and select one.
336 (unless (equal (frame-parameter (selected-frame) 'display) display)
337 (let* ((buffer (generate-new-buffer " *server-dummy*"))
338 (frame (make-frame-on-display
339 display
340 ;; Make it display (and remember) some dummy buffer, so
341 ;; we can detect later if the frame is in use or not.
342 `((server-dummmy-buffer . ,buffer)
343 ;; This frame may be deleted later (see
344 ;; server-unselect-display) so we want it to be as
345 ;; unobtrusive as possible.
346 (visibility . nil)))))
347 (select-frame frame)
348 (set-window-buffer (selected-window) buffer)
349 frame))))
351 (defun server-unselect-display (frame)
352 (when (frame-live-p frame)
353 ;; If the temporary frame is in use (displays something real), make it
354 ;; visible. If not (which can happen if the user's customizations call
355 ;; pop-to-buffer etc.), delete it to avoid preserving the connection after
356 ;; the last real frame is deleted.
357 (if (and (eq (frame-first-window frame)
358 (next-window (frame-first-window frame) 'nomini))
359 (eq (window-buffer (frame-first-window frame))
360 (frame-parameter frame 'server-dummy-buffer)))
361 ;; The temp frame still only shows one buffer, and that is the
362 ;; internal temp buffer.
363 (delete-frame frame)
364 (set-frame-parameter frame 'visibility t))
365 (kill-buffer (frame-parameter frame 'server-dummy-buffer))
366 (set-frame-parameter frame 'server-dummy-buffer nil)))
368 (defun server-handle-delete-frame (frame)
369 "Delete the client connection when the emacsclient frame is deleted."
370 (let ((proc (frame-parameter frame 'client)))
371 (when (and (frame-live-p frame)
372 proc
373 ;; See if this is the last frame for this client.
374 (>= 1 (let ((frame-num 0))
375 (dolist (f (frame-list))
376 (when (eq proc (frame-parameter f 'client))
377 (setq frame-num (1+ frame-num))))
378 frame-num)))
379 (server-log (format "server-handle-delete-frame, frame %s" frame) proc)
380 (server-delete-client proc 'noframe)))) ; Let delete-frame delete the frame later.
382 (defun server-handle-suspend-tty (terminal)
383 "Notify the emacsclient process to suspend itself when its tty device is suspended."
384 (dolist (proc (server-clients-with 'terminal terminal))
385 (server-log (format "server-handle-suspend-tty, terminal %s" terminal) proc)
386 (condition-case err
387 (server-send-string proc "-suspend \n")
388 (file-error ;The pipe/socket was closed.
389 (ignore-errors (server-delete-client proc))))))
391 (defun server-unquote-arg (arg)
392 "Remove &-quotation from ARG.
393 See `server-quote-arg' and `server-process-filter'."
394 (replace-regexp-in-string
395 "&." (lambda (s)
396 (case (aref s 1)
397 (?& "&")
398 (?- "-")
399 (?n "\n")
400 (t " ")))
401 arg t t))
403 (defun server-quote-arg (arg)
404 "In ARG, insert a & before each &, each space, each newline, and -.
405 Change spaces to underscores, too, so that the return value never
406 contains a space.
408 See `server-unquote-arg' and `server-process-filter'."
409 (replace-regexp-in-string
410 "[-&\n ]" (lambda (s)
411 (case (aref s 0)
412 (?& "&&")
413 (?- "&-")
414 (?\n "&n")
415 (?\s "&_")))
416 arg t t))
418 (defun server-send-string (proc string)
419 "A wrapper around `proc-send-string' for logging."
420 (server-log (concat "Sent " string) proc)
421 (process-send-string proc string))
423 (defun server-ensure-safe-dir (dir)
424 "Make sure DIR is a directory with no race-condition issues.
425 Creates the directory if necessary and makes sure:
426 - there's no symlink involved
427 - it's owned by us
428 - it's not readable/writable by anybody else."
429 (setq dir (directory-file-name dir))
430 (let ((attrs (file-attributes dir)))
431 (unless attrs
432 (letf (((default-file-modes) ?\700)) (make-directory dir t))
433 (setq attrs (file-attributes dir)))
434 ;; Check that it's safe for use.
435 (unless (and (eq t (car attrs)) (eql (nth 2 attrs) (user-uid))
436 (or (eq system-type 'windows-nt)
437 (zerop (logand ?\077 (file-modes dir)))))
438 (error "The directory %s is unsafe" dir))))
440 ;;;###autoload
441 (defun server-start (&optional leave-dead)
442 "Allow this Emacs process to be a server for client processes.
443 This starts a server communications subprocess through which
444 client \"editors\" can send your editing commands to this Emacs
445 job. To use the server, set up the program `emacsclient' in the
446 Emacs distribution as your standard \"editor\".
448 Optional argument LEAVE-DEAD (interactively, a prefix arg) means just
449 kill any existing server communications subprocess."
450 (interactive "P")
451 (when (or
452 (not server-clients)
453 (yes-or-no-p
454 "The current server still has clients; delete them? "))
455 ;; It is safe to get the user id now.
456 (setq server-socket-dir (or server-socket-dir
457 (format "/tmp/emacs%d" (user-uid))))
458 (when server-process
459 ;; kill it dead!
460 (ignore-errors (delete-process server-process)))
461 ;; Delete the socket files made by previous server invocations.
462 (condition-case ()
463 (delete-file (expand-file-name server-name server-socket-dir))
464 (error nil))
465 ;; If this Emacs already had a server, clear out associated status.
466 (while server-clients
467 (server-delete-client (car server-clients)))
468 ;; Now any previous server is properly stopped.
469 (if leave-dead
470 (progn
471 (server-log (message "Server stopped"))
472 (setq server-process nil))
473 (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir))
474 (server-file (expand-file-name server-name server-dir)))
475 ;; Make sure there is a safe directory in which to place the socket.
476 (server-ensure-safe-dir server-dir)
477 ;; Remove any leftover socket or authentication file.
478 (ignore-errors (delete-file server-file))
479 (when server-process
480 (server-log (message "Restarting server")))
481 (letf (((default-file-modes) ?\700))
482 (add-hook 'suspend-tty-functions 'server-handle-suspend-tty)
483 (add-hook 'delete-frame-functions 'server-handle-delete-frame)
484 (add-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)
485 (add-hook 'kill-emacs-query-functions 'server-kill-emacs-query-function)
486 (setq server-process
487 (apply #'make-network-process
488 :name server-name
489 :server t
490 :noquery t
491 :sentinel 'server-sentinel
492 :filter 'server-process-filter
493 ;; We must receive file names without being decoded.
494 ;; Those are decoded by server-process-filter according
495 ;; to file-name-coding-system.
496 :coding 'raw-text
497 ;; The rest of the args depends on the kind of socket used.
498 (if server-use-tcp
499 (list :family nil
500 :service t
501 :host (or server-host 'local)
502 :plist '(:authenticated nil))
503 (list :family 'local
504 :service server-file
505 :plist '(:authenticated t)))))
506 (unless server-process (error "Could not start server process"))
507 (when server-use-tcp
508 (let ((auth-key
509 (loop
510 ;; The auth key is a 64-byte string of random chars in the
511 ;; range `!'..`~'.
512 for i below 64
513 collect (+ 33 (random 94)) into auth
514 finally return (concat auth))))
515 (process-put server-process :auth-key auth-key)
516 (with-temp-file server-file
517 (set-buffer-multibyte nil)
518 (setq buffer-file-coding-system 'no-conversion)
519 (insert (format-network-address
520 (process-contact server-process :local))
521 " " (int-to-string (emacs-pid))
522 "\n" auth-key)))))))))
524 (defun server-running-p (&optional name)
525 "Test whether server NAME is running."
526 (interactive
527 (list (if current-prefix-arg
528 (read-string "Server name: " nil nil server-name))))
529 (unless name (setq name server-name))
530 (condition-case nil
531 (progn
532 (delete-process
533 (make-network-process
534 :name "server-client-test" :family 'local :server nil :noquery t
535 :service (expand-file-name name server-socket-dir)))
537 (file-error nil)))
539 ;;;###autoload
540 (define-minor-mode server-mode
541 "Toggle Server mode.
542 With ARG, turn Server mode on if ARG is positive, off otherwise.
543 Server mode runs a process that accepts commands from the
544 `emacsclient' program. See `server-start' and Info node `Emacs server'."
545 :global t
546 :group 'server
547 :version "22.1"
548 ;; Fixme: Should this check for an existing server socket and do
549 ;; nothing if there is one (for multiple Emacs sessions)?
550 (server-start (not server-mode)))
552 (defun server-eval-and-print (expr proc)
553 "Eval EXPR and send the result back to client PROC."
554 (let ((v (eval (car (read-from-string expr)))))
555 (when (and v proc)
556 (with-temp-buffer
557 (let ((standard-output (current-buffer)))
558 (pp v)
559 (let ((text (buffer-substring-no-properties
560 (point-min) (point-max))))
561 (server-send-string
562 proc (format "-print %s\n"
563 (server-quote-arg text)))))))))
565 (defun server-create-tty-frame (tty type proc)
566 (add-to-list 'frame-inherited-parameters 'client)
567 (let ((frame
568 (server-with-environment (process-get proc 'env)
569 '("LANG" "LC_CTYPE" "LC_ALL"
570 ;; For tgetent(3); list according to ncurses(3).
571 "BAUDRATE" "COLUMNS" "ESCDELAY" "HOME" "LINES"
572 "NCURSES_ASSUMED_COLORS" "NCURSES_NO_PADDING"
573 "NCURSES_NO_SETBUF" "TERM" "TERMCAP" "TERMINFO"
574 "TERMINFO_DIRS" "TERMPATH"
575 ;; rxvt wants these
576 "COLORFGBG" "COLORTERM")
577 (make-frame-on-tty tty type
578 ;; Ignore nowait here; we always need to
579 ;; clean up opened ttys when the client dies.
580 `((client . ,proc)
581 ;; This is a leftover from an earlier
582 ;; attempt at making it possible for process
583 ;; run in the server process to use the
584 ;; environment of the client process.
585 ;; It has no effect now and to make it work
586 ;; we'd need to decide how to make
587 ;; process-environment interact with client
588 ;; envvars, and then to change the
589 ;; C functions `child_setup' and
590 ;; `getenv_internal' accordingly.
591 (environment . ,(process-get proc 'env)))))))
593 ;; ttys don't use the `display' parameter, but callproc.c does to set
594 ;; the DISPLAY environment on subprocesses.
595 (set-frame-parameter frame 'display
596 (getenv-internal "DISPLAY" (process-get proc 'env)))
597 (select-frame frame)
598 (process-put proc 'frame frame)
599 (process-put proc 'terminal (frame-terminal frame))
601 ;; Display *scratch* by default.
602 (switch-to-buffer (get-buffer-create "*scratch*") 'norecord)
604 ;; Reply with our pid.
605 (server-send-string proc (concat "-emacs-pid "
606 (number-to-string (emacs-pid)) "\n"))
607 frame))
609 (defun server-create-window-system-frame (display nowait proc)
610 (add-to-list 'frame-inherited-parameters 'client)
611 (if (not (fboundp 'make-frame-on-display))
612 (progn
613 ;; This emacs does not support X.
614 (server-log "Window system unsupported" proc)
615 (server-send-string proc "-window-system-unsupported \n")
616 nil)
617 ;; Flag frame as client-created, but use a dummy client.
618 ;; This will prevent the frame from being deleted when
619 ;; emacsclient quits while also preventing
620 ;; `server-save-buffers-kill-terminal' from unexpectedly
621 ;; killing emacs on that frame.
622 (let* ((params `((client . ,(if nowait 'nowait proc))
623 ;; This is a leftover, see above.
624 (environment . ,(process-get proc 'env))))
625 (frame (make-frame-on-display
626 (or display
627 (frame-parameter nil 'display)
628 (getenv "DISPLAY")
629 (error "Please specify display"))
630 params)))
631 (server-log (format "%s created" frame) proc)
632 ;; XXX We need to ensure the parameters are really set because Emacs
633 ;; forgets unhandled initialization parameters for X frames at
634 ;; the moment.
635 (modify-frame-parameters frame params)
636 (select-frame frame)
637 (process-put proc 'frame frame)
638 (process-put proc 'terminal (frame-terminal frame))
640 ;; Display *scratch* by default.
641 (switch-to-buffer (get-buffer-create "*scratch*") 'norecord)
642 frame)))
645 (defun server-goto-toplevel (proc)
646 (condition-case nil
647 ;; If we're running isearch, we must abort it to allow Emacs to
648 ;; display the buffer and switch to it.
649 (dolist (buffer (buffer-list))
650 (with-current-buffer buffer
651 (when (bound-and-true-p isearch-mode)
652 (isearch-cancel))))
653 ;; Signaled by isearch-cancel.
654 (quit (message nil)))
655 (when (> (recursion-depth) 0)
656 ;; We're inside a minibuffer already, so if the emacs-client is trying
657 ;; to open a frame on a new display, we might end up with an unusable
658 ;; frame because input from that display will be blocked (until exiting
659 ;; the minibuffer). Better exit this minibuffer right away.
660 ;; Similarly with recursive-edits such as the splash screen.
661 (run-with-timer 0 nil (lexical-let ((proc proc))
662 (lambda () (server-execute-continuation proc))))
663 (top-level)))
665 ;; We use various special properties on process objects:
666 ;; - `env' stores the info about the environment of the emacsclient process.
667 ;; - `continuation' is a no-arg function that we need to execute. It contains
668 ;; commands we wanted to execute in some earlier invocation of the process
669 ;; filter but that we somehow were unable to process at that time
670 ;; (e.g. because we first need to throw to the toplevel).
672 (defun server-execute-continuation (proc)
673 (let ((continuation (process-get proc 'continuation)))
674 (process-put proc 'continuation nil)
675 (if continuation (ignore-errors (funcall continuation)))))
677 (defun* server-process-filter (proc string)
678 "Process a request from the server to edit some files.
679 PROC is the server process. STRING consists of a sequence of
680 commands prefixed by a dash. Some commands have arguments; these
681 are &-quoted and need to be decoded by `server-unquote-arg'. The
682 filter parses and executes these commands.
684 To illustrate the protocol, here is an example command that
685 emacsclient sends to create a new X frame (note that the whole
686 sequence is sent on a single line):
688 -env HOME /home/lorentey
689 -env DISPLAY :0.0
690 ... lots of other -env commands
691 -display :0.0
692 -window-system
694 The following commands are accepted by the server:
696 `-auth AUTH-STRING'
697 Authenticate the client using the secret authentication string
698 AUTH-STRING.
700 `-env NAME=VALUE'
701 An environment variable on the client side.
703 `-dir DIRNAME'
704 The current working directory of the client process.
706 `-current-frame'
707 Forbid the creation of new frames.
709 `-nowait'
710 Request that the next frame created should not be
711 associated with this client.
713 `-display DISPLAY'
714 Set the display name to open X frames on.
716 `-position LINE[:COLUMN]'
717 Go to the given line and column number
718 in the next file opened.
720 `-file FILENAME'
721 Load the given file in the current frame.
723 `-eval EXPR'
724 Evaluate EXPR as a Lisp expression and return the
725 result in -print commands.
727 `-window-system'
728 Open a new X frame.
730 `-tty DEVICENAME TYPE'
731 Open a new tty frame at the client.
733 `-suspend'
734 Suspend this tty frame. The client sends this string in
735 response to SIGTSTP and SIGTTOU. The server must cease all I/O
736 on this tty until it gets a -resume command.
738 `-resume'
739 Resume this tty frame. The client sends this string when it
740 gets the SIGCONT signal and it is the foreground process on its
741 controlling tty.
743 `-ignore COMMENT'
744 Do nothing, but put the comment in the server
745 log. Useful for debugging.
748 The following commands are accepted by the client:
750 `-emacs-pid PID'
751 Describes the process id of the Emacs process;
752 used to forward window change signals to it.
754 `-window-system-unsupported'
755 Signals that the server does not support creating X frames;
756 the client must try again with a tty frame.
758 `-print STRING'
759 Print STRING on stdout. Used to send values
760 returned by -eval.
762 `-error DESCRIPTION'
763 Signal an error (but continue processing).
765 `-suspend'
766 Suspend this terminal, i.e., stop the client process.
767 Sent when the user presses C-z."
768 (server-log (concat "Received " string) proc)
769 ;; First things first: let's check the authentication
770 (unless (process-get proc :authenticated)
771 (if (and (string-match "-auth \\(.*?\\)\n" string)
772 (equal (match-string 1 string) (process-get proc :auth-key)))
773 (progn
774 (setq string (substring string (match-end 0)))
775 (process-put proc :authenticated t)
776 (server-log "Authentication successful" proc))
777 (server-log "Authentication failed" proc)
778 (server-send-string
779 proc (concat "-error " (server-quote-arg "Authentication failed")))
780 (delete-process proc)
781 ;; We return immediately
782 (return-from server-process-filter)))
783 (let ((prev (process-get proc 'previous-string)))
784 (when prev
785 (setq string (concat prev string))
786 (process-put proc 'previous-string nil)))
787 (condition-case err
788 (progn
789 (server-add-client proc)
790 (if (not (string-match "\n" string))
791 ;; Save for later any partial line that remains.
792 (when (> (length string) 0)
793 (process-put proc 'previous-string string))
795 ;; In earlier versions of server.el (where we used an `emacsserver'
796 ;; process), there could be multiple lines. Nowadays this is not
797 ;; supported any more.
798 (assert (eq (match-end 0) (length string)))
799 (let ((request (substring string 0 (match-beginning 0)))
800 (coding-system (and default-enable-multibyte-characters
801 (or file-name-coding-system
802 default-file-name-coding-system)))
803 nowait ; t if emacsclient does not want to wait for us.
804 frame ; The frame that was opened for the client (if any).
805 display ; Open the frame on this display.
806 dontkill ; t if the client should not be killed.
807 (commands ())
809 (tty-name nil) ;nil, `window-system', or the tty name.
810 tty-type ;string.
811 (files nil)
812 (lineno 1)
813 (columnno 0))
814 ;; Remove this line from STRING.
815 (setq string (substring string (match-end 0)))
816 (while (string-match " *[^ ]* " request)
817 (let ((arg (substring request (match-beginning 0)
818 (1- (match-end 0)))))
819 (setq request (substring request (match-end 0)))
820 (cond
821 ;; -version CLIENT-VERSION: obsolete at birth.
822 ((and (equal "-version" arg) (string-match "[^ ]+ " request))
823 (setq request (substring request (match-end 0))))
825 ;; -nowait: Emacsclient won't wait for a result.
826 ((equal "-nowait" arg) (setq nowait t))
828 ;; -current-frame: Don't create frames.
829 ((equal "-current-frame" arg) (setq tty-name nil))
831 ;; -display DISPLAY:
832 ;; Open X frames on the given display instead of the default.
833 ((and (equal "-display" arg)
834 (string-match "\\([^ ]*\\) " request))
835 (setq display (match-string 1 request))
836 (setq request (substring request (match-end 0))))
838 ;; -window-system: Open a new X frame.
839 ((equal "-window-system" arg)
840 (setq dontkill t)
841 (setq tty-name 'window-system))
843 ;; -resume: Resume a suspended tty frame.
844 ((equal "-resume" arg)
845 (lexical-let ((terminal (process-get proc 'terminal)))
846 (setq dontkill t)
847 (push (lambda ()
848 (when (eq (terminal-live-p terminal) t)
849 (resume-tty terminal)))
850 commands)))
852 ;; -suspend: Suspend the client's frame. (In case we
853 ;; get out of sync, and a C-z sends a SIGTSTP to
854 ;; emacsclient.)
855 ((equal "-suspend" arg)
856 (lexical-let ((terminal (process-get proc 'terminal)))
857 (setq dontkill t)
858 (push (lambda ()
859 (when (eq (terminal-live-p terminal) t)
860 (suspend-tty terminal)))
861 commands)))
863 ;; -ignore COMMENT: Noop; useful for debugging emacsclient.
864 ;; (The given comment appears in the server log.)
865 ((and (equal "-ignore" arg) (string-match "[^ ]* " request))
866 (setq dontkill t
867 request (substring request (match-end 0))))
869 ;; -tty DEVICE-NAME TYPE: Open a new tty frame at the client.
870 ((and (equal "-tty" arg)
871 (string-match "\\([^ ]*\\) \\([^ ]*\\) " request))
872 (setq tty-name (match-string 1 request))
873 (setq tty-type (match-string 2 request))
874 (setq dontkill t)
875 (setq request (substring request (match-end 0))))
877 ;; -position LINE[:COLUMN]: Set point to the given
878 ;; position in the next file.
879 ((and (equal "-position" arg)
880 (string-match "\\+\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)? "
881 request))
882 (setq lineno (string-to-number (match-string 1 request))
883 columnno (if (null (match-end 2)) 0
884 (string-to-number (match-string 2 request)))
885 request (substring request (match-end 0))))
887 ;; -file FILENAME: Load the given file.
888 ((and (equal "-file" arg)
889 (string-match "\\([^ ]+\\) " request))
890 (let ((file (server-unquote-arg (match-string 1 request))))
891 (setq request (substring request (match-end 0)))
892 (if coding-system
893 (setq file (decode-coding-string file coding-system)))
894 (setq file (command-line-normalize-file-name file))
895 (push (list file lineno columnno) files)
896 (server-log (format "New file: %s (%d:%d)"
897 file lineno columnno) proc))
898 (setq lineno 1
899 columnno 0))
901 ;; -eval EXPR: Evaluate a Lisp expression.
902 ((and (equal "-eval" arg)
903 (string-match "\\([^ ]+\\) " request))
904 (lexical-let ((expr (server-unquote-arg
905 (match-string 1 request))))
906 (setq request (substring request (match-end 0)))
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 lineno 1
912 columnno 0)))
914 ;; -env NAME=VALUE: An environment variable.
915 ((and (equal "-env" arg) (string-match "\\([^ ]+\\) " request))
916 (let ((var (server-unquote-arg (match-string 1 request))))
917 ;; XXX Variables should be encoded as in getenv/setenv.
918 (setq request (substring request (match-end 0)))
919 (process-put proc 'env
920 (cons var (process-get proc 'env)))))
922 ;; -dir DIRNAME: The cwd of the emacsclient process.
923 ((and (equal "-dir" arg) (string-match "\\([^ ]+\\) " request))
924 (setq dir (server-unquote-arg (match-string 1 request)))
925 (setq request (substring request (match-end 0)))
926 (if coding-system
927 (setq dir (decode-coding-string dir coding-system)))
928 (setq dir (command-line-normalize-file-name dir)))
930 ;; Unknown command.
931 (t (error "Unknown command: %s" arg)))))
933 (setq frame
934 (case tty-name
935 ((nil) (if display (server-select-display display)))
936 ((window-system)
937 (server-create-window-system-frame display nowait proc))
938 (t (server-create-tty-frame tty-name tty-type proc))))
940 (process-put proc 'continuation
941 (lexical-let ((proc proc)
942 (files files)
943 (nowait nowait)
944 (commands commands)
945 (dontkill dontkill)
946 (frame frame)
947 (tty-name tty-name))
948 (lambda ()
949 (server-execute proc files nowait commands
950 dontkill frame tty-name))))
952 (when (or frame files)
953 (server-goto-toplevel proc))
955 (server-execute-continuation proc))))
956 ;; condition-case
957 (error (server-return-error proc err))))
959 (defun server-execute (proc files nowait commands dontkill frame tty-name)
960 (condition-case err
961 (let* ((buffers
962 (when files
963 (run-hooks 'pre-command-hook)
964 (prog1 (server-visit-files files proc nowait)
965 (run-hooks 'post-command-hook)))))
967 (mapc 'funcall (nreverse commands))
969 ;; Delete the client if necessary.
970 (cond
971 (nowait
972 ;; Client requested nowait; return immediately.
973 (server-log "Close nowait client" proc)
974 (server-delete-client proc))
975 ((and (not dontkill) (null buffers))
976 ;; This client is empty; get rid of it immediately.
977 (server-log "Close empty client" proc)
978 (server-delete-client proc)))
979 (cond
980 ((or isearch-mode (minibufferp))
981 nil)
982 ((and frame (null buffers))
983 (message "%s" (substitute-command-keys
984 "When done with this frame, type \\[delete-frame]")))
985 ((not (null buffers))
986 (server-switch-buffer (car buffers))
987 (run-hooks 'server-switch-hook)
988 (unless nowait
989 (message "%s" (substitute-command-keys
990 "When done with a buffer, type \\[server-edit]")))))
991 (when (and frame (null tty-name))
992 (server-unselect-display frame)))
993 (error (server-return-error proc err))))
995 (defun server-return-error (proc err)
996 (ignore-errors
997 (server-send-string
998 proc (concat "-error " (server-quote-arg
999 (error-message-string err))))
1000 (server-log (error-message-string err) proc)
1001 (delete-process proc)))
1003 (defun server-goto-line-column (file-line-col)
1004 "Move point to the position indicated in FILE-LINE-COL.
1005 FILE-LINE-COL should be a three-element list as described in
1006 `server-visit-files'."
1007 (goto-line (nth 1 file-line-col))
1008 (let ((column-number (nth 2 file-line-col)))
1009 (when (> column-number 0)
1010 (move-to-column (1- column-number)))))
1012 (defun server-visit-files (files proc &optional nowait)
1013 "Find FILES and return a list of buffers created.
1014 FILES is an alist whose elements are (FILENAME LINENUMBER COLUMNNUMBER).
1015 PROC is the client that requested this operation.
1016 NOWAIT non-nil means this client is not waiting for the results,
1017 so don't mark these buffers specially, just visit them normally."
1018 ;; Bind last-nonmenu-event to force use of keyboard, not mouse, for queries.
1019 (let ((last-nonmenu-event t) client-record)
1020 ;; Restore the current buffer afterward, but not using save-excursion,
1021 ;; because we don't want to save point in this buffer
1022 ;; if it happens to be one of those specified by the server.
1023 (save-current-buffer
1024 (dolist (file files)
1025 ;; If there is an existing buffer modified or the file is
1026 ;; modified, revert it. If there is an existing buffer with
1027 ;; deleted file, offer to write it.
1028 (let* ((minibuffer-auto-raise (or server-raise-frame
1029 minibuffer-auto-raise))
1030 (filen (car file))
1031 (obuf (get-file-buffer filen)))
1032 (add-to-history 'file-name-history filen)
1033 (if (and obuf (set-buffer obuf))
1034 (progn
1035 (cond ((file-exists-p filen)
1036 (when (not (verify-visited-file-modtime obuf))
1037 (revert-buffer t nil)))
1039 (when (y-or-n-p
1040 (concat "File no longer exists: " filen
1041 ", write buffer to file? "))
1042 (write-file filen))))
1043 (unless server-buffer-clients
1044 (setq server-existing-buffer t))
1045 (server-goto-line-column file))
1046 (set-buffer (find-file-noselect filen))
1047 (server-goto-line-column file)
1048 (run-hooks 'server-visit-hook)))
1049 (unless nowait
1050 ;; When the buffer is killed, inform the clients.
1051 (add-hook 'kill-buffer-hook 'server-kill-buffer nil t)
1052 (push proc server-buffer-clients))
1053 (push (current-buffer) client-record)))
1054 (unless nowait
1055 (process-put proc 'buffers
1056 (nconc (process-get proc 'buffers) client-record)))
1057 client-record))
1059 (defun server-buffer-done (buffer &optional for-killing)
1060 "Mark BUFFER as \"done\" for its client(s).
1061 This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED).
1062 NEXT-BUFFER is another server buffer, as a suggestion for what to select next,
1063 or nil. KILLED is t if we killed BUFFER (typically, because it was visiting
1064 a temp file).
1065 FOR-KILLING if non-nil indicates that we are called from `kill-buffer'."
1066 (let ((next-buffer nil)
1067 (killed nil))
1068 (dolist (proc server-clients)
1069 (let ((buffers (process-get proc 'buffers)))
1070 (or next-buffer
1071 (setq next-buffer (nth 1 (memq buffer buffers))))
1072 (when buffers ; Ignore bufferless clients.
1073 (setq buffers (delq buffer buffers))
1074 ;; Delete all dead buffers from PROC.
1075 (dolist (b buffers)
1076 (and (bufferp b)
1077 (not (buffer-live-p b))
1078 (setq buffers (delq b buffers))))
1079 (process-put proc 'buffers buffers)
1080 ;; If client now has no pending buffers,
1081 ;; tell it that it is done, and forget it entirely.
1082 (unless buffers
1083 (server-log "Close" proc)
1084 (server-delete-client proc)))))
1085 (when (and (bufferp buffer) (buffer-name buffer))
1086 ;; We may or may not kill this buffer;
1087 ;; if we do, do not call server-buffer-done recursively
1088 ;; from kill-buffer-hook.
1089 (let ((server-kill-buffer-running t))
1090 (with-current-buffer buffer
1091 (setq server-buffer-clients nil)
1092 (run-hooks 'server-done-hook))
1093 ;; Notice whether server-done-hook killed the buffer.
1094 (if (null (buffer-name buffer))
1095 (setq killed t)
1096 ;; Don't bother killing or burying the buffer
1097 ;; when we are called from kill-buffer.
1098 (unless for-killing
1099 (when (and (not killed)
1100 server-kill-new-buffers
1101 (with-current-buffer buffer
1102 (not server-existing-buffer)))
1103 (setq killed t)
1104 (bury-buffer buffer)
1105 (kill-buffer buffer))
1106 (unless killed
1107 (if (server-temp-file-p buffer)
1108 (progn
1109 (kill-buffer buffer)
1110 (setq killed t))
1111 (bury-buffer buffer)))))))
1112 (list next-buffer killed)))
1114 (defun server-temp-file-p (&optional buffer)
1115 "Return non-nil if BUFFER contains a file considered temporary.
1116 These are files whose names suggest they are repeatedly
1117 reused to pass information to another program.
1119 The variable `server-temp-file-regexp' controls which filenames
1120 are considered temporary."
1121 (and (buffer-file-name buffer)
1122 (string-match server-temp-file-regexp (buffer-file-name buffer))))
1124 (defun server-done ()
1125 "Offer to save current buffer, mark it as \"done\" for clients.
1126 This kills or buries the buffer, then returns a list
1127 of the form (NEXT-BUFFER KILLED). NEXT-BUFFER is another server buffer,
1128 as a suggestion for what to select next, or nil.
1129 KILLED is t if we killed BUFFER, which happens if it was created
1130 specifically for the clients and did not exist before their request for it."
1131 (when server-buffer-clients
1132 (if (server-temp-file-p)
1133 ;; For a temp file, save, and do make a non-numeric backup
1134 ;; (unless make-backup-files is nil).
1135 (let ((version-control nil)
1136 (buffer-backed-up nil))
1137 (save-buffer))
1138 (when (and (buffer-modified-p)
1139 buffer-file-name
1140 (y-or-n-p (concat "Save file " buffer-file-name "? ")))
1141 (save-buffer)))
1142 (server-buffer-done (current-buffer))))
1144 ;; Ask before killing a server buffer.
1145 ;; It was suggested to release its client instead,
1146 ;; but I think that is dangerous--the client would proceed
1147 ;; using whatever is on disk in that file. -- rms.
1148 (defun server-kill-buffer-query-function ()
1149 "Ask before killing a server buffer."
1150 (or (not server-buffer-clients)
1151 (let ((res t))
1152 (dolist (proc server-buffer-clients res)
1153 (when (and (memq proc server-clients)
1154 (eq (process-status proc) 'open))
1155 (setq res nil))))
1156 (yes-or-no-p (format "Buffer `%s' still has clients; kill it? "
1157 (buffer-name (current-buffer))))))
1159 (defun server-kill-emacs-query-function ()
1160 "Ask before exiting Emacs if it has live clients."
1161 (or (not server-clients)
1162 (let (live-client)
1163 (dolist (proc server-clients live-client)
1164 (when (memq t (mapcar 'buffer-live-p (process-get
1165 proc 'buffers)))
1166 (setq live-client t))))
1167 (yes-or-no-p "This Emacs session has clients; exit anyway? ")))
1169 (defvar server-kill-buffer-running nil
1170 "Non-nil while `server-kill-buffer' or `server-buffer-done' is running.")
1172 (defun server-kill-buffer ()
1173 "Remove the current buffer from its clients' buffer list.
1174 Designed to be added to `kill-buffer-hook'."
1175 ;; Prevent infinite recursion if user has made server-done-hook
1176 ;; call kill-buffer.
1177 (or server-kill-buffer-running
1178 (and server-buffer-clients
1179 (let ((server-kill-buffer-running t))
1180 (when server-process
1181 (server-buffer-done (current-buffer) t))))))
1183 (defun server-edit (&optional arg)
1184 "Switch to next server editing buffer; say \"Done\" for current buffer.
1185 If a server buffer is current, it is marked \"done\" and optionally saved.
1186 The buffer is also killed if it did not exist before the clients asked for it.
1187 When all of a client's buffers are marked as \"done\", the client is notified.
1189 Temporary files such as MH <draft> files are always saved and backed up,
1190 no questions asked. (The variable `make-backup-files', if nil, still
1191 inhibits a backup; you can set it locally in a particular buffer to
1192 prevent a backup for it.) The variable `server-temp-file-regexp' controls
1193 which filenames are considered temporary.
1195 If invoked with a prefix argument, or if there is no server process running,
1196 starts server process and that is all. Invoked by \\[server-edit]."
1197 (interactive "P")
1198 (cond
1199 ((or arg
1200 (not server-process)
1201 (memq (process-status server-process) '(signal exit)))
1202 (server-mode 1))
1203 (server-clients (apply 'server-switch-buffer (server-done)))
1204 (t (message "No server editing buffers exist"))))
1206 (defun server-switch-buffer (&optional next-buffer killed-one)
1207 "Switch to another buffer, preferably one that has a client.
1208 Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it.
1210 KILLED-ONE is t in a recursive call if we have already killed one
1211 temp-file server buffer. This means we should avoid the final
1212 \"switch to some other buffer\" since we've already effectively
1213 done that."
1214 (if (null next-buffer)
1215 (progn
1216 (let ((rest server-clients))
1217 (while (and rest (not next-buffer))
1218 (let ((proc (car rest)))
1219 ;; Only look at frameless clients.
1220 (when (not (process-get proc 'frame))
1221 (setq next-buffer (car (process-get proc 'buffers))))
1222 (setq rest (cdr rest)))))
1223 (and next-buffer (server-switch-buffer next-buffer killed-one))
1224 (unless (or next-buffer killed-one (window-dedicated-p (selected-window)))
1225 ;; (switch-to-buffer (other-buffer))
1226 (message "No server buffers remain to edit")))
1227 (if (not (buffer-live-p next-buffer))
1228 ;; If NEXT-BUFFER is a dead buffer, remove the server records for it
1229 ;; and try the next surviving server buffer.
1230 (apply 'server-switch-buffer (server-buffer-done next-buffer))
1231 ;; OK, we know next-buffer is live, let's display and select it.
1232 (if (functionp server-window)
1233 (funcall server-window next-buffer)
1234 (let ((win (get-buffer-window next-buffer 0)))
1235 (if (and win (not server-window))
1236 ;; The buffer is already displayed: just reuse the window.
1237 (progn
1238 (select-window win)
1239 (set-buffer next-buffer))
1240 ;; Otherwise, let's find an appropriate window.
1241 (cond ((window-live-p server-window)
1242 (select-window server-window))
1243 ((framep server-window)
1244 (unless (frame-live-p server-window)
1245 (setq server-window (make-frame)))
1246 (select-window (frame-selected-window server-window))))
1247 (when (window-minibuffer-p (selected-window))
1248 (select-window (next-window nil 'nomini 0)))
1249 ;; Move to a non-dedicated window, if we have one.
1250 (when (window-dedicated-p (selected-window))
1251 (select-window
1252 (get-window-with-predicate
1253 (lambda (w)
1254 (and (not (window-dedicated-p w))
1255 (equal (frame-terminal (window-frame w))
1256 (frame-terminal (selected-frame)))))
1257 'nomini 'visible (selected-window))))
1258 (condition-case nil
1259 (switch-to-buffer next-buffer)
1260 ;; After all the above, we might still have ended up with
1261 ;; a minibuffer/dedicated-window (if there's no other).
1262 (error (pop-to-buffer next-buffer)))))))
1263 (when server-raise-frame
1264 (select-frame-set-input-focus (window-frame (selected-window))))))
1266 ;;;###autoload
1267 (defun server-save-buffers-kill-terminal (proc &optional arg)
1268 "Offer to save each buffer, then kill PROC.
1270 With prefix arg, silently save all file-visiting buffers, then kill.
1272 If emacsclient was started with a list of filenames to edit, then
1273 only these files will be asked to be saved."
1274 (let ((buffers (process-get proc 'buffers)))
1275 ;; If client is bufferless, emulate a normal Emacs session
1276 ;; exit and offer to save all buffers. Otherwise, offer to
1277 ;; save only the buffers belonging to the client.
1278 (save-some-buffers arg
1279 (if buffers
1280 (lambda () (memq (current-buffer) buffers))
1282 (server-delete-client proc)))
1284 (define-key ctl-x-map "#" 'server-edit)
1286 (defun server-unload-function ()
1287 "Unload the server library."
1288 (server-mode -1)
1289 (save-current-buffer
1290 (dolist (buffer (buffer-list))
1291 (set-buffer buffer)
1292 (remove-hook 'kill-buffer-hook 'server-kill-buffer t)))
1293 ;; continue standard unloading
1294 nil)
1296 (add-hook 'kill-emacs-hook (lambda () (server-mode -1))) ;Cleanup upon exit.
1297 (defvar server-unload-function 'server-unload-function)
1299 (provide 'server)
1301 ;; arch-tag: 1f7ecb42-f00a-49f8-906d-61995d84c8d6
1302 ;;; server.el ends here