on master help sign in
Implement no-yank delete/change (#1099)
commit72f606ee19a20eca7901ac1647e8efa58ea4f8b8 authorath3 <45574139+ath3@users.noreply.github.com> dateWed 24 Nov 2021, 08:46 (5 years ago) parent57c14d4
3 files changed+81−59
book/src/keymap.md+37−35 helix-term/src/commands.rs+42−22 helix-term/src/keymap.rs+2−2
book/src/keymap.md+37−35
@@ -45,44 +45,46 @@
4545
4646### Changes
4747
48−| Key | Description | Command |
49−| ----- | ----------- | ------- |
50−| `r` | Replace with a character | `replace` |
51−| `R` | Replace with yanked text | `replace_with_yanked` |
52−| `~` | Switch case of the selected text | `switch_case` |
53−| `` ` `` | Set the selected text to lower case | `switch_to_lowercase` |
54−| `` Alt-` `` | Set the selected text to upper case | `switch_to_uppercase` |
55−| `i` | Insert before selection | `insert_mode` |
56−| `a` | Insert after selection (append) | `append_mode` |
57−| `I` | Insert at the start of the line | `prepend_to_line` |
58−| `A` | Insert at the end of the line | `append_to_line` |
59−| `o` | Open new line below selection | `open_below` |
60−| `O` | Open new line above selection | `open_above` |
61−| `.` | Repeat last change | N/A |
62−| `u` | Undo change | `undo` |
63−| `U` | Redo change | `redo` |
64−| `Alt-u` | Move backward in history | `earlier` |
65−| `Alt-U` | Move forward in history | `later` |
66−| `y` | Yank selection | `yank` |
67−| `p` | Paste after selection | `paste_after` |
68−| `P` | Paste before selection | `paste_before` |
69−| `"` `<reg>` | Select a register to yank to or paste from | `select_register` |
70−| `>` | Indent selection | `indent` |
71−| `<` | Unindent selection | `unindent` |
72−| `=` | Format selection (**LSP**) | `format_selections` |
73−| `d` | Delete selection | `delete_selection` |
74−| `c` | Change selection (delete and enter insert mode) | `change_selection` |
75−| `Ctrl-a` | Increment object (number) under cursor | `increment` |
76−| `Ctrl-x` | Decrement object (number) under cursor | `decrement` |
48+| Key | Description | Command |
49+| ----- | ----------- | ------- |
50+| `r` | Replace with a character | `replace` |
51+| `R` | Replace with yanked text | `replace_with_yanked` |
52+| `~` | Switch case of the selected text | `switch_case` |
53+| `` ` `` | Set the selected text to lower case | `switch_to_lowercase` |
54+| `` Alt-` `` | Set the selected text to upper case | `switch_to_uppercase` |
55+| `i` | Insert before selection | `insert_mode` |
56+| `a` | Insert after selection (append) | `append_mode` |
57+| `I` | Insert at the start of the line | `prepend_to_line` |
58+| `A` | Insert at the end of the line | `append_to_line` |
59+| `o` | Open new line below selection | `open_below` |
60+| `O` | Open new line above selection | `open_above` |
61+| `.` | Repeat last change | N/A |
62+| `u` | Undo change | `undo` |
63+| `U` | Redo change | `redo` |
64+| `Alt-u` | Move backward in history | `earlier` |
65+| `Alt-U` | Move forward in history | `later` |
66+| `y` | Yank selection | `yank` |
67+| `p` | Paste after selection | `paste_after` |
68+| `P` | Paste before selection | `paste_before` |
69+| `"` `<reg>` | Select a register to yank to or paste from | `select_register` |
70+| `>` | Indent selection | `indent` |
71+| `<` | Unindent selection | `unindent` |
72+| `=` | Format selection (**LSP**) | `format_selections` |
73+| `d` | Delete selection | `delete_selection` |
74+| `Alt-d` | Delete selection, without yanking | `delete_selection_noyank` |
75+| `c` | Change selection (delete and enter insert mode) | `change_selection` |
76+| `Alt-c` | Change selection (delete and enter insert mode, without yanking) | `change_selection_noyank` |
77+| `Ctrl-a` | Increment object (number) under cursor | `increment` |
78+| `Ctrl-x` | Decrement object (number) under cursor | `decrement` |
7779
7880#### Shell
7981
80−| Key | Description | Command |
81−| ------ | ----------- | ------- |
82−| <code>&#124;</code> | Pipe each selection through shell command, replacing with output | `shell_pipe` |
83−| <code>A-&#124;</code> | Pipe each selection into shell command, ignoring output | `shell_pipe_to` |
84−| `!` | Run shell command, inserting output before each selection | `shell_insert_output` |
85−| `A-!` | Run shell command, appending output after each selection | `shell_append_output` |
82+| Key | Description | Command |
83+| ------ | ----------- | ------- |
84+| <code>&#124;</code> | Pipe each selection through shell command, replacing with output | `shell_pipe` |
85+| <code>Alt-&#124;</code> | Pipe each selection into shell command, ignoring output | `shell_pipe_to` |
86+| `!` | Run shell command, inserting output before each selection | `shell_insert_output` |
87+| `Alt-!` | Run shell command, appending output after each selection | `shell_append_output` |
8688
8789
8890### Selection manipulation
helix-term/src/commands.rs+42−22
@@ -9,7 +9,6 @@ use helix_core::{
99 numbers::NumberIncrementor,
1010 object, pos_at_coords,
1111 regex::{self, Regex, RegexBuilder},
12− register::Register,
1312 search, selection, surround, textobject,
1413 unicode::width::UnicodeWidthChar,
1514 LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, Selection, SmallVec, Tendril,
@@ -232,7 +231,9 @@ impl Command {
232231 extend_line, "Select current line, if already selected, extend to next line",
233232 extend_to_line_bounds, "Extend selection to line bounds (line-wise selection)",
234233 delete_selection, "Delete selection",
234+ delete_selection_noyank, "Delete selection, without yanking",
235235 change_selection, "Change selection (delete and enter insert mode)",
236+ change_selection_noyank, "Change selection (delete and enter insert mode, without yanking)",
236237 collapse_selection, "Collapse selection onto a single cursor",
237238 flip_selections, "Flip selection cursor and anchor",
238239 insert_mode, "Insert before selection",
@@ -1693,19 +1694,42 @@ fn extend_to_line_bounds(cx: &mut Context) {
16931694 );
16941695}
16951696
1696−fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId) {
1697+enum Operation {
1698+ Delete,
1699+ Change,
1700+}
1701+
1702+fn delete_selection_impl(cx: &mut Context, op: Operation) {
1703+ let (view, doc) = current!(cx.editor);
1704+
16971705 let text = doc.text().slice(..);
1698− let selection = doc.selection(view_id);
1706+ let selection = doc.selection(view.id);
16991707
1700− // first yank the selection
1701− let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
1702− reg.write(values);
1708+ if cx.register != Some('_') {
1709+ // first yank the selection
1710+ let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
1711+ let reg_name = cx.register.unwrap_or('"');
1712+ let registers = &mut cx.editor.registers;
1713+ let reg = registers.get_mut(reg_name);
1714+ reg.write(values);
1715+ };
17031716
17041717 // then delete
17051718 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
17061719 (range.from(), range.to(), None)
17071720 });
1708− doc.apply(&transaction, view_id);
1721+ doc.apply(&transaction, view.id);
1722+
1723+ match op {
1724+ Operation::Delete => {
1725+ doc.append_changes_to_history(view.id);
1726+ // exit select mode, if currently in select mode
1727+ exit_select_mode(cx);
1728+ }
1729+ Operation::Change => {
1730+ enter_insert_mode(doc);
1731+ }
1732+ }
17091733}
17101734
17111735#[inline]
@@ -1720,25 +1744,21 @@ fn delete_selection_insert_mode(doc: &mut Document, view: &View, selection: &Sel
17201744}
17211745
17221746fn delete_selection(cx: &mut Context) {
1723− let reg_name = cx.register.unwrap_or('"');
1724− let (view, doc) = current!(cx.editor);
1725− let registers = &mut cx.editor.registers;
1726− let reg = registers.get_mut(reg_name);
1727− delete_selection_impl(reg, doc, view.id);
1728−
1729− doc.append_changes_to_history(view.id);
1747+ delete_selection_impl(cx, Operation::Delete);
1748+}
17301749
1731− // exit select mode, if currently in select mode
1732− exit_select_mode(cx);
1750+fn delete_selection_noyank(cx: &mut Context) {
1751+ cx.register = Some('_');
1752+ delete_selection_impl(cx, Operation::Delete);
17331753}
17341754
17351755fn change_selection(cx: &mut Context) {
1736− let reg_name = cx.register.unwrap_or('"');
1737− let (view, doc) = current!(cx.editor);
1738− let registers = &mut cx.editor.registers;
1739− let reg = registers.get_mut(reg_name);
1740− delete_selection_impl(reg, doc, view.id);
1741− enter_insert_mode(doc);
1756+ delete_selection_impl(cx, Operation::Change);
1757+}
1758+
1759+fn change_selection_noyank(cx: &mut Context) {
1760+ cx.register = Some('_');
1761+ delete_selection_impl(cx, Operation::Change);
17421762}
17431763
17441764fn collapse_selection(cx: &mut Context) {
helix-term/src/keymap.rs+2−2
@@ -537,9 +537,9 @@ impl Default for Keymaps {
537537 "O" => open_above,
538538
539539 "d" => delete_selection,
540− // TODO: also delete without yanking
540+ "A-d" => delete_selection_noyank,
541541 "c" => change_selection,
542− // TODO: also change delete without yanking
542+ "A-c" => change_selection_noyank,
543543
544544 "C" => copy_selection_on_next_line,
545545 "A-C" => copy_selection_on_prev_line,