shoin 0.1.9

A no-clutter terminal Markdown editor with modal editing
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Motions. SPEC.md §7.3.
//!
//! A motion resolves to a target `Cursor` plus whether it is inclusive,
//! exclusive, or linewise — which is what operators need to compute a range.

use super::buffer::Buffer;
use super::cursor::Cursor;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Motion {
    Left,
    Right,
    Up,
    Down,

    WordForward { big: bool },
    WordBack { big: bool },
    WordEnd { big: bool },

    LineStart,
    LineFirstNonBlank,
    LineEnd,

    BufferStart,
    BufferEnd,
    GotoLine(usize),

    ParagraphForward,
    ParagraphBack,

    HalfPageDown,
    HalfPageUp,
    PageDown,
    PageUp,

    ScreenTop,
    ScreenMiddle,
    ScreenBottom,

    /// `f` `F` `t` `T`
    FindChar {
        target: char,
        forward: bool,
        till: bool,
    },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MotionKind {
    /// Range excludes the target character (`w`, `0`).
    Exclusive,
    /// Range includes the target character (`e`, `f`).
    Inclusive,
    /// Whole lines (`j`, `G`, `{`).
    Linewise,
}

pub struct MotionResult {
    pub target: Cursor,
    pub kind: MotionKind,
}

pub fn char_class(c: char) -> u8 {
    if c.is_whitespace() {
        0
    } else if c.is_alphanumeric() || c == '_' {
        1
    } else {
        2
    }
}

/// Resolve a motion from the buffer's current cursor, applying `count`.
///
/// Returns `None` if the motion cannot move (e.g. `k` on line 0), which
/// operators treat as a no-op rather than an error. `page` is the viewport
/// height, needed by the scrolling motions.
///
/// `operator` is set when the motion is an operator's range (`dw`, `d}`)
/// rather than a cursor move. A few motions then reach where the cursor can
/// never rest: `l` and `}` the line end past the last character, and `w`'s
/// last step stops at the end of its line instead of crossing it.
pub fn resolve(
    buffer: &Buffer,
    motion: Motion,
    count: usize,
    page: usize,
    operator: bool,
) -> Option<MotionResult> {
    let count = count.max(1);
    let cur = buffer.cursor;
    let last_line = buffer.last_line();

    use Motion::*;
    use MotionKind::*;

    let (target, kind) = match motion {
        Left => {
            if cur.col == 0 {
                return None;
            }
            (Cursor::new(cur.line, cur.col.saturating_sub(count)), Exclusive)
        }
        Right => {
            // `dl` on the last character takes it: the range runs to the line
            // end, a column the cursor itself never rests on.
            let len = buffer.line_len(cur.line);
            let max = if operator { len } else { len.saturating_sub(1) };
            if len == 0 || cur.col >= max {
                return None;
            }
            (Cursor::new(cur.line, (cur.col + count).min(max)), Exclusive)
        }
        Up => {
            if cur.line == 0 {
                return None;
            }
            let line = cur.line.saturating_sub(count);
            let mut c = cur;
            c.line = line;
            c.col = cur.goal_col.min(buffer.line_len(line).saturating_sub(1));
            (c, Linewise)
        }
        Down => {
            if cur.line >= last_line {
                return None;
            }
            let line = (cur.line + count).min(last_line);
            let mut c = cur;
            c.line = line;
            c.col = cur.goal_col.min(buffer.line_len(line).saturating_sub(1));
            (c, Linewise)
        }

        LineStart => (Cursor::new(cur.line, 0), Exclusive),
        LineFirstNonBlank => {
            let text = buffer.line_text(cur.line);
            let col = text.chars().take_while(|c| c.is_whitespace()).count();
            (Cursor::new(cur.line, col), Exclusive)
        }
        LineEnd => {
            // `3$` is the end of the line two below, as in vim. The count
            // reached here and was dropped, so every `N$` acted as `$`.
            let line = (cur.line + count - 1).min(last_line);
            let len = buffer.line_len(line);
            (Cursor::new(line, len.saturating_sub(1)), Inclusive)
        }

        BufferStart => (Cursor::new(0, 0), Linewise),
        BufferEnd => (Cursor::new(last_line, 0), Linewise),
        GotoLine(n) => (Cursor::new(n.min(last_line), 0), Linewise),

        WordForward { big } => {
            let mut line = cur.line;
            let mut col = cur.col;
            for i in 0..count {
                let last_step = operator && i + 1 == count;
                (line, col) = word_forward(buffer, line, col, big, last_line, last_step);
            }
            (Cursor::new(line, col), Exclusive)
        }
        WordBack { big } => {
            let mut line = cur.line;
            let mut col = cur.col;
            for _ in 0..count {
                let (l, c) = word_back(buffer, line, col, big);
                line = l;
                col = c;
            }
            (Cursor::new(line, col), Exclusive)
        }

        WordEnd { big } => {
            let mut line = cur.line;
            let mut col = cur.col;
            for _ in 0..count {
                let (l, c) = word_end(buffer, line, col, big, last_line);
                line = l;
                col = c;
            }
            // Inclusive: `de` deletes through the last char of the word.
            (Cursor::new(line, col), Inclusive)
        }

        // `}` and `{` are exclusive, as in vim: `d}` stops before the blank
        // line it lands on. With no blank line left, `}` runs to the end of
        // the last line.
        ParagraphForward => {
            let mut line = cur.line;
            for _ in 0..count {
                line = (line + 1).min(last_line);
                while line < last_line && !buffer.line_text(line).trim().is_empty() {
                    line += 1;
                }
            }
            let col = if buffer.line_text(line).trim().is_empty() {
                0
            } else if operator {
                buffer.line_len(line)
            } else {
                buffer.line_len(line).saturating_sub(1)
            };
            (Cursor::new(line, col), Exclusive)
        }
        ParagraphBack => {
            let mut line = cur.line;
            for _ in 0..count {
                line = line.saturating_sub(1);
                while line > 0 && !buffer.line_text(line).trim().is_empty() {
                    line -= 1;
                }
            }
            (Cursor::new(line, 0), Exclusive)
        }

        HalfPageDown => {
            let line = (cur.line + page / 2).min(last_line);
            (Cursor::new(line, cur.goal_col), Linewise)
        }
        HalfPageUp => {
            let line = cur.line.saturating_sub(page / 2);
            (Cursor::new(line, cur.goal_col), Linewise)
        }
        PageDown => {
            let line = (cur.line + page).min(last_line);
            (Cursor::new(line, cur.goal_col), Linewise)
        }
        PageUp => {
            let line = cur.line.saturating_sub(page);
            (Cursor::new(line, cur.goal_col), Linewise)
        }

        FindChar {
            target,
            forward,
            till,
        } => {
            let text: Vec<char> = buffer.line_text(cur.line).chars().collect();
            // `3fx` is the THIRD x, not the first: a count multiplies a find
            // the way it multiplies a word motion. `Pending` collects it and
            // `repeat_find` passes it on, and it was dropped here.
            let nth = count - 1;
            let found = if forward {
                (cur.col + 1..text.len())
                    .filter(|&i| text[i] == target)
                    .nth(nth)
            } else {
                (0..cur.col)
                    .rev()
                    .filter(|&i| text[i] == target)
                    .nth(nth)
            }?;
            let col = if till {
                if forward {
                    found.saturating_sub(1)
                } else {
                    found + 1
                }
            } else {
                found
            };
            (Cursor::new(cur.line, col), Inclusive)
        }

        // Deliberately unanswerable here: `H`/`M`/`L` are about the VIEWPORT,
        // which this module cannot see. `App::resolve_screen_motion` turns them
        // into a `GotoLine` before a motion is ever asked for one.
        ScreenTop | ScreenMiddle | ScreenBottom => return None,
    };

    let mut target = target;
    // Vertical motions keep the goal column; everything else resets it.
    match motion {
        Up | Down | HalfPageDown | HalfPageUp | PageDown | PageUp => {
            target.goal_col = cur.goal_col;
            target.col = target.col.min(buffer.line_len(target.line).saturating_sub(1));
        }
        _ => target.goal_col = target.col,
    }

    // Position only: `Cursor`'s equality also compares the hidden goal column.
    // A linewise operator range that stays put still covers its line, so
    // `dgg` on the first line deletes it.
    if (target.line, target.col) == (cur.line, cur.col) && !(operator && kind == Linewise) {
        return None;
    }
    Some(MotionResult { target, kind })
}

/// `cw` on a word: `e`, except that a cursor already on a word's last
/// character counts as there — vim's `end_word` with `stop` set — so `cw` on
/// the `a` of `a b` changes only `a`. Inclusive, and never `None`: at worst
/// the range is the character under the cursor.
pub fn change_word_end(buffer: &Buffer, count: usize, big: bool) -> Cursor {
    let cur = buffer.cursor;
    let last_line = buffer.last_line();
    let chars: Vec<char> = buffer.line_text(cur.line).chars().collect();
    let at_end = match (chars.get(cur.col), chars.get(cur.col + 1)) {
        (Some(&c), Some(&next)) => class_of(next, big) != class_of(c, big),
        _ => true,
    };
    let (mut line, mut col) = (cur.line, cur.col);
    for _ in usize::from(at_end)..count.max(1) {
        (line, col) = word_end(buffer, line, col, big, last_line);
    }
    Cursor::new(line, col)
}

/// One `w` step. With `stop_at_eol` — the last step of an operator's `w` —
/// it never crosses a line break: it stops on the line end (`col == len`)
/// instead, so `dw` on a line's last word keeps the newline. From an empty
/// line it still steps to the start of the next, as vim's `fwd_word` does.
fn word_forward(
    buffer: &Buffer,
    line: usize,
    mut col: usize,
    big: bool,
    last_line: usize,
    stop_at_eol: bool,
) -> (usize, usize) {
    let chars: Vec<char> = buffer.line_text(line).chars().collect();
    if col < chars.len() {
        // Step off the current run, then over the blanks after it.
        let start = class_of(chars[col], big);
        while col < chars.len() && class_of(chars[col], big) == start {
            col += 1;
        }
        while col < chars.len() && chars[col].is_whitespace() {
            col += 1;
        }
        if col < chars.len() {
            return (line, col);
        }
        if stop_at_eol {
            return (line, chars.len());
        }
        if line == last_line {
            return (line, chars.len().saturating_sub(1));
        }
    } else if line == last_line {
        return (line, col);
    } else if stop_at_eol {
        return (line + 1, 0);
    }
    // On to the first word below. An empty line counts as a word; a line of
    // nothing but blanks does not.
    let mut line = line;
    loop {
        line += 1;
        let next: Vec<char> = buffer.line_text(line).chars().collect();
        if next.is_empty() {
            return (line, 0);
        }
        if let Some(first) = next.iter().position(|c| !c.is_whitespace()) {
            return (line, first);
        }
        if line == last_line {
            return (line, next.len() - 1);
        }
    }
}

fn class_of(c: char, big: bool) -> u8 {
    if big {
        if c.is_whitespace() {
            0
        } else {
            1
        }
    } else {
        char_class(c)
    }
}

/// `e` / `E`: land on the last character of the current or next word.
fn word_end(
    buffer: &Buffer,
    mut line: usize,
    mut col: usize,
    big: bool,
    last_line: usize,
) -> (usize, usize) {
    let mut chars: Vec<char> = buffer.line_text(line).chars().collect();

    // Step one char forward, crossing a line boundary if needed.
    if col + 1 < chars.len() {
        col += 1;
    } else if line < last_line {
        line += 1;
        col = 0;
        chars = buffer.line_text(line).chars().collect();
    } else {
        return (line, chars.len().saturating_sub(1));
    }

    // Skip whitespace (and blank lines) to the start of the next word.
    loop {
        while col < chars.len() && chars[col].is_whitespace() {
            col += 1;
        }
        if col < chars.len() {
            break;
        }
        if line < last_line {
            line += 1;
            col = 0;
            chars = buffer.line_text(line).chars().collect();
        } else {
            return (line, chars.len().saturating_sub(1));
        }
    }

    // Run to the last char of this word.
    let cls = class_of(chars[col], big);
    while col + 1 < chars.len() && class_of(chars[col + 1], big) == cls {
        col += 1;
    }
    (line, col)
}

fn word_back(buffer: &Buffer, mut line: usize, mut col: usize, big: bool) -> (usize, usize) {
    if col == 0 {
        if line == 0 {
            return (0, 0);
        }
        line -= 1;
        col = buffer.line_len(line);
    }
    let chars: Vec<char> = buffer.line_text(line).chars().collect();
    if chars.is_empty() {
        return (line, 0);
    }
    col = col.min(chars.len());

    // Skip whitespace to the left.
    while col > 0 && chars[col - 1].is_whitespace() {
        col -= 1;
    }
    if col == 0 {
        return (line, 0);
    }
    let cls = if big {
        1
    } else {
        char_class(chars[col - 1])
    };
    while col > 0 {
        let c = chars[col - 1];
        let this = if big {
            if c.is_whitespace() {
                0
            } else {
                1
            }
        } else {
            char_class(c)
        };
        if this != cls {
            break;
        }
        col -= 1;
    }
    (line, col)
}

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

    fn buf(text: &str, line: usize, col: usize) -> Buffer {
        let mut b = Buffer::empty();
        b.rope = Rope::from_str(text);
        b.cursor = Cursor::new(line, col);
        b
    }

    /// Resolve and report `(line, col)`, or `None` when the motion cannot move.
    fn go(b: &Buffer, m: Motion, count: usize) -> Option<(usize, usize)> {
        resolve(b, m, count, 10, false).map(|r| (r.target.line, r.target.col))
    }

    fn kind(b: &Buffer, m: Motion) -> MotionKind {
        resolve(b, m, 1, 10, false).expect("a motion").kind
    }

    #[test]
    fn word_motions_cross_lines_and_classes() {
        let b = buf("foo bar.baz\nnext line\n", 0, 0);
        assert_eq!(go(&b, Motion::WordForward { big: false }, 1), Some((0, 4)));
        // `.` is its own class, so `w` stops on it; `W` runs past it.
        assert_eq!(go(&b, Motion::WordForward { big: false }, 2), Some((0, 7)));
        // `W` ignores the `.`, so `bar.baz` is one big word.
        assert_eq!(go(&b, Motion::WordForward { big: true }, 1), Some((0, 4)));
        assert_eq!(go(&b, Motion::WordForward { big: true }, 2), Some((1, 0)));

        let b = buf("foo bar\nnext\n", 0, 5);
        assert_eq!(go(&b, Motion::WordBack { big: false }, 1), Some((0, 4)));
        assert_eq!(go(&b, Motion::WordBack { big: false }, 2), Some((0, 0)));

        // `e` lands ON the last character, which is why it is inclusive.
        let b = buf("foo bar\n", 0, 0);
        assert_eq!(go(&b, Motion::WordEnd { big: false }, 1), Some((0, 2)));
        assert_eq!(kind(&b, Motion::WordEnd { big: false }), MotionKind::Inclusive);
    }

    #[test]
    fn line_motions_land_where_vim_puts_them() {
        let b = buf("    indented text\n", 0, 10);
        assert_eq!(go(&b, Motion::LineStart, 1), Some((0, 0)));
        assert_eq!(go(&b, Motion::LineFirstNonBlank, 1), Some((0, 4)));
        // `$` is the last character, not one past it.
        assert_eq!(go(&b, Motion::LineEnd, 1), Some((0, 16)));
        assert_eq!(kind(&b, Motion::LineEnd), MotionKind::Inclusive);

        // An empty line has nowhere to go.
        let b = buf("\nx\n", 0, 0);
        assert_eq!(go(&b, Motion::LineEnd, 1), None);
    }

    /// A count multiplies a find: `3fx` is the THIRD x on the line.
    #[test]
    fn a_count_multiplies_a_character_find() {
        let b = buf("axbxcxd\n", 0, 0);
        let f = |n| {
            go(
                &b,
                Motion::FindChar { target: 'x', forward: true, till: false },
                n,
            )
        };
        assert_eq!(f(1), Some((0, 1)));
        assert_eq!(f(2), Some((0, 3)));
        assert_eq!(f(3), Some((0, 5)));
        assert_eq!(f(4), None, "there is no fourth x");

        // Backwards, and `t` stopping one short in each direction.
        let b = buf("axbxcxd\n", 0, 6);
        let back = |n, till| {
            go(
                &b,
                Motion::FindChar { target: 'x', forward: false, till },
                n,
            )
        };
        assert_eq!(back(1, false), Some((0, 5)));
        assert_eq!(back(2, false), Some((0, 3)));
        assert_eq!(back(2, true), Some((0, 4)), "`T` stops one past it");

        let b = buf("axbxcxd\n", 0, 0);
        assert_eq!(
            go(&b, Motion::FindChar { target: 'x', forward: true, till: true }, 2),
            Some((0, 2)),
            "`2tx` stops before the second x"
        );
    }

    /// A count multiplies `$` too: `3$` is the end of the second line below.
    #[test]
    fn a_count_multiplies_the_end_of_line() {
        let b = buf("one\ntwo two\nthree\n", 0, 0);
        assert_eq!(go(&b, Motion::LineEnd, 1), Some((0, 2)));
        assert_eq!(go(&b, Motion::LineEnd, 2), Some((1, 6)));
        // Clamped to the last line — `three`, not the empty string ropey
        // counts after the trailing newline.
        assert_eq!(go(&b, Motion::LineEnd, 9), Some((2, 4)));
        assert_eq!(go(&b, Motion::LineEnd, 3), Some((2, 4)));
    }

    #[test]
    fn vertical_motions_keep_the_goal_column() {
        let b = buf("a long first line\nshort\nanother long line\n", 0, 15);
        // Down onto a short line clamps, but the GOAL is remembered.
        let r = resolve(&b, Motion::Down, 1, 10, false).unwrap();
        assert_eq!((r.target.line, r.target.col), (1, 4));
        assert_eq!(r.target.goal_col, 15, "the goal survives the clamp");
        assert_eq!(r.kind, MotionKind::Linewise);

        // Two lines down it can be honoured again.
        let mut b2 = buf("a long first line\nshort\nanother long line\n", 1, 4);
        b2.cursor.goal_col = 15;
        assert_eq!(go(&b2, Motion::Down, 1), Some((2, 15)));
    }

    #[test]
    fn edges_return_none_rather_than_moving_nowhere() {
        let b = buf("one\ntwo\n", 0, 0);
        assert_eq!(go(&b, Motion::Up, 1), None, "no line above the first");
        assert_eq!(go(&b, Motion::Left, 1), None);
        assert_eq!(go(&b, Motion::LineStart, 1), None, "already there");

        // The trailing newline ends `two`; it does not open a line below it,
        // so `two` is the last line, as in vim.
        let b = buf("one\ntwo\n", 1, 2);
        assert_eq!(go(&b, Motion::Down, 1), None, "nothing below the last line");
        assert_eq!(go(&b, Motion::Right, 1), None, "col 2 is the last char of `two`");

        // `H`/`M`/`L` need a viewport this module cannot see.
        assert_eq!(go(&b, Motion::ScreenTop, 1), None);
    }

    #[test]
    fn buffer_and_paragraph_motions_are_linewise() {
        let b = buf("one\n\ntwo\nthree\n\nfour\n", 0, 0);
        assert_eq!(kind(&b, Motion::BufferEnd), MotionKind::Linewise);
        assert_eq!(go(&b, Motion::ParagraphForward, 1), Some((1, 0)));
        assert_eq!(go(&b, Motion::ParagraphForward, 2), Some((4, 0)));

        let b = buf("one\n\ntwo\nthree\n\nfour\n", 5, 0);
        assert_eq!(go(&b, Motion::ParagraphBack, 1), Some((4, 0)));
        assert_eq!(go(&b, Motion::GotoLine(2), 1), Some((2, 0)));
        assert_eq!(go(&b, Motion::GotoLine(999), 1), None, "clamped to `four`, already here");
    }

    /// `G` lands on the last line of text, not on the empty string ropey
    /// counts after a trailing newline — and a file without one, or an empty
    /// one, still has its last line where it was.
    #[test]
    fn buffer_end_is_the_last_line_of_text() {
        let b = buf("one\ntwo\n", 0, 0);
        assert_eq!(go(&b, Motion::BufferEnd, 1), Some((1, 0)));
        assert_eq!(go(&b, Motion::GotoLine(99), 1), Some((1, 0)), "`99G` clamps there too");
        assert_eq!(go(&b, Motion::Down, 5), Some((1, 0)), "`5j` stops on it");
        assert_eq!(go(&b, Motion::PageDown, 1), Some((1, 0)));
        assert_eq!(go(&b, Motion::ParagraphForward, 1), Some((1, 2)), "`}}` ends on `two`");

        let b = buf("one\ntwo", 0, 0);
        assert_eq!(go(&b, Motion::BufferEnd, 1), Some((1, 0)), "no trailing newline");
        let b = buf("one\n\n", 0, 0);
        assert_eq!(go(&b, Motion::BufferEnd, 1), Some((1, 0)), "a real empty last line");
        // One line: already on the last line, so `G` stays put.
        let b = buf("", 0, 0);
        assert_eq!(go(&b, Motion::BufferEnd, 1), None, "an empty file");
        let b = buf("\n", 0, 0);
        assert_eq!(go(&b, Motion::BufferEnd, 1), None, "a lone newline is one line");
    }

    #[test]
    fn char_classes_separate_words_punctuation_and_space() {
        assert_eq!(char_class(' '), 0);
        assert_eq!(char_class('\t'), 0);
        assert_eq!(char_class('a'), 1);
        assert_eq!(char_class('_'), 1);
        assert_eq!(char_class('7'), 1);
        assert_eq!(char_class('é'), 1, "letters outside ASCII are still letters");
        assert_eq!(char_class('.'), 2);
    }
}