diff options
author | Federico Igne <undyamon@disroot.org> | 2024-01-12 14:15:52 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2024-01-12 14:15:52 +0100 |
commit | 0042ffe8ee737c89b66a566b4bf71b78b0836249 (patch) | |
tree | 3bb2fbdc3991bc6c8922751cdeb3cb4c0cf33814 | |
parent | 2f6150f7e549836df78d91d6cf42d4a75d310930 (diff) | |
download | sandy-0042ffe8ee737c89b66a566b4bf71b78b0836249.tar.gz sandy-0042ffe8ee737c89b66a566b4bf71b78b0836249.zip |
feat: allow backspace and delete to span multiple lines in insert mode
-rw-r--r-- | lib/editor.ml | 8 | ||||
-rw-r--r-- | lib/editorBuffer.ml | 23 |
2 files changed, 26 insertions, 5 deletions
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 = | |||
186 | let open Command in | 186 | let open Command in |
187 | let open Action in | 187 | let open Action in |
188 | function | 188 | function |
189 | | Simple Backspace -> Buffer.Action.delete_before ~n:1 |> on_focused_buffer | ||
190 | | Simple Arrow_down -> Buffer.Action.move_down |> on_focused_buffer | 189 | | Simple Arrow_down -> Buffer.Action.move_down |> on_focused_buffer |
191 | | Simple Arrow_left -> Buffer.Action.move_left |> on_focused_buffer | 190 | | Simple Arrow_left -> Buffer.Action.move_left |> on_focused_buffer |
192 | | Simple Arrow_right -> Buffer.Action.move_right |> on_focused_buffer | 191 | | Simple Arrow_right -> Buffer.Action.move_right |> on_focused_buffer |
193 | | Simple Arrow_up -> Buffer.Action.move_up |> on_focused_buffer | 192 | | Simple Arrow_up -> Buffer.Action.move_up |> on_focused_buffer |
193 | | Simple Backspace -> | ||
194 | Buffer.Action.delete_before ~cross_lines:true ~n:1 |> on_focused_buffer | ||
194 | | Simple (Ctrl 'Q') -> quit 0 | 195 | | Simple (Ctrl 'Q') -> quit 0 |
195 | | Simple Delete -> Buffer.Action.delete_after ~n:1 |> on_focused_buffer | 196 | | Simple Delete -> |
197 | Buffer.Action.delete_after ~cross_lines:true ~n:1 |> on_focused_buffer | ||
196 | | Simple Enter -> Buffer.Action.newline |> on_focused_buffer | 198 | | Simple Enter -> Buffer.Action.newline |> on_focused_buffer |
197 | | Simple Esc -> | 199 | | Simple Esc -> |
198 | (Buffer.Action.move_left |> on_focused_buffer) *> set_mode Normal | 200 | (Buffer.Action.move_left |> on_focused_buffer) *> set_mode Normal |
@@ -223,7 +225,7 @@ let handle_normal_command = | |||
223 | (* | Key 's' -> *) | 225 | (* | Key 's' -> *) |
224 | (* (Buffer.Action.delete_after |> on_focused_buffer_or_new) *) | 226 | (* (Buffer.Action.delete_after |> on_focused_buffer_or_new) *) |
225 | (* *> set_mode Insert *) | 227 | (* *> set_mode Insert *) |
226 | (* | Key 'x' -> Buffer.Action.delete_after |> on_focused_buffer_or_new *) | 228 | (* | Key 'x' | Delete -> Buffer.Action.delete_after |> on_focused_buffer_or_new *) |
227 | (* | Key 'X' -> Buffer.Action.delete_before |> on_focused_buffer_or_new *) | 229 | (* | Key 'X' -> Buffer.Action.delete_before |> on_focused_buffer_or_new *) |
228 | (* | Key '$' -> Buffer.Action.eol |> on_focused_buffer_or_new *) | 230 | (* | Key '$' -> Buffer.Action.eol |> on_focused_buffer_or_new *) |
229 | | Shortcut (_, n, Change, Line) -> | 231 | | 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 | |||
29 | let bof = far_left |> on_content | 29 | let bof = far_left |> on_content |
30 | let eof = far_right |> on_content | 30 | let eof = far_right |> on_content |
31 | let insert k = map_focus (push k) |> on_content | 31 | let insert k = map_focus (push k) |> on_content |
32 | let delete_after ~n = Fn.apply_n_times ~n (map_focus pop_after) |> on_content | 32 | |
33 | let delete_before ~n = Fn.apply_n_times ~n (map_focus pop) |> on_content | 33 | let delete_after ?(cross_lines = false) ~n = |
34 | let aux z = | ||
35 | let line = focus_or ~default:Zipper.empty z in | ||
36 | if cross_lines && is_far_right line && not (is_far_right z) then | ||
37 | (* let next = right z |> focus_or ~default:Zipper.empty |> far_left in *) | ||
38 | (* right z |> pop_after |> left |> map_focus (Fn.flip join next) *) | ||
39 | pop_after z |> map_focus_or ~default:line (far_left &> join line) | ||
40 | else map_focus pop_after z | ||
41 | in | ||
42 | Fn.apply_n_times ~n aux |> on_content | ||
43 | |||
44 | let delete_before ?(cross_lines = false) ~n = | ||
45 | let aux z = | ||
46 | let line = focus_or ~default:Zipper.empty z in | ||
47 | if cross_lines && is_far_left line && not (is_far_left z) then | ||
48 | pop_after z |> left |> map_focus (far_right &> Fn.flip join line) | ||
49 | else map_focus pop z | ||
50 | in | ||
51 | Fn.apply_n_times ~n aux |> on_content | ||
52 | |||
34 | let delete_to_eol = map_focus (split &> fst) |> on_content | 53 | let delete_to_eol = map_focus (split &> fst) |> on_content |
35 | let delete_to_bol = map_focus (split &> snd) |> on_content | 54 | let delete_to_bol = map_focus (split &> snd) |> on_content |
36 | let delete_lines ~n = Fn.apply_n_times ~n pop_after |> on_content | 55 | let delete_lines ~n = Fn.apply_n_times ~n pop_after |> on_content |