newmain.ml 41.6 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
3
(*  Copyright (C) 2010-2011                                               *)
4 5 6
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)


open Format

let () =
24
  eprintf "[Info] Init the GTK interface...@?";
25 26 27 28
  ignore (GtkMain.Main.init ());
  eprintf " done.@."


29
open Why3
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
open Whyconf
open Gconfig

(************************)
(* parsing command line *)
(************************)

let includes = ref []
let file = ref None
let opt_version = ref false

let spec = Arg.align [
  ("-I",
   Arg.String (fun s -> includes := s :: !includes),
   "<s> add s to loadpath") ;
(*
  ("-f",
   Arg.String (fun s -> input_files := s :: !input_files),
   "<f> add file f to the project (ignored if it is already there)") ;
*)
  ("-v",
   Arg.Set opt_version,
   " print version information") ;
]

let version_msg = sprintf "Why3 IDE, version %s (build date: %s)"
  Config.version Config.builddate

let usage_str = sprintf
  "Usage: %s [options] [<file.why>|<project directory>]"
  (Filename.basename Sys.argv.(0))

let set_file f = match !file with
  | Some _ ->
      raise (Arg.Bad "only one parameter")
  | None ->
      file := Some f

let () = Arg.parse spec set_file usage_str

let () =
  if !opt_version then begin
    printf "%s@." version_msg;
    exit 0
  end

let fname = match !file with
  | None ->
      Arg.usage spec usage_str;
      exit 1
  | Some f ->
      f

