hjkl 0.34.2

Vim-modal terminal editor: standalone TUI built on the hjkl engine.
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
//! Script-mode runner for `hjkl --headless`. No TUI, no event loop.
//!
//! Opens each file, dispatches the `+cmd` / `-c` command stream through the
//! editor ex dispatcher, writes back to disk when `:w` / `:wq` / `:x` runs,
//! and exits. ratatui and crossterm are never initialised.
//!
//! # No auto-write
//!
//! Like vim's `--headless` mode, hjkl does **not** auto-save buffers. You must
//! include an explicit write command (`:w`, `:wq`, `:x`) in your command
//! stream. Exiting without writing leaves the file on disk unchanged.
//!
//! # Exit codes
//!
//! - `0` — all commands completed without errors.
//! - `1` — at least one ex-command returned an `Error` effect, or an I/O
//!   failure occurred while reading or writing a file.
//!
//! # Command ordering
//!
//! All `-c CMD` commands are dispatched first (in flag order), then all `+cmd`
//! tokens (in argv order). Document this in your scripts if ordering matters.

use std::path::PathBuf;

use anyhow::Result;
use hjkl_buffer::View;
use hjkl_engine::{BufferEdit, DefaultHost, Editor, Options};
use hjkl_ex::ExEffect;

/// Run in headless (script) mode.
///
/// `files` — list of files to edit in sequence. When empty, a single
/// anonymous scratch buffer is used (mirrors `nvim --headless` behaviour).
///
/// `commands` — ex commands to dispatch against each file (without the
/// leading `:`). `-c` commands are prepended by the caller; `+cmd` tokens
/// are appended.
///
/// Returns the desired process exit code: `0` on full success, `1` on any
/// ex-command error or I/O failure.
pub fn run(files: Vec<PathBuf>, commands: Vec<String>) -> Result<i32> {
    // Stdout carries command output here — keep the clipboard off so an OSC 52
    // fallback can't inject escapes into it (#264).
    crate::host::disable_clipboard_for_rpc();
    if files.is_empty() && commands.is_empty() {
        eprintln!("hjkl --headless: no commands or files; exiting");
        return Ok(0);
    }

    let targets: Vec<Option<PathBuf>> = if files.is_empty() {
        vec![None]
    } else {
        files.into_iter().map(Some).collect()
    };

    let mut exit_code = 0i32;

    // Session-shared banks (registers, global marks, last :s command,
    // abbrevs, search pattern, changelist), created ONCE up front and wired
    // into every file's editor below — mirroring how `App::new` shares one
    // bank across every window/split. Without this, each file got a fresh
    // `vim_editor` with its own private banks, so e.g. a yank while
    // processing one file was invisible while processing the next — unlike
    // the TUI, where all editors share the six banks (audit R2, fix 4).
    let banks = SharedBanks::new();

    for maybe_path in targets {
        let display_name = maybe_path
            .as_ref()
            .map(|p| p.display().to_string())
            .unwrap_or_else(|| "<scratch>".to_string());

        // --- load buffer ---
        let mut buffer = View::new();
        let mut is_new_file = false;

        if let Some(ref path) = maybe_path {
            match std::fs::read_to_string(path) {
                Ok(content) => {
                    let content = content.strip_suffix('\n').unwrap_or(&content);
                    BufferEdit::replace_all(&mut buffer, content);
                }
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    is_new_file = true;
                }
                Err(e) => {
                    eprintln!("hjkl: {display_name}: {e}");
                    exit_code = 1;
                    continue;
                }
            }
        }

        let _ = is_new_file; // tracked for callers; file is created on first :w

        // --- build editor ---
        let host = DefaultHost::new();
        let mut editor = hjkl_vim::vim_editor(buffer, host, Options::default());
        // Wire in the session-shared banks (see above) so registers/marks/
        // last-substitute/abbrevs/search/changelist state set while
        // processing one file is visible while processing the next.
        banks.apply(&mut editor);

        // Track current save target. Starts as the source path; `:w <path>`
        // updates it so subsequent `:w` writes to the new location.
        let mut current_filename: Option<PathBuf> = maybe_path.clone();
        let mut wrote = false;

        // --- dispatch commands ---
        for cmd in &commands {
            // Strip an optional leading `:` so both `-c ':wq'` and `-c 'wq'`
            // work — matches the `+:cmd` / `+cmd` tolerance for `+` tokens.
            let cmd = cmd.strip_prefix(':').unwrap_or(cmd);
            let reg = hjkl_ex::default_registry::<hjkl_engine::DefaultHost>();
            let effect = hjkl_ex::try_dispatch(&reg, &mut editor, cmd)
                .unwrap_or(ExEffect::Unknown(cmd.to_string()));
            match effect {
                ExEffect::None => {}

                // Quickfix / location list have no popup in headless mode — no-op.
                ExEffect::Quickfix(_) | ExEffect::Location(_) => {}

                ExEffect::Ok => {}

                ExEffect::Info(_) | ExEffect::InfoTitled { .. } => {
                    // Suppress info/listing output in silent headless mode.
                    // Future: -v flag could enable it.
                }

                ExEffect::Substituted { .. } => {
                    // Suppress count output; errors already handled above.
                }

                ExEffect::Error(msg) => {
                    eprintln!("hjkl: {display_name}: {msg}");
                    exit_code = 1;
                }

                ExEffect::Unknown(name) => {
                    eprintln!("hjkl: {display_name}: unknown ex command: {name}");
                    exit_code = 1;
                }

                ExEffect::Save => {
                    if let Err(e) = write_buffer(&editor, &current_filename, &display_name) {
                        eprintln!("{e}");
                        exit_code = 1;
                    } else {
                        wrote = true;
                    }
                }

                ExEffect::SaveAs(path_str) => {
                    let new_path = PathBuf::from(&path_str);
                    if let Err(e) = write_buffer(&editor, &Some(new_path.clone()), &display_name) {
                        eprintln!("{e}");
                        exit_code = 1;
                    } else {
                        current_filename = Some(new_path);
                        wrote = true;
                    }
                }

                ExEffect::Quit { save, force: _ } => {
                    if save {
                        if let Err(e) = write_buffer(&editor, &current_filename, &display_name) {
                            eprintln!("{e}");
                            exit_code = 1;
                        } else {
                            wrote = true;
                        }
                    }
                    // Stop dispatching further commands for this file.
                    break;
                }

                ExEffect::EditFile { path, .. } => {
                    // In headless mode, treat :e as switching the current file target.
                    match std::fs::read_to_string(&path) {
                        Ok(content) => {
                            let content = content.strip_suffix('\n').unwrap_or(&content);
                            hjkl_engine::BufferEdit::replace_all(editor.buffer_mut(), content);
                            current_filename = Some(PathBuf::from(&path));
                        }
                        Err(e) => {
                            eprintln!("hjkl: {path}: {e}");
                            exit_code = 1;
                        }
                    }
                }

                ExEffect::BufferDelete { .. } => {
                    // No multi-buffer in headless mode — stop processing.
                    break;
                }

                ExEffect::PutRegister { .. } => {
                    // No multi-buffer paste support in headless mode — no-op.
                }

                ExEffect::SaveAndRename { path } => {
                    let new_path = PathBuf::from(&path);
                    if let Err(e) = write_buffer(&editor, &Some(new_path.clone()), &display_name) {
                        eprintln!("{e}");
                        exit_code = 1;
                    } else {
                        current_filename = Some(new_path);
                        wrote = true;
                    }
                }

                ExEffect::RenameBuffer { .. } => {
                    // In-memory rename — no write; no-op in headless mode.
                }

                ExEffect::Cwd(_) => {
                    // Directory already changed by the handler — no-op.
                }

                ExEffect::Redraw { .. } => {
                    // No terminal to clear in headless mode — no-op.
                }

                ExEffect::Preserve => {
                    // No swap files in headless mode — no-op.
                }

                ExEffect::Recover(_) => {
                    // No swap recovery in headless mode — no-op.
                }

                ExEffect::SubstituteConfirm { matches } => {
                    // Headless mode has no interactive prompt; apply all matches.
                    let count = matches.len();
                    if count > 0 {
                        let accepted = vec![true; count];
                        hjkl_engine::apply_collected_matches(&mut editor, &matches, &accepted);
                    }
                }
            }
        }

        let _ = wrote; // No auto-write; documented above.
    }

    Ok(exit_code)
}

