Skip to content

Commit

Permalink
Add support for Unicode input
Browse files Browse the repository at this point in the history
note: a better approach is here https://github.com/quantonganh/snippets-ls
ref: helix-editor/helix#1438
ref: helix-editor/helix#2852

Co-authored by: Linden Krouse <[email protected]>
  • Loading branch information
omentic committed Nov 1, 2023
1 parent f98a221 commit 92a4f5f
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 0 deletions.
12 changes: 12 additions & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,15 @@ Sets explorer side width and style.
|------------|-------------|---------|
| `enable` | If set to true, then when the cursor is in a position with non-whitespace to its left, instead of inserting a tab, it will run `move_parent_node_end`. If there is only whitespace to the left, then it inserts a tab as normal. With the default bindings, to explicitly insert a tab character, press Shift-tab. | `true` |
| `supersede-menu` | Normally, when a menu is on screen, such as when auto complete is triggered, the tab key is bound to cycling through the items. This means when menus are on screen, one cannot use the tab key to trigger the `smart-tab` command. If this option is set to true, the `smart-tab` command always takes precedence, which means one cannot use the tab key to cycle through menu items. One of the other bindings must be used instead, such as arrow keys or `C-n`/`C-p`. | `false` |

### `[editor.digraphs]` Section

By default, special characters can be input using the `insert_digraphs` command, bound to `\` in normal 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 = "¯\\_(ツ)_/¯"
```
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Normal mode is the default mode when you launch helix. Return to it from other m
| `a` | Insert after selection (append) | `append_mode` |
| `I` | Insert at the start of the line | `insert_at_line_start` |
| `A` | Insert at the end of the line | `insert_at_line_end` |
| `\` | Insert digraphs | `insert_digraph` |
| `o` | Open new line below selection | `open_below` |
| `O` | Open new line above selection | `open_above` |
| `.` | Repeat last insert | N/A |
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 @@ -494,6 +494,7 @@ impl MappableCommand {
command_palette, "Open command palette",
open_or_focus_explorer, "Open or focus explorer",
reveal_current_file, "Reveal current file in explorer",
insert_digraph, "Insert Unicode characters with prompt",
);
}

Expand Down Expand Up @@ -6020,3 +6021,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);
},
)
}
2 changes: 2 additions & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"N" => search_prev,
"*" => search_selection,

"\\" => insert_digraph,

"u" => undo,
"U" => redo,
"A-u" => earlier,
Expand Down
Loading

0 comments on commit 92a4f5f

Please sign in to comment.