newmain.ml 38.7 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
3
(*  Copyright (C) 2010-2011                                               *)
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 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
(*    François Bobot                                                     *)
(*    Jean-Christophe Filliâtre                                          *)
(*    Claude Marché                                                      *)
(*    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 () =
  eprintf "Init the GTK interface...@?";
  ignore (GtkMain.Main.init ());
  eprintf " done.@."


open Why
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 136 137 138 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

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
174
  b1#misc#set_tooltip_markup "When selected, tools below are applied only to <b>unproved</b> goals";
175 176 177 178 179 180 181
  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
182
  b2#misc#set_tooltip_markup "When selected, tools below are applied to all goals";
183 184 185 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
  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
256
let index_column = cols#add Gobject.Data.int
257

258 259 260 261
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 [ ]
262

263 264
let view_name_column =
  GTree.view_column ~title:"Theories/Goals" ()
265

266 267 268 269 270 271 272 273
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;
  ()
274

275 276 277 278
let view_status_column =
  GTree.view_column ~title:"Status"
    ~renderer:(image_renderer, ["pixbuf", status_column])
    ()
279

280 281 282
let view_time_column =
  GTree.view_column ~title:"Time"
    ~renderer:(renderer, ["text", time_column]) ()
283

284 285 286 287 288
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
289

290
let goals_model,goals_view =
291
  eprintf "Creating tree model...@?";
292 293
  let model = GTree.tree_store cols in
  let view = GTree.view ~model ~packing:scrollview#add () in
294
  let () = view#selection#set_mode (* `SINGLE *) `MULTIPLE in
295 296 297 298
  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);
299
  eprintf " done@.";
300
  model,view
301

302 303 304 305
let clear model = model#clear ()

let image_of_result ~obsolete result =
  match result with
MARCHE Claude's avatar
MARCHE Claude committed
306
    | Session.Undone -> !image_undone
307 308 309 310
    | Session.Scheduled -> !image_scheduled
    | Session.Running -> !image_running
    | Session.InternalFailure _ -> !image_failure
    | Session.Done r -> match r.Call_provers.pr_answer with
311 312 313 314 315 316 317 318 319 320 321 322
        | 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
323

324
(* connecting to the Session model *)
325 326 327

module M = Session.Make
  (struct
328 329 330 331 332 333 334 335
     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
336
       goals_model#set ~row:iter ~column:index_column (-1);
337
       goals_model#get_row_reference (goals_model#get_path iter)
338 339


340
     let remove row =
341
       let (_:bool) = goals_model#remove row#iter in ()
342

MARCHE Claude's avatar
MARCHE Claude committed
343 344
     let reset () = goals_model#clear ()

345
     let idle f =
346
       let (_ : GMain.Idle.id) = GMain.Idle.add f in ()
347

348
     let timeout ~ms f =
349 350 351 352
       let (_ : GMain.Timeout.id) = GMain.Timeout.add ~ms ~callback:f in
       ()

   end)
MARCHE Claude's avatar
MARCHE Claude committed
353 354 355

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

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

373 374
let model_index = Hashtbl.create 17

375

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

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

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
393 394 395 396

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

418 419 420 421 422 423

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

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

MARCHE Claude's avatar
MARCHE Claude committed
425
let notify any =
426 427
  let row,exp =
    match any with
428
      | M.Goal g -> 
429
(*
430 431 432 433 434 435 436
          if M.goal_expanded g then
            begin
              let n = 
                Hashtbl.fold (fun _ _ acc -> acc+1) (M.external_proofs g) 0
              in
              eprintf "expand_row on a goal with %d proofs@." n;
            end;
437
*)
438
          (M.goal_key g),(M.goal_expanded g)
439 440 441
      | 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
442
      | M.Transformation tr -> tr.M.transf_key,tr.M.transf_expanded
443
  in
444 445
  if exp then goals_view#expand_to_path row#path else 
    goals_view#collapse_row row#path;
MARCHE Claude's avatar
MARCHE Claude committed
446 447
  match any with
    | M.Goal g ->
