forked from helix-editor/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement keyboard layout remap for all modes but NOR
Adds a config option to remap keys in a different keyboard layout into English so that keybindings work even when switched into a non-English keyboard layout. The corresponding config option is `editor.layout-remap` which is a list of dictionaries with two mandatory keys, `from` and `into`, which specify the translation mapping, e.g.: ```toml [[editor.layout-remap]] from = 'йцукенгшщзхъЙЦУКЕНГШЩЗХЪ' into = 'qwertyuiop[]QWERTYUIOP{}' ``` These can be repeated multiple times to facilitate specifying mappings containing different types of quotes and other special characters which may require escaping in TOML config. This circumvents helix-editor#133 in a way that Helix still does not recognise keypresses by their scan-codes but still allows for the non-English layout users to operate Helix without switching the layout which can be especially useful for writing documentation in their native language with Helix.
- Loading branch information
Ilya Novozhilov
committed
Dec 7, 2022
1 parent
c4d7cde
commit 537405b
Showing
6 changed files
with
224 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use helix_term::keymap::default; | ||
use helix_view::document::Mode; | ||
use helix_view::input::{KeyCode, KeyEvent}; | ||
use std::collections::HashMap; | ||
|
||
const LANGMAP: [(&'static str, &'static str); 6] = [ | ||
(r#"йцукенгшщзхъ"#, r#"qwertyuiop[]"#), | ||
(r#"ЙЦУКЕНГШЩЗХЪ"#, r#"QWERTYUIOP{}"#), | ||
(r#"фывапролджэё"#, r#"asdfghjkl;'\"#), | ||
(r#"ФЫВАПРОЛДЖЭЁ"#, r#"ASDFGHJKL:"|"#), | ||
(r#"]ячсмитьбю/"#, r#"`zxcvbnm,./"#), | ||
(r#"[ЯЧСМИТЬБЮ?"#, r#"~ZXCVBNM<>?"#), | ||
]; | ||
|
||
fn translate<F>(ev: &KeyEvent, f: F) -> Option<KeyEvent> | ||
where | ||
F: Fn(char) -> Option<char>, | ||
{ | ||
if let KeyCode::Char(c) = ev.code { | ||
Some(KeyEvent { | ||
code: KeyCode::Char(f(c)?), | ||
modifiers: ev.modifiers.clone(), | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut langmap = LANGMAP | ||
.iter() | ||
.map(|(ru, en)| ru.chars().zip(en.chars())) | ||
.flatten() | ||
.filter(|(en, ru)| en != ru) | ||
.map(|(ru, en)| (en, ru)) | ||
.collect::<HashMap<_, _>>(); | ||
|
||
langmap | ||
.iter() | ||
.filter_map(|(en, ru)| langmap.contains_key(ru).then(|| *en)) | ||
.collect::<Vec<_>>() | ||
.into_iter() | ||
.for_each(|c| { | ||
langmap.remove(&c); | ||
}); | ||
|
||
let keymaps = default::default(); | ||
for mode in [Mode::Normal, Mode::Select] { | ||
println!("[keys.{}]", mode); | ||
keymaps[&mode].traverse_map(|keys, name| { | ||
let tr_keys = keys | ||
.iter() | ||
.filter_map(|ev| translate(ev, |c| langmap.get(&c).map(|c| *c))) | ||
.enumerate() | ||
.map(|(i, ev)| { | ||
if i == 0 { | ||
ev.to_string() | ||
} else { | ||
format!("+{}", ev) | ||
} | ||
}) | ||
.collect::<String>(); | ||
if !tr_keys.is_empty() { | ||
println!(r#"{:?} = "{}""#, tr_keys, name); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters