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
//! App-level chord and action dispatch — `dispatch_action`, `dispatch_keymap`, and chord timeout.
use super::App;
use super::keymap;
impl App {
/// Dispatch an [`crate::keymap_actions::AppAction`] with an optional repeat count.
///
/// This is the single authoritative dispatch site for all chord-triggered
/// app actions. Routing by domain — each cluster delegates to a focused
/// sub-dispatcher that lives in the corresponding glue module:
/// - picker opens → inline (3 one-liners)
/// - git actions → `picker_glue::dispatch_git_action`
/// - LSP actions → `lsp_glue::dispatch_lsp_action`
/// - window actions → `window::dispatch_window_action` (incl. TmuxNavigate)
/// - buffer actions → `buffer_ops::dispatch_buffer_action`
/// - prompt actions → `prompt::dispatch_prompt_action`
/// - pending-state → `pending_actions::dispatch_pending_state_action`
/// - engine actions → `engine_actions::dispatch_engine_action`
/// - QuitOrClose → inline (app-lifecycle, 5 LOC)
pub fn dispatch_action(&mut self, action: crate::keymap_actions::AppAction, count: u32) {
use crate::keymap_actions::AppAction;
let count = count.max(1) as usize;
match action {
// ── File / buffer pickers (open) ───────────────────────────────
AppAction::OpenFilePicker => self.open_picker(),
AppAction::OpenBufferPicker => self.open_buffer_picker(),
AppAction::OpenGrepPicker => self.open_grep_picker(None),
// ── Git picker openers ─────────────────────────────────────────
AppAction::GitStatus
| AppAction::GitLog
| AppAction::GitBranch
| AppAction::GitFileHistory
| AppAction::GitStashes
| AppAction::GitTags
| AppAction::GitRemotes => self.dispatch_git_action(action),
// ── LSP + diagnostic navigation ────────────────────────────────
AppAction::ShowDiagAtCursor
| AppAction::LspCodeActions
| AppAction::LspRename
| AppAction::LspGotoDef
| AppAction::LspGotoDecl
| AppAction::LspGotoRef
| AppAction::LspGotoImpl
| AppAction::LspGotoTypeDef
| AppAction::LspHover
| AppAction::DiagNext
| AppAction::DiagPrev
| AppAction::DiagNextError
| AppAction::DiagPrevError => self.dispatch_lsp_action(action),
// ── Window / layout management ─────────────────────────────────
AppAction::FocusLeft
| AppAction::FocusBelow
| AppAction::FocusAbove
| AppAction::FocusRight
| AppAction::FocusNext
| AppAction::FocusPrev
| AppAction::CloseFocusedWindow
| AppAction::OnlyFocusedWindow
| AppAction::SwapWithSibling
| AppAction::MoveWindowToNewTab
| AppAction::NewSplit
| AppAction::ResizeHeight(_)
| AppAction::ResizeWidth(_)
| AppAction::EqualizeLayout
| AppAction::MaximizeHeight
| AppAction::MaximizeWidth
| AppAction::TmuxNavigate(_) => self.dispatch_window_action(action, count),
// ── Buffer / tab navigation ────────────────────────────────────
AppAction::Tabnext
| AppAction::Tabprev
| AppAction::BufferNext
| AppAction::BufferPrev
| AppAction::BufferAlt
| AppAction::BufferCycleH
| AppAction::BufferCycleL => self.dispatch_buffer_action(action, count),
// ── Prompt / overlay entry ─────────────────────────────────────
AppAction::OpenCommandPrompt | AppAction::OpenSearchPrompt(_) => {
self.dispatch_prompt_action(action)
}
// ── Pending-state chords ───────────────────────────────────────
AppAction::BeginPendingReplace { .. }
| AppAction::BeginPendingFind { .. }
| AppAction::BeginPendingAfterG { .. }
| AppAction::BeginPendingAfterZ { .. }
| AppAction::BeginPendingAfterOp { .. }
| AppAction::BeginPendingSelectRegister
| AppAction::BeginPendingSetMark
| AppAction::BeginPendingGotoMarkLine
| AppAction::BeginPendingGotoMarkChar
| AppAction::QChord { .. }
| AppAction::BeginPendingPlayMacro { .. } => self.dispatch_pending_state_action(action),
// ── App lifecycle ──────────────────────────────────────────────
AppAction::QuitOrClose => {
if self.layout().leaves().len() > 1 {
self.close_focused_window();
} else {
self.exit_requested = true;
}
}
// ── Engine-mutating actions ────────────────────────────────────
_ => self.dispatch_engine_action(action, count),
}
}
/// Feed a crossterm key event through the app-level chord keymap and
/// dispatch any resolved action. Returns `true` if the key was consumed
/// (either resolved or still pending), `false` if the keymap returned
/// `Unbound` and the caller should replay the events to the engine.
///
/// Replayed events are stored in `out_replay` (never `None`-cleared).
///
/// This is a thin shim over [`dispatch_keymap_in_mode`] fixed to Normal mode.
pub fn dispatch_keymap(
&mut self,
km_ev: hjkl_keymap::KeyEvent,
count: u32,
out_replay: &mut Vec<hjkl_keymap::KeyEvent>,
) -> bool {
self.dispatch_keymap_in_mode(km_ev, count, out_replay, keymap::HjklMode::Normal)
}
/// Mode-generalized chord dispatch. Feed `km_ev` into the trie for `mode`
/// and dispatch any resolved action.
///
/// Returns `true` if consumed (Pending / Ambiguous / Match),
/// `false` if Unbound (events stored in `out_replay`).
pub fn dispatch_keymap_in_mode(
&mut self,
km_ev: hjkl_keymap::KeyEvent,
count: u32,
out_replay: &mut Vec<hjkl_keymap::KeyEvent>,
mode: keymap::HjklMode,
) -> bool {
use hjkl_keymap::KeyResolve;
let now = std::time::Instant::now();
match self.app_keymap.feed(mode, km_ev, now) {
KeyResolve::Pending => {
self.note_prefix_set();
true
}
KeyResolve::Ambiguous => {
self.note_prefix_set();
true
}
KeyResolve::Match(binding) => {
self.clear_prefix_state();
self.dispatch_action(binding.action, count);
true
}
KeyResolve::Unbound(events) => {
self.clear_prefix_state();
out_replay.extend(events);
false
}
}
}
/// Force-resolve a pending chord buffer after the keymap timeout has
/// elapsed. Called from the event loop's poll-timeout branch when a chord
/// is pending (typically `Ambiguous`: e.g. both `g` and `gd` bound — the
/// shorter binding fires after `timeoutlen`).
///
/// Returns:
/// - `Some(events)` to be replayed to the engine for `Unbound` with
/// drained events (real dead-end case).
/// - `Some(empty)` after a `Match` (the action was already dispatched).
/// - `None` when the buffer was empty OR when the buffer is a pure prefix
/// (user is mid-chord and `timeout_resolve` left the buffer in place —
/// needed so the which-key popup stays visible past the timeout).
pub fn resolve_chord_timeout(
&mut self,
mode: keymap::HjklMode,
) -> Option<Vec<hjkl_keymap::KeyEvent>> {
use hjkl_keymap::KeyResolve;
if self.app_keymap.pending(mode).is_empty() {
return None;
}
match self.app_keymap.timeout_resolve(mode) {
KeyResolve::Match(binding) => {
self.clear_prefix_state();
self.dispatch_action(binding.action, 1);
Some(Vec::new())
}
KeyResolve::Unbound(events) if events.is_empty() => {
// Pure-prefix: timeout_resolve was a no-op. Keep prefix state
// alive so the which-key popup stays visible.
None
}
KeyResolve::Unbound(events) => {
self.clear_prefix_state();
Some(events)
}
// timeout_resolve only returns Match or Unbound; defensive fallthrough.
_ => None,
}
}
}