Source file textbuffer.ml

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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
(*********************************************************************************)
(*                OCaml-Stk                                                      *)
(*                                                                               *)
(*    Copyright (C) 2023-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU General Public License as                    *)
(*    published by the Free Software Foundation, version 3 of the License.       *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public                  *)
(*    License along with this program; if not, write to the Free Software        *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    As a special exception, you have permission to link this program           *)
(*    with the OCaml compiler and distribute executables, as long as you         *)
(*    follow the requirements of the GNU GPL in regard to all of the             *)
(*    software in the executable aside from the OCaml compiler.                  *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

open Tsdl

[@@@landmark "auto"]


include (val Log.create_src "stk.textbuffer")

let default_word_char_re = Pcre.(regexp ~iflags:(cflags [`UTF8]) "(*UCP)\\w")

type line_offset = {
    line: int ;
    bol: int ;
    offset: int ;
}

let line_offset ~line ~bol ~offset = { line ; bol; offset }
let pp_line_offset ppf lo =
  Format.fprintf ppf "{line=%d, bol=%d, offset=%d}" lo.line lo.bol lo.offset

let compare_line_offset lo1 lo2 =
  match Stdlib.compare lo1.line lo2.line with
  | 0 -> Stdlib.compare lo1.offset lo2.offset
  | n -> n

let order_line_offsets lo1 lo2 =
  if compare_line_offset lo1 lo2 <= 0
  then (lo1, lo2)
  else (lo2, lo1)

type line_range = {
    lstart : line_offset;
    lstop : line_offset;
  }

let line_range ~start ~stop = { lstart = start ; lstop = stop }
let pp_line_range ppf r =
  Format.fprintf ppf "{start=%a, stop=%a}"
    pp_line_offset r.lstart pp_line_offset r.lstop

let range_of_line_range lr =
  let lstart = lr.lstart in
  let start = lstart.bol + lstart.offset in
  let lstop = lr.lstop in
  let size = (lstop.bol + lstop.offset) - start in
  Rope.range ~start ~size

module Cursor =
  struct
    type gravity = [`Left | `Right]
    let pp_gravity ppf = function
    | `Left -> Format.pp_print_string ppf "L"
    | `Right -> Format.pp_print_string ppf "R"

    type t = {
        mutable offset : int;
        mutable leaf : Rope.leaf;
        mutable offset_in_leaf : int;
        gravity : gravity ;
      } (** A cursor should never point to offset_in_leaf 0 of a right
         branch of a rope node; Rope.move_in_rope should make sure of this. *)

    let create ~gravity ~offset ~leaf ~offset_in_leaf =
      { offset ; leaf ; offset_in_leaf ; gravity }

    let copy_position ~src ~dst =
      dst.offset <- src.offset ;
      dst.offset_in_leaf <- src.offset_in_leaf ;
      dst.leaf <- src.leaf

    let offset c = c.offset

    let pp ppf c =
      Format.fprintf ppf "{offset=%d, leaf=%a, offset_in_leaf=%d, g=%a}"
        c.offset Rope.pp_leaf c.leaf c.offset_in_leaf
        pp_gravity c.gravity

    module Id = Misc.Id()
    module Map = Map.Make(Id)
    module Set = Set.Make(Id)

    let pp_map ppf map =
      Map.iter (fun id c ->
         Format.fprintf ppf "[%a]%a\n" Id.pp id pp c)
        map
  end

module Cursor_map = Cursor.Map
type cursor_gravity = Cursor.gravity
type cursor = Cursor.Id.t
let compare_cursor = Cursor.Id.compare
let equal_cursor = Cursor.Id.equal
let pp_cursor_id = Cursor.Id.pp

module Region =
  struct
    type t = { rstart: Cursor.t; rstop: Cursor.t}
    let create rstart rstop = { rstart ; rstop }
    let pp ppf r = Format.fprintf ppf
      "{start=%a; stop=%a}" Cursor.pp r.rstart Cursor.pp r.rstop
    module Id = Misc.Id()
    module Map = Map.Make(Id)
    let pp_map ppf map =
      Map.iter (fun id c ->
         Format.fprintf ppf "[%a]%a\n" Id.pp id pp c)
        map
  end

type region = Region.Id.t

type change =
| Del of line_range * string
| Ins of line_range * string
| Group of change list

let rec pp_change ppf = function
| Del (range, str) -> Format.fprintf ppf "Del%a(%S)" pp_line_range range str
| Ins (range, str) -> Format.fprintf ppf "Ins%a(%S)" pp_line_range range str
| Group l ->
    Format.fprintf ppf "Group[";
    let len = List.length l in
    List.iteri (fun i c ->
       Format.fprintf ppf "%a%s" pp_change c (if i = len - 1 then "" else "; "))
       l;
    Format.fprintf ppf "]"

type _ Events.ev +=
| Delete_range : (line_range * string -> unit) Events.ev
| Insert_text : (line_range * string -> unit) Events.ev
| Cursor_moved : (cursor * line_offset -> unit) Events.ev
| Modified_changed : (bool -> unit) Events.ev
| Source_language_changed : (string option -> unit) Events.ev

type widget_intf = {
    widget : Widget.widget ;
    on_change : change -> unit ;
    on_cursor_change : cursor -> prev:line_offset -> now:line_offset -> unit ;
    on_tag_change : line_range list -> unit ;
    mutable wcursors : Cursor.Set.t ;
  }

type t = {
    o : Object.o ;
    rope: Rope.t ;
    mutable modified : bool ;
    mutable cursors : Cursor.t Cursor.Map.t ;
    mutable regions : Region.t Region.Map.t ;
    mutable lines : Rope.range array ; (** a line range includes the final '\n' if present *)
    mutable max_undo_levels : int ;
    mutable current_action : (int * change list) option ;
    mutable history : (bool * change) list * (bool * change) list ; (** left list for undo, right list for redo *)
    mutable widgets : widget_intf Oid.Map.t ;
    mutable last_used_cursor : Cursor.Id.t option ;
    mutable source_language : string option ;
    mutable word_char_re : Pcre.regexp ;
    mutable map_in : (Uchar.t -> Uchar.t) option ;
    mutable map_out : (Uchar.t -> Uchar.t) option ;
  }

let obj t = t.o
let lines t = t.lines
let line_count t = Array.length t.lines
let size t = Rope.rope_size t.rope
let rope t = t.rope
let max_undo_levels t = t.max_undo_levels
let last_used_cursor t = t.last_used_cursor

let pp_cursor b ppf id =
  match Cursor.Map.find_opt id b.cursors with
  | None -> Format.fprintf ppf "no cursor %a" Cursor.Id.pp id
  | Some c -> Format.fprintf ppf "%a %a" Cursor.Id.pp id Cursor.pp c

let cut_undo_history t =
  let n = t.max_undo_levels in
  let (undo, redo) = t.history in
  if List.length undo > n then
    let rec iter acc i = function
    | [] -> acc
    | h :: q when i >= n -> acc
    | h :: q -> iter (h::acc) (i+1) q
    in
    let undo = iter [] 0 undo in
    t.history <- (List.rev undo, redo)
  else
    ()
let set_max_undo_levels t n =
  t.max_undo_levels <- n ;
  cut_undo_history t

let reset_history t = t.history <- [], []

let signal_modified_changed t =
  t.o#trigger_unit_event Modified_changed t.modified

let signal_source_language_changed t =
  t.o#trigger_unit_event Source_language_changed t.source_language

let set_modified_ b t =
  if b <> t.modified then
    (
     t.modified <- b;
     signal_modified_changed t
    )

let modified t = t.modified
let set_modified t b =
  if t.modified = b then
    ()
  else
    (
     (* set in history *)
     let f (_,change) = (true, change) in
     let (undo, redo) = t.history in
     t.history <- (List.map f undo, List.map f redo) ;
     set_modified_ b t
    )

let line_of_offset =
  let rec iter (lines:Rope.range array) len offset left right =
    (*debug (fun m -> m "iter left=%d right=%d" left right) ;*)
    if left >= right then
      right
    else
      (
       let sum = left + right in
       let i = sum / 2 in
       (*debug (fun m -> m "lines.(%d) = %a" i Rope.pp_range lines.(i));*)
       let line = lines.(i) in
       if line.start + line.size <= offset then
         iter lines len offset (i+1) right
       else
         if offset < line.start then
           iter lines len offset left i
         else
           i
      )
  in
  fun t offset ->
    debug (fun m -> m "line_of_offset offset=%d" offset);
    let lines = t.lines in
    let len = Array.length lines in
    assert (len > 0);
    iter lines len offset 0 (len-1)

let line_char_of_offset t offset =
  let i = line_of_offset t offset in
  let line = t.lines.(i) in
  (i, offset - line.start)

let offset_of_line_char t ~line ~char =
  let nblines = Array.length t.lines in
  if line >= nblines then
    Rope.rope_size t.rope
  else
    let l = t.lines.(line) in
    (* line.size includes the \n, but we must not take it into
       account when addressing with (line,char), so we use
       line.size - 1, except if this is the last line *)
    let size = max 0
      (if line = nblines - 1 then l.size else l.size - 1)
    in
    l.start + min size char

let line_offset_of_offset t offset =
  let i = line_of_offset t offset in
  let line = t.lines.(i) in
  line_offset ~line:i ~bol:line.start ~offset:(offset - line.start)

let line_range_of_range t r =
  let start = line_offset_of_offset t r.Rope.start in
  let stop = line_offset_of_offset t (r.start + r.size) in
  line_range ~start ~stop

let line_ranges_of_ranges t ranges =
  let lineranges = List.map (line_range_of_range t) ranges in
  (* sort and merge line ranges *)
  let l = List.sort Stdlib.compare lineranges in
  let rec iter acc current = function
  | [] -> List.rev (current :: acc)
  | h::q ->
      if h.lstart.line <= current.lstop.line(* &&
        h.lstart.offset <= current.lstop.offset*)
      then
        iter acc { current with lstop = h.lstop } q
      else
        iter (current::acc) h q
  in
  match l with
  | [] -> []
  | h :: q ->
    let l = iter [] h q in
    (*prerr_endline (Printf.sprintf
       "DONE (%d ranges => %d line ranges)" (List.length ranges) (List.length l));*)
    l

let cursor_of_offset t ?(gravity=`Right) offset =
  let offset2 = min offset (Rope.rope_size t.rope) in
  if offset2 < offset then
    warn (fun m -> m "Textbuffer.cursor_of_offset: offset %d is too big, using %d"
      offset offset2);
  match Rope.leaf_at t.rope offset2 with
  | Some (offset_in_leaf, leaf) ->
      Cursor.create ~gravity ~offset ~leaf ~offset_in_leaf
  | None -> assert false

let get_cursor t id =
  match Cursor.Map.find_opt id t.cursors with
  | None ->
      err (fun m -> m "Invalid cursor %a" Cursor.Id.pp id);
      None
  | x -> x

let add_cursor_to_widget t = function
| None -> (fun _ -> ())
| Some wid ->
    fun cid ->
      match Oid.Map.find_opt wid t.widgets with
      | None ->
          Log.warn (fun m -> m "Widget %a not registered to buffer" Oid.pp wid)
      | Some w -> w.wcursors <- Cursor.Set.add cid w.wcursors

let create_cursor ?widget ?gravity ?(line=0) ?(char=0) ?offset t =
  let c =
    match offset with
    | Some o -> cursor_of_offset t ?gravity o
    | None ->
        let offset = offset_of_line_char t ~line ~char in
        cursor_of_offset t ?gravity offset
  in
  let id = Cursor.Id.gen() in
  t.cursors <- Cursor.Map.add id c t.cursors;
  add_cursor_to_widget t widget id ;
  id

let set_last_used_cursor t c = t.last_used_cursor <- Some c

let dup_cursor t ?widget ?gravity c =
  let gravity = Option.value ~default:c.Cursor.gravity gravity in
  let id = Cursor.Id.gen() in
  let c = { c with gravity } in
  t.cursors <- Cursor.Map.add id c t.cursors;
  add_cursor_to_widget t widget id ;
  id

let remove_cursor t id =
  t.cursors <- Cursor.Map.remove id t.cursors;
  match t.last_used_cursor with
  | Some i when Cursor.Id.equal id i -> t.last_used_cursor <- None
  | _ -> ()

let create_insert_cursor ?widget t =
  match t.last_used_cursor with
  | None -> create_cursor ?widget ~offset:0 t
  | Some id ->
      match get_cursor t id with
      | None -> create_cursor ?widget ~offset:0 t
      | Some c -> dup_cursor t ?widget ~gravity:`Right c

let dup_cursor t ?widget ?gravity id =
  match get_cursor t id with
  | None -> None
  | Some c -> Some (dup_cursor t ?widget ?gravity c)

let register_widget =
  let f intf t =
    let id = intf.widget#id in
    if Oid.Map.mem id t.widgets then
      (
       Log.warn
         (fun m -> m "Widget %s already registered in textbuffer" intf.widget#me);
       None
      )
    else
      (
       t.widgets <- Oid.Map.add id intf t.widgets;
       (* and return a cursor *)
       Some (create_insert_cursor ~widget:intf.widget#id t)
      )
  in
  fun t ~widget ~on_change ~on_cursor_change ~on_tag_change ->
    let intf = {
        widget ; on_change ; on_cursor_change ;
        on_tag_change ; wcursors = Cursor.Set.empty }
    in
    f intf t

let unregister_widget t w =
  let id = w#id in
  match Oid.Map.find_opt id t.widgets with
  | None ->
      Log.warn (fun m -> m "Widget %s was not registered in textbuffer" w#me)
  | Some w ->
      Cursor.Set.iter (remove_cursor t) w.wcursors ;
      w.wcursors <- Cursor.Set.empty ;
      t.widgets <- Oid.Map.remove id t.widgets

let map_string f str =
  match f with
  | None -> str
  | Some f -> Utf8.map f str

let to_string ?(start=0) ?size t =
  if start < 0 then invalid_arg "Textbuffer.to_string" ;
  let str =
    match start, size with
    | 0, None -> Rope.to_string t.rope
    | _ ->
        let size = match size with
          | None -> Rope.rope_size t.rope - start
          | Some s -> s
        in
        Rope.sub_to_string ~start ~size t.rope
  in
  map_string t.map_out str

let chars ~map_out ?(start=0) ?size t =
  if start < 0 then invalid_arg "Textbuffer.chars" ;
  let size =
    match size with
    | None -> Rope.rope_size t.rope - start
    | Some s -> s
  in
  let l = Rope.sub_to_chars ~start ~size t.rope in
  match t.map_out with
  | None -> l
  | Some _ when not map_out -> l
  | Some f -> List.map (fun (c, tags) -> (f c, tags)) l

let get_line t i =
  let len = Array.length t.lines in
  if i < 0 || i >= len then
    invalid_arg
      (Printf.sprintf "Textbuffer.line_to_string i=%d, len=%d" i len)
  else
    t.lines.(i)

let line_chars ~map_out t i =
  let len = Array.length t.lines in
  let range = get_line t i in
  (* last line has no \n *)
  let size = if i = len - 1 then range.size else range.size - 1 in
  chars ~map_out ~start:range.start ~size t

let line_to_string t i =
  let len = Array.length t.lines in
  let range = get_line t i in
  (* last line has no \n *)
  let size = if i = len - 1 then range.size else range.size - 1 in
  let s = to_string ~start:range.start ~size t in
  debug (fun m -> m "line %d: %S" i s);
  s

let create ?source_language ?word_char_re () =
  let word_char_re =
    match word_char_re with
    | Some re -> re
    | None -> default_word_char_re
  in
  let t =
    { o = new Object.o () ;
      rope = Rope.create () ;
      modified = false ;
      cursors = Cursor.Map.empty ;
      lines = Array.make 1 (Rope.range ~start:0 ~size:0);
      regions = Region.Map.empty ;
      current_action = None ;
      history = [], [] ;
      max_undo_levels = 100 ;
      widgets = Oid.Map.empty ;
      last_used_cursor = None ;
      source_language ;
      word_char_re ;
      map_in = None ;
      map_out = None ;
    }
  in
  t


let set_map_in t f = t.map_in <- f
let set_map_out t f = t.map_out <- f

let connect t ev cb = t.o#connect ev cb
let disconnect t cbid = t.o#disconnect cbid

let pcre_match_char rex str = Pcre.(pmatch ~rex str)

let pp_lines ppf lines =
  Array.iteri
    (fun i r -> Format.fprintf ppf "[%d]%a, " i Rope.pp_range r)
    lines

let pp ppf t =
  Format.fprintf ppf
    "{ rope = %a\n  cursors = %a;\n  lines = %a;\n  regions = %a}"
    Rope.pp t.rope Cursor.pp_map t.cursors pp_lines t.lines
    Region.pp_map t.regions

let check =
  let rec check_lines (lines : Rope.range array) len i start =
    if i >= len then
      ()
    else
      (
       let line = lines.(i) in
       if line.start <> start then
         err (fun m -> m "Line %d: start = %d, instead of expected %d"
            i line.start start);
       if line.size < 0 then
         err (fun m -> m "Line %d: size = %d < 0" i line.size);
       check_lines lines len (i+1) (line.start + line.size)
      )
  in
  let check_cursors rope t =
    let f id (c:Cursor.t) =
      match Rope.leaf_offset c.leaf with
      | None ->
          err (fun m -> m "Cursor %a has leaf ouf of rope" Cursor.pp c)
      | Some leaf_off ->
          let off = c.offset - c.offset_in_leaf in
          if off <> leaf_off then
            err (fun m ->
               let file = Filename.temp_file "ropedump" ".txt" in
               let oc = Stdlib.open_out file in
               let ppf = Format.formatter_of_out_channel oc in
               Rope.pp ppf rope;
               Format.pp_print_flush ppf ();
               close_out oc;
               m "Cursor %a has offset %d but its leaf has offset %d (rope dump in %s)"
                 Cursor.Id.pp id off leaf_off file)
    in
    Cursor.Map.iter f t.cursors
  in
  fun t ->
    Rope.check t.rope;
    check_lines t.lines (Array.length t.lines) 0 0;
    check_cursors t.rope t

let update_cursor t c =
  let at = min (Rope.rope_size t.rope) c.Cursor.offset in
  match Rope.leaf_at t.rope at with
  | None -> assert false
  | Some (offset_in_leaf, leaf) ->
      c.offset <- at ;
      c.offset_in_leaf <- offset_in_leaf;
      c.leaf <- leaf

let cursor_offset t id =
  match get_cursor t id with
  | None -> 0
  | Some c -> Cursor.offset c

let cursor_line_offset t id =
  line_offset_of_offset t (cursor_offset t id)

let create_region ?(start_gravity=`Left) ~start ?(stop_gravity=`Right) ~stop t =
  let start = cursor_of_offset t ~gravity:start_gravity start in
  let stop = cursor_of_offset t ~gravity:stop_gravity stop in
  let id = Region.Id.gen () in
  let r = Region.create start stop in
  t.regions <- Region.Map.add id r t.regions;
  id

let remove_region t id =
  t.regions <- Region.Map.remove id t.regions

let regions_by_offset =
  let pred offset r =
    let comp_start =
      match r.Region.rstart.gravity with
      | `Left -> (>=)
      | `Right -> (>)
    in
    let comp_stop =
      match r.Region.rstop.gravity with
      | `Left -> (<)
      | `Right -> (<=)
    in
    (comp_start offset r.rstart) && (comp_stop offset r.rstop)
  in
  fun t offset ->
    Region.Map.fold
      (fun _ r acc -> if pred offset r then r :: acc else acc)
      t.regions []

let line_ranges_from_string ?(start=0) str =
  let (r,l,last_is_nl) = Uutf.String.fold_utf_8
    (fun ((current:Rope.range),acc,last_is_nl) _pos -> function
       | `Malformed str ->
           warn (fun m -> m "Textbuffer.line_ranges_from_string: malformed char %S" str);
           { current with size = current.size + 1 }, acc, false
       | `Uchar c ->
          let current = { current with size = current.size + 1 } in
           match Uchar.to_int c with
           | 10 (* '\n' *) ->
               let acc = current :: acc in
               { start = current.start + current.size ; size = 0 }, acc, true
           | _ ->
               current, acc, false
    )
      ({ start ; size = 0 }, [], false) str
  in
  let l = if r.size > 0 || last_is_nl then r :: l else l in
  let l = List.rev l in
  debug (fun m -> m "line_ranges_from_string start=%d str=%S" start str);
  List.iter (fun r -> debug (fun m -> m "%a" Rope.pp_range r)) l;
  l

let update_cursor_after_insert t (range : Rope.range) c =
  let comp = match c.Cursor.gravity with
    | `Left -> (>)
    | `Right -> (>=)
  in
  let prev = line_offset_of_offset t c.offset in
  if comp c.offset range.start then
    (
     (*warn (fun m -> m "update_cursor_after_insert range=%a c=%a"
       Rope.pp_range range Cursor.pp c);*)
     c.offset <- c.offset + range.size ;
     if c.offset_in_leaf + range.size <= c.leaf.size then
       (* if cursor can 'stay' in its leaf, update only
          offset and offset_in_leaf *)
       c.offset_in_leaf <- c.offset_in_leaf + range.size
     else
       (* else we get new leaf from new offset *)
       update_cursor t c;
     let now = line_offset_of_offset t c.offset in
     Some (prev, now)
    )
  else
    (
     (** Rope leaf the cursor was pointing to may have
        been splitted; In this case, cursor leaf must be
        updated. This can only happen when
        (range.start - c.coffset <= !Rope.max_leaf_size).
        In this case, we update the cursor but do not report
        change (its position did not change).
        *)
     if range.start - c.offset <= !Rope.max_leaf_size then
       update_cursor t c;
     None
    )

let update_cursors_after_insert t range =
  Cursor.Map.fold
    (fun id c acc ->
       match update_cursor_after_insert t range c with
       | None -> acc
       | Some change -> (id, change) :: acc
    ) t.cursors []

let update_regions_after_delete t range =
  Region.Map.iter
    (fun _ r ->
       (* FIXME: gather and return changes *)
       let _ = update_cursor_after_insert t range r.Region.rstart in
       let _ = update_cursor_after_insert t range r.Region.rstop in
       ()
    )
    t.regions

let update_lines_after_insert t (range:Rope.range) str =
  let nb_old_lines = Array.length t.lines in
  let ranges = line_ranges_from_string ~start:range.start str in
  match ranges with
  | [] -> ()
  | _ ->
      let nb_new_lines = List.length ranges - 1 in
      let nb_lines = nb_old_lines + nb_new_lines in
      debug (fun m -> m "update_lines_after_insert %a %S\nnb_old_lines=%d, nb_new_lines=%d, nb_lines=%d"
         Rope.pp_range range str nb_old_lines nb_new_lines nb_lines);
      debug (fun m -> m "t.lines=");
      debug (fun m -> Array.iter (fun l -> m "%a" Rope.pp_range l) t.lines);

      let lines = Array.make nb_lines Rope.zero_range in
      let start_line = line_of_offset t range.start in

      (* ranges should be copied/modified to these lines at the end:
         [0..start_line-1 : old lines
         [start_line..start_line+nb_new_lines] : merged/inserted lines
         [start_line+nb_new_lines..nb_lines] : old lines with start position updated
         *)

      Array.blit t.lines 0 lines 0 start_line ;
      debug (fun m -> m "lines array blit ok, start_line=%d" start_line);

      let next_start =
        match ranges with
        | [] -> assert false
        | [r] ->
            let line = t.lines.(start_line) in
            let line = { line with size = line.size + r.size } in
            lines.(start_line) <- line ;
            debug (fun m -> m "update_lines_after_insert: one_range %a, lines.(%d) <- %a"
               Rope.pp_range r start_line Rope.pp_range line);
            line.start + line.size
        | _ ->
            let rec iter ~i ~start = function
            | [] -> start
            | (r:Rope.range) :: q ->
                (*debug (fun m -> m "iter i=%d start=%d r=%a" i start Rope.pp_range r);*)
                let r =
                  if i = 0 then
                    ( (* add line to beginning of old line; start of old line is
                        start parameter, since i = 0 *)
                     let size = r.start + r.size - start in
                     Rope.range ~start ~size
                    )
                  else
                    match q with
                    | [] ->
                        (* merge line with end of old line where insertion took place *)
                        let old_r = t.lines.(start_line) in
                        let size = r.size + old_r.size - (range.start - old_r.start) in
                        (*debug (fun m -> m "merging line with end of old line: old_r=%a" Rope.pp_range old_r);*)
                        { start ; size }
                    | _ -> r
                in
                (*debug (fun m -> m "lines.(%d) <- %a" (start_line+i) Rope.pp_range r);*)
                lines.(start_line + i) <- r;
                assert (start=r.start);
                iter ~i:(i+1)  ~start:(r.start + r.size) q
            in
            iter ~i:0 ~start:(t.lines.(start_line).start) ranges
      in
      let rec iter start i =
        let old_p = start_line + 1 + i in
        (*debug (fun m -> m "iter start=%d i=%d old_p=%d" start i old_p);*)
        if old_p >= nb_old_lines then
          ()
        else
          (
           let old_r = t.lines.(old_p) in
           let r = { old_r with start } in
           (*debug (fun m -> m "t.lines.(%d) = %a" old_p Rope.pp_range r);*)
           lines.(old_p + nb_new_lines) <- r;
           iter (start + r.size) (i+1)
          )
      in
      iter next_start 0;
      t.lines <- lines;
      debug (fun m -> m "lines set, result: %a" pp t)

let update_after_insert t range str =
  update_lines_after_insert t range str;
  update_cursors_after_insert t range

let flatten_changes =
  let rec iter acc = function
  | Del (r, str) -> Del (r, str) :: acc
  | Ins (r, str) -> Ins (r, str) :: acc
  | Group l -> List.fold_left iter acc l
  in
  fun c -> List.rev (iter [] c)

let raw_offset_in_editable_region offset t =
  true

let signal_changes_to_widget changes w =
  let f c =
    try w.on_change c
    with e ->
        Log.warn (fun m -> m "When signaling %a to %s: %s\n%s"
           pp_change c w.widget#me
             (Printexc.to_string e)
             (Printexc.get_backtrace ()))
  in
  List.iter f changes

let trigger_change_event t = function
| Del (r,str) -> t.o#trigger_unit_event Delete_range (r, str)
| Ins (r,str) -> t.o#trigger_unit_event Insert_text (r, str)
| Group _ -> ()

let rec map_change_out f = function
| Del (r, str) -> Del (r, Utf8.map f str)
| Ins (r, str) -> Ins (r, Utf8.map f str)
| Group g -> Group (List.map (map_change_out f) g)

let signal_change t changes =
  let changes = flatten_changes changes in
  let changes =
    match t.map_out with
    | None -> changes
    | Some f -> List.map (map_change_out f) changes
  in
  let l = Oid.Map.fold (fun _ w acc -> w::acc) t.widgets [] in
  List.iter (signal_changes_to_widget changes) l;
  List.iter (trigger_change_event t) changes

let signal_changes t changes =
  List.iter (signal_change t) changes

let signal_tag_change_to_widget ranges w =
  try w.on_tag_change ranges
  with e ->
      Log.warn (fun m -> m "When signaling tag changes to %s: %s\n%s"
         w.widget#me
           (Printexc.to_string e)
           (Printexc.get_backtrace ()))

let[@landmark] signal_tag_change t ranges =
  let l = Oid.Map.fold (fun _ w acc -> w::acc) t.widgets [] in
  List.iter (signal_tag_change_to_widget ranges) l

let signal_cursor_changes_to_widget changes w =
  let f (c,(prev,now)) =
    try
      if prev <> now && Cursor.Set.mem c w.wcursors then
        w.on_cursor_change c ~prev ~now
      else
        ()
    with e ->
        Log.warn (fun m ->
           m "When signaling cursor_change ~prev:%a ~now:%a to %s: %s\n%s"
           pp_line_offset prev
           pp_line_offset now
             w.widget#me
             (Printexc.to_string e)
             (Printexc.get_backtrace ()))
  in
  List.iter f changes

let signal_cursor_changes t changes =
  let l = Oid.Map.fold (fun _ w acc -> w::acc) t.widgets [] in
  List.iter (signal_cursor_changes_to_widget changes) l;
  List.iter (fun (c,(_,lo)) -> t.o#trigger_unit_event Cursor_moved (c,lo)) changes

let move_cursor t ?(line=0) ?(char=0) ?offset id =
  match get_cursor t id with
  | None -> None
  | Some c ->
      let prev = line_offset_of_offset t c.offset in
      (
       match offset with
       | Some o -> c.offset <- min o (Rope.rope_size t.rope)
       | None ->
           let offset = offset_of_line_char t ~line ~char in
           c.offset <- offset
      );
      update_cursor t c;
      let now = line_offset_of_offset t c.offset in
      signal_cursor_changes t [id,(prev,now)] ;
      Some (now.bol + now.offset)

let move_cursor_to_cursor t ~src ~dst =
  match get_cursor t src with
  | None -> None
  | Some src ->
      let id_dst = dst in
      match get_cursor t dst with
      | None -> None
      | Some dst ->
          let prev = line_offset_of_offset t dst.offset in
          Cursor.copy_position ~src ~dst;
          let now = line_offset_of_offset t dst.offset in
          signal_cursor_changes t [id_dst,(prev,now)] ;
          Some (now.bol + now.offset)

let move_cursor_to_line_start t id =
  match get_cursor t id with
  | None -> None
  | Some c ->
      let (line, char) = line_char_of_offset t c.Cursor.offset in
      match char with
      | 0 -> None
      | _ -> move_cursor t ~line ~char:0 id

let move_cursor_to_line_end t id =
  match get_cursor t id with
  | None -> None
  | Some c ->
      let line_i = line_of_offset t c.Cursor.offset in
      let nblines = Array.length t.lines in
      let line = t.lines.(line_i) in
      let offset =
        if line_i + 1 >= nblines then
          (* last line, no \n at the end *)
          line.start + line.size
        else
          line.start + line.size - 1
      in
      move_cursor t ~offset id

let line_forward_cursor t id n =
  match get_cursor t id with
  | None -> None
  | Some c ->
      let (line, char) = line_char_of_offset t c.Cursor.offset in
      move_cursor t ~line:(max 0 (line+n)) ~char id

let line_backward_cursor t c n = line_forward_cursor t c (- n)

let forward_cursor t cid n =
  match get_cursor t cid with
  | None -> None
  | Some c ->
      let prev = line_offset_of_offset t c.offset in
      let target = max 0 (c.offset + n) in
      let pos = c.offset - c.offset_in_leaf in
      let rope = Rope.Leaf c.leaf in
      debug (fun m -> m "cursor_forward: move_in_rope ~target:%d ~pos:%d" target pos);
      let (offset, offset_in_leaf, leaf) = Rope.move_in_rope ~target ~pos rope in
      c.leaf <- leaf;
      c.offset <- offset;
      c.offset_in_leaf <- offset_in_leaf ;
      let now = line_offset_of_offset t c.offset in
      signal_cursor_changes t [cid,(prev,now)] ;
      Some (now.bol + now.offset)

let backward_cursor t c n = forward_cursor t c (- n)

let look_for_char_from =
  let chunk_size = 10 in
  let rec iter rope rsize pred start size s i =
    if size = 0 then
      None
    else
      let char = Utf8.sub s ~pos:i ~len:1 in
      if not (pred char) then
        let i = i + 1 in
        if i < size then
          iter rope rsize pred start size s i
        else
          let start = start + size in
          let size = min chunk_size (rsize - start) in
          let s = Rope.sub_to_string ~start ~size rope in
          iter rope rsize pred start size s 0
      else
        Some (start+i)
  in
  fun t pred start ->
    let rope_size = Rope.rope_size t.rope in
    let size = min chunk_size (rope_size - start) in
    let s = Rope.sub_to_string ~start ~size t.rope in
    iter t.rope rope_size pred start size s 0

let forward_cursor_to_word_end t cid =
  match get_cursor t cid with
  | None -> None
  | Some c ->
      let pos0 = c.offset in
      let rope_size = Rope.rope_size t.rope in
      if pos0 >= rope_size then
        Some c.offset
      else
        (* look forward for first word char *)
        let pos =
          match look_for_char_from t
            (fun c -> pcre_match_char t.word_char_re c)
            pos0
          with
          | None -> pos0
          | Some pos ->
              (* if we found a word char, let's look for the first
                 char not being a word char *)
              match look_for_char_from t
                (fun c -> not (pcre_match_char t.word_char_re c))
                  pos
              with
              | None -> (* go to end of rope *) rope_size
              | Some p -> p
        in
        (* then move forward cursor to new pos *)
        forward_cursor t cid (pos-pos0)

let look_back_for_char_from =
  let chunk_size = 10 in
  let rec iter rope rsize pred start size s i =
    if i < 0 then
      None
    else
      let char = Utf8.sub s ~pos:i ~len:1 in
      if not (pred char) then
        let i = i - 1 in
        if i >= 0 then
          iter rope rsize pred start size s i
        else
          let s_start = max 0 (start - chunk_size) in
          let size = start - s_start in
          let s = Rope.sub_to_string ~start:s_start ~size rope in
          iter rope rsize pred s_start size s (size-1)
      else
        Some (start+i)
  in
  fun t pred start ->
    let rope_size = Rope.rope_size t.rope in
    let s_start = max 0 (start - chunk_size) in
    let size = start - s_start in
    if size <= 0 then
      None
    else
      let s = Rope.sub_to_string ~start:s_start ~size t.rope in
      iter t.rope rope_size pred s_start size s (size-1)

let backward_cursor_to_word_start t cid =
  match get_cursor t cid with
  | None -> None
  | Some c ->
      let pos0 = c.offset in
      if pos0 <= 0 then
        Some c.offset
      else
        let pos =
          (* look backward for first word char *)
          match look_back_for_char_from t
            (fun c -> pcre_match_char t.word_char_re c)
              pos0
          with
          | None -> pos0
          | Some pos ->
              (* if we found a word char, let's look for the
                 first char not being a word char *)
              match look_back_for_char_from t
                (fun c -> not (pcre_match_char t.word_char_re c))
                  pos
              with
              | None -> (* go to rope start*) 0
              | Some p -> p + 1
        in
        (* then move forward cursor to new pos (negative forward here) *)
        forward_cursor t cid (pos-pos0)

let[@landmark] apply_lang_ lang t =
  let ranges = Rope.apply_lang t.rope lang in
  List.map (line_range_of_range t) ranges

let[@landmark] apply_lang t lang =
  signal_tag_change t (apply_lang_ lang t)

let can_insert t ?readonly offset =
  match readonly with
  | None -> true
  | Some ro ->
      let rsize = Rope.rope_size t.rope in
      let (tags1, tags2) =
        if offset < 0 || offset > rsize then
          invalid_arg (Printf.sprintf "Textbuffer.raw_can_insert (at=%d, rsize=%d)"
           offset rsize)
        else
          if offset = 0 then
            if rsize = 0 then
              (None, None)
            else
              let (_,(t,_)) = Rope.get t.rope offset in
              (None, Some t)
          else
            let (_,(t1,_)) = Rope.get t.rope (offset-1) in
            if offset >= rsize then
              (Some t1, None)
            else
              let (_,(t2,_)) = Rope.get t.rope offset in
              (Some t1, Some t2)
      in
      not (ro tags1 tags2)

let insert_at_cursor t cursor ?readonly ?tags str =
  match get_cursor t cursor with
  | None -> ()
  | Some c when not (can_insert t ?readonly c.offset) -> ()
  | Some c ->
      let str = map_string t.map_in str in
      let tags = Option.map Texttag.TSet.of_list tags in
      let old_offset = c.offset in
      let lstart = line_offset_of_offset t old_offset in
      debug (fun m -> m
         "Textbuffer.insert_at_cursor: leaf=%a offset=%d, offset_in_leaf=%d, lstart=%a"
           Rope.pp_leaf c.leaf old_offset c.offset_in_leaf pp_line_offset lstart);
      let size = Rope.insert_at_leaf c.leaf ?tags str c.offset_in_leaf in
        debug (fun m -> m "insert_at_cursor: old_offset=%d, size inserted=%d, c.offset=%d"
         old_offset size c.offset);
      let range = Rope.range ~start:old_offset ~size in
      let cursor_changes = update_after_insert t range str in
      let lstop = line_offset_of_offset t (old_offset+size) in
      let lrange = { lstart ; lstop } in
      let cursor_changes =
        match c.gravity with
        | `Left -> cursor_changes
        | `Right -> (cursor, (lstart, lstop)) :: cursor_changes
      in
      let change = Ins (lrange, str) in
      (match t.current_action with
       | None ->
           let (undo, redo) = t.history in
           t.history <- ((t.modified, change) :: undo), [] ;
           cut_undo_history t;
           set_modified_ true t
       | Some (count, l) -> t.current_action <- Some (count, change :: l)
      );
      signal_change t change ;
      signal_cursor_changes t cursor_changes ;
      (
       match t.source_language with
       | None -> ()
       | Some lang -> apply_lang t lang
      )

let insert_ ~from_history ?tags at str t =
  let tags = Option.map Texttag.TSet.of_list tags in
  debug (fun m -> m "Rope.raw_insert ~str:%S ~at: %d" str at);
  let lstart = line_offset_of_offset t at in
  let size = Rope.insert_string t.rope ?tags str at in
  let range = Rope.range ~start:at ~size in
  let cursor_changes = update_after_insert t range str in
  let lstop = line_offset_of_offset t (at+size) in
  let lrange = { lstart ; lstop } in
  let change = Ins (lrange, str) in
  if not from_history then
    (
     match t.current_action with
     | None ->
         let (undo, _) =  t.history in
         t.history <- ((t.modified, change) :: undo, []) ;
         cut_undo_history t;
         set_modified_ true t
     | Some (count, l) ->
         t.current_action <- Some (count, change :: l)
    )
  else
    ();
  (*check t;*)
  (change, cursor_changes)

let insert t ?readonly ?tags at str =
  let str = map_string t.map_in str in
  debug (fun m -> m "Textbuffer.insert at=%d %S" at str);
  if can_insert t ?readonly at then
    (
     let (change, cursor_changes) = insert_ ~from_history:false ?tags at str t in
     signal_change t change ;
     signal_cursor_changes t cursor_changes ;
     match t.source_language with
     | None -> ()
     | Some lang -> apply_lang t lang
    )
  else
    ()

let update_cursor_after_delete t (range:Rope.range) c =
  (*warn (fun m -> m "update_after_cursor_delete range=%a cursor=%a"
    Rope.pp_range range Cursor.pp c);*)
  if c.Cursor.offset <= range.start then
    (
     (*warn (fun m -> m "update_after_cursor_delete: no change");*)
     None
    )
  else
    (
     let prev = line_offset_of_offset t c.offset in
     (* it could be optimized but let's update cursor data
        from root of rope by now *)
     if c.offset >= range.start + range.size then
       c.offset <- c.offset - range.size
     else
       c.offset <- range.start;
     update_cursor t c;
     let now = line_offset_of_offset t c.offset in
     (*warn (fun m -> m "update_after_cursor_delete: now = %a" Cursor.pp c);*)
     Some (prev, now)
    )

let update_cursors_after_delete t range =
  Cursor.Map.fold
    (fun id c acc ->
       match update_cursor_after_delete t range c with
       | None -> acc
       | Some change -> (id, change) :: acc
    ) t.cursors []

let update_regions_after_delete t range =
  Region.Map.iter
    (fun _ r ->
      (* FIXME: gather and return cursor changes *)
       let _ = update_cursor_after_delete t range r.Region.rstart in
       let _ = update_cursor_after_delete t range r.Region.rstop in
       ()
    )
    t.regions

let update_lines_after_delete t (range:Rope.range) str =
  let nb_old_lines = Array.length t.lines in
  match line_ranges_from_string ~start:range.start str with
  | [] -> ()
  | ranges ->
      let nb_del_lines = List.length ranges in
      let nb_lines = nb_old_lines - nb_del_lines + 1 in

      debug (fun m -> m "update_lines_after_delete %a %S\nnb_old_lines=%d, nb_del_lines=%d, nb_lines=%d"
         Rope.pp_range range str nb_old_lines nb_del_lines nb_lines);

      let lines = Array.sub t.lines 0 nb_lines in
      let start_line = line_of_offset t range.start in

      let merged_line =
        let old_r = t.lines.(start_line) in
        match ranges with
        | [] -> assert false
        | [line] -> { old_r with size = old_r.size - line.size }
        | first :: q ->
            let last =
              match List.rev q with [] -> assert false | x :: _ -> x
            in
            let old_last = t.lines.(start_line + nb_del_lines - 1) in
            let last_remaining = old_last.size - last.size in
            { old_r with size = old_r.size - first.size + last_remaining }
      in
      debug (fun m -> m "merged_line at %d: %a" start_line Rope.pp_range merged_line);
      lines.(start_line) <- merged_line ;
      let rec iter ~i ~start =
        debug (fun m -> m "iter i=%d start=%d" i start);
        if i >= nb_lines then
          ()
        else
          (
           let old_line = t.lines.(i + nb_del_lines - 1) in
           lines.(i) <- { old_line with start };
           iter ~i:(i+1) ~start:(start+old_line.size)
          )
      in
      iter ~i:(start_line+1) ~start:(merged_line.start+merged_line.size);
      debug (fun m -> m "lines set in buffer:");
      Array.iter (fun l -> debug (fun m -> m "%a" Rope.pp_range l)) lines;
      t.lines <- lines;
      debug (fun m -> m "lines set, result: %a" pp t)

let update_after_delete t range str =
  update_lines_after_delete t range str;
  update_cursors_after_delete t range

let merge_cursor_changes =
  let module M = Cursor.Map in
  let add map (cid, (prev, now)) =
    match M.find_opt cid map with
    | None -> M.add cid (prev, now) map
    | Some (prev,_) -> M.add cid (prev, now) map
  in
  fun changes ->
    let m = List.fold_left add M.empty changes in
    let l = M.bindings m in
    (*let pp ppf l =
      List.iter
        (fun (cid, (prev,now)) ->
           Format.fprintf ppf "(%a: %a -> %a) " Cursor.Id.pp cid
             pp_line_offset prev pp_line_offset now) l
    in
    warn (fun m -> m "merge_cursor_changes: %a@.=> %a" pp changes pp l);*)
    l

let delete_range_ (start,size) t =
  let lstart = line_offset_of_offset t start in
  let lstop = line_offset_of_offset t (start+size) in
  let str = Rope.delete t.rope ~start ~size in
  let lrange = { lstart ; lstop } in
  let range =
    let size = (lstop.bol + lstop.offset) - (lstart.bol + lstart.offset) in
    Rope.range ~start ~size
  in
  let cursor_changes = update_after_delete t range str in
  let change = Del (lrange, str) in
  (change, cursor_changes)

let deletable_ranges readonly start chars =
  let rec iter acc start size = function
  | [] ->
      if size > 0 then
        List.rev ((start, size) :: acc)
      else
        List.rev acc
  | (c,(tags,_)) :: q ->
      let ro = readonly tags in
      debug (fun m -> m "character %S is read-only: %b"
        (Utf8.string_of_uchar c) ro);
      if ro then
        if size > 0 then
          iter ((start,size)::acc) (start+size+1) 0 q
        else
          iter acc (start+1) 0 q
      else
        iter acc start (size+1) q
  in
  debug (fun m -> m "Textview.deletable_ranges start=%d len(char)s=%d"
    start (List.length chars));
  let ranges = iter [] start 0 chars in
  debug (fun m ->
    List.iter
      (fun (start, size) -> m "deletable range: start=%d, size=%d" start size)
      ranges);
  ranges

let delete_ ?readonly ~from_history ~start ~size t =
  debug (fun m -> m "Rope.delete_ ~start:%d ~size: %d" start size);
  let ranges =
    match readonly with
    | None -> [start, size]
    | Some ro ->
        let chars = Rope.sub_to_chars ~start ~size t.rope in
        deletable_ranges ro start chars
  in
  let (changes, cursor_changes, _) =
    (* do not forget that when removing a range, we must translate
       the start position of the next one by the deleted size. We
       use the offset accumulator to do so. *)
    List.fold_left
      (fun (acc_ch, acc_ch_cur, offset) (start, size) ->
         let start = start - offset in
         let (ch, ch_cur) = delete_range_ (start, size) t in
         (ch :: acc_ch, ch_cur :: acc_ch_cur, offset+size)
      )
      ([], [], 0) ranges
  in
  let cursor_changes = merge_cursor_changes
    (List.flatten (List.rev cursor_changes))
  in
  let change =
    match List.rev changes with
    | [x] -> x
    | l -> Group l
  in
  if not from_history then
    (
    match t.current_action with
     | None ->
         let (undo, _) =  t.history in
         t.history <- ((t.modified, change) :: undo, []);
         cut_undo_history t ;
         set_modified_ true t
     | Some (count, l) ->
         t.current_action <- Some (count, change :: l)
    )
  else
    ();
  (*check t;*)
  (change, cursor_changes)

let set_text t str =
  let str = map_string t.map_in str in
  let size = size t in
  let (del_change, del_cursor_changes) = delete_ ~from_history:true ~start:0 ~size t in
  let (ins_change, _) = insert_ ~from_history:true 0 str t in
  let change = Group [ del_change ; ins_change ] in
  (match t.current_action with
   | None ->
       let undo = (t.modified, change) :: (fst t.history) in
       t.history <- (undo, []) ;
       cut_undo_history t ;
       set_modified_ true t
   | Some (count, l) ->
       t.current_action <- Some (count, change :: l)
  );
  signal_change t change ;
  signal_cursor_changes t del_cursor_changes ;
  match t.source_language with
  | None -> ()
  | Some lang -> apply_lang t lang

let delete ?readonly ?(start=0) ?size t =
  let size = match size with None -> Rope.rope_size t.rope | Some s -> s in
  let (change, cursor_changes) =
    delete_ ~from_history:false ?readonly ~start ~size t
  in
  signal_change t change ;
  signal_cursor_changes t cursor_changes ;
  let () =
    match t.source_language with
    | None -> ()
    | Some lang -> apply_lang t lang
  in
  let str =
    match change with
    | Del (_, str) -> str
    | Group l ->
        let b = Buffer.create 256 in
        List.iter
          (function
           | Del (_,str) -> Buffer.add_string b str
           | _ -> assert false)
          l;
        Buffer.contents b
    | _ -> assert false
  in
  map_string t.map_out str

let add_tag t tag ?(start=0) ?(size=size t) () =
  Rope.add_tag t.rope tag ~start ~size ;
  let range = Rope.range ~start ~size in
  let line_range = line_range_of_range t range in
  signal_tag_change t [line_range]

let remove_tag t tag ?(start=0) ?(size=size t) () =
  Rope.remove_tag t.rope tag ~start ~size ;
  let range = Rope.range ~start ~size in
  let line_range = line_range_of_range t range in
  signal_tag_change t [line_range]

let source_language t = t.source_language
let[@landmark] set_source_language t l =
  (* make sure the language is known *)
  match Option.map Higlo.Lang.get_lexer l with
  | exception e ->
      warn
        (fun m -> m "Cannot set source language: %s"
           (Printexc.to_string e))
  | _ ->
      if t.source_language <> l then
        (
         t.source_language <- l;
         let ranges =
           match l with
           | None ->
               Rope.remove_lang_tags t.rope ;
               [line_range_of_range t
                 (Rope.range ~start:0 ~size:(Rope.rope_size t.rope))]
           | Some lang -> apply_lang_ lang t
         in
         signal_tag_change t ranges;
         signal_source_language_changed t
        )

let word_char_re t = t.word_char_re
let set_word_char_re t re = t.word_char_re <- re
let set_word_char_re_string t str =
  let re = Pcre.(regexp ~iflags:(cflags [`UTF8]) str) in
  t.word_char_re <- re

