summaryrefslogtreecommitdiffstats
path: root/otherlibs/labltk/builtin/builtini_text.ml
blob: 4db49c05e9da96c1aab0c3b14e24f1630460a62f (plain)
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
let cCAMLtoTKtextMark  x =  TkToken x;;
let cTKtoCAMLtextMark x = x;;

let cCAMLtoTKtextTag  x =  TkToken x;;
let cTKtoCAMLtextTag x = x;;

##ifdef CAMLTK

(* TextModifiers are never returned by Tk *)
let ppTextModifier = function
   CharOffset n ->
      if n > 0 then "+" ^ (string_of_int n) ^ "chars"
      else if n = 0 then ""
      else (string_of_int n) ^ "chars"
 | LineOffset n ->
      if n > 0 then "+" ^ (string_of_int n) ^ "lines"
      else if n = 0 then ""
      else (string_of_int n) ^ "lines"
 | LineStart -> " linestart"
 | LineEnd -> " lineend"
 | WordStart -> " wordstart"
 | WordEnd -> " wordend"
;;

let ppTextIndex = function
 | TextIndexNone -> ""
 | TextIndex (base, ml) ->
     match cCAMLtoTKindex index_text_table base with
     | TkToken ppbase -> List.fold_left (^) ppbase (List.map ppTextModifier ml)
     | _ -> assert false
;;

let cCAMLtoTKtextIndex i =
  TkToken (ppTextIndex i)
;;

##else

(* TextModifiers are never returned by Tk *)
let cCAMLtoTKtextIndex (i : textIndex) =
  let ppTextModifier = function
    | `Char n ->
        if n > 0 then "+" ^ (string_of_int n) ^ "chars"
        else if n = 0 then ""
        else (string_of_int n) ^ "chars"
    | `Line n ->
        if n > 0 then "+" ^ (string_of_int n) ^ "lines"
        else if n = 0 then ""
        else (string_of_int n) ^ "lines"
    | `Linestart -> " linestart"
    | `Lineend -> " lineend"
    | `Wordstart -> " wordstart"
    | `Wordend -> " wordend"
  in
  let ppTextIndex (base, ml : textIndex) =
    match cCAMLtoTKtext_index base with
      TkToken ppbase ->
        String.concat ~sep:"" (ppbase :: List.map ~f:ppTextModifier ml)
    | _ -> assert false
  in
  TkToken (ppTextIndex i)
;;

##endif