rat-text 3.1.0

ratatui text input widgets
Documentation
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
use crate::core::{TextCore, TextString};
use crate::text_input_mask::MaskedInputState;
use crate::text_input_mask::mask_token::{EditDirection, Mask, MaskToken};
use crate::{TextError, TextPosition, TextRange, upos_type};
use format_num_pattern::core::{clean_num, map_num};
use format_num_pattern::{CurrencySym, NumberFormat, NumberSymbols};
use std::ops::Range;
use unicode_segmentation::UnicodeSegmentation;

/// Remove the selection
pub fn remove_range(
    state: &mut MaskedInputState,
    range: Range<upos_type>,
) -> Result<bool, TextError> {
    // check valid range
    state
        .value
        .bytes_at_range(TextRange::new((range.start, 0), (range.end, 0)))?;

    if range.is_empty() {
        return Ok(false);
    }

    let mask = &state.mask[range.start as usize];
    if range.start >= mask.sub_start && range.end <= mask.sub_end {
        if mask.right.is_rtol() {
            state.value.begin_undo_seq();
            state
                .value
                .remove_str_range(TextRange::new((range.start, 0), (range.end, 0)))
                .expect("valid_range");
            let fill_before =
                &state.mask[mask.sub_start as usize..mask.sub_start as usize + range.len()];
            state
                .value
                .insert_str(
                    TextPosition::new(mask.sub_start, 0),
                    &MaskToken::empty_section(fill_before),
                )
                .expect("valid_range");
            reformat(&mut state.value, &state.mask, mask.sub_start..mask.sub_end);
            state.value.end_undo_seq();
        } else if mask.right.is_ltor() {
            state.value.begin_undo_seq();
            state
                .value
                .remove_str_range(TextRange::new((range.start, 0), (range.end, 0)))
                .expect("valid_range");
            let fill_after =
                &state.mask[mask.sub_end as usize - range.len()..mask.sub_end as usize];
            state
                .value
                .insert_str(
                    TextPosition::new(mask.sub_end - range.len() as upos_type, 0),
                    &MaskToken::empty_section(fill_after),
                )
                .expect("valid_range");
            reformat(&mut state.value, &state.mask, mask.sub_start..mask.sub_end);
            state.value.end_undo_seq();
        }

        return Ok(true);
    }

    let mut pos = range.start;
    state.value.begin_undo_seq();
    loop {
        let mask = &state.mask[pos as usize];

        if mask.sub_start < range.start {
            // partial start
            if mask.right.is_rtol() {
                state
                    .value
                    .remove_str_range(TextRange::new((range.start, 0), (mask.sub_end, 0)))
                    .expect("valid_range");

                let len = mask.sub_end - range.start;
                let fill_before =
                    &state.mask[mask.sub_start as usize..(mask.sub_start + len) as usize];
                state
                    .value
                    .insert_str(
                        TextPosition::new(mask.sub_start, 0),
                        &MaskToken::empty_section(fill_before),
                    )
                    .expect("valid_range");

                reformat(&mut state.value, &state.mask, mask.sub_start..mask.sub_end);

                pos = mask.sub_end;
            } else if mask.right.is_ltor() {
                state
                    .value
                    .remove_str_range(TextRange::new((range.start, 0), (mask.sub_end, 0)))
                    .expect("valid_range");

                let fill_after = &state.mask[range.start as usize..mask.sub_end as usize];
                state
                    .value
                    .insert_str(
                        TextPosition::new(range.start, 0),
                        &MaskToken::empty_section(fill_after),
                    )
                    .expect("valid_range");

                reformat(&mut state.value, &state.mask, mask.sub_start..mask.sub_end);

                pos = mask.sub_end;
            }
        } else if mask.sub_end > range.end {
            // partial end
            if mask.right.is_rtol() {
                state
                    .value
                    .remove_str_range(TextRange::new((mask.sub_start, 0), (range.end, 0)))
                    .expect("valid_range");

                let fill_before = &state.mask[mask.sub_start as usize..range.end as usize];
                state
                    .value
                    .insert_str(
                        TextPosition::new(mask.sub_start, 0),
                        &MaskToken::empty_section(fill_before),
                    )
                    .expect("valid_range");

                reformat(&mut state.value, &state.mask, mask.sub_start..mask.sub_end);
                pos = mask.sub_end;
            } else if mask.right.is_ltor() {
                state
                    .value
                    .remove_str_range(TextRange::new((mask.sub_start, 0), (range.end, 0)))
                    .expect("valid_range");

                let len = range.end - mask.sub_start;
                let fill_after = &state.mask[(mask.sub_end - len) as usize..mask.sub_end as usize];
                state
                    .value
                    .insert_str(
                        TextPosition::new(mask.sub_end - len, 0),
                        &MaskToken::empty_section(fill_after),
                    )
                    .expect("valid_range");

                pos = mask.sub_end;
            }
        } else {
            // full section
            state
                .value
                .remove_str_range(TextRange::new((mask.sub_start, 0), (mask.sub_end, 0)))
                .expect("valid_range");

            let sec_range = &state.mask[mask.sub_start as usize..mask.sub_end as usize];
            state
                .value
                .insert_str(
                    TextPosition::new(mask.sub_start, 0),
                    &MaskToken::empty_section(sec_range),
                )
                .expect("valid_range");

            pos = mask.sub_end;
        }

        if pos >= range.end {
            break;
        }
    }
    state.value.end_undo_seq();

    Ok(true)
}

