diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index 5b6b90e..a22f821 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -89,7 +89,7 @@ impl backend::Backend for Concrete { // Is it a UTF-8 starting point? if 32 <= ch && ch <= 255 && ch != 127 { - Event::Char(utf8::read_char(ch as u8, || ncurses::getch() as u8) + Event::Char(utf8::read_char(ch as u8, || Some(ncurses::getch() as u8)) .unwrap()) } else { parse_ncurses_char(ch) diff --git a/src/backend/curses/pan.rs b/src/backend/curses/pan.rs index eb01eec..68c0c30 100644 --- a/src/backend/curses/pan.rs +++ b/src/backend/curses/pan.rs @@ -5,6 +5,7 @@ use event::{Event, Key}; use self::super::find_closest; use theme::{Color, ColorStyle, Effect}; +use utf8; pub struct Concrete { window: pancurses::Window, @@ -89,8 +90,22 @@ impl backend::Backend for Concrete { // TODO: wait for a very short delay. If more keys are // pipelined, it may be an escape sequence. pancurses::Input::Character('\u{1b}') => Event::Key(Key::Esc), - pancurses::Input::Character('\u{7f}') => Event::Key(Key::Backspace), - pancurses::Input::Character(c) => Event::Char(c), + pancurses::Input::Character('\u{7f}') => { + Event::Key(Key::Backspace) + } + pancurses::Input::Character(c) if 32 <= (c as u32) && + (c as u32) <= 255 => { + Event::Char(utf8::read_char(c as u8, || { + self.window.getch().and_then(|i| match i { + pancurses::Input::Character(c) => { + Some(c as u8) + } + _ => None, + }) + }) + .unwrap()) + } + pancurses::Input::Character(c) => Event::Unknown(c as i32), // TODO: Some key combos are not recognized by pancurses, // but are sent as Unknown. We could still parse them here. pancurses::Input::Unknown(i) => Event::Unknown(i), diff --git a/src/utf8.rs b/src/utf8.rs index 8141281..7bcfdca 100644 --- a/src/utf8.rs +++ b/src/utf8.rs @@ -7,7 +7,7 @@ use std::char::from_u32; /// /// Returns an error if the stream is invalid utf-8. pub fn read_char(first: u8, next: F) -> Result - where F: Fn() -> u8 + where F: Fn() -> Option { if first < 0x80 { return Ok(first as char); @@ -28,7 +28,7 @@ pub fn read_char(first: u8, next: F) -> Result // We already have one byte, now read the others. for _ in 1..n_bytes { - let byte = next(); + let byte = try!(next().ok_or("Missing UTF-8 byte".to_string())); if byte & 0xC0 != 0x80 { return Err(format!("Found non-continuation byte after leading: \ {}", diff --git a/src/views/edit_view.rs b/src/views/edit_view.rs index ed71bc9..4ac8cbd 100644 --- a/src/views/edit_view.rs +++ b/src/views/edit_view.rs @@ -352,11 +352,7 @@ impl View for EditView { fn on_event(&mut self, event: Event) -> EventResult { match event { - Event::Char(ch) => { - // Find the byte index of the char at self.cursor - - self.insert(ch); - } + Event::Char(ch) => self.insert(ch), // TODO: handle ctrl-key? Event::Key(Key::Home) => self.cursor = 0, Event::Key(Key::End) => self.cursor = self.content.len(),