on master help sign in
Add movement shortcut for history (#1088)
b7c3877Ivan Tham
book/src/keymap.md+2
@@ -61,6 +61,8 @@
61| `.` | Repeat last change | N/A |
61| `.` | Repeat last change | N/A |
62| `u` | Undo change | `undo` |
62| `u` | Undo change | `undo` |
63| `U` | Redo change | `redo` |
63| `U` | Redo change | `redo` |
64+| `Alt-u` | Move backward in history | `earlier` |
65+| `Alt-U` | Move forward in history | `later` |
64| `y` | Yank selection | `yank` |
66| `y` | Yank selection | `yank` |
65| `p` | Paste after selection | `paste_after` |
67| `p` | Paste after selection | `paste_after` |
66| `P` | Paste before selection | `paste_before` |
68| `P` | Paste before selection | `paste_before` |
helix-core/src/history.rs+1−1
@@ -282,7 +282,7 @@ impl History {
282}
282}
283
283
284/// Whether to undo by a number of edits or a duration of time.
284/// Whether to undo by a number of edits or a duration of time.
285−#[derive(Debug, PartialEq)]
285+#[derive(Debug, PartialEq, Clone, Copy)]
286pub enum UndoKind {
286pub enum UndoKind {
287 Steps(usize),
287 Steps(usize),
288 TimePeriod(std::time::Duration),
288 TimePeriod(std::time::Duration),
helix-term/src/commands.rs+43−17
@@ -1,5 +1,7 @@
1use helix_core::{
1use helix_core::{
2− comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent,
2+ comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes,
3+ history::UndoKind,
4+ indent,
3 indent::IndentStyle,
5 indent::IndentStyle,
4 line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending},
6 line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending},
5 match_brackets,
7 match_brackets,
@@ -284,6 +286,8 @@ impl Command {
284 delete_word_backward, "Delete previous word",
286 delete_word_backward, "Delete previous word",
285 undo, "Undo change",
287 undo, "Undo change",
286 redo, "Redo change",
288 redo, "Redo change",
289+ earlier, "Move backward in history",
290+ later, "Move forward in history",
287 yank, "Yank selection",
291 yank, "Yank selection",
288 yank_joined_to_clipboard, "Join and yank selections to clipboard",
292 yank_joined_to_clipboard, "Join and yank selections to clipboard",
289 yank_main_selection_to_clipboard, "Yank main selection to clipboard",
293 yank_main_selection_to_clipboard, "Yank main selection to clipboard",
@@ -1877,10 +1881,7 @@ mod cmd {
1877 args: &[&str],
1881 args: &[&str],
1878 _event: PromptEvent,
1882 _event: PromptEvent,
1879 ) -> anyhow::Result<()> {
1883 ) -> anyhow::Result<()> {
1880− let uk = args
1881− .join(" ")
1882− .parse::<helix_core::history::UndoKind>()
1883− .map_err(|s| anyhow!(s))?;
1884+ let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?;
1884
1885
1885 let (view, doc) = current!(cx.editor);
1886 let (view, doc) = current!(cx.editor);
1886 let success = doc.earlier(view.id, uk);
1887 let success = doc.earlier(view.id, uk);
@@ -1896,10 +1897,7 @@ mod cmd {
1896 args: &[&str],
1897 args: &[&str],
1897 _event: PromptEvent,
1898 _event: PromptEvent,
1898 ) -> anyhow::Result<()> {
1899 ) -> anyhow::Result<()> {
1899− let uk = args
1900− .join(" ")
1901− .parse::<helix_core::history::UndoKind>()
1902− .map_err(|s| anyhow!(s))?;
1900+ let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?;
1903 let (view, doc) = current!(cx.editor);
1901 let (view, doc) = current!(cx.editor);
1904 let success = doc.later(view.id, uk);
1902 let success = doc.later(view.id, uk);
1905 if !success {
1903 if !success {
@@ -3963,20 +3961,48 @@ pub mod insert {
3963// storing it?
3961// storing it?
3964
3962
3965fn undo(cx: &mut Context) {
3963fn undo(cx: &mut Context) {
3964+ let count = cx.count();
3966 let (view, doc) = current!(cx.editor);
3965 let (view, doc) = current!(cx.editor);
3967− let view_id = view.id;
3966+ for _ in 0..count {
3968− let success = doc.undo(view_id);
3967+ if !doc.undo(view.id) {
3969− if !success {
3970− cx.editor.set_status("Already at oldest change".to_owned());
3968+ cx.editor.set_status("Already at oldest change".to_owned());
3969+ break;
3970+ }
3971 }
3971 }
3972}
3972}
3973
3973
3974fn redo(cx: &mut Context) {
3974fn redo(cx: &mut Context) {
3975+ let count = cx.count();
3976+ let (view, doc) = current!(cx.editor);
3977+ for _ in 0..count {
3978+ if !doc.redo(view.id) {
3979+ cx.editor.set_status("Already at newest change".to_owned());
3980+ break;
3981+ }
3982+ }
3983+}
3984+
3985+fn earlier(cx: &mut Context) {
3986+ let count = cx.count();
3987+ let (view, doc) = current!(cx.editor);
3988+ for _ in 0..count {
3989+ // rather than doing in batch we do this so get error halfway
3990+ if !doc.earlier(view.id, UndoKind::Steps(1)) {
3991+ cx.editor.set_status("Already at oldest change".to_owned());
3992+ break;
3993+ }
3994+ }
3995+}
3996+
3997+fn later(cx: &mut Context) {
3998+ let count = cx.count();
3975 let (view, doc) = current!(cx.editor);
3999 let (view, doc) = current!(cx.editor);
3976− let view_id = view.id;
4000+ for _ in 0..count {
3977− let success = doc.redo(view_id);
4001+ // rather than doing in batch we do this so get error halfway
3978− if !success {
4002+ if !doc.later(view.id, UndoKind::Steps(1)) {
3979− cx.editor.set_status("Already at newest change".to_owned());
4003+ cx.editor.set_status("Already at newest change".to_owned());
4004+ break;
4005+ }
3980 }
4006 }
3981}
4007}
3982
4008
helix-term/src/keymap.rs+2
@@ -582,6 +582,8 @@ impl Default for Keymaps {
582
582
583 "u" => undo,
583 "u" => undo,
584 "U" => redo,
584 "U" => redo,
585+ "A-u" => earlier,
586+ "A-U" => later,
585
587
586 "y" => yank,
588 "y" => yank,
587 // yank_all
589 // yank_all