/// Rebuild a section according to number-formatting.
/// The main purpose is to rebuild the grouping separators.
fn reformat(core: &mut TextCore<TextString>, mask: &[MaskToken], section: Range<upos_type>) {
    if mask[section.start as usize].right.is_rtol() {
        let cursor = core.cursor();
        let anchor = core.anchor();

        let sec_str = core
            .str_slice(TextRange::new((section.start, 0), (section.end, 0)))
            .expect("valid_range");

        // to be safe, always use our internal symbol set.
        let sym = NumberSymbols {
            decimal_sep: '.',
            decimal_grp: Some(','),
            negative_sym: '-',
            positive_sym: ' ',
            exponent_upper_sym: 'E',
            exponent_lower_sym: 'e',
            currency_sym: CurrencySym::new("$"),
        };

        // remove all non numbers and leading 0.
        let mut clean = String::new();
        _ = clean_num(sec_str.as_ref(), &sym, &mut clean);

        // create number format
        let mut tok = String::new();
        let submask = &mask[section.start as usize..section.end as usize];
        // default fmt.sym is nice
        for t in submask {
            match &t.right {
                Mask::Digit0(_) => tok.push('0'),
                Mask::Digit(_) => tok.push('9'),
                Mask::Numeric(_) => tok.push('#'),
                Mask::DecimalSep => tok.push('.'),
                Mask::GroupingSep => tok.push(','),
                Mask::Sign => tok.push('-'),
                Mask::Plus => tok.push('+'),
                Mask::Separator(s) => {
                    for c in s.chars() {
                        tok.push('\\');
                        tok.push(c);
                    }
                }
                Mask::None => {}
                _ => unreachable!("invalid mask"),
            }
        }

        let fmt = match NumberFormat::news(tok, sym) {
            Ok(v) => v,
            Err(_) => unreachable!("invalid mask"),
        };
        let mut out = String::new();
        match map_num::<_, false>(clean.as_str(), &fmt, fmt.sym(), &mut out) {
            Ok(_) => {}
            Err(_) => unreachable!("invalid mask"),
        }

        core.remove_char_range(TextRange::new((section.start, 0), (section.end, 0)))
            .expect("valid_range");
        core.insert_str(TextPosition::new(section.start, 0), &out)
            .expect("valid_position");

        // keep cursor intact
        core.set_cursor(anchor, false);
        core.set_cursor(cursor, true);
    } else if mask[section.start as usize].right.is_ltor() {
        let cursor = core.cursor();
        let anchor = core.anchor();

        let sec_str = core
            .str_slice(TextRange::new((section.start, 0), (section.end, 0)))
            .expect("valid_range");
        let sec_mask = &mask[section.start as usize..section.end as usize];
        let mut str_new = String::new();
        for (g, t) in sec_str.graphemes(true).zip(sec_mask.iter()) {
            match t.right {
                Mask::Digit0(_) | Mask::Hex0 | Mask::Oct0 | Mask::Dec0 => {
                    if g == " " {
                        str_new.push('0');
                    } else {
                        str_new.push_str(g);
                    }
                }
                _ => {
                    str_new.push_str(g);
                }
            }
        }

        if sec_str != str_new {
            core.remove_char_range(TextRange::new((section.start, 0), (section.end, 0)))
                .expect("valid_range");
            core.insert_str(TextPosition::new(section.start, 0), &str_new)
                .expect("valid_position");

            // keep cursor intact
            core.set_cursor(anchor, false);
            core.set_cursor(cursor, true);
        }
    }
}