let lang =
  let main = get_main () in
  let load_path = Filename.concat (datadir main) "lang" in
  let languages_manager =
    GSourceView2.source_language_manager ~default:true
  in
  languages_manager#set_search_path
    (load_path :: languages_manager#search_path);
  match languages_manager#language "why" with
    | None ->
        Format.eprintf "language file for 'why' not found in directory %s@."
          load_path;
        exit 1
    | Some _ as l -> l

let source_text fname =
  let ic = open_in fname in
  let size = in_channel_length ic in
  let buf = String.create size in
  really_input ic buf 0 size;
  close_in ic;
  buf

(********************************)
(* loading WhyIDE configuration *)
(********************************)

let gconfig =
  let c = Gconfig.config in
  let loadpath = (Whyconf.loadpath (get_main ())) @ List.rev !includes in
Andrei Paskevich's avatar
Andrei Paskevich committed
113
  c.env <- Env.create_env_of_loadpath loadpath;
114
(*
115 116
  let provers = Whyconf.get_provers c.Gconfig.config in
  c.provers <-
117
    Util.Mstr.fold (Session.get_prover_data c.env) provers Util.Mstr.empty;
118
*)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  c

let () =
  Whyconf.load_plugins (get_main ())



(***************)
(* Main window *)
(***************)

let w = GWindow.window
  ~allow_grow:true ~allow_shrink:true
  ~width:gconfig.window_width
  ~height:gconfig.window_height
MARCHE Claude's avatar
MARCHE Claude committed
134
  ~title:"Why3 Interactive Proof Session" ()
135

MARCHE Claude's avatar
MARCHE Claude committed
136 137 138
let () =
  w#set_icon (Some !Gconfig.why_icon)

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
let (_ : GtkSignal.id) =
  w#misc#connect#size_allocate
    ~callback:
    (fun {Gtk.width=w;Gtk.height=h} ->
       gconfig.window_height <- h;
       gconfig.window_width <- w)

let vbox = GPack.vbox ~packing:w#add ()

(* Menu *)

let menubar = GMenu.menu_bar ~packing:vbox#pack ()

let factory = new GMenu.factory menubar

let accel_group = factory#accel_group

let hb = GPack.hbox ~packing:vbox#add ~border_width:2 ()

let tools_window_vbox =
  try
    GPack.vbox ~packing:(hb#pack ~expand:false) ~border_width:2 ()
  with Gtk.Error _ -> assert false

let context_frame =
  GBin.frame ~label:"Context"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let context_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
    ~packing:context_frame#add ()

let context_unproved_goals_only = ref true

let () =
  let b1 = GButton.radio_button
    ~packing:context_box#add ~label:"Unproved goals" ()
  in
177
  b1#misc#set_tooltip_markup "When selected, tools below are applied only to <b>unproved</b> goals";
178 179 180 181 182 183 184
  let (_ : GtkSignal.id) =
    b1#connect#clicked
      ~callback:(fun () -> context_unproved_goals_only := true)
  in
  let b2 = GButton.radio_button
    ~group:b1#group ~packing:context_box#add ~label:"All goals" ()
  in
185
  b2#misc#set_tooltip_markup "When selected, tools below are applied to all goals";
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  let (_ : GtkSignal.id) =
    b2#connect#clicked
      ~callback:(fun () -> context_unproved_goals_only := false)
  in ()


let provers_frame =
  GBin.frame ~label:"Provers"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let provers_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:provers_frame#add ()

let () = provers_frame#set_resize_mode `PARENT

let transf_frame =
  GBin.frame ~label:"Transformations"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let transf_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:transf_frame#add ()

let tools_frame =
  GBin.frame ~label:"Tools"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let tools_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:tools_frame#add ()

let cleaning_frame =
  GBin.frame ~label:"Cleaning"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let cleaning_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:cleaning_frame#add ()


(* horizontal paned *)

let hp = GPack.paned `HORIZONTAL ~packing:hb#add ()


(* tree view *)
let scrollview =
  try
    GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
      ~width:gconfig.tree_width
      ~packing:hp#add ()
  with Gtk.Error _ -> assert false

let () = scrollview#set_shadow_type `ETCHED_OUT
let (_ : GtkSignal.id) =
  scrollview#misc#connect#size_allocate
    ~callback:
    (fun {Gtk.width=w;Gtk.height=_h} ->
       gconfig.tree_width <- w)




(****************)
(* goals widget *)
(****************)

let cols = new GTree.column_list
let name_column = cols#add Gobject.Data.string
let icon_column = cols#add Gobject.Data.gobject
let status_column = cols#add Gobject.Data.gobject
let time_column = cols#add Gobject.Data.string
259
let index_column = cols#add Gobject.Data.int
260

261 262 263 264
let name_renderer = GTree.cell_renderer_text [`XALIGN 0.]
let renderer = GTree.cell_renderer_text [`XALIGN 0.]
let image_renderer = GTree.cell_renderer_pixbuf [ ]
let icon_renderer = GTree.cell_renderer_pixbuf [ ]
265

266 267
let view_name_column =
  GTree.view_column ~title:"Theories/Goals" ()
268

269 270 271 272 273 274 275 276
let () =
  view_name_column#pack icon_renderer ;
  view_name_column#add_attribute icon_renderer "pixbuf" icon_column ;
  view_name_column#pack name_renderer;
  view_name_column#add_attribute name_renderer "text" name_column;
  view_name_column#set_resizable true;
  view_name_column#set_max_width 400;
  ()
277

278 279 280 281
let view_status_column =
  GTree.view_column ~title:"Status"
    ~renderer:(image_renderer, ["pixbuf", status_column])
    ()
282

283 284 285
let view_time_column =
  GTree.view_column ~title:"Time"
    ~renderer:(renderer, ["text", time_column]) ()
286

287 288 289 290 291
let () =
  view_status_column#set_resizable false;
  view_status_column#set_visible true;
  view_time_column#set_resizable false;
  view_time_column#set_visible true
292

293
let goals_model,goals_view =
294
  eprintf "[Info] Creating tree model...@?";
295 296
  let model = GTree.tree_store cols in
  let view = GTree.view ~model ~packing:scrollview#add () in
297
  let () = view#selection#set_mode (* `SINGLE *) `MULTIPLE in
298 299 300 301
  let () = view#set_rules_hint true in
  ignore (view#append_column view_name_column);
  ignore (view#append_column view_status_column);
  ignore (view#append_column view_time_column);
302
  eprintf " done@.";
303
  model,view
304

305 306 307 308
let clear model = model#clear ()

let image_of_result ~obsolete result =
  match result with
MARCHE Claude's avatar
MARCHE Claude committed
309
    | Session.Undone -> !image_undone
310 311 312 313
    | Session.Scheduled -> !image_scheduled
    | Session.Running -> !image_running
    | Session.InternalFailure _ -> !image_failure
    | Session.Done r -> match r.Call_provers.pr_answer with
314 315 316 317 318 319 320 321 322 323 324 325
        | Call_provers.Valid ->
            if obsolete then !image_valid_obs else !image_valid
        | Call_provers.Invalid ->
            if obsolete then !image_invalid_obs else !image_invalid
        | Call_provers.Timeout ->
            if obsolete then !image_timeout_obs else !image_timeout
        | Call_provers.Unknown _ ->
            if obsolete then !image_unknown_obs else !image_unknown
        | Call_provers.Failure _ ->
            if obsolete then !image_failure_obs else !image_failure
        | Call_provers.HighFailure ->
            if obsolete then !image_failure_obs else !image_failure
326

327
(* connecting to the Session model *)
328 329 330

module M = Session.Make
  (struct
331 332 333 334 335 336 337 338
     type key = GTree.row_reference

     let create ?parent () =
       let parent = match parent with
         | None -> None
         | Some r -> Some r#iter
       in
       let iter = goals_model#append ?parent () in
MARCHE Claude's avatar
MARCHE Claude committed
339
       goals_model#set ~row:iter ~column:index_column (-1);
340
       goals_model#get_row_reference (goals_model#get_path iter)
341 342


343
     let remove row =
344
       let (_:bool) = goals_model#remove row#iter in ()
345

MARCHE Claude's avatar
MARCHE Claude committed
346 347
     let reset () = goals_model#clear ()

348
     let idle f =
349
       let (_ : GMain.Idle.id) = GMain.Idle.add f in ()
350

351
     let timeout ~ms f =
352 353 354 355
       let (_ : GMain.Timeout.id) = GMain.Timeout.add ~ms ~callback:f in
       ()

   end)
MARCHE Claude's avatar
MARCHE Claude committed
356 357 358

let set_row_status row b =
  if b then
359
    goals_model#set ~row:row#iter ~column:status_column !image_yes
MARCHE Claude's avatar
MARCHE Claude committed
360
  else
361
    goals_model#set ~row:row#iter ~column:status_column !image_unknown
MARCHE Claude's avatar
MARCHE Claude committed
362 363 364 365

let set_proof_state ~obsolete a =
  let row = a.M.proof_key in
  let res = a.M.proof_state in
366
  goals_model#set ~row:row#iter ~column:status_column
MARCHE Claude's avatar
MARCHE Claude committed
367 368 369
    (image_of_result ~obsolete res);
  let t = match res with
    | Session.Done { Call_provers.pr_time = time } ->
370
        Format.sprintf "%.2f" time
MARCHE Claude's avatar
MARCHE Claude committed
371 372 373
    | _ -> ""
  in
  let t = if obsolete then t ^ " (obsolete)" else t in
374
  goals_model#set ~row:row#iter ~column:time_column t
MARCHE Claude's avatar
MARCHE Claude committed
375

376 377
let model_index = Hashtbl.create 17

378

379
let get_any_from_iter row =
MARCHE Claude's avatar
MARCHE Claude committed
380
  try
381 382
    let idx = goals_model#get ~row ~column:index_column in
    Hashtbl.find model_index idx
383 384
  with Not_found -> invalid_arg "Gmain.get_any_from_iter"

385
(*
386 387
let get_any (row:Gtk.tree_path) : M.any =
  get_any_from_iter (goals_model#get_iter row)
388 389 390 391 392 393 394 395
*)

let get_any_from_row_reference r = get_any_from_iter r#iter

let get_selected_row_references () =
  List.map
    (fun path -> goals_model#get_row_reference path)
    goals_view#selection#get_selected_rows
396

397
let row_expanded b iter _path =
398
  match get_any_from_iter iter with
399
    | M.File f ->
400
(*
401
        eprintf "file_expanded <- %b@." b;
402
*)
403
        M.set_file_expanded f b
404
    | M.Theory t ->
405
(*
406
        eprintf "theory_expanded <- %b@." b;
407
*)
408
        M.set_theory_expanded t b
409
    | M.Goal g ->
410
(*
411
        eprintf "goal_expanded <- %b@." b;
412
*)
413
        M.set_goal_expanded g b
414
    | M.Transformation tr ->
415
(*
416
        eprintf "transf_expanded <- %b@." b;
417
*)
418
        M.set_transf_expanded tr b
419 420
    | M.Proof_attempt _ -> ()

421

422
let (_:GtkSignal.id) =
423 424
  goals_view#connect#row_collapsed ~callback:(row_expanded false)

425
let (_:GtkSignal.id) =
426
  goals_view#connect#row_expanded ~callback:(row_expanded true)
427

MARCHE Claude's avatar
MARCHE Claude committed
428
let notify any =
429 430
  let row,exp =
    match any with
431
      | M.Goal g ->
432
(*
433 434
          if M.goal_expanded g then
            begin
435
              let n =
436 437 438 439
                Hashtbl.fold (fun _ _ acc -> acc+1) (M.external_proofs g) 0
              in
              eprintf "expand_row on a goal with %d proofs@." n;
            end;
440
*)
441
          (M.goal_key g),(M.goal_expanded g)
442 443 444
      | M.Theory t -> (M.theory_key t),(M.theory_expanded t)
      | M.File f -> f.M.file_key,f.M.file_expanded
      | M.Proof_attempt a -> a.M.proof_key,false
445
      | M.Transformation tr -> tr.M.transf_key,tr.M.transf_expanded
446
  in
447
  if exp then goals_view#expand_to_path row#path else
448
    goals_view#collapse_row row#path;
MARCHE Claude's avatar
MARCHE Claude committed
449 450
  match any with
    | M.Goal g ->
451
        set_row_status row (M.goal_proved g)
MARCHE Claude's avatar
MARCHE Claude committed
452
    | M.Theory th ->
453
        set_row_status row (M.verified th)
MARCHE Claude's avatar
MARCHE Claude committed
454
    | M.File file ->
455
        set_row_status row file.M.file_verified
MARCHE Claude's avatar
MARCHE Claude committed
456
    | M.Proof_attempt a ->
457
        set_proof_state ~obsolete:a.M.proof_obsolete a
MARCHE Claude's avatar
MARCHE Claude committed
458
    | M.Transformation tr ->
459
        set_row_status row tr.M.transf_proved
460

MARCHE Claude's avatar
MARCHE Claude committed
461 462
let init =
  let cpt = ref (-1) in
463
  fun row any ->
MARCHE Claude's avatar
MARCHE Claude committed
464 465 466 467 468 469 470 471 472 473 474
    let ind = goals_model#get ~row:row#iter ~column:index_column in
    if ind < 0 then
      begin
        incr cpt;
        Hashtbl.add model_index !cpt any;
        goals_model#set ~row:row#iter ~column:index_column !cpt
      end
    else
      begin
        Hashtbl.replace model_index ind any;
      end;
475
    (* useless since it has no child: goals_view#expand_row row#path; *)
MARCHE Claude's avatar
MARCHE Claude committed
476
    goals_model#set ~row:row#iter ~column:icon_column
477
      (match any with
478 479 480 481 482
         | M.Goal _ -> !image_file
         | M.Theory _
         | M.File _ -> !image_directory
         | M.Proof_attempt _ -> !image_prover
         | M.Transformation _ -> !image_transf);
MARCHE Claude's avatar
MARCHE Claude committed
483
    goals_model#set ~row:row#iter ~column:name_column
484
      (match any with
485 486 487
         | M.Goal g -> M.goal_expl g
         | M.Theory th -> M.theory_name th
         | M.File f -> Filename.basename f.M.file_name
488
         | M.Proof_attempt a ->
489 490 491
             begin
               match a.M.prover with
                 | M.Detected_prover p ->
492
                     p.Session.prover_name ^ " " ^ p.Session.prover_version
493 494
                 | M.Undetected_prover s -> s
             end
495
         | M.Transformation tr -> Session.transformation_id tr.M.transf);
MARCHE Claude's avatar
MARCHE Claude committed
496
    notify any
MARCHE Claude's avatar
MARCHE Claude committed
497

498 499


500 501 502 503 504 505 506 507 508
(********************)
(* opening database *)
(********************)

let project_dir, file_to_read =
  if Sys.file_exists fname then
    begin
      if Sys.is_directory fname then
        begin
509
          eprintf "[Info] found directory '%s' for the project@." fname;
510 511 512 513
          fname, None
        end
      else
        begin
514
          eprintf "[Info] found regular file '%s'@." fname;
515 516 517 518
          let d =
            try Filename.chop_extension fname
            with Invalid_argument _ -> fname
          in
519
          eprintf "[Info] using '%s' as directory for the project@." d;
520
          d, Some (Filename.concat Filename.parent_dir_name
521
                     (Filename.basename fname))
522 523 524 525 526 527 528 529
        end
    end
  else
    fname, None

let () =
  if not (Sys.file_exists project_dir) then
    begin
530
      eprintf "[Info] '%s' does not exists. Creating directory of that name \
531 532 533 534
 for the project@." project_dir;
      Unix.mkdir project_dir 0o777
    end

535

536 537 538 539 540 541 542 543 544 545 546
let info_window ?(callback=(fun () -> ())) mt s =
  let buttons = match mt with
    | `INFO -> GWindow.Buttons.close
    | `WARNING -> GWindow.Buttons.close
    | `QUESTION -> GWindow.Buttons.ok_cancel
    | `ERROR -> GWindow.Buttons.close
  in
  let d = GWindow.message_dialog
    ~message:s
    ~message_type:mt
    ~buttons
547
    ~title:"Why3IDE"
548
    ~modal:true
549 550 551 552
    ~show:true ()
  in
  let (_ : GtkSignal.id) =
    d#connect#response
553 554 555
      ~callback:(function x -> 
                   d#destroy ();
                   if mt <> `QUESTION || x = `OK then callback ())
556 557
  in ()

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
(* check if provers are present *)
let () =
  if Util.Mstr.is_empty (Whyconf.get_provers gconfig.Gconfig.config) then
    begin
      info_window `ERROR "No prover configured.\nPlease run 'why3config --detect-provers' first" 
        ~callback:GMain.quit;
      GMain.main ();
      exit 2;
    end

let () =
  try
    eprintf "[Info] Opening session...@\n@[<v 2>  ";
    M.open_session ~env:gconfig.env
      (* ~provers:gconfig.provers *)
      ~config:gconfig.Gconfig.config
      ~init ~notify project_dir;
    M.maximum_running_proofs := gconfig.max_running_processes;
    eprintf "@]@\n[Info] Opening session: done@."
  with e ->
    eprintf "@[Error while opening session:@ %a@.@]"
      Exn_printer.exn_printer e;
    exit 1


583 584 585 586 587 588 589 590 591

(**********************************)
(* add new file from command line *)
(**********************************)

let () =
  match file_to_read with
    | None -> ()
    | Some fn ->
592
        if M.file_exists fn then
593
          eprintf "[Info] file %s already in database@." fn
594 595
        else
          try
MARCHE Claude's avatar
MARCHE Claude committed
596
            M.add_file fn
597
          with e ->
598
            eprintf "@[Error while reading file@ '%s':@ %a@.@]" fn
599
              Exn_printer.exn_printer e;
600
            exit 1
601 602 603 604 605 606 607 608 609 610




(*****************************************************)
(* method: run a given prover on each unproved goals *)
(*****************************************************)

let prover_on_selected_goals pr =
  List.iter
611
    (fun row ->
612
       let a = get_any_from_row_reference row in
613 614
        M.run_prover
          ~context_unproved_goals_only:!context_unproved_goals_only
615
          ~timelimit:gconfig.time_limit
616
          pr a)
617
    (get_selected_row_references ())
618 619 620 621 622 623 624

(**********************************)
(* method: replay obsolete proofs *)
(**********************************)

let replay_obsolete_proofs () =
  List.iter
625 626
    (fun r ->
       let a = get_any_from_row_reference r in
MARCHE Claude's avatar
MARCHE Claude committed
627
       M.replay ~obsolete_only:true
628
         ~context_unproved_goals_only:!context_unproved_goals_only a)
629
    (get_selected_row_references ())
630

631 632 633 634 635 636 637 638 639 640
(***********************************)
(* method: mark proofs as obsolete *)
(***********************************)

let cancel_proofs () =
  List.iter
    (fun r ->
       let a = get_any_from_row_reference r in
       M.cancel a)
    (get_selected_row_references ())
641 642 643 644 645 646


(*****************************************************)
(* method: split selected goals *)
(*****************************************************)

647 648 649 650
let lookup_trans = Session.lookup_transformation gconfig.env
let split_transformation = lookup_trans "split_goal"
let inline_transformation = lookup_trans "inline_goal"
let intro_transformation = lookup_trans "introduce_premises"
651 652


653
let apply_trans_on_selection tr =
654
  List.iter
655 656
    (fun r ->
       let a = get_any_from_row_reference r in
657 658
        M.transform
          ~context_unproved_goals_only:!context_unproved_goals_only
659
          tr
660
          a)
661
    (get_selected_row_references ())
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692


(*********************************)
(* add a new file in the project *)
(*********************************)

let filter_all_files () =
  let f = GFile.filter ~name:"All" () in
  f#add_pattern "*" ;
  f

let filter_why_files () =
  GFile.filter
    ~name:"Why3 source files"
    ~patterns:[ "*.why"; "*.mlw"] ()

let select_file () =
  let d = GWindow.file_chooser_dialog ~action:`OPEN
    ~title:"Why3: Add file in project"
    ()
  in
  d#add_button_stock `CANCEL `CANCEL ;
  d#add_select_button_stock `OPEN `OPEN ;
  d#add_filter (filter_why_files ()) ;
  d#add_filter (filter_all_files ()) ;
  begin match d#run () with
  | `OPEN ->
      begin
        match d#filename with
          | None -> ()
          | Some f ->
693
              let f = Sysutil.relativize_filename project_dir f in
694 695
              eprintf "Adding file '%s'@." f;
              try
MARCHE Claude's avatar
MARCHE Claude committed
696
                M.add_file f
697
              with e ->
698
                fprintf str_formatter
699 700
                  "@[Error while reading file@ '%s':@ %a@]" f
                  Exn_printer.exn_printer e;
701 702
                let msg = flush_str_formatter () in
                info_window `ERROR msg
703 704 705 706 707 708 709 710 711
      end
  | `DELETE_EVENT | `CANCEL -> ()
  end ;
  d#destroy ()


let not_implemented () =
  info_window `INFO "This feature is not yet implemented, sorry."

712

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
(*************)
(* File menu *)
(*************)

let file_menu = factory#add_submenu "_File"
let file_factory = new GMenu.factory file_menu ~accel_group

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~key:GdkKeysyms._A
    ~label:"_Add file" ~callback:select_file
    ()

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~label:"_Preferences" ~callback:
    (fun () ->
       Gconfig.preferences gconfig;
729
       M.maximum_running_proofs := gconfig.max_running_processes)
730 731 732 733
    ()

let refresh_provers = ref (fun () -> ())

MARCHE Claude's avatar
MARCHE Claude committed
734 735
let add_refresh_provers f _msg =
(*
736
  eprintf "[Info] recording '%s' for refresh provers@." msg;
MARCHE Claude's avatar
MARCHE Claude committed
737
*)
738 739 740
  let rp = !refresh_provers in
  refresh_provers := (fun () -> rp (); f ())

741
(*
742 743 744 745
let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~label:"_Detect provers" ~callback:
    (fun () -> Gconfig.run_auto_detection gconfig; !refresh_provers () )
    ()
746
*)
747

748 749 750 751
let save_session () =
  eprintf "[Info] saving session@.";
  M.save_session ()

752
let exit_function () =
753
  eprintf "[Info] saving IDE config file@.";
754
  save_config ();
755
  (*
MARCHE Claude's avatar
MARCHE Claude committed
756 757 758 759 760 761 762
  eprintf "saving session (testing only)@.";
  begin
    M.test_save ();
    try
      let l = M.test_load () in
      eprintf "reloaded successfully %d elements@." (List.length l);
      match l with
763 764 765 766
        | [] -> ()
        | f :: _ ->
            eprintf "first element is a '%s' with %d sub-elements@."
              f.Xml.name (List.length f.Xml.elements);
MARCHE Claude's avatar
MARCHE Claude committed
767

MARCHE Claude's avatar
MARCHE Claude committed
768 769 770 771
    with e -> eprintf "test reloading failed with exception %s@."
      (Printexc.to_string e)
  end;
  let ret = Sys.command "xmllint --noout --dtdvalid share/why3session.dtd essai.xml" in
MARCHE Claude's avatar
MARCHE Claude committed
772
  if ret = 0 then eprintf "DTD validation succeeded, good!@.";
773
  *)
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
  match config.saving_policy with
    | 0 -> save_session (); GMain.quit ()
    | 1 -> GMain.quit ()
    | 2 ->
        let d = GWindow.message_dialog
          ~message:"Do you want to save the session?"
          ~message_type:`QUESTION
          ~buttons:GWindow.Buttons.yes_no
          ~title:"Why3 save"
          ~modal:true
          ~show:true ()
        in
        let (_ : GtkSignal.id) =
          d#connect#response
            ~callback:(function x -> d#destroy ();
                         if x = `YES then save_session ();
                         GMain.quit ()
                      )
        in
        ()
794
    | _ ->
795 796
        eprintf "unexpected value for saving_policy@.";
        GMain.quit ()
797

798 799 800 801 802 803 804 805 806 807 808 809 810
(*************)
(* View menu *)
(*************)

let view_menu = factory#add_submenu "_View"
let view_factory = new GMenu.factory view_menu ~accel_group
let (_ : GMenu.image_menu_item) =
  view_factory#add_image_item ~key:GdkKeysyms._E
    ~label:"Expand all" ~callback:(fun () -> goals_view#expand_all ()) ()



let rec collapse_proved_goal g =
MARCHE Claude's avatar
MARCHE Claude committed
811
  if M.goal_proved g then
812
    begin
MARCHE Claude's avatar
MARCHE Claude committed
813
      let row = M.goal_key g in
814
      goals_view#collapse_row row#path;
815 816 817
    end
  else
    Hashtbl.iter
818
      (fun _ t -> List.iter collapse_proved_goal t.M.subgoals)
MARCHE Claude's avatar
MARCHE Claude committed
819
      (M.transformations g)
820 821

let collapse_verified_theory th =
MARCHE Claude's avatar
MARCHE Claude committed
822
  if M.verified th then
823
    begin
MARCHE Claude's avatar
MARCHE Claude committed
824
      let row = M.theory_key th in
825
      goals_view#collapse_row row#path;
826 827
    end
  else
MARCHE Claude's avatar
MARCHE Claude committed
828
    List.iter collapse_proved_goal (M.goals th)
829 830

let collapse_verified_file f =
831
  if f.M.file_verified then
832
    begin
833
      let row = f.M.file_key in
834
      goals_view#collapse_row row#path;
835 836
    end
  else
837
    List.iter collapse_verified_theory f.M.theories
838 839

let collapse_all_verified_things () =
840
  List.iter collapse_verified_file (M.get_all_files ())
841 842 843 844 845 846 847 848 849

let (_ : GMenu.image_menu_item) =
  view_factory#add_image_item ~key:GdkKeysyms._C
    ~label:"Collapse proved goals"
    ~callback:collapse_all_verified_things
    ()

(*
let rec hide_proved_in_goal g =
850
  if g.M.proved then
851
    begin
852
      let row = g.M.goal_row in
853 854
      goals_view#collapse_row (goals_model#get_path row);
(*
855
      goals_model#set ~row ~column:M.visible_column false
856 857 858 859
*)
    end
  else
    Hashtbl.iter
860 861
      (fun _ t -> List.iter hide_proved_in_goal t.M.subgoals)
      g.M.transformations
862 863

let hide_proved_in_theory th =
864
  if th.M.verified then
865
    begin
866
      let row = th.M.theory_row in
867
      goals_view#collapse_row (goals_model#get_path row);
868
      goals_model#set ~row ~column:M.visible_column false
869 870
    end
  else
871
    List.iter hide_proved_in_goal th.M.goals
872 873

let hide_proved_in_file f =
874
  if f.M.file_verified then
875
    begin
876
      let row = f.M.file_row in
877
      goals_view#collapse_row (goals_model#get_path row);
878
      goals_model#set ~row ~column:M.visible_column false
879 880
    end
  else
881
    List.iter hide_proved_in_theory f.M.theories
882 883

let hide_proved_in_files () =
884
  List.iter hide_proved_in_file !M.all_files
885 886

let rec show_all_in_goal g =
887 888 889
  let row = g.M.goal_row in
  goals_model#set ~row ~column:M.visible_column true;
  if g.M.proved then
890 891 892 893
    goals_view#collapse_row (goals_model#get_path row)
  else
    goals_view#expand_row (goals_model#get_path row);
  Hashtbl.iter
894 895
    (fun _ t -> List.iter show_all_in_goal t.M.subgoals)
    g.M.transformations
896 897

let show_all_in_theory th =
898 899 900
  let row = th.M.theory_row in
  goals_model#set ~row ~column:M.visible_column true;
  if th.M.verified then
901 902 903 904
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
905
      List.iter show_all_in_goal th.M.goals
906 907 908
    end

let show_all_in_file f =
909 910 911
  let row = f.M.file_row in
  goals_model#set ~row ~column:M.visible_column true;
  if f.M.file_verified then
912 913 914 915
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
916
      List.iter show_all_in_theory f.M.theories
917 918 919
    end

let show_all_in_files () =
920
  List.iter show_all_in_file !M.all_files
921 922 923 924


let (_ : GMenu.check_menu_item) = view_factory#add_check_item
  ~callback:(fun b ->
925
               M.toggle_hide_proved_goals := b;
926 927 928 929 930 931 932 933 934 935 936 937 938
               if b then hide_proved_in_files ()
               else show_all_in_files ())
  "Hide proved goals"

*)

(**************)
(* Tools menu *)
(**************)


let () = add_refresh_provers (fun () ->
  List.iter (fun item -> item#destroy ()) provers_box#all_children)
939
  "remove from provers box"
940 941 942 943 944 945

let tools_menu = factory#add_submenu "_Tools"
let tools_factory = new GMenu.factory tools_menu ~accel_group

let () = add_refresh_provers (fun () ->
  List.iter (fun item -> item#destroy ()) tools_menu#all_children)
946
  "remove from tools menu"
947 948 949 950 951

let () =
  let add_item_provers () =
    Util.Mstr.iter
      (fun _ p ->
952
         let n = p.Session.prover_name ^ " " ^ p.Session.prover_version in
953 954 955 956 957 958
         let (_ : GMenu.image_menu_item) =
           tools_factory#add_image_item ~label:n
             ~callback:(fun () -> prover_on_selected_goals p)
             ()
         in
         let b = GButton.button ~packing:provers_box#add ~label:n () in
959 960
         b#misc#set_tooltip_markup ("Start <tt>" ^ p.Session.prover_name ^
           "</tt> on the <b>selected goals</b>");
961 962 963 964 965 966 967 968 969

(* prend de la place pour rien
         let i = GMisc.image ~pixbuf:(!image_prover) () in
         let () = b#set_image i#coerce in
*)
         let (_ : GtkSignal.id) =
           b#connect#pressed
             ~callback:(fun () -> prover_on_selected_goals p)
         in ())
970
      (M.get_provers ())
971
  in
972
  add_refresh_provers add_item_provers "Add in tools menu and provers box";
973 974
  add_item_provers ()

975 976 977 978 979 980
let split_selected_goals () =
  apply_trans_on_selection split_transformation

let inline_selected_goals () =
  apply_trans_on_selection inline_transformation

981
let () =
982 983 984
  let add_separator () =
    ignore(tools_factory#add_separator
             () : GMenu.menu_item) in
985 986
  let add_item_split () =
    ignore(tools_factory#add_image_item
987
             ~label:"Split in selection"
988 989
             ~callback:split_selected_goals
             () : GMenu.image_menu_item) in
990 991 992 993 994
  let add_item_inline () =
    ignore(tools_factory#add_image_item
             ~label:"Inline in selection"
             ~callback:inline_selected_goals
             () : GMenu.image_menu_item) in
995 996 997
  add_refresh_provers add_separator "add separator in tools menu";
  add_refresh_provers add_item_split "add split in tools menu";
  add_refresh_provers add_item_inline  "add inline in tools menu";
998 999 1000
  add_separator ();
  add_item_split ();
  add_item_inline ()
1001 1002 1003 1004


let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Split" () in
1005
  b#misc#set_tooltip_markup "Apply the transformation <tt>split_goal</tt> to the <b>selected goals</b>";
1006 1007 1008 1009 1010

  let i = GMisc.image ~pixbuf:(!image_transf) () in
  let () = b#set_image i#coerce in
  let (_ : GtkSignal.id) =
    b#connect#pressed ~callback:split_selected_goals
1011 1012
  in
  ()
1013 1014 1015

let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Inline" () in
1016
  b#misc#set_tooltip_markup "Apply the transformation <tt>inline_goal</tt> to the <b>selected goals</b>";
1017 1018 1019 1020
  let i = GMisc.image ~pixbuf:(!image_transf) () in
  let () = b#set_image i#coerce in
  let (_ : GtkSignal.id) =
    b#connect#pressed ~callback:inline_selected_goals
MARCHE Claude's avatar
MARCHE Claude committed
1021
  in
1022
  ()
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104



(*************)
(* Help menu *)
(*************)


let help_menu = factory#add_submenu "_Help"
let help_factory = new GMenu.factory help_menu ~accel_group

let (_ : GMenu.image_menu_item) =
  help_factory#add_image_item
    ~label:"Legend"
    ~callback:show_legend_window
    ()

let (_ : GMenu.image_menu_item) =
  help_factory#add_image_item
    ~label:"About"
    ~callback:show_about_window
    ()


(******************************)
(* vertical paned on the right*)
(******************************)

let right_vb = GPack.vbox ~packing:hp#add ()

let vp =
  try
    GPack.paned `VERTICAL ~packing:right_vb#add ()
  with Gtk.Error _ -> assert false

let right_hb = GPack.hbox ~packing:(right_vb#pack ~expand:false) ()

(******************)
(* goal text view *)
(******************)

let scrolled_task_view = GBin.scrolled_window
  ~hpolicy: `AUTOMATIC ~vpolicy: `AUTOMATIC
  ~packing:vp#add ()

let () = scrolled_task_view#set_shadow_type `ETCHED_OUT

let (_ : GtkSignal.id) =
  scrolled_task_view#misc#connect#size_allocate
    ~callback:
    (fun {Gtk.width=_w;Gtk.height=h} ->
       gconfig.task_height <- h)

let task_view =
  GSourceView2.source_view
    ~editable:false
    ~show_line_numbers:true
    ~packing:scrolled_task_view#add
    ~height:gconfig.task_height
    ()

let () = task_view#source_buffer#set_language lang
let () = task_view#set_highlight_current_line true

(***************)
(* source view *)
(***************)

let scrolled_source_view = GBin.scrolled_window
  ~hpolicy: `AUTOMATIC ~vpolicy: `AUTOMATIC
  ~packing:vp#add
  ()

let () = scrolled_source_view#set_shadow_type `ETCHED_OUT

let source_view =
  GSourceView2.source_view
    ~auto_indent:true
    ~insert_spaces_instead_of_tabs:true ~tab_width:2
    ~show_line_numbers:true
    ~right_margin_position:80 ~show_right_margin:true
    (* ~smart_home_end:true *)
1105
    ~editable:false
1106 1107 1108
    ~packing:scrolled_source_view#add
    ()

1109 1110


1111 1112 1113 1114 1115 1116 1117 1118 1119
(*
  source_view#misc#modify_font_by_name font_name;
*)
let () = source_view#source_buffer#set_language lang
let () = source_view#set_highlight_current_line true
(*
let () = source_view#source_buffer#set_text (source_text fname)
*)

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
let current_file = ref ""

let file_info = GMisc.label ~text:""
  ~packing:(right_hb#pack ~fill:true) ()

let set_current_file f =
  current_file := f;
  file_info#set_text ("file: " ^ !current_file)


1130 1131 1132 1133 1134 1135 1136
let move_to_line ~yalign (v : GSourceView2.source_view) line =
  let line = max 0 line in
  let line = min line v#buffer#line_count in
  let it = v#buffer#get_iter (`LINE line) in
  let mark = `MARK (v#buffer#create_mark it) in
  v#scroll_to_mark ~use_align:true ~yalign mark

1137

1138 1139 1140 1141 1142
let location_tag = source_view#buffer#create_tag
  ~name:"location_tag" [`BACKGROUND "green"]

let error_tag = source_view#buffer#create_tag
  ~name:"error_tag" [`BACKGROUND "orange"]
1143 1144 1145

let erase_color_loc (v:GSourceView2.source_view) =
  let buf = v#buffer in
1146 1147
  buf#remove_tag location_tag ~start:buf#start_iter ~stop:buf#end_iter;
  buf#remove_tag error_tag ~start:buf#start_iter ~stop:buf#end_iter
1148

1149
let color_loc (v:GSourceView2.source_view) ~color l b e =
1150 1151 1152 1153 1154
  let buf = v#buffer in
  let top = buf#start_iter in
  let start = top#forward_lines (l-1) in
  let start = start#forward_chars b in
  let stop = start#forward_chars (e-b) in
1155
  buf#apply_tag ~start ~stop color
1156

1157
let scroll_to_loc ?(yalign=0.0) ~color loc =
1158 1159 1160 1161 1162 1163
  let (f,l,b,e) = Loc.get loc in
  if f <> !current_file then
    begin
      source_view#source_buffer#set_text (source_text f);
      set_current_file f;
    end;
1164
  move_to_line ~yalign source_view (l-1);
1165
  erase_color_loc source_view;
1166
  color_loc source_view ~color l b e
1167

1168
let scroll_to_id ~color id =
1169
  match id.Ident.id_loc with