yggdrasil/red/src/config.rs

67 lines
1.8 KiB
Rust
Raw Normal View History

2023-11-20 13:03:02 +02:00
use crate::{
buffer::Mode,
command::Action,
2023-11-24 13:29:53 +02:00
keymap::{bindn, bind1, KeyMap, KeySeq, PrefixNode},
2023-11-20 13:03:02 +02:00
};
2023-11-18 22:44:11 +02:00
pub struct Config {
// TODO must be a power of 2, lol
pub tab_width: usize,
pub number: bool,
2023-11-20 13:03:02 +02:00
pub nmap: KeyMap,
pub imap: KeyMap,
2023-11-18 22:44:11 +02:00
}
impl Default for Config {
fn default() -> Self {
use Action::*;
2023-11-20 13:03:02 +02:00
let nmap = KeyMap::from_iter([
2023-11-24 13:29:53 +02:00
bind1('i', [InsertBefore]),
bind1('a', [InsertAfter]),
bind1('h', [MoveCharPrev]),
bind1('l', [MoveCharNext]),
bind1('j', [MoveLineForward(1)]),
bind1('J', [MoveLineForward(25)]),
bind1('k', [MoveLineBack(1)]),
bind1('K', [MoveLineBack(25)]),
bindn(['g', 'g'], [MoveFirstLine]),
bind1('G', [MoveLastLine]),
bind1('I', [MoveLineStart, InsertBefore]),
bind1('A', [MoveLineEnd, InsertAfter]),
bind1('o', [NewlineAfter, MoveLineForward(1), InsertBefore]),
bind1('O', [NewlineBefore, MoveLineBack(1), InsertBefore]),
bindn(['d', 'd'], [KillLine]),
2023-11-20 13:03:02 +02:00
]);
let imap = KeyMap::from_iter([
2023-11-24 13:29:53 +02:00
bind1('\x7F', [EraseBackward]),
bind1(
2023-11-20 13:03:02 +02:00
'\n',
[BreakLine, MoveLineForward(1), MoveLineStart, InsertBefore],
),
2023-11-24 13:29:53 +02:00
bind1(
2023-11-20 13:03:02 +02:00
'\x0D',
[BreakLine, MoveLineForward(1), MoveLineStart, InsertBefore],
),
]);
2023-11-18 22:44:11 +02:00
Self {
tab_width: 4,
number: true,
2023-11-20 13:03:02 +02:00
nmap,
imap,
2023-11-18 22:44:11 +02:00
}
}
}
impl Config {
2023-11-24 13:29:53 +02:00
pub fn key_seq(&self, mode: Mode, seq: &KeySeq) -> Option<&PrefixNode<KeySeq, Vec<Action>>> {
2023-11-18 22:44:11 +02:00
match mode {
2023-11-24 13:29:53 +02:00
Mode::Normal => self.nmap.get(seq),
Mode::Insert => self.imap.get(seq),
2023-11-20 13:03:02 +02:00
}
2023-11-18 22:44:11 +02:00
}
}