/// Start at the cursor position and find a valid insert position for the input c.
/// Put the cursor at that position.
#[allow(clippy::if_same_then_else)]
pub fn advance_cursor(state: &mut MaskedInputState, c: char) -> bool {
    if state.mask.is_empty() {
        return false;
    }

    let mask_c = &state.mask[state.value.cursor().x as usize];

    let mut new_cursor = state.value.cursor().x;

    loop {
        let mask = &state.mask[new_cursor as usize];

        if can_insert_integer_left(state, mask, new_cursor, c) {
            // At the gap between an integer field and something else.
            // Integer fields are served first.
            break;
        } else if can_insert_integer(state, mask, new_cursor, c) {
            // Insert position inside an integer field. After any spaces
            // and the sign.
            break;
        } else if can_insert_sign(state, mask, new_cursor, c) {
            // Can insert a sign here.
            break;
        } else if can_insert_decimal_sep(state, mask, c) {
            // Decimal separator matches.
            break;
        } else if mask.right == Mask::GroupingSep {
            // Never stop here.
            new_cursor += 1;
        } else if can_insert_separator(state, mask, c) {
            break;
        } else if can_move_left_in_fraction(state, mask_c, mask, new_cursor, c) {
            // skip left
            new_cursor -= 1;
        } else if can_insert_fraction(state, mask_c, mask, c) {
            break;
        } else if can_insert_other(state, mask, c) {
            break;
        } else if mask.right == Mask::None {
            // No better position found. Reset and break;
            new_cursor = state.value.cursor().x;
            break;
        } else {
            new_cursor += 1;
        }
    }

    state
        .value
        .set_cursor(TextPosition::new(new_cursor, 0), false)
}

// Can edit the field left of the cursor.
#[inline]
fn can_insert_integer_left(
    state: &MaskedInputState,
    mask: &MaskToken,
    new_cursor: upos_type,
    c: char,
) -> bool {
    if !mask.peek_left.is_rtol() {
        return false;
    }
    if !mask.right.is_ltor() && !mask.right.is_none() {
        return false;
    }

    let left = &state.mask[new_cursor as usize - 1];
    if !is_valid_char(state, &left.right, c) {
        return false;
    }

    let mask0 = &state.mask[left.sub_start as usize];
    let g0 = state
        .value
        .grapheme_at(TextPosition::new(left.sub_start, 0))
        .expect("valid_position")
        .expect("grapheme");
    if !mask0.right.can_drop(g0.grapheme()) {
        return false;
    }

    true
}

// Is this the correct input position for a rtol field
#[inline]
fn can_insert_integer(
    state: &MaskedInputState,
    mask: &MaskToken,
    new_cursor: upos_type,
    c: char,
) -> bool {
    if !mask.right.is_rtol() {
        return false;
    }

    if !is_valid_char(state, &mask.right, c) {
        return false;
    }

    let g = state
        .value
        .grapheme_at(TextPosition::new(new_cursor, 0))
        .expect("valid_position")
        .expect("grapheme");
    if mask.right.can_drop(g.grapheme()) {
        return false;
    }
    if g == "-" {
        return false;
    }

    true
}

// Can input a sign here?
#[inline]
fn can_insert_sign<'a>(
    state: &'a MaskedInputState,
    mut mask: &'a MaskToken,
    new_cursor: upos_type,
    c: char,
) -> bool {
    if !is_valid_char(state, &Mask::Sign, c) {
        return false;
    }
    // boundary right/left. prefer right, change mask.
    if mask.peek_left.is_number() && (mask.right.is_ltor() || mask.right.is_none()) {
        mask = &state.mask[new_cursor as usize - 1];
    }
    if !mask.right.is_number() {
        return false;
    }

    // check possible positions for the sign.
    for i in mask.sec_start..mask.sec_end {
        let t = &state.mask[i as usize];
        match t.right {
            Mask::Plus => return true,
            Mask::Sign => return true,
            Mask::Numeric(EditDirection::Rtol) => {
                // Numeric fields can hold a sign.
                // If they are not otherwise occupied.
                let gi = state
                    .value
                    .grapheme_at(TextPosition::new(i, 0))
                    .expect("valid_position")
                    .expect("grapheme");

                return t.right.can_drop(gi.grapheme()) || gi == "-";
            }
            _ => {}
        }
    }

    false
}

