struct LispEditor;
impl pomprt::Editor for LispEditor {
fn insert(&self, buffer: &mut String, cursor: &mut usize, c: char) {
if !(c == ')' && buffer[*cursor..].starts_with(')')) {
buffer.insert(*cursor, c);
}
*cursor += c.len_utf8();
if c == '(' && !buffer[*cursor..].starts_with(|c: char| c.is_ascii_alphanumeric()) {
buffer.insert(*cursor, ')');
}
}
fn is_multiline(&self, buffer: &str, cursor: usize) -> bool {
buffer[..cursor]
.chars()
.fold(0_isize, |depth, c| match c {
'(' => depth + 1,
')' => depth - 1,
_ => depth,
})
.is_positive()
}
}
fn main() {
for line in pomprt::with_multiline(LispEditor, ">> ", ".. ") {
println!("{line}");
}
}