src/Doc/antiquote_setup.ML
author wenzelm
Mon Dec 22 16:44:24 2014 +0100 (2014-12-22)
changeset 59175 bf465f335e85
parent 59083 88b0b1f28adc
child 59809 87641097d0f3
permissions -rw-r--r--
system option "pretty_margin" is superseded by "thy_output_margin";
wenzelm@48985
     1
(*  Title:      Doc/antiquote_setup.ML
wenzelm@21375
     2
    Author:     Makarius
wenzelm@21375
     3
wenzelm@26742
     4
Auxiliary antiquotations for the Isabelle manuals.
wenzelm@21375
     5
*)
wenzelm@21375
     6
wenzelm@56059
     7
structure Antiquote_Setup: sig end =
wenzelm@26742
     8
struct
wenzelm@21375
     9
wenzelm@26742
    10
(* misc utils *)
wenzelm@26742
    11
wenzelm@29736
    12
fun translate f = Symbol.explode #> map f #> implode;
wenzelm@29736
    13
wenzelm@29736
    14
val clean_string = translate
wenzelm@26853
    15
  (fn "_" => "\\_"
wenzelm@30120
    16
    | "#" => "\\#"
wenzelm@52408
    17
    | "$" => "\\$"
wenzelm@52408
    18
    | "%" => "\\%"
wenzelm@26897
    19
    | "<" => "$<$"
wenzelm@26768
    20
    | ">" => "$>$"
wenzelm@26768
    21
    | "{" => "\\{"
wenzelm@30120
    22
    | "|" => "$\\mid$"
wenzelm@26768
    23
    | "}" => "\\}"
wenzelm@42666
    24
    | "\<hyphen>" => "-"
wenzelm@26768
    25
    | c => c);
wenzelm@26751
    26
wenzelm@31546
    27
fun clean_name "\<dots>" = "dots"
wenzelm@26897
    28
  | clean_name ".." = "ddot"
wenzelm@26897
    29
  | clean_name "." = "dot"
wenzelm@26910
    30
  | clean_name "_" = "underscore"
wenzelm@26897
    31
  | clean_name "{" = "braceleft"
wenzelm@26897
    32
  | clean_name "}" = "braceright"
wenzelm@42666
    33
  | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
wenzelm@26897
    34
wenzelm@26742
    35
wenzelm@26742
    36
(* ML text *)
wenzelm@26742
    37
wenzelm@26742
    38
local
wenzelm@26742
    39
wenzelm@59067
    40
fun ml_val (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read ");"
wenzelm@59067
    41
  | ml_val (toks1, toks2) =
wenzelm@59067
    42
      ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");";
wenzelm@21375
    43
wenzelm@59067
    44
fun ml_op (toks1, []) = ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read ");"
wenzelm@59067
    45
  | ml_op (toks1, toks2) =
wenzelm@59067
    46
      ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");";
wenzelm@46261
    47
wenzelm@59067
    48
fun ml_type (toks1, []) = ML_Lex.read "val _ = NONE : (" @ toks1 @ ML_Lex.read ") option;"
wenzelm@55831
    49
  | ml_type (toks1, toks2) =
wenzelm@59067
    50
      ML_Lex.read "val _ = [NONE : (" @ toks1 @ ML_Lex.read ") option, NONE : (" @
wenzelm@59067
    51
        toks2 @ ML_Lex.read ") option];";
haftmann@22289
    52
wenzelm@59067
    53
fun ml_exception (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : exn);"
wenzelm@55837
    54
  | ml_exception (toks1, toks2) =
wenzelm@59067
    55
      ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read " -> exn);";
haftmann@22289
    56
wenzelm@55831
    57
fun ml_structure (toks, _) =
wenzelm@59067
    58
  ML_Lex.read "functor XXX() = struct structure XX = " @ toks @ ML_Lex.read " end;";
wenzelm@21375
    59
wenzelm@55831
    60
fun ml_functor (Antiquote.Text tok :: _, _) =
wenzelm@59067
    61
      ML_Lex.read "ML_Env.check_functor " @
wenzelm@59067
    62
      ML_Lex.read (ML_Syntax.print_string (ML_Lex.content_of tok))
wenzelm@55831
    63
  | ml_functor _ = raise Fail "Bad ML functor specification";
wenzelm@21375
    64
wenzelm@59082
    65
val is_name =
wenzelm@59082
    66
  ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.Long_Ident);