// Can insert a decimal separator.
#[inline]
fn can_insert_decimal_sep(state: &MaskedInputState, mask: &MaskToken, c: char) -> bool {
    if mask.right != Mask::DecimalSep {
        return false;
    }
    if !is_valid_char(state, &mask.right, c) {
        return false;
    }
    true
}

// Separator char matches
#[inline]
fn can_insert_separator(state: &MaskedInputState, mask: &MaskToken, c: char) -> bool {
    if !matches!(mask.right, Mask::Separator(_)) {
        return false;
    }
    if !is_valid_char(state, &mask.right, c) {
        return false;
    }
    true
}

// When inserting to the fraction we want to left-align
// the digits. This checks if a digit could possibly be
// inserted to the left of the current position.
#[inline]
fn can_move_left_in_fraction(
    state: &MaskedInputState,
    mask_c: &MaskToken,
    mask: &MaskToken,
    new_cursor: upos_type,
    c: char,
) -> bool {
    if !mask.peek_left.is_fraction() {
        return false;
    }
    if !is_valid_char(state, &mask.peek_left, c) {
        return false;
    }
    // don't jump from integer to fraction
    if mask_c.is_integer_part() {
        return false;
    }

    let gl = state
        .value
        .grapheme_at(TextPosition::new(new_cursor - 1, 0))
        .expect("valid_position")
        .expect("grapheme");

    // is there space to the left?
    if gl != " " {
        return false;
    }

    true
}

// Can insert fraction.
#[inline]
fn can_insert_fraction(
    state: &MaskedInputState,
    mask_c: &MaskToken,
    mask: &MaskToken,
    c: char,
) -> bool {
    if !mask.right.is_fraction() {
        return false;
    }
    if !is_valid_char(state, &mask.right, c) {
        return false;
    }
    // don't jump from integer to fraction
    if mask_c.is_integer_part() {
        return false;
    }

    true
}

// Can insert other field types
#[inline]
fn can_insert_other(state: &MaskedInputState, mask: &MaskToken, c: char) -> bool {
    match mask.right {
        Mask::Hex0
        | Mask::Hex
        | Mask::Oct0
        | Mask::Oct
        | Mask::Dec0
        | Mask::Dec
        | Mask::Letter
        | Mask::LetterOrDigit
        | Mask::LetterDigitSpace
        | Mask::AnyChar => is_valid_char(state, &mask.right, c),
        _ => false,
    }
}

/// Valid input for this mask.
fn is_valid_char(state: &MaskedInputState, mask: &Mask, c: char) -> bool {
    match mask {
        Mask::Digit0(_) => c.is_ascii_digit(),
        Mask::Digit(_) => c.is_ascii_digit() || c == ' ',
        Mask::Numeric(_) => c.is_ascii_digit() || c == state.neg_sym() || c == '-',
        Mask::DecimalSep => c == state.dec_sep(),
        Mask::GroupingSep => false,
        Mask::Sign => c == state.neg_sym() || c == '-',
        Mask::Plus => c == state.neg_sym() || c == '-',
        Mask::Hex0 => c.is_ascii_hexdigit(),
        Mask::Hex => c.is_ascii_hexdigit() || c == ' ',
        Mask::Oct0 => c.is_digit(8),
        Mask::Oct => c.is_digit(8) || c == ' ',
        Mask::Dec0 => c.is_ascii_digit(),
        Mask::Dec => c.is_ascii_digit() || c == ' ',
        Mask::Letter => c.is_alphabetic(),
        Mask::LetterOrDigit => c.is_alphanumeric(),
        Mask::LetterDigitSpace => c.is_alphanumeric() || c == ' ',
        Mask::AnyChar => true,
        Mask::Separator(sep) => {
            // ',' and '.' match any separator.
            if c == '.' || c == ',' {
                true
            } else if let Some(sepc) = sep.chars().next() {
                sepc == c
            } else {
                false
            }
        }
        Mask::None => false,
    }
}

