server.el 66 KB

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