Skip to main content

indigo_core/mode/
insert.rs

1#![cfg_attr(not(test), expect(clippy::enum_glob_use))]
2
3use crate::{
4    client::ClientMut,
5    mode::{Mode, normal},
6};
7use indigo_key::{Keymap, KeymapResult, Mapping, keymap};
8use std::sync::Arc;
9
10#[derive(Debug)]
11pub enum Action {
12    EnterNormalMode,
13    DeleteBefore,
14    DeleteAfter,
15    InsertChar(char),
16    Todo(&'static str),
17}
18
19#[must_use]
20pub fn keymap() -> Keymap<Vec<Action>> {
21    use Action::*;
22    keymap! { Vec<Action>;
23        "<esc>" => vec![EnterNormalMode], /// Exit insert mode
24        "<tab>" => vec![InsertChar('\t')], /// Insert tab
25        "<ret>" => vec![InsertChar('\n')], /// Insert newline
26        "<bs>" => vec![DeleteBefore], /// Delete before cursor
27        "<del>" => vec![DeleteAfter], /// Delete after cursor
28        "<c-r>" => vec![Todo("<c-r>")], /// TODO: Insert register contents
29        "<c-v>" => vec![Todo("<c-v>")], /// TODO: Insert next key literally
30        "<a-;>" => vec![Todo("<a-;>")], /// TODO: Run one normal mode command
31        "<any>" => Mapping::Any(|key| Some(vec![InsertChar(char::from(key.typed_char()?))])), /// Insert character
32    }
33}
34
35pub fn handle_actions(client: &mut ClientMut, actions: &[Action]) {
36    for action in actions {
37        handle_action(client, action);
38    }
39}
40
41pub fn handle_action(client: &mut ClientMut, action: &Action) {
42    match action {
43        Action::EnterNormalMode => normal::enter(client),
44        Action::DeleteBefore => delete_before(client),
45        Action::DeleteAfter => delete_after(client),
46        Action::InsertChar(c) => insert_char(client, *c),
47        Action::Todo(keys) => todo(client, keys),
48    }
49}
50
51pub fn handle_keys(client: &mut ClientMut) -> bool {
52    let keymap = Arc::clone(&client.keymaps.insert);
53    match keymap.get_keys(&client.pending_keys) {
54        KeymapResult::Mapped(actions) => {
55            client.pending_keys.clear();
56            handle_actions(client, actions);
57            true
58        }
59        KeymapResult::Computed(actions) => {
60            client.pending_keys.clear();
61            handle_actions(client, &actions);
62            true
63        }
64        KeymapResult::Unmapped => {
65            client.pending_keys.clear();
66            false
67        }
68        KeymapResult::Pending => true,
69    }
70}
71
72pub fn enter(client: &mut ClientMut) {
73    if client.focused_buffer().text.readonly {
74        client.set_error("Buffer is readonly");
75        client.count = None;
76        return;
77    }
78    let mut window = client.focused_window_mut();
79    window
80        .selection_move()
81        .for_each_move(|mut range| range.collapse());
82    window.scroll_to_selection();
83    client.mode = Mode::Insert;
84    client.count = None;
85}
86
87fn delete_before(client: &mut ClientMut) {
88    let mut window = client.focused_window_mut();
89    window.selection_edit().delete_before();
90    window.scroll_to_selection();
91    client.count = None;
92}
93
94fn delete_after(client: &mut ClientMut) {
95    let mut window = client.focused_window_mut();
96    window.selection_edit().delete_after();
97    window.scroll_to_selection();
98    client.count = None;
99}
100
101fn insert_char(client: &mut ClientMut, char: char) {
102    let mut window = client.focused_window_mut();
103    window.selection_edit().insert_char(char);
104    window.scroll_to_selection();
105    client.count = None;
106}
107
108fn todo(client: &mut ClientMut, keys: &str) {
109    client.set_error(format!("{keys} is not implemented yet"));
110    client.count = None;
111}
112
113pub fn paste(client: &mut ClientMut, text: &str) {
114    let mut window = client.focused_window_mut();
115    window.selection_edit().insert(text);
116    window.scroll_to_selection();
117    client.count = None;
118}