/// Insert the char if it matches the cursor mask and the current section is not full.
///
/// `advance_cursor()` must be called before for correct functionality.
///
/// Otherwise: your mileage might vary.
pub fn insert_char(state: &mut MaskedInputState, c: char) -> bool {
    if state.mask.is_empty() {
        return false;
    }

    let cursor = state.value.cursor();

    // note: because of borrow checker. calls &mut methods.
    {
        let mask = &state.mask[cursor.x as usize];
        if mask.right.is_number() && can_insert_sign(state, mask, cursor.x, c) {
            if insert_sign(state, c) {
                return true;
            }
        }
    }
    {
        let mask = &state.mask[cursor.x as usize];
        if mask.peek_left.is_number() && (mask.right.is_ltor() || mask.right.is_none()) {
            let left = &state.mask[cursor.x as usize - 1];
            if can_insert_sign(state, left, cursor.x, c) {
                if insert_sign(state, c) {
                    return true;
                }
            }
        }
    }
    {
        let mask = &state.mask[cursor.x as usize];
        if mask.right.is_rtol() {
            if insert_rtol(state, c) {
                return true;
            }
        }
    }
    {
        let mask = &state.mask[cursor.x as usize];
        if mask.peek_left.is_rtol() && (mask.right.is_ltor() || mask.right.is_none()) {
            if insert_rtol(state, c) {
                return true;
            }
        }
    }
    {
        let mask = &state.mask[cursor.x as usize];
        if mask.right.is_ltor() {
            if insert_ltor(state, c) {
                #[allow(clippy::needless_return)]
                return true;
            }
        }
    }

    false
}

/// Insert a sign c into the current number section
#[allow(clippy::single_match)]
fn insert_sign(state: &mut MaskedInputState, c: char) -> bool {
    let cursor = state.value.cursor();

    let mut mask = &state.mask[cursor.x as usize];
    // boundary right/left. prefer right, change mask.
    if mask.peek_left.is_number() && (mask.right.is_ltor() || mask.right.is_none()) {
        mask = &state.mask[cursor.x as usize - 1];
    }

    // explicit sign?
    let idx = state.mask[mask.sec_start as usize..mask.sec_end as usize]
        .iter()
        .enumerate()
        .find(|(_, t)| matches!(t.right, Mask::Sign | Mask::Plus))
        .map(|(i, _)| mask.sec_start as usize + i);

    // existing sign somewhere?
    let idx = if idx.is_none() {
        state
            .value
            .graphemes(
                TextRange::new((mask.sec_start, 0), (mask.sec_end, 0)),
                TextPosition::new(mask.sec_start, 0),
            )
            .expect("valid_range")
            .enumerate()
            .find(|(_, g)| *g == "-" || *g == "+")
            .map(|(i, _)| mask.sec_start as usize + i)
    } else {
        idx
    };

    let idx = if idx.is_none() {
        // moving sign
        let mut idx = mask.sec_end - 1;
        'f: {
            while idx >= mask.sec_start {
                if state.mask[idx as usize].right == Mask::Numeric(EditDirection::Rtol) {
                    let g = state
                        .grapheme_at(idx)
                        .expect("valid_position")
                        .expect("grapheme");

                    if state.mask[idx as usize].right.can_drop(g.grapheme()) {
                        break 'f Some(idx as usize);
                    }
                }
                idx -= 1;
            }
            None
        }
    } else {
        idx
    };

    if let Some(idx) = idx {
        let mask_sign = &state.mask[idx];

        if c == state.neg_sym() || c == '-' {
            // negate current
            let g = state
                .value
                .str_slice(TextRange::new(
                    (idx as upos_type, 0),
                    (idx as upos_type + 1, 0),
                ))
                .expect("valid_pos")
                .to_string();

            state.value.begin_undo_seq();
            state
                .value
                .remove_char_range(TextRange::new(
                    (idx as upos_type, 0),
                    (idx as upos_type + 1, 0),
                ))
                .expect("valid_range");

            let cc = match &mask_sign.right {
                Mask::Numeric(_) | Mask::Sign => {
                    if g == "-" {
                        ' '
                    } else {
                        '-'
                    }
                }
                Mask::Plus => {
                    if g == "-" {
                        '+'
                    } else {
                        '-'
                    }
                }
                _ => unreachable!(),
            };

            state
                .value
                .insert_char(TextPosition::new(idx as upos_type, 0), cc)
                .expect("valid_range");
            state.set_cursor(cursor.x, false);
            state.value.end_undo_seq();
            true
        } else {
            false
        }
    } else {
        false
    }
}

