Skip to content

Commit c6eb801

Browse files
Preserve manual ghostel buffer renames
1 parent cabb939 commit c6eb801

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

ghostel.el

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ DIR is the module directory."
582582
(defvar-local ghostel--last-directory nil
583583
"Last known working directory from OSC 7, used for dedup.")
584584

585+
(defvar-local ghostel--managed-buffer-name nil
586+
"Last buffer name managed by Ghostel title tracking.
587+
Nil means title tracking has not claimed the buffer yet. Clearing this
588+
variable re-enables automatic renaming for the next title update.")
589+
585590
(defvar-local ghostel--prompt-positions nil
586591
"List of prompt positions as (buffer-line . exit-status) pairs.
587592
Used for prompt navigation and optional re-application after full redraws.")
@@ -1544,8 +1549,14 @@ This ensures terminal text is visible regardless of the Emacs theme."
15441549
:background bg)))
15451550

15461551
(defun ghostel--set-title (title)
1547-
"Update the buffer name with TITLE from the terminal."
1548-
(rename-buffer (format "*ghostel: %s*" title) t))
1552+
"Update the buffer name with TITLE from the terminal.
1553+
Do not overwrite a manual buffer rename."
1554+
(let ((new-name (format "*ghostel: %s*" title)))
1555+
(when (or (null ghostel--managed-buffer-name)
1556+
(equal (buffer-name) ghostel--managed-buffer-name))
1557+
(rename-buffer new-name t)
1558+
;; Keep the actual name because `rename-buffer' may uniquify it.
1559+
(setq ghostel--managed-buffer-name (buffer-name)))))
15491560

15501561
(defun ghostel--set-cursor-style (style visible)
15511562
"Set the cursor style based on terminal state.
@@ -1991,6 +2002,7 @@ wheel events reach ghostel's own scroll commands."
19912002
(buffer (generate-new-buffer buf-name)))
19922003
(with-current-buffer buffer
19932004
(ghostel-mode)
2005+
(setq ghostel--managed-buffer-name (buffer-name))
19942006
(let* ((height (window-body-height))
19952007
(width (window-max-chars-per-line)))
19962008
(setq ghostel--term

test/ghostel-test.el

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,35 @@ cell, so the visual line width must equal the terminal column count."
317317
(ghostel--write-input term "\e]2;My Title\e\\")
318318
(should (equal "My Title" (ghostel--get-title term))))) ; title set via OSC 2
319319

320+
(ert-deftest ghostel-test-title-does-not-overwrite-manual-rename ()
321+
"Test that title updates do not overwrite a manual buffer rename."
322+
(let (buf)
323+
(unwind-protect
324+
(cl-letf (((symbol-function 'ghostel--new)
325+
(lambda (&rest _args) 'fake-term))
326+
((symbol-function 'ghostel--apply-palette)
327+
(lambda (&rest _args) nil))
328+
((symbol-function 'ghostel--start-process)
329+
(lambda () nil)))
330+
(let ((ghostel--buffer-counter 0))
331+
(ghostel)
332+
(setq buf (current-buffer)))
333+
(with-current-buffer buf
334+
(should (equal "*ghostel*" (buffer-name)))
335+
(should (equal "*ghostel*" ghostel--managed-buffer-name))
336+
(ghostel--set-title "Title A")
337+
(should (equal "*ghostel: Title A*" (buffer-name)))
338+
(should (equal "*ghostel: Title A*" ghostel--managed-buffer-name))
339+
(ghostel--set-title "Title A2")
340+
(should (equal "*ghostel: Title A2*" (buffer-name)))
341+
(should (equal "*ghostel: Title A2*" ghostel--managed-buffer-name))
342+
(rename-buffer "ghostel manual title test" t)
343+
(ghostel--set-title "Title B")
344+
(should (equal "ghostel manual title test" (buffer-name)))
345+
(should (equal "*ghostel: Title A2*" ghostel--managed-buffer-name))))
346+
(when (buffer-live-p buf)
347+
(kill-buffer buf)))))
348+
320349
;; -----------------------------------------------------------------------
321350
;; Test: CRLF normalization in Zig
322351
;; -----------------------------------------------------------------------
@@ -1511,6 +1540,7 @@ cell, so the visual line width must equal the terminal column count."
15111540
ghostel-test-module-version-match
15121541
ghostel-test-module-version-mismatch
15131542
ghostel-test-module-version-newer-than-minimum
1543+
ghostel-test-title-does-not-overwrite-manual-rename
15141544
ghostel-test-immediate-redraw-triggers-on-small-echo
15151545
ghostel-test-immediate-redraw-skips-large-output
15161546
ghostel-test-immediate-redraw-skips-stale-send

0 commit comments

Comments
 (0)