-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathledit.ml
More file actions
1715 lines (1635 loc) · 51 KB
/
Copy pathledit.ml
File metadata and controls
1715 lines (1635 loc) · 51 KB
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
(***********************************************************************)
(* *)
(* Ledit *)
(* *)
(* Daniel de Rauglaudre, INRIA Rocquencourt *)
(* *)
(* Copyright 2001-2014 Institut National de Recherche en Informatique *)
(* et Automatique. Distributed only by permission. *)
(* *)
(***********************************************************************)
(* $Id$ *)
#load "pa_macro.cmo";
#load "pa_fstream.cmo";
#load "pa_local.cmo";
open Printf;
type encoding = [ Ascii | Iso_8859 | Utf_8 ];
value string_make =
IFDEF OCAML_VERSION < OCAML_4_02_0 THEN String.make
ELSE Bytes.make END
;
value string_create =
IFDEF OCAML_VERSION < OCAML_4_02_0 THEN String.create
ELSE Bytes.create END
;
value string_length =
IFDEF OCAML_VERSION < OCAML_4_02_0 THEN String.length
ELSE Bytes.length END
;
value string_unsafe_set =
IFDEF OCAML_VERSION < OCAML_4_02_0 THEN String.unsafe_set
ELSE Bytes.unsafe_set END
;
module A :
sig
value encoding : ref encoding;
module Char :
sig
type t = 'abstract;
value of_ascii : char -> t;
value to_ascii : t -> option char;
value is_word_char : t -> bool;
value ctrl_val : t -> option t;
value meta_ctrl_val : t -> option t;
value not_ascii_val : t -> option (t * t * t);
value uppercase : t -> t;
value lowercase : t -> t;
value parse : Stream.t char -> t;
value to_string : t -> string;
value input : in_channel -> t;
value read : unit -> t;
value print : t -> unit;
value prerr : t -> unit;
value prerr_backsp : t -> unit;
end;
module String :
sig
type t = 'abstract;
value empty : t;
value of_char : Char.t -> t;
value of_ascii : string -> t;
value length : t -> int;
value set : t -> int -> Char.t -> unit;
value get : t -> int -> Char.t;
value sub : t -> int -> int -> t;
value concat : t -> t -> t;
value input_line : in_channel -> t;
value output : out_channel -> t -> unit;
end;
end =
struct
value encoding = ref Iso_8859;
value char_code = Char.code;
value nbc c =
if Char.code c < 0b10000000 then 1
else if Char.code c < 0b11000000 then -1
else if Char.code c < 0b11100000 then 2
else if Char.code c < 0b11110000 then 3
else if Char.code c < 0b11111000 then 4
else if Char.code c < 0b11111100 then 5
else if Char.code c < 0b11111110 then 6
else -1
;
module Char =
struct
type t = string;
value of_ascii c = String.make 1 c;
value to_ascii c =
if String.length c = 1 && Char.code c.[0] < 128 then Some c.[0]
else None
;
value is_word_char c =
if String.length c = 1 then
match c.[0] with
[ 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' -> True
| x ->
if Char.code x < 128 then False
else
match encoding.val with
[ Ascii -> False
| Iso_8859 -> Char.code x >= 160
| Utf_8 -> assert False ] ]
else True
;
value ctrl_val c =
if String.length c = 1 then
let c = c.[0] in
if Char.code c < 32 || Char.code c == 127 then
Some (String.make 1 (Char.chr (127 land (Char.code c + 64))))
else None
else None
;
value meta_ctrl_val c =
if String.length c = 1 then
let c = c.[0] in
if Char.code c >= 128 && Char.code c < 160 then
Some (String.make 1 (Char.chr (127 land (Char.code c + 64))))
else None
else None
;
value not_ascii_val c =
if String.length c = 1 then
let c = c.[0] in
if Char.code c >= 128 then
let c1 = Char.chr (Char.code c / 100 + Char.code '0') in
let c2 = Char.chr (Char.code c mod 100 / 10 + Char.code '0') in
let c3 = Char.chr (Char.code c mod 10 + Char.code '0') in
Some (String.make 1 c1, String.make 1 c2, String.make 1 c3)
else None
else None
;
value uppercase c =
match encoding.val with
[ Ascii | Iso_8859 -> String.uppercase_ascii c
| Utf_8 ->
if String.length c = 1 then
if Char.code c.[0] < 128 then String.uppercase_ascii c else c
else c ]
;
value lowercase c =
match encoding.val with
[ Ascii | Iso_8859 -> String.lowercase_ascii c
| Utf_8 ->
if String.length c = 1 then
if Char.code c.[0] < 128 then String.lowercase_ascii c else c
else c ]
;
value to_string s = s;
value get_char f =
match encoding.val with
[ Ascii | Iso_8859 -> String.make 1 (f ())
| Utf_8 ->
let c = f () in
let nbc = nbc c in
if nbc < 0 then "?"
else if nbc = 1 then String.make 1 c
else
loop (String.make 1 c) (nbc - 1) where rec loop s n =
if n = 0 then s
else
let c = f () in
if Char.code c < 0b10000000 then "?"
else if Char.code c > 0b10111111 then "?"
else loop (s ^ String.make 1 c) (n - 1) ]
;
value input ic = get_char (fun () -> input_char ic);
value read =
let buff = string_make 2 ' ' in
fun () ->
get_char
(fun () ->
let len = Unix.read Unix.stdin buff 0 1 in
if len == 0 then raise End_of_file else Bytes.get buff 0)
;
value parse s = get_char (fun () -> Stream.next s);
value print c =
if String.length c = 1 && c.[0] = '\n' then print_newline ()
else print_string c
;
value prerr c = output_string stderr c;
value prerr_backsp c = do {
if encoding.val = Utf_8 &&
Char.code c.[0] >= 228 && Char.code c.[0] <= 233
then
(* hack for Chinese; it seems that terminals (at least
"konsole" and "xterm") need 2 backspaces to put the
cursor on the glyph. *)
output_char stderr '\b'
else ();
output_char stderr '\b';
};
end;
module String =
struct
type t = array string;
value empty = [| |];
value of_char c = [| c |];
value of_ascii s =
Array.init (String.length s)
(fun i ->
if char_code s.[i] < 128 then String.make 1 s.[i]
else invalid_arg "A.String.of_ascii")
;
value length = Array.length;
value set = Array.set;
value get = Array.get;
value sub = Array.sub;
value concat = Array.append;
value input_line ic =
let s = input_line ic in
match encoding.val with
[ Ascii | Iso_8859 ->
Array.init (String.length s) (fun i -> String.make 1 s.[i])
| Utf_8 ->
loop [] 0 where rec loop list i =
if i >= String.length s then Array.of_list (List.rev list)
else
let n = nbc s.[i] in
if n < 0 then loop ["?" :: list] (i + 1)
else loop [String.sub s i n :: list] (i + n) ]
;
value output oc s = Array.iter (output_string oc) s;
end;
end
;
DEFINE CTRL(x) = EVAL (Char.chr (Char.code x - (Char.code 'a' - 1)));
DEFINE META(x) = EVAL (Char.chr (Char.code x + 128));
DEFINE DEL = '\127';
DEFINE ESC = '\027';
value max_len = ref 70;
value set_max_len x =
max_len.val := if x > 3 then x else failwith "set_max_len"
;
value son = ref None;
value set_son_pid pid = son.val := Some pid;
type command =
[ Abort
| Accept_line
| Backward_char
| Backward_delete_char
| Backward_kill_word
| Backward_word
| Beginning_of_history
| Beginning_of_line
| Capitalize_word
| Complete_file_name
| Delete_char
| Delete_char_or_end_of_file
| Downcase_word
| End_of_history
| End_of_line
| Expand_abbrev
| Forward_char
| Forward_word
| Insert of string
| Interrupt
| Kill_line
| Kill_word
| Next_history
| Operate_and_get_next
| Previous_history
| Quit
| Quoted_insert
| Redraw_current_line
| Reverse_search_history
| Self_insert
| Sequence of string
| Suspend
| Transpose_chars
| Unix_line_discard
| Upcase_word
| Yank ]
;
type istate = [ Normal of string | Quote ];
value meta_as_escape = ref True;
value unset_meta_as_escape () = meta_as_escape.val := False;
value set_utf8 () = A.encoding.val := Utf_8;
value set_ascii () = A.encoding.val := Ascii;
(* key binding tree *)
type kb_tree =
[ KB_tree of list kb_node
| KB_comm of command
| KB_none ]
and kb_node = { char : char; son : kb_tree };
value hex_value c =
match c with
[ '0'..'9' -> Some (Char.code c - Char.code '0')
| 'a'..'f' -> Some (Char.code c - Char.code 'a' + 10)
| 'A'..'F' -> Some (Char.code c - Char.code 'A' + 10)
| _ -> None ]
;
value oct_value c =
match c with
[ '0'..'7' -> Some (Char.code c - Char.code '0')
| _ -> None ]
;
value rec next_char s i =
if i < String.length s then
let (c, i) =
if s.[i] = '\\' then
if i + 1 < String.length s then
match s.[i+1] with
[ 'C' ->
if i + 3 < String.length s && s.[i+2] = '-' then
let c = s.[i+3] in
if Char.code c >= 64 && Char.code c <= 95 then
(Char.chr (Char.code c - 64), i + 4)
else if Char.code c >= 96 && Char.code c <= 127 then
(Char.chr (Char.code c - 96), i + 4)
else
(s.[i], i + 1)
else (s.[i], i + 1)
| 'M' ->
if i + 2 < String.length s && s.[i+2] = '-' then
match next_char s (i + 3) with
[ Some (c, j) ->
if Char.code c < 128 then
(Char.chr (Char.code c + 128), j)
else
(s.[i], i + 1)
| None -> (s.[i], i + 1) ]
else (s.[i], i + 1)
| 'e' -> ('\027', i + 2)
| '\\' -> ('\\', i + 2)
| '"' -> ('"', i + 2)
| ''' -> (''', i + 2)
| 'a' -> ('\007', i + 2)
| 'b' -> ('\b', i + 2)
| 'd' -> ('\255', i + 2)
| 'f' -> ('\012', i + 2)
| 'n' -> ('\n', i + 2)
| 'r' -> ('\r', i + 2)
| 't' -> ('\009', i + 2)
| 'v' -> ('\011', i + 2)
| 'x' ->
if i + 2 < String.length s then
match hex_value s.[i+2] with
[ Some v ->
if i + 3 < String.length s then
match hex_value s.[i+3] with
[ Some v1 -> (Char.chr (16 * v + v1), i + 4)
| None -> (Char.chr v, i + 3) ]
else (Char.chr v, i + 3)
| None -> (s.[i], i + 1) ]
else (s.[i], i + 1)
| c ->
match oct_value s.[i+1] with
[ Some v ->
if i + 2 < String.length s then
match oct_value s.[i+2] with
[ Some v1 ->
let v = 8 * v + v1 in
if i + 3 < String.length s then
match oct_value s.[i+3] with
[ Some v1 ->
let v1 = 8 * v + v1 in
if v1 <= 255 then (Char.chr v1, i + 4)
else (Char.chr v, i + 3)
| None -> (Char.chr v, i + 3) ]
else (Char.chr v, i + 3)
| None -> (Char.chr v, i + 2) ]
else (Char.chr v, i + 2)
| None -> (s.[i], i + 1) ] ]
else (s.[i], i + 1)
else (s.[i], i + 1)
in
Some (c, i)
else None
;
value insert_command s comm kb =
let rec insert_in_tree i kb =
match next_char s i with
[ Some (c, i) ->
let cnl =
match kb with
[ KB_tree cnl -> cnl
| KB_comm _ | KB_none -> [] ]
in
KB_tree (insert_in_node_list (c, i) cnl)
| None -> KB_comm comm ]
and insert_in_node_list (c, i) =
fun
[ [] -> [{char = c; son = insert_in_tree i (KB_tree [])}]
| [n :: nl] ->
if c < n.char then
[{char = c; son = insert_in_tree i (KB_tree [])}; n :: nl]
else if c > n.char then
[n :: insert_in_node_list (c, i) nl]
else
[{char = n.char; son = insert_in_tree i n.son} :: nl] ]
in
insert_in_tree 0 kb
;
value init_default_commands kb =
List.fold_left (fun kb (key, bind) -> insert_command key bind kb) kb
[("\\C-a", Beginning_of_line);
("\\C-e", End_of_line);
("\\C-f", Forward_char);
("\\C-b", Backward_char);
("\\C-p", Previous_history);
("\\C-n", Next_history);
("\\C-r", Reverse_search_history);
("\\C-d", Delete_char_or_end_of_file);
("\\C-h", Backward_delete_char);
("\\177", Backward_delete_char);
("\\C-i", Complete_file_name);
("\\C-t", Transpose_chars);
("\\C-q", Quoted_insert);
("\\C-k", Kill_line);
("\\C-y", Yank);
("\\C-u", Unix_line_discard);
("\\C-l", Redraw_current_line);
("\\C-g", Abort);
("\\C-c", Interrupt);
("\\C-z", Suspend);
("\\C-\\", Quit);
("\\n", Accept_line);
("\\C-x", Operate_and_get_next);
("\\ef", Forward_word);
("\\eb", Backward_word);
("\\ec", Capitalize_word);
("\\eu", Upcase_word);
("\\el", Downcase_word);
("\\e<", Beginning_of_history);
("\\e>", End_of_history);
("\\ed", Kill_word);
("\\e\\C-h", Backward_kill_word);
("\\e\\177", Backward_kill_word);
("\\e/", Expand_abbrev);
("\\e[A", Previous_history); (* Up arrow *)
("\\e[B", Next_history); (* Down arrow *)
("\\e[C", Forward_char); (* Left arrow *)
("\\e[D", Backward_char); (* Right arrow *)
("\\e[3~", Delete_char); (* Delete *)
("\\e[H", Beginning_of_line); (* Home *)
("\\e[F", End_of_line); (* End *)
("\\e[5~", Previous_history); (* Page Up *)
("\\e[6~", Next_history); (* Page Down *)
("\\e[2H", Beginning_of_history); (* Shift Home *)
("\\e[2F", End_of_history); (* Shift End *)
("\\e[OA", Previous_history);
("\\e[OC", Forward_char);
("\\e[OD", Backward_char);
("\\e[OH", Beginning_of_line);
(* gnome-terminal *)
("\\eOH", Beginning_of_line); (* Home *)
("\\eOF", End_of_line); (* End *)
(* rxvt *)
("\\e[7~", Beginning_of_line); (* Home *)
("\\e[8~", End_of_line) (* End *)
::
if meta_as_escape.val then
[("\\M-b", Backward_word);
("\\M-c", Capitalize_word);
("\\M-d", Kill_word);
("\\M-f", Forward_word);
("\\M-l", Downcase_word);
("\\M-u", Upcase_word);
("\\M-<", Beginning_of_history);
("\\M->", End_of_history);
("\\M-/", Expand_abbrev);
("\\M-\\C-h", Backward_kill_word);
("\\M-\\127", Backward_kill_word)]
else []]
;
(* Reading the leditrc file *)
value rev_implode l =
let s = string_create (List.length l) in
loop (string_length s - 1) l where rec loop i =
fun
[ [c :: l] -> do { string_unsafe_set s i c; loop (i - 1) l }
| [] -> Bytes.to_string s ]
;
value rec parse_string rev_cl =
fparser
[ [: `'"' :] -> rev_implode rev_cl
| [: `'\\'; `c; r = parse_string [c; '\\' :: rev_cl] :] -> r
| [: `c; r = parse_string [c :: rev_cl] :] -> r ]
;
value rec skip_to_eos =
fparser
[ [: `_; r = skip_to_eos :] -> r
| [: :] -> () ]
;
value rec skip_spaces =
fparser
[ [: `(' ' | '\t'); r = skip_spaces :] -> r
| [: `'#'; r = skip_to_eos :] -> r
| [: :] -> () ]
;
value rec parse_command rev_cl =
fparser
[ [: `('a'..'z' | 'A'..'Z' | '-' as c); r = parse_command [c :: rev_cl] :] ->
r
| [: :] -> rev_implode rev_cl ]
;
type binding = [ B_string of string | B_comm of string ];
value parse_binding =
fparser
[ [: `'"'; s = parse_string [] :] -> B_string s
| [: c = parse_command [] :] -> B_comm c ]
;
value parse_line =
fparser
[ [: `'"'; key = parse_string []; _ = skip_spaces; `':'; _ = skip_spaces;
binding = parse_binding; _ = skip_spaces; _ = Fstream.empty :] ->
(key, binding) ]
;
value command_of_name = do {
let ht = Hashtbl.create 1 in
let add = Hashtbl.add ht in
add "abort" Abort;
add "accept-line" Accept_line;
add "backward-char" Backward_char;
add "backward-delete-char" Backward_delete_char;
add "backward-kill-word" Backward_kill_word;
add "backward-word" Backward_word;
add "beginning-of-history" Beginning_of_history;
add "beginning-of-line" Beginning_of_line;
add "capitalize-word" Capitalize_word;
add "delete-char" Delete_char;
add "delete-char-or-end-of-file" Delete_char_or_end_of_file;
add "downcase-word" Downcase_word;
add "end-of-history" End_of_history;
add "end-of-line" End_of_line;
add "expand-abbrev" Expand_abbrev;
add "complete-file-name" Complete_file_name;
add "forward-char" Forward_char;
add "forward-word" Forward_word;
add "interrupt" Interrupt;
add "kill-line" Kill_line;
add "kill-word" Kill_word;
add "next-history" Next_history;
add "operate-and-get-next" Operate_and_get_next;
add "previous-history" Previous_history;
add "quit" Quit;
add "quoted-insert" Quoted_insert;
add "redraw-current-line" Redraw_current_line;
add "reverse-search-history" Reverse_search_history;
add "self-insert" Self_insert;
add "suspend" Suspend;
add "transpose-chars" Transpose_chars;
add "unix-line-discard" Unix_line_discard;
add "upcase-word" Upcase_word;
add "yank" Yank;
fun name ->
try Some (Hashtbl.find ht name) with
[ Not_found -> None (*failwith ("command not found: " ^ name)*) ]
};
value init_file_commands kb fname =
let ic = open_in fname in
loop kb where rec loop kb =
match try Some (input_line ic) with [ End_of_file -> None ] with
[ Some s ->
let kb =
match parse_line (Fstream.of_string s) with
[ Some ((key, B_string s), _) ->
let s =
loop [] 0 where rec loop rev_cl i =
match next_char s i with
[ Some (c, i) -> loop [c :: rev_cl] i
| None -> rev_implode rev_cl ]
in
insert_command key (Insert s) kb
| Some ((key, B_comm comm_name), _) ->
match command_of_name comm_name with
[ Some comm -> insert_command key comm kb
| None -> kb ]
| None -> kb ]
in
loop kb
| None -> do { close_in ic; kb } ]
;
type line =
{ buf : mutable A.String.t;
cur : mutable int;
len : mutable int }
;
type abbrev_data =
{ hist : list A.String.t;
rpos : int;
clen : int;
abbr : A.String.t;
found : list A.String.t }
;
type state =
{ od : line;
nd : line;
line : line;
leditrc_name : string;
leditrc_mtime : mutable float;
last_line : mutable A.String.t;
istate : mutable istate;
shift : mutable int;
cut : mutable A.String.t;
init_kb : mutable option kb_tree;
total_kb : mutable option kb_tree;
last_comm : mutable command;
histfile : mutable option out_channel;
history : mutable Cursor.t A.String.t;
abbrev : mutable option abbrev_data;
complete_fn : mutable int;
complete_fn_screen : mutable int}
;
value eval_comm s st =
let rec search_in_tree i kb =
if i = String.length s then
match kb with
[ KB_tree _ -> Some None
| KB_comm comm -> Some (Some comm)
| KB_none -> None ]
else
let c = s.[i] in
match kb with
[ KB_tree cnl -> search_in_node_list c (i + 1) cnl
| KB_comm _ | KB_none -> None ]
and search_in_node_list c i =
fun
[ [] -> None
| [n :: nl] ->
if c < n.char then None
else if c > n.char then search_in_node_list c i nl
else search_in_tree i n.son ]
in
let kb =
let leditrc_mtime =
if Sys.file_exists st.leditrc_name then
let stat = Unix.stat st.leditrc_name in
if stat.Unix.st_mtime > st.leditrc_mtime then Some stat.Unix.st_mtime
else None
else None
in
match (leditrc_mtime, st.total_kb) with
[ (None, Some kb) -> kb
| _ -> do {
let init_kb =
match st.init_kb with
[ Some kb -> kb
| None -> do {
let kb = init_default_commands KB_none in
st.init_kb := Some kb;
kb
} ]
in
let total_kb =
match leditrc_mtime with
[ Some mtime -> do {
st.leditrc_mtime := mtime;
init_file_commands init_kb st.leditrc_name
}
| None -> init_kb ]
in
st.total_kb := Some total_kb;
total_kb
} ]
in
search_in_tree 0 kb
;
value put_bs st c = A.Char.prerr_backsp c;
value put_space st = output_char stderr ' ';
value put_newline st = prerr_endline "";
value flush_out st = flush stderr;
value bell () = do { prerr_string "\007"; flush stderr };
value saved_tcio =
try Some (Unix.tcgetattr Unix.stdin) with
[ Unix.Unix_error _ _ _ -> None ]
;
value edit_tcio = ref None;
value set_edit () =
match saved_tcio with
[ Some _ ->
let tcio =
match edit_tcio.val with
[ Some e -> e
| None -> do {
let tcio = Unix.tcgetattr Unix.stdin in
tcio.Unix.c_echo := False;
tcio.Unix.c_icanon := False;
tcio.Unix.c_vmin := 1;
tcio.Unix.c_isig := False;
tcio.Unix.c_ixon := False;
edit_tcio.val := Some tcio;
tcio
} ]
in
Unix.tcsetattr Unix.stdin Unix.TCSADRAIN tcio
| None -> () ]
and unset_edit () =
match saved_tcio with
[ Some tcio -> Unix.tcsetattr Unix.stdin Unix.TCSADRAIN tcio
| None -> () ]
;
value line_set_nth_char line i c =
if i == A.String.length line.buf then
line.buf := A.String.concat line.buf (A.String.of_char c)
else
A.String.set line.buf i c
;
value line_to_nd st = do {
let rec line_rec i = do {
if i == st.line.cur then st.nd.cur := st.nd.len else ();
if i < st.line.len then do {
let c = A.String.get st.line.buf i in
if c = A.Char.of_ascii '\t' then
for i = st.nd.len + 1 to (st.nd.len + 8) / 8 * 8 do {
line_set_nth_char st.nd st.nd.len (A.Char.of_ascii ' ');
st.nd.len := st.nd.len + 1;
}
else if
match A.Char.ctrl_val c with
[ Some c -> do {
line_set_nth_char st.nd st.nd.len (A.Char.of_ascii '^');
line_set_nth_char st.nd (st.nd.len + 1) c;
st.nd.len := st.nd.len + 2;
True
}
| None -> False ]
then ()
else
match A.encoding.val with
[ Ascii ->
match A.Char.not_ascii_val c with
[ Some (c1, c2, c3) -> do {
line_set_nth_char st.nd st.nd.len (A.Char.of_ascii '\\');
line_set_nth_char st.nd (st.nd.len + 1) c1;
line_set_nth_char st.nd (st.nd.len + 2) c2;
line_set_nth_char st.nd (st.nd.len + 3) c3;
st.nd.len := st.nd.len + 4;
}
| None -> do {
line_set_nth_char st.nd st.nd.len c;
st.nd.len := st.nd.len + 1
} ]
| Iso_8859 ->
match A.Char.meta_ctrl_val c with
[ Some c -> do {
line_set_nth_char st.nd st.nd.len (A.Char.of_ascii 'M');
line_set_nth_char st.nd (st.nd.len + 1) (A.Char.of_ascii '-');
line_set_nth_char st.nd (st.nd.len + 2) (A.Char.of_ascii '^');
line_set_nth_char st.nd (st.nd.len + 3) c;
st.nd.len := st.nd.len + 4
}
| None -> do {
line_set_nth_char st.nd st.nd.len c;
st.nd.len := st.nd.len + 1
} ]
| Utf_8 -> do {
line_set_nth_char st.nd st.nd.len c;
st.nd.len := st.nd.len + 1
} ];
line_rec (i + 1)
}
else if st.nd.len > max_len.val then do {
let shift =
if st.nd.cur - st.shift >= 0 &&
st.nd.cur - st.shift < max_len.val - 2
then
st.shift
else if st.nd.cur < max_len.val - 3 then 0
else st.nd.cur - max_len.val / 2
in
for i = 0 to max_len.val - 3 do {
let ni = i + shift in
A.String.set st.nd.buf i
(if ni < st.nd.len then A.String.get st.nd.buf ni
else A.Char.of_ascii ' ');
};
A.String.set st.nd.buf (max_len.val - 2) (A.Char.of_ascii ' ');
A.String.set st.nd.buf (max_len.val - 1)
(if shift = 0 then A.Char.of_ascii '>'
else if st.nd.len - shift < max_len.val - 2 then A.Char.of_ascii '<'
else A.Char.of_ascii '*');
st.nd.cur := st.nd.cur - shift;
st.nd.len := max_len.val;
st.shift := shift
}
else st.shift := 0
}
in
st.nd.len := 0;
line_rec 0
};
value display st =
disp_rec 0 where rec disp_rec i =
if i < st.nd.len then do {
if i >= st.od.len ||
A.String.get st.od.buf i <> A.String.get st.nd.buf i
then do {
while i < st.od.cur do {
st.od.cur := st.od.cur - 1;
put_bs st (A.String.get st.od.buf i);
};
while st.od.cur < i do {
let c = A.String.get st.nd.buf st.od.cur in
st.od.cur := st.od.cur + 1;
A.Char.prerr c;
};
let c = A.String.get st.nd.buf i in
line_set_nth_char st.od i c;
st.od.cur := st.od.cur + 1;
A.Char.prerr c
}
else ();
disp_rec (i + 1)
}
else do {
if st.od.len > st.nd.len then do {
while st.od.cur < st.od.len do {
let c =
if st.od.cur < st.nd.len then A.String.get st.nd.buf st.od.cur
else A.Char.of_ascii ' '
in
A.Char.prerr c;
st.od.cur := st.od.cur + 1;
};
while st.od.cur > st.nd.len do {
st.od.cur := st.od.cur - 1;
put_bs st (A.String.get st.od.buf st.od.cur);
put_space st;
put_bs st (A.Char.of_ascii ' ');
}
}
else ();
st.od.len := st.nd.len;
while st.od.cur < st.nd.cur do {
A.Char.prerr (A.String.get st.nd.buf st.od.cur);
st.od.cur := st.od.cur + 1;
};
while st.od.cur > st.nd.cur do {
st.od.cur := st.od.cur - 1;
put_bs st (A.String.get st.nd.buf st.od.cur);
};
flush_out st
}
;
value update_output st = do {line_to_nd st; display st};
value balance_paren st c =
match A.Char.to_ascii c with
[ Some (')' | ']' | '}' as c) ->
let i =
find_lparen c (st.line.cur - 2) where rec find_lparen r i =
if i < 0 then i
else
match A.Char.to_ascii (A.String.get st.line.buf i) with
[ Some (')' | ']' | '}' as c) ->
find_lparen r (find_lparen c (i - 1) - 1)
| Some '(' -> if r == ')' then i else -1
| Some '[' -> if r == ']' then i else -1
| Some '{' -> if r == '}' then i else -1
| Some '"' ->
let rec skip_string i =
if i < 0 then i
else if
A.String.get st.line.buf i = A.Char.of_ascii '"'
then i - 1
else skip_string (i - 1)
in
find_lparen r (skip_string (i - 1))
| _ -> find_lparen r (i - 1) ]
in
if i >= 0 then do {
let c = st.line.cur in
st.line.cur := i;
update_output st;
st.line.cur := c;
let _ = Unix.select [Unix.stdin] [] [] 1.0 in
()
}
else ()
| Some _ | None -> () ]
;
value delete_char st = do {
st.line.len := st.line.len - 1;
for i = st.line.cur to st.line.len - 1 do {
A.String.set st.line.buf i (A.String.get st.line.buf (i + 1));
}
};
value insert_char st x = do {
for i = st.line.len downto st.line.cur + 1 do {
line_set_nth_char st.line i (A.String.get st.line.buf (i - 1));
};
st.line.len := st.line.len + 1;
line_set_nth_char st.line st.line.cur x
};
value move_in_word buf e f g =
move_rec where rec move_rec i =
if e i then i
else if A.Char.is_word_char (A.String.get buf i) then f move_rec i
else g move_rec i
;
value forward_move line = move_in_word line.buf (fun i -> i == line.len);
value backward_move line = move_in_word line.buf (fun i -> i == -1);
value forward_word line =
let i = line.cur in
let i = forward_move line (fun _ i -> i) (fun mv i -> mv (i + 1)) i in
forward_move line (fun mv i -> mv (i + 1)) (fun _ i -> i) i
;
value backward_word line =
let i = line.cur - 1 in
let i = backward_move line (fun _ i -> i) (fun mv i -> mv (i - 1)) i in
backward_move line (fun mv i -> mv (i - 1)) (fun _ i -> i) i + 1
;
value get_word_len st =
let i = st.line.cur - 1 in
i - backward_move st.line (fun mv i -> mv (i - 1)) (fun _ i -> i) i
;
value kill_word st =
let i = st.line.cur in
let i =
forward_move st.line (fun _ i -> i)
(fun mv i -> do { delete_char st; mv i }) i
in
forward_move st.line (fun mv i -> do { delete_char st; mv i })
(fun _ i -> i) i
;
value backward_kill_word st = do {
let k = backward_word st.line in
let sh = st.line.cur - k in
st.line.len := st.line.len - sh;
for i = k to st.line.len - 1 do {
A.String.set st.line.buf i (A.String.get st.line.buf (i + sh));
};
k
};
value capitalize_word st =
let i = st.line.cur in
let i0 = forward_move st.line (fun _ i -> i) (fun mv i -> mv (i + 1)) i in
forward_move st.line
(fun mv i -> do {
let f = if i == i0 then A.Char.uppercase else A.Char.lowercase in
A.String.set st.line.buf i (f (A.String.get st.line.buf i));
mv (i + 1)
})
(fun _ i -> i) i0
;
value upcase_word st =
let i = st.line.cur in
let i = forward_move st.line (fun _ i -> i) (fun mv i -> mv (i + 1)) i in
forward_move st.line
(fun mv i -> do {
let f = A.Char.uppercase in
A.String.set st.line.buf i (f (A.String.get st.line.buf i));
mv (i + 1)
})
(fun _ i -> i) i