summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Igne <undyamon@disroot.org>2024-01-12 19:00:24 +0100
committerFederico Igne <undyamon@disroot.org>2024-01-12 19:00:24 +0100
commit123ba3b46acd16d420b82d5a25c5a4c50135afe6 (patch)
tree7199741670cfcc43debc665fa243d6fb1ec37f5b
parentd096051f807441d8a55d44ae55ee1f9463d6d808 (diff)
downloadsandy-123ba3b46acd16d420b82d5a25c5a4c50135afe6.tar.gz
sandy-123ba3b46acd16d420b82d5a25c5a4c50135afe6.zip
feat: add support for "instant" normal commands (like [J]oin)
-rw-r--r--lib/command.ml114
-rw-r--r--lib/editor.ml57
-rw-r--r--lib/editorBuffer.ml16
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
3 3
4type register = char option 4type register = char option
5type count = int option 5type count = int option
6type operation = Noop | Yank | Paste | Delete | Change 6
7type operation =
8 | Noop
9 | Yank
10 | Delete
11 | Change
12 | Join
13 | Paste_before
14 | Paste_after
15 | Erase_before
16 | Erase_after
17
7type scope = Line | To_bol | To_eol | Down | Left | Right | Up 18type scope = Line | To_bol | To_eol | Down | Left | Right | Up
8 19
9type command = 20type command =
10 | Type of char 21 | Type of char
11 | Simple of Key.t 22 | Simple of Key.t
12 | Partial of Key.t 23 | Partial of Key.t
13 | Shortcut of register * count * operation * scope 24 | Shortcut of register * count * operation
14 | Chord of register * count * operation * count * scope 25 | Chord of register * count * operation * count * scope
15 26
16type t = command 27type t = command
17 28
18let shortcut ?r ?n c s = Shortcut (r, n, c, s) 29let shortcut ?r ?n c = Shortcut (r, n, c)
19let chord ?r ?n1 c ?n2 m = Chord (r, n1, c, n2, m) 30let chord ?r ?n1 c ?n2 m = Chord (r, n1, c, n2, m)
20 31
21let i_stream = 32let i_stream =
@@ -42,6 +53,11 @@ let simple_movements =
42 Backspace; 53 Backspace;
43 ] 54 ]
44 55
56let instant_operation =
57 [ Key 'C'; Key 'D'; Key 'J'; Key 'P'; Key 'X'; Key 'p'; Key 'x' ]
58
59let chord_operation = [ Key 'c'; Key 'd'; Key 'y' ]
60
45let to_scope = function 61let to_scope = function
46 | Key 'j' | Arrow_down -> Down 62 | Key 'j' | Arrow_down -> Down
47 | Key 'h' | Arrow_left | Backspace -> Left 63 | Key 'h' | Arrow_left | Backspace -> Left
@@ -52,61 +68,71 @@ let to_scope = function
52let n_stream = 68let n_stream =
53 let step s k = 69 let step s k =
54 let open Sequence.Step in 70 let open Sequence.Step in
55 let is_chord_op c = String.contains "ydc" (Char.lowercase c) in 71 let is_chord_operation k = List.mem ~equal:Poly.equal chord_operation k in
56 let is_simple_movement k = List.mem ~equal:Poly.equal simple_movements k in 72 let is_simple_movement k = List.mem ~equal:Poly.equal simple_movements k in
57 let to_op c = 73 let is_instant_operation k =
58 match Char.lowercase c with 74 List.mem ~equal:Poly.equal instant_operation k
59 | 'y' -> Yank 75 in
60 | 'p' -> Paste 76 let to_op = function
61 | 'd' -> Delete 77 | Key 'J' -> Join
62 | 'c' -> Change 78 | Key 'c' | Key 'C' -> Change
79 | Key 'd' | Key 'D' -> Delete
80 | Key 'y' | Key 'Y' -> Yank
81 | Key 'p' -> Paste_after
82 | Key 'P' -> Paste_before
83 | Key 'x' -> Erase_after
84 | Key 'X' -> Erase_before
63 | _ -> failwith "Invalid operation in chord." 85 | _ -> failwith "Invalid operation in chord."
64 in 86 in
65 match (s, k) with 87 match (s, k) with
66 | `start, Key '"' -> Yield { value = Partial k; state = `chord_reg_pre } 88 | `start, Key '"' -> Yield { value = Partial k; state = `chord_reg_pre }
89 (* Register *)
67 | `chord_reg_pre, Key c -> Yield { value = Partial k; state = `chord_reg c } 90 | `chord_reg_pre, Key c -> Yield { value = Partial k; state = `chord_reg c }
68 | `chord_reg r, Key n when Char.('1' <= n && n <= '9') -> 91 (* Count (first) *)
69 let n = Char.to_int n - 48 in
70 Yield { value = Partial k; state = `chord_n (Some r, n) }
71 | `start, Key n when Char.('1' <= n && n <= '9') -> 92 | `start, Key n when Char.('1' <= n && n <= '9') ->
72 let n = Char.to_int n - 48 in 93 let n = Char.to_int n - 48 in
73 Yield { value = Partial k; state = `chord_n (None, n) } 94 Yield { value = Partial k; state = `chord_fst_n (None, n) }
74 | `chord_n (r, m), Key n when Char.('0' <= n && n <= '9') -> 95 | `chord_reg r, Key n when Char.('1' <= n && n <= '9') ->
96 let n = Char.to_int n - 48 in
97 Yield { value = Partial k; state = `chord_fst_n (Some r, n) }
98 | `chord_fst_n (r, m), Key n when Char.('0' <= n && n <= '9') ->
75 let n = (10 * m) + Char.to_int n - 48 in 99 let n = (10 * m) + Char.to_int n - 48 in
76 Yield { value = Partial k; state = `chord_n (r, n) } 100 Yield { value = Partial k; state = `chord_fst_n (r, n) }
77 | `start, Key c when is_chord_op c -> 101 (* Instant operations *)
78 if Char.is_uppercase c then 102 | `start, k when is_instant_operation k ->
79 Yield { value = shortcut (to_op c) To_eol; state = `start } 103 Yield { value = shortcut (to_op k); state = `start }
80 else 104 | `chord_reg r, k when is_instant_operation k ->
81 Yield { value = Partial k; state = `chord_cmd (None, None, to_op c) } 105 Yield { value = shortcut ~r (to_op k); state = `start }
82 | `chord_reg r, Key c when is_chord_op c -> 106 | `chord_fst_n (r, n), k when is_instant_operation k ->
83 if Char.is_uppercase c then 107 Yield { value = shortcut ?r ~n (to_op k); state = `start }
84 Yield { value = shortcut ~r (to_op c) To_eol; state = `start } 108 (* Chord operation (first) *)
85 else 109 | `start, k when is_chord_operation k ->
86 Yield 110 Yield { value = Partial k; state = `chord_cmd (None, None, to_op k) }
87 { value = Partial k; state = `chord_cmd (Some r, None, to_op c) } 111 | `chord_reg r, k when is_chord_operation k ->
88 | `chord_n (r, n), Key c when is_chord_op c -> 112 Yield { value = Partial k; state = `chord_cmd (Some r, None, to_op k) }
89 if Char.is_uppercase c then 113 | `chord_fst_n (r, n), k when is_chord_operation k ->
90 Yield { value = shortcut ?r ~n (to_op c) To_eol; state = `start } 114 Yield { value = Partial k; state = `chord_cmd (r, Some n, to_op k) }
91 else 115 (* Count (second) *)
92 Yield { value = Partial k; state = `chord_cmd (r, Some n, to_op c) } 116 | `chord_cmd (r, n1, c), Key n when Char.('1' <= n && n <= '9') ->
93 | `chord_cmd (r, n, c), Key ch when is_chord_op ch && Poly.(c = to_op ch) -> 117 let n = Char.to_int n - 48 in
94 if Char.is_uppercase ch then 118 Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n) }
95 Yield { value = shortcut ?r ?n c To_bol; state = `start } 119 | `chord_snd_n (r, n1, c, n2), Key n when Char.('0' <= n && n <= '9') ->
96 else Yield { value = shortcut ?r ?n c Line; state = `start } 120 let n2 = (10 * n2) + Char.to_int n - 48 in
121 Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n2) }
122 (* Chord operation (second) *)
123 | `chord_cmd (r, n, c), k when is_chord_operation k && Poly.(c = to_op k) ->
124 Yield { value = chord ?r ?n1:n c Line; state = `start }
125 | `chord_snd_n (r, n1, c, n2), k
126 when is_chord_operation k && Poly.(c = to_op k) ->
127 Yield { value = chord ?r ?n1 c ~n2 Line; state = `start }
128 (* Movement *)
97 | (`start | `chord_reg _), k when is_simple_movement k -> 129 | (`start | `chord_reg _), k when is_simple_movement k ->
98 Yield { value = chord Noop (to_scope k); state = `start } 130 Yield { value = chord Noop (to_scope k); state = `start }
99 | `chord_n (_, n), k when is_simple_movement k -> 131 | `chord_fst_n (_, n), k when is_simple_movement k ->
100 Yield { value = chord ~n1:n Noop (to_scope k); state = `start } 132 Yield { value = chord ~n1:n Noop (to_scope k); state = `start }
101 | `chord_cmd (r, n, c), k when is_simple_movement k -> 133 | `chord_cmd (r, n, c), k when is_simple_movement k ->
102 Yield { value = chord ?r ?n1:n c (to_scope k); state = `start } 134 Yield { value = chord ?r ?n1:n c (to_scope k); state = `start }
103 | `chord_cmd (r, n1, c), Key n when Char.('1' <= n && n <= '9') -> 135 | `chord_snd_n (r, n1, c, n2), k when is_simple_movement k ->
104 let n = Char.to_int n - 48 in
105 Yield { value = Partial k; state = `chord_mv_n (r, n1, c, n) }
106 | `chord_mv_n (r, n1, c, n2), Key n when Char.('0' <= n && n <= '9') ->
107 let n2 = (10 * n2) + Char.to_int n - 48 in
108 Yield { value = Partial k; state = `chord_mv_n (r, n1, c, n2) }
109 | `chord_mv_n (r, n1, c, n2), k when is_simple_movement k ->
110 Yield { value = chord ?r ?n1 c ~n2 (to_scope k); state = `start } 136 Yield { value = chord ?r ?n1 c ~n2 (to_scope k); state = `start }
111 (* Catch-all rules *) 137 (* Catch-all rules *)
112 | `start, _ -> Yield { value = Simple k; state = `start } 138 | `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 =
228 (* | Key 'x' | Delete -> Buffer.Action.delete_after |> on_focused_buffer_or_new *) 228 (* | Key 'x' | Delete -> Buffer.Action.delete_after |> on_focused_buffer_or_new *)
229 (* | Key 'X' -> Buffer.Action.delete_before |> on_focused_buffer_or_new *) 229 (* | Key 'X' -> Buffer.Action.delete_before |> on_focused_buffer_or_new *)
230 (* | Key '$' -> Buffer.Action.eol |> on_focused_buffer_or_new *) 230 (* | Key '$' -> Buffer.Action.eol |> on_focused_buffer_or_new *)
231 | Shortcut (_, n, Change, Line) -> 231 (* Change *)
232 | Shortcut (_, n, Change) ->
232 let n = Option.value ~default:1 n - 1 in 233 let n = Option.value ~default:1 n - 1 in
234 (Buffer.Action.(
235 delete_to_eol &> move_down &> delete_lines ~n &> move_up &> eol)
236 |> on_focused_buffer_or_new)
237 *> set_mode Insert
238 | Chord (_, n1, Change, n2, Line) ->
239 let n = Option.((value ~default:1 n1 * value ~default:1 n2) - 1) in
240 (Buffer.Action.(delete_lines ~n &> bol &> delete_to_eol)
241 |> on_focused_buffer_or_new)
242 *> set_mode Insert
243 | Chord (_, n1, Change, n2, Down) ->
244 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
233 (Buffer.Action.(delete_lines ~n &> bol &> delete_to_eol) 245 (Buffer.Action.(delete_lines ~n &> bol &> delete_to_eol)
234 |> on_focused_buffer_or_new) 246 |> on_focused_buffer_or_new)
235 *> set_mode Insert 247 *> set_mode Insert
236 | Shortcut (_, _, Change, To_eol) -> 248 | Chord (_, n1, Change, n2, Left) ->
237 (Buffer.Action.delete_to_eol |> on_focused_buffer_or_new) 249 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
250 (Buffer.Action.delete_before ~n |> on_focused_buffer_or_new)
238 *> set_mode Insert 251 *> set_mode Insert
239 | Shortcut (_, _, Change, To_bol) -> 252 | Chord (_, n1, Change, n2, Right) ->
240 (Buffer.Action.delete_to_bol |> on_focused_buffer_or_new) 253 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
254 (Buffer.Action.delete_after ~n |> on_focused_buffer_or_new)
255 *> set_mode Insert
256 | Chord (_, n1, Change, n2, Up) ->
257 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
258 (Buffer.Action.(bol &> delete_to_eol &> delete_lines_before ~n)
259 |> on_focused_buffer_or_new)
241 *> set_mode Insert 260 *> set_mode Insert
242 (* Delete *) 261 (* Delete *)
262 | Shortcut (_, n, Delete) ->
263 let n = Option.value ~default:1 n - 1 in
264 Buffer.Action.(
265 delete_to_eol &> move_down &> delete_lines ~n &> move_up &> eol)
266 |> on_focused_buffer_or_new
267 | Chord (_, n1, Delete, n2, Line) ->
268 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
269 Buffer.Action.delete_lines ~n |> on_focused_buffer_or_new
243 | Chord (_, n1, Delete, n2, Down) -> 270 | Chord (_, n1, Delete, n2, Down) ->
244 let n = Option.((value ~default:1 n1 * value ~default:1 n2) + 1) in 271 let n = Option.((value ~default:1 n1 * value ~default:1 n2) + 1) in
245 Buffer.Action.delete_lines ~n |> on_focused_buffer_or_new 272 Buffer.Action.delete_lines ~n |> on_focused_buffer_or_new
246 | Chord (_, n1, Delete, n2, Left) -> 273 | Chord (_, n1, Delete, n2, Left) ->
247 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in 274 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
248 Buffer.Action.delete_before ~n |> on_focused_buffer_or_new 275 Buffer.Action.delete_before ~n |> on_focused_buffer_or_new
276 | Shortcut (_, n, Erase_before) ->
277 let n = Option.(value ~default:1 n) in
278 Buffer.Action.delete_before ~n |> on_focused_buffer_or_new
249 | Chord (_, n1, Delete, n2, Right) -> 279 | Chord (_, n1, Delete, n2, Right) ->
250 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in 280 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
251 Buffer.Action.delete_after ~n |> on_focused_buffer_or_new 281 Buffer.Action.delete_after ~n |> on_focused_buffer_or_new
282 | Shortcut (_, n, Erase_after) ->
283 let n = Option.(value ~default:1 n) in
284 Buffer.Action.delete_after ~n |> on_focused_buffer_or_new
252 | Chord (_, n1, Delete, n2, Up) -> 285 | Chord (_, n1, Delete, n2, Up) ->
253 let n = Option.((value ~default:1 n1 * value ~default:1 n2) + 1) in 286 let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
254 Buffer.Action.delete_lines_before ~n |> on_focused_buffer_or_new 287 Buffer.Action.(delete_lines ~n:1 &> delete_lines_before ~n)
255 | Shortcut (_, n, Delete, Line) ->
256 Buffer.Action.delete_lines ~n:Option.(value ~default:1 n)
257 |> on_focused_buffer_or_new 288 |> on_focused_buffer_or_new
258 | Shortcut (_, _, Delete, To_eol) -> 289 (* Join *)
259 Buffer.Action.delete_to_eol |> on_focused_buffer_or_new 290 | Shortcut (_, n, Join) ->
260 | Shortcut (_, _, Delete, To_bol) -> 291 let n = Option.(value ~default:1 n) in
261 Buffer.Action.delete_to_bol |> on_focused_buffer_or_new 292 Buffer.Action.join_lines ~n |> on_focused_buffer_or_new
262 | _ -> noop 293 | _ -> noop
263 294
264let handle_next_command m e = 295let 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
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 replace k = () *)
32 33
33 let delete_after ?(cross_lines = false) ~n = 34 let delete_after ?(cross_lines = false) ~n =
34 let aux z = 35 let aux z =
35 let line = focus_or ~default:Zipper.empty z in 36 let line = focus_or ~default:Zipper.empty z in
36 if cross_lines && is_far_right line && not (is_far_right z) then 37 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) 38 pop_after z |> map_focus_or ~default:line (far_left &> join line)
40 else map_focus pop_after z 39 else map_focus pop_after z
41 in 40 in
@@ -53,9 +52,7 @@ module Action = struct
53 let delete_to_eol = map_focus (split &> fst) |> on_content 52 let delete_to_eol = map_focus (split &> fst) |> on_content
54 let delete_to_bol = map_focus (split &> snd) |> on_content 53 let delete_to_bol = map_focus (split &> snd) |> on_content
55 let delete_lines ~n = Fn.apply_n_times ~n pop_after |> on_content 54 let delete_lines ~n = Fn.apply_n_times ~n pop_after |> on_content
56 55 let delete_lines_before ~n = Fn.apply_n_times ~n pop_before |> on_content
57 let delete_lines_before ~n =
58 on_content (fun z -> pop_after z |> Fn.apply_n_times ~n:(n - 1) pop_before)
59 56
60 let newline = 57 let newline =
61 let aux z = 58 let aux z =
@@ -64,6 +61,15 @@ module Action = struct
64 in 61 in
65 on_content aux 62 on_content aux
66 63
64 let join_lines ~n =
65 let aux z =
66 if is_far_right z || is_far_right (right z) then z
67 else
68 let line = focus_or ~default:Zipper.empty z |> far_right in
69 pop_after z |> map_focus (far_left &> join line)
70 in
71 Fn.apply_n_times ~n aux |> on_content
72
67 (* let save_history_to ?(clear = true) r = () *) 73 (* let save_history_to ?(clear = true) r = () *)
68end 74end
69 75