Commit 9abf4fd4 authored by Ticki's avatar Ticki

Fix bug related to modifier keys

parent e6ba1dc3
- [x] Make editor.pos method and use that instead of
- [ ] Add word navigation
- [ ] `.` command
Known bugs:
......@@ -30,4 +31,9 @@ The bug causing these two bugs, is localised to be in position.rs. It resolves b
- [x] When typing the first char in a line in normal insert mode, it wont go to the next char.
- [ ] The modifier keys are only working for one command
- [x] The modifier keys are only working for one command
Solutions:
- Make a struct KeyState storing info on the modifiers active. Add a method `feed` which feeds the keystate with a key, updating it. This should Option<Key>, where a key should be returned iff the key entered was not a modifier
Refactoring:
- Organize into modules
......@@ -21,6 +21,8 @@ pub struct Editor {
pub prompt: String,
/// The settings
pub options: Options,
/// The key state
pub key_state: KeyState,
}
impl Editor {
......@@ -44,6 +46,7 @@ impl Editor {
status_bar: StatusBar::new(),
prompt: String::new(),
options: Options::new(),
key_state: KeyState::new(),
};
editor.text.push_back(VecDeque::new());
......
......@@ -12,7 +12,7 @@ impl Editor {
let n = para.d();
if cmd.key == Key::Char(' ') && cmd.shift {
if cmd.key == Key::Char(' ') && self.key_state.shift {
let mode = self.cursor().mode;
......@@ -22,11 +22,11 @@ impl Editor {
}
self.cursor_mut().mode = Mode::Command(CommandMode::Normal);
} else if cmd.alt && cmd.key == Key::Char(' ') {
} else if self.key_state.alt && cmd.key == Key::Char(' ') {
self.next_cursor();
} else if cmd.alt {
} else if self.key_state.alt {
if let Some(m) = self.to_motion(Inst(para, cmd)) {
self.goto(m);
......
use redox::*;
#[derive(Copy, Clone, PartialEq)]
/// A key
pub enum Key {
......@@ -14,12 +16,38 @@ pub enum Key {
Unknown(u8),
}
impl Key {
pub fn from_event(k: orbital::event::KeyEvent) -> Key {
match k.character {
'\0' => match k.scancode {
s if k.pressed => match s {
K_BKSP => Key::Backspace,
K_LEFT => Key::Left,
K_RIGHT => Key::Right,
K_UP => Key::Up,
K_DOWN => Key::Down,
K_TAB => Key::Tab,
K_ESC => Key::Escape,
_ => Key::Unknown(s),
},
_ => Key::Null,
},
c => Key::Char(c),
}
}
pub fn to_char(self) -> char {
match self {
Key::Char(c) => c,
_ => '\0'
}
}
}
#[derive(Copy, Clone, PartialEq)]
/// A command, i.e. a key toghether with information on the modifiers.
pub struct Cmd {
pub key: Key,
pub ctrl: bool,
pub alt: bool,
pub shift: bool,
}
......@@ -39,10 +39,10 @@ impl Editor {
if let EventOption::Key(k) = self.window.poll()
.unwrap_or(Event::new())
.to_option() {
if k.pressed {
self.status_bar.cmd.push(k.character);
if let Some(Key::Char(c)) = self.key_state.feed(k) {
self.status_bar.cmd.push(c);
self.redraw_status_bar();
return k.character;
return c;
}
}
}
......@@ -63,98 +63,74 @@ impl Editor {
// self.status_bar.cmd = String::new();
loop {
if let EventOption::Key(k) = self.window.poll().unwrap_or(Event::new()).to_option() {
if let EventOption::Key(key_event) = self.window.poll().unwrap_or(Event::new()).to_option() {
let c = k.character;
match c {
'\0' => {
// "I once lived here" - bug
match k.scancode {
K_ALT => alt = k.pressed,
K_CTRL => ctrl = k.pressed,
K_LEFT_SHIFT | K_RIGHT_SHIFT => shift = k.pressed,
s if k.pressed => key = match s {
K_BKSP => Key::Backspace,
K_LEFT => Key::Left,
K_RIGHT => Key::Right,
K_UP => Key::Up,
K_DOWN => Key::Down,
K_TAB => Key::Tab,
K_ESC => Key::Escape,
_ => Key::Unknown(s),
},
s => key = Key::Unknown(s),
}
}
_ => if k.pressed {
self.status_bar.cmd.push(c);
self.redraw_status_bar();
if let Some(k) = self.key_state.feed(key_event) {
let c = k.to_char();
self.status_bar.cmd.push(c);
self.redraw_status_bar();
match self.cursor().mode {
Mode::Primitive(_) => {
key = Key::Char(c);
},
Mode::Command(_) => {
n = match c {
'0' => {
unset = false;
n * 10
},
'1' => {
unset = false;
n * 10 + 1
},
'2' => {
unset = false;
n * 10 + 2
},
'3' => {
unset = false;
n * 10 + 3
},
'4' => {
unset = false;
n * 10 + 4
},
'5' => {
unset = false;
n * 10 + 5
},
'6' => {
unset = false;
n * 10 + 6
},
'7' => {
unset = false;
n * 10 + 7
},
'8' => {
unset = false;
n * 10 + 8
},
'9' => {
unset = false;
n * 10 + 9
},
_ => {
match self.cursor().mode {
Mode::Primitive(_) => {
key = Key::Char(c);
},
Mode::Command(_) => {
n = match c {
'0' => {
unset = false;
n * 10
},
'1' => {
unset = false;
n * 10 + 1
},
'2' => {
unset = false;
n * 10 + 2
},
'3' => {
unset = false;
n * 10 + 3
},
'4' => {
unset = false;
n * 10 + 4
},
'5' => {
unset = false;
n * 10 + 5
},
'6' => {
unset = false;
n * 10 + 6
},
'7' => {
unset = false;
n * 10 + 7
},
'8' => {
unset = false;
n * 10 + 8
},
'9' => {
unset = false;
n * 10 + 9
},
_ => {
key = Key::Char(c);
n
}
};
}
key = Key::Char(c);
n
}
};
}
},
}
}
}
if key != Key::Null {
return Inst(if unset { Parameter::Null } else { Parameter::Int(n) }, {
Cmd {
key: key,
ctrl: ctrl,
alt: alt,
shift: shift,
}
});
}
......
......@@ -7,6 +7,9 @@ pub use self::editor::*;
mod parse;
pub use self::parse::*;
mod key_state;
pub use self::key_state::*;
mod key;
pub use self::key::*;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment