summaryrefslogtreecommitdiff
path: root/lib/editor.ml
blob: 315067b3e888201e010b95ad06f5c0a3428fd16d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
open Base
module Buffer = EditorBuffer
open Util

type selection =
  | Empty
  | Glyphwise of char Sequence.t Sequence.t
  | Linewise of char Sequence.t Sequence.t

type cursor = int * int

type editor = {
  term : Terminal.state;
  mode : Mode.t;
  offset : int * int;
  cursor : cursor;
  buffer : Buffer.t Tipper.t;
  rendered : bool;
  istream : Command.t Sequence.t;
  nstream : Command.t Sequence.t;
  status_size : int;
  message : string option;
  message_timestamp : float;
  message_duration : float;
  pending_command : string;
  registers : selection Array.t;
  control : Control.t;
  search_history : (bool * char Sequence.t) Zipper.t;
}

type t = editor

let init (c : Config.t) : editor =
  {
    term = Terminal.init ();
    mode = Normal;
    offset = (0, 0);
    cursor = (1, 1);
    buffer =
      List.hd c.files
      |> Option.map ~f:Buffer.from_file
      |> Option.value_or_thunk ~default:Buffer.empty
      |> Tipper.create;
    rendered = true;
    istream = Command.i_stream;
    nstream = Command.n_stream;
    status_size = 2;
    message = Some "Welcome to the sandy editor!";
    message_timestamp = Unix.time ();
    message_duration = 5.;
    pending_command = "";
    registers = Array.create ~len:128 Empty;
    control = Control.create Key.Nul;
    search_history = Zipper.empty;
  }

let statusbar e =
  let open Text in
  (* let open Sequence.Infix in *)
  let w = e.term.size |> snd in
  let status =
    let mode = e.mode |> Mode.to_string |> sequence_of_string in
    let lsize = Sequence.length mode in
    let buf = Tipper.focus e.buffer in
    let c = buf.kind |> Buffer.string_of_kind |> sequence_of_string
    and br, bc = Buffer.size buf
    and cr, cc = Buffer.cursor ~rendered:false buf in
    let perc =
      match cr with
      | 0 -> "Top"
      | n when n = br -> "Bot"
      | n -> Printf.sprintf "%2d%%" (100 * n / br)
    in
    let nav =
      Printf.sprintf "%d/%d %2d/%2d [%s] " cr br cc bc perc
      |> sequence_of_string
    in
    let rsize = Sequence.length nav in
    spread ~l:(bold mode) ~lsize ~c ~r:(bold nav) ~rsize ~fill:' ' w |> invert
  and control =
    match e.mode with
    | Control -> Control.render e.control
    | _ ->
        let msg = Option.value ~default:"" e.message |> sequence_of_string
        and cmd = e.pending_command |> sequence_of_string in
        spread ~l:msg ~r:cmd ~fill:' ' w
  in
  Sequence.(take (of_list [ status; control ]) e.status_size)

type 'a action = t -> 'a * t

module Action = struct
  let run ~editor action = action () editor
  let eval ~editor action = run ~editor action |> fst
  let exec ~editor action = run ~editor action |> snd

  include Applicative.Make (struct
    type 'a t = 'a action

    let return a e = (a, e)

    let apply f a e =
      let g, e = f e in
      let x, e = a e in
      (g x, e)

    let map = `Define_using_apply
  end)

  include Monad.Make (struct
    type 'a t = 'a action

    let return x e = (x, e)

    let bind a ~f x =
      let y, a' = a x in
      f y a'

    let map = `Define_using_bind
  end)

  let ( let+ ) e f = map e ~f
  let ( let* ) e f = bind e ~f
  let ( and* ) = both

  let rec repeat ?(n = 1) a =
    match n with
    | _ when n <= 0 -> return ()
    | 1 -> a
    | _ -> a *> repeat ~n:(n - 1) a

  let get e = (e, e)
  let put e _ = ((), e)
  let modify ~f e = ((), f e)

  let update_cursor =
    let aux e =
      let dx, dy = e.offset and rs, cs = e.term.size in
      (* Limit cursor to buffer view *)
      let rs = rs - e.status_size in
      let cx, cy = e.buffer |> Tipper.focus |> Buffer.cursor in
      let dx' = Int.clamp_exn ~min:(cx - rs + 1) ~max:cx dx
      and dy' = Int.clamp_exn ~min:(cy - cs + 1) ~max:cy dy in
      { e with cursor = (cx - dx' + 1, cy - dy' + 1); offset = (dx', dy') }
    in
    modify ~f:aux

  let get_focused_buffer_history e = (e.buffer, e)

  let set_focused_buffer_history h =
    (fun e -> ((), { e with buffer = h })) *> update_cursor

  let on_focused_buffer_history f =
    get_focused_buffer_history >>| f >>= set_focused_buffer_history

  let get_focused_buffer e = (Tipper.focus e.buffer, e)

  let set_focused_buffer b e =
    ((), { e with buffer = Tipper.set_focus b e.buffer })

  let on_focused_buffer f =
    let* b = get_focused_buffer in
    let out, b = f b in
    let* () = set_focused_buffer b in
    let* () = update_cursor in
    return out

  let simulate f =
    let* b = get_focused_buffer in
    return (f b |> fst)

  let get_control_buffer e = (e.control, e)
  let set_control_buffer c e = ((), { e with control = c })

  let on_control_buffer f =
    let* c = get_control_buffer in
    set_control_buffer (f c)

  let get_mode e = (e.mode, e)
  let set_mode m e = ((), { e with mode = m })
  let get_search_history e = (e.search_history, e)
  let set_search_history h e = ((), { e with search_history = h })
  let on_search_history f = get_search_history >>| f >>= set_search_history

  let set_last_search dir word =
    Zipper.(far_left &> swap_focus (dir, word)) |> on_search_history

  let get_terminal_size e = (e.term.size, e)

  let get_register ?(r = '"') e =
    assert (Char.('!' <= r && r <= '~'));
    (e.registers.(Char.to_int r), e)

  let set_register ?(r = '"') s e =
    assert (Char.('!' <= r && r <= '~'));
    e.registers.(Char.to_int r) <- s;
    ((), e)

  let render =
    let aux e =
      let x, y = e.offset
      and r, c = e.term.size
      and fill = Sequence.singleton '~'
      and status = statusbar e
      and limit =
        (* debug *)
        Buffer.(if e.rendered then rendered_view else unrendered_view)
      in
      let ssize = e.status_size in
      let bufview =
        e.buffer |> Tipper.focus
        |> limit x y (r - ssize) c
        |> Text.extend ~fill r
        |> Fn.flip Sequence.take (r - ssize)
      in
      let screen = Sequence.append bufview status
      and cursor =
        let open Mode in
        match e.mode with
        | Control -> (r, Control.cursor e.control)
        | _ -> e.cursor
      in
      Terminal.redraw screen cursor
    in
    get >>| aux

  (* TODO: save logic *)
  let quit n = Stdlib.exit n

  (* Statusbar *)
  let set_message m e =
    ((), { e with message = Some m; message_timestamp = Unix.time () })

  let get_pending_command e = (e.pending_command, e)
  let set_pending_command p e = ((), { e with pending_command = p })

  let append_pending_command k =
    let aux p = p ^ Key.to_string k in
    get_pending_command >>| aux >>= set_pending_command

  let clear_pending_command = set_pending_command ""

  let tick =
    let check_message_timestamp e =
      let now = Unix.time () in
      let expired = Float.(e.message_timestamp < now - e.message_duration) in
      if Option.is_some e.message && expired then { e with message = None }
      else e
    in
    get >>| check_message_timestamp >>= put

  (* Control line *)
  let search dir word =
    let* coords = Buffer.Action.(search dir word |> on_focused_buffer) in
    match coords with
    | None ->
        let word = word |> Sequence.to_list |> String.of_list in
        set_message (Printf.sprintf "Pattern not found: %s" word)
    | Some (r, c) -> Buffer.Action.goto ~r ~c |> on_focused_buffer

  (* History *)
  let take_buffer_snapshot =
    let* h = get_focused_buffer_history in
    let buf = { (Tipper.focus h) with last_modified = Unix.gettimeofday () } in
    let h = Tipper.(h |> push (create buf) |> down) in
    set_focused_buffer_history h

  let undo =
    let* h = get_focused_buffer_history in
    if Tipper.is_root h then set_message "Already at the oldest change"
    else set_focused_buffer_history (Tipper.up h)

  let redo =
    let* h = get_focused_buffer_history in
    if Tipper.is_leaf h then set_message "Already at the newest change"
    else set_focused_buffer_history (Tipper.down h)

  let timetravel ?(later = false) ?(_secs = 0.) =
    let last_modified (n : Buffer.t Tipper.t) =
      (Tipper.focus n).last_modified
    in
    let find ts h =
      let f a t =
        let tmax =
          if later then
            Option.(map ~f:last_modified a |> value ~default:Float.max_value)
          else ts
        in
        let tmin =
          if later then ts
          else Option.(map ~f:last_modified a |> value ~default:0.)
        and cur = last_modified t in
        if Float.(tmin < cur && cur < tmax) then Some t else a
      in
      Tipper.fold ~a:None ~f h
    in
    let* h = get_focused_buffer_history in
    let ts = last_modified h in
    match find ts (Tipper.root h) with
    | None -> set_message "Already at the oldest change"
    | Some h -> set_focused_buffer_history h

  (* Debug *)
  let get_rendered e = (e.rendered, e)
  let set_rendered r e = ((), { e with rendered = r })
  let toggle_rendered = get_rendered >>| not >>= set_rendered
  let noop = return ()
end

let move ?(up = 0) ?(down = 0) ?(left = 0) ?(right = 0) (x, y) =
  (x + down - up, y + right - left)

let move_to ?x ?y (sx, sy) = Option.(value x ~default:sx, value y ~default:sy)

let handle_insert_command =
  let open Command in
  let open Action in
  function
  | 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 |> ignore_m
  | Simple (Ctrl 'Q') -> quit 0
  | Simple Delete ->
      Buffer.Action.delete_after ~cross_lines:true ~n:1
      |> on_focused_buffer |> ignore_m
  | Simple Enter -> Buffer.Action.newline |> on_focused_buffer
  | Simple Esc ->
      let* () = Buffer.Action.move_left |> on_focused_buffer in
      set_mode Normal
  | Simple Page_down | Simple (Ctrl 'F') ->
      (* TODO consider using the buffer window size (i.e., subtract status_size) *)
      let* n, _ = get_terminal_size in
      Buffer.Action.move_down ~n |> on_focused_buffer
  | Simple Page_up | Simple (Ctrl 'B') ->
      let* n, _ = get_terminal_size in
      Buffer.Action.move_up ~n |> on_focused_buffer
  | Simple (Ctrl 'D') ->
      let* r, _ = get_terminal_size in
      Buffer.Action.move_down ~n:(r / 2) |> on_focused_buffer
  | Simple (Ctrl 'U') ->
      let* r, _ = get_terminal_size in
      Buffer.Action.move_up ~n:(r / 2) |> on_focused_buffer
  | Simple Home -> Buffer.Action.bol |> on_focused_buffer
  | Simple End -> Buffer.Action.eol |> on_focused_buffer
  | Simple Tab -> Buffer.Action.insert '\t' |> on_focused_buffer
  | Type k -> Buffer.Action.insert k |> on_focused_buffer
  | _ -> noop

