summaryrefslogtreecommitdiff
path: root/lib/command.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/command.ml')
-rw-r--r--lib/command.ml47
1 files changed, 23 insertions, 24 deletions
diff --git a/lib/command.ml b/lib/command.ml
index 31e2ca2..74b616a 100644
--- a/lib/command.ml
+++ b/lib/command.ml
@@ -18,6 +18,7 @@ type operation =
18type scope = Line | To_bol | To_eol | Down | Left | Right | Up 18type scope = Line | To_bol | To_eol | Down | Left | Right | Up
19 19
20type command = 20type command =
21 | Reset
21 | Type of char 22 | Type of char
22 | Simple of Key.t 23 | Simple of Key.t
23 | Partial of Key.t 24 | Partial of Key.t
@@ -89,59 +90,57 @@ let is_instant_operation k = List.mem ~equal:Poly.equal instant_operation k
89 90
90let n_stream = 91let n_stream =
91 let step s k = 92 let step s k =
92 let open Sequence.Step in
93 match (s, k) with 93 match (s, k) with
94 | `start, Key '"' -> Yield { value = Partial k; state = `chord_reg_pre } 94 | `start, Key '"' -> (`chord_reg_pre, Partial k)
95 (* Register *) 95 (* Register *)
96 | `chord_reg_pre, Key c -> Yield { value = Partial k; state = `chord_reg c } 96 | `chord_reg_pre, Key c -> (`chord_reg c, Partial k)
97 (* Count (first) *) 97 (* Count (first) *)
98 | `start, Key n when Char.('1' <= n && n <= '9') -> 98 | `start, Key n when Char.('1' <= n && n <= '9') ->
99 let n = Char.to_int n - 48 in 99 let n = Char.to_int n - 48 in
100 Yield { value = Partial k; state = `chord_fst_n (None, n) } 100 (`chord_fst_n (None, n), Partial k)
101 | `chord_reg r, Key n when Char.('1' <= n && n <= '9') -> 101 | `chord_reg r, Key n when Char.('1' <= n && n <= '9') ->
102 let n = Char.to_int n - 48 in 102 let n = Char.to_int n - 48 in
103 Yield { value = Partial k; state = `chord_fst_n (Some r, n) } 103 (`chord_fst_n (Some r, n), Partial k)
104 | `chord_fst_n (r, m), Key n when Char.('0' <= n && n <= '9') -> 104 | `chord_fst_n (r, m), Key n when Char.('0' <= n && n <= '9') ->
105 let n = (10 * m) + Char.to_int n - 48 in 105 let n = (10 * m) + Char.to_int n - 48 in
106 Yield { value = Partial k; state = `chord_fst_n (r, n) } 106 (`chord_fst_n (r, n), Partial k)
107 (* Instant operations *) 107 (* Instant operations *)
108 | `start, k when is_instant_operation k -> 108 | `start, k when is_instant_operation k -> (`start, shortcut (to_op k))
109 Yield { value = shortcut (to_op k); state = `start }
110 | `chord_reg r, k when is_instant_operation k -> 109 | `chord_reg r, k when is_instant_operation k ->
111 Yield { value = shortcut ~r (to_op k); state = `start } 110 (`start, shortcut ~r (to_op k))
112 | `chord_fst_n (r, n), k when is_instant_operation k -> 111 | `chord_fst_n (r, n), k when is_instant_operation k ->
113 Yield { value = shortcut ?r ~n (to_op k); state = `start } 112 (`start, shortcut ?r ~n (to_op k))
114 (* Chord operation (first) *) 113 (* Chord operation (first) *)
115 | `start, k when is_chord_operation k -> 114 | `start, k when is_chord_operation k ->
116 Yield { value = Partial k; state = `chord_cmd (None, None, to_op k) } 115 (`chord_cmd (None, None, to_op k), Partial k)
117 | `chord_reg r, k when is_chord_operation k -> 116 | `chord_reg r, k when is_chord_operation k ->
118 Yield { value = Partial k; state = `chord_cmd (Some r, None, to_op k) } 117 (`chord_cmd (Some r, None, to_op k), Partial k)
119 | `chord_fst_n (r, n), k when is_chord_operation k -> 118 | `chord_fst_n (r, n), k when is_chord_operation k ->
120 Yield { value = Partial k; state = `chord_cmd (r, Some n, to_op k) } 119 (`chord_cmd (r, Some n, to_op k), Partial k)
121 (* Count (second) *) 120 (* Count (second) *)
122 | `chord_cmd (r, n1, c), Key n when Char.('1' <= n && n <= '9') -> 121 | `chord_cmd (r, n1, c), Key n when Char.('1' <= n && n <= '9') ->
123 let n = Char.to_int n - 48 in 122 let n = Char.to_int n - 48 in
124 Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n) } 123 (`chord_snd_n (r, n1, c, n), Partial k)
125 | `chord_snd_n (r, n1, c, n2), Key n when Char.('0' <= n && n <= '9') -> 124 | `chord_snd_n (r, n1, c, n2), Key n when Char.('0' <= n && n <= '9') ->
126 let n2 = (10 * n2) + Char.to_int n - 48 in 125 let n2 = (10 * n2) + Char.to_int n - 48 in
127 Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n2) } 126 (`chord_snd_n (r, n1, c, n2), Partial k)
128 (* Chord operation (second) *) 127 (* Chord operation (second) *)
129 | `chord_cmd (r, n, c), k when is_chord_operation k && Poly.(c = to_op k) -> 128 | `chord_cmd (r, n, c), k when is_chord_operation k && Poly.(c = to_op k) ->
130 Yield { value = chord ?r ?n1:n c Line; state = `start } 129 (`start, chord ?r ?n1:n c Line)
131 | `chord_snd_n (r, n1, c, n2), k 130 | `chord_snd_n (r, n1, c, n2), k
132 when is_chord_operation k && Poly.(c = to_op k) -> 131 when is_chord_operation k && Poly.(c = to_op k) ->
133 Yield { value = chord ?r ?n1 c ~n2 Line; state = `start } 132 (`start, chord ?r ?n1 c ~n2 Line)
134 (* Movement *) 133 (* Movement *)
135 | (`start | `chord_reg _), k when is_simple_movement k -> 134 | (`start | `chord_reg _), k when is_simple_movement k ->
136 Yield { value = chord Noop (to_scope k); state = `start } 135 (`start, chord Noop (to_scope k))
137 | `chord_fst_n (_, n), k when is_simple_movement k -> 136 | `chord_fst_n (_, n), k when is_simple_movement k ->
138 Yield { value = chord ~n1:n Noop (to_scope k); state = `start } 137 (`start, chord ~n1:n Noop (to_scope k))
139 | `chord_cmd (r, n, c), k when is_simple_movement k -> 138 | `chord_cmd (r, n, c), k when is_simple_movement k ->
140 Yield { value = chord ?r ?n1:n c (to_scope k); state = `start } 139 (`start, chord ?r ?n1:n c (to_scope k))
141 | `chord_snd_n (r, n1, c, n2), k when is_simple_movement k -> 140 | `chord_snd_n (r, n1, c, n2), k when is_simple_movement k ->
142 Yield { value = chord ?r ?n1 c ~n2 (to_scope k); state = `start } 141 (`start, chord ?r ?n1 c ~n2 (to_scope k))
143 (* Catch-all rules *) 142 (* Catch-all rules *)
144 | `start, _ -> Yield { value = Simple k; state = `start } 143 | `start, _ -> (`start, Simple k)
145 | _, _ -> Skip { state = `start } 144 | _, _ -> (`start, Reset)
146 in 145 in
147 Sequence.unfold_with ~init:`start ~f:step Key.stream 146 Sequence.folding_map ~init:`start ~f:step Key.stream