src/Pure/Tools/jedit.ML
author wenzelm
Fri, 12 Aug 2016 20:58:05 +0200
changeset 63681 d2448471ffba
parent 63680 6e1e8b5abbfa
child 66670 e5188cb1c3d8
permissions -rw-r--r--
active jEdit actions;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61617
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Tools/jedit.ML
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     3
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     4
jEdit support.
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     5
*)
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     6
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     7
signature JEDIT =
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
     8
sig
61620
01db1bed4487 tuned signature;
wenzelm
parents: 61617
diff changeset
     9
  val check_action: string * Position.T -> string
61617
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    10
end;
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    11
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    12
structure JEdit: JEDIT =
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    13
struct
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    14
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    15
(* jEdit actions *)
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    16
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    17
local
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    18
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    19
fun parse_named a (XML.Elem ((b, props), _)) =
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    20
      (case Properties.get props "NAME" of
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    21
        SOME name => if a = b then [name] else []
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    22
      | NONE => [])
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    23
  | parse_named _ _ = [];
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    24
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    25
val isabelle_jedit_actions =
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    26
  Lazy.lazy (fn () =>
63680
6e1e8b5abbfa more symbols;
wenzelm
parents: 63669
diff changeset
    27
    (case XML.parse (File.read \<^file>\<open>~~/src/Tools/jEdit/src/actions.xml\<close>) of
61617
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    28
      XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    29
    | _ => []));
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    30
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    31
val isabelle_jedit_dockables =
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    32
  Lazy.lazy (fn () =>
63680
6e1e8b5abbfa more symbols;
wenzelm
parents: 63669
diff changeset
    33
    (case XML.parse (File.read \<^file>\<open>~~/src/Tools/jEdit/src/dockables.xml\<close>) of
61617
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    34
      XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    35
    | _ => []));
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    36
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    37
val jedit_actions =
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    38
  Lazy.lazy (fn () =>
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    39
    (case Isabelle_System.bash_output
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    40
      "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    41
      (txt, 0) =>
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    42
        (case XML.parse txt of
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    43
          XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    44
        | _ => [])
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    45
    | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    46
61621
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    47
val all_actions =
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    48
  Lazy.lazy (fn () =>
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    49
    Lazy.force isabelle_jedit_actions @
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    50
    Lazy.force isabelle_jedit_dockables @
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    51
    Lazy.force jedit_actions);
61617
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    52
61620
01db1bed4487 tuned signature;
wenzelm
parents: 61617
diff changeset
    53
in
01db1bed4487 tuned signature;
wenzelm
parents: 61617
diff changeset
    54
61621
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    55
fun check_action (name, pos) =
63681
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    56
  if member (op =) (Lazy.force all_actions) name then
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    57
    let
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    58
      val ((bg1, bg2), en) =
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    59
        YXML.output_markup_elem
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    60
          (Active.make_markup Markup.jedit_actionN {implicit = false, properties = []});
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    61
      val msg = "Invoke " ^ bg1 ^ name ^ bg2 ^ name ^ en ^ " jEdit action";
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    62
    in writeln (msg ^ Position.here pos); name end
61621
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    63
  else
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    64
    let
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    65
      val completion =
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    66
        Completion.make (name, pos)
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    67
          (fn completed =>
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    68
            Lazy.force all_actions
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    69
            |> filter completed
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    70
            |> sort_strings
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    71
            |> map (fn a => (a, ("action", a))));
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    72
      val report = Markup.markup_report (Completion.reported_text completion);
70b8085f51b4 more thorough check_action, including completion;
wenzelm
parents: 61620
diff changeset
    73
    in error ("Bad jEdit action " ^ quote name ^ Position.here pos ^ report) end
61620
01db1bed4487 tuned signature;
wenzelm
parents: 61617
diff changeset
    74
63681
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    75
val _ =
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    76
  Theory.setup
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    77
    (Thy_Output.antiquotation @{binding action} (Scan.lift (Parse.position Parse.embedded))
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    78
      (fn {context = ctxt, ...} => fn (name, pos) =>
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    79
       (if Context_Position.is_reported ctxt pos then ignore (check_action (name, pos)) else ();
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    80
        Thy_Output.verbatim_text ctxt name)));
d2448471ffba active jEdit actions;
wenzelm
parents: 63680
diff changeset
    81
61617
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    82
end;
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    83
cd7549cd5fe7 clarified modules;
wenzelm
parents:
diff changeset
    84
end;