leotui 0.2.1

A terminal front end for leolib.
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
//! vim's `:s`, over the lines of one body.
//!
//! `[range]s/pattern/replacement/[flags]`. The pattern is a `regex` crate
//! regex, not vim's dialect. The replacement takes vim's `&`, `\0`-`\9` and
//! `\r`. Flags: `g`, `i`, `I`, `n`.

use regex::{Captures, Regex, RegexBuilder};

/// A line address.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Addr {
    /// `.`, the cursor's line.
    Current,
    /// `$`.
    Last,
    /// A line number, counting from 1.
    Line(usize),
}

#[derive(Debug, PartialEq, Eq)]
pub struct Substitute {
    /// First and last line. None is the cursor's line.
    pub range: Option<(Addr, Addr)>,
    /// Empty means the last search.
    pub pattern: String,
    pub replacement: String,
    pub global: bool,
    /// None is smartcase, as `/` uses. `i` sets true, `I` false.
    pub ignore_case: Option<bool>,
    /// `n`: count the matches, change nothing.
    pub count_only: bool,
}

/// What a substitute did.
#[derive(Debug, PartialEq, Eq)]
pub struct Outcome {
    pub count: usize,
    pub lines: usize,
    /// The last line changed, for the cursor.
    pub last_line: usize,
}

/// Parse a `:` line, without its `:`. None if it is not a substitute.
pub fn parse(line: &str) -> Option<Result<Substitute, String>> {
    let (range, rest) = parse_range(line);
    let rest = rest
        .strip_prefix("substitute")
        .or_else(|| rest.strip_prefix('s'))?;
    let delim = rest.chars().next()?;
    if delim.is_alphanumeric() || delim.is_whitespace() || matches!(delim, '\\' | '"' | '|') {
        return None;
    }
    let (pattern, after) = take_field(&rest[delim.len_utf8()..], delim);
    let (replacement, flags) = match after {
        Some(s) => {
            let (r, f) = take_field(s, delim);
            (r, f.unwrap_or(""))
        }
        None => (String::new(), ""),
    };
    let mut sub = Substitute {
        range,
        pattern,
        replacement,
        global: false,
        ignore_case: None,
        count_only: false,
    };
    for c in flags.trim_end().chars() {
        match c {
            'g' => sub.global = true,
            'i' => sub.ignore_case = Some(true),
            'I' => sub.ignore_case = Some(false),
            'n' => sub.count_only = true,
            'c' => return Some(Err("substitute: the c flag is not supported".into())),
            other => return Some(Err(format!("substitute: unknown flag {other}"))),
        }
    }
    Some(Ok(sub))
}

/// `%`, `N`, `N,M`, with `.` and `$` for N or M.
fn parse_range(s: &str) -> (Option<(Addr, Addr)>, &str) {
    if let Some(rest) = s.strip_prefix('%') {
        return (Some((Addr::Line(1), Addr::Last)), rest);
    }
    let Some((first, rest)) = parse_addr(s) else {
        return (None, s);
    };
    match rest.strip_prefix(',') {
        Some(after) => match parse_addr(after) {
            Some((second, rest)) => (Some((first, second)), rest),
            None => (Some((first, Addr::Current)), after),
        },
        None => (Some((first, first)), rest),
    }
}

fn parse_addr(s: &str) -> Option<(Addr, &str)> {
    if let Some(rest) = s.strip_prefix('.') {
        return Some((Addr::Current, rest));
    }
    if let Some(rest) = s.strip_prefix('$') {
        return Some((Addr::Last, rest));
    }
    let digits = s.len() - s.trim_start_matches(|c: char| c.is_ascii_digit()).len();
    let n = s[..digits].parse().ok()?;
    Some((Addr::Line(n), &s[digits..]))
}

/// Text up to an unescaped `delim`, and what follows it. `\delim` is `delim`.
fn take_field(s: &str, delim: char) -> (String, Option<&str>) {
    let mut out = String::new();
    let mut chars = s.char_indices();
    while let Some((i, c)) = chars.next() {
        if c == delim {
            return (out, Some(&s[i + c.len_utf8()..]));
        }
        if c == '\\' {
            match chars.next() {
                Some((_, d)) if d == delim => out.push(d),
                Some((_, d)) => {
                    out.push('\\');
                    out.push(d);
                }
                None => out.push('\\'),
            }
        } else {
            out.push(c);
        }
    }
    (out, None)
}