let handle_normal_command c =
  let open Command in
  let open Action in
  let update_command_cue =
    match c with
    | Partial k -> append_pending_command k
    | _ -> clear_pending_command
  and compute_action =
    match c with
    (* Movements *)
    | Chord (_, n, Noop, _, Down) ->
        Buffer.Action.move_down ?n |> on_focused_buffer
    | Chord (_, n, Noop, _, Left) ->
        Buffer.Action.move_left ?n |> on_focused_buffer
    | Chord (_, n, Noop, _, Right) ->
        Buffer.Action.move_right ?n |> on_focused_buffer
    | Chord (_, n, Noop, _, Up) -> Buffer.Action.move_up ?n |> on_focused_buffer
    | Chord (_, n, Noop, _, To_bol) ->
        let n = Option.value ~default:1 n - 1 in
        Buffer.Action.bol ~n |> on_focused_buffer
    | Chord (_, n, Noop, _, To_eol) ->
        let n = Option.value ~default:1 n - 1 in
        Buffer.Action.eol ~n |> on_focused_buffer
    | Simple Page_down | Simple (Ctrl 'F') ->
        let* n, _ = get_terminal_size in
        Buffer.Action.move_down ~n |> on_focused_buffer
    | Simple Page_up | Simple (Ctrl 'B') ->
        let* n, _ = get_terminal_size in
        Buffer.Action.move_up ~n |> on_focused_buffer
    | Simple (Ctrl 'D') ->
        let* r, _ = get_terminal_size in
        Buffer.Action.move_down ~n:(r / 2) |> on_focused_buffer
    | Simple (Ctrl 'U') ->
        let* r, _ = get_terminal_size in
        Buffer.Action.move_up ~n:(r / 2) |> on_focused_buffer
    | Shortcut (_, n, Goto) -> (
        match n with
        | None -> Buffer.Action.eof |> on_focused_buffer
        | Some n -> Buffer.Action.goto ~r:n |> on_focused_buffer)
    (* Yank *)
    | Shortcut (r, n, Yank) ->
        let n = Option.value ~default:1 n - 1 in
        let* out = Buffer.Action.delete_to_eol ~n |> simulate in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Yank, n2, Line) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* out = Buffer.Action.delete_lines ~n |> simulate in
        set_register ?r (Linewise out)
    | Chord (r, n1, Yank, n2, Down) ->
        let n = Option.((value ~default:1 n1 * value ~default:1 n2) + 1) in
        let* out = Buffer.Action.delete_lines ~n |> simulate in
        set_register ?r (Linewise out)
    | Chord (r, n1, Yank, n2, Left) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* out = Buffer.Action.delete_before ~n |> simulate in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Yank, n2, Right) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* out = Buffer.Action.delete_after ~n |> simulate in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Yank, n2, Up) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* out =
          Buffer.Action.(move_up ~n *> delete_lines ~n:(n + 1)) |> simulate
        in
        set_register ?r (Linewise out)
    | Chord (r, _, Yank, _, To_bol) ->
        let* out = Buffer.Action.delete_to_bol |> simulate in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Yank, n2, To_eol) ->
        let n = Option.((value ~default:1 n1 * value ~default:1 n2) - 1) in
        let* out = Buffer.Action.delete_to_eol ~n |> simulate in
        set_register ?r (Glyphwise out)
    (* Change *)
    | Shortcut (r, n, Change) ->
        let n = Option.value ~default:1 n - 1 in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_to_eol ~n |> on_focused_buffer in
        let* () = set_register ?r (Glyphwise out) in
        set_mode Insert
    | Chord (r, n1, Change, n2, Line) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let act =
          let open Buffer.Action in
          let* out = delete_lines ~n
          and* () = insert_line ~before:true *> move_up in
          return out
        in
        let* () = take_buffer_snapshot in
        let* out = act |> on_focused_buffer in
        let* () = set_register ?r (Linewise out) in
        set_mode Insert
    | Chord (r, n1, Change, n2, Down) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) + 1 in
        let act =
          let open Buffer.Action in
          let* out = delete_lines ~n
          and* () = insert_line ~before:true *> move_up in
          return out
        in
        let* () = take_buffer_snapshot in
        let* out = act |> on_focused_buffer in
        let* () = set_register ?r (Linewise out) in
        set_mode Insert
    | Chord (r, n1, Change, n2, Left) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_before ~n |> on_focused_buffer in
        let* () = set_register ?r (Glyphwise out) in
        set_mode Insert
    | Chord (r, n1, Change, n2, Right) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_after ~n |> on_focused_buffer in
        let* () = set_register ?r (Glyphwise out) in
        set_mode Insert
    | Chord (r, n1, Change, n2, Up) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let act =
          let open Buffer.Action in
          let* out = move_up ~n *> delete_lines ~n:(n + 1)
          and* () = insert_line ~before:true *> move_up in
          return out
        in
        let* () = take_buffer_snapshot in
        let* out = act |> on_focused_buffer in
        let* () = set_register ?r (Linewise out) in
        set_mode Insert
    | Chord (r, _, Change, _, To_bol) ->
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_to_bol |> on_focused_buffer in
        let* () = set_register ?r (Glyphwise out) in
        set_mode Insert
    | Chord (r, n1, Change, n2, To_eol) ->
        let n = Option.((value ~default:1 n1 * value ~default:1 n2) - 1) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_to_eol ~n |> on_focused_buffer in
        let* () = set_register ?r (Glyphwise out) in
        set_mode Insert
    (* Delete *)
    | Shortcut (r, n, Delete) ->
        let n = Option.value ~default:1 n - 1 in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_to_eol ~n |> on_focused_buffer in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Delete, n2, Line) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_lines ~n |> on_focused_buffer in
        set_register ?r (Linewise out)
    | Chord (r, n1, Delete, n2, Down) ->
        let n = Option.((value ~default:1 n1 * value ~default:1 n2) + 1) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_lines ~n |> on_focused_buffer in
        set_register ?r (Linewise out)
    | Chord (r, n1, Delete, n2, Left) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_before ~n |> on_focused_buffer in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Delete, n2, Right) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_after ~n |> on_focused_buffer in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Delete, n2, Up) ->
        let n = Option.(value ~default:1 n1 * value ~default:1 n2) in
        let* () = take_buffer_snapshot in
        let* out =
          Buffer.Action.(move_up ~n *> delete_lines ~n:(n + 1))
          |> on_focused_buffer
        in
        set_register ?r (Linewise out)
    | Shortcut (r, n, Erase_before) ->
        let n = Option.value ~default:1 n in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_before ~n |> on_focused_buffer in
        set_register ?r (Glyphwise out)
    | Shortcut (r, n, Erase_after) ->
        let n = Option.value ~default:1 n in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_after ~n |> on_focused_buffer in
        set_register ?r (Glyphwise out)
    | Chord (r, _, Delete, _, To_bol) ->
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_to_bol |> on_focused_buffer in
        set_register ?r (Glyphwise out)
    | Chord (r, n1, Delete, n2, To_eol) ->
        let n = Option.((value ~default:1 n1 * value ~default:1 n2) - 1) in
        let* () = take_buffer_snapshot in
        let* out = Buffer.Action.delete_to_eol ~n |> on_focused_buffer in
        set_register ?r (Glyphwise out)
    (* Paste *)
    | Shortcut (r, n, Paste_after) -> (
        get_register ?r >>= function
        | Empty -> noop
        | Glyphwise z ->
            let* () = take_buffer_snapshot in
            Buffer.Action.paste ?n z |> on_focused_buffer
        | Linewise z ->
            let* () = take_buffer_snapshot in
            Buffer.Action.paste ~linewise:true ?n z |> on_focused_buffer)
    | Shortcut (r, n, Paste_before) -> (
        get_register ?r >>= function
        | Empty -> noop
        | Glyphwise z ->
            let* () = take_buffer_snapshot in
            Buffer.Action.paste ~before:true ?n z |> on_focused_buffer
        | Linewise z ->
            let* () = take_buffer_snapshot in
            Buffer.Action.paste ~before:true ~linewise:true ?n z
            |> on_focused_buffer)
    (* Join *)
    | Shortcut (_, n, Join) ->
        let n = Option.(value ~default:2 n) in
        let* () = take_buffer_snapshot in
        Buffer.Action.join_lines ~n |> on_focused_buffer
    (* Control *)
    | Simple (Key ':' as k) ->
        let c = Control.create k in
        set_control_buffer c *> set_mode Control
    | Simple (Key '/' as k) ->
        let c = Control.create k in
        (Zipper.(far_left &> push (true, Sequence.empty)) |> on_search_history)
        *> set_control_buffer c *> set_mode Control
    | Simple (Key '?' as k) ->
        let c = Control.create k in
        (Zipper.(far_left &> push (false, Sequence.empty)) |> on_search_history)
        *> set_control_buffer c *> set_mode Control
    | Shortcut (_, n, Search) -> (
        let* h = get_search_history in
        match Zipper.focus h with
        | None -> set_message "No search history"
        | Some (dir, word) -> search dir word |> repeat ?n)
    | Shortcut (_, n, Search_rev) -> (
        let* h = get_search_history in
        match Zipper.focus h with
        | None -> set_message "No search history"
        | Some (dir, word) -> search (not dir) word |> repeat ?n)
    | Simple (Ctrl 'Q') -> quit 0
    (* History *)
    | Shortcut (_, n, Undo) -> repeat ?n undo
    | Shortcut (_, n, Redo) -> repeat ?n redo
    | Shortcut (_, n, Earlier) -> repeat ?n timetravel
    | Shortcut (_, n, Later) -> repeat ?n (timetravel ~later:true)
    (* | Shortcut (_, n, Redo) -> repeat ?n redo *)
    (* Misc *)
    | Simple (Key 'A') ->
        (Buffer.Action.eol |> on_focused_buffer) *> set_mode Insert
    | Simple (Key 'a') ->
        (Buffer.Action.move_right |> on_focused_buffer) *> set_mode Insert
    (* | Key 'G' -> Buffer.Action.eof |> on_focused_buffer_or_new *)
    (* | Key 'I' -> noop *)
    | Simple (Key 'i') -> set_mode Insert
    (* | Key 's' -> *)
    (*     (Buffer.Action.delete_after |> on_focused_buffer_or_new) *)
    (* *> set_mode Insert *)
    (* Debug *)
    | Simple (Ctrl 'R') -> toggle_rendered
    | _ -> noop
  in
  compute_action *> update_command_cue

