src/Pure/Tools/jedit.ML
author wenzelm
Tue Nov 10 22:20:46 2015 +0100 (2015-11-10)
changeset 61621 70b8085f51b4
parent 61620 01db1bed4487
child 63669 256fc20716f2
permissions -rw-r--r--
more thorough check_action, including completion;
wenzelm@61617
     1
(*  Title:      Pure/Tools/jedit.ML
wenzelm@61617
     2
    Author:     Makarius
wenzelm@61617
     3
wenzelm@61617
     4
jEdit support.
wenzelm@61617
     5
*)
wenzelm@61617
     6
wenzelm@61617
     7
signature JEDIT =
wenzelm@61617
     8
sig
wenzelm@61620
     9
  val check_action: string * Position.T -> string
wenzelm@61617
    10
end;
wenzelm@61617
    11
wenzelm@61617
    12
structure JEdit: JEDIT =
wenzelm@61617
    13
struct
wenzelm@61617
    14
wenzelm@61617
    15
(* jEdit actions *)
wenzelm@61617
    16
wenzelm@61617
    17
local
wenzelm@61617
    18
wenzelm@61617
    19
fun parse_named a (XML.Elem ((b, props), _)) =
wenzelm@61617
    20
      (case Properties.get props "NAME" of
wenzelm@61617
    21
        SOME name => if a = b then [name] else []
wenzelm@61617
    22
      | NONE => [])
wenzelm@61617
    23
  | parse_named _ _ = [];
wenzelm@61617
    24
wenzelm@61617
    25
val isabelle_jedit_actions =
wenzelm@61617
    26
  Lazy.lazy (fn () =>
wenzelm@61617
    27
    (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
wenzelm@61617
    28
      XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@61617
    29
    | _ => []));
wenzelm@61617
    30
wenzelm@61617
    31
val isabelle_jedit_dockables =
wenzelm@61617
    32
  Lazy.lazy (fn () =>
wenzelm@61617
    33
    (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
wenzelm@61617
    34
      XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
wenzelm@61617
    35
    | _ => []));
wenzelm@61617
    36
wenzelm@61617
    37
val jedit_actions =
wenzelm@61617
    38
  Lazy.lazy (fn () =>
wenzelm@61617
    39
    (case Isabelle_System.bash_output
wenzelm@61617
    40
      "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
wenzelm@61617
    41
      (txt, 0) =>
wenzelm@61617
    42
        (case XML.parse txt of
wenzelm@61617
    43
          XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@61617
    44
        | _ => [])
wenzelm@61617
    45
    | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
wenzelm@61617
    46
wenzelm@61621
    47
val all_actions =
wenzelm@61621
    48
  Lazy.lazy (fn () =>
wenzelm@61621
    49
    Lazy.force isabelle_jedit_actions @
wenzelm@61621
    50
    Lazy.force isabelle_jedit_dockables @
wenzelm@61621
    51
    Lazy.force jedit_actions);
wenzelm@61617
    52
wenzelm@61620
    53
in
wenzelm@61620
    54
wenzelm@61621
    55
fun check_action (name, pos) =
wenzelm@61621
    56
  if member (op =) (Lazy.force all_actions) name then name
wenzelm@61621
    57
  else
wenzelm@61621
    58
    let
wenzelm@61621
    59
      val completion =
wenzelm@61621
    60
        Completion.make (name, pos)
wenzelm@61621
    61
          (fn completed =>
wenzelm@61621
    62
            Lazy.force all_actions
wenzelm@61621
    63
            |> filter completed
wenzelm@61621
    64
            |> sort_strings
wenzelm@61621
    65
            |> map (fn a => (a, ("action", a))));
wenzelm@61621
    66
      val report = Markup.markup_report (Completion.reported_text completion);
wenzelm@61621
    67
    in error ("Bad jEdit action " ^ quote name ^ Position.here pos ^ report) end
wenzelm@61620
    68
wenzelm@61617
    69
end;
wenzelm@61617
    70
wenzelm@61617
    71
end;