5 type tags = [`none|`control|`define|`structure|`char|`infix|`label|`uident]
7 let colors : (tags * GDraw.color) list Lazy.t =
9 (List.map ~f:(fun (tag,col) -> tag, `COLOR (GDraw.color (`NAME col)))
11 `define, "forestgreen";
16 `uident, "midnightblue";
19 let tag ?(start=0) ?stop:pend (tw : GEdit.text) =
20 let pend = Gaux.default tw#length ~opt:pend in
21 let colors = Lazy.force colors in
23 let position = tw#position
24 and text = tw#get_chars ~start ~stop:pend in
25 let replace ~start:pstart ~stop:pend ~tag =
26 if pend > pstart then begin
27 tw#delete_text ~start:(start+pstart) ~stop:(start+pend);
28 tw#set_point (start+pstart);
29 tw#insert ~foreground:(List.assoc tag colors)
30 (String.sub text ~pos:pstart ~len:(pend-pstart));
32 and next_lf = ref (-1) in
33 let colorize ~start:rstart ~stop:rend ~tag =
34 let rstart = ref rstart in
35 while !rstart < rend do
36 if !next_lf < !rstart then begin
37 try next_lf := String.index_from text !rstart '\n'
38 with Not_found -> next_lf := pend-start
40 replace ~start:!rstart ~stop:(min !next_lf rend) ~tag;
41 rstart := !next_lf + 1
44 let buffer = Lexing.from_string text
45 and last = ref (EOF, 0, 0)
46 and last_pos = ref 0 in
49 let token = Lexer.token buffer
50 and start = Lexing.lexeme_start buffer
51 and stop = Lexing.lexeme_end buffer in
123 | UIDENT _ -> `uident
125 begin match !last with
126 (QUESTION | TILDE), _, _ -> `label
130 begin match !last with
131 LIDENT _, lstart, lstop when lstop = start ->
132 colorize ~tag:`none ~start:!last_pos ~stop:lstart;
133 colorize ~tag:`label ~start:lstart ~stop;
138 | EOF -> raise End_of_file
141 if tag <> `none then begin
142 colorize ~tag:`none ~start:!last_pos ~stop:start;
143 colorize ~tag ~start ~stop;
146 last := (token, start, stop)
149 colorize ~tag:`none ~start:!last_pos ~stop:(pend-start);
151 tw#set_position position;
152 tw#set_point position;
154 End_of_file | Lexer.Error _ -> ()