/// Insert c into a rtol section
fn insert_rtol(state: &mut MaskedInputState, c: char) -> bool {
    let cursor = state.value.cursor();

    let mut mask = &state.mask[cursor.x as usize];

    // boundary right/left. prefer right, change mask.
    if mask.peek_left.is_rtol() && (mask.right.is_ltor() || mask.right.is_none()) {
        mask = &state.mask[cursor.x as usize - 1];
    }

    let mask0 = &state.mask[mask.sub_start as usize];

    let g0 = state
        .value
        .grapheme_at(TextPosition::new(mask.sub_start, 0))
        .expect("valid_pos")
        .expect("grapheme");
    if mask0.right.can_drop(g0.grapheme()) && is_valid_char(state, &mask.right, c) {
        state.value.begin_undo_seq();
        state
            .value
            .remove_char_range(TextRange::new((mask.sub_start, 0), (mask.sub_start + 1, 0)))
            .expect("valid_position");
        state
            .value
            .insert_char(TextPosition::new(cursor.x - 1, 0), c)
            .expect("valid_position");
        reformat(&mut state.value, &state.mask, mask.sub_start..mask.sub_end);
        state.value.end_undo_seq();
        return true;
    }

    false
}

/// Insert c into a ltor section.
fn insert_ltor(state: &mut MaskedInputState, c: char) -> bool {
    let cursor = state.value.cursor();

    let mask = &state.mask[cursor.x as usize];
    let mask9 = &state.mask[mask.sub_end as usize - 1];

    // overwrite digit in fraction?
    let g = state
        .value
        .grapheme_at(cursor)
        .expect("valid_cursor")
        .expect("mask");
    if mask.right.is_fraction()
        && mask.right.can_overwrite_fraction(g.grapheme())
        && is_valid_char(state, &mask.right, c)
    {
        // to the right only defaults
        let frac_mask = &state.mask[cursor.x as usize + 1..mask.sub_end as usize];
        let frac_str = state
            .value
            .str_slice(TextRange::new((cursor.x + 1, 0), (mask.sub_end, 0)))
            .expect("valid_range");
        if frac_str == MaskToken::empty_section(frac_mask) {
            state.value.begin_undo_seq();
            state
                .value
                .remove_char_range(TextRange::new(cursor, (cursor.x + 1, 0)))
                .expect("valid_cursor");
            state.value.insert_char(cursor, c).expect("valid_cursor");
            state.value.end_undo_seq();
            return true;
        }
    }

    let g = state
        .value
        .grapheme_at(cursor)
        .expect("valid_cursor")
        .expect("mask");
    if mask.right.can_overwrite(g.grapheme()) && is_valid_char(state, &mask.right, c) {
        if mask.right.is_separator() {
            state.value.begin_undo_seq();
            let r = if let Some(next) = state.next_section_cursor(cursor.x) {
                state.value.set_cursor(TextPosition::new(next, 0), false)
            } else {
                state
                    .value
                    .set_cursor(TextPosition::new(state.line_width(), 0), false)
            };
            state.value.end_undo_seq();
            return r;
        } else if mask.right == Mask::DecimalSep {
            state.value.begin_undo_seq();
            state
                .value
                .set_cursor(TextPosition::new(cursor.x + 1, 0), false);
            state.value.end_undo_seq();
            return true;
        } else {
            state.value.begin_undo_seq();
            state
                .value
                .remove_char_range(TextRange::new(cursor, (cursor.x + 1, 0)))
                .expect("valid_cursor");
            state.value.insert_char(cursor, c).expect("valid_cursor");
            state.value.end_undo_seq();
            return true;
        }
    }

    // can shift right
    let g9 = state
        .value
        .grapheme_at(TextPosition::new(mask.sub_end - 1, 0))
        .expect("valid_pos")
        .expect("mask");
    if mask9.right.can_drop(g9.grapheme()) && is_valid_char(state, &mask.right, c) {
        state.value.begin_undo_seq();
        state
            .value
            .remove_char_range(TextRange::new((mask.sub_end - 1, 0), (mask.sub_end, 0)))
            .expect("valid_range");
        state.value.insert_char(cursor, c).expect("valid_cursor");
        state.value.end_undo_seq();
        return true;
    }
    false
}

