diff --git a/lisp/server.el b/lisp/server.el index b65053267a6..06bba74bfd7 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -438,7 +438,8 @@ server-sentinel (ignore-errors (delete-file (process-get proc :server-file)))) (server-log (format "Status changed to %s: %s" - (process-status proc) msg) proc) + (process-status proc) msg) + proc) (server-delete-client proc)) (defun server--on-display-p (frame display) @@ -1046,6 +1047,9 @@ server-execute-continuation (process-put proc 'continuation nil) (if continuation (ignore-errors (funcall continuation))))) +(defvar server--pending-filter-calls nil) +(defvar server--processing-pending-filters nil) + (cl-defun server-process-filter (proc string) "Process a request from the server to edit some files. PROC is the server process. STRING consists of a sequence of @@ -1145,6 +1149,37 @@ server-process-filter `-suspend' Suspend this terminal, i.e., stop the client process. Sent when the user presses \\[suspend-frame]." + ;; Push this to the end of the list, so the list is in the order in which + ;; we need to process it. + ;; FIXME: This implies an O(Nē) worst-case, which is not good: + ;; we should use a "true" O(N) queue. + (setq server--pending-filter-calls + (nconc server--pending-filter-calls (list (cons proc string)))) + (unless server--processing-pending-filters + (server-process-pending-filters))) + +(defun server-process-pending-filters () + (let ((server--processing-pending-filters t)) + (unwind-protect + (while server--pending-filter-calls + (let* ((oldest (pop server--pending-filter-calls))) + (server--process-filter (car oldest) (cdr oldest)))) + ;; In case we're exiting early (e.g. for `server-goto-toplevel'), + ;; make sure we continue running the other pending filters. + (when server--pending-filter-calls + (run-with-timer 0 nil #'server-process-pending-filters))))) + +(defun server--message-timed (time &rest args) + (apply #'message args) + ;; FIXME: Ideally we should not need `sit-for' here and instead use + ;; some `message-timed' call which makes sure the message is visible + ;; without forcing us to wait here. + ;; If there's already another process-filter pending, skip `sit-for', + ;; just as it does when there's pending user input. + (unless (consp server--pending-filter-calls) + (sit-for time))) + +(cl-defun server--process-filter (proc string) (server-log (concat "Received " string) proc) ;; First things first: let's check the authentication (unless (process-get proc :authenticated) @@ -1158,8 +1193,7 @@ server-process-filter ;; Display the error as a message and give the user time to see ;; it, in case the error written by emacsclient to stderr is not ;; visible for some reason. - (message "Authentication failed") - (sit-for 2) + (server--message-timed 2 "Authentication failed") (server-send-string proc (concat "-error " (server-quote-arg "Authentication failed"))) (unless (eq system-type 'windows-nt) @@ -1169,10 +1203,10 @@ server-process-filter (delete-terminal terminal)))) ;; Before calling `delete-process', give emacsclient time to ;; receive the error string and shut down on its own. - (sit-for 1) - (delete-process proc) + ;; FIXME: Why do we wait 1s here but 5s in the other one? + (run-with-timer 1 nil #'delete-process proc) ;; We return immediately. - (cl-return-from server-process-filter))) + (cl-return-from server--process-filter))) (let ((prev (process-get proc 'previous-string))) (when prev (setq string (concat prev string)) @@ -1507,8 +1541,7 @@ server-return-error ;; Display the error as a message and give the user time to see ;; it, in case the error written by emacsclient to stderr is not ;; visible for some reason. - (message (error-message-string err)) - (sit-for 2) + (server--message-timed 2 (error-message-string err)) (server-send-string proc (concat "-error " (server-quote-arg (error-message-string err)))) @@ -1520,8 +1553,8 @@ server-return-error (delete-terminal terminal)))) ;; Before calling `delete-process', give emacsclient time to ;; receive the error string and shut down on its own. - (sit-for 5) - (delete-process proc))) + ;; FIXME: Why do we wait 5s here but 1s in the other one? + (run-with-timer 5 nil #'delete-process proc))) (defun server-goto-line-column (line-col) "Move point to the position indicated in LINE-COL.