diff --git a/Cargo.lock b/Cargo.lock index 9f85172..a0b67ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,8 @@ dependencies = [ "glib 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "gtk 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "neovim-lib 0.1.0", + "phf 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_codegen 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -233,11 +235,50 @@ dependencies = [ "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "phf" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_shared 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "phf_codegen" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_generator 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_shared 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "phf_generator" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_shared 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "phf_shared" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pkg-config" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rand" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rmp" version = "0.7.3" diff --git a/Cargo.toml b/Cargo.toml index ae44453..596eff8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,10 @@ glib = "0.0.8" gdk = "0.3.0" #neovim-lib = "0.1" rmp = "0.7" +phf = "0.7" + +[build-dependencies] +phf_codegen = "0.7" [dependencies.neovim-lib] path = "../neovim-lib/" diff --git a/build.rs b/build.rs index 699c1ca..49cf003 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,23 @@ +extern crate phf_codegen; + +use std::env; +use std::fs::File; +use std::io::{BufWriter, Write}; +use std::path::Path; + fn main() { if cfg!(target_os = "windows") { println!("cargo:rustc-link-search=native=C:\\msys64\\mingw64\\lib"); } + + let path = Path::new(&env::var("OUT_DIR").unwrap()).join("key_map_table.rs"); + let mut file = BufWriter::new(File::create(&path).unwrap()); + + write!(&mut file, "static KEYVAL_MAP: phf::Map<&'static str, &'static str> = ").unwrap(); + phf_codegen::Map::new() + .entry("Enter", "\"CR\"") + .entry("Return", "\"CR\"") + .build(&mut file) + .unwrap(); + write!(&mut file, ";\n").unwrap(); } diff --git a/src/main.rs b/src/main.rs index 9a630ef..8f82d74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate glib; extern crate cairo; extern crate neovim_lib; extern crate rmp; +extern crate phf; mod nvim; mod ui_model; diff --git a/src/ui.rs b/src/ui.rs index e245db6..4f43af8 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -96,6 +96,13 @@ impl Ui { } } +use phf; +include!(concat!(env!("OUT_DIR"), "/key_map_table.rs")); + +fn convert_keyval(input: &str) -> Option<&'static str> { + KEYVAL_MAP.get(input).cloned() +} + fn gtk_key_press(_: &Window, ev: &EventKey) -> Inhibit { let keyval = ev.get_keyval(); if let Some(keyval_name) = gdk::keyval_name(keyval) { @@ -106,7 +113,13 @@ fn gtk_key_press(_: &Window, ev: &EventKey) -> Inhibit { } else { keyval_name }; - ui.nvim().input(&input).expect("Error run input command to nvim"); + + let converted_input: &str = if let Some(cnvt) = convert_keyval(&input) { + cnvt + } else { + &input + }; + ui.nvim().input(converted_input).expect("Error run input command to nvim"); }); } Inhibit(true)