let begin_action t =
  match t.current_action with
  | None -> t.current_action <- Some (1, [])
  | Some (count, l) -> t.current_action <- Some (count + 1, l)

let end_action t =
  match t.current_action with
  | None -> warn (fun m -> m "Textbuffer.end_action: %s has no current action" t.o#me)
  | Some (count, l) ->
      let count = count - 1 in
      if count <= 0 then
        (
         let undo, redo = t.history in
         t.history <- ((t.modified, Group l) :: undo, []);
         set_modified_ true t ;
         t.current_action <- None
        )
      else
        t.current_action <- Some (count, l)

let rec rev_change = function
| Group l -> Group (List.rev_map rev_change l)
| x -> x

let undo =
  let rec undo_change t (acc, acc_cursors) = function
  | Ins (lrange, str) ->
      let range = range_of_line_range lrange in
      let (change, cursor_changes) = delete_ ~from_history:true ~start:range.start ~size:range.size t in
      (change :: acc, cursor_changes :: acc_cursors)
  | Del (lrange, str) ->
      let range = range_of_line_range lrange in
      let (change, cursor_changes) = insert_ ~from_history:true range.start str t in
      (change :: acc, cursor_changes :: acc_cursors)
  | Group l ->
     List.fold_left (undo_change t) (acc, acc_cursors) l
  in
  let f t =
    try
      match t.history with
      | [], _ -> ([], [])
      | (modified,action)::q, redo ->
          debug (fun m -> m "undo %a" pp_change action);
          let (changes, cursor_changes) = undo_change t ([], []) action in
          t.history <- q, ((t.modified, rev_change action)::redo);
          set_modified_ modified t;
          let cursor_changes = List.flatten cursor_changes in
          (List.rev changes, List.rev cursor_changes)
    with
      e ->
        Log.err (fun m -> m "Undo: %s\n%s"
           (Printexc.to_string e) (Printexc.get_backtrace()));
        ([], [])
  in
  fun t ->
    let (changes, cursor_changes) = f t in
    signal_changes t changes ;
    signal_cursor_changes t cursor_changes ;
    match t.source_language with
    | None -> ()
    | Some lang -> apply_lang t lang

let redo =
  let rec redo_change t (acc, cursors) = function
  | Ins (lrange, str) ->
      let range = range_of_line_range lrange in
      let (change, cursor_changes) = insert_ ~from_history:true range.start str t in
      (change :: acc, cursor_changes :: cursors)
  | Del (lrange, str) ->
      let range = range_of_line_range lrange in
      let (change, cursors_changes) =
        delete_ ~from_history:true ~start:range.start ~size:range.size t
      in
      (change:: acc, cursors_changes :: cursors)
  | Group l -> List.fold_left (redo_change t) (acc, cursors) l
  in
  let f t =
    try
      match t.history with
      | _, [] -> ([], [])
      | undo, (modified,action)::q ->
        debug (fun m -> m "redo %a" pp_change action);
          let (changes, cursors_changes) = redo_change t ([], []) action in
          t.history <- (t.modified, rev_change action)::undo, q;
          set_modified_ modified t;
          let cursors_changes = List.flatten cursors_changes in
          (List.rev changes, List.rev cursors_changes)
    with
      e ->
        Log.err (fun m -> m "Redo: %s\n%s"
           (Printexc.to_string e) (Printexc.get_backtrace()));
        ([], [])
  in
  fun t ->
    let (changes, cursor_changes) = f t in
    signal_changes t changes ;
    signal_cursor_changes t cursor_changes ;
    match t.source_language with
    | None -> ()
    | Some lang -> apply_lang t lang