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