neovim-gtk/src/input.rs

100 lines
2.5 KiB
Rust
Raw Normal View History

2016-04-04 10:14:57 +00:00
use gtk::prelude::*;
2016-04-04 10:14:57 +00:00
use gdk;
use gdk::EventKey;
use phf;
use neovim_lib::{Neovim, NeovimApi};
2016-04-04 10:14:57 +00:00
include!(concat!(env!("OUT_DIR"), "/key_map_table.rs"));
2017-04-02 19:09:03 +00:00
pub fn keyval_to_input_string(in_str: &str, in_state: gdk::ModifierType) -> String {
2017-03-31 21:12:00 +00:00
let mut val = in_str;
2017-04-02 19:09:03 +00:00
let mut state = in_state;
let mut input = String::new();
2017-04-03 11:17:06 +00:00
debug!("keyval -> {}", in_str);
2017-03-31 21:12:00 +00:00
// CTRL-^ and CTRL-@ don't work in the normal way.
2017-12-31 09:47:50 +00:00
if state.contains(gdk::ModifierType::CONTROL_MASK) && !state.contains(gdk::ModifierType::SHIFT_MASK) &&
!state.contains(gdk::ModifierType::MOD1_MASK)
2017-12-03 17:50:00 +00:00
{
2017-03-31 21:12:00 +00:00
if val == "6" {
val = "^";
} else if val == "2" {
val = "@";
}
}
2017-04-02 19:09:03 +00:00
let chars: Vec<char> = in_str.chars().collect();
if chars.len() == 1 {
let ch = chars[0];
// Remove SHIFT
if ch.is_ascii() && !ch.is_alphanumeric() {
2017-12-31 09:47:50 +00:00
state.remove(gdk::ModifierType::SHIFT_MASK);
2017-03-07 14:12:22 +00:00
}
2016-04-04 10:14:57 +00:00
}
2017-04-02 19:09:03 +00:00
if val == "<" {
val = "lt";
}
2017-12-31 09:47:50 +00:00
if state.contains(gdk::ModifierType::SHIFT_MASK) {
2017-04-02 19:09:03 +00:00
input.push_str("S-");
}
2017-12-31 09:47:50 +00:00
if state.contains(gdk::ModifierType::CONTROL_MASK) {
2016-04-04 10:14:57 +00:00
input.push_str("C-");
}
2017-12-31 09:47:50 +00:00
if state.contains(gdk::ModifierType::MOD1_MASK) {
2016-04-04 10:14:57 +00:00
input.push_str("A-");
}
2016-04-04 10:14:57 +00:00
input.push_str(val);
2017-03-12 15:11:43 +00:00
if input.chars().count() > 1 {
format!("<{}>", input)
} else {
input
}
2016-04-04 10:14:57 +00:00
}
pub fn convert_key(ev: &EventKey) -> Option<String> {
let keyval = ev.get_keyval();
let state = ev.get_state();
if let Some(ref keyval_name) = gdk::keyval_name(keyval) {
if let Some(cnvt) = KEYVAL_MAP.get(keyval_name as &str).cloned() {
return Some(keyval_to_input_string(cnvt, state));
}
}
2016-05-03 10:34:36 +00:00
2016-04-04 10:14:57 +00:00
if let Some(ch) = gdk::keyval_to_unicode(keyval) {
2017-04-03 12:54:36 +00:00
Some(keyval_to_input_string(&ch.to_string(), state))
2016-05-03 10:34:36 +00:00
} else {
None
2016-04-04 10:14:57 +00:00
}
}
pub fn im_input(nvim: &mut Neovim, input: &str) {
debug!("nvim_input -> {}", input);
2017-12-03 14:33:34 +00:00
2017-12-03 17:50:00 +00:00
let input: String = input
.chars()
.map(|ch| {
keyval_to_input_string(&ch.to_string(), gdk::ModifierType::empty())
})
.collect();
2017-12-03 14:33:34 +00:00
nvim.input(&input).expect("Error run input command to nvim");
}
pub fn gtk_key_press(nvim: &mut Neovim, ev: &EventKey) -> Inhibit {
if let Some(input) = convert_key(ev) {
debug!("nvim_input -> {}", input);
nvim.input(&input).expect("Error run input command to nvim");
Inhibit(true)
} else {
Inhibit(false)
}
}