From f46d6661a8f33730c17cceb7e2885e789d6123d8 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sun, 28 Jan 2024 01:30:55 +0100 Subject: feat: add goto (G key) command --- lib/command.ml | 3 +++ lib/editor.ml | 6 +++++- lib/editorBuffer.ml | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/command.ml b/lib/command.ml index ac1dbf1..2d7b51b 100644 --- a/lib/command.ml +++ b/lib/command.ml @@ -16,6 +16,7 @@ type operation = | Erase_after | Search | Search_rev + | Goto type scope = Line | To_bol | To_eol | Down | Left | Right | Up @@ -75,6 +76,7 @@ let instant_operation = [ Key 'C'; Key 'D'; + Key 'G'; Key 'Y'; Key 'J'; Key 'n'; @@ -92,6 +94,7 @@ let to_op = function | Key 'c' | Key 'C' -> Change | Key 'd' | Key 'D' -> Delete | Key 'y' | Key 'Y' -> Yank + | Key 'G' -> Goto | Key 'n' -> Search | Key 'N' -> Search_rev | Key 'p' -> Paste_after diff --git a/lib/editor.ml b/lib/editor.ml index 5a68e53..ea2e68a 100644 --- a/lib/editor.ml +++ b/lib/editor.ml @@ -244,7 +244,7 @@ module Action = struct | None -> let word = word |> Sequence.to_list |> String.of_list in set_message (Printf.sprintf "Pattern not found: %s" word) - | Some (r, c) -> Buffer.Action.goto r c |> on_focused_buffer + | Some (r, c) -> Buffer.Action.goto ~r ~c |> on_focused_buffer (* Debug *) let get_rendered e = (e.rendered, e) @@ -331,6 +331,10 @@ let handle_normal_command c = | Simple (Ctrl 'U') -> let* r, _ = get_terminal_size in Buffer.Action.move_up ~n:(r / 2) |> on_focused_buffer + | Shortcut (_, n, Goto) -> ( + match n with + | None -> Buffer.Action.eof |> on_focused_buffer + | Some n -> Buffer.Action.goto ~r:n |> on_focused_buffer) (* Yank *) | Shortcut (r, n, Yank) -> let n = Option.value ~default:1 n - 1 in diff --git a/lib/editorBuffer.ml b/lib/editorBuffer.ml index 1f363ef..959c04a 100644 --- a/lib/editorBuffer.ml +++ b/lib/editorBuffer.ml @@ -145,7 +145,7 @@ module Action = struct in (horizontal left, horizontal right) - let goto r c = + let goto ~r ?(c = 0) = let change_content = Zipper.(goto r &> map_focus (goto c)) |> on_content and change_rendered = Zipper.goto r |> on_rendered in change_content *> change_rendered -- cgit v1.2.3