let handle_control_command =
  let open Command in
  let open Action in
  function
  | Simple Arrow_down ->
      let* c = get_control_buffer in
      if Control.is_search c then
        let* () = Zipper.left |> on_search_history
        and* h = get_search_history in
        match Zipper.focus h with
        | None -> noop
        | Some (_, word) -> Control.set_content word |> on_control_buffer
      else failwith "Control line command history unimplemented!"
  | Simple Arrow_left -> Control.move_left |> on_control_buffer
  | Simple Arrow_right -> Control.move_right |> on_control_buffer
  | Simple Arrow_up ->
      let* c = get_control_buffer in
      if Control.is_search c then
        let* () = Zipper.right |> on_search_history
        and* h = get_search_history in
        match Zipper.focus h with
        | None -> noop
        | Some (_, word) -> Control.set_content word |> on_control_buffer
      else failwith "Control line command history unimplemented!"
  | Simple Backspace -> Control.delete_before |> on_control_buffer
  | Simple Delete -> Control.delete_after |> on_control_buffer
  | Simple Enter -> (
      let* () = set_mode Normal and* c = get_control_buffer in
      match Control.get_result c with
      | Search (dir, word) -> search dir word *> set_last_search dir word
      | No_result -> noop)
  | Simple Esc -> (
      let* () = set_mode Normal and* c = get_control_buffer in
      match Control.get_result c with
      | Search _ -> Zipper.(far_left &> pop &> snd) |> on_search_history
      | No_result -> noop)
  | Simple Home -> Control.bol |> on_control_buffer
  | Simple End -> Control.eol |> on_control_buffer
  | Type k -> Control.insert k |> on_control_buffer
  | _ -> noop

let handle_next_command m e =
  let open Mode in
  match m with
  | Insert -> (
      match Sequence.next e.istream with
      | None -> ((), e)
      | Some (h, t) -> handle_insert_command h { e with istream = t })
  | Normal -> (
      match Sequence.next e.nstream with
      | None -> ((), e)
      | Some (h, t) -> handle_normal_command h { e with nstream = t })
  | Control -> (
      match Sequence.next e.istream with
      | None -> ((), e)
      | Some (h, t) -> handle_control_command h { e with istream = t })

let handle_next_command =
  let open Action in
  get_mode >>= handle_next_command