wenzelm@39869
    67
wenzelm@39869
    68
fun ml_name txt =
wenzelm@39869
    69
  (case filter is_name (ML_Lex.tokenize txt) of
wenzelm@39869
    70
    toks as [_] => ML_Lex.flatten toks
wenzelm@39869
    71
  | _ => error ("Single ML name expected in input: " ^ quote txt));
wenzelm@39869
    72
wenzelm@55831
    73
fun prep_ml source =
wenzelm@59066
    74
  (Input.source_content source, ML_Lex.read_source false source);
wenzelm@55831
    75
wenzelm@37216
    76
fun index_ml name kind ml = Thy_Output.antiquotation name
wenzelm@58069
    77
  (Scan.lift (Args.text_source_position -- Scan.option (Args.colon |-- Args.text_source_position)))
wenzelm@55831
    78
  (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
wenzelm@30394
    79
    let
wenzelm@55831
    80
      val (txt1, toks1) = prep_ml source1;
wenzelm@55831
    81
      val (txt2, toks2) =
wenzelm@55831
    82
        (case opt_source2 of
wenzelm@55831
    83
          SOME source => prep_ml source
wenzelm@55831
    84
        | NONE => ("", []));
wenzelm@55831
    85
wenzelm@30394
    86
      val txt =
wenzelm@30394
    87
        if txt2 = "" then txt1
wenzelm@30394
    88
        else if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@30394
    89
        else if kind = "exception" then txt1 ^ " of " ^ txt2
wenzelm@50239
    90
        else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
wenzelm@42290
    91
        then txt1 ^ ": " ^ txt2
wenzelm@39858
    92
        else txt1 ^ " : " ^ txt2;
wenzelm@30394
    93
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
wenzelm@55831
    94
wenzelm@59064
    95
      val pos = Input.pos_of source1;
wenzelm@56275
    96
      val _ =
wenzelm@57477
    97
        ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
wenzelm@57477
    98
          handle ERROR msg => error (msg ^ Position.here pos);
wenzelm@30394
    99
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
   100
    in
wenzelm@39869
   101
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
wenzelm@58716
   102
      (Thy_Output.verbatim_text ctxt txt')
wenzelm@30394
   103
    end);
wenzelm@26742
   104
wenzelm@26742
   105
in
wenzelm@21375
   106
wenzelm@56059
   107
val _ =
wenzelm@56059
   108
  Theory.setup
wenzelm@56059
   109
   (index_ml @{binding index_ML} "" ml_val #>
wenzelm@56059
   110
    index_ml @{binding index_ML_op} "infix" ml_op #>
wenzelm@56059
   111
    index_ml @{binding index_ML_type} "type" ml_type #>
wenzelm@56059
   112
    index_ml @{binding index_ML_exception} "exception" ml_exception #>
wenzelm@56059
   113
    index_ml @{binding index_ML_structure} "structure" ml_structure #>
wenzelm@56059
   114
    index_ml @{binding index_ML_functor} "functor" ml_functor);
wenzelm@21375
   115
wenzelm@26742
   116
end;
wenzelm@21375
   117
berghofe@23846
   118
wenzelm@30394
   119
(* named theorems *)
berghofe@23846
   120
wenzelm@56059
   121
val _ =
wenzelm@56059
   122
  Theory.setup (Thy_Output.antiquotation @{binding named_thms}
wenzelm@43564
   123
    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@43564
   124
    (fn {context = ctxt, ...} =>
wenzelm@43564
   125
      map (apfst (Thy_Output.pretty_thm ctxt))
wenzelm@43564
   126
      #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
wenzelm@43564
   127
      #> (if Config.get ctxt Thy_Output.display
wenzelm@43564
   128
          then
wenzelm@43564
   129
            map (fn (p, name) =>
wenzelm@59175
   130
              Output.output
wenzelm@59175
   131
                (Thy_Output.string_of_margin ctxt
wenzelm@59175
   132
                  (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
wenzelm@43564
   133
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   134
            #> space_implode "\\par\\smallskip%\n"
wenzelm@43564
   135
            #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@43564
   136
          else
wenzelm@43564
   137
            map (fn (p, name) =>
wenzelm@43564
   138
              Output.output (Pretty.str_of p) ^
wenzelm@43564
   139
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   140
            #> space_implode "\\par\\smallskip%\n"
wenzelm@56059
   141
            #> enclose "\\isa{" "}")));
wenzelm@26742
   142
wenzelm@26742
   143
wenzelm@30394
   144
(* theory file *)
wenzelm@26742
   145
wenzelm@56059
   146
val _ =
wenzelm@56059
   147
  Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
wenzelm@43564
   148
    (fn {context = ctxt, ...} =>
wenzelm@56208
   149
      fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
wenzelm@56059
   150
wenzelm@56059
   151
wenzelm@56059
   152
(* Isabelle/jEdit elements *)
wenzelm@56059
   153
wenzelm@56059
   154
local
wenzelm@56059
   155
wenzelm@56059
   156
fun parse_named a (XML.Elem ((b, props), _)) =
wenzelm@56059
   157
      (case Properties.get props "NAME" of
wenzelm@56059
   158
        SOME name => if a = b then [name] else []
wenzelm@56059
   159
      | NONE => [])
wenzelm@56059
   160
  | parse_named _ _ = [];
wenzelm@56059
   161
wenzelm@57334
   162
val isabelle_jedit_actions =
wenzelm@56135
   163
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
wenzelm@56059
   164
    XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@56059
   165
  | _ => []);
wenzelm@56059
   166
wenzelm@57334
   167
val isabelle_jedit_dockables =
wenzelm@56135
   168
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
wenzelm@56059
   169
    XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
wenzelm@56059
   170
  | _ => []);
wenzelm@56059
   171
wenzelm@57334
   172
val jedit_actions =
wenzelm@57334
   173
  Lazy.lazy (fn () =>
wenzelm@57334
   174
    (case Isabelle_System.bash_output
wenzelm@57334
   175
      "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
wenzelm@57334
   176
      (txt, 0) =>
wenzelm@57334
   177
        (case XML.parse txt of
wenzelm@57334
   178
          XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@57334
   179
        | _ => [])
wenzelm@57334
   180
    | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
wenzelm@56059
   181
wenzelm@56059
   182
in
wenzelm@56059
   183
wenzelm@57334
   184
fun is_action a =
wenzelm@57334
   185
  member (op =) isabelle_jedit_actions a orelse
wenzelm@57334
   186
  member (op =) isabelle_jedit_dockables a orelse
wenzelm@57334
   187
  member (op =) (Lazy.force jedit_actions) a;
wenzelm@56059
   188
wenzelm@56059
   189
end;
wenzelm@26742
   190
wenzelm@26751
   191
wenzelm@30394
   192
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   193
wenzelm@26751
   194
local
wenzelm@26751
   195
wenzelm@26894
   196
fun no_check _ _ = true;
wenzelm@26894
   197
wenzelm@58928
   198
fun is_keyword ctxt (name, _) =
wenzelm@58931
   199
  Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name;
wenzelm@58919
   200
wenzelm@56294
   201
fun check_command ctxt (name, pos) =
wenzelm@58928
   202
  let
wenzelm@58928
   203
    val thy = Proof_Context.theory_of ctxt;
wenzelm@58928
   204
    val keywords = Thy_Header.get_keywords thy;
wenzelm@58928
   205
  in
wenzelm@58928
   206
    Keyword.is_command keywords name andalso
wenzelm@58923
   207
      let
wenzelm@58923
   208
        val markup =
wenzelm@59083
   209
          Token.explode keywords Position.none name
wenzelm@58928
   210
          |> maps (Outer_Syntax.command_reports thy)
wenzelm@58923
   211
          |> map (snd o fst);
wenzelm@58923
   212
        val _ = Context_Position.reports ctxt (map (pair pos) markup);
wenzelm@58923
   213
      in true end
wenzelm@58923
   214
  end;
wenzelm@53061
   215
wenzelm@56467
   216
fun check_system_option ctxt (name, pos) =
wenzelm@56467
   217
  (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
wenzelm@56467
   218
    handle ERROR _ => false;
wenzelm@56467
   219
wenzelm@56294
   220
fun check_tool ctxt (name, pos) =
wenzelm@53044
   221
  let
wenzelm@53044
   222
    fun tool dir =
wenzelm@53044
   223
      let val path = Path.append dir (Path.basic name)
wenzelm@53044
   224
      in if File.exists path then SOME path else NONE end;
wenzelm@53044
   225
  in
wenzelm@53045
   226
    (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
wenzelm@53044
   227
      NONE => false
wenzelm@56294
   228
    | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
wenzelm@53044
   229
  end;
wenzelm@28237
   230
wenzelm@26751
   231
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   232
wenzelm@56185
   233
fun entity check markup binding index =
wenzelm@37216
   234
  Thy_Output.antiquotation
wenzelm@56185
   235
    (binding |> Binding.map_name (fn name => name ^
wenzelm@43564
   236
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
wenzelm@53044
   237
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
wenzelm@53044
   238
    (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
wenzelm@30394
   239
      let
wenzelm@56185
   240
        val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
wenzelm@30394
   241
        val hyper_name =
wenzelm@30394
   242
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   243
        val hyper =
wenzelm@30394
   244
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   245
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   246
        val idx =
wenzelm@30394
   247
          (case index of
wenzelm@30394
   248
            NONE => ""
wenzelm@30394
   249
          | SOME is_def =>
wenzelm@30394
   250
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@30394
   251
      in
wenzelm@53044
   252
        if check ctxt (name, pos) then
wenzelm@30394
   253
          idx ^
wenzelm@30394
   254
          (Output.output name
wenzelm@30394
   255
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@38767
   256
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
   257
            |> (if Config.get ctxt Thy_Output.display
wenzelm@38767
   258
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   259
                else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@53044
   260
        else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
wenzelm@30394
   261
      end);
wenzelm@26897
   262
wenzelm@26894
   263
fun entity_antiqs check markup kind =
wenzelm@43564
   264
  entity check markup kind NONE #>
wenzelm@43564
   265
  entity check markup kind (SOME true) #>
wenzelm@43564
   266
  entity check markup kind (SOME false);
wenzelm@26751
   267
wenzelm@26751
   268
in
wenzelm@26751
   269
wenzelm@56059
   270
val _ =
wenzelm@56059
   271
  Theory.setup
wenzelm@56185
   272
   (entity_antiqs no_check "" @{binding syntax} #>
wenzelm@56294
   273
    entity_antiqs check_command "isacommand" @{binding command} #>
wenzelm@58919
   274
    entity_antiqs is_keyword "isakeyword" @{binding keyword} #>
wenzelm@58919
   275
    entity_antiqs is_keyword "isakeyword" @{binding element} #>
wenzelm@56185
   276
    entity_antiqs (can o Method.check_name) "" @{binding method} #>
wenzelm@56185
   277
    entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
wenzelm@56185
   278
    entity_antiqs no_check "" @{binding fact} #>
wenzelm@56185
   279
    entity_antiqs no_check "" @{binding variable} #>
wenzelm@56185
   280
    entity_antiqs no_check "" @{binding case} #>
wenzelm@56185
   281
    entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
wenzelm@56185
   282
    entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
wenzelm@58716
   283
    entity_antiqs no_check "isasystem" @{binding setting} #>
wenzelm@58716
   284
    entity_antiqs check_system_option "isasystem" @{binding system_option} #>
wenzelm@56185
   285
    entity_antiqs no_check "" @{binding inference} #>
wenzelm@58716
   286
    entity_antiqs no_check "isasystem" @{binding executable} #>
wenzelm@56294
   287
    entity_antiqs check_tool "isatool" @{binding tool} #>
wenzelm@56185
   288
    entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
wenzelm@58716
   289
    entity_antiqs (K (is_action o #1)) "isasystem" @{binding action});
wenzelm@26751
   290
wenzelm@26742
   291
end;
wenzelm@26751
   292
wenzelm@26751
   293
end;