-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
oxa.rs
361 lines (342 loc) · 10.7 KB
/
oxa.rs
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
/*
Oxa.rs - Tools for parsing and lexing the Ox Assembly format
Oxa is an interpreted specific purpose language inspired by x86 assembly
It is used to write macros for the editor that make editing text painless
It is also used for writing commands in the "macro mode" when editing
An example usage could be writing a macro to delete the current line
*/
use crate::undo::BankType;
use crate::util::line_offset;
use crate::{Direction, Event, Position, Row};
#[derive(Debug, Copy, Clone)]
pub enum Variable {
Saved,
}
pub fn interpret_line(
line: &str,
cursor: &Position,
graphemes: usize,
rows: &[Row],
) -> Option<Vec<Event>> {
// Take an instruction of Oxa and interpret it
let mut events = vec![];
let mut line = line.split(' ');
if let Some(instruction) = line.next() {
let mut args: Vec<&str> = line.collect();
let root = if let Some(&"sudo") = args.get(0) {
args.remove(0);
true
} else {
false
};
match instruction {
"new" => events.push(Event::New),
"open" => events.push(open_command(&args)),
"undo" => events.push(Event::Undo),
"commit" => events.push(Event::Commit),
"redo" => events.push(Event::Redo),
"quit" => events.push(quit_command(&args)),
"prev" => events.push(Event::PrevTab),
"next" => events.push(Event::NextTab),
"set" => events.push(set_command(&args, &cursor, &rows)),
"split" => events.push(Event::SplitDown(*cursor, *cursor)),
"splice" => events.push(Event::SpliceUp(*cursor, *cursor)),
"search" => events.push(Event::Search),
"reload" => events.push(Event::ReloadConfig),
"cmd" => events.push(Event::Cmd),
"replace" => events.push(replace_command(&args)),
// Shell with substitution and no confirm
"shs" => events.push(Event::Shell(args.join(" "), false, true, root)),
// Shell with substitution and confirm
"shcs" => events.push(Event::Shell(args.join(" "), true, true, root)),
// Shell with confirm and no substitution
"shc" => events.push(Event::Shell(args.join(" "), true, false, root)),
// Shell with no confirm nor substitution
"sh" => events.push(Event::Shell(args.join(" "), false, false, root)),
"is" => {
if let Some(set) = is_command(&args) {
events.push(set)
} else {
return None;
}
}
"theme" => {
if let Some(theme) = theme_command(&args) {
events.push(theme)
} else {
return None;
}
}
"line" => {
if let Some(line) = line_command(&args, &cursor) {
events.push(line);
} else {
return None;
}
}
_ => {
let i = match instruction {
"save" => save_command(&args),
"goto" => goto_command(&args),
"move" => move_command(&args),
"put" => put_command(&args, &cursor),
"delete" => delete_command(&args, &cursor, graphemes, &rows),
"load" => load_command(&args),
"store" => store_command(&args),
"overwrite" => overwrite_command(&args, &rows),
_ => return None,
};
if let Some(mut command) = i {
events.append(&mut command);
} else {
return None;
}
}
}
}
Some(events)
}
fn is_command(args: &[&str]) -> Option<Event> {
Some(Event::Set(
match &args[0][..] {
"saved" => Variable::Saved,
_ => return None,
},
true,
))
}
fn theme_command(args: &[&str]) -> Option<Event> {
if args.is_empty() {
None
} else {
Some(Event::Theme(args[0].to_string()))
}
}
fn replace_command(args: &[&str]) -> Event {
if !args.is_empty() && args[0] == "*" {
Event::ReplaceAll
} else {
Event::Replace
}
}
fn open_command(args: &[&str]) -> Event {
Event::Open(if args.is_empty() {
None
} else {
Some(args[0].to_string())
})
}
fn quit_command(args: &[&str]) -> Event {
if args.contains(&"*") {
Event::QuitAll(args.contains(&"!"))
} else {
Event::Quit(args.contains(&"!"))
}
}
fn line_command(args: &[&str], cursor: &Position) -> Option<Event> {
if args.is_empty() {
return None;
} else if let Some(dir) = args.get(0) {
return match *dir {
"below" => Some(Event::InsertLineBelow(*cursor)),
"above" => Some(Event::InsertLineAbove(*cursor)),
_ => None,
};
}
None
}
fn set_command(args: &[&str], cursor: &Position, rows: &[Row]) -> Event {
if args.is_empty() {
Event::UpdateLine(
*cursor,
0,
Box::new(rows[cursor.y].clone()),
Box::new(Row::from("")),
)
} else {
Event::UpdateLine(
*cursor,
0,
Box::new(rows[cursor.y].clone()),
Box::new(Row::from(args.join(" ").as_str())),
)
}
}
fn overwrite_command(args: &[&str], rows: &[Row]) -> Option<Vec<Event>> {
Some(vec![if args.is_empty() {
Event::Overwrite(rows.to_vec(), vec![Row::from("")])
} else {
Event::Overwrite(
rows.to_vec(),
args.join(" ")
.split("\\n")
.map(Row::from)
.collect::<Vec<_>>(),
)
}])
}
fn save_command(args: &[&str]) -> Option<Vec<Event>> {
let mut events = vec![];
if args.is_empty() {
events.push(Event::Save(None, false))
} else {
events.push(if args[0] == "*" {
Event::SaveAll
} else if args[0] == "?" {
Event::Save(None, true)
} else {
Event::Save(Some(args[0].to_string()), false)
})
}
Some(events)
}
fn store_command(args: &[&str]) -> Option<Vec<Event>> {
let mut events = vec![];
if args.len() == 2 {
if let Ok(bank) = args[1].parse::<usize>() {
if let Some(kind) = args.get(0) {
match *kind {
"cursor" => events.push(Event::Store(BankType::Cursor, bank)),
"line" => events.push(Event::Store(BankType::Line, bank)),
_ => return None,
}
}
} else {
return None;
}
} else {
return None;
}
Some(events)
}
fn load_command(args: &[&str]) -> Option<Vec<Event>> {
let mut events = vec![];
if args.len() == 2 {
if let Ok(bank) = args[1].parse::<usize>() {
if let Some(kind) = args.get(0) {
match *kind {
"cursor" => events.push(Event::Load(BankType::Cursor, bank)),
"line" => events.push(Event::Load(BankType::Line, bank)),
_ => return None,
}
}
} else {
return None;
}
} else {
return None;
}
Some(events)
}
fn goto_command(args: &[&str]) -> Option<Vec<Event>> {
let mut events = vec![];
match args.len() {
0 => events.push(Event::GotoCursor(Position { x: 0, y: 0 })),
1 => {
if let Ok(y) = args[0].parse::<usize>() {
events.push(Event::GotoCursor(Position {
x: 0,
y: y.saturating_sub(1),
}));
} else {
return None;
}
}
2 => {
if let (Ok(x), Ok(y)) = (args[0].parse::<usize>(), args[1].parse::<usize>()) {
events.push(Event::GotoCursor(Position {
x: x.saturating_sub(1),
y: y.saturating_sub(1),
}));
} else {
return None;
}
}
_ => return None,
}
Some(events)
}
fn put_command(args: &[&str], cursor: &Position) -> Option<Vec<Event>> {
let mut events = vec![];
if args[0] == "\\t" {
events.push(Event::InsertTab(*cursor));
} else {
for (c, ch) in args.join(" ").chars().enumerate() {
events.push(Event::Insertion(
Position {
x: cursor.x.saturating_add(c),
y: cursor.y,
},
ch,
))
}
}
Some(events)
}
fn move_command(args: &[&str]) -> Option<Vec<Event>> {
let mut events = vec![];
if args.len() == 2 {
if let Ok(magnitude) = args[0].parse::<usize>() {
let direction = args[1];
events.push(Event::MoveCursor(
magnitude as i128,
match direction {
"up" => Direction::Up,
"down" => Direction::Down,
"left" => Direction::Left,
"right" => Direction::Right,
_ => return None,
},
));
} else if args[0] == "word" {
events.push(Event::MoveWord(match args[1] {
"left" => Direction::Left,
"right" => Direction::Right,
_ => return None,
}));
} else {
return None;
}
} else if let Some(direction) = args.get(0) {
events.push(match *direction {
"home" => Event::Home,
"end" => Event::End,
"pageup" => Event::PageUp,
"pagedown" => Event::PageDown,
_ => return None,
});
} else {
return None;
}
Some(events)
}
fn delete_command(
args: &[&str],
cursor: &Position,
graphemes: usize,
rows: &[Row],
) -> Option<Vec<Event>> {
// Handle the delete command (complicated)
let mut events = vec![];
if args.is_empty() {
if let Some(ch) = rows[cursor.y]
.string
.chars()
.collect::<Vec<_>>()
.get(graphemes)
{
events.push(Event::Deletion(*cursor, *ch));
}
} else if args[0] == "word" {
events.push(Event::DeleteWord(*cursor, "egg".to_string()));
} else if let Ok(line) = args[0].parse::<i128>() {
events.push(Event::DeleteLine(
*cursor,
line,
Box::new(rows[line_offset(cursor.y, line, rows.len())].clone()),
));
} else {
return None;
}
Some(events)
}