-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathai-code-backends-infra-etc.el
More file actions
123 lines (105 loc) · 5.13 KB
/
Copy pathai-code-backends-infra-etc.el
File metadata and controls
123 lines (105 loc) · 5.13 KB
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
;;; ai-code-backends-infra-etc.el --- Side-panel resize extras for AI Code terminals -*- lexical-binding: t; -*-
;; Author: AI Agent
;; SPDX-License-Identifier: Apache-2.0
;;; Commentary:
;; Extra side-panel resize commands, keybindings, and platform compatibility
;; activation for AI Code sessions.
;;; Code:
(require 'cl-lib)
(require 'ai-code-backends-infra-platform)
(declare-function ai-code-backends-infra--fit-side-window-body-width
"ai-code-backends-infra" (window))
(declare-function ai-code-backends-infra--configure-session-buffer
"ai-code-backends-infra" (buffer &optional escape-fn multiline-input-sequence))
(declare-function ai-code-backends-infra--session-buffer-p
"ai-code-backends-infra" (buffer))
(declare-function ai-code-backends-infra--sync-terminal-dimensions
"ai-code-backends-infra" (buffer window))
(defvar ai-code-backends-infra-window-width)
(defvar ai-code-backends-infra-window-height)
(defconst ai-code-backends-infra-etc--side-window-sides
'(left right top bottom)
"Valid side positions for AI side windows.")
(defcustom ai-code-backends-infra-etc-resize-step 5
"Columns/rows to adjust on each panel resize command."
:type 'integer
:group 'ai-code-backends-infra)
(defun ai-code-backends-infra-etc--side-window-p (window)
"Return non-nil when WINDOW is one of the configured side windows."
(memq (window-parameter window 'window-side)
ai-code-backends-infra-etc--side-window-sides))
(defun ai-code-backends-infra-etc--active-side-window ()
"Return an active AI session side window, or nil when unavailable."
(let ((current-window (get-buffer-window (current-buffer) t)))
(cond
((and current-window
(ai-code-backends-infra-etc--side-window-p current-window)
(ai-code-backends-infra--session-buffer-p (current-buffer)))
current-window)
(t
(cl-find-if
(lambda (window)
(let ((buffer (window-buffer window)))
(and (window-live-p window)
(ai-code-backends-infra-etc--side-window-p window)
(buffer-live-p buffer)
(ai-code-backends-infra--session-buffer-p buffer))))
(window-list))))))
(defun ai-code-backends-infra-etc--apply-resize-delta (delta)
"Apply panel resize DELTA to the current AI session side window."
(let ((window (ai-code-backends-infra-etc--active-side-window)))
(unless window
(user-error "No visible AI side panel found"))
(let ((side (window-parameter window 'window-side))
(buffer (window-buffer window)))
(pcase side
((or 'left 'right)
(setq ai-code-backends-infra-window-width
(max 1 (+ ai-code-backends-infra-window-width delta)))
(ai-code-backends-infra--fit-side-window-body-width window))
((or 'top 'bottom)
(setq ai-code-backends-infra-window-height
(max 1 (+ ai-code-backends-infra-window-height delta)))
(let ((height-delta (- ai-code-backends-infra-window-height
(window-body-height window))))
(unless (zerop height-delta)
(window-resize window height-delta nil t))))
(_
(user-error "AI panel is not in a side window")))
(ai-code-backends-infra--sync-terminal-dimensions buffer window))))
;;;###autoload
(defun ai-code-backends-infra-etc-grow-panel ()
"Grow AI session side panel by `ai-code-backends-infra-etc-resize-step'."
(interactive)
(ai-code-backends-infra-etc--apply-resize-delta
ai-code-backends-infra-etc-resize-step))
;;;###autoload
(defun ai-code-backends-infra-etc-shrink-panel ()
"Shrink AI session side panel by `ai-code-backends-infra-etc-resize-step'."
(interactive)
(ai-code-backends-infra-etc--apply-resize-delta
(- ai-code-backends-infra-etc-resize-step)))
(defun ai-code-backends-infra-etc--bind-panel-resize-keys ()
"Install local resize keybindings in the current AI session buffer."
(local-set-key (kbd "C-.") #'ai-code-backends-infra-etc-grow-panel)
(local-set-key (kbd "C-,") #'ai-code-backends-infra-etc-shrink-panel))
(defun ai-code-backends-infra-etc--configure-session-buffer-advice (buffer &rest _args)
"Bind resize keys after shared session setup for BUFFER."
(when (buffer-live-p buffer)
(with-current-buffer buffer
(ai-code-backends-infra-etc--bind-panel-resize-keys))))
(defun ai-code-backends-infra-etc-activate ()
"Activate extra terminal behavior for AI session buffers."
(ai-code-backends-infra-platform-activate)
(unless (advice-member-p #'ai-code-backends-infra-etc--configure-session-buffer-advice
#'ai-code-backends-infra--configure-session-buffer)
(advice-add #'ai-code-backends-infra--configure-session-buffer
:after
#'ai-code-backends-infra-etc--configure-session-buffer-advice))
(dolist (buffer (buffer-list))
(when (and (buffer-live-p buffer)
(ai-code-backends-infra--session-buffer-p buffer))
(with-current-buffer buffer
(ai-code-backends-infra-etc--bind-panel-resize-keys)))))
(provide 'ai-code-backends-infra-etc)
;;; ai-code-backends-infra-etc.el ends here