Fix behavior of client frames when 'find-alternate-file' is used

* lisp/files.el (find-alternate-file-dont-kill-client): New var.
(find-alternate-file): Bind it to a special value when invoking
kill-buffer-hook.
* lisp/server.el (server-delete-client): If NOFRAME is
'dont-kill-client', don't kill the client and its terminals.
(server-buffer-done): Pass 'find-alternate-file-dont-kill-client'
to 'server-delete-client'.  (Bug#65277)
This commit is contained in:
Eli Zaretskii 2023-08-19 10:34:07 +03:00
parent b12ce748d1
commit e3207b13ce
2 changed files with 20 additions and 8 deletions

View file

@ -1998,6 +1998,8 @@ INHIBIT-BUFFER-HOOKS non-nil.
Note: Be careful with let-binding this hook considering it is
frequently used for cleanup.")
(defvar find-alternate-file-dont-kill-client nil
"If non-nil, `server-buffer-done' should not delete the client.")
(defun find-alternate-file (filename &optional wildcards)
"Find file FILENAME, select its buffer, kill previous buffer.
If the current buffer now contains an empty file that you just visited
@ -2044,7 +2046,8 @@ killed."
;; save a modified buffer visiting a file. Rather, `kill-buffer'
;; asks that itself. Thus, there's no need to temporarily do
;; `(set-buffer-modified-p nil)' before running this hook.
(run-hooks 'kill-buffer-hook)
(let ((find-alternate-file-dont-kill-client 'dont-kill-client))
(run-hooks 'kill-buffer-hook))
;; Okay, now we can end-of-life the old buffer.
(if (get-buffer " **lose**")
(kill-buffer " **lose**"))

View file

@ -330,6 +330,9 @@ ENV should be in the same format as `process-environment'."
(defun server-delete-client (proc &optional noframe)
"Delete PROC, including its buffers, terminals and frames.
If NOFRAME is non-nil, let the frames live.
If NOFRAME is the symbol \\='dont-kill-client, also don't
delete PROC or its terminals, just kill its buffers: this is
for when `find-alternate-file' calls this via `kill-buffer-hook'.
Updates `server-clients'."
(server-log (concat "server-delete-client" (if noframe " noframe")) proc)
;; Force a new lookup of client (prevents infinite recursion).
@ -366,23 +369,28 @@ Updates `server-clients'."
(set-frame-parameter frame 'client nil)
(delete-frame frame))))
(setq server-clients (delq proc server-clients))
(or (eq noframe 'dont-kill-client)
(setq server-clients (delq proc server-clients)))
;; Delete the client's tty, except on Windows (both GUI and
;; console), where there's only one terminal and does not make
;; sense to delete it, or if we are explicitly told not.
(unless (or (eq system-type 'windows-nt)
;; 'find-alternate-file' caused the last client
;; buffer to be killed, but we will reuse the client
;; for another buffer.
(eq noframe 'dont-kill-client)
(process-get proc 'no-delete-terminal))
(let ((terminal (process-get proc 'terminal)))
;; Only delete the terminal if it is non-nil.
(when (and terminal (eq (terminal-live-p terminal) t))
(delete-terminal terminal))))
;; Delete the client's process.
(if (eq (process-status proc) 'open)
(delete-process proc))
(server-log "Deleted" proc))))
;; Delete the client's process (or don't).
(unless (eq noframe 'dont-kill-client)
(if (eq (process-status proc) 'open)
(delete-process proc))
(server-log "Deleted" proc)))))
(defvar server-log-time-function #'current-time-string
"Function to generate timestamps for `server-buffer'.")
@ -1590,7 +1598,8 @@ FOR-KILLING if non-nil indicates that we are called from `kill-buffer'."
;; frames, which might change the current buffer. We
;; don't want that (bug#640).
(save-current-buffer
(server-delete-client proc))
(server-delete-client proc
find-alternate-file-dont-kill-client))
(server-delete-client proc))))))
(when (and (bufferp buffer) (buffer-name buffer))
;; We may or may not kill this buffer;