From 0042ffe8ee737c89b66a566b4bf71b78b0836249 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 12 Jan 2024 14:15:52 +0100 Subject: feat: allow backspace and delete to span multiple lines in insert mode --- lib/editor.ml | 8 +++++--- lib/editorBuffer.ml | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/editor.ml b/lib/editor.ml index 9078a09..2e031e1 100644 --- a/lib/editor.ml +++ b/lib/editor.ml @@ -186,13 +186,15 @@ let handle_insert_command = let open Command in let open Action in function - | Simple Backspace -> Buffer.Action.delete_before ~n:1 |> on_focused_buffer | Simple Arrow_down -> Buffer.Action.move_down |> on_focused_buffer | Simple Arrow_left -> Buffer.Action.move_left |> on_focused_buffer | Simple Arrow_right -> Buffer.Action.move_right |> on_focused_buffer | Simple Arrow_up -> Buffer.Action.move_up |> on_focused_buffer + | Simple Backspace -> + Buffer.Action.delete_before ~cross_lines:true ~n:1 |> on_focused_buffer | Simple (Ctrl 'Q') -> quit 0 - | Simple Delete -> Buffer.Action.delete_after ~n:1 |> on_focused_buffer + | Simple Delete -> + Buffer.Action.delete_after ~cross_lines:true ~n:1 |> on_focused_buffer | Simple Enter -> Buffer.Action.newline |> on_focused_buffer | Simple Esc -> (Buffer.Action.move_left |> on_focused_buffer) *> set_mode Normal @@ -223,7 +225,7 @@ let handle_normal_command = (* | Key 's' -> *) (* (Buffer.Action.delete_after |> on_focused_buffer_or_new) *) (* *> set_mode Insert *) - (* | Key 'x' -> Buffer.Action.delete_after |> on_focused_buffer_or_new *) + (* | 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) -> diff --git a/lib/editorBuffer.ml b/lib/editorBuffer.ml index 2e2b4b6..b682116 100644 --- a/lib/editorBuffer.ml +++ b/lib/editorBuffer.ml @@ -29,8 +29,27 @@ 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 delete_after ~n = Fn.apply_n_times ~n (map_focus pop_after) |> on_content - let delete_before ~n = Fn.apply_n_times ~n (map_focus pop) |> on_content + + 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 + Fn.apply_n_times ~n aux |> on_content + + let delete_before ?(cross_lines = false) ~n = + let aux z = + let line = focus_or ~default:Zipper.empty z in + if cross_lines && is_far_left line && not (is_far_left z) then + pop_after z |> left |> map_focus (far_right &> Fn.flip join line) + else map_focus pop z + in + Fn.apply_n_times ~n aux |> on_content + 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 -- cgit v1.2.3