Debian Codegraph @

ide/rocqide/ideutils.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 open Preferences
12
13 let _ = GtkMain.Main.init ()
14
15 let warn_image () =
16 let img = GMisc.image () in
17 img#set_stock `DIALOG_WARNING;
18 img#set_icon_size `DIALOG;
19 img
20
21 let warning msg =
22 GToolbox.message_box ~title:"Warning" ~icon:(warn_image ())#coerce msg
23
24 let cb = GData.clipboard Gdk.Atom.primary
25
26 (* status bar and locations *)
27
28 let status = GMisc.statusbar ()
29
30 (* These functions seem confused:
31 1. They should be per-session rather than global (e.g. for "Rocq is computing")
32 2. I don't see how pushing and popping is particularly useful
33 and there's no explanation of when to push/pop *)
34 let push_info,pop_info,clear_info =
35 let status_context = status#new_context ~name:"Messages" in
36 let size = ref 0 in
37 (fun s -> incr size; ignore (status_context#push s)),
38 (fun () -> decr size; status_context#pop ()),
39 (fun () -> for _i = 1 to !size do status_context#pop () done; size := 0)
40
41 type 'a mlist = Nil | Cons of { hd : 'a ; mutable tl : 'a mlist }
42
43 let enqueue a x =
44 let rec aux x = match x with
45 | Nil -> assert false
46 | Cons p ->
47 match p.tl with
48 | Nil -> p.tl <- Cons { hd = a ; tl = Nil }
49 | _ -> aux p.tl in
50 match !x with
51 | Nil -> x := Cons { hd = a ; tl = Nil }
52 | _ -> aux !x
53
54 let pop = function
55 | Cons p -> p.tl
56 | Nil -> assert false
57
58 let flash_info =
59 let queue = ref Nil in
60 let flash_context = status#new_context ~name:"Flash" in
61 let rec process () = match !queue with
62 | Cons { hd = (delay,text) } ->
63 let msg = flash_context#push text in
64 ignore (Glib.Timeout.add ~ms:delay ~callback:(fun () ->
65 flash_context#remove msg;
66 queue := pop !queue;
67 process (); false))
68 | Nil -> () in
69 fun ?(delay=5000) text ->
70 let processing = !queue <> Nil in
71 enqueue (delay,text) queue;
72 if not processing then process ()
73
74 (* Note: Setting the same attribute with two separate tags appears to use
75 the first value applied and not the second. I saw this trying to set the background
76 color on Windows. A clean fix, if ever needed, would be to combine the attributes
77 of the tags into a single composite tag before applying. This is left as an
78 exercise for the reader. *)
79 let insert_with_tags (buf : #GText.buffer_skel) mark rmark tags text =
80 (* FIXME: LablGTK2 does not export the C insert_with_tags function, so that
81 it has to reimplement its own helper function. Unluckily, it relies on
82 a slow algorithm, so that we have to have our own quicker version here.
83 Alas, it is still much slower than the native version... *)
84 if CList.is_empty tags then buf#insert ~iter:(buf#get_iter_at_mark mark) text
85 else
86 let it = buf#get_iter_at_mark mark in
87 let () = buf#move_mark rmark ~where:it in
88 let () = buf#insert ~iter:(buf#get_iter_at_mark mark) text in
89 let start = buf#get_iter_at_mark mark in
90 let stop = buf#get_iter_at_mark rmark in
91 let iter tag = buf#apply_tag tag ~start ~stop in
92 List.iter iter (List.rev tags)
93
94 let nl_white_regex = Str.regexp "^\\( *\n *\\)"
95 let diff_regex = Str.regexp "^diff."
96 let escape_regex = Str.regexp "[^\n\x20-\xff]\\|\x7f"
97
98 let escaped s =
99 let subst s = Printf.sprintf "\\x%02x" (Char.code (Str.matched_string s).[0]) in
100 Str.global_substitute escape_regex subst s
101
102 let insert_xml ?(mark = `INSERT) ?(tags = []) (buf : #GText.buffer_skel) msg =
103 let open Xml_datatype in
104 let dtags = ref [] in
105 let tag name =
106 match GtkText.TagTable.lookup buf#tag_table name with
107 | None -> raise Not_found
108 | Some tag -> new GText.tag tag
109 in
110 let rmark = `MARK (buf#create_mark buf#start_iter) in
111 (* insert the string, but don't apply diff highlights to white space at the begin/end of line *)
112 let rec insert_str tags s =
113 let etags = try List.hd !dtags :: tags with hd -> tags in
114 try
115 let start = Str.search_forward nl_white_regex s 0 in
116 insert_with_tags buf mark rmark etags (String.sub s 0 start);
117 insert_with_tags buf mark rmark tags (Str.matched_group 1 s);
118 let mend = Str.match_end () in
119 insert_str tags (String.sub s mend (String.length s - mend))
120 with Not_found ->
121 insert_with_tags buf mark rmark etags s
122 in
123 let rec insert tags = function
124 | PCData s -> insert_str tags (escaped s)
125 | Element (t, _, children) ->
126 let (pfx, tname) = Pp.split_tag t in
127 let is_diff = try let _ = Str.search_forward diff_regex tname 0 in true with Not_found -> false in
128 let (tags, have_tag) =
129 try
130 let t = tag tname in
131 if is_diff && pfx <> Pp.end_pfx then
132 dtags := t :: !dtags;
133 if pfx = "" then
134 ((if is_diff then tags else t :: tags), true)
135 else
136 (tags, true)
137 with Not_found -> (tags, false)
138 in
139 List.iter (fun xml -> insert tags xml) children;
140 if have_tag && is_diff && pfx <> Pp.start_pfx then
141 dtags := (try List.tl !dtags with tl -> []);
142 in
143 let () = try insert tags msg with _ -> () in
144 buf#delete_mark rmark
145
146 let set_location = ref (function s -> failwith "not ready")
147
148 let display_location ins =
149 let line = ins#line + 1 in
150 let off = ins#line_offset + 1 in
151 let msg = Printf.sprintf "Line: %5d Char: %3d Offset: %5d" line off (ins#offset) in
152 !set_location msg
153
154 (** A utf8 char is either a single byte (ascii char, 0xxxxxxx)
155 or multi-byte (with a leading byte 11xxxxxx and extra bytes 10xxxxxx) *)
156
157 let is_extra_byte c = ((Char.code c) lsr 6 = 2)
158
159 (** Convert the byte offset in a string that may contain
160 multibyte utf-8 chars into a char offset
161 by counting the char-starting bytes. *)
162
163 let byte_offset_to_char_offset s byte_offset =
164 let extra_bytes = ref 0 in
165 for i = 0 to min byte_offset (String.length s - 1) do
166 if is_extra_byte s.[i] then incr extra_bytes
167 done;
168 byte_offset - !extra_bytes
169
170 (* get byte length of a Unicode character *)
171 let ulen uni_ch =
172 if uni_ch < 0x80 then 1
173 else if uni_ch < 0x800 then 2
174 else if uni_ch < 0x10000 then 3
175 else 4
176
177 (* workaround for lablgtk bug up to version 3.1.4 *)
178 (* replaces: buffer#get_iter_at_byte ~line index *)
179 (* see https://github.com/garrigue/lablgtk/pull/181 *)
180 let get_iter_at_byte buffer ~line index =
181 let iter = buffer#get_iter_at_byte ~line 0 in
182 let rec adjust iter cnt =
183 if cnt <= 0 then
184 iter
185 else
186 adjust (iter#forward_char) (cnt - (ulen iter#char))
187 in
188 adjust iter index
189
190 (** convert UTF-8 byte offset (used by Rocq) to unicode offset (used by GTK buffer) *)
191 let byte_off_to_buffer_off buffer byte_off =
192 let rec cvt iter cnt =
193 if cnt <= 0 then
194 iter#offset
195 else
196 cvt iter#forward_char (cnt - (ulen iter#char))
197 in
198 cvt (buffer#get_iter `START) byte_off
199
200 (** convert unicode offset (used by GTK buffer) to UTF-8 byte offset (used by Rocq) *)
201 let buffer_off_to_byte_off (buffer : GText.buffer) uni_off =
202 let rec cvt iter rv =
203 if iter#offset <= 0 then
204 rv
205 else
206 cvt iter#backward_char (rv + (ulen iter#char))
207 in
208 cvt (buffer#get_iter (`OFFSET uni_off)) 0
209
210 let do_convert s =
211 let from_loc () =
212 let _,char_set = Glib.Convert.get_charset () in
213 flash_info ("Converting from locale ("^char_set^")");
214 Glib.Convert.convert_with_fallback
215 ~to_codeset:"UTF-8" ~from_codeset:char_set s
216 in
217 let from_manual enc =
218 flash_info ("Converting from "^ enc);
219 Glib.Convert.convert s ~to_codeset:"UTF-8" ~from_codeset:enc
220 in
221 let s =
222 match encoding#get with
223 | Eutf8 when Glib.Utf8.validate s -> (Minilib.log "Input is UTF-8"; s)
224 | Eutf8 | Elocale -> from_loc ()
225 | Emanual enc -> try from_manual enc with _ -> from_loc ()
226 in
227 Utf8_convert.f s
228
229 let try_convert s =
230 try
231 do_convert s
232 with _ ->
233 "(* Fatal error: wrong encoding in input. \
234 Please choose a correct encoding in the preference panel.*)";;
235
236 let export file_name s =
237 let oc = open_out_bin file_name in
238 let ending = line_ending#get in
239 let is_windows = ref false in
240 for i = 0 to String.length s - 1 do
241 match s.[i] with
242 | '\r' -> is_windows := true
243 | '\n' ->
244 begin match ending with
245 | `DEFAULT ->
246 if !is_windows then (output_char oc '\r'; output_char oc '\n')
247 else output_char oc '\n'
248 | `WINDOWS -> output_char oc '\r'; output_char oc '\n'
249 | `UNIX -> output_char oc '\n'
250 end
251 | c -> output_char oc c
252 done;
253 close_out oc
254
255 let try_export file_name s =
256 let s =
257 try match encoding#get with
258 |Eutf8 -> Minilib.log "UTF-8 is enforced" ; s
259 |Elocale ->
260 let is_unicode,char_set = Glib.Convert.get_charset () in
261 if is_unicode then
262 (Minilib.log "Locale is UTF-8" ; s)
263 else
264 (Minilib.log ("Locale is "^char_set);
265 Glib.Convert.convert_with_fallback
266 ~from_codeset:"UTF-8" ~to_codeset:char_set s)
267 |Emanual enc ->
268 (Minilib.log ("Manual charset is "^ enc);
269 Glib.Convert.convert_with_fallback
270 ~from_codeset:"UTF-8" ~to_codeset:enc s)
271 with e ->
272 let str = Printexc.to_string e in
273 Minilib.log ("Error ("^str^") in transcoding: falling back to UTF-8");
274 s
275 in
276 try export file_name s; true
277 with e -> Minilib.log (Printexc.to_string e);false
278
279 type timer = { run : ms:int -> callback:(unit->bool) -> unit;
280 kill : unit -> unit }
281
282 let mktimer () =
283 let timer = ref None in
284 { run =
285 (fun ~ms ~callback ->
286 timer := Some (GMain.Timeout.add ~ms ~callback));
287 kill =
288 (fun () -> match !timer with
289 | None -> ()
290 | Some id ->
291 (try GMain.Timeout.remove id
292 with Glib.GError _ -> ());
293 timer := None) }
294
295 let filter_all_files () = GFile.filter
296 ~name:"All"
297 ~patterns:["*"] ()
298
299 let filter_rocq_files () = GFile.filter
300 ~name:"Coq source code"
301 ~patterns:[ "*.v"] ()
302
303 let current_dir () = match project_path#get with
304 | None -> ""
305 | Some dir -> dir
306
307 let select_file_for_open ~title ?(filter=true) ?(multiple=false) ?parent ?filename () =
308 let file_chooser =
309 GWindow.file_chooser_dialog ~action:`OPEN ~modal:true ~title ?parent ()
310 in
311 file_chooser#add_button_stock `CANCEL `CANCEL ;
312 file_chooser#add_select_button_stock `OPEN `OPEN ;
313 if filter then
314 begin
315 file_chooser#add_filter (filter_rocq_files ());
316 file_chooser#add_filter (filter_all_files ())
317 end;
318 file_chooser#set_default_response `OPEN;
319 file_chooser#set_select_multiple multiple;
320 let dir = match filename with
321 | None -> current_dir ()
322 | Some f -> Filename.dirname f in
323 ignore (file_chooser#set_current_folder dir);
324 let file =
325 match file_chooser#run () with
326 | `OPEN ->
327 begin
328 let filenames = file_chooser#get_filenames in
329 (if filenames <> [] then
330 project_path#set file_chooser#current_folder);
331 filenames
332 end
333 | `DELETE_EVENT | `CANCEL -> [] in
334 file_chooser#destroy ();
335 file
336
337 let select_file_for_save ~title ?parent ?filename () =
338 let file = ref None in
339 let file_chooser =
340 GWindow.file_chooser_dialog ~action:`SAVE ~modal:true ~title ?parent ()
341 in
342 file_chooser#add_button_stock `CANCEL `CANCEL ;
343 file_chooser#add_select_button_stock `SAVE `SAVE ;
344 file_chooser#add_filter (filter_rocq_files ());
345 file_chooser#add_filter (filter_all_files ());
346 file_chooser#set_do_overwrite_confirmation true;
347 file_chooser#set_default_response `SAVE;
348 let dir,filename = match filename with
349 |None -> current_dir (), ""
350 |Some f -> Filename.dirname f, Filename.basename f
351 in
352 ignore (file_chooser#set_current_folder dir);
353 ignore (file_chooser#set_current_name filename);
354 begin match file_chooser#run () with
355 | `SAVE ->
356 begin
357 file := file_chooser#filename;
358 match !file with
359 None -> ()
360 | Some s -> project_path#set file_chooser#current_folder
361 end
362 | `DELETE_EVENT | `CANCEL -> ()
363 end ;
364 file_chooser#destroy ();
365 !file
366
367 let find_tag_start (tag :GText.tag) (it:GText.iter) =
368 let it = it#copy in
369 let tag = Some tag in
370 while not (it#begins_tag tag) && it#nocopy#backward_char do
371 ()
372 done;
373 it
374 let find_tag_stop (tag :GText.tag) (it:GText.iter) =
375 let it = it#copy in
376 let tag = Some tag in
377 while not (it#ends_tag tag) && it#nocopy#forward_char do
378 ()
379 done;
380 it
381 let find_tag_limits (tag :GText.tag) (it:GText.iter) =
382 (find_tag_start tag it , find_tag_stop tag it)
383
384 let stock_to_widget ?(size=`BUTTON) s =
385 let img = GMisc.image () in
386 (match size with
387 | `CUSTOM(width,height) ->
388 let opb = img#misc#render_icon ~size:`BUTTON s in
389 let pb = GdkPixbuf.create ~width ~height
390 ~bits:(GdkPixbuf.get_bits_per_sample opb)
391 ~has_alpha:(GdkPixbuf.get_has_alpha opb) () in
392 GdkPixbuf.scale ~width ~height ~dest:pb opb;
393 img#set_pixbuf pb
394 | #Gtk.Tags.icon_size as icon_size ->
395 img#set_stock s;
396 img#set_icon_size icon_size);
397 img#coerce
398
399 let custom_rocqtop = ref None
400
401 let rocqtop_path () =
402 let file = match !custom_rocqtop with
403 | Some s -> s
404 | None ->
405 match cmd_rocqtop#get with
406 | Some s -> s
407 | None ->
408 try
409 let new_prog = System.get_toplevel_path "coqidetop" in
410 (* The file exists or it is to be found by path *)
411 if Sys.file_exists new_prog ||
412 CString.equal Filename.(basename new_prog) new_prog
413 then new_prog
414 else
415 let in_macos_bundle =
416 Filename.concat
417 (Filename.dirname new_prog)
418 (Filename.concat "../Resources/bin" (Filename.basename new_prog))
419 in if Sys.file_exists in_macos_bundle then in_macos_bundle
420 else "coqidetop.opt"
421 with Not_found -> "coqidetop.opt"
422 in file
423
424 (* In win32, when a command-line is to be executed via cmd.exe
425 (i.e. Sys.command, Unix.open_process, ...), it cannot contain several
426 quoted "..." zones otherwise some quotes are lost. Solution: we re-quote
427 everything. Reference: http://ss64.com/nt/cmd.html *)
428
429 let requote cmd = if Sys.os_type = "Win32" then "\""^cmd^"\"" else cmd
430
431 let textview_width (view : #GText.view_skel) =
432 let rect = view#visible_rect in
433 let pixel_width = Gdk.Rectangle.width rect in
434 let metrics = view#misc#pango_context#get_metrics () in
435 let char_width = GPango.to_pixels metrics#approx_char_width in
436 pixel_width / char_width
437
438 type logger = Feedback.level -> Pp.t -> unit
439
440 let default_logger level message =
441 let level = match level with
442 | Feedback.Debug -> `DEBUG
443 | Feedback.Info -> `INFO
444 | Feedback.Notice -> `NOTICE
445 | Feedback.Warning -> `WARNING
446 | Feedback.Error -> `ERROR
447 in
448 Minilib.log_pp ~level message
449
450
451 (** {6 File operations} *)
452
453 (** A customized [stat] function. Exceptions are caught. *)
454
455 type stats = MTime of float | NoSuchFile | OtherError
456
457 let stat f =
458 try MTime (Unix.stat f).Unix.st_mtime
459 with
460 | Unix.Unix_error (Unix.ENOENT,_,_) -> NoSuchFile
461 | _ -> OtherError
462
463 (** I/O utilities
464
465 Note: In a mono-thread rocqide, we use the same buffer for
466 different read operations *)
467
468 let maxread = 4096
469
470 let read_string = Bytes.create maxread
471 let read_buffer = Buffer.create maxread
472
473 (** Read the content of file [f] and add it to buffer [b].
474 I/O Exceptions are propagated. *)
475
476 let read_file name buf =
477 let ic = Util.open_utf8_file_in name in
478 let len = ref 0 in
479 try
480 while len := input ic read_string 0 maxread; !len > 0 do
481 Buffer.add_subbytes buf read_string 0 !len
482 done;
483 close_in ic
484 with e -> close_in ic; raise e
485
486 (** Read what is available on a gtk channel. This channel should have been
487 set as non-blocking. When there's nothing more to read, the inner loop
488 will be exited via a GError exception concerning a EAGAIN unix error.
489 Anyway, any other exception also stops the read. *)
490
491 let io_read_all chan =
492 Buffer.clear read_buffer;
493 let read_once () =
494 let len = Glib.Io.read_chars ~buf:read_string ~pos:0 ~len:maxread chan in
495 Buffer.add_subbytes read_buffer read_string 0 len
496 in
497 begin
498 try while true do read_once () done
499 with Glib.GError _ -> ()
500 end;
501 Buffer.contents read_buffer
502
503 (** Run an external command asynchronously *)
504
505 let run_command display finally cmd =
506 Minilib.log ("Start async command: " ^ cmd);
507 let cin = Unix.open_process_in cmd in
508 let fd = Unix.descr_of_in_channel cin in
509 let () = Unix.set_nonblock fd in
510 let io_chan = Glib.Io.channel_of_descr fd in
511 let all_conds = [`ERR; `HUP; `IN; `NVAL; `PRI] in (* all except `OUT *)
512 let rec has_errors = function
513 | [] -> false
514 | (`IN | `PRI) :: conds -> has_errors conds
515 | e :: _ -> true
516 in
517 let handle_end () = finally (Unix.close_process_in cin); false
518 in
519 let handle_input conds =
520 if has_errors conds then handle_end ()
521 else
522 let s = io_read_all io_chan in
523 if s = "" then handle_end ()
524 else (display (try_convert s); true)
525 in
526 ignore (Glib.Io.add_watch ~cond:all_conds ~callback:handle_input io_chan)
527
528 (** Web browsing *)
529
530 let browse prerr url =
531 let com = Util.subst_command_placeholder cmd_browse#get url in
532 let finally = function
533 | Unix.WEXITED 127 ->
534 prerr
535 ("Could not execute:\n"^com^"\n"^
536 "check your preferences for setting a valid browser command\n")
537 | _ -> ()
538 in
539 run_command (fun _ -> ()) finally com
540
541 let url_for_keyword =
542 let ht = Hashtbl.create 97 in
543 lazy (
544 begin try
545 let cin =
546 try let index_urls = Filename.concat (List.find
547 (fun x -> Sys.file_exists (Filename.concat x "index_urls.txt"))
548 (Minilib.rocqide_data_dirs ())) "index_urls.txt" in
549 open_in index_urls
550 with Not_found -> raise_notrace Exit
551 in
552 try while true do
553 let s = input_line cin in
554 try
555 let i = String.index s ',' in
556 let k = String.sub s 0 i in
557 let u = String.sub s (i + 1) (String.length s - i - 1) in
558 Hashtbl.add ht k u
559 with _ ->
560 Minilib.log "Warning: Cannot parse documentation index file."
561 done with End_of_file ->
562 close_in cin
563 with _ ->
564 Minilib.log "Warning: Cannot find documentation index file."
565 end;
566 Hashtbl.find ht : string -> string)
567
568 let browse_keyword prerr text =
569 try
570 let u = Lazy.force url_for_keyword text in
571 browse prerr (Coq_config.wwwrefman ^ u)
572 with Not_found -> prerr ("No documentation found for \""^text^"\".\n")
573
574 let rec is_valid (s : Pp.t) = match Pp.repr s with
575 | Pp.Ppcmd_empty
576 | Pp.Ppcmd_print_break _
577 | Pp.Ppcmd_force_newline -> true
578 | Pp.Ppcmd_glue l -> List.for_all is_valid l
579 | Pp.Ppcmd_string s | Ppcmd_sized_string (_,s) -> Glib.Utf8.validate s
580 | Pp.Ppcmd_box (_,s)
581 | Pp.Ppcmd_tag (_,s) -> is_valid s
582 | Pp.Ppcmd_comment s -> List.for_all Glib.Utf8.validate s
583 let validate s =
584 if is_valid s then s else Pp.str "This error massage can't be printed."
585
586 (** encoding list of strings as a string in a shell-like compatible way:
587 string with spaces and no ' -> '...'
588 string with spaces and ' -> split string into substrings separated with \'
589 ' -> \'
590 \ -> \\
591 *)
592
593 let decode_string_list s =
594 let l = String.length s in
595 let fail_backslash () =
596 failwith "Backslash is used to quote single quotes in quoted strings; it should otherwise be doubled" in
597 let rec find_word quoted b i =
598 if i = l then
599 if quoted then failwith "Unmatched single quote"
600 else i
601 else
602 let c = s.[i] in
603 if c = ' ' && not quoted then i+1
604 else if c = '\'' then find_word (not quoted) b (i+1)
605 else if c = '\\' && not quoted then
606 if i = l-1 then fail_backslash ()
607 else
608 let c = s.[i+1] in
609 if c = '\'' || c = '\\' then
610 (Buffer.add_char b c; find_word quoted b (i+2))
611 else fail_backslash ()
612 else
613 (Buffer.add_char b c;
614 find_word quoted b (i+1)) in
615 let rec collect_words i =
616 if i = l then [] else
617 let b = Buffer.create l in
618 let i = find_word false b i in
619 Buffer.contents b :: collect_words i in
620 collect_words 0
621
622 let needs_quote s i =
623 (* Tells if there is a space and if a space, before the next single quote *)
624 match CString.index_from_opt s i ' ', CString.index_from_opt s i '\'' with
625 | Some _, None -> true
626 | Some i, Some j -> i < j
627 | _ -> false
628
629 let encode_string s =
630 (* Could be optimized so that "a b'c" is "'a b'\'c" rather than "'a b'\''c'" *)
631 let l = String.length s in
632 let b = Buffer.create (l + 10) in
633 let close quoted = if quoted then Buffer.add_char b '\'' in
634 let rec aux quoted i =
635 if i = l then close quoted
636 else
637 let c = s.[i] in
638 if c = '\'' then
639 (close quoted;
640 Buffer.add_string b "\\'";
641 start (i+1))
642 else if c = '\\' && not quoted then
643 (Buffer.add_string b "\\\\"; aux quoted (i+1))
644 else
645 (Buffer.add_char b c; aux quoted (i+1))
646 and start i =
647 let q = needs_quote s i in
648 if q then Buffer.add_char b '\'';
649 aux q i in
650 start 0;
651 Buffer.contents b
652
653 let encode_string_list l = String.concat " " (List.map encode_string l)
654
655 let filter_key ev =
656 let filter mods = List.filter (fun m -> List.mem m [`SHIFT; `CONTROL; `MOD1 (* Alt *)]) mods in
657 GdkEvent.Key.(keyval ev, (filter (state ev)))