448
        set_row_status row (M.goal_proved g)
MARCHE Claude's avatar
MARCHE Claude committed
449
    | M.Theory th ->
450
        set_row_status row (M.verified th)
MARCHE Claude's avatar
MARCHE Claude committed
451
    | M.File file ->
452
        set_row_status row file.M.file_verified
MARCHE Claude's avatar
MARCHE Claude committed
453
    | M.Proof_attempt a ->
454
        set_proof_state ~obsolete:a.M.proof_obsolete a
MARCHE Claude's avatar
MARCHE Claude committed
455
    | M.Transformation tr ->
456 457
        set_row_status row tr.M.transf_proved
          
MARCHE Claude's avatar
MARCHE Claude committed
458 459
let init =
  let cpt = ref (-1) in
460
  fun row any ->
MARCHE Claude's avatar
MARCHE Claude committed
461 462 463 464 465 466 467 468 469 470 471
    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;
472
    (* useless since it has no child: goals_view#expand_row row#path; *)
MARCHE Claude's avatar
MARCHE Claude committed
473
    goals_model#set ~row:row#iter ~column:icon_column
474
      (match any with
475 476 477 478 479
         | 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
480
    goals_model#set ~row:row#iter ~column:name_column
481
      (match any with
482 483 484 485
         | M.Goal g -> M.goal_expl g
         | M.Theory th -> M.theory_name th
         | M.File f -> Filename.basename f.M.file_name
         | M.Proof_attempt a -> 
486 487 488
             begin
               match a.M.prover with
                 | M.Detected_prover p ->
489
                     p.Session.prover_name ^ " " ^ p.Session.prover_version
490 491
                 | M.Undetected_prover s -> s
             end
492
         | M.Transformation tr -> Session.transformation_id tr.M.transf);
MARCHE Claude's avatar
MARCHE Claude committed
493
    notify any
MARCHE Claude's avatar
MARCHE Claude committed
494

495 496


497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
(********************)
(* opening database *)
(********************)

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

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

532

533 534
let () =
  try
535
    eprintf "Opening session...@?";
536 537 538
    M.open_session ~env:gconfig.env 
      (* ~provers:gconfig.provers *)
      ~config:gconfig.Gconfig.config
MARCHE Claude's avatar
MARCHE Claude committed
539
      ~init ~notify project_dir;
540 541
    M.maximum_running_proofs := gconfig.max_running_processes;
    eprintf " done@."
542
  with e ->
543 544 545
    eprintf "@[Error while opening session:@ %a@.@]" 
      Exn_printer.exn_printer e;
    exit 1
546 547 548 549 550 551 552 553 554 555 556 557 558 559


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
    ~title:"Why3 info or error"
560
    ~modal:true
561 562 563 564 565
    ~show:true ()
  in
  let (_ : GtkSignal.id) =
    d#connect#response
      ~callback:(function x -> d#destroy ();
566
                   if x = `OK then callback ())
567 568 569 570 571 572 573 574 575 576 577
  in ()


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

let () =
  match file_to_read with
    | None -> ()
    | Some fn ->
578
        if M.file_exists fn then
579 580 581
          eprintf "Info: file %s already in database@." fn
        else
          try
MARCHE Claude's avatar
MARCHE Claude committed
582
            M.add_file fn
583
          with e ->
584
            eprintf "@[Error while reading file@ '%s':@ %a@.@]" fn
585
              Exn_printer.exn_printer e;
586
            exit 1
587 588 589 590 591 592 593 594 595 596




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

let prover_on_selected_goals pr =
  List.iter
597
    (fun row ->
598
       let a = get_any_from_row_reference row in
599 600
        M.run_prover
          ~context_unproved_goals_only:!context_unproved_goals_only
601
          ~timelimit:gconfig.time_limit
602
          pr a)
603
    (get_selected_row_references ())
604 605 606 607 608 609 610

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

let replay_obsolete_proofs () =
  List.iter
611 612
    (fun r ->
       let a = get_any_from_row_reference r in
MARCHE Claude's avatar
MARCHE Claude committed
613
       M.replay ~obsolete_only:true
614
         ~context_unproved_goals_only:!context_unproved_goals_only a)
615
    (get_selected_row_references ())
616

617 618 619 620 621 622 623 624 625 626
(***********************************)
(* 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 ())
627 628 629 630 631 632


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

633 634 635 636
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"
637 638


639
let apply_trans_on_selection tr =
640
  List.iter
641 642
    (fun r ->
       let a = get_any_from_row_reference r in
643 644
        M.transform
          ~context_unproved_goals_only:!context_unproved_goals_only
645
          tr
646
          a)
647
    (get_selected_row_references ())
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678


(*********************************)
(* 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 ->
679
              let f = Sysutil.relativize_filename project_dir f in
680 681
              eprintf "Adding file '%s'@." f;
              try
MARCHE Claude's avatar
MARCHE Claude committed
682
                M.add_file f
683
              with e ->
684
                fprintf str_formatter
685 686
                  "@[Error while reading file@ '%s':@ %a@]" f
                  Exn_printer.exn_printer e;
687 688
                let msg = flush_str_formatter () in
                info_window `ERROR msg
689 690 691 692 693 694 695 696 697
      end
  | `DELETE_EVENT | `CANCEL -> ()
  end ;
  d#destroy ()


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

698

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
(*************)
(* 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;
715
       M.maximum_running_proofs := gconfig.max_running_processes)
716 717 718 719 720 721 722 723 724 725 726 727 728
    ()

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

let add_refresh_provers f =
  let rp = !refresh_provers in
  refresh_provers := (fun () -> rp (); f ())

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~label:"_Detect provers" ~callback:
    (fun () -> Gconfig.run_auto_detection gconfig; !refresh_provers () )
    ()

729 730 731
let exit_function () =
  eprintf "saving IDE config file@.";
  save_config ();
732
  (*
MARCHE Claude's avatar
MARCHE Claude committed
733 734 735 736 737 738 739
  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
740 741 742 743
        | [] -> ()
        | 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
744

MARCHE Claude's avatar
MARCHE Claude committed
745 746 747 748
    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
749
  if ret = 0 then eprintf "DTD validation succeeded, good!@.";
750
  *)
751 752 753 754 755 756 757 758 759 760 761
  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 ();
762
                   if x = `YES then M.save_session ();
763 764 765 766
                   GMain.quit ()
                )
  in
  ()
767

768 769 770 771 772 773 774 775 776 777 778 779 780
(*************)
(* 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
781
  if M.goal_proved g then
782
    begin
MARCHE Claude's avatar
MARCHE Claude committed
783
      let row = M.goal_key g in
784
      goals_view#collapse_row row#path;
785 786 787
    end
  else
    Hashtbl.iter
788
      (fun _ t -> List.iter collapse_proved_goal t.M.subgoals)
MARCHE Claude's avatar
MARCHE Claude committed
789
      (M.transformations g)
790 791

let collapse_verified_theory th =
MARCHE Claude's avatar
MARCHE Claude committed
792
  if M.verified th then
793
    begin
MARCHE Claude's avatar
MARCHE Claude committed
794
      let row = M.theory_key th in
795
      goals_view#collapse_row row#path;
796 797
    end
  else
MARCHE Claude's avatar
MARCHE Claude committed
798
    List.iter collapse_proved_goal (M.goals th)
799 800

let collapse_verified_file f =
801
  if f.M.file_verified then
802
    begin
803
      let row = f.M.file_key in
804
      goals_view#collapse_row row#path;
805 806
    end
  else
807
    List.iter collapse_verified_theory f.M.theories
808 809

let collapse_all_verified_things () =
810
  List.iter collapse_verified_file (M.get_all_files ())
811 812 813 814 815 816 817 818 819

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 =
820
  if g.M.proved then
821
    begin
822
      let row = g.M.goal_row in
823 824
      goals_view#collapse_row (goals_model#get_path row);
(*
825
      goals_model#set ~row ~column:M.visible_column false
826 827 828 829
*)
    end
  else
    Hashtbl.iter
830 831
      (fun _ t -> List.iter hide_proved_in_goal t.M.subgoals)
      g.M.transformations
832 833

let hide_proved_in_theory th =
834
  if th.M.verified then
835
    begin
836
      let row = th.M.theory_row in
837
      goals_view#collapse_row (goals_model#get_path row);
838
      goals_model#set ~row ~column:M.visible_column false
839 840
    end
  else
841
    List.iter hide_proved_in_goal th.M.goals
842 843

let hide_proved_in_file f =
844
  if f.M.file_verified then
845
    begin
846
      let row = f.M.file_row in
847
      goals_view#collapse_row (goals_model#get_path row);
848
      goals_model#set ~row ~column:M.visible_column false
849 850
    end
  else
851
    List.iter hide_proved_in_theory f.M.theories
852 853

let hide_proved_in_files () =
854
  List.iter hide_proved_in_file !M.all_files
855 856

let rec show_all_in_goal g =
857 858 859
  let row = g.M.goal_row in
  goals_model#set ~row ~column:M.visible_column true;
  if g.M.proved then
860 861 862 863
    goals_view#collapse_row (goals_model#get_path row)
  else
    goals_view#expand_row (goals_model#get_path row);
  Hashtbl.iter
864 865
    (fun _ t -> List.iter show_all_in_goal t.M.subgoals)
    g.M.transformations
866 867

let show_all_in_theory th =
868 869 870
  let row = th.M.theory_row in
  goals_model#set ~row ~column:M.visible_column true;
  if th.M.verified then
871 872 873 874
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
875
      List.iter show_all_in_goal th.M.goals
876 877 878
    end

let show_all_in_file f =
879 880 881
  let row = f.M.file_row in
  goals_model#set ~row ~column:M.visible_column true;
  if f.M.file_verified then
882 883 884 885
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
886
      List.iter show_all_in_theory f.M.theories
887 888 889
    end

let show_all_in_files () =
890
  List.iter show_all_in_file !M.all_files
891 892 893 894


let (_ : GMenu.check_menu_item) = view_factory#add_check_item
  ~callback:(fun b ->
895
               M.toggle_hide_proved_goals := b;
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
               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)

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)

let () =
  let add_item_provers () =
    Util.Mstr.iter
      (fun _ p ->
920
         let n = p.Session.prover_name ^ " " ^ p.Session.prover_version in
921 922 923 924 925 926
         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
927 928
         b#misc#set_tooltip_markup ("Start <tt>" ^ p.Session.prover_name ^
           "</tt> on the <b>selected goals</b>");
929 930 931 932 933 934 935 936 937

(* 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 ())
938
      (M.get_provers ())
939 940 941 942
  in
  add_refresh_provers add_item_provers;
  add_item_provers ()

943 944 945 946 947 948
let split_selected_goals () =
  apply_trans_on_selection split_transformation

let inline_selected_goals () =
  apply_trans_on_selection inline_transformation

949 950 951 952 953 954 955 956 957 958 959 960
let () =
  let add_item_split () =
    ignore(tools_factory#add_image_item
             ~label:"Split selection"
             ~callback:split_selected_goals
             () : GMenu.image_menu_item) in
  add_refresh_provers add_item_split;
  add_item_split ()


let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Split" () in
961
  b#misc#set_tooltip_markup "Apply the transformation <tt>split_goal</tt> to the <b>selected goals</b>";
962 963 964 965 966

  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
967 968
  in
  ()
969 970 971

let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Inline" () in
972
  b#misc#set_tooltip_markup "Apply the transformation <tt>inline_goal</tt> to the <b>selected goals</b>";
973 974 975 976
  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
977
  in
978
  ()
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 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



(*************)
(* 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 *)
1061
    ~editable:false
1062 1063 1064
    ~packing:scrolled_source_view#add
    ()

1065 1066


1067 1068 1069 1070 1071 1072 1073 1074 1075
(*
  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)
*)

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
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)


1086 1087 1088 1089 1090 1091 1092
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

1093

1094 1095 1096 1097 1098
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"]
1099 1100 1101

let erase_color_loc (v:GSourceView2.source_view) =
  let buf = v#buffer in
1102 1103
  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
1104

1105
let color_loc (v:GSourceView2.source_view) ~color l b e =
1106 1107 1108 1109 1110
  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
1111
  buf#apply_tag ~start ~stop color
1112

1113
let scroll_to_loc ?(yalign=0.0) ~color loc =
1114 1115 1116 1117 1118 1119
  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;
1120
  move_to_line ~yalign source_view (l-1);
1121
  erase_color_loc source_view;
1122
  color_loc source_view ~color l b e
1123

1124
let scroll_to_id ~color id =
1125
  match id.Ident.id_loc with
1126
    | Some loc -> scroll_to_loc ~color loc
1127
    | None ->
1128
        source_view#source_buffer#set_text
1129
          "Non-localized ident (no position available)\n";
1130 1131
        set_current_file ""

1132
let color_loc ~color loc =
1133
  let f, l, b, e = Loc.get loc in
1134
  if f = !current_file then color_loc ~color source_view l b e
1135

1136
let rec color_t_locs () f =
1137
  Util.option_iter (color_loc ~color:location_tag) f.Term.t_loc;
1138
  Term.t_fold color_t_locs () f
1139 1140

let scroll_to_source_goal g =
MARCHE Claude's avatar
MARCHE Claude committed
1141 1142
  let t = M.get_task g in
  let id = (Task.task_goal t).Decl.pr_name in
1143
  scroll_to_id ~color:location_tag id;
MARCHE Claude's avatar
MARCHE Claude committed
1144 1145 1146 1147 1148
  match t with
    | Some
        { Task.task_decl =
            { Theory.td_node =
                Theory.Decl { Decl.d_node = Decl.Dprop (Decl.Pgoal, _, f)}}} ->
1149
        color_t_locs () f
MARCHE Claude's avatar
MARCHE Claude committed
1150 1151
    | _ ->
        assert false
1152 1153

let scroll_to_theory th =
MARCHE Claude's avatar
MARCHE Claude committed
1154 1155
  let t = M.get_theory th in
  let id = t.Theory.th_name in
1156 1157 1158 1159 1160 1161 1162
  scroll_to_id ~color:location_tag id


let reload () =
  try
    erase_color_loc source_view;
    current_file := "";
1163
    M.reload_all ()
1164 1165 1166 1167 1168 1169 1170
  with
    | e ->
        let e = match e with
          | Loc.Located(loc,e) -> 
              scroll_to_loc ~color:error_tag ~yalign:0.5 loc; e
          | e -> e
        in
1171 1172 1173
        fprintf str_formatter
          "@[Error:@ %a@]" Exn_printer.exn_printer e;
        let msg = flush_str_formatter () in
1174 1175
        file_info#set_text msg
(*
1176
        info_window `ERROR msg
1177 1178 1179 1180 1181 1182 1183
*)

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~key:GdkKeysyms._R
    ~label:"_Reload" ~callback:reload
    ()

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

(* Saving the session *)

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item (* no shortcut ~key:GdkKeysyms._S *)
    ~label:"_Save session" ~callback:M.save_session
    ()


(*

Saving the source_view: deactivated for the moment

1197 1198 1199 1200
let save_file () =
  let f = !current_file in
  if f <> "" then
    begin
1201
      M.save_session ();
1202 1203 1204 1205 1206 1207
      let s = source_view#source_buffer#get_text () in
      let c = open_out f in
      output_string c s;
      close_out c;
      reload ()
    end
1208 1209
  else
    info_window `ERROR "No file currently edited"
1210 1211 1212 1213 1214

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~key:GdkKeysyms._S
    ~label:"_Save" ~callback:save_file
    ()
1215
*)
1216

1217
let (_ : GtkSignal.id) = w#connect#destroy ~callback:exit_function