Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to allow Windows backspace keybindings #422

Closed
wants to merge 12 commits into from
6 changes: 6 additions & 0 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum KeyPress {
ControlRight,
/// Ctrl-↑
ControlUp,
/// Control Backspace (may not be able to be used on unix)
ControlBackspace,
/// Ctrl-char
Ctrl(char),
/// ⌦
Expand All @@ -46,6 +48,8 @@ pub enum KeyPress {
Left,
/// Escape-char or Alt-char
Meta(char),
/// Alt Backspace
MetaBackspace,
/// `KeyPress::Char('\0')`
Null,
/// ⇟
Expand All @@ -54,6 +58,8 @@ pub enum KeyPress {
PageUp,
/// → arrow key
Right,
/// Shift Backspace
ShiftBackspace,
/// Shift-↓
ShiftDown,
/// Shift-←
Expand Down
19 changes: 17 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::path::Path;
use std::result;
use std::sync::{Arc, Mutex, RwLock};

use log::debug;
use log::{warn, debug};
use unicode_width::UnicodeWidthStr;

use crate::tty::{RawMode, Renderer, Term, Terminal};
Expand Down Expand Up @@ -888,7 +888,22 @@ impl<H: Helper> Editor<H> {
/// Bind a sequence to a command.
pub fn bind_sequence(&mut self, key_seq: KeyPress, cmd: Cmd) -> Option<Cmd> {
if let Ok(mut bindings) = self.custom_bindings.write() {
bindings.insert(key_seq, cmd)
let key = if let KeyPress::Char(ref c) = key_seq {
if c.is_control() {
keys::char_to_key_press(*c)
} else {
key_seq
}
} else if let KeyPress::Ctrl(ref c) = key_seq {
if c.is_control() {
keys::char_to_key_press(*c);
fdncred marked this conversation as resolved.
Show resolved Hide resolved
warn!(target: "rustyline", "KeyPress::Ctrl({:?}) may not work on unix", c)
fdncred marked this conversation as resolved.
Show resolved Hide resolved
}
key_seq
} else {
key_seq
};
bindings.insert(key, cmd)
} else {
None
}
Expand Down
11 changes: 9 additions & 2 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,21 @@ impl RawReader for ConsoleRawReader {
return Err(error::ReadlineError::Eof);
};
let c = rc?;
let mut key = keys::char_to_key_press(c);
if meta {
return Ok(KeyPress::Meta(c));
if key == KeyPress::Backspace && meta {
fdncred marked this conversation as resolved.
Show resolved Hide resolved
key = KeyPress::MetaBackspace;
} else {
key = KeyPress::Meta(c);
}
return Ok(key);
} else {
let mut key = keys::char_to_key_press(c);
if key == KeyPress::Tab && shift {
key = KeyPress::BackTab;
} else if key == KeyPress::Char(' ') && ctrl {
key = KeyPress::Ctrl(' ');
} else if key == KeyPress::Backspace && ctrl {
key = KeyPress::ControlBackspace;
fdncred marked this conversation as resolved.
Show resolved Hide resolved
}
return Ok(key);
}
Expand Down