/// Apply `sub` to `lines`, with the cursor on line `cursor`, counting from 0.
///
/// `last_search` is the `/` pattern, used when the pattern is empty. A
/// replacement containing `\r` splits its line.
pub fn apply(
    sub: &Substitute,
    lines: &mut Vec<String>,
    cursor: usize,
    last_search: Option<&str>,
) -> Result<Outcome, String> {
    let re = compile(sub, last_search)?;
    apply_with(sub, &re, lines, cursor)
}

/// `apply`, with the pattern compiled once for many bodies.
pub fn apply_with(
    sub: &Substitute,
    re: &Regex,
    lines: &mut Vec<String>,
    cursor: usize,
) -> Result<Outcome, String> {
    let (a, b) = match sub.range {
        None => (cursor, cursor),
        Some((x, y)) => (
            resolve(x, cursor, lines.len())?,
            resolve(y, cursor, lines.len())?,
        ),
    };
    let (first, last) = (a.min(b), a.max(b));
    let template = parse_template(&sub.replacement);
    let (mut count, mut changed, mut last_line) = (0, 0, first);
    let mut out: Vec<String> = Vec::with_capacity(lines.len());
    for (i, line) in lines.iter().enumerate() {
        let n = match (first..=last).contains(&i) {
            true if sub.global => re.find_iter(line).count(),
            true => usize::from(re.is_match(line)),
            false => 0,
        };
        if n == 0 || sub.count_only {
            if n > 0 {
                last_line = out.len();
            }
            out.push(line.clone());
        } else {
            let limit = if sub.global { 0 } else { 1 };
            let new = re.replacen(line, limit, |caps: &Captures| expand(&template, caps));
            out.extend(new.split('\n').map(str::to_string));
            last_line = out.len() - 1;
        }
        count += n;
        changed += usize::from(n > 0);
    }
    if count == 0 {
        return Err(format!("pattern not found: {}", re.as_str()));
    }
    if !sub.count_only {
        *lines = out;
    }
    Ok(Outcome {
        count,
        lines: changed,
        last_line,
    })
}

/// The pattern as a regex: the last search if it is empty, with smartcase
/// unless a flag decides.
pub fn compile(sub: &Substitute, last_search: Option<&str>) -> Result<Regex, String> {
    let pattern = match sub.pattern.is_empty() {
        true => last_search
            .ok_or("substitute: no previous search")?
            .to_string(),
        false => sub.pattern.clone(),
    };
    let ignore = sub
        .ignore_case
        .unwrap_or_else(|| !crate::search::has_capital(&pattern));
    RegexBuilder::new(&pattern)
        .case_insensitive(ignore)
        .build()
        .map_err(|e| {
            let detail = e.to_string();
            format!(
                "substitute: {}",
                detail.lines().last().unwrap_or("bad pattern")
            )
        })
}

fn resolve(addr: Addr, cursor: usize, len: usize) -> Result<usize, String> {
    match addr {
        Addr::Current => Ok(cursor),
        Addr::Last => Ok(len.saturating_sub(1)),
        Addr::Line(n) if (1..=len).contains(&n) => Ok(n - 1),
        Addr::Line(n) => Err(format!("substitute: no line {n}; the body has {len}")),
    }
}

enum Piece {
    Text(String),
    Group(usize),
}

/// vim's replacement: `&` and `\0` are the match, `\1`-`\9` its groups, `\r`
/// and `\n` a new line, `\t` a tab, and a backslash before anything else
/// makes it literal.
fn parse_template(s: &str) -> Vec<Piece> {
    let mut out = Vec::new();
    let mut text = String::new();
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        let group = match c {
            '&' => Some(0),
            '\\' => match chars.next() {
                Some(d) if d.is_ascii_digit() => Some(d as usize - '0' as usize),
                Some('r' | 'n') => {
                    text.push('\n');
                    None
                }
                Some('t') => {
                    text.push('\t');
                    None
                }
                Some(other) => {
                    text.push(other);
                    None
                }
                None => {
                    text.push('\\');
                    None
                }
            },
            c => {
                text.push(c);
                None
            }
        };
        if let Some(n) = group {
            if !text.is_empty() {
                out.push(Piece::Text(std::mem::take(&mut text)));
            }
            out.push(Piece::Group(n));
        }
    }
    if !text.is_empty() {
        out.push(Piece::Text(text));
    }
    out
}

fn expand(template: &[Piece], caps: &Captures) -> String {
    let mut out = String::new();
    for piece in template {
        match piece {
            Piece::Text(t) => out.push_str(t),
            Piece::Group(n) => out.push_str(caps.get(*n).map_or("", |m| m.as_str())),
        }
    }
    out
}

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

    fn sub(line: &str) -> Substitute {
        parse(line)
            .expect("not a substitute")
            .expect("did not parse")
    }

    fn run(line: &str, text: &[&str], cursor: usize) -> Result<(Vec<String>, Outcome), String> {
        let mut lines: Vec<String> = text.iter().map(|s| s.to_string()).collect();
        let outcome = apply(&sub(line), &mut lines, cursor, Some("a.b"))?;
        Ok((lines, outcome))
    }

    #[test]
    fn the_parts_are_read_as_vim_reads_them() {
        let s = sub("s/a/b/");
        assert_eq!(
            (s.range, s.pattern.as_str(), s.replacement.as_str()),
            (None, "a", "b")
        );
        assert!(!s.global);
        let s = sub("%s/a/b/gi");
        assert_eq!(s.range, Some((Addr::Line(1), Addr::Last)));
        assert!(s.global && s.ignore_case == Some(true));
        let s = sub("2,$substitute#x/y#z#n");
        assert_eq!(s.range, Some((Addr::Line(2), Addr::Last)));
        assert_eq!(s.pattern, "x/y");
        assert!(s.count_only);
        assert_eq!(sub(r"s/a\/b/c").pattern, "a/b");
        assert_eq!(sub("s/a").replacement, "");
    }

    #[test]
    fn other_commands_are_not_substitutes() {
        for line in [
            "set wrap",
            "save",
            "search-forward",
            "12",
            "e path",
            "s",
            "theme x",
        ] {
            assert!(parse(line).is_none(), "{line}");
        }
    }

    #[test]
    fn an_unsupported_flag_is_an_error() {
        assert!(parse("s/a/b/c").unwrap().is_err());
        assert!(parse("s/a/b/x").unwrap().is_err());
    }

    #[test]
    fn without_a_range_only_the_cursor_line_changes() {
        let (lines, o) = run("s/x/y/", &["x x", "x x"], 1).unwrap();
        assert_eq!(lines, ["x x", "y x"]);
        assert_eq!((o.count, o.lines, o.last_line), (1, 1, 1));
    }

    #[test]
    fn a_range_and_g_change_every_match_in_those_lines() {
        let (lines, o) = run("%s/x/y/g", &["x x", "z", "x"], 0).unwrap();
        assert_eq!(lines, ["y y", "z", "y"]);
        assert_eq!((o.count, o.lines, o.last_line), (3, 2, 2));
        let (lines, _) = run("2,3s/x/y/", &["x", "x", "x"], 0).unwrap();
        assert_eq!(lines, ["x", "y", "y"]);
    }

    #[test]
    fn the_replacement_takes_groups_the_match_and_new_lines() {
        let (lines, _) = run(r"s/(\w+) (\w+)/\2 \1 [&]/", &["hello world"], 0).unwrap();
        assert_eq!(lines, ["world hello [hello world]"]);
        let (lines, o) = run(r"s/, /,\r/g", &["a, b, c", "d"], 0).unwrap();
        assert_eq!(lines, ["a,", "b,", "c", "d"]);
        assert_eq!(o.last_line, 2);
        let (lines, _) = run(r"s/a/\&\$1/", &["a"], 0).unwrap();
        assert_eq!(lines, ["&$1"]);
    }

    #[test]
    fn case_follows_smartcase_unless_a_flag_says_otherwise() {
        assert!(run("s/foo/x/", &["Foo"], 0).is_ok());
        assert!(run("s/Foo/x/", &["foo"], 0).is_err());
        assert!(run("s/foo/x/I", &["Foo"], 0).is_err());
        assert!(run("s/Foo/x/i", &["foo"], 0).is_ok());
        // An escape such as \S is not a capital.
        assert!(run(r"s/\Sx/y/", &["AX"], 0).is_ok());
    }

    #[test]
    fn an_empty_pattern_is_the_last_search() {
        // The last search is a regex, as `/` reads it: `a.b` matches `axb`.
        let (lines, _) = run("s//X/", &["axb a.b"], 0).unwrap();
        assert_eq!(lines, ["X a.b"]);
    }

    #[test]
    fn counting_changes_nothing() {
        let (lines, o) = run("%s/x//gn", &["x x", "x"], 0).unwrap();
        assert_eq!(lines, ["x x", "x"]);
        assert_eq!((o.count, o.lines), (3, 2));
    }

    #[test]
    fn a_missing_match_or_line_or_bad_pattern_is_an_error() {
        assert!(run("s/q/x/", &["x"], 0)
            .unwrap_err()
            .contains("pattern not found"));
        assert!(run("5s/x/y/", &["x"], 0).unwrap_err().contains("no line 5"));
        assert!(run("s/(/y/", &["x"], 0)
            .unwrap_err()
            .starts_with("substitute:"));
    }
}