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/command.ml | 114 ++++++++++++++++++++++++++++++++-------------------- lib/editor.ml | 57 ++++++++++++++++++++------ lib/editorBuffer.ml | 16 +++++--- 3 files changed, 125 insertions(+), 62 deletions(-) diff --git a/lib/command.ml b/lib/command.ml index 7dedc6c..97c1fdf 100644 --- a/lib/command.ml +++ b/lib/command.ml @@ -3,19 +3,30 @@ open Key type register = char option type count = int option -type operation = Noop | Yank | Paste | Delete | Change + +type operation = + | Noop + | Yank + | Delete + | Change + | Join + | Paste_before + | Paste_after + | Erase_before + | Erase_after + type scope = Line | To_bol | To_eol | Down | Left | Right | Up type command = | Type of char | Simple of Key.t | Partial of Key.t - | Shortcut of register * count * operation * scope + | Shortcut of register * count * operation | Chord of register * count * operation * count * scope type t = command -let shortcut ?r ?n c s = Shortcut (r, n, c, s) +let shortcut ?r ?n c = Shortcut (r, n, c) let chord ?r ?n1 c ?n2 m = Chord (r, n1, c, n2, m) let i_stream = @@ -42,6 +53,11 @@ let simple_movements = Backspace; ] +let instant_operation = + [ Key 'C'; Key 'D'; Key 'J'; Key 'P'; Key 'X'; Key 'p'; Key 'x' ] + +let chord_operation = [ Key 'c'; Key 'd'; Key 'y' ] + let to_scope = function | Key 'j' | Arrow_down -> Down | Key 'h' | Arrow_left | Backspace -> Left @@ -52,61 +68,71 @@ let to_scope = function let n_stream = let step s k = let open Sequence.Step in - let is_chord_op c = String.contains "ydc" (Char.lowercase c) in + let is_chord_operation k = List.mem ~equal:Poly.equal chord_operation k in let is_simple_movement k = List.mem ~equal:Poly.equal simple_movements k in - let to_op c = - match Char.lowercase c with - | 'y' -> Yank - | 'p' -> Paste - | 'd' -> Delete - | 'c' -> Change + let is_instant_operation k = + List.mem ~equal:Poly.equal instant_operation k + in + let to_op = function + | Key 'J' -> Join + | Key 'c' | Key 'C' -> Change + | Key 'd' | Key 'D' -> Delete + | Key 'y' | Key 'Y' -> Yank + | Key 'p' -> Paste_after + | Key 'P' -> Paste_before + | Key 'x' -> Erase_after + | Key 'X' -> Erase_before | _ -> failwith "Invalid operation in chord." in match (s, k) with | `start, Key '"' -> Yield { value = Partial k; state = `chord_reg_pre } + (* Register *) | `chord_reg_pre, Key c -> Yield { value = Partial k; state = `chord_reg c } - | `chord_reg r, Key n when Char.('1' <= n && n <= '9') -> - let n = Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_n (Some r, n) } + (* Count (first) *) | `start, Key n when Char.('1' <= n && n <= '9') -> let n = Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_n (None, n) } - | `chord_n (r, m), Key n when Char.('0' <= n && n <= '9') -> + Yield { value = Partial k; state = `chord_fst_n (None, n) } + | `chord_reg r, Key n when Char.('1' <= n && n <= '9') -> + let n = Char.to_int n - 48 in + Yield { value = Partial k; state = `chord_fst_n (Some r, n) } + | `chord_fst_n (r, m), Key n when Char.('0' <= n && n <= '9') -> let n = (10 * m) + Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_n (r, n) } - | `start, Key c when is_chord_op c -> - if Char.is_uppercase c then - Yield { value = shortcut (to_op c) To_eol; state = `start } - else - Yield { value = Partial k; state = `chord_cmd (None, None, to_op c) } - | `chord_reg r, Key c when is_chord_op c -> - if Char.is_uppercase c then - Yield { value = shortcut ~r (to_op c) To_eol; state = `start } - else - Yield - { value = Partial k; state = `chord_cmd (Some r, None, to_op c) } - | `chord_n (r, n), Key c when is_chord_op c -> - if Char.is_uppercase c then - Yield { value = shortcut ?r ~n (to_op c) To_eol; state = `start } - else - Yield { value = Partial k; state = `chord_cmd (r, Some n, to_op c) } - | `chord_cmd (r, n, c), Key ch when is_chord_op ch && Poly.(c = to_op ch) -> - if Char.is_uppercase ch then - Yield { value = shortcut ?r ?n c To_bol; state = `start } - else Yield { value = shortcut ?r ?n c Line; state = `start } + Yield { value = Partial k; state = `chord_fst_n (r, n) } + (* Instant operations *) + | `start, k when is_instant_operation k -> + Yield { value = shortcut (to_op k); state = `start } + | `chord_reg r, k when is_instant_operation k -> + Yield { value = shortcut ~r (to_op k); state = `start } + | `chord_fst_n (r, n), k when is_instant_operation k -> + Yield { value = shortcut ?r ~n (to_op k); state = `start } + (* Chord operation (first) *) + | `start, k when is_chord_operation k -> + Yield { value = Partial k; state = `chord_cmd (None, None, to_op k) } + | `chord_reg r, k when is_chord_operation k -> + Yield { value = Partial k; state = `chord_cmd (Some r, None, to_op k) } + | `chord_fst_n (r, n), k when is_chord_operation k -> + Yield { value = Partial k; state = `chord_cmd (r, Some n, to_op k) } + (* Count (second) *) + | `chord_cmd (r, n1, c), Key n when Char.('1' <= n && n <= '9') -> + let n = Char.to_int n - 48 in + Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n) } + | `chord_snd_n (r, n1, c, n2), Key n when Char.('0' <= n && n <= '9') -> + let n2 = (10 * n2) + Char.to_int n - 48 in + Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n2) } + (* Chord operation (second) *) + | `chord_cmd (r, n, c), k when is_chord_operation k && Poly.(c = to_op k) -> + Yield { value = chord ?r ?n1:n c Line; state = `start } + | `chord_snd_n (r, n1, c, n2), k + when is_chord_operation k && Poly.(c = to_op k) -> + Yield { value = chord ?r ?n1 c ~n2 Line; state = `start } + (* Movement *) | (`start | `chord_reg _), k when is_simple_movement k -> Yield { value = chord Noop (to_scope k); state = `start } - | `chord_n (_, n), k when is_simple_movement k -> + | `chord_fst_n (_, n), k when is_simple_movement k -> Yield { value = chord ~n1:n Noop (to_scope k); state = `start } | `chord_cmd (r, n, c), k when is_simple_movement k -> Yield { value = chord ?r ?n1:n c (to_scope k); state = `start } - | `chord_cmd (r, n1, c), Key n when Char.('1' <= n && n <= '9') -> - let n = Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_mv_n (r, n1, c, n) } - | `chord_mv_n (r, n1, c, n2), Key n when Char.('0' <= n && n <= '9') -> - let n2 = (10 * n2) + Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_mv_n (r, n1, c, n2) } - | `chord_mv_n (r, n1, c, n2), k when is_simple_movement k -> + | `chord_snd_n (r, n1, c, n2), k when is_simple_movement k -> Yield { value = chord ?r ?n1 c ~n2 (to_scope k); state = `start } (* Catch-all rules *) | `start, _ -> Yield { value = Simple k; state = `start } 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 = diff --git a/lib/editorBuffer.ml b/lib/editorBuffer.ml index b682116..ea89fb9 100644 --- a/lib/editorBuffer.ml +++ b/lib/editorBuffer.ml @@ -29,13 +29,12 @@ module Action = struct let bof = far_left |> on_content let eof = far_right |> on_content let insert k = map_focus (push k) |> on_content + (* let replace k = () *) let delete_after ?(cross_lines = false) ~n = let aux z = let line = focus_or ~default:Zipper.empty z in if cross_lines && is_far_right line && not (is_far_right z) then - (* let next = right z |> focus_or ~default:Zipper.empty |> far_left in *) - (* right z |> pop_after |> left |> map_focus (Fn.flip join next) *) pop_after z |> map_focus_or ~default:line (far_left &> join line) else map_focus pop_after z in @@ -53,9 +52,7 @@ module Action = struct let delete_to_eol = map_focus (split &> fst) |> on_content let delete_to_bol = map_focus (split &> snd) |> on_content let delete_lines ~n = Fn.apply_n_times ~n pop_after |> on_content - - let delete_lines_before ~n = - on_content (fun z -> pop_after z |> Fn.apply_n_times ~n:(n - 1) pop_before) + let delete_lines_before ~n = Fn.apply_n_times ~n pop_before |> on_content let newline = let aux z = @@ -64,6 +61,15 @@ module Action = struct in on_content aux + let join_lines ~n = + let aux z = + if is_far_right z || is_far_right (right z) then z + else + let line = focus_or ~default:Zipper.empty z |> far_right in + pop_after z |> map_focus (far_left &> join line) + in + Fn.apply_n_times ~n aux |> on_content + (* let save_history_to ?(clear = true) r = () *) end -- cgit v1.2.3