/// Remove the previous char.
pub fn remove_next(state: &mut MaskedInputState) {
    let cursor = state.value.cursor();

    if cursor.x as usize == state.mask.len() - 1 {
        return;
    }

    let right = &state.mask[cursor.x as usize];

    // remove and fill with empty
    if right.right.is_rtol() {
        let l0 = &state.mask[right.sub_start as usize];

        state.value.begin_undo_seq();
        state
            .value
            .remove_char_range(TextRange::new(cursor, (cursor.x + 1, 0)))
            .expect("valid_range");
        state
            .value
            .insert_str(TextPosition::new(right.sub_start, 0), &l0.edit)
            .expect("valid_position");
        reformat(
            &mut state.value,
            &state.mask,
            right.sub_start..right.sub_end,
        );

        state
            .value
            .set_cursor(TextPosition::new(cursor.x + 1, 0), false);

        state.value.end_undo_seq();
    } else if right.right.is_ltor() {
        // Check if the section is empty
        let sec_str = state
            .value
            .str_slice(TextRange::new((right.sub_start, 0), (right.sub_end, 0)))
            .expect("valid_range");
        let sec_mask = &state.mask[right.sub_start as usize..right.sub_end as usize];
        let sec_empty = sec_str == MaskToken::empty_section(sec_mask);

        let l9 = &state.mask[right.sub_end as usize - 1];

        state.value.begin_undo_seq();
        state
            .value
            .remove_char_range(TextRange::new(cursor, (cursor.x + 1, 0)))
            .expect("valid_range");
        state
            .value
            .insert_str(TextPosition::new(right.sub_end - 1, 0), &l9.edit)
            .expect("valid_position");

        reformat(
            &mut state.value,
            &state.mask,
            right.sub_start..right.sub_end,
        );

        // in a ltor field keep the cursor at the same position until the
        // whole section is empty. Only then put it at the end of the section
        // to continue right of the section.
        if sec_empty {
            state
                .value
                .set_cursor(TextPosition::new(right.sub_end, 0), false);
        } else {
            // cursor stays
        }

        state.value.end_undo_seq();
    }
}

/// Remove the previous char.
pub fn remove_prev(state: &mut MaskedInputState) {
    let cursor = state.value.cursor();

    if cursor.x == 0 {
        return;
    }

    let left = &state.mask[cursor.x as usize - 1];

    if left.right.is_rtol() {
        // Check if the section is empty
        let sec_empty = if left.right.is_rtol() {
            let sec_str = state
                .value
                .str_slice(TextRange::new((left.sub_start, 0), (left.sub_end, 0)))
                .expect("valid_range");
            let sec_mask = &state.mask[left.sub_start as usize..left.sub_end as usize];
            sec_str == MaskToken::empty_section(sec_mask)
        } else {
            false
        };

        let l0 = &state.mask[left.sub_start as usize];

        state.value.begin_undo_seq();
        state
            .value
            .remove_char_range(TextRange::new((cursor.x - 1, 0), cursor))
            .expect("valid_range");
        state
            .value
            .insert_str(TextPosition::new(left.sub_start, 0), &l0.edit)
            .expect("valid_position");
        reformat(&mut state.value, &state.mask, left.sub_start..left.sub_end);

        // in a rtol field keep the cursor at the same position until the
        // whole section is empty. Only then put it at the beginning of the section
        // to continue left of the section.
        if sec_empty {
            state
                .value
                .set_cursor(TextPosition::new(left.sub_start, 0), false);
        } else {
            // cursor stays
        }

        state.value.end_undo_seq();
    } else if left.right.is_ltor() {
        let l9 = &state.mask[left.sub_end as usize - 1];

        state.value.begin_undo_seq();
        state
            .value
            .remove_char_range(TextRange::new((cursor.x - 1, 0), cursor))
            .expect("valid_range");
        state
            .value
            .insert_str(TextPosition::new(left.sub_end - 1, 0), &l9.edit)
            .expect("valid_position");

        reformat(&mut state.value, &state.mask, left.sub_start..left.sub_end);

        state
            .value
            .set_cursor(TextPosition::new(cursor.x - 1, 0), false);

        state.value.end_undo_seq();
    }
}