From fb5663954ca8c692f1bebbfdb2dd139e8a63667f Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Fri, 8 May 2026 19:21:26 -0500 Subject: feat(ai-vterm): F9 toggle/redisplay/pick + persistent split geometry F9 was a single command that always opened the project picker. Three small frustrations stacked up. With one claude buffer open and not visible, F9 was a redundant prompt to pick a project that already had a session. With claude visible, there was no way to bury it without M-x quit-window. With two projects' buffers alive, swapping between them was a buffer-switch chore. F9 is now a dispatch: - Claude visible in this frame: quit the window (toggle off) and capture the geometry first. - Exactly one claude buffer alive but hidden: re-display it (DWIM single-buffer case). - Zero or two-plus alive: fall through to the project picker. C-F9 is the always-pick-project entry point for explicit project switches. M-F9 is a buffer picker over the alive claude buffers. If a claude window is currently shown, the picked buffer replaces it in that window so the split orientation and size carry over. The shown buffer sorts last in the picker with a [shown] marker so RET picks "the other one." Split geometry persists across toggles. Two module-level vars (cj/--ai-vterm-last-direction, cj/--ai-vterm-last-size) capture at toggle-off and feed a custom display action. After M-S-t flips claude from right to bottom, F9 toggle-off-then-on returns it at the bottom. After a mouse resize, the next toggle restores that fraction. State is per-session. Restarts reset to default right/0.5. Two display-buffer fixes came out of testing: - save-window-excursion around (vterm name) keeps the dashboard from being buried on a fresh F9 at startup. vterm calls pop-to-buffer-same-window internally, which would otherwise replace the selected window's buffer before the alist could route the new one. - The action chain swaps display-buffer-use-some-window for a more specific cj/--ai-vterm-reuse-existing-claude. The generic version stole non-claude windows on C-F9 when the user was focused inside claude (claude on bottom, code on top -> new project landed in the code window). The specific version only reuses windows that already show a claude buffer. I reclaimed C-F9 from the gptel toggle in ai-config.el. C-; a t still binds gptel. I added eight new test files (claude-buffers, displayed-claude-window, dispatch, pick-buffer-candidates, window-geometry, capture-state, display-saved, reuse-existing-claude) plus a regression test on cj/--ai-vterm-show-or-create for the dashboard-preservation fix. All 73 ai-vterm tests pass and the full make test suite is green. --- tests/test-ai-vterm--claude-buffers.el | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/test-ai-vterm--claude-buffers.el (limited to 'tests/test-ai-vterm--claude-buffers.el') diff --git a/tests/test-ai-vterm--claude-buffers.el b/tests/test-ai-vterm--claude-buffers.el new file mode 100644 index 00000000..56668ca1 --- /dev/null +++ b/tests/test-ai-vterm--claude-buffers.el @@ -0,0 +1,63 @@ +;;; test-ai-vterm--claude-buffers.el --- Tests for cj/--ai-vterm-claude-buffers -*- lexical-binding: t; -*- + +;;; Commentary: +;; The helper returns the list of buffers whose names start with the +;; literal prefix "claude [". Order is the same order `buffer-list' +;; gives them (most-recently-selected first). Non-claude buffers and +;; buffers whose names merely contain the prefix as a substring are +;; excluded. + +;;; Code: + +(require 'ert) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'ai-vterm) + +(defun test-ai-vterm--claude-buffers-cleanup () + "Kill any leftover claude-prefixed buffers before/after a test." + (dolist (b (buffer-list)) + (when (string-prefix-p "claude [" (buffer-name b)) + (kill-buffer b)))) + +(ert-deftest test-ai-vterm--claude-buffers-empty-when-none-exist () + "Boundary: no claude-prefixed buffers anywhere -> empty list." + (test-ai-vterm--claude-buffers-cleanup) + (unwind-protect + (should (null (cj/--ai-vterm-claude-buffers))) + (test-ai-vterm--claude-buffers-cleanup))) + +(ert-deftest test-ai-vterm--claude-buffers-returns-only-claude-buffers () + "Normal: filters to only claude-prefixed buffers, leaves others alone." + (test-ai-vterm--claude-buffers-cleanup) + (let ((c1 (get-buffer-create "claude [a]")) + (c2 (get-buffer-create "claude [b]")) + (other (get-buffer-create "regular-buffer"))) + (unwind-protect + (let ((result (cj/--ai-vterm-claude-buffers))) + (should (memq c1 result)) + (should (memq c2 result)) + (should-not (memq other result)) + (should (= (length result) 2))) + (kill-buffer c1) + (kill-buffer c2) + (kill-buffer other)))) + +(ert-deftest test-ai-vterm--claude-buffers-anchors-prefix-not-substring () + "Boundary: 'foo claude [bar]' is not a claude buffer -- prefix anchored." + (test-ai-vterm--claude-buffers-cleanup) + (let ((not-claude (get-buffer-create "foo claude [bar]"))) + (unwind-protect + (should-not (memq not-claude (cj/--ai-vterm-claude-buffers))) + (kill-buffer not-claude)))) + +(ert-deftest test-ai-vterm--claude-buffers-bare-claude-not-included () + "Boundary: 'claude' alone (no bracket) doesn't match the 'claude [' prefix." + (test-ai-vterm--claude-buffers-cleanup) + (let ((bare (get-buffer-create "claude"))) + (unwind-protect + (should-not (memq bare (cj/--ai-vterm-claude-buffers))) + (kill-buffer bare)))) + +(provide 'test-ai-vterm--claude-buffers) +;;; test-ai-vterm--claude-buffers.el ends here -- cgit v1.2.3