1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
;;; ai-vterm.el --- In-Emacs Claude launcher with vertical-split vterm -*- lexical-binding: t; -*-
;; Author: Craig Jennings <c@cjennings.net>
;;; Commentary:
;; Picks a Claude-template project (a dir under ~/.emacs.d, ~/code/*, or
;; ~/projects/* containing .ai/protocols.org), opens or reuses a vterm
;; buffer named "claude [<basename>]", sends Claude Code's startup
;; instruction to it, and routes the buffer to a right-side window via
;; display-buffer-alist. Multiple projects produce multiple coexisting
;; buffers that share the same right-side slot; switching among them is a
;; buffer-switch, not a kill-and-recreate.
;;
;; Three F-key entry points:
;;
;; - F9 `cj/ai-vterm' -- DWIM dispatch. If a claude buffer is
;; currently displayed in this frame, F9 quits its window
;; (toggle off). Otherwise, if exactly one claude buffer is
;; alive, F9 re-displays it; if zero or two-plus are alive, F9
;; falls through to the project picker.
;; - C-F9 `cj/ai-vterm-pick-project' -- always show the project
;; picker, even when a claude buffer is currently displayed.
;; Used when the user wants to start a new project session
;; instead of toggling the current one.
;; - M-F9 `cj/ai-vterm-pick-buffer' -- pick from the alive claude
;; buffers (no project candidates, no creation). When a claude
;; buffer is currently displayed, the picked buffer replaces it
;; in that window so orientation and size are preserved.
;;
;; Existing windmove (Shift-arrows) handles code <-> Claude focus
;; toggling. Buffer-move (C-M-arrows) handles side-swap. Neither
;; needs anything new from this module.
;;; Code:
(require 'cl-lib)
(require 'seq)
(declare-function vterm "vterm" (&optional buffer-name))
(declare-function vterm-send-string "vterm" (string &optional paste-p))
(declare-function vterm-send-return "vterm" ())
(defgroup ai-vterm nil
"In-Emacs Claude launcher with vertical-split vterm."
:group 'tools)
(defcustom cj/ai-vterm-claude-command
"claude \"Read .ai/protocols.org and follow all instructions.\""
"Shell command sent to a fresh AI-vterm to start Claude Code."
:type 'string
:group 'ai-vterm)
(defvar cj/--ai-vterm-suppress-tmux nil
"When non-nil, the generic vterm tmux-launch hook skips its auto-tmux step.
ai-vterm dynamically binds this around `(vterm)' so the hook in
eshell-vterm-config.el doesn't send a bare \"tmux\\n\" before the named
session launch command runs. The hook reads the variable via
`bound-and-true-p' so loading order between the two modules doesn't
matter.")
(defcustom cj/ai-vterm-project-roots
(list (expand-file-name "~/.emacs.d"))
"Directories that are themselves Claude-template projects.
Each entry is included as a candidate when it exists and contains
.ai/protocols.org. Use this for single-project roots like ~/.emacs.d."
:type '(repeat directory)
:group 'ai-vterm)
(defcustom cj/ai-vterm-container-roots
(list (expand-file-name "~/code")
(expand-file-name "~/projects"))
"Directories whose immediate children are scanned for Claude projects.
Each entry's child directories are included as candidates when they
contain .ai/protocols.org. Use this for container dirs like ~/code."
:type '(repeat directory)
:group 'ai-vterm)
(defconst cj/--ai-vterm-name-prefix "claude ["
"Buffer-name prefix shared by all AI-vterm buffers.
Single source of truth for both buffer construction in
`cj/--ai-vterm-buffer-name' and detection in
`cj/--ai-vterm-buffer-p'. The display-buffer-alist rule keys on the
escaped form \"\\\\`claude \\\\[\" -- they must stay in sync.")
(defun cj/--ai-vterm-buffer-name (dir)
"Return the AI-vterm buffer name for project directory DIR.
The name pattern is \"claude [<basename>]\". The display-buffer-alist
rule keys on the literal prefix \"claude [\", so changing the format
breaks routing to the right-side window."
(format "%s%s]"
cj/--ai-vterm-name-prefix
(file-name-nondirectory (directory-file-name dir))))
(defun cj/--ai-vterm-buffer-p (buffer)
"Return non-nil when BUFFER is an AI-vterm buffer.
A buffer qualifies when its name starts with the literal prefix in
`cj/--ai-vterm-name-prefix' (\"claude [\"). The check is anchored at
the start so names like \"foo claude [bar]\" do not match."
(and (bufferp buffer)
(buffer-live-p buffer)
(string-prefix-p cj/--ai-vterm-name-prefix (buffer-name buffer))))
(defun cj/--ai-vterm-claude-buffers ()
"Return the live AI-vterm buffers in `buffer-list' order.
Order matches `buffer-list' on the selected frame, which is most-
recently-selected first. Non-AI-vterm buffers are filtered out via
`cj/--ai-vterm-buffer-p'."
(seq-filter #'cj/--ai-vterm-buffer-p (buffer-list)))
(defun cj/--ai-vterm-displayed-claude-window (&optional frame)
"Return a window in FRAME currently displaying an AI-vterm buffer, or nil.
FRAME defaults to the selected frame. When more than one window in
the frame shows a claude buffer, the first one in `window-list' order
is returned. The minibuffer is excluded from the search."
(seq-find (lambda (w)
(cj/--ai-vterm-buffer-p (window-buffer w)))
(window-list (or frame (selected-frame)) 'never)))
(defun cj/--ai-vterm-tmux-session-name (dir)
"Return the tmux name derived from project directory DIR.
The basename of DIR, with any run of whitespace collapsed to a single
hyphen so the result is safe to pass on a tmux command line."
(replace-regexp-in-string
"[[:space:]]+" "-"
(file-name-nondirectory (directory-file-name dir))))
(defun cj/--ai-vterm-launch-command (dir)
"Return the shell command line that runs Claude in a project tmux session.
Uses `tmux new-session -A' so a second F9 on the same project reattaches
to the running session instead of spawning a new one. The session name
is the project's basename via `cj/--ai-vterm-tmux-session-name'.
The shell command run on first creation is
<claude-command>; exec bash
so the tmux window survives Claude exiting -- the session stays alive
with a bare bash prompt for recovery, and reattach works the same way."
(let ((session (cj/--ai-vterm-tmux-session-name dir))
(start-dir (expand-file-name dir)))
(format "tmux new-session -A -s %s -c %s '%s'"
(shell-quote-argument session)
(shell-quote-argument start-dir)
(concat cj/ai-vterm-claude-command "; exec bash"))))
(defun cj/--ai-vterm-has-marker-p (dir)
"Return non-nil when DIR contains .ai/protocols.org."
(file-exists-p (expand-file-name ".ai/protocols.org" dir)))
(defun cj/--ai-vterm-candidates ()
"Return the list of Claude-template project paths.
Each entry of `cj/ai-vterm-project-roots' contributes itself when it
exists and contains .ai/protocols.org. Each entry of
`cj/ai-vterm-container-roots' contributes its immediate child
directories that contain .ai/protocols.org.
Returns absolute paths. Nonexistent roots are skipped silently."
(let (result)
(dolist (root cj/ai-vterm-project-roots)
(let ((expanded (expand-file-name root)))
(when (and (file-directory-p expanded)
(cj/--ai-vterm-has-marker-p expanded))
(push expanded result))))
(dolist (root cj/ai-vterm-container-roots)
(let ((expanded (expand-file-name root)))
(when (file-directory-p expanded)
(dolist (child (directory-files
expanded t directory-files-no-dot-files-regexp t))
(when (and (file-directory-p child)
(cj/--ai-vterm-has-marker-p child))
(push child result))))))
(nreverse result)))
(defun cj/--ai-vterm-process-live-p (buffer)
"Return non-nil when BUFFER has a live process attached."
(let ((proc (get-buffer-process buffer)))
(and proc (process-live-p proc))))
(defcustom cj/ai-vterm-window-width 0.5
"Default fraction of frame allocated to the AI-vterm window.
Used by `cj/--ai-vterm-display-saved' as the size fallback when
`cj/--ai-vterm-last-size' is nil (i.e. the user hasn't yet toggled
off a claude window in this session). Applies to both width and
height axes -- the same fallback fraction is used for either default
direction."
:type 'number
:group 'ai-vterm)
(defvar cj/--ai-vterm-last-direction nil
"Last user-chosen direction for the AI-vterm display.
Symbol: right, below, left, or above. nil means no claude window
has been toggled off yet this session, so the default direction
applies. Captured at toggle-off by `cj/--ai-vterm-capture-state'
and consumed by `cj/--ai-vterm-display-saved'.")
(defvar cj/--ai-vterm-last-size nil
"Last user-chosen size fraction for the AI-vterm display.
Float between 0 and 1, expressed on the axis matching
`cj/--ai-vterm-last-direction' (width fraction for right/left,
height fraction for below/above). nil means use the customizable
default `cj/ai-vterm-window-width'.")
(defun cj/--ai-vterm-window-direction (window)
"Return the side WINDOW occupies in its frame.
Returns one of right, below, left, above. Falls back to right when
WINDOW fills its frame's root area (single-window or atypical
layout), since right is the module's default split direction.
Comparison uses `frame-root-window' edges rather than frame edges so
the minibuffer doesn't make every full-area window look like it
fails to span the full height."
(let* ((root (frame-root-window (window-frame window)))
(edges (window-edges window))
(root-edges (window-edges root))
(left (nth 0 edges))
(top (nth 1 edges))
(right (nth 2 edges))
(bottom (nth 3 edges))
(root-left (nth 0 root-edges))
(root-top (nth 1 root-edges))
(root-right (nth 2 root-edges))
(root-bottom (nth 3 root-edges))
(spans-full-width (and (= left root-left) (= right root-right)))
(spans-full-height (and (= top root-top) (= bottom root-bottom))))
(cond
((not spans-full-width) (if (= left root-left) 'left 'right))
((not spans-full-height) (if (= top root-top) 'above 'below))
(t 'right))))
(defun cj/--ai-vterm-window-fraction (window direction)
"Return WINDOW's size as a fraction of the frame's root on DIRECTION's axis.
For right/left, returns WINDOW's total-width / root's total-width.
For below/above, total-height / root's total-height. The root
window excludes the minibuffer so the fraction matches what
`display-buffer-in-direction' will use as window-width or
window-height when re-creating the split."
(let ((root (frame-root-window (window-frame window))))
(if (memq direction '(right left))
(/ (float (window-total-width window))
(window-total-width root))
(/ (float (window-total-height window))
(window-total-height root)))))
(defun cj/--ai-vterm-capture-state (window)
"Capture WINDOW's direction and size into module-level state.
Sets `cj/--ai-vterm-last-direction' and `cj/--ai-vterm-last-size'
so a subsequent F9 display can restore the user's chosen orientation
and size. Called at toggle-off (just before `quit-window' tears the
window down).
Does nothing when WINDOW is not live."
(when (window-live-p window)
(let* ((dir (cj/--ai-vterm-window-direction window))
(frac (cj/--ai-vterm-window-fraction window dir)))
(setq cj/--ai-vterm-last-direction dir
cj/--ai-vterm-last-size frac))))
(defun cj/--ai-vterm-reuse-existing-claude (buffer _alist)
"Display-buffer action: reuse any window in this frame already showing
a claude buffer.
Looks up `cj/--ai-vterm-displayed-claude-window' on the selected
frame. When a claude window exists, replaces its buffer with BUFFER
and returns the window. When none exists, returns nil so the next
action in the chain runs.
This is more specific than `display-buffer-use-some-window', which
would happily steal any non-selected window (e.g. a code window
above the claude split) when the user is focused in claude and
swaps projects via C-F9. The selective lookup here keeps non-claude
windows undisturbed and preserves the user's split geometry across
project changes."
(let ((win (cj/--ai-vterm-displayed-claude-window)))
(when win
(set-window-buffer win buffer)
win)))
(defun cj/--ai-vterm-display-saved (buffer alist)
"Display-buffer action: split per saved direction and size.
Reads `cj/--ai-vterm-last-direction' and `cj/--ai-vterm-last-size'
(falling back to right and `cj/ai-vterm-window-width' when nil) and
delegates to `display-buffer-in-direction' with an alist that carries
the saved values.
Any direction/window-width/window-height entries in ALIST are
stripped so the saved-state values control placement -- callers
shouldn't specify direction or size in the rule when this action is
used."
(let* ((direction (or cj/--ai-vterm-last-direction 'right))
(size (or cj/--ai-vterm-last-size cj/ai-vterm-window-width))
(size-key (if (memq direction '(right left))
'window-width
'window-height))
(filtered (cl-remove-if
(lambda (cell)
(memq (car-safe cell)
'(direction window-width window-height)))
alist))
(effective (append
(list (cons 'direction direction)
(cons size-key size))
filtered)))
(display-buffer-in-direction buffer effective)))
(defun cj/--ai-vterm-display-rule-list ()
"Return the `display-buffer-alist' entry list installed by this module.
The single rule routes any buffer whose name starts with \"claude [\"
through three actions in order:
1. `display-buffer-reuse-window' -- if the same buffer is already
visible in any window, focus that one.
2. `cj/--ai-vterm-reuse-existing-claude' -- otherwise, if any
window in this frame already shows a claude-prefixed buffer,
swap its buffer for the new one (preserves geometry across
project changes via C-F9).
3. `cj/--ai-vterm-display-saved' -- otherwise, split per the saved
direction + size from the last toggle-off (or defaults when no
capture has happened this session).
`display-buffer-in-side-window' is avoided deliberately. Side
windows enforce dedication, which breaks `buffer-move' (C-M-arrows)
and `switch-to-buffer' replacement. The chain above keeps the
resulting window an ordinary window so all standard window commands
work.
`display-buffer-use-some-window' is also avoided -- it would happily
steal any non-selected window (e.g. a code window above a claude
split) when the user is focused in claude and switches projects."
'(("\\`claude \\["
(display-buffer-reuse-window
cj/--ai-vterm-reuse-existing-claude
cj/--ai-vterm-display-saved)
(inhibit-same-window . t))))
(dolist (entry (cj/--ai-vterm-display-rule-list))
(add-to-list 'display-buffer-alist entry))
(defun cj/--ai-vterm-show-or-create (dir name)
"Show or create the AI-vterm buffer for project DIR with buffer NAME.
If a buffer named NAME exists with a live process, display it. If
the buffer exists but its process is dead, kill it and recreate. If
no such buffer exists, create a new vterm in DIR and send the
project's tmux launch command (see `cj/--ai-vterm-launch-command') so
the same project basename reattaches across Emacs restarts.
The dynamic binding of `cj/--ai-vterm-suppress-tmux' around `(vterm)'
suppresses the generic tmux-launch hook in eshell-vterm-config.el so
it doesn't fire a bare \"tmux\\n\" before the project-named launch
command runs.
Returns the buffer."
(let ((existing (get-buffer name)))
(cond
((and existing (cj/--ai-vterm-process-live-p existing))
(display-buffer existing)
existing)
(t
(when existing
(kill-buffer existing))
;; `vterm' calls pop-to-buffer-same-window internally, which
;; replaces the selected window's buffer (e.g. the dashboard at
;; fresh startup) before our display-buffer-alist rule has a
;; chance to route it. `save-window-excursion' reverts that
;; side-effect; the explicit display-buffer call below then
;; routes the buffer through the alist into a right-side split.
(save-window-excursion
(let ((default-directory dir)
(cj/--ai-vterm-suppress-tmux t))
(vterm name)))
(let ((buf (get-buffer name)))
(with-current-buffer buf
(vterm-send-string (cj/--ai-vterm-launch-command dir))
(vterm-send-return))
(display-buffer buf)
buf)))))
(defun cj/--ai-vterm-pick-project ()
"Prompt for a Claude-template project; return its absolute path.
Candidates come from `cj/--ai-vterm-candidates'. Display uses
`abbreviate-file-name' so paths read as ~/code/foo instead of the
full home-dir form. Signals `user-error' when no candidates exist."
(let ((candidates (cj/--ai-vterm-candidates)))
(unless candidates
(user-error "No Claude-template projects found under %s"
(mapconcat #'identity
(append cj/ai-vterm-project-roots
cj/ai-vterm-container-roots)
", ")))
(let* ((display-alist
(mapcar (lambda (p) (cons (abbreviate-file-name p) p))
candidates))
(chosen (completing-read "AI vterm project: "
display-alist nil t)))
(or (cdr (assoc chosen display-alist))
(expand-file-name chosen)))))
(defun cj/--ai-vterm-dispatch ()
"Compute the F9 (`cj/ai-vterm') action without performing it.
Returns one of:
- (toggle-off . WINDOW) -- claude is displayed in WINDOW; quit it.
- (redisplay-single . BUFFER) -- exactly one alive claude buffer; show it.
- (pick-project) -- zero or 2+ alive claude buffers; prompt.
A pure-decision helper so the dispatch logic is exercisable in tests
without firing real `display-buffer' or `quit-window' calls."
(let ((win (cj/--ai-vterm-displayed-claude-window)))
(cond
(win (cons 'toggle-off win))
(t
(let ((buffers (cj/--ai-vterm-claude-buffers)))
(cond
((= (length buffers) 1) (cons 'redisplay-single (car buffers)))
(t '(pick-project))))))))
(defun cj/--ai-vterm-pick-buffer-candidates (buffers shown-buffer)
"Build the M-F9 picker alist.
BUFFERS is an MRU-ordered list of alive AI-vterm buffers.
SHOWN-BUFFER is the AI-vterm buffer currently displayed in this frame,
or nil.
When SHOWN-BUFFER is one of BUFFERS, it sorts last with a
\" [shown]\" suffix so the default `completing-read' selection lands
on a non-shown candidate (i.e. RET picks \"the other one\"). When
SHOWN-BUFFER is not in BUFFERS (a stale window state), every entry is
treated as non-shown.
Each cell is (DISPLAY-NAME . BUFFER)."
(let ((non-shown (seq-remove (lambda (b) (eq b shown-buffer)) buffers))
(shown (when (memq shown-buffer buffers) shown-buffer)))
(append
(mapcar (lambda (b) (cons (buffer-name b) b)) non-shown)
(when shown
(list (cons (format "%s [shown]" (buffer-name shown)) shown))))))
(defun cj/ai-vterm-pick-project (&optional arg)
"Pick a Claude-template project and open or reuse its vterm.
The project is picked from a filtered completing-read list of dirs
that contain .ai/protocols.org. The vterm buffer is named
\"claude [<basename>]\" and is routed to a right-side window via
`display-buffer-alist'. Multiple projects coexist as separate
buffers; reinvoking on the same project reuses its existing vterm.
With prefix ARG, display the buffer without selecting its window.
Bound to C-F9 -- always shows the project picker, even when a claude
buffer is currently displayed."
(interactive "P")
(let* ((dir (cj/--ai-vterm-pick-project))
(name (cj/--ai-vterm-buffer-name dir))
(buf (cj/--ai-vterm-show-or-create dir name)))
(unless arg
(let ((win (get-buffer-window buf)))
(when win (select-window win))))
buf))
(defun cj/ai-vterm-pick-buffer ()
"Pick from the alive AI-vterm buffers; switch to the chosen one.
When an AI-vterm buffer is currently displayed in this frame, the
picked buffer replaces it in the same window via `set-window-buffer'.
Orientation and size are preserved so the user's split layout doesn't
change. When no AI-vterm buffer is displayed, default placement
applies via `display-buffer'.
The currently-displayed buffer (if any) is sorted last in the picker
with a \" [shown]\" suffix; the default selection lands on a
non-shown candidate so RET means \"give me the other one\".
Signals `user-error' when no AI-vterm buffers exist.
Bound to M-F9."
(interactive)
(let ((buffers (cj/--ai-vterm-claude-buffers)))
(unless buffers
(user-error "No Claude buffers"))
(let* ((shown-win (cj/--ai-vterm-displayed-claude-window))
(shown-buf (and shown-win (window-buffer shown-win)))
(alist (cj/--ai-vterm-pick-buffer-candidates buffers shown-buf))
(chosen (completing-read "AI vterm buffer: " alist nil t))
(buf (cdr (assoc chosen alist))))
(cond
((and shown-win (window-live-p shown-win))
(set-window-buffer shown-win buf)
(select-window shown-win))
(t
(display-buffer buf)
(let ((w (get-buffer-window buf)))
(when w (select-window w)))))
buf)))
(defun cj/ai-vterm (&optional arg)
"Smart F9 dispatch for the AI-vterm launcher.
Behavior depends on the current state:
- If an AI-vterm buffer is currently displayed in this frame, F9
quits its window (toggle off, buffer stays alive).
- Else, if exactly one alive AI-vterm buffer exists, F9 re-displays
it (DWIM -- the obvious next step is to look at it).
- Else (zero or 2+), F9 falls through to `cj/ai-vterm-pick-project'.
With prefix ARG, display the buffer without selecting its window
when a buffer is being shown (no effect on the toggle-off branch).
See `cj/ai-vterm-pick-project' (C-F9) to force the project picker
and `cj/ai-vterm-pick-buffer' (M-F9) to switch among existing
AI-vterm buffers without touching the project list."
(interactive "P")
(pcase (cj/--ai-vterm-dispatch)
(`(toggle-off . ,win)
(cj/--ai-vterm-capture-state win)
(quit-window nil win)
nil)
(`(redisplay-single . ,buf)
(display-buffer buf)
(unless arg
(let ((w (get-buffer-window buf)))
(when w (select-window w))))
buf)
(`(pick-project)
(cj/ai-vterm-pick-project arg))))
(keymap-global-set "<f9>" #'cj/ai-vterm)
(keymap-global-set "C-<f9>" #'cj/ai-vterm-pick-project)
(keymap-global-set "M-<f9>" #'cj/ai-vterm-pick-buffer)
(provide 'ai-vterm)
;;; ai-vterm.el ends here
|