From 123ba3b46acd16d420b82d5a25c5a4c50135afe6 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 12 Jan 2024 19:00:24 +0100 Subject: feat: add support for "instant" normal commands (like [J]oin) --- lib/editor.ml | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'lib/editor.ml') diff --git a/lib/editor.ml b/lib/editor.ml index 2e031e1..adc9994 100644 --- a/lib/editor.ml +++ b/lib/editor.ml @@ -228,37 +228,68 @@ let handle_normal_command = (* | Key 'x' | Delete -> Buffer.Action.delete_after |> on_focused_buffer_or_new *) (* | Key 'X' -> Buffer.Action.delete_before |> on_focused_buffer_or_new *) (* | Key '$' -> Buffer.Action.eol |> on_focused_buffer_or_new *) - | Shortcut (_, n, Change, Line) -> + (* Change *) + | Shortcut (_, n, Change) -> let n = Option.value ~default:1 n - 1 in + (Buffer.Action.( + delete_to_eol &> move_down &> delete_lines ~n &> move_up &> eol) + |> on_focused_buffer_or_new) + *> set_mode Insert + | Chord (_, n1, Change, n2, Line) -> + let n = Option.((value ~default:1 n1 * value ~default:1 n2) - 1) in + (Buffer.Action.(delete_lines ~n &> bol &> delete_to_eol) + |> on_focused_buffer_or_new) + *> set_mode Insert + | Chord (_, n1, Change, n2, Down) -> + let n = Option.(value ~default:1 n1 * value ~default:1 n2) in (Buffer.Action.(delete_lines ~n &> bol &> delete_to_eol) |> on_focused_buffer_or_new) *> set_mode Insert - | Shortcut (_, _, Change, To_eol) -> - (Buffer.Action.delete_to_eol |> on_focused_buffer_or_new) + | Chord (_, n1, Change, n2, Left) -> + let n = Option.(value ~default:1 n1 * value ~default:1 n2) in + (Buffer.Action.delete_before ~n |> on_focused_buffer_or_new) *> set_mode Insert - | Shortcut (_, _, Change, To_bol) -> - (Buffer.Action.delete_to_bol |> on_focused_buffer_or_new) + | Chord (_, n1, Change, n2, Right) -> + let n = Option.(value ~default:1 n1 * value ~default:1 n2) in + (Buffer.Action.delete_after ~n |> on_focused_buffer_or_new) + *> set_mode Insert + | Chord (_, n1, Change, n2, Up) -> + let n = Option.(value ~default:1 n1 * value ~default:1 n2) in + (Buffer.Action.(bol &> delete_to_eol &> delete_lines_before ~n) + |> on_focused_buffer_or_new) *> set_mode Insert (* Delete *) + | Shortcut (_, n, Delete) -> + let n = Option.value ~default:1 n - 1 in + Buffer.Action.( + delete_to_eol &> move_down &> delete_lines ~n &> move_up &> eol) + |> on_focused_buffer_or_new + | Chord (_, n1, Delete, n2, Line) -> + let n = Option.(value ~default:1 n1 * value ~default:1 n2) in + Buffer.Action.delete_lines ~n |> on_focused_buffer_or_new | Chord (_, n1, Delete, n2, Down) -> let n = Option.((value ~default:1 n1 * value ~default:1 n2) + 1) in Buffer.Action.delete_lines ~n |> on_focused_buffer_or_new | Chord (_, n1, Delete, n2, Left) -> let n = Option.(value ~default:1 n1 * value ~default:1 n2) in Buffer.Action.delete_before ~n |> on_focused_buffer_or_new + | Shortcut (_, n, Erase_before) -> + let n = Option.(value ~default:1 n) in + Buffer.Action.delete_before ~n |> on_focused_buffer_or_new | Chord (_, n1, Delete, n2, Right) -> let n = Option.(value ~default:1 n1 * value ~default:1 n2) in Buffer.Action.delete_after ~n |> on_focused_buffer_or_new + | Shortcut (_, n, Erase_after) -> + let n = Option.(value ~default:1 n) in + Buffer.Action.delete_after ~n |> on_focused_buffer_or_new | Chord (_, n1, Delete, n2, Up) -> - let n = Option.((value ~default:1 n1 * value ~default:1 n2) + 1) in - Buffer.Action.delete_lines_before ~n |> on_focused_buffer_or_new - | Shortcut (_, n, Delete, Line) -> - Buffer.Action.delete_lines ~n:Option.(value ~default:1 n) + let n = Option.(value ~default:1 n1 * value ~default:1 n2) in + Buffer.Action.(delete_lines ~n:1 &> delete_lines_before ~n) |> on_focused_buffer_or_new - | Shortcut (_, _, Delete, To_eol) -> - Buffer.Action.delete_to_eol |> on_focused_buffer_or_new - | Shortcut (_, _, Delete, To_bol) -> - Buffer.Action.delete_to_bol |> on_focused_buffer_or_new + (* Join *) + | Shortcut (_, n, Join) -> + let n = Option.(value ~default:1 n) in + Buffer.Action.join_lines ~n |> on_focused_buffer_or_new | _ -> noop let handle_next_command m e = -- cgit v1.2.3