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
pub mod error_consts;
mod cmd;
pub mod ui;
pub mod buffer;
use ui::UI;
use buffer::Buffer;
pub struct Ed <'a, B: Buffer> {
selection: Option<(usize, usize)>,
buffer: &'a mut B,
path: String,
s_args: Option<(String, String, bool)>,
print_errors: bool,
error: Option<&'static str>,
}
impl <'a, B: Buffer> Ed <'a, B> {
pub fn new(
buffer: &'a mut B,
path: String,
) -> Result<Self, &'static str> {
let len = path.len();
if len != 0 {
buffer.read_from(&path, None, false)?;
}
let tmp = Self {
print_errors: true,
error: None,
s_args: None,
selection: None,
buffer: buffer,
path: path,
};
Ok(tmp)
}
pub fn run_command(
&mut self,
ui: &mut dyn UI,
command: &str,
) -> Result<bool, &'static str> {
match cmd::run(self, ui, command) {
Err(e) => {
self.error = Some(e);
Err(e)
},
x => x,
}
}
pub fn run_macro(
&mut self,
ui: &mut dyn UI,
) -> Result<(), &'static str> {
loop {
let cmd = match ui.get_command( self.buffer ) {
Err(e) => { self.error = Some(e); return Err(e) },
Ok(x) => x,
};
if self.run_command(ui, &cmd)? {
break;
}
}
Ok(())
}
pub fn run(
&mut self,
ui: &mut dyn UI,
) -> Result<(), &'static str> {
loop {
match self.run_macro(ui) {
Ok(()) => break,
Err(_) => {
if self.print_errors {
ui.print(self.error.unwrap())?;
}
else {
ui.print("?\n")?;
}
},
}
}
Ok(())
}
}