| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
let all_bindings = ref [] |
| 13 |
|
| 14 |
; |
| 15 |
; |
| 16 |
; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 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 |
|
| 61 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
let real_filenames = List.rev !selected_filenames in |
| 94 |
List.iter process_file real_filenames; |
| 95 |
all_bindings := List.rev !all_bindings |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|