diff options
Diffstat (limited to 'lib/command.ml')
| -rw-r--r-- | lib/command.ml | 114 |
1 files changed, 70 insertions, 44 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 | ||
| 4 | type register = char option | 4 | type register = char option |
| 5 | type count = int option | 5 | type count = int option |
| 6 | type operation = Noop | Yank | Paste | Delete | Change | 6 | |
| 7 | type operation = | ||
| 8 | | Noop | ||
| 9 | | Yank | ||
| 10 | | Delete | ||
| 11 | | Change | ||
| 12 | | Join | ||
| 13 | | Paste_before | ||
| 14 | | Paste_after | ||
| 15 | | Erase_before | ||
| 16 | | Erase_after | ||
| 17 | |||
| 7 | type scope = Line | To_bol | To_eol | Down | Left | Right | Up | 18 | type scope = Line | To_bol | To_eol | Down | Left | Right | Up |
| 8 | 19 | ||
| 9 | type command = | 20 | type 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 | ||
| 16 | type t = command | 27 | type t = command |
| 17 | 28 | ||
| 18 | let shortcut ?r ?n c s = Shortcut (r, n, c, s) | 29 | let shortcut ?r ?n c = Shortcut (r, n, c) |
| 19 | let chord ?r ?n1 c ?n2 m = Chord (r, n1, c, n2, m) | 30 | let chord ?r ?n1 c ?n2 m = Chord (r, n1, c, n2, m) |
| 20 | 31 | ||
| 21 | let i_stream = | 32 | let i_stream = |
| @@ -42,6 +53,11 @@ let simple_movements = | |||
| 42 | Backspace; | 53 | Backspace; |
| 43 | ] | 54 | ] |
| 44 | 55 | ||
| 56 | let instant_operation = | ||
| 57 | [ Key 'C'; Key 'D'; Key 'J'; Key 'P'; Key 'X'; Key 'p'; Key 'x' ] | ||
| 58 | |||
| 59 | let chord_operation = [ Key 'c'; Key 'd'; Key 'y' ] | ||
| 60 | |||
| 45 | let to_scope = function | 61 | let 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 | |||
| 52 | let n_stream = | 68 | let 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 } |