/// Serialise the buffer and write it to `path`. Returns a formatted error
/// string on failure so the caller can print it and set `exit_code = 1`.
fn write_buffer(
    editor: &Editor<View, DefaultHost>,
    path: &Option<PathBuf>,
    display_name: &str,
) -> Result<(), String> {
    match path {
        None => Err(format!("hjkl: {display_name}: E32: No file name")),
        Some(p) => {
            let joined = editor.buffer().content_joined();
            let mut content = String::with_capacity(joined.len() + 1);
            content.push_str(&joined);
            if !joined.is_empty() {
                content.push('\n');
            }
            std::fs::write(p, &content).map_err(|e| format!("hjkl: {}: {e}", p.display()))
        }
    }
}

/// The six session-shared banks (registers, global marks, last `:s`
/// command, abbrevs, search pattern, changelist) that every buffer-slot
/// editor shares in the TUI (`App::new` / `App::nvim_create_buffer`), so
/// e.g. a yank in one window is pasteable in another. `--headless` gave
/// each file's editor its own private banks instead, so state set while
/// processing one file (a register, a global mark, `&hlsearch` pattern, …)
/// was lost by the time the next file's editor was built (audit R2, fix 4).
///
/// One `SharedBanks` is created per [`run`] call and applied to every
/// file's editor via [`SharedBanks::apply`].
struct SharedBanks {
    registers: std::sync::Arc<std::sync::Mutex<hjkl_engine::Registers>>,
    global_marks: std::sync::Arc<std::sync::Mutex<hjkl_engine::GlobalMarks>>,
    last_substitute: std::sync::Arc<std::sync::Mutex<Option<hjkl_engine::SubstituteCmd>>>,
    abbrevs: std::sync::Arc<std::sync::Mutex<Vec<hjkl_engine::Abbrev>>>,
    search: std::sync::Arc<std::sync::Mutex<hjkl_engine::SearchBank>>,
    // UNLIKE the TUI's per-`buffer_id`-keyed map (`App::change_banks`),
    // headless files are processed strictly one at a time — there is no
    // "switch back to a still-open earlier buffer" to preserve a distinct
    // changelist for — so a single shared bank for the whole session
    // suffices here.
    change_bank: std::sync::Arc<std::sync::Mutex<hjkl_engine::ChangeBank>>,
}

impl SharedBanks {
    fn new() -> Self {
        Self {
            registers: std::sync::Arc::new(
                std::sync::Mutex::new(hjkl_engine::Registers::default()),
            ),
            global_marks: std::sync::Arc::new(std::sync::Mutex::new(
                hjkl_engine::GlobalMarks::new(),
            )),
            last_substitute: std::sync::Arc::new(std::sync::Mutex::new(None)),
            abbrevs: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
            search: std::sync::Arc::new(std::sync::Mutex::new(hjkl_engine::SearchBank::default())),
            change_bank: std::sync::Arc::new(std::sync::Mutex::new(
                hjkl_engine::ChangeBank::default(),
            )),
        }
    }

    /// Point `editor` at this session's shared banks — the same six-setter
    /// pattern `App::new`/`App::nvim_create_buffer` use to wire a freshly
    /// created editor into the app-wide banks.
    fn apply(&self, editor: &mut Editor<View, DefaultHost>) {
        editor.set_registers_arc(self.registers.clone());
        editor.set_global_marks_arc(self.global_marks.clone());
        editor.set_last_substitute_arc(self.last_substitute.clone());
        editor.set_abbrevs_arc(self.abbrevs.clone());
        editor.set_search_arc(self.search.clone());
        editor.set_change_bank_arc(self.change_bank.clone());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Regression (audit R2, fix 4): before the fix, each file in a
    /// `--headless a b …` run got a fresh `vim_editor` with NO shared
    /// banks, so a register populated while processing one file was gone
    /// by the time the next file's (independent) editor was built. Drives
    /// real keystrokes (`yy` / `p`) through two SEPARATE editors — mirroring
    /// how `run` builds one editor per file — wired to the same
    /// `SharedBanks`, exactly as `run` wires every file's editor.
    ///
    /// (There is no ex-command-level way to exercise this: `:normal` is not
    /// implemented and no other ex command currently writes a register, so
    /// this has to drive the editor directly rather than through
    /// `hjkl_ex::try_dispatch` — see the crate-level docs on `run`'s command
    /// dispatch.)
    #[test]
    fn shared_banks_carry_a_yank_across_separate_editors() {
        let banks = SharedBanks::new();

        // "File A": yank its only line into the unnamed register.
        let mut view_a = View::new();
        BufferEdit::replace_all(&mut view_a, "hello from file a");
        let mut editor_a = hjkl_vim::vim_editor(view_a, DefaultHost::new(), Options::default());
        banks.apply(&mut editor_a);
        for input in hjkl_engine::decode_macro("yy") {
            hjkl_vim::dispatch_input(&mut editor_a, input);
        }

        // "File B": a wholly separate editor/buffer (as `run` builds fresh
        // per file), wired to the SAME `SharedBanks`. Paste with no yank of
        // its own first.
        let mut view_b = View::new();
        BufferEdit::replace_all(&mut view_b, "line already in file b");
        let mut editor_b = hjkl_vim::vim_editor(view_b, DefaultHost::new(), Options::default());
        banks.apply(&mut editor_b);
        for input in hjkl_engine::decode_macro("p") {
            hjkl_vim::dispatch_input(&mut editor_b, input);
        }

        let content_b = editor_b.buffer().content_joined();
        assert!(
            content_b.contains("hello from file a"),
            "pasting in editor B must see the yank from editor A when both \
             are wired to the same SharedBanks (mirrors --headless processing \
             file A, then file B); got: {content_b:?}"
        );
    }

    /// Sanity check for the regression test above: WITHOUT `SharedBanks`
    /// (i.e. each editor's own default private registers), the same paste
    /// must NOT see the other editor's yank — proving the assertion above
    /// actually exercises bank-sharing and isn't trivially true.
    #[test]
    fn unshared_editors_do_not_carry_a_yank_across() {
        let mut view_a = View::new();
        BufferEdit::replace_all(&mut view_a, "hello from file a");
        let mut editor_a = hjkl_vim::vim_editor(view_a, DefaultHost::new(), Options::default());
        for input in hjkl_engine::decode_macro("yy") {
            hjkl_vim::dispatch_input(&mut editor_a, input);
        }

        let mut view_b = View::new();
        BufferEdit::replace_all(&mut view_b, "line already in file b");
        let mut editor_b = hjkl_vim::vim_editor(view_b, DefaultHost::new(), Options::default());
        for input in hjkl_engine::decode_macro("p") {
            hjkl_vim::dispatch_input(&mut editor_b, input);
        }

        let content_b = editor_b.buffer().content_joined();
        assert!(
            !content_b.contains("hello from file a"),
            "editors with independent (non-shared) registers must not see \
             each other's yanks; got: {content_b:?}"
        );
    }
}