1999-12-16 04:25:11 -08:00
|
|
|
(*************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml LablTk library *)
|
|
|
|
(* *)
|
|
|
|
(* Jacques Garrigue, Kyoto University RIMS *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1999 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique and Kyoto University. All rights reserved. *)
|
|
|
|
(* This file is distributed under the terms of the GNU Library *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* General Public License, with the special exception on linking *)
|
|
|
|
(* described in file ../../../LICENSE. *)
|
1999-12-16 04:25:11 -08:00
|
|
|
(* *)
|
|
|
|
(*************************************************************************)
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* $Id$ *)
|
|
|
|
|
2001-09-06 01:52:32 -07:00
|
|
|
open StdLabels
|
1999-11-30 06:59:39 -08:00
|
|
|
open Tk
|
|
|
|
open Parsetree
|
|
|
|
open Location
|
|
|
|
open Jg_tk
|
|
|
|
open Mytypes
|
|
|
|
|
|
|
|
let lex_on_load = ref true
|
|
|
|
and type_on_load = ref false
|
|
|
|
|
|
|
|
let compiler_preferences () =
|
|
|
|
let tl = Jg_toplevel.titled "Compiler" in
|
2000-04-11 20:43:25 -07:00
|
|
|
Wm.transient_set tl ~master:Widget.default_toplevel;
|
|
|
|
let mk_chkbutton ~text ~ref ~invert =
|
|
|
|
let variable = Textvariable.create ~on:tl () in
|
2000-03-03 01:38:43 -08:00
|
|
|
if (if invert then not !ref else !ref) then
|
2000-04-02 18:57:52 -07:00
|
|
|
Textvariable.set variable "1";
|
2000-04-11 20:43:25 -07:00
|
|
|
Checkbutton.create tl ~text ~variable,
|
2000-03-03 01:38:43 -08:00
|
|
|
(fun () ->
|
|
|
|
ref := Textvariable.get variable = (if invert then "0" else "1"))
|
1999-11-30 06:59:39 -08:00
|
|
|
in
|
2001-09-22 01:51:54 -07:00
|
|
|
let use_pp = ref (!Clflags.preprocessor <> None) in
|
1999-11-30 06:59:39 -08:00
|
|
|
let chkbuttons, setflags = List.split
|
2000-03-03 01:38:43 -08:00
|
|
|
(List.map
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:(fun (text, ref, invert) -> mk_chkbutton ~text ~ref ~invert)
|
2000-03-03 01:38:43 -08:00
|
|
|
[ "No pervasives", Clflags.nopervasives, false;
|
|
|
|
"No warnings", Typecheck.nowarnings, false;
|
2001-09-06 01:52:32 -07:00
|
|
|
"No labels", Clflags.classic, false;
|
2000-04-14 01:29:53 -07:00
|
|
|
"Recursive types", Clflags.recursive_types, false;
|
2000-03-03 01:38:43 -08:00
|
|
|
"Lex on load", lex_on_load, false;
|
2001-09-22 01:51:54 -07:00
|
|
|
"Type on load", type_on_load, false;
|
|
|
|
"Preprocessor", use_pp, false ])
|
1999-11-30 06:59:39 -08:00
|
|
|
in
|
2001-09-22 01:51:54 -07:00
|
|
|
let pp_command = Entry.create tl (* ~state:(if !use_pp then `Normal else`Disabled) *) in
|
|
|
|
begin match !Clflags.preprocessor with None -> ()
|
|
|
|
| Some pp -> Entry.insert pp_command ~index:(`Num 0) ~text:pp
|
|
|
|
end;
|
1999-11-30 06:59:39 -08:00
|
|
|
let buttons = Frame.create tl in
|
2000-04-11 20:43:25 -07:00
|
|
|
let ok = Button.create buttons ~text:"Ok" ~padx:20 ~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter ~f:(fun f -> f ()) setflags;
|
2001-09-22 01:51:54 -07:00
|
|
|
Clflags.preprocessor :=
|
|
|
|
if !use_pp then Some (Entry.get pp_command) else None;
|
1999-11-30 06:59:39 -08:00
|
|
|
destroy tl
|
|
|
|
end
|
2000-04-11 20:43:25 -07:00
|
|
|
and cancel = Jg_button.create_destroyer tl ~parent:buttons ~text:"Cancel"
|
1999-11-30 06:59:39 -08:00
|
|
|
in
|
2000-04-11 20:43:25 -07:00
|
|
|
pack chkbuttons ~side:`Top ~anchor:`W;
|
2001-09-22 01:51:54 -07:00
|
|
|
pack [pp_command] ~side:`Top ~anchor:`E;
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [ok;cancel] ~side:`Left ~fill:`X ~expand:true;
|
|
|
|
pack [buttons] ~side:`Bottom ~fill:`X
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-02 18:57:52 -07:00
|
|
|
let rec exclude txt = function
|
1999-11-30 06:59:39 -08:00
|
|
|
[] -> []
|
2000-04-02 18:57:52 -07:00
|
|
|
| x :: l -> if txt.number = x.number then l else x :: exclude txt l
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
let goto_line tw =
|
|
|
|
let tl = Jg_toplevel.titled "Go to" in
|
2000-04-11 20:43:25 -07:00
|
|
|
Wm.transient_set tl ~master:Widget.default_toplevel;
|
1999-11-30 06:59:39 -08:00
|
|
|
Jg_bind.escape_destroy tl;
|
|
|
|
let ef = Frame.create tl in
|
|
|
|
let fl = Frame.create ef
|
|
|
|
and fi = Frame.create ef in
|
2000-04-11 20:43:25 -07:00
|
|
|
let ll = Label.create fl ~text:"Line ~number:"
|
|
|
|
and il = Entry.create fi ~width:10
|
|
|
|
and lc = Label.create fl ~text:"Col ~number:"
|
|
|
|
and ic = Entry.create fi ~width:10
|
1999-11-30 06:59:39 -08:00
|
|
|
and get_int ew =
|
|
|
|
try int_of_string (Entry.get ew)
|
|
|
|
with Failure "int_of_string" -> 0
|
|
|
|
in
|
|
|
|
let buttons = Frame.create tl in
|
2000-04-11 20:43:25 -07:00
|
|
|
let ok = Button.create buttons ~text:"Ok" ~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
|
|
|
let l = get_int il
|
|
|
|
and c = get_int ic in
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.mark_set tw ~mark:"insert" ~index:(`Linechar (l,0), [`Char c]);
|
|
|
|
Text.see tw ~index:(`Mark "insert", []);
|
1999-11-30 06:59:39 -08:00
|
|
|
destroy tl
|
|
|
|
end
|
2000-04-11 20:43:25 -07:00
|
|
|
and cancel = Jg_button.create_destroyer tl ~parent:buttons ~text:"Cancel" in
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
Focus.set il;
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter [il; ic] ~f:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun w ->
|
|
|
|
Jg_bind.enter_focus w;
|
2000-04-11 20:43:25 -07:00
|
|
|
Jg_bind.return_invoke w ~button:ok
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [ll; lc] ~side:`Top ~anchor:`W;
|
|
|
|
pack [il; ic] ~side:`Top ~fill:`X ~expand:true;
|
|
|
|
pack [fl; fi] ~side:`Left ~fill:`X ~expand:true;
|
|
|
|
pack [ok; cancel] ~side:`Left ~fill:`X ~expand:true;
|
|
|
|
pack [ef; buttons] ~side:`Top ~fill:`X ~expand:true
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
let select_shell txt =
|
|
|
|
let shells = Shell.get_all () in
|
2001-09-06 01:52:32 -07:00
|
|
|
let shells = List.sort shells ~cmp:compare in
|
1999-11-30 06:59:39 -08:00
|
|
|
let tl = Jg_toplevel.titled "Select Shell" in
|
|
|
|
Jg_bind.escape_destroy tl;
|
2000-04-11 20:43:25 -07:00
|
|
|
Wm.transient_set tl ~master:(Winfo.toplevel txt.tw);
|
2000-11-14 18:27:38 -08:00
|
|
|
let label = Label.create tl ~text:"Send to:"
|
1999-11-30 06:59:39 -08:00
|
|
|
and box = Listbox.create tl
|
|
|
|
and frame = Frame.create tl in
|
|
|
|
Jg_bind.enter_focus box;
|
2000-04-11 20:43:25 -07:00
|
|
|
let cancel = Jg_button.create_destroyer tl ~parent:frame ~text:"Cancel"
|
|
|
|
and ok = Button.create frame ~text:"Ok" ~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
|
|
|
try
|
2000-04-11 20:43:25 -07:00
|
|
|
let name = Listbox.get box ~index:`Active in
|
2001-09-17 06:08:34 -07:00
|
|
|
txt.shell <- Some (name, List.assoc name shells);
|
1999-11-30 06:59:39 -08:00
|
|
|
destroy tl
|
|
|
|
with Not_found -> txt.shell <- None; destroy tl
|
|
|
|
end
|
|
|
|
in
|
2000-04-11 20:43:25 -07:00
|
|
|
Listbox.insert box ~index:`End ~texts:(List.map ~f:fst shells);
|
|
|
|
Listbox.configure box ~height:(List.length shells);
|
|
|
|
bind box ~events:[`KeyPressDetail"Return"] ~breakable:true
|
|
|
|
~action:(fun _ -> Button.invoke ok; break ());
|
|
|
|
bind box ~events:[`Modified([`Double],`ButtonPressDetail 1)] ~breakable:true
|
|
|
|
~fields:[`MouseX;`MouseY]
|
|
|
|
~action:(fun ev ->
|
|
|
|
Listbox.activate box ~index:(`Atxy (ev.ev_MouseX, ev.ev_MouseY));
|
1999-12-16 00:37:38 -08:00
|
|
|
Button.invoke ok; break ());
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [label] ~side:`Top ~anchor:`W;
|
|
|
|
pack [box] ~side:`Top ~fill:`Both;
|
|
|
|
pack [frame] ~side:`Bottom ~fill:`X ~expand:true;
|
|
|
|
pack [ok;cancel] ~side:`Left ~fill:`X ~expand:true
|
1999-11-30 06:59:39 -08:00
|
|
|
|
1999-12-09 07:08:02 -08:00
|
|
|
open Parser
|
|
|
|
|
|
|
|
let send_phrase txt =
|
|
|
|
if txt.shell = None then begin
|
|
|
|
match Shell.get_all () with [] -> ()
|
|
|
|
| [sh] -> txt.shell <- Some sh
|
|
|
|
| l -> select_shell txt
|
|
|
|
end;
|
|
|
|
match txt.shell with None -> ()
|
|
|
|
| Some (_,sh) ->
|
|
|
|
try
|
2000-04-11 20:43:25 -07:00
|
|
|
let i1,i2 = Text.tag_nextrange txt.tw ~tag:"sel" ~start:tstart in
|
|
|
|
let phrase = Text.get txt.tw ~start:(i1,[]) ~stop:(i2,[]) in
|
1999-12-09 07:08:02 -08:00
|
|
|
sh#send phrase;
|
2001-09-06 01:52:32 -07:00
|
|
|
if Str.string_match (Str.regexp ";;") phrase 0
|
2000-02-15 02:09:55 -08:00
|
|
|
then sh#send "\n" else sh#send ";;\n"
|
1999-12-09 07:08:02 -08:00
|
|
|
with Not_found | Protocol.TkError _ ->
|
2000-04-11 20:43:25 -07:00
|
|
|
let text = Text.get txt.tw ~start:tstart ~stop:tend in
|
1999-12-09 07:08:02 -08:00
|
|
|
let buffer = Lexing.from_string text in
|
|
|
|
let start = ref 0
|
|
|
|
and block_start = ref []
|
|
|
|
and pend = ref (-1)
|
|
|
|
and after = ref false in
|
|
|
|
while !pend = -1 do
|
|
|
|
let token = Lexer.token buffer in
|
1999-12-09 07:27:04 -08:00
|
|
|
let pos =
|
|
|
|
if token = SEMISEMI then Lexing.lexeme_end buffer
|
|
|
|
else Lexing.lexeme_start buffer
|
|
|
|
in
|
|
|
|
let bol = (pos = 0) || text.[pos-1] = '\n' in
|
1999-12-09 07:08:02 -08:00
|
|
|
if not !after &&
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.compare txt.tw ~index:(tpos pos) ~op:(if bol then `Gt else `Ge)
|
|
|
|
~index:(`Mark"insert",[])
|
1999-12-09 07:08:02 -08:00
|
|
|
then begin
|
|
|
|
after := true;
|
1999-12-09 07:48:36 -08:00
|
|
|
let anon, real =
|
2000-04-11 20:43:25 -07:00
|
|
|
List.partition !block_start ~f:(fun x -> x = -1) in
|
1999-12-09 07:48:36 -08:00
|
|
|
block_start := anon;
|
|
|
|
if real <> [] then start := List.hd real;
|
1999-12-09 07:08:02 -08:00
|
|
|
end;
|
|
|
|
match token with
|
|
|
|
CLASS | EXTERNAL | EXCEPTION | FUNCTOR
|
|
|
|
| LET | MODULE | OPEN | TYPE | VAL | SHARP when bol ->
|
|
|
|
if !block_start = [] then
|
|
|
|
if !after then pend := pos else start := pos
|
|
|
|
else block_start := pos :: List.tl !block_start
|
|
|
|
| SEMISEMI ->
|
|
|
|
if !block_start = [] then
|
1999-12-09 07:27:04 -08:00
|
|
|
if !after then pend := Lexing.lexeme_start buffer
|
|
|
|
else start := pos
|
|
|
|
else block_start := pos :: List.tl !block_start
|
1999-12-09 07:48:36 -08:00
|
|
|
| BEGIN | OBJECT ->
|
|
|
|
block_start := -1 :: !block_start
|
|
|
|
| STRUCT | SIG ->
|
1999-12-09 07:08:02 -08:00
|
|
|
block_start := Lexing.lexeme_end buffer :: !block_start
|
|
|
|
| END ->
|
|
|
|
if !block_start = [] then
|
|
|
|
if !after then pend := pos else ()
|
|
|
|
else block_start := List.tl !block_start
|
|
|
|
| EOF ->
|
|
|
|
pend := pos
|
|
|
|
| _ ->
|
|
|
|
()
|
|
|
|
done;
|
2000-04-11 20:43:25 -07:00
|
|
|
let phrase = String.sub text ~pos:!start ~len:(!pend - !start) in
|
1999-12-09 07:08:02 -08:00
|
|
|
sh#send phrase;
|
|
|
|
sh#send ";;\n"
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let search_pos_window txt ~x ~y =
|
2000-12-28 05:07:42 -08:00
|
|
|
if txt.structure = [] && txt.psignature = [] then () else
|
2000-04-11 20:43:25 -07:00
|
|
|
let `Linechar (l, c) = Text.index txt.tw ~index:(`Atxy(x,y), []) in
|
1999-11-30 06:59:39 -08:00
|
|
|
let text = Jg_text.get_all txt.tw in
|
2000-04-11 20:43:25 -07:00
|
|
|
let pos = Searchpos.lines_to_chars l ~text + c in
|
2001-09-22 01:51:54 -07:00
|
|
|
try if txt.structure <> [] then begin match
|
|
|
|
Searchpos.search_pos_structure txt.structure ~pos
|
|
|
|
with [] -> ()
|
|
|
|
| (kind, env, loc) :: _ -> Searchpos.view_type kind ~env
|
|
|
|
end else begin match
|
|
|
|
Searchpos.search_pos_signature txt.psignature ~pos ~env:!Searchid.start_env
|
|
|
|
with [] -> ()
|
|
|
|
| ((kind, lid), env, loc) :: _ ->
|
2000-04-11 20:43:25 -07:00
|
|
|
Searchpos.view_decl lid ~kind ~env
|
2001-09-22 01:51:54 -07:00
|
|
|
end
|
1999-11-30 06:59:39 -08:00
|
|
|
with Not_found -> ()
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let search_pos_menu txt ~x ~y =
|
2000-12-28 05:07:42 -08:00
|
|
|
if txt.structure = [] && txt.psignature = [] then () else
|
2000-04-11 20:43:25 -07:00
|
|
|
let `Linechar (l, c) = Text.index txt.tw ~index:(`Atxy(x,y), []) in
|
1999-11-30 06:59:39 -08:00
|
|
|
let text = Jg_text.get_all txt.tw in
|
2000-04-11 20:43:25 -07:00
|
|
|
let pos = Searchpos.lines_to_chars l ~text + c in
|
2001-09-22 01:51:54 -07:00
|
|
|
try if txt.structure <> [] then begin match
|
|
|
|
Searchpos.search_pos_structure txt.structure ~pos
|
|
|
|
with [] -> ()
|
|
|
|
| (kind, env, loc) :: _ ->
|
2000-04-11 20:43:25 -07:00
|
|
|
let menu = Searchpos.view_type_menu kind ~env ~parent:txt.tw in
|
1999-11-30 06:59:39 -08:00
|
|
|
let x = x + Winfo.rootx txt.tw and y = y + Winfo.rooty txt.tw - 10 in
|
2000-04-11 20:43:25 -07:00
|
|
|
Menu.popup menu ~x ~y
|
2001-09-22 01:51:54 -07:00
|
|
|
end else begin match
|
|
|
|
Searchpos.search_pos_signature txt.psignature ~pos ~env:!Searchid.start_env
|
|
|
|
with [] -> ()
|
|
|
|
| ((kind, lid), env, loc) :: _ ->
|
2000-04-11 20:43:25 -07:00
|
|
|
let menu = Searchpos.view_decl_menu lid ~kind ~env ~parent:txt.tw in
|
1999-11-30 06:59:39 -08:00
|
|
|
let x = x + Winfo.rootx txt.tw and y = y + Winfo.rooty txt.tw - 10 in
|
2000-04-11 20:43:25 -07:00
|
|
|
Menu.popup menu ~x ~y
|
2001-09-22 01:51:54 -07:00
|
|
|
end
|
1999-11-30 06:59:39 -08:00
|
|
|
with Not_found -> ()
|
|
|
|
|
|
|
|
let string_width s =
|
|
|
|
let width = ref 0 in
|
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
if s.[i] = '\t' then width := (!width / 8 + 1) * 8
|
|
|
|
else incr width
|
|
|
|
done;
|
|
|
|
!width
|
|
|
|
|
|
|
|
let indent_line =
|
|
|
|
let ins = `Mark"insert" and reg = Str.regexp "[ \t]*" in
|
|
|
|
fun tw ->
|
2000-04-11 20:43:25 -07:00
|
|
|
let `Linechar(l,c) = Text.index tw ~index:(ins,[])
|
|
|
|
and line = Text.get tw ~start:(ins,[`Linestart]) ~stop:(ins,[`Lineend]) in
|
2001-09-06 01:52:32 -07:00
|
|
|
ignore (Str.string_match reg line 0);
|
1999-12-10 01:40:51 -08:00
|
|
|
let len = Str.match_end () in
|
2000-04-11 20:43:25 -07:00
|
|
|
if len < c then Text.insert tw ~index:(ins,[]) ~text:"\t" else
|
1999-12-10 01:40:51 -08:00
|
|
|
let width = string_width (Str.matched_string line) in
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.mark_set tw ~mark:"insert" ~index:(ins,[`Linestart;`Char len]);
|
1999-12-10 01:40:51 -08:00
|
|
|
let indent =
|
1999-11-30 06:59:39 -08:00
|
|
|
if l <= 1 then 2 else
|
|
|
|
let previous =
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.get tw ~start:(ins,[`Line(-1);`Linestart])
|
|
|
|
~stop:(ins,[`Line(-1);`Lineend]) in
|
2001-09-06 01:52:32 -07:00
|
|
|
ignore (Str.string_match reg previous 0);
|
1999-11-30 06:59:39 -08:00
|
|
|
let previous = Str.matched_string previous in
|
1999-12-10 01:40:51 -08:00
|
|
|
let width_previous = string_width previous in
|
1999-11-30 06:59:39 -08:00
|
|
|
if width_previous <= width then 2 else width_previous - width
|
|
|
|
in
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.insert tw ~index:(ins,[]) ~text:(String.make indent ' ')
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* The editor class *)
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
class editor ~top ~menus = object (self)
|
|
|
|
val file_menu = new Jg_menu.c "File" ~parent:menus
|
|
|
|
val edit_menu = new Jg_menu.c "Edit" ~parent:menus
|
|
|
|
val compiler_menu = new Jg_menu.c "Compiler" ~parent:menus
|
|
|
|
val module_menu = new Jg_menu.c "Modules" ~parent:menus
|
|
|
|
val window_menu = new Jg_menu.c "Windows" ~parent:menus
|
1999-11-30 06:59:39 -08:00
|
|
|
val label =
|
2000-04-11 20:43:25 -07:00
|
|
|
Checkbutton.create menus ~state:`Disabled
|
|
|
|
~onvalue:"modified" ~offvalue:"unchanged"
|
1999-11-30 06:59:39 -08:00
|
|
|
val mutable current_dir = Unix.getcwd ()
|
|
|
|
val mutable error_messages = []
|
|
|
|
val mutable windows = []
|
|
|
|
val mutable current_tw = Text.create top
|
2000-04-11 20:43:25 -07:00
|
|
|
val vwindow = Textvariable.create ~on:top ()
|
1999-11-30 06:59:39 -08:00
|
|
|
val mutable window_counter = 0
|
|
|
|
|
|
|
|
method reset_window_menu =
|
2000-04-11 20:43:25 -07:00
|
|
|
Menu.delete window_menu#menu ~first:(`Num 0) ~last:`End;
|
1999-11-30 06:59:39 -08:00
|
|
|
List.iter
|
2001-09-06 01:52:32 -07:00
|
|
|
(List.sort windows ~cmp:
|
|
|
|
(fun w1 w2 ->
|
|
|
|
compare (Filename.basename w1.name) (Filename.basename w2.name)))
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun txt ->
|
|
|
|
Menu.add_radiobutton window_menu#menu
|
2000-04-11 20:43:25 -07:00
|
|
|
~label:(Filename.basename txt.name)
|
|
|
|
~variable:vwindow ~value:txt.number
|
|
|
|
~command:(fun () -> self#set_edit txt)
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
method set_edit txt =
|
|
|
|
if windows <> [] then
|
|
|
|
Pack.forget [(List.hd windows).frame];
|
2000-04-02 18:57:52 -07:00
|
|
|
windows <- txt :: exclude txt windows;
|
1999-11-30 06:59:39 -08:00
|
|
|
self#reset_window_menu;
|
|
|
|
current_tw <- txt.tw;
|
2000-04-11 20:43:25 -07:00
|
|
|
Checkbutton.configure label ~text:(Filename.basename txt.name)
|
|
|
|
~variable:txt.modified;
|
2000-04-02 18:57:52 -07:00
|
|
|
Textvariable.set vwindow txt.number;
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.yview txt.tw ~scroll:(`Page 0);
|
|
|
|
pack [txt.frame] ~fill:`Both ~expand:true ~side:`Bottom
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
method new_window name =
|
|
|
|
let tl, tw, sb = Jg_text.create_with_scrollbar top in
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.configure tw ~background:`White;
|
1999-11-30 06:59:39 -08:00
|
|
|
Jg_bind.enter_focus tw;
|
|
|
|
window_counter <- window_counter + 1;
|
|
|
|
let txt =
|
|
|
|
{ name = name; tw = tw; frame = tl;
|
|
|
|
number = string_of_int window_counter;
|
2000-04-11 20:43:25 -07:00
|
|
|
modified = Textvariable.create ~on:tw ();
|
1999-11-30 06:59:39 -08:00
|
|
|
shell = None;
|
|
|
|
structure = []; signature = []; psignature = [] }
|
|
|
|
in
|
|
|
|
let control c = Char.chr (Char.code c - 96) in
|
2000-04-11 20:43:25 -07:00
|
|
|
bind tw ~events:[`Modified([`Alt], `KeyPress)] ~action:ignore;
|
|
|
|
bind tw ~events:[`KeyPress] ~fields:[`Char]
|
|
|
|
~action:(fun ev ->
|
2000-12-28 05:07:42 -08:00
|
|
|
if ev.ev_Char <> "" &&
|
|
|
|
(ev.ev_Char.[0] >= ' ' ||
|
2000-04-02 18:57:52 -07:00
|
|
|
List.mem ev.ev_Char.[0]
|
2000-04-11 20:43:25 -07:00
|
|
|
(List.map ~f:control ['d'; 'h'; 'i'; 'k'; 'o'; 't'; 'w'; 'y']))
|
2000-04-02 18:57:52 -07:00
|
|
|
then Textvariable.set txt.modified "modified");
|
2000-04-11 20:43:25 -07:00
|
|
|
bind tw ~events:[`KeyPressDetail"Tab"] ~breakable:true
|
|
|
|
~action:(fun _ ->
|
1999-11-30 06:59:39 -08:00
|
|
|
indent_line tw;
|
2000-04-02 18:57:52 -07:00
|
|
|
Textvariable.set txt.modified "modified";
|
1999-12-16 00:37:38 -08:00
|
|
|
break ());
|
2000-04-11 20:43:25 -07:00
|
|
|
bind tw ~events:[`Modified([`Control],`KeyPressDetail"k")]
|
|
|
|
~action:(fun _ ->
|
1999-11-30 06:59:39 -08:00
|
|
|
let text =
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.get tw ~start:(`Mark"insert",[]) ~stop:(`Mark"insert",[`Lineend])
|
2001-09-06 01:52:32 -07:00
|
|
|
in ignore (Str.string_match (Str.regexp "[ \t]*") text 0);
|
1999-11-30 06:59:39 -08:00
|
|
|
if Str.match_end () <> String.length text then begin
|
|
|
|
Clipboard.clear ();
|
2000-04-11 20:43:25 -07:00
|
|
|
Clipboard.append ~data:text ()
|
1999-12-16 00:37:38 -08:00
|
|
|
end);
|
2000-04-11 20:43:25 -07:00
|
|
|
bind tw ~events:[`KeyRelease] ~fields:[`Char]
|
|
|
|
~action:(fun ev ->
|
1999-11-30 06:59:39 -08:00
|
|
|
if ev.ev_Char <> "" then
|
2000-04-11 20:43:25 -07:00
|
|
|
Lexical.tag tw ~start:(`Mark"insert", [`Linestart])
|
|
|
|
~stop:(`Mark"insert", [`Lineend]));
|
|
|
|
bind tw ~events:[`Motion] ~action:(fun _ -> Focus.set tw);
|
|
|
|
bind tw ~events:[`ButtonPressDetail 2]
|
|
|
|
~action:(fun _ ->
|
2000-04-02 18:57:52 -07:00
|
|
|
Textvariable.set txt.modified "modified";
|
2000-04-11 20:43:25 -07:00
|
|
|
Lexical.tag txt.tw ~start:(`Mark"insert", [`Linestart])
|
|
|
|
~stop:(`Mark"insert", [`Lineend]));
|
|
|
|
bind tw ~events:[`Modified([`Double], `ButtonPressDetail 1)]
|
|
|
|
~fields:[`MouseX;`MouseY]
|
|
|
|
~action:(fun ev -> search_pos_window txt ~x:ev.ev_MouseX ~y:ev.ev_MouseY);
|
|
|
|
bind tw ~events:[`ButtonPressDetail 3] ~fields:[`MouseX;`MouseY]
|
|
|
|
~action:(fun ev -> search_pos_menu txt ~x:ev.ev_MouseX ~y:ev.ev_MouseY);
|
|
|
|
|
|
|
|
pack [sb] ~fill:`Y ~side:`Right;
|
|
|
|
pack [tw] ~fill:`Both ~expand:true ~side:`Left;
|
1999-11-30 06:59:39 -08:00
|
|
|
self#set_edit txt;
|
|
|
|
Checkbutton.deselect label;
|
|
|
|
Lexical.init_tags txt.tw
|
|
|
|
|
|
|
|
method clear_errors () =
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.tag_remove current_tw ~tag:"error" ~start:tstart ~stop:tend;
|
1999-11-30 06:59:39 -08:00
|
|
|
List.iter error_messages
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:(fun tl -> try destroy tl with Protocol.TkError _ -> ());
|
1999-11-30 06:59:39 -08:00
|
|
|
error_messages <- []
|
|
|
|
|
|
|
|
method typecheck () =
|
|
|
|
self#clear_errors ();
|
|
|
|
error_messages <- Typecheck.f (List.hd windows)
|
|
|
|
|
|
|
|
method lex () =
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.tag_remove current_tw ~tag:"error" ~start:tstart ~stop:tend;
|
1999-11-30 06:59:39 -08:00
|
|
|
Lexical.tag current_tw
|
|
|
|
|
|
|
|
method save_text ?name:l txt =
|
|
|
|
let l = match l with None -> [txt.name] | Some l -> l in
|
|
|
|
if l = [] then () else
|
|
|
|
let name = List.hd l in
|
|
|
|
if txt.name <> name then current_dir <- Filename.dirname name;
|
|
|
|
try
|
|
|
|
if Sys.file_exists name then
|
|
|
|
if txt.name = name then
|
2001-09-06 01:52:32 -07:00
|
|
|
Sys.rename name (name ^ "~")
|
1999-11-30 06:59:39 -08:00
|
|
|
else begin match
|
2000-04-11 20:43:25 -07:00
|
|
|
Jg_message.ask ~master:top ~title:"Save"
|
1999-11-30 06:59:39 -08:00
|
|
|
("File `" ^ name ^ "' exists. Overwrite it?")
|
|
|
|
with `yes -> () | `no | `cancel -> raise Exit
|
|
|
|
end;
|
|
|
|
let file = open_out name in
|
2000-04-11 20:43:25 -07:00
|
|
|
let text = Text.get txt.tw ~start:tstart ~stop:(tposend 1) in
|
2000-04-02 18:57:52 -07:00
|
|
|
output_string file text;
|
1999-11-30 06:59:39 -08:00
|
|
|
close_out file;
|
2000-04-11 20:43:25 -07:00
|
|
|
Checkbutton.configure label ~text:(Filename.basename name);
|
1999-11-30 06:59:39 -08:00
|
|
|
Checkbutton.deselect label;
|
|
|
|
txt.name <- name
|
|
|
|
with
|
|
|
|
Sys_error _ | Exit -> ()
|
|
|
|
|
|
|
|
method load_text l =
|
|
|
|
if l = [] then () else
|
|
|
|
let name = List.hd l in
|
|
|
|
try
|
|
|
|
let index =
|
|
|
|
try
|
2000-04-11 20:43:25 -07:00
|
|
|
self#set_edit (List.find windows ~f:(fun x -> x.name = name));
|
1999-11-30 06:59:39 -08:00
|
|
|
let txt = List.hd windows in
|
|
|
|
if Textvariable.get txt.modified = "modified" then
|
2000-04-11 20:43:25 -07:00
|
|
|
begin match Jg_message.ask ~master:top ~title:"Open"
|
1999-11-30 06:59:39 -08:00
|
|
|
("`" ^ Filename.basename txt.name ^ "' modified. Save it?")
|
|
|
|
with `yes -> self#save_text txt
|
|
|
|
| `no -> ()
|
|
|
|
| `cancel -> raise Exit
|
|
|
|
end;
|
|
|
|
Checkbutton.deselect label;
|
2000-04-11 20:43:25 -07:00
|
|
|
(Text.index current_tw ~index:(`Mark"insert", []), [])
|
1999-11-30 06:59:39 -08:00
|
|
|
with Not_found -> self#new_window name; tstart
|
|
|
|
in
|
|
|
|
current_dir <- Filename.dirname name;
|
|
|
|
let file = open_in name
|
|
|
|
and tw = current_tw
|
|
|
|
and len = ref 0
|
2000-04-02 18:57:52 -07:00
|
|
|
and buf = String.create 4096 in
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.delete tw ~start:tstart ~stop:tend;
|
1999-11-30 06:59:39 -08:00
|
|
|
while
|
2001-09-06 01:52:32 -07:00
|
|
|
len := input file buf 0 4096;
|
1999-11-30 06:59:39 -08:00
|
|
|
!len > 0
|
|
|
|
do
|
2000-04-11 20:43:25 -07:00
|
|
|
Jg_text.output tw ~buf ~pos:0 ~len:!len
|
1999-11-30 06:59:39 -08:00
|
|
|
done;
|
|
|
|
close_in file;
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.mark_set tw ~mark:"insert" ~index;
|
|
|
|
Text.see tw ~index;
|
2000-12-28 05:07:42 -08:00
|
|
|
if Filename.check_suffix name ".ml" ||
|
2000-04-02 18:57:52 -07:00
|
|
|
Filename.check_suffix name ".mli"
|
1999-11-30 06:59:39 -08:00
|
|
|
then begin
|
|
|
|
if !lex_on_load then self#lex ();
|
|
|
|
if !type_on_load then self#typecheck ()
|
|
|
|
end
|
|
|
|
with
|
|
|
|
Sys_error _ | Exit -> ()
|
|
|
|
|
|
|
|
method close_window txt =
|
|
|
|
try
|
|
|
|
if Textvariable.get txt.modified = "modified" then
|
2000-04-11 20:43:25 -07:00
|
|
|
begin match Jg_message.ask ~master:top ~title:"Close"
|
1999-11-30 06:59:39 -08:00
|
|
|
("`" ^ Filename.basename txt.name ^ "' modified. Save it?")
|
|
|
|
with `yes -> self#save_text txt
|
|
|
|
| `no -> ()
|
|
|
|
| `cancel -> raise Exit
|
|
|
|
end;
|
2000-04-02 18:57:52 -07:00
|
|
|
windows <- exclude txt windows;
|
1999-11-30 06:59:39 -08:00
|
|
|
if windows = [] then
|
|
|
|
self#new_window (current_dir ^ "/untitled")
|
|
|
|
else self#set_edit (List.hd windows);
|
|
|
|
destroy txt.frame
|
|
|
|
with Exit -> ()
|
|
|
|
|
|
|
|
method open_file () =
|
2000-04-11 20:43:25 -07:00
|
|
|
Fileselect.f ~title:"Open File" ~action:self#load_text
|
|
|
|
~dir:current_dir ~filter:("*.{ml,mli}") ~sync:true ()
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
method save_file () = self#save_text (List.hd windows)
|
|
|
|
|
|
|
|
method close_file () = self#close_window (List.hd windows)
|
|
|
|
|
2000-04-19 19:45:44 -07:00
|
|
|
method quit ?(cancel=true) () =
|
2000-02-12 15:19:35 -08:00
|
|
|
try
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter windows ~f:
|
2000-02-12 15:19:35 -08:00
|
|
|
begin fun txt ->
|
1999-11-30 06:59:39 -08:00
|
|
|
if Textvariable.get txt.modified = "modified" then
|
2000-04-19 19:45:44 -07:00
|
|
|
match Jg_message.ask ~master:top ~title:"Quit" ~cancel
|
1999-11-30 06:59:39 -08:00
|
|
|
("`" ^ Filename.basename txt.name ^ "' modified. Save it?")
|
|
|
|
with `yes -> self#save_text txt
|
|
|
|
| `no -> ()
|
2000-02-12 15:19:35 -08:00
|
|
|
| `cancel -> raise Exit
|
|
|
|
end;
|
2000-04-11 20:43:25 -07:00
|
|
|
bind top ~events:[`Destroy];
|
2000-04-19 19:45:44 -07:00
|
|
|
destroy top
|
|
|
|
with Exit -> ()
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
method reopen ~file ~pos =
|
1999-11-30 06:59:39 -08:00
|
|
|
if not (Winfo.ismapped top) then Wm.deiconify top;
|
|
|
|
match file with None -> ()
|
|
|
|
| Some file ->
|
2001-11-21 23:36:15 -08:00
|
|
|
self#load_text [file];
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.mark_set current_tw ~mark:"insert" ~index:(tpos pos);
|
2001-11-21 23:36:15 -08:00
|
|
|
try
|
|
|
|
let index =
|
|
|
|
Text.search current_tw ~switches:[`Backwards] ~pattern:"*)"
|
|
|
|
~start:(tpos pos) ~stop:(tpos pos ~modi:[`Line(-1)]) in
|
|
|
|
let index =
|
|
|
|
Text.search current_tw ~switches:[`Backwards] ~pattern:"(*"
|
|
|
|
~start:(index,[]) ~stop:(tpos pos ~modi:[`Line(-20)]) in
|
|
|
|
let s = Text.get current_tw ~start:(index,[`Line(-1);`Linestart])
|
|
|
|
~stop:(index,[`Line(-1);`Lineend]) in
|
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
match s.[i] with '\t'|' ' -> () | _ -> raise Not_found
|
|
|
|
done;
|
|
|
|
Text.yview_index current_tw ~index:(index,[`Line(-1)])
|
|
|
|
with _ ->
|
|
|
|
Text.yview_index current_tw ~index:(tpos pos ~modi:[`Line(-2)])
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
initializer
|
|
|
|
(* Create a first window *)
|
|
|
|
self#new_window (current_dir ^ "/untitled");
|
|
|
|
|
|
|
|
(* Bindings for the main window *)
|
|
|
|
List.iter
|
|
|
|
[ [`Control], "s", (fun () -> Jg_text.search_string current_tw);
|
|
|
|
[`Control], "g", (fun () -> goto_line current_tw);
|
1999-12-10 01:40:51 -08:00
|
|
|
[`Alt], "s", self#save_file;
|
1999-12-09 07:08:02 -08:00
|
|
|
[`Alt], "x", (fun () -> send_phrase (List.hd windows));
|
1999-11-30 06:59:39 -08:00
|
|
|
[`Alt], "l", self#lex;
|
|
|
|
[`Alt], "t", self#typecheck ]
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:begin fun (modi,key,act) ->
|
|
|
|
bind top ~events:[`Modified(modi, `KeyPressDetail key)] ~breakable:true
|
|
|
|
~action:(fun _ -> act (); break ())
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
|
|
|
|
2000-04-19 19:45:44 -07:00
|
|
|
bind top ~events:[`Destroy] ~fields:[`Widget] ~action:
|
2000-02-12 15:19:35 -08:00
|
|
|
begin fun ev ->
|
1999-12-16 00:37:38 -08:00
|
|
|
if Widget.name ev.ev_Widget = Widget.name top
|
2000-04-19 19:45:44 -07:00
|
|
|
then self#quit ~cancel:false ()
|
2000-02-12 15:19:35 -08:00
|
|
|
end;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* File menu *)
|
2000-04-11 20:43:25 -07:00
|
|
|
file_menu#add_command "Open File..." ~command:self#open_file;
|
1999-11-30 06:59:39 -08:00
|
|
|
file_menu#add_command "Reopen"
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:(fun () -> self#load_text [(List.hd windows).name]);
|
|
|
|
file_menu#add_command "Save File" ~command:self#save_file ~accelerator:"M-s";
|
|
|
|
file_menu#add_command "Save As..." ~underline:5 ~command:
|
2000-02-12 15:19:35 -08:00
|
|
|
begin fun () ->
|
1999-11-30 06:59:39 -08:00
|
|
|
let txt = List.hd windows in
|
2000-04-11 20:43:25 -07:00
|
|
|
Fileselect.f ~title:"Save as File"
|
|
|
|
~action:(fun name -> self#save_text txt ~name)
|
|
|
|
~dir:(Filename.dirname txt.name)
|
|
|
|
~filter:"*.{ml,mli}"
|
|
|
|
~file:(Filename.basename txt.name)
|
|
|
|
~sync:true ~usepath:false ()
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
2000-04-11 20:43:25 -07:00
|
|
|
file_menu#add_command "Close File" ~command:self#close_file;
|
|
|
|
file_menu#add_command "Close Window" ~command:self#quit ~underline:6;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* Edit menu *)
|
2000-04-11 20:43:25 -07:00
|
|
|
edit_menu#add_command "Paste selection" ~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
2000-04-11 20:43:25 -07:00
|
|
|
Text.insert current_tw ~index:(`Mark"insert",[])
|
|
|
|
~text:(Selection.get ~displayof:top ())
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
2000-04-11 20:43:25 -07:00
|
|
|
edit_menu#add_command "Goto..." ~accelerator:"C-g"
|
|
|
|
~command:(fun () -> goto_line current_tw);
|
|
|
|
edit_menu#add_command "Search..." ~accelerator:"C-s"
|
|
|
|
~command:(fun () -> Jg_text.search_string current_tw);
|
|
|
|
edit_menu#add_command "To shell" ~accelerator:"M-x"
|
|
|
|
~command:(fun () -> send_phrase (List.hd windows));
|
1999-11-30 06:59:39 -08:00
|
|
|
edit_menu#add_command "Select shell..."
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:(fun () -> select_shell (List.hd windows));
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* Compiler menu *)
|
|
|
|
compiler_menu#add_command "Preferences..."
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:compiler_preferences;
|
|
|
|
compiler_menu#add_command "Lex" ~accelerator:"M-l"
|
|
|
|
~command:self#lex;
|
|
|
|
compiler_menu#add_command "Typecheck" ~accelerator:"M-t"
|
|
|
|
~command:self#typecheck;
|
1999-11-30 06:59:39 -08:00
|
|
|
compiler_menu#add_command "Clear errors"
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:self#clear_errors;
|
|
|
|
compiler_menu#add_command "Signature..." ~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
|
|
|
let txt = List.hd windows in if txt.signature <> [] then
|
|
|
|
let basename = Filename.basename txt.name in
|
|
|
|
let modname = String.capitalize
|
|
|
|
(try Filename.chop_extension basename with _ -> basename) in
|
|
|
|
let env =
|
|
|
|
Env.add_module (Ident.create modname)
|
|
|
|
(Types.Tmty_signature txt.signature)
|
|
|
|
Env.initial
|
2000-04-11 20:43:25 -07:00
|
|
|
in Viewer.view_defined (Longident.Lident modname) ~env
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
|
|
|
|
|
|
|
(* Modules *)
|
|
|
|
module_menu#add_command "Path editor..."
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:(fun () -> Setpath.set ~dir:current_dir);
|
1999-11-30 06:59:39 -08:00
|
|
|
module_menu#add_command "Reset cache"
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:(fun () -> Setpath.exec_update_hooks (); Env.reset_cache ());
|
1999-11-30 06:59:39 -08:00
|
|
|
module_menu#add_command "Search symbol..."
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:Viewer.search_symbol;
|
1999-11-30 06:59:39 -08:00
|
|
|
module_menu#add_command "Close all"
|
2000-04-11 20:43:25 -07:00
|
|
|
~command:Viewer.close_all_views;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* pack everything *)
|
2000-04-11 20:43:25 -07:00
|
|
|
pack (List.map ~f:(fun m -> coe m#button)
|
1999-11-30 06:59:39 -08:00
|
|
|
[file_menu; edit_menu; compiler_menu; module_menu; window_menu]
|
|
|
|
@ [coe label])
|
2000-04-11 20:43:25 -07:00
|
|
|
~side:`Left ~ipadx:5 ~anchor:`W;
|
|
|
|
pack [menus] ~before:(List.hd windows).frame ~side:`Top ~fill:`X
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
(* The main function starts here ! *)
|
|
|
|
|
|
|
|
let already_open : editor option ref = ref None
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let editor ?file ?(pos=0) () =
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
if match !already_open with None -> false
|
|
|
|
| Some ed ->
|
2000-04-11 20:43:25 -07:00
|
|
|
try ed#reopen ~file ~pos; true
|
1999-11-30 06:59:39 -08:00
|
|
|
with Protocol.TkError _ -> already_open := None; false
|
|
|
|
then () else
|
|
|
|
let top = Jg_toplevel.titled "Editor" in
|
2000-04-11 20:43:25 -07:00
|
|
|
let menus = Frame.create top ~name:"menubar" in
|
|
|
|
let ed = new editor ~top ~menus in
|
1999-11-30 06:59:39 -08:00
|
|
|
already_open := Some ed;
|
2000-04-11 20:43:25 -07:00
|
|
|
if file <> None then ed#reopen ~file ~pos
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let f ?file ?pos ?(opendialog=false) () =
|
1999-11-30 06:59:39 -08:00
|
|
|
if opendialog then
|
2000-04-11 20:43:25 -07:00
|
|
|
Fileselect.f ~title:"Open File"
|
|
|
|
~action:(function [file] -> editor ~file () | _ -> ())
|
|
|
|
~filter:("*.{ml,mli}") ~sync:true ()
|
|
|
|
else editor ?file ?pos ()
|