:version "22.1")
(put 'server-auth-dir 'risky-local-variable t)
-(defvar server-auth-key nil
- "The current server authentication key.")
-(put 'server-auth-key 'risky-local-variable t)
+(defcustom server-raise-frame t
+ "If non-nil, raise frame when switching to a buffer."
+ :group 'server
+ :type 'boolean
+ :version "22.1")
(defcustom server-visit-hook nil
- "*Hook run when visiting a file for the Emacs server."
+ "Hook run when visiting a file for the Emacs server."
:group 'server
:type 'hook)
(defcustom server-switch-hook nil
- "*Hook run when switching to a buffer for the Emacs server."
+ "Hook run when switching to a buffer for the Emacs server."
:group 'server
:type 'hook)
(defcustom server-done-hook nil
- "*Hook run when done editing a buffer for the Emacs server."
+ "Hook run when done editing a buffer for the Emacs server."
:group 'server
:type 'hook)
(put 'server-buffer-clients 'permanent-local t)
(defcustom server-window nil
- "*Specification of the window to use for selecting Emacs server buffers.
+ "Specification of the window to use for selecting Emacs server buffers.
If nil, use the selected window.
If it is a function, it should take one argument (a buffer) and
display and select it. A common value is `pop-to-buffer'.
(function :tag "Other function")))
(defcustom server-temp-file-regexp "^/tmp/Re\\|/draft$"
- "*Regexp matching names of temporary files.
+ "Regexp matching names of temporary files.
These are deleted and reused after each edit by the programs that
invoke the Emacs server."
:group 'server
:type 'regexp)
(defcustom server-kill-new-buffers t
- "*Whether to kill buffers when done with them.
+ "Whether to kill buffers when done with them.
If non-nil, kill a buffer unless it already existed before editing
it with Emacs server. If nil, kill only buffers as specified by
`server-temp-file-regexp'.
(when (and (eq (process-status proc) 'open)
(process-query-on-exit-flag proc))
(set-process-query-on-exit-flag proc nil))
+ ;; Delete the associated connection file, if applicable.
+ ;; This is actually problematic: the file may have been overwritten by
+ ;; another Emacs server in the mean time, so it's not ours any more.
+ ;; (and (process-contact proc :server)
+ ;; (eq (process-status proc) 'closed)
+ ;; (ignore-errors (delete-file (process-get proc :server-file))))
(server-log (format "Status changed to %s" (process-status proc)) proc))
(defun server-select-display (display)
(interactive "P")
(when server-process
;; kill it dead!
- (ignore-errors (delete-process server-process))
- (ignore-errors
- ;; Delete the socket or authentication files made by previous
- ;; server invocations.
- (if (eq (process-contact server-process :family) 'local)
- (delete-file (expand-file-name server-name server-socket-dir))
- (setq server-auth-key nil)
- (delete-file (expand-file-name server-name server-auth-dir)))))
+ (ignore-errors (delete-process server-process)))
;; If this Emacs already had a server, clear out associated status.
(while server-clients
(let ((buffer (nth 1 (car server-clients))))
(server-buffer-done buffer)))
;; Now any previous server is properly stopped.
(unless leave-dead
- ;; Make sure there is a safe directory in which to place the socket.
- (server-ensure-safe-dir
- (if server-use-tcp server-auth-dir server-socket-dir))
- (when server-process
- (server-log (message "Restarting server")))
- (letf (((default-file-modes) ?\700))
- (setq server-process
- (apply #'make-network-process
- :name server-name
- :server t
- :noquery t
- :sentinel 'server-sentinel
- :filter 'server-process-filter
- ;; We must receive file names without being decoded.
- ;; Those are decoded by server-process-filter according
- ;; to file-name-coding-system.
- :coding 'raw-text
- ;; The rest of the args depends on the kind of socket used.
- (if server-use-tcp
- (list :family nil
- :service t
- :host (or server-host 'local)
- :plist '(:authenticated nil))
- (list :family 'local
- :service (expand-file-name server-name server-socket-dir)
- :plist '(:authenticated t)))))
- (unless server-process (error "Could not start server process"))
- (when server-use-tcp
- (setq server-auth-key
- (loop
- ;; The auth key is a 64-byte string of random chars in the
- ;; range `!'..`~'.
- for i below 64
- collect (+ 33 (random 94)) into auth
- finally return (concat auth)))
- (with-temp-file (expand-file-name server-name server-auth-dir)
- (set-buffer-multibyte nil)
- (setq buffer-file-coding-system 'no-conversion)
- (insert (format-network-address
- (process-contact server-process :local))
- "\n" server-auth-key))))))
+ (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir))
+ (server-file (expand-file-name server-name server-dir)))
+ ;; Make sure there is a safe directory in which to place the socket.
+ (server-ensure-safe-dir server-dir)
+ ;; Remove any leftover socket or authentication file.
+ (ignore-errors (delete-file server-file))
+ (when server-process
+ (server-log (message "Restarting server")))
+ (letf (((default-file-modes) ?\700))
+ (setq server-process
+ (apply #'make-network-process
+ :name server-name
+ :server t
+ :noquery t
+ :sentinel 'server-sentinel
+ :filter 'server-process-filter
+ ;; We must receive file names without being decoded.
+ ;; Those are decoded by server-process-filter according
+ ;; to file-name-coding-system.
+ :coding 'raw-text
+ ;; The rest of the args depends on the kind of socket used.
+ (if server-use-tcp
+ (list :family nil
+ :service t
+ :host (or server-host 'local)
+ :plist '(:authenticated nil))
+ (list :family 'local
+ :service server-file
+ :plist '(:authenticated t)))))
+ (unless server-process (error "Could not start server process"))
+ (when server-use-tcp
+ (let ((auth-key
+ (loop
+ ;; The auth key is a 64-byte string of random chars in the
+ ;; range `!'..`~'.
+ for i below 64
+ collect (+ 33 (random 94)) into auth
+ finally return (concat auth))))
+ (process-put server-process :auth-key auth-key)
+ (with-temp-file server-file
+ (set-buffer-multibyte nil)
+ (setq buffer-file-coding-system 'no-conversion)
+ (insert (format-network-address
+ (process-contact server-process :local))
+ " " (int-to-string (emacs-pid))
+ "\n" auth-key))))))))
;;;###autoload
(define-minor-mode server-mode
;; First things first: let's check the authentication
(unless (process-get proc :authenticated)
(if (and (string-match "-auth \\(.*?\\)\n" string)
- (string= (match-string 1 string) server-auth-key))
+ (equal (match-string 1 string) (process-get proc :auth-key)))
(progn
(setq string (substring string (match-end 0)))
(process-put proc :authenticated t)
(when prev
(setq string (concat prev string))
(process-put proc :previous-string nil)))
+ (when (> (recursion-depth) 0)
+ ;; We're inside a minibuffer already, so if the emacs-client is trying
+ ;; to open a frame on a new display, we might end up with an unusable
+ ;; frame because input from that display will be blocked (until exiting
+ ;; the minibuffer). Better exit this minibuffer right away.
+ ;; Similarly with recursive-edits such as the splash screen.
+ (process-put proc :previous-string string)
+ (run-with-timer 0 nil (lexical-let ((proc proc))
+ (lambda () (server-process-filter proc ""))))
+ (top-level))
;; If the input is multiple lines,
;; process each line individually.
(while (string-match "\n" string)
;; If there is an existing buffer modified or the file is
;; modified, revert it. If there is an existing buffer with
;; deleted file, offer to write it.
- (let* ((filen (car file))
+ (let* ((minibuffer-auto-raise (or server-raise-frame
+ minibuffer-auto-raise))
+ (filen (car file))
(obuf (get-file-buffer filen)))
(add-to-history 'file-name-history filen)
(if (and obuf (set-buffer obuf))
(let ((win (get-buffer-window next-buffer 0)))
(if (and win (not server-window))
;; The buffer is already displayed: just reuse the window.
- (let ((frame (window-frame win)))
- (when (eq (frame-visible-p frame) 'icon)
- (raise-frame frame))
- (select-window win)
- (set-buffer next-buffer))
+ (progn
+ (select-window win)
+ (set-buffer next-buffer))
;; Otherwise, let's find an appropriate window.
(cond ((and (windowp server-window)
(window-live-p server-window))
(switch-to-buffer next-buffer)
;; After all the above, we might still have ended up with
;; a minibuffer/dedicated-window (if there's no other).
- (error (pop-to-buffer next-buffer)))))))))
+ (error (pop-to-buffer next-buffer)))))))
+ (when server-raise-frame
+ (select-frame-set-input-focus (window-frame (selected-window))))))
(define-key ctl-x-map "#" 'server-edit)