Debian Codegraph @

ide/rocqide/unicode_bindings.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11
12 let all_bindings = ref []
13 (* example unicode bindings table:
14 [ ("\\pi", "π", None);
15 ("\\lambdas", "λs", Some 4);
16 ("\\lambda", "λ", Some 3);
17 ("\\lake", "0", Some 2);
18 ("\\lemma", "Lemma foo : x. Proof. Qed", Some 1); ] *)
19
20 (** Auxiliary function used by [load_files].
21 Takes as argument a valid path. *)
22
23 let process_file filename =
24 if not (Sys.file_exists filename) then begin
25 Ideutils.warning (Printf.sprintf "Warning: unicode bindings file '%s' was not found." filename)
26 end else begin
27 let ch = open_in filename in
28 begin try while true do
29 let line = input_line ch in
30 begin try
31 let chline = Scanf.Scanning.from_string line in
32 let (key,value) =
33 Scanf.bscanf chline "%s %s" (fun x y -> (x,y)) in
34 let prio =
35 try Scanf.bscanf chline " %d" (fun x -> Some x)
36 with Scanf.Scan_failure _ | Failure _ | End_of_file -> None
37 in
38 all_bindings := (key,value,prio)::!all_bindings;
39 (* Note: storing bindings in reverse order, flipping is done later *)
40 Scanf.Scanning.close_in chline;
41 with End_of_file -> () end;
42 done with End_of_file -> () end;
43 close_in ch
44 end
45
46 let load_files filenames =
47 let selected_filenames = ref [] in
48 let add f =
49 selected_filenames := f::!selected_filenames in
50 let warn_default_not_found () =
51 let dirs = Minilib.rocqide_data_dirs () in
52 Ideutils.warning Format.(
53 asprintf
54 "@[Warning: the file 'ide/default.bindings' was not found in:@\n @[<v>%a@]@]."
55 (pp_print_list ~pp_sep:pp_print_cut pp_print_string) dirs) in
56 let warn_local_not_found () =
57 Ideutils.warning (Printf.sprintf
58 "Warning: the local configuration file 'coqide.bindings' was not found.") in
59 if filenames = [] then begin
60 (* If no argument is provided using [-unicode-bindings],
61 then use the default file and the local file, if it exists *)
62 begin match Preferences.get_unicode_bindings_default_file() with
63 | Some f -> add f
64 | None -> warn_default_not_found()
65 end;
66 begin match Preferences.get_unicode_bindings_local_file() with
67 | Some f -> add f
68 | None -> ()
69 end;
70 end else begin
71 (* If [-unicode-bindings] is used with a list of file, consider
72 these files in order, with a special treatment for the tokens
73 "default" and "local", which are replaced by the appropriate path. *)
74 let add_arg f =
75 match f with
76 | "default" ->
77 begin match Preferences.get_unicode_bindings_default_file() with
78 | Some f -> add f
79 | None -> warn_default_not_found()
80 end
81 | "local" ->
82 begin match Preferences.get_unicode_bindings_local_file() with
83 | Some f -> add f
84 | None -> warn_local_not_found()
85 end
86 | _ -> add f
87 in
88 List.iter add_arg filenames
89 end;
90 (* Files must be processed in order, to build the list of bindings
91 by iteratively consing entry to its head, the list being reversed
92 at the very end *)
93 let real_filenames = List.rev !selected_filenames in
94 List.iter process_file real_filenames;
95 all_bindings := List.rev !all_bindings
96 (* For debugging the list of unicode files loaded:
97 List.iter (fun f -> Printf.eprintf "%s\n" f) real_filenames; *)
98
99 (** Auxiliary function to test whether [s] is a prefix of [str];
100 Note that there might be overlap with wg_Completion::is_substring *)
101
102 let string_is_prefix s str =
103 let n = String.length s in
104 let m = String.length str in
105 if m < n
106 then false
107 else (s = String.sub str 0 n)
108
109 let lookup prefix =
110 let max_priority = 100000000 in
111 let cur_word = ref None in
112 let cur_prio = ref (max_priority+1) in
113 let test_binding (key, word, prio_opt) =
114 let prio =
115 match prio_opt with
116 | None -> max_priority
117 | Some p -> p
118 in
119 if string_is_prefix prefix key && prio < !cur_prio then begin
120 cur_word := Some word;
121 cur_prio := prio;
122 end in
123 List.iter test_binding !all_bindings;
124 !cur_word
125
126
127 (* For debugging the list of unicode bindings loaded:
128 let print_unicode_bindings () =
129 List.iter (fun (x,y,p) ->
130 Printf.eprintf "%s %s %d\n" x y (match p with None -> -1 | Some n -> n))
131 !all_bindings;
132 prerr_newline()
133 *)