Merged from emacs@sv.gnu.org
[bpt/emacs.git] / lisp / server.el
CommitLineData
55535639 1;;; server.el --- Lisp code for GNU Emacs running as server process
c88ab9ce 2
0d30b337 3;; Copyright (C) 1986, 1987, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
aaef169d 4;; 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6d74b528 5
630cc463 6;; Author: William Sommerfeld <wesommer@athena.mit.edu>
e4874521 7;; Maintainer: FSF
d7b4d18f 8;; Keywords: processes
630cc463 9
9ae0f972 10;; Changes by peck@sun.com and by rms.
35dfa9b6 11;; Overhaul by Karoly Lorentey <lorentey@elte.hu> for multi-tty support.
9ae0f972 12
13;; This file is part of GNU Emacs.
14
15;; GNU Emacs is free software; you can redistribute it and/or modify
16;; it under the terms of the GNU General Public License as published by
e5167999 17;; the Free Software Foundation; either version 2, or (at your option)
9ae0f972 18;; any later version.
19
20;; GNU Emacs is distributed in the hope that it will be useful,
21;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;; GNU General Public License for more details.
24
25;; You should have received a copy of the GNU General Public License
b578f267 26;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
27;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28;; Boston, MA 02110-1301, USA.
9ae0f972 29
630cc463 30;;; Commentary:
9ae0f972 31
b578f267
EN
32;; This Lisp code is run in Emacs when it is to operate as
33;; a server for other processes.
9ae0f972 34
b578f267 35;; Load this library and do M-x server-edit to enable Emacs as a server.
44a56b29
SM
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))
9ae0f972 39
b578f267
EN
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.
9ae0f972 44
6d3a46f7 45;; Note that any number of clients may dispatch files to Emacs to be edited.
9ae0f972 46
b578f267 47;; When you finish editing a Server buffer, again call server-edit
64f51134
JPW
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
b578f267 50;; and exited with server-edit, the client "editor" will return
64f51134 51;; to the program that invoked it.
9ae0f972 52
b578f267
EN
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:
9ae0f972 57
b578f267
EN
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.
9ae0f972 60
b578f267
EN
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.
9ae0f972 63
b578f267
EN
64;; 3. When the program that wants to use "the editor" is running
65;; as a subprocess of Emacs.
9ae0f972 66
b578f267
EN
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.
9ae0f972 72
64f51134
JPW
73;; The buffer local variable "server-buffer-clients" lists
74;; the clients who are waiting for this buffer to be edited.
b578f267
EN
75;; The global variable "server-clients" lists all the waiting clients,
76;; and which files are yet to be edited for each.
630cc463
ER
77
78;;; Code:
8b3e840e
SM
79
80(eval-when-compile (require 'cl))
81
ab1c7f35
RS
82(defgroup server nil
83 "Emacs running as a server process."
84 :group 'external)
85
ab1c7f35 86(defcustom server-visit-hook nil
0c851d78 87 "*Hook run when visiting a file for the Emacs server."
ab1c7f35 88 :group 'server
0c851d78 89 :type 'hook)
ab1c7f35
RS
90
91(defcustom server-switch-hook nil
0c851d78 92 "*Hook run when switching to a buffer for the Emacs server."
ab1c7f35 93 :group 'server
0c851d78 94 :type 'hook)
ab1c7f35
RS
95
96(defcustom server-done-hook nil
0c851d78 97 "*Hook run when done editing a buffer for the Emacs server."
ab1c7f35 98 :group 'server
0c851d78 99 :type 'hook)
f9b3ef88 100
64f51134
JPW
101(defvar server-process nil
102 "The current server process.")
9ae0f972 103
104(defvar server-clients nil
105 "List of current server clients.
9002956f
KL
106Each element is (PROC PROPERTIES...) where PROC is a process object,
107and PROPERTIES is an association list of client properties.")
0a125897 108
9ae0f972 109(defvar server-buffer-clients nil
b5a9911c 110 "List of client ids for clients requesting editing of current buffer.")
faf931a8 111(make-variable-buffer-local 'server-buffer-clients)
9ae0f972 112;; Changing major modes should not erase this local.
113(put 'server-buffer-clients 'permanent-local t)
114
33186f32
DL
115(defcustom server-window nil
116 "*Specification of the window to use for selecting Emacs server buffers.
ec40ed9f 117If nil, use the selected window.
408784a7 118If it is a function, it should take one argument (a buffer) and
33186f32
DL
119display and select it. A common value is `pop-to-buffer'.
120If it is a window, use that.
121If it is a frame, use the frame's selected window.
122
123It is not meaningful to set this to a specific frame or window with Custom.
124Only programs can do so."
125 :group 'server
bf247b6e 126 :version "22.1"
33186f32
DL
127 :type '(choice (const :tag "Use selected window"
128 :match (lambda (widget value)
129 (not (functionp value)))
130 nil)
131 (function-item :tag "Use pop-to-buffer" pop-to-buffer)
132 (function :tag "Other function")))
ec40ed9f 133
ab1c7f35 134(defcustom server-temp-file-regexp "^/tmp/Re\\|/draft$"
33186f32
DL
135 "*Regexp matching names of temporary files.
136These are deleted and reused after each edit by the programs that
137invoke the Emacs server."
ab1c7f35
RS
138 :group 'server
139 :type 'regexp)
9ae0f972 140
c6a117f0
GM
141(defcustom server-kill-new-buffers t
142 "*Whether to kill buffers when done with them.
143If non-nil, kill a buffer unless it already existed before editing
64f51134 144it with Emacs server. If nil, kill only buffers as specified by
c6a117f0
GM
145`server-temp-file-regexp'.
146Please note that only buffers are killed that still have a client,
147i.e. buffers visited which \"emacsclient --no-wait\" are never killed in
148this way."
149 :group 'server
150 :type 'boolean
151 :version "21.1")
152
9ae0f972 153(or (assq 'server-buffer-clients minor-mode-alist)
154 (setq minor-mode-alist (cons '(server-buffer-clients " Server") minor-mode-alist)))
155
c6a117f0 156(defvar server-existing-buffer nil
ca0c7250 157 "Non-nil means the buffer existed before the server was asked to visit it.
4dd04714 158This means that the server should not kill the buffer when you say you
ca0c7250 159are done with it in the server.")
c6a117f0
GM
160(make-variable-buffer-local 'server-existing-buffer)
161
03ae35cf
SM
162(defvar server-name "server")
163
fc0dcdef
KL
164(defvar server-socket-dir nil
165 "The directory in which to place the server socket.
166Initialized by `server-start'.")
0c851d78 167
9002956f
KL
168(defun server-client (proc)
169 "Return the Emacs client corresponding to PROC.
170PROC must be a process object.
171The car of the result is PROC; the cdr is an association list.
172See `server-client-get' and `server-client-set'."
173 (assq proc server-clients))
174
175(defun server-client-get (client property)
176 "Get the value of PROPERTY in CLIENT.
177CLIENT may be a process object, or a client returned by `server-client'.
178Return nil if CLIENT has no such property."
179 (or (listp client) (setq client (server-client client)))
180 (cdr (assq property (cdr client))))
181
182(defun server-client-set (client property value)
183 "Set the PROPERTY to VALUE in CLIENT, and return VALUE.
184CLIENT may be a process object, or a client returned by `server-client'."
185 (let (p proc)
186 (if (listp client)
187 (setq proc (car client))
188 (setq proc client
189 client (server-client client)))
190 (setq p (assq property client))
191 (cond
192 (p (setcdr p value))
193 (client (setcdr client (cons (cons property value) (cdr client))))
194 (t (setq server-clients
195 `((,proc (,property . ,value)) . ,server-clients))))
196 value))
197
198(defun server-clients-with (property value)
199 "Return a list of clients with PROPERTY set to VALUE."
200 (let (result)
201 (dolist (client server-clients result)
202 (when (equal value (server-client-get client property))
203 (setq result (cons (car client) result))))))
204
205(defun server-add-client (proc)
206 "Create a client for process PROC, if it doesn't already have one.
207New clients have no properties."
208 (unless (server-client proc)
209 (setq server-clients (cons (cons proc nil)
210 server-clients))))
211
59e085e0
KL
212(defun server-getenv-from (env variable)
213 "Get the value of VARIABLE in ENV.
214VARIABLE should be a string. Value is nil if VARIABLE is
215undefined in ENV. Otherwise, value is a string.
216
217ENV should be in the same format as `process-environment'."
218 (let (entry result)
219 (while (and env (null result))
220 (setq entry (car env)
221 env (cdr env))
222 (if (and (> (length entry) (length variable))
223 (eq ?= (aref entry (length variable)))
224 (equal variable (substring entry 0 (length variable))))
225 (setq result (substring entry (+ (length variable) 1)))))
226 result))
227
228(defmacro server-with-environment (env vars &rest body)
229 "Evaluate BODY with environment variables VARS set to those in ENV.
65f64034
KL
230The environment variables are then restored to their previous values.
231
59e085e0
KL
232VARS should be a list of strings.
233ENV should be in the same format as `process-environment'."
65f64034
KL
234 (declare (indent 2))
235 (let ((oldvalues (make-symbol "oldvalues"))
236 (var (make-symbol "var"))
237 (value (make-symbol "value"))
238 (pair (make-symbol "pair")))
239 `(let (,oldvalues)
59e085e0
KL
240 (dolist (,var ,vars)
241 (let ((,value (server-getenv-from ,env ,var)))
65f64034
KL
242 (setq ,oldvalues (cons (cons ,var (getenv ,var)) ,oldvalues))
243 (setenv ,var ,value)))
244 (unwind-protect
245 (progn ,@body)
246 (dolist (,pair ,oldvalues)
247 (setenv (car ,pair) (cdr ,pair)))))))
248
de93c791 249(defun server-delete-client (client &optional noframe)
6ed8eeff 250 "Delete CLIENT, including its buffers, terminals and frames.
de93c791
KL
251If NOFRAME is non-nil, let the frames live. (To be used from
252`delete-frame-functions'."
9002956f
KL
253 ;; Force a new lookup of client (prevents infinite recursion).
254 (setq client (server-client
255 (if (listp client) (car client) client)))
256 (let ((proc (car client))
257 (buffers (server-client-get client 'buffers)))
258 (when client
259 (setq server-clients (delq client server-clients))
260
261 (dolist (buf buffers)
6ed9e43a
KL
262 (when (buffer-live-p buf)
263 (with-current-buffer buf
264 ;; Remove PROC from the clients of each buffer.
265 (setq server-buffer-clients (delq proc server-buffer-clients))
266 ;; Kill the buffer if necessary.
267 (when (and (null server-buffer-clients)
268 (or (and server-kill-new-buffers
269 (not server-existing-buffer))
270 (server-temp-file-p)))
271 (kill-buffer (current-buffer))))))
9002956f 272
160f0817
KL
273 ;; Delete the client's frames.
274 (unless noframe
275 (dolist (frame (frame-list))
276 (when (and (frame-live-p frame)
277 (equal proc (frame-parameter frame 'client)))
278 ;; Prevent `server-handle-delete-frame' from calling us
279 ;; recursively.
280 (set-frame-parameter frame 'client nil)
281 (delete-frame frame))))
282
9002956f 283 ;; Delete the client's tty.
6ed8eeff
KL
284 (let ((terminal (server-client-get client 'terminal)))
285 (when (eq (terminal-live-p terminal) t)
286 (delete-terminal terminal)))
9002956f 287
9002956f
KL
288 ;; Delete the client's process.
289 (if (eq (process-status (car client)) 'open)
290 (delete-process (car client)))
291
292 (server-log "Deleted" proc))))
293
8b3e840e 294(defun server-log (string &optional client)
6d3a46f7
KL
295 "If a *server* buffer exists, write STRING to it for logging purposes.
296If CLIENT is non-nil, add a description of it to the logged
297message."
9ae0f972 298 (if (get-buffer "*server*")
8b3e840e 299 (with-current-buffer "*server*"
9ae0f972 300 (goto-char (point-max))
8b3e840e 301 (insert (current-time-string)
9002956f
KL
302 (cond
303 ((null client) " ")
304 ((listp client) (format " %s: " (car client)))
305 (t (format " %s: " client)))
8b3e840e 306 string)
74c20cd3 307 (or (bolp) (newline)))))
9ae0f972 308
309(defun server-sentinel (proc msg)
9002956f 310 "The process sentinel for Emacs server connections."
cbfc02e4
RF
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))
9002956f
KL
316 (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc)
317 (server-delete-client proc))
0c851d78 318
77134727
KL
319(defun server-handle-delete-frame (frame)
320 "Delete the client connection when the emacsclient frame is deleted."
9002956f 321 (let ((proc (frame-parameter frame 'client)))
e519a50b
KL
322 (when (and (frame-live-p frame)
323 proc
160f0817 324 ;; See if this is the last frame for this client.
d5381da2 325 (>= 1 (let ((frame-num 0))
aabf0cb9 326 (dolist (f (frame-list))
160f0817
KL
327 (when (eq proc (frame-parameter f 'client))
328 (setq frame-num (1+ frame-num))))
329 frame-num)))
9002956f 330 (server-log (format "server-handle-delete-frame, frame %s" frame) proc)
de93c791 331 (server-delete-client proc 'noframe)))) ; Let delete-frame delete the frame later.
9002956f 332
6ed8eeff 333(defun server-handle-suspend-tty (terminal)
9002956f 334 "Notify the emacsclient process to suspend itself when its tty device is suspended."
6ed8eeff
KL
335 (dolist (proc (server-clients-with 'terminal terminal))
336 (server-log (format "server-handle-suspend-tty, terminal %s" terminal) proc)
6d3a46f7 337 (condition-case err
6afdd335 338 (server-send-string proc "-suspend \n")
6d3a46f7 339 (file-error (condition-case nil (server-delete-client proc) (error nil))))))
44a56b29 340
0c851d78 341(defun server-unquote-arg (arg)
6afdd335
KL
342 "Remove &-quotation from ARG.
343See `server-quote-arg' and `server-process-filter'."
0c851d78
SM
344 (replace-regexp-in-string
345 "&." (lambda (s)
346 (case (aref s 1)
347 (?& "&")
348 (?- "-")
349 (?n "\n")
350 (t " ")))
351 arg t t))
9ae0f972 352
0b0d3e0b 353(defun server-quote-arg (arg)
9002956f 354 "In ARG, insert a & before each &, each space, each newline, and -.
0b0d3e0b 355Change spaces to underscores, too, so that the return value never
6afdd335
KL
356contains a space.
357
358See `server-unquote-arg' and `server-process-filter'."
0b0d3e0b
KL
359 (replace-regexp-in-string
360 "[-&\n ]" (lambda (s)
361 (case (aref s 0)
362 (?& "&&")
363 (?- "&-")
364 (?\n "&n")
365 (?\s "&_")))
366 arg t t))
367
6afdd335
KL
368(defun server-send-string (proc string)
369 "A wrapper around `proc-send-string' for logging."
370 (server-log (concat "Sent " string) proc)
371 (process-send-string proc string))
372
724629d2
SM
373(defun server-ensure-safe-dir (dir)
374 "Make sure DIR is a directory with no race-condition issues.
375Creates the directory if necessary and makes sure:
376- there's no symlink involved
377- it's owned by us
378- it's not readable/writable by anybody else."
379 (setq dir (directory-file-name dir))
380 (let ((attrs (file-attributes dir)))
381 (unless attrs
382 (letf (((default-file-modes) ?\700)) (make-directory dir))
383 (setq attrs (file-attributes dir)))
384 ;; Check that it's safe for use.
385 (unless (and (eq t (car attrs)) (eq (nth 2 attrs) (user-uid))
386 (zerop (logand ?\077 (file-modes dir))))
387 (error "The directory %s is unsafe" dir))))
388
7229064d 389;;;###autoload
9ae0f972 390(defun server-start (&optional leave-dead)
391 "Allow this Emacs process to be a server for client processes.
392This starts a server communications subprocess through which
6d3a46f7
KL
393client \"editors\" can send your editing commands to this Emacs
394job. To use the server, set up the program `emacsclient' in the
9ae0f972 395Emacs distribution as your standard \"editor\".
396
6d3a46f7
KL
397Prefix arg LEAVE-DEAD means just kill any existing server
398communications subprocess."
9ae0f972 399 (interactive "P")
d6b4b3cf
KL
400 (when (or
401 (not server-clients)
402 (yes-or-no-p
403 "The current server still has clients; delete them? "))
404 ;; It is safe to get the user id now.
405 (setq server-socket-dir (or server-socket-dir
406 (format "/tmp/emacs%d" (user-uid))))
407 ;; Make sure there is a safe directory in which to place the socket.
408 (server-ensure-safe-dir server-socket-dir)
409 ;; kill it dead!
9ae0f972 410 (if server-process
d6b4b3cf
KL
411 (condition-case () (delete-process server-process) (error nil)))
412 ;; Delete the socket files made by previous server invocations.
413 (condition-case ()
414 (delete-file (expand-file-name server-name server-socket-dir))
415 (error nil))
416 ;; If this Emacs already had a server, clear out associated status.
417 (while server-clients
418 (server-delete-client (car server-clients)))
419 (if leave-dead
420 (progn
421 (server-log (message "Server stopped"))
422 (setq server-process nil))
423 (if server-process
424 (server-log (message "Restarting server"))
425 (server-log (message "Starting server")))
426 (letf (((default-file-modes) ?\700))
d6b4b3cf
KL
427 (add-hook 'suspend-tty-functions 'server-handle-suspend-tty)
428 (add-hook 'delete-frame-functions 'server-handle-delete-frame)
429 (add-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)
430 (add-hook 'kill-emacs-query-functions 'server-kill-emacs-query-function)
431 (setq server-process
432 (make-network-process
433 :name "server" :family 'local :server t :noquery t
434 :service (expand-file-name server-name server-socket-dir)
435 :sentinel 'server-sentinel :filter 'server-process-filter
436 ;; We must receive file names without being decoded.
437 ;; Those are decoded by server-process-filter according
438 ;; to file-name-coding-system.
439 :coding 'raw-text))))))
33186f32
DL
440
441;;;###autoload
442(define-minor-mode server-mode
443 "Toggle Server mode.
444With ARG, turn Server mode on if ARG is positive, off otherwise.
445Server mode runs a process that accepts commands from the
446`emacsclient' program. See `server-start' and Info node `Emacs server'."
447 :global t
448 :group 'server
bf247b6e 449 :version "22.1"
33186f32
DL
450 ;; Fixme: Should this check for an existing server socket and do
451 ;; nothing if there is one (for multiple Emacs sessions)?
452 (server-start (not server-mode)))
9ae0f972 453\f
9ae0f972 454(defun server-process-filter (proc string)
33186f32 455 "Process a request from the server to edit some files.
6afdd335
KL
456PROC is the server process. STRING consists of a sequence of
457commands prefixed by a dash. Some commands have arguments; these
458are &-quoted and need to be decoded by `server-unquote-arg'. The
459filter parses and executes these commands.
460
461To illustrate the protocol, here is an example command that
462emacsclient sends to create a new X frame (note that the whole
463sequence is sent on a single line):
464
465 -version 21.3.50 xterm
466 -env HOME /home/lorentey
467 -env DISPLAY :0.0
468 ... lots of other -env commands
469 -display :0.0
470 -window-system
471
472The server normally sends back the single command `-good-version'
473as a response.
474
475The following commands are accepted by the server:
476
477`-version CLIENT-VERSION'
478 Check version numbers between server and client, and signal an
479 error if there is a mismatch. The server replies with
480 `-good-version' to confirm the match.
481
59e085e0 482`-env NAME=VALUE'
6afdd335
KL
483 An environment variable on the client side.
484
92071250
KL
485`-current-frame'
486 Forbid the creation of new frames.
487
6afdd335
KL
488`-nowait'
489 Request that the next frame created should not be
490 associated with this client.
491
492`-display DISPLAY'
493 Set the display name to open X frames on.
494
495`-position LINE[:COLUMN]'
496 Go to the given line and column number
497 in the next file opened.
498
499`-file FILENAME'
500 Load the given file in the current frame.
501
502`-eval EXPR'
503 Evaluate EXPR as a Lisp expression and return the
504 result in -print commands.
505
506`-window-system'
507 Open a new X frame.
508
509`-tty DEVICENAME TYPE'
510 Open a new tty frame at the client.
511
512`-resume'
513 Resume this tty frame. The client sends this string when it
514 gets the SIGCONT signal and it is the foreground process on its
515 controlling tty.
516
517`-suspend'
518 Suspend this tty frame. The client sends this string in
519 response to SIGTSTP and SIGTTOU. The server must cease all I/O
520 on this tty until it gets a -resume command.
521
522`-ignore COMMENT'
523 Do nothing, but put the comment in the server
524 log. Useful for debugging.
525
526
527The following commands are accepted by the client:
528
529`-good-version'
530 Signals a version match between the client and the server.
531
532`-emacs-pid PID'
533 Describes the process id of the Emacs process;
534 used to forward window change signals to it.
535
536`-window-system-unsupported'
537 Signals that the server does not
538 support creating X frames; the client must try again with a tty
539 frame.
540
541`-print STRING'
542 Print STRING on stdout. Used to send values
543 returned by -eval.
544
545`-error DESCRIPTION'
546 Signal an error (but continue processing).
547
548`-suspend'
549 Suspend this terminal, i.e., stop the client process. Sent
550 when the user presses C-z."
551 (server-log (concat "Received " string) proc)
3f7ef08e
SM
552 (let ((prev (process-get proc 'previous-string)))
553 (when prev
554 (setq string (concat prev string))
555 (process-put proc 'previous-string nil)))
a9298135 556 (condition-case err
0b0d3e0b 557 (progn
9002956f 558 (server-add-client proc)
0b0d3e0b
KL
559 ;; If the input is multiple lines,
560 ;; process each line individually.
561 (while (string-match "\n" string)
562 (let ((request (substring string 0 (match-beginning 0)))
563 (coding-system (and default-enable-multibyte-characters
564 (or file-name-coding-system
565 default-file-name-coding-system)))
9002956f 566 (client (server-client proc))
92071250 567 current-frame
9002956f
KL
568 nowait ; t if emacsclient does not want to wait for us.
569 frame ; The frame that was opened for the client (if any).
59e085e0 570 display ; Open the frame on this display.
0b0d3e0b 571 dontkill ; t if the client should not be killed.
59e085e0 572 env
0b0d3e0b
KL
573 (files nil)
574 (lineno 1)
575 (columnno 0))
576 ;; Remove this line from STRING.
577 (setq string (substring string (match-end 0)))
9002956f 578 (while (string-match " *[^ ]* " request)
0b0d3e0b
KL
579 (let ((arg (substring request (match-beginning 0) (1- (match-end 0)))))
580 (setq request (substring request (match-end 0)))
581 (cond
9002956f
KL
582 ;; -version CLIENT-VERSION:
583 ;; Check version numbers, signal an error if there is a mismatch.
584 ((and (equal "-version" arg)
585 (string-match "\\([0-9.]+\\) " request))
0b0d3e0b 586 (let* ((client-version (match-string 1 request))
9002956f
KL
587 (truncated-emacs-version
588 (substring emacs-version 0 (length client-version))))
0b0d3e0b
KL
589 (setq request (substring request (match-end 0)))
590 (if (equal client-version truncated-emacs-version)
591 (progn
6afdd335 592 (server-send-string proc "-good-version \n")
9002956f
KL
593 (server-client-set client 'version client-version))
594 (error (concat "Version mismatch: Emacs is "
595 truncated-emacs-version
596 ", emacsclient is " client-version)))))
0b0d3e0b 597
9002956f 598 ;; -nowait: Emacsclient won't wait for a result.
0b0d3e0b
KL
599 ((equal "-nowait" arg) (setq nowait t))
600
92071250
KL
601 ;; -current-frame: Don't create frames.
602 ((equal "-current-frame" arg) (setq current-frame t))
603
9002956f 604 ;; -display DISPLAY:
59e085e0 605 ;; Open X frames on the given display instead of the default.
0b0d3e0b
KL
606 ((and (equal "-display" arg) (string-match "\\([^ ]*\\) " request))
607 (setq display (match-string 1 request)
608 request (substring request (match-end 0))))
609
9002956f 610 ;; -window-system: Open a new X frame.
0b0d3e0b 611 ((equal "-window-system" arg)
9002956f 612 (unless (server-client-get client 'version)
a9298135 613 (error "Protocol error; make sure to use the correct version of emacsclient"))
92071250
KL
614 (unless current-frame
615 (if (fboundp 'x-create-frame)
616 (let ((params (if nowait
62c5106c
KL
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
6ed8eeff 620 ;; `server-save-buffers-kill-terminal' from unexpectedly
62c5106c 621 ;; killing emacs on that frame.
da8e8fc1
KL
622 (list (cons 'client 'nowait) (cons 'environment env))
623 (list (cons 'client proc) (cons 'environment env)))))
92071250
KL
624 (setq frame (make-frame-on-display
625 (or display
f105f403 626 (frame-parameter nil 'display)
92071250
KL
627 (getenv "DISPLAY")
628 (error "Please specify display"))
629 params))
630 (server-log (format "%s created" frame) proc)
631 ;; XXX We need to ensure the parameters are
632 ;; really set because Emacs forgets unhandled
633 ;; initialization parameters for X frames at
634 ;; the moment.
635 (modify-frame-parameters frame params)
636 (select-frame frame)
637 (server-client-set client 'frame frame)
6ed8eeff 638 (server-client-set client 'terminal (frame-terminal frame))
8573f9ad
KL
639
640 ;; Display *scratch* by default.
641 (switch-to-buffer (get-buffer-create "*scratch*") 'norecord)
642
92071250
KL
643 (setq dontkill t))
644 ;; This emacs does not support X.
645 (server-log "Window system unsupported" proc)
646 (server-send-string proc "-window-system-unsupported \n")
647 (setq dontkill t))))
9002956f
KL
648
649 ;; -resume: Resume a suspended tty frame.
0b0d3e0b 650 ((equal "-resume" arg)
6ed8eeff 651 (let ((terminal (server-client-get client 'terminal)))
0b0d3e0b 652 (setq dontkill t)
6ed8eeff
KL
653 (when (eq (terminal-live-p terminal) t)
654 (resume-tty terminal))))
0b0d3e0b 655
9002956f
KL
656 ;; -suspend: Suspend the client's frame. (In case we
657 ;; get out of sync, and a C-z sends a SIGTSTP to
658 ;; emacsclient.)
0b0d3e0b 659 ((equal "-suspend" arg)
6ed8eeff 660 (let ((terminal (server-client-get client 'terminal)))
0b0d3e0b 661 (setq dontkill t)
6ed8eeff
KL
662 (when (eq (terminal-live-p terminal) t)
663 (suspend-tty terminal))))
0b0d3e0b 664
9002956f
KL
665 ;; -ignore COMMENT: Noop; useful for debugging emacsclient.
666 ;; (The given comment appears in the server log.)
0b0d3e0b
KL
667 ((and (equal "-ignore" arg) (string-match "\\([^ ]*\\) " request))
668 (setq dontkill t
669 request (substring request (match-end 0))))
670
9002956f 671 ;; -tty DEVICE-NAME TYPE: Open a new tty frame at the client.
0b0d3e0b
KL
672 ((and (equal "-tty" arg) (string-match "\\([^ ]*\\) \\([^ ]*\\) " request))
673 (let ((tty (server-unquote-arg (match-string 1 request)))
674 (type (server-unquote-arg (match-string 2 request))))
675 (setq request (substring request (match-end 0)))
9002956f
KL
676 (unless (server-client-get client 'version)
677 (error "Protocol error; make sure you use the correct version of emacsclient"))
92071250 678 (unless current-frame
59e085e0
KL
679 (server-with-environment env
680 '("LANG" "LC_CTYPE" "LC_ALL"
681 ;; For tgetent(3); list according to ncurses(3).
682 "BAUDRATE" "COLUMNS" "ESCDELAY" "HOME" "LINES"
683 "NCURSES_ASSUMED_COLORS" "NCURSES_NO_PADDING"
684 "NCURSES_NO_SETBUF" "TERM" "TERMCAP" "TERMINFO"
685 "TERMINFO_DIRS" "TERMPATH")
92071250
KL
686 (setq frame (make-frame-on-tty tty type
687 ;; Ignore nowait here; we always need to clean
688 ;; up opened ttys when the client dies.
da8e8fc1
KL
689 `((client . ,proc)
690 (environment . ,env)))))
92071250
KL
691 (select-frame frame)
692 (server-client-set client 'frame frame)
6ed8eeff
KL
693 (server-client-set client 'tty (terminal-name frame))
694 (server-client-set client 'terminal (frame-terminal frame))
92071250 695
8573f9ad
KL
696 ;; Display *scratch* by default.
697 (switch-to-buffer (get-buffer-create "*scratch*") 'norecord)
698
92071250
KL
699 ;; Reply with our pid.
700 (server-send-string proc (concat "-emacs-pid " (number-to-string (emacs-pid)) "\n"))
701 (setq dontkill t))))
9002956f
KL
702
703 ;; -position LINE: Go to the given line in the next file.
0b0d3e0b 704 ((and (equal "-position" arg) (string-match "\\(\\+[0-9]+\\) " request))
80a426c2
KL
705 (setq lineno (string-to-number (substring (match-string 1 request) 1))
706 request (substring request (match-end 0))))
0b0d3e0b 707
9002956f 708 ;; -position LINE:COLUMN: Set point to the given position in the next file.
0b0d3e0b 709 ((and (equal "-position" arg) (string-match "\\+\\([0-9]+\\):\\([0-9]+\\) " request))
b4bb3cbc
KL
710 (setq lineno (string-to-number (match-string 1 request))
711 columnno (string-to-number (match-string 2 request))
4e15f072 712 request (substring request (match-end 0))))
0b0d3e0b 713
9002956f 714 ;; -file FILENAME: Load the given file.
0b0d3e0b
KL
715 ((and (equal "-file" arg) (string-match "\\([^ ]+\\) " request))
716 (let ((file (server-unquote-arg (match-string 1 request))))
717 (setq request (substring request (match-end 0)))
718 (if coding-system
719 (setq file (decode-coding-string file coding-system)))
720 (setq file (command-line-normalize-file-name file))
80a426c2
KL
721 (push (list file lineno columnno) files)
722 (server-log (format "New file: %s (%d:%d)" file lineno columnno) proc))
0b0d3e0b
KL
723 (setq lineno 1
724 columnno 0))
725
9002956f 726 ;; -eval EXPR: Evaluate a Lisp expression.
0b0d3e0b
KL
727 ((and (equal "-eval" arg) (string-match "\\([^ ]+\\) " request))
728 (let ((expr (server-unquote-arg (match-string 1 request))))
729 (setq request (substring request (match-end 0)))
730 (if coding-system
731 (setq expr (decode-coding-string expr coding-system)))
732 (let ((v (eval (car (read-from-string expr)))))
9002956f 733 (when (and (not frame) v)
a9298135
KL
734 (with-temp-buffer
735 (let ((standard-output (current-buffer)))
736 (pp v)
6afdd335
KL
737 (server-send-string
738 proc (format "-print %s\n"
739 (server-quote-arg
740 (buffer-substring-no-properties (point-min)
741 (point-max)))))))))
0b0d3e0b
KL
742 (setq lineno 1
743 columnno 0)))
744
59e085e0
KL
745 ;; -env NAME=VALUE: An environment variable.
746 ((and (equal "-env" arg) (string-match "\\([^ ]+\\) " request))
747 (let ((var (server-unquote-arg (match-string 1 request))))
da8e8fc1 748 ;; XXX Variables should be encoded as in getenv/setenv.
9002956f 749 (setq request (substring request (match-end 0)))
59e085e0 750 (setq env (cons var env))))
9002956f 751
0b0d3e0b
KL
752 ;; Unknown command.
753 (t (error "Unknown command: %s" arg)))))
754
c5b0a355
KL
755 (let (buffers)
756 (when files
757 (run-hooks 'pre-command-hook)
758 (setq buffers (server-visit-files files client nowait))
759 (run-hooks 'post-command-hook))
760
fe74bf60
KL
761 (when frame
762 (with-selected-frame frame
b3e6f69c 763 (display-startup-echo-area-message)
fe74bf60 764 (unless inhibit-splash-screen
b3e6f69c
KL
765 (condition-case err
766 ;; This looks scary because `fancy-splash-screens'
767 ;; will call `recursive-edit' from a process filter.
768 ;; However, that should be safe to do now.
769 (display-splash-screen)
770 ;; `recursive-edit' will throw an error if Emacs is
771 ;; already doing a recursive edit elsewhere. Catch it
772 ;; here so that we can finish normally.
773 (error nil)))))
c2844302 774
c5b0a355
KL
775 ;; Delete the client if necessary.
776 (cond
777 (nowait
778 ;; Client requested nowait; return immediately.
779 (server-log "Close nowait client" proc)
780 (server-delete-client proc))
781 ((and (not dontkill) (null buffers))
782 ;; This client is empty; get rid of it immediately.
783 (server-log "Close empty client" proc)
784 (server-delete-client proc)))
785 (cond
786 ((or isearch-mode (minibufferp))
787 nil)
788 ((and frame (null buffers))
0ff21b4e
KL
789 (message "%s" (substitute-command-keys
790 "When done with this frame, type \\[delete-frame]")))
c5b0a355
KL
791 ((not (null buffers))
792 (server-switch-buffer (car buffers))
793 (run-hooks 'server-switch-hook)
794 (unless nowait
0ff21b4e
KL
795 (message "%s" (substitute-command-keys
796 "When done with a buffer, type \\[server-edit]"))))))))
0b0d3e0b 797
a9298135
KL
798 ;; Save for later any partial line that remains.
799 (when (> (length string) 0)
800 (process-put proc 'previous-string string)))
801 ;; condition-case
802 (error (ignore-errors
6afdd335 803 (server-send-string
0b0d3e0b 804 proc (concat "-error " (server-quote-arg (error-message-string err))))
a9298135
KL
805 (setq string "")
806 (server-log (error-message-string err) proc)
807 (delete-process proc)))))
9ae0f972 808
6b98185f 809(defun server-goto-line-column (file-line-col)
6d3a46f7
KL
810 "Move point to the position indicated in FILE-LINE-COL.
811FILE-LINE-COL should be a three-element list as described in
812`server-visit-files'."
6b98185f
RS
813 (goto-line (nth 1 file-line-col))
814 (let ((column-number (nth 2 file-line-col)))
815 (if (> column-number 0)
816 (move-to-column (1- column-number)))))
817
dfa35e6b 818(defun server-visit-files (files client &optional nowait)
c5b0a355 819 "Find FILES and return a list of buffers created.
8c493570 820FILES is an alist whose elements are (FILENAME LINENUMBER COLUMNNUMBER).
6d3a46f7 821CLIENT is the client that requested this operation.
dfa35e6b
RS
822NOWAIT non-nil means this client is not waiting for the results,
823so don't mark these buffers specially, just visit them normally."
e82e73c2 824 ;; Bind last-nonmenu-event to force use of keyboard, not mouse, for queries.
44a56b29 825 (let ((last-nonmenu-event t) client-record)
3a0ce849
RS
826 ;; Restore the current buffer afterward, but not using save-excursion,
827 ;; because we don't want to save point in this buffer
828 ;; if it happens to be one of those specified by the server.
44a56b29
SM
829 (save-current-buffer
830 (dolist (file files)
831 ;; If there is an existing buffer modified or the file is
832 ;; modified, revert it. If there is an existing buffer with
833 ;; deleted file, offer to write it.
834 (let* ((filen (car file))
835 (obuf (get-file-buffer filen)))
836 (push filen file-name-history)
837 (if (and obuf (set-buffer obuf))
838 (progn
839 (cond ((file-exists-p filen)
840 (if (not (verify-visited-file-modtime obuf))
841 (revert-buffer t nil)))
842 (t
843 (if (y-or-n-p
9002956f 844 (concat "File no longer exists: " filen
44a56b29
SM
845 ", write buffer to file? "))
846 (write-file filen))))
847 (setq server-existing-buffer t)
848 (server-goto-line-column file))
849 (set-buffer (find-file-noselect filen))
850 (server-goto-line-column file)
851 (run-hooks 'server-visit-hook)))
852 (unless nowait
853 ;; When the buffer is killed, inform the clients.
854 (add-hook 'kill-buffer-hook 'server-kill-buffer nil t)
c5b0a355
KL
855 (push (car client) server-buffer-clients))
856 (push (current-buffer) client-record)))
857 (unless nowait
858 (server-client-set
859 client 'buffers
860 (nconc (server-client-get client 'buffers) client-record)))
861 client-record))
9ae0f972 862\f
b392bac9 863(defun server-buffer-done (buffer &optional for-killing)
9ae0f972 864 "Mark BUFFER as \"done\" for its client(s).
9184aafb
RS
865This buries the buffer, then returns a list of the form (NEXT-BUFFER KILLED).
866NEXT-BUFFER is another server buffer, as a suggestion for what to select next,
8b3e840e
SM
867or nil. KILLED is t if we killed BUFFER (typically, because it was visiting
868a temp file).
869FOR-KILLING if non-nil indicates that we are called from `kill-buffer'."
0c851d78 870 (let ((next-buffer nil)
9002956f
KL
871 (killed nil))
872 (dolist (client server-clients)
873 (let ((buffers (server-client-get client 'buffers)))
8b3e840e 874 (or next-buffer
9002956f
KL
875 (setq next-buffer (nth 1 (memq buffer buffers))))
876 (when buffers ; Ignore bufferless clients.
877 (setq buffers (delq buffer buffers))
878 ;; Delete all dead buffers from CLIENT.
879 (dolist (b buffers)
880 (and (bufferp b)
881 (not (buffer-live-p b))
882 (setq buffers (delq b buffers))))
883 (server-client-set client 'buffers buffers)
884 ;; If client now has no pending buffers,
885 ;; tell it that it is done, and forget it entirely.
886 (unless buffers
887 (server-log "Close" client)
888 (server-delete-client client)))))
68469f74 889 (if (and (bufferp buffer) (buffer-name buffer))
599f9a5c
RS
890 ;; We may or may not kill this buffer;
891 ;; if we do, do not call server-buffer-done recursively
892 ;; from kill-buffer-hook.
893 (let ((server-kill-buffer-running t))
408784a7 894 (with-current-buffer buffer
f9b3ef88
RS
895 (setq server-buffer-clients nil)
896 (run-hooks 'server-done-hook))
599f9a5c
RS
897 ;; Notice whether server-done-hook killed the buffer.
898 (if (null (buffer-name buffer))
899 (setq killed t)
900 ;; Don't bother killing or burying the buffer
901 ;; when we are called from kill-buffer.
902 (unless for-killing
c6a117f0
GM
903 (when (and (not killed)
904 server-kill-new-buffers
4dd04714
RS
905 (with-current-buffer buffer
906 (not server-existing-buffer)))
c6a117f0 907 (setq killed t)
3d2a0e0b 908 (bury-buffer buffer)
c6a117f0
GM
909 (kill-buffer buffer))
910 (unless killed
911 (if (server-temp-file-p buffer)
912 (progn
913 (kill-buffer buffer)
914 (setq killed t))
915 (bury-buffer buffer)))))))
9184aafb 916 (list next-buffer killed)))
9ae0f972 917
408784a7 918(defun server-temp-file-p (&optional buffer)
9ae0f972 919 "Return non-nil if BUFFER contains a file considered temporary.
920These are files whose names suggest they are repeatedly
921reused to pass information to another program.
922
923The variable `server-temp-file-regexp' controls which filenames
924are considered temporary."
925 (and (buffer-file-name buffer)
926 (string-match server-temp-file-regexp (buffer-file-name buffer))))
927
928(defun server-done ()
cc9875f9 929 "Offer to save current buffer, mark it as \"done\" for clients.
ed9ae328
RS
930This kills or buries the buffer, then returns a list
931of the form (NEXT-BUFFER KILLED). NEXT-BUFFER is another server buffer,
932as a suggestion for what to select next, or nil.
933KILLED is t if we killed BUFFER, which happens if it was created
934specifically for the clients and did not exist before their request for it."
408784a7
SM
935 (when server-buffer-clients
936 (if (server-temp-file-p)
937 ;; For a temp file, save, and do make a non-numeric backup
938 ;; (unless make-backup-files is nil).
939 (let ((version-control nil)
940 (buffer-backed-up nil))
941 (save-buffer))
942 (if (and (buffer-modified-p)
943 buffer-file-name
944 (y-or-n-p (concat "Save file " buffer-file-name "? ")))
945 (save-buffer)))
946 (server-buffer-done (current-buffer))))
faf931a8 947
71207de2
RS
948;; Ask before killing a server buffer.
949;; It was suggested to release its client instead,
950;; but I think that is dangerous--the client would proceed
951;; using whatever is on disk in that file. -- rms.
03d78665 952(defun server-kill-buffer-query-function ()
9002956f 953 "Ask before killing a server buffer."
03d78665 954 (or (not server-buffer-clients)
114a8b8c
KL
955 (let ((res t))
956 (dolist (proc server-buffer-clients res)
9002956f
KL
957 (let ((client (server-client proc)))
958 (when (and client (eq (process-status proc) 'open))
959 (setq res nil)))))
03d78665
RS
960 (yes-or-no-p (format "Buffer `%s' still has clients; kill it? "
961 (buffer-name (current-buffer))))))
962
03d78665 963(defun server-kill-emacs-query-function ()
7540c1e0 964 "Ask before exiting Emacs it has live clients."
9002956f
KL
965 (or (not server-clients)
966 (let (live-client)
967 (dolist (client server-clients live-client)
968 (if (memq t (mapcar 'buffer-live-p (server-client-get
969 client 'buffers)))
970 (setq live-client t))))
971 (yes-or-no-p "This Emacs session has clients; exit anyway? ")))
b392bac9 972
fb873cfc 973(defvar server-kill-buffer-running nil
599f9a5c 974 "Non-nil while `server-kill-buffer' or `server-buffer-done' is running.")
fb873cfc 975
b392bac9 976(defun server-kill-buffer ()
6d3a46f7
KL
977 "Remove the current buffer from its clients' buffer list.
978Designed to be added to `kill-buffer-hook'."
fb873cfc
RS
979 ;; Prevent infinite recursion if user has made server-done-hook
980 ;; call kill-buffer.
981 (or server-kill-buffer-running
599f9a5c
RS
982 (and server-buffer-clients
983 (let ((server-kill-buffer-running t))
984 (when server-process
985 (server-buffer-done (current-buffer) t))))))
9ae0f972 986\f
987(defun server-edit (&optional arg)
988 "Switch to next server editing buffer; say \"Done\" for current buffer.
989If a server buffer is current, it is marked \"done\" and optionally saved.
ed9ae328 990The buffer is also killed if it did not exist before the clients asked for it.
9ae0f972 991When all of a client's buffers are marked as \"done\", the client is notified.
992
993Temporary files such as MH <draft> files are always saved and backed up,
991298c3
RS
994no questions asked. (The variable `make-backup-files', if nil, still
995inhibits a backup; you can set it locally in a particular buffer to
996prevent a backup for it.) The variable `server-temp-file-regexp' controls
9ae0f972 997which filenames are considered temporary.
998
64f51134 999If invoked with a prefix argument, or if there is no server process running,
9ae0f972 1000starts server process and that is all. Invoked by \\[server-edit]."
9ae0f972 1001 (interactive "P")
1002 (if (or arg
1003 (not server-process)
1004 (memq (process-status server-process) '(signal exit)))
1005 (server-start nil)
9184aafb 1006 (apply 'server-switch-buffer (server-done))))
9ae0f972 1007
98a4349c 1008(defun server-switch-buffer (&optional next-buffer killed-one)
9ae0f972 1009 "Switch to another buffer, preferably one that has a client.
6d3a46f7
KL
1010Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it.
1011
1012KILLED-ONE is t in a recursive call if we have already killed one
1013temp-file server buffer. This means we should avoid the final
1014\"switch to some other buffer\" since we've already effectively
1015done that."
ca0c7250 1016 (if (null next-buffer)
9002956f
KL
1017 (progn
1018 (let ((rest server-clients))
1019 (while (and rest (not next-buffer))
1020 (let ((client (car rest)))
1021 ;; Only look at frameless clients.
1022 (when (not (server-client-get client 'frame))
1023 (setq next-buffer (car (server-client-get client 'buffers))))
1024 (setq rest (cdr rest)))))
1025 (and next-buffer (server-switch-buffer next-buffer killed-one))
1026 (unless (or next-buffer killed-one (window-dedicated-p (selected-window)))
1027 ;; (switch-to-buffer (other-buffer))
90ee5627 1028 (message "No server buffers remain to edit")))
9002956f 1029 (if (not (buffer-live-p next-buffer))
ca0c7250 1030 ;; If NEXT-BUFFER is a dead buffer, remove the server records for it
9ae0f972 1031 ;; and try the next surviving server buffer.
ca0c7250
SM
1032 (apply 'server-switch-buffer (server-buffer-done next-buffer))
1033 ;; OK, we know next-buffer is live, let's display and select it.
408784a7
SM
1034 (if (functionp server-window)
1035 (funcall server-window next-buffer)
1036 (let ((win (get-buffer-window next-buffer 0)))
1037 (if (and win (not server-window))
1038 ;; The buffer is already displayed: just reuse the window.
1039 (let ((frame (window-frame win)))
1040 (if (eq (frame-visible-p frame) 'icon)
1041 (raise-frame frame))
1042 (select-window win)
1043 (set-buffer next-buffer))
1044 ;; Otherwise, let's find an appropriate window.
1045 (cond ((and (windowp server-window)
1046 (window-live-p server-window))
1047 (select-window server-window))
1048 ((framep server-window)
1049 (if (not (frame-live-p server-window))
1050 (setq server-window (make-frame)))
1051 (select-window (frame-selected-window server-window))))
1052 (if (window-minibuffer-p (selected-window))
1053 (select-window (next-window nil 'nomini 0)))
1054 ;; Move to a non-dedicated window, if we have one.
1055 (when (window-dedicated-p (selected-window))
1056 (select-window
1057 (get-window-with-predicate
1058 (lambda (w)
1059 (and (not (window-dedicated-p w))
6ed8eeff
KL
1060 (equal (frame-terminal (window-frame w))
1061 (frame-terminal (selected-frame)))))
408784a7
SM
1062 'nomini 'visible (selected-window))))
1063 (condition-case nil
1064 (switch-to-buffer next-buffer)
1065 ;; After all the above, we might still have ended up with
1066 ;; a minibuffer/dedicated-window (if there's no other).
1067 (error (pop-to-buffer next-buffer)))))))))
9ae0f972 1068
59e085e0 1069;;;###autoload
6ed8eeff 1070(defun server-save-buffers-kill-terminal (proc &optional arg)
59e085e0 1071 "Offer to save each buffer, then kill PROC.
b4ca0271 1072
7540c1e0
KL
1073With prefix arg, silently save all file-visiting buffers, then kill.
1074
1075If emacsclient was started with a list of filenames to edit, then
1076only these files will be asked to be saved."
59e085e0
KL
1077 (let ((buffers (server-client-get proc 'buffers)))
1078 ;; If client is bufferless, emulate a normal Emacs session
1079 ;; exit and offer to save all buffers. Otherwise, offer to
1080 ;; save only the buffers belonging to the client.
1081 (save-some-buffers arg
1082 (if buffers
1083 (lambda () (memq (current-buffer) buffers))
1084 t))
1085 (server-delete-client proc)))
b4ca0271 1086
772c5eb7 1087(define-key ctl-x-map "#" 'server-edit)
df4e8a11
DL
1088
1089(defun server-unload-hook ()
6d3a46f7 1090 "Unload the server library."
33186f32 1091 (server-start t)
9002956f
KL
1092 (remove-hook 'suspend-tty-functions 'server-handle-suspend-tty)
1093 (remove-hook 'delete-frame-functions 'server-handle-delete-frame)
df4e8a11
DL
1094 (remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)
1095 (remove-hook 'kill-emacs-query-functions 'server-kill-emacs-query-function)
1096 (remove-hook 'kill-buffer-hook 'server-kill-buffer))
3bb38bc2
RS
1097
1098(add-hook 'server-unload-hook 'server-unload-hook)
16c15321
RM
1099\f
1100(provide 'server)
c88ab9ce 1101
ab5796a9 1102;;; arch-tag: 1f7ecb42-f00a-49f8-906d-61995d84c8d6
c88ab9ce 1103;;; server.el ends here