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
use anyhow::Result;
use crossterm::{
cursor::SetCursorStyle,
event::{self, Event, KeyCode, KeyModifiers},
execute,
};
use hjkl_engine::{CursorShape, Host, VimMode};
use ratatui::{Terminal, backend::CrosstermBackend};
use std::io::Stdout;
use std::time::Duration;
use super::{App, STATUS_LINE_HEIGHT, SearchDir, prompt_cursor_shape};
use crate::render;
impl App {
/// Main event loop. Draws every frame, routes key events through
/// the vim FSM, handles resize, exits on Ctrl-C.
pub fn run(&mut self, terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
loop {
// Update host viewport dimensions from the current terminal size.
{
let size = terminal.size()?;
let vp = self.active_mut().editor.host_mut().viewport_mut();
vp.width = size.width;
vp.height = size.height.saturating_sub(STATUS_LINE_HEIGHT);
}
// Emit cursor shape before the draw call, once per transition.
let current_shape = if let Some(ref f) = self.command_field {
prompt_cursor_shape(f)
} else if let Some(ref f) = self.search_field {
prompt_cursor_shape(f)
} else {
self.active().editor.host().cursor_shape()
};
if current_shape != self.last_cursor_shape {
match current_shape {
CursorShape::Block => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyBlock);
}
CursorShape::Bar => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyBar);
}
CursorShape::Underline => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyUnderScore);
}
}
self.last_cursor_shape = current_shape;
}
// Draw the current frame.
terminal.draw(|frame| render::frame(frame, self))?;
// Wait for the next event with a 120 ms ceiling.
if !event::poll(Duration::from_millis(120))? {
// Timeout — advance the splash animation if active.
if let Some(ref mut screen) = self.start_screen {
screen.advance();
}
continue;
}
match event::read()? {
Event::Key(key) => {
if key.code == KeyCode::Char('c')
&& key.modifiers.contains(KeyModifiers::CONTROL)
{
if self.command_field.is_some() {
self.command_field = None;
continue;
}
if self.search_field.is_some() {
self.cancel_search_prompt();
continue;
}
break;
}
// Dismiss the start screen on any non-Ctrl-C keypress and
// let the key fall through to normal handling so `:`,
// `/`, `i`, etc. take effect on the same press.
if self.start_screen.is_some() {
self.start_screen = None;
}
self.status_message = None;
// ── Info popup dismissal ──────────────────────────────────
if self.info_popup.is_some() {
self.info_popup = None;
continue;
}
// ── Command palette (`:` prompt) ─────────────────────────
if self.command_field.is_some() {
self.handle_command_field_key(key);
if self.exit_requested {
break;
}
continue;
}
// ── Search prompt (`/` `?`) ──────────────────────────────
if self.search_field.is_some() {
self.handle_search_field_key(key);
if self.exit_requested {
break;
}
continue;
}
// ── Picker overlay ────────────────────────────────────────
if self.picker.is_some() {
self.handle_picker_key(key);
if self.exit_requested {
break;
}
continue;
}
// ── Git sub-command resolution ───────────────────────────
if self.pending_git && self.active().editor.vim_mode() == VimMode::Normal {
self.pending_git = false;
self.pending_leader = false;
match key.code {
KeyCode::Char('s') if key.modifiers == KeyModifiers::NONE => {
self.open_git_status_picker();
}
KeyCode::Char('l') if key.modifiers == KeyModifiers::NONE => {
self.open_git_log_picker();
}
KeyCode::Char('b') if key.modifiers == KeyModifiers::NONE => {
self.open_git_branch_picker();
}
// <leader>gB — file history for the current buffer.
// Uppercase B (Shift+b).
KeyCode::Char('B')
if key.modifiers == KeyModifiers::NONE
|| key.modifiers == KeyModifiers::SHIFT =>
{
self.open_git_file_history_picker();
}
// <leader>gS — stashes picker (uppercase S).
KeyCode::Char('S')
if key.modifiers == KeyModifiers::NONE
|| key.modifiers == KeyModifiers::SHIFT =>
{
self.open_git_stash_picker();
}
// <leader>gt — tags picker.
KeyCode::Char('t') if key.modifiers == KeyModifiers::NONE => {
self.open_git_tags_picker();
}
// <leader>gr — remotes picker.
KeyCode::Char('r') if key.modifiers == KeyModifiers::NONE => {
self.open_git_remotes_picker();
}
_ => {}
}
continue;
}
// ── Leader resolution ────────────────────────────────────
let leader = self.config.editor.leader;
if self.pending_leader && self.active().editor.vim_mode() == VimMode::Normal {
self.pending_leader = false;
if key.modifiers == KeyModifiers::NONE {
match key.code {
// The leader key itself + 'f' both open the file picker
// (matches buffr-style "press leader twice or leader+f").
KeyCode::Char(c) if c == leader => {
self.open_picker();
}
KeyCode::Char('f') => {
self.open_picker();
}
KeyCode::Char('b') => {
self.open_buffer_picker();
}
KeyCode::Char('/') => {
self.open_grep_picker(None);
}
KeyCode::Char('g') => {
// Begin git sub-command chord.
self.pending_git = true;
}
_ => {}
}
}
continue;
}
// ── Leader prefix ────────────────────────────────────────
if key.code == KeyCode::Char(leader)
&& key.modifiers == KeyModifiers::NONE
&& self.active().editor.vim_mode() == VimMode::Normal
{
self.pending_leader = true;
continue;
}
// ── Alt-buffer toggle (Ctrl-^ / Ctrl-6) ─────────────────
if self.active().editor.vim_mode() == VimMode::Normal
&& key.modifiers.contains(KeyModifiers::CONTROL)
&& (key.code == KeyCode::Char('^') || key.code == KeyCode::Char('6'))
{
self.buffer_alt();
continue;
}
// ── Shift-H / Shift-L cycle buffers ──────────────────────
// Only when more than one buffer is open; with a single
// slot fall through to the engine's H/L viewport motions.
if self.active().editor.vim_mode() == VimMode::Normal
&& self.slots.len() > 1
&& (key.modifiers == KeyModifiers::SHIFT
|| key.modifiers == KeyModifiers::NONE)
{
if key.code == KeyCode::Char('H') {
self.buffer_prev();
continue;
}
if key.code == KeyCode::Char('L') {
self.buffer_next();
continue;
}
}
// ── Buffer-motion pending state ──────────────────────────
if self.active().editor.vim_mode() == VimMode::Normal
&& key.modifiers == KeyModifiers::NONE
{
if let Some(prefix) = self.pending_buffer_motion.take() {
match (prefix, key.code) {
('g', KeyCode::Char('t')) => {
self.buffer_next();
continue;
}
('g', KeyCode::Char('T')) => {
self.buffer_prev();
continue;
}
(']', KeyCode::Char('b')) => {
self.buffer_next();
continue;
}
('[', KeyCode::Char('b')) => {
self.buffer_prev();
continue;
}
// Didn't match — forward only the current key;
// drop the pending prefix (g/]/[ alone has no
// other mapped meaning in our engine yet).
_ => {
self.active_mut().editor.handle_key(key);
continue;
}
}
}
} else {
// Any non-Normal key clears pending motions.
self.pending_buffer_motion = None;
self.pending_git = false;
self.pending_leader = false;
}
// ── Intercept `:` in Normal mode ─────────────────────────
if key.code == KeyCode::Char(':')
&& key.modifiers == KeyModifiers::NONE
&& self.active().editor.vim_mode() == VimMode::Normal
{
self.open_command_prompt();
continue;
}
// ── Intercept `/` and `?` in Normal mode ─────────────────
if key.modifiers == KeyModifiers::NONE
&& self.active().editor.vim_mode() == VimMode::Normal
{
if key.code == KeyCode::Char('/') {
self.open_search_prompt(SearchDir::Forward);
continue;
}
if key.code == KeyCode::Char('?') {
self.open_search_prompt(SearchDir::Backward);
continue;
}
}
// ── Set pending buffer-motion prefix ─────────────────────
if self.active().editor.vim_mode() == VimMode::Normal
&& key.modifiers == KeyModifiers::NONE
&& matches!(
key.code,
KeyCode::Char('g') | KeyCode::Char(']') | KeyCode::Char('[')
)
&& let KeyCode::Char(c) = key.code
{
self.pending_buffer_motion = Some(c);
// Fall through: also forward the key to the engine
// so its own `g`-pending state is updated correctly
// (the engine handles gj/gk/gg/G etc).
}
// ── Normal editor key handling ───────────────────────────
self.active_mut().editor.handle_key(key);
// Drain dirty for the persistent UI flag.
if self.active_mut().editor.take_dirty() {
let elapsed = self.active_mut().refresh_dirty_against_saved();
self.last_signature_us = elapsed;
if self.active().dirty {
self.active_mut().is_new_file = false;
}
}
// Fan engine ContentEdits into the syntax tree.
let buffer_id = self.active().buffer_id;
if self.active_mut().editor.take_content_reset() {
self.syntax.reset(buffer_id);
}
let edits = self.active_mut().editor.take_content_edits();
if !edits.is_empty() {
self.syntax.apply_edits(buffer_id, &edits);
}
self.recompute_and_install();
}
Event::Resize(w, h) => {
let vp = self.active_mut().editor.host_mut().viewport_mut();
vp.width = w;
vp.height = h.saturating_sub(STATUS_LINE_HEIGHT);
}
Event::FocusGained => {
self.checktime_all();
}
_ => {}
}
if self.exit_requested {
break;
}
}
Ok(())
}
}