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
use crate::{
DEBUG,
action::match_action,
commands::Command as Cmd,
meta_command::match_meta_command,
mode::Mode,
motion::{Motion, next_row, prev_row},
operator::Operator,
register::RegisterHandler,
status_bar::StatusBar,
text_object::{TextObject, TextObjectType},
undo::{Action, UndoTree},
view::View,
view_command::ViewCommand,
};
use anyhow::Result;
use crossterm::event::{Event, KeyCode, read};
/// The main loop of Orinfar
/// Essentially just waits for a keypress, matches on it, then updates the state of the editor in
/// accordance with the action taken.
/// # Arguments
/// This function essentially consumes every relevant piece of data in the program
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_lines)]
pub fn program_loop<'a>(
commands: &[Cmd],
operators: &'a [Operator<'a>],
motions: &[Motion],
text_objects: &[TextObject],
view_commands: &[ViewCommand],
mut count: u16,
mut chained: Vec<char>,
mut next_operation: Option<&'a Operator<'a>>,
mut text_object_type: Option<TextObjectType>,
all_normal_chars: &[char],
mut search_str: Vec<char>,
mut status_bar: StatusBar,
mut register_handler: RegisterHandler,
mut undo_tree: UndoTree,
mut view: View,
mut mode: Mode,
) -> Result<()> {
let mut last_count = 1;
let mut last_chained: Vec<char> = vec![];
'main: loop {
let buffer = view.get_buffer_mut();
buffer.update_list_reset();
if let Event::Key(event) = read()? {
match (event.code, mode.clone()) {
(KeyCode::Char(c), Mode::Normal) if c.is_numeric() => {
let c = u16::try_from(c.to_digit(10).expect("Numeric digit not in base 10"))
.unwrap();
if count == 1 {
count = 0;
}
count *= 10;
count += c;
}
(KeyCode::Char(':'), Mode::Normal) => {
mode = Mode::Meta;
status_bar.push(':');
}
(KeyCode::Char('/'), Mode::Normal) => {
mode.search();
status_bar.push('/');
}
(KeyCode::Char('n'), Mode::Normal) => buffer.goto_next_string(&search_str),
(KeyCode::Char('N'), Mode::Normal) => buffer.goto_prev_string(&search_str),
(KeyCode::Char('.'), Mode::Normal) => match_action(
&mut chained,
&mut next_operation,
&mut text_object_type,
&mut count,
&mut last_chained,
&mut last_count,
&mut register_handler,
&mut undo_tree,
&mut view,
&mut mode,
commands,
operators,
motions,
text_objects,
view_commands,
),
(KeyCode::Char(c), Mode::Normal) => {
log!("c {}", c);
if !all_normal_chars.contains(&c) {
continue;
}
chained.push(c);
match_action(
&mut chained,
&mut next_operation,
&mut text_object_type,
&mut count,
&mut last_chained,
&mut last_count,
&mut register_handler,
&mut undo_tree,
&mut view,
&mut mode,
commands,
operators,
motions,
text_objects,
view_commands,
);
}
(KeyCode::Esc, Mode::Normal) => {
chained.clear();
count = 1;
next_operation = None;
}
(KeyCode::Esc, Mode::Insert) => {
if buffer.cursor != buffer.get_start_of_line() {
buffer.cursor -= 1;
}
mode.normal();
}
(KeyCode::Backspace, Mode::Insert) => {
buffer.backspace(&mut undo_tree);
}
(KeyCode::Char(c), Mode::Insert) => {
buffer.insert_char(c);
buffer.cursor += 1;
buffer.update_list_use_current_line();
let action = Action::insert(buffer.cursor - 1, &c);
undo_tree.new_action_merge(action);
}
(KeyCode::Tab, Mode::Insert) => {
// NOTE
// Iterates two separate times because we want the insertation batched and
// the traversal to happen after
buffer.insert_char_n_times(' ', 4);
(0..4).for_each(|_| {
buffer.next_char();
});
buffer.update_list_use_current_line();
}
(KeyCode::Enter, Mode::Insert) => {
let newline = buffer.insert_newline();
let action = Action::insert(buffer.cursor - newline.len(), &newline);
undo_tree.new_action(action);
}
(KeyCode::Enter, Mode::Meta) => {
if match_meta_command(
&mut status_bar,
&mut view,
®ister_handler,
&mut undo_tree,
&mut mode,
)? {
break 'main;
}
}
(KeyCode::Char(c), Mode::Meta | Mode::Search) => {
status_bar.push(c);
}
(KeyCode::Esc, Mode::Meta | Mode::Search) => {
mode.normal();
status_bar.clear();
}
(KeyCode::Backspace, Mode::Meta | Mode::Search) => {
status_bar.delete();
}
(_, Mode::Meta) => {}
(KeyCode::Enter, Mode::Search) => {
search_str = status_bar.buffer().split_at(1).1.chars().collect();
mode.normal();
status_bar.clear();
}
(KeyCode::Left, _) => {
buffer.prev_char();
}
(KeyCode::Right, _) => {
buffer.next_char();
}
(KeyCode::Up, _) => {
prev_row(buffer);
}
(KeyCode::Down, _) => {
next_row(buffer);
}
_ => continue,
}
let _ = view.get_view_box().parse();
let adjusted = view.adjust();
view.flush(
&status_bar,
&mode,
&chained,
count,
register_handler.get_curr_reg(),
adjusted,
)?;
}
}
Ok(())
}