(*********************************************************************************)
(*                Chamo                                                          *)
(*                                                                               *)
(*    Copyright (C) 2003-2021 Institut National de Recherche en Informatique     *)
(*    et en Automatique. All rights reserved.                                    *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU Lesser General Public License version        *)
(*    3 as published by the Free Software Foundation.                            *)
(*                                                                               *)
(*    This program 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.  See the              *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public License          *)
(*    along with this program; if not, write to the Free Software                *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

let output_name = "ocamltop";;
class ocamltop_output ?(on_destroy=fun()->()) name =
  object(self)
    inherit Outputs.text_output ~on_destroy name as super

    initializer
      (*let lang = Gtksv_utils.source_language_manager#guess_language
         ~content_type: "text/x-ocaml"  ()
      in
      *)
      super#view#set_source_language (Some "text/x-ocaml");
      (*
      super#view#source_buffer#set_highlight_syntax true;
      super#view#source_buffer#set_highlight_matching_brackets true;
      *)
  end;;

let ocamltop_output = ref None;;
let ocamltop_output () =
  match !ocamltop_output with
    None ->
      let o = new ocamltop_output
        ~on_destroy: (fun () -> ocamltop_output := None)
          output_name
      in
      ocamltop_output := Some o ;
      o
  | Some o -> o
;;

let print_ocaml_output ?(output=ocamltop_output()) args =
  let outputs = Outputs.outputs () in
  begin
    try ignore(outputs#output_by_name output#name)
    with Not_found ->
        outputs#add_output (output :> Outputs.output);
  end;
  outputs#show output#name;
  ignore(output#insert (if Array.length args < 1 then "" else args.(0)));
  Lwt.return_unit
;;

Commands.register
  (Commands.create_com "print_ocaml_output" [|"string"|]
   (print_ocaml_output ?output: None));;
