+ (let ((client (assq proc server-clients)))
+ ;; Remove PROC from the list of clients.
+ (when client
+ (setq server-clients (delq client server-clients))
+ (dolist (buf (cdr client))
+ (with-current-buffer buf
+ ;; Remove PROC from the clients of each buffer.
+ (setq server-buffer-clients (delq proc server-buffer-clients))
+ ;; Kill the buffer if necessary.
+ (when (and (null server-buffer-clients)
+ (or (and server-kill-new-buffers
+ (not server-existing-buffer))
+ (server-temp-file-p)))
+ (kill-buffer (current-buffer)))))))
+ (server-log (format "Status changed to %s" (process-status proc)) proc))
+
+(defun server-select-display (display)
+ ;; If the current frame is on `display' we're all set.
+ (unless (equal (frame-parameter (selected-frame) 'display) display)
+ ;; Otherwise, look for an existing frame there and select it.
+ (dolist (frame (frame-list))
+ (when (equal (frame-parameter frame 'display) display)
+ (select-frame frame)))
+ ;; If there's no frame on that display yet, create a dummy one
+ ;; and select it.
+ (unless (equal (frame-parameter (selected-frame) 'display) display)
+ (select-frame
+ (make-frame-on-display
+ display
+ ;; This frame is only there in place of an actual "current display"
+ ;; setting, so we want it to be as unobtrusive as possible. That's
+ ;; what the invisibility is for. The minibuffer setting is so that
+ ;; we don't end up displaying a buffer in it (which noone would
+ ;; notice).
+ '((visibility . nil) (minibuffer . only)))))))
+
+(defun server-unquote-arg (arg)
+ (replace-regexp-in-string
+ "&." (lambda (s)
+ (case (aref s 1)
+ (?& "&")
+ (?- "-")
+ (?n "\n")
+ (t " ")))
+ arg t t))
+
+(defun server-ensure-safe-dir (dir)
+ "Make sure DIR is a directory with no race-condition issues.
+Creates the directory if necessary and makes sure:
+- there's no symlink involved
+- it's owned by us
+- it's not readable/writable by anybody else."
+ (setq dir (directory-file-name dir))
+ (let ((attrs (file-attributes dir)))
+ (unless attrs
+ (letf (((default-file-modes) ?\700)) (make-directory dir))
+ (setq attrs (file-attributes dir)))
+ ;; Check that it's safe for use.
+ (unless (and (eq t (car attrs)) (eq (nth 2 attrs) (user-uid))
+ (zerop (logand ?\077 (file-modes dir))))
+ (error "The directory %s is unsafe" dir))))