Add key map converter table

This commit is contained in:
daa 2016-04-02 23:00:18 +03:00
parent 52ec77137d
commit dc1539d0d3
5 changed files with 78 additions and 1 deletions

41
Cargo.lock generated
View File

@ -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"

View File

@ -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/"

View File

@ -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();
}

View File

@ -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;

View File

@ -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)