@@ -3397,19 +3397,27 @@ prevent redraw flicker."
33973397
33983398; ;; Entry point
33993399
3400+ (defun ghostel--prepare-buffer (buffer &optional identity )
3401+ " Put BUFFER into `ghostel-mode' and record its terminal identity.
3402+ IDENTITY, if given, is stored as `ghostel--buffer-identity' so the
3403+ buffer can be found again after title-tracking renames it."
3404+ (with-current-buffer buffer
3405+ (unless (derived-mode-p 'ghostel-mode )
3406+ (ghostel-mode)
3407+ (setq ghostel--managed-buffer-name (buffer-name ))
3408+ (setq ghostel--buffer-identity (or identity (buffer-name ))))))
3409+
34003410(defun ghostel--init-buffer (buffer &optional identity )
3401- " Initialize BUFFER as a ghostel terminal if it isn't one already .
3411+ " Initialize BUFFER as a ghostel terminal if no terminal handle exists yet .
34023412Terminal dimensions come from BUFFER's displayed window when one
34033413exists, otherwise from the selected window. The terminal resizes
34043414itself via `window-size-change-functions' once the buffer is
34053415displayed, so a mismatch at creation time self-corrects.
34063416IDENTITY, if given, is stored as `ghostel--buffer-identity' so the
34073417buffer can be found again after title-tracking renames it."
34083418 (with-current-buffer buffer
3409- (unless (derived-mode-p 'ghostel-mode )
3410- (ghostel-mode)
3411- (setq ghostel--managed-buffer-name (buffer-name ))
3412- (setq ghostel--buffer-identity (or identity (buffer-name )))
3419+ (unless ghostel--term
3420+ (ghostel--prepare-buffer buffer identity)
34133421 (let* ((w (or (get-buffer-window buffer t ) (selected-window )))
34143422 (height (if (window-live-p w) (window-body-height w) 24 ))
34153423 (width (if (window-live-p w) (window-max-chars-per-line w) 80 )))
@@ -3448,6 +3456,8 @@ The name of the buffer is determined by the value of `ghostel-buffer-name'."
34483456 (generate-new-buffer ghostel-buffer-name)
34493457 (or (ghostel--find-buffer-by-identity identity)
34503458 (get-buffer-create identity)))))
3459+ (unless (with-current-buffer buffer (derived-mode-p 'ghostel-mode ))
3460+ (ghostel--prepare-buffer buffer identity))
34513461 (pop-to-buffer buffer (append display-buffer--same-window-action
34523462 '((category . comint))))
34533463 (ghostel--init-buffer buffer identity)))
@@ -3471,9 +3481,7 @@ Signals `user-error' if BUFFER already has a live ghostel process."
34713481 (buffer-name buffer)))
34723482 (let ((window (or (get-buffer-window buffer t ) (selected-window ))))
34733483 (with-current-buffer buffer
3474- (unless (derived-mode-p 'ghostel-mode )
3475- (ghostel-mode))
3476- (setq ghostel--managed-buffer-name (buffer-name ))
3484+ (ghostel--prepare-buffer buffer nil )
34773485 (let* ((height (max 1 (window-body-height window)))
34783486 (width (max 1 (window-max-chars-per-line window)))
34793487 (remote-p (file-remote-p default-directory)))
0 commit comments