forked from mars/breed
161 lines
4.5 KiB
Rust
161 lines
4.5 KiB
Rust
/*
|
|
* Copyright (c) 2023 Marceline Cramer
|
|
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
* later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
*/
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use crate::{Direction, InsertState, Mode, NormalState, State};
|
|
|
|
pub type Action = fn(&mut State);
|
|
|
|
pub fn load_actions() -> HashMap<String, Action> {
|
|
let list: &'static [(&'static str, fn(&mut State))] = &[
|
|
("move_char_left", move_char_left),
|
|
("move_line_down", move_line_down),
|
|
("move_line_up", move_line_up),
|
|
("move_char_right", move_char_right),
|
|
("page_up", page_up),
|
|
("page_down", page_down),
|
|
("goto_line_start", goto_line_start),
|
|
("goto_line_end_newline", goto_line_end_newline),
|
|
("command_mode", command_mode),
|
|
("normal_mode", normal_mode),
|
|
("visual_mode", visual_mode),
|
|
("insert_mode", insert_mode),
|
|
("append_mode", append_mode),
|
|
("insert_at_line_start", insert_at_line_start),
|
|
("insert_at_line_end", insert_at_line_end),
|
|
("open_below", open_below),
|
|
("open_above", open_above),
|
|
("undo", undo),
|
|
("redo", redo),
|
|
("delete_char_backward", delete_char_backward),
|
|
("insert_newline", insert_newline),
|
|
];
|
|
|
|
let mut actions = HashMap::with_capacity(list.len());
|
|
for (name, action) in list.iter() {
|
|
actions.insert(name.to_string(), *action);
|
|
}
|
|
|
|
actions
|
|
}
|
|
|
|
pub fn move_char_left(state: &mut State) {
|
|
state.move_cursor(Direction::Left);
|
|
}
|
|
|
|
pub fn move_char_right(state: &mut State) {
|
|
state.move_cursor(Direction::Right);
|
|
}
|
|
|
|
pub fn move_line_down(state: &mut State) {
|
|
state.move_cursor(Direction::Down);
|
|
}
|
|
|
|
pub fn move_line_up(state: &mut State) {
|
|
state.move_cursor(Direction::Up);
|
|
}
|
|
|
|
pub fn page_up(state: &mut State) {
|
|
state.set_error("page_up is unimplemented");
|
|
}
|
|
|
|
pub fn page_down(state: &mut State) {
|
|
state.set_error("page_down is unimplemented");
|
|
}
|
|
|
|
pub fn goto_line_start(state: &mut State) {
|
|
state.set_error("goto_line_start is unimplemented");
|
|
}
|
|
|
|
pub fn goto_line_end_newline(state: &mut State) {
|
|
state.set_error("goto_line_end_newline is unimplemented");
|
|
}
|
|
|
|
pub fn command_mode(state: &mut State) {
|
|
state.mode = Mode::Command(Default::default());
|
|
}
|
|
|
|
pub fn normal_mode(state: &mut State) {
|
|
match state.mode {
|
|
Mode::Insert(InsertState { append: true }) => {
|
|
state.move_cursor(Direction::Left);
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
state.mode = Mode::Normal(NormalState::default());
|
|
}
|
|
|
|
pub fn visual_mode(state: &mut State) {
|
|
state.mode = Mode::Visual;
|
|
}
|
|
|
|
pub fn insert_mode(state: &mut State) {
|
|
state.mode = Mode::Insert(InsertState { append: false });
|
|
}
|
|
|
|
pub fn append_mode(state: &mut State) {
|
|
state.mode = Mode::Insert(InsertState { append: true });
|
|
}
|
|
|
|
pub fn insert_at_line_start(state: &mut State) {
|
|
state.set_error("insert_at_line_start is unimplemented");
|
|
}
|
|
|
|
pub fn insert_at_line_end(state: &mut State) {
|
|
state.set_error("insert_at_line_end is unimplemented");
|
|
}
|
|
|
|
pub fn open_below(state: &mut State) {
|
|
state.cursor.line += 1;
|
|
state.cursor.column = 0;
|
|
state.buffer.insert_char(state.cursor, '\n');
|
|
state.mode = Mode::Insert(InsertState { append: false });
|
|
}
|
|
|
|
pub fn open_above(state: &mut State) {
|
|
state.cursor.column = 0;
|
|
state.buffer.insert_char(state.cursor, '\n');
|
|
state.mode = Mode::Insert(InsertState { append: false });
|
|
}
|
|
|
|
pub fn undo(state: &mut State) {
|
|
state.set_error("undo is unimplemented");
|
|
}
|
|
|
|
pub fn redo(state: &mut State) {
|
|
state.set_error("redo is unimplemented");
|
|
}
|
|
|
|
pub fn delete_char_backward(state: &mut State) {
|
|
state.move_cursor(Direction::Left);
|
|
state.buffer.remove(state.cursor);
|
|
}
|
|
|
|
pub fn delete_char_forward(state: &mut State) {
|
|
state.buffer.remove(state.cursor);
|
|
}
|
|
|
|
pub fn insert_newline(state: &mut State) {
|
|
state.buffer.insert_char(state.cursor, '\n');
|
|
state.cursor.line += 1;
|
|
state.cursor.column = 0;
|
|
}
|