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

Add support for digraphs #2852

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Key Remapping](./remapping.md)
- [Hooks](./hooks.md)
- [Languages](./languages.md)
- [Digraphs](./digraphs.md)
- [Guides](./guides/README.md)
- [Adding Languages](./guides/adding_languages.md)
- [Adding Textobject Queries](./guides/textobject.md)
Expand Down
14 changes: 14 additions & 0 deletions book/src/digraphs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Digraphs

By default, special characters can be input using the `insert_digraphs` (or `C-K`) command while in insert mode. Custom digraphs can be added to the `editor.digraphs` section of the config:

```toml
[editor.digraphs]
ka = "か"
ku = { symbols = "く", description = "The japanese character Ku" }
shrug = "¯\\_(ツ)_/¯"
```

## Defaults

TBD
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ undo-able "save point" until you return to normal mode.
| `PageDown` | Move one page down | `page_down` |
| `Alt->` | Go to end of buffer | `goto_file_end` |
| `Alt-<` | Go to start of buffer | `goto_file_start` |
| `Ctrl-K` | Insert digraph | `insert_digraph` |

## Select / extend mode

Expand Down
48 changes: 48 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ impl MappableCommand {
record_macro, "Record macro",
replay_macro, "Replay macro",
command_palette, "Open command palette",
insert_digraph, "Insert unicode characters with prompt",
);
}

Expand Down Expand Up @@ -4687,3 +4688,50 @@ fn replay_macro(cx: &mut Context) {
cx.editor.macro_replaying.pop();
}));
}

fn insert_digraph(cx: &mut Context) {
ui::prompt(
cx,
"digraph:".into(),
Some('K'), //todo: decide on register to use
move |editor, input| {
editor
.config()
.digraphs
.search(input)
.take(10)
.map(|entry| {
// todo: Prompt does not currently allow additional text as part
// of it's suggestions. Show the user the symbol and description
// once prompt has been made more robust
#[allow(clippy::useless_format)]
((0..), Cow::from(format!("{}", entry.sequence)))
})
.collect()
},
move |cx, input, event| {
match event {
PromptEvent::Validate => (),
_ => return,
}
let config = cx.editor.config();
let symbols = if let Some(entry) = config.digraphs.get(input) {
&entry.symbols
} else {
cx.editor.set_error("Digraph not found");
return;
};

let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);
let mut changes = Vec::with_capacity(selection.len());

for range in selection.ranges() {
changes.push((range.from(), range.from(), Some(symbols.clone().into())));
}
let trans = Transaction::change(doc.text(), changes.into_iter());
doc.apply(&trans, view.id);
doc.append_changes_to_history(view.id);
},
)
}
2 changes: 2 additions & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ pub fn default() -> HashMap<Mode, Keymap> {

"C-x" => completion,
"C-r" => insert_register,

"C-K" => insert_digraph,
});
hashmap!(
Mode::Normal => Keymap::new(normal),
Expand Down
Loading