src/Pure/Thy/bibtex.ML
author wenzelm
Sat, 22 Aug 2020 20:37:31 +0200
changeset 72194 eef421b724c0
parent 71881 71de0a253842
child 72613 d01ea9e3bd2d
permissions -rw-r--r--
clarified names;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67274
4588f714a78a clarified directories;
wenzelm
parents: 67147
diff changeset
     1
(*  Title:      Pure/Thy/bibtex.ML
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     3
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     4
BibTeX support.
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     5
*)
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     6
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     7
signature BIBTEX =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     8
sig
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
     9
  val check_database:
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    10
    Position.T -> string -> (string * Position.T) list * (string * Position.T) list
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    11
  val check_database_output: Position.T -> string -> unit
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    12
  val cite_macro: string Config.T
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    13
end;
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    14
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    15
structure Bibtex: BIBTEX =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    16
struct
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    17
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    18
(* check database *)
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    19
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    20
type message = string * Position.T;
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    21
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    22
fun check_database pos0 database =
72194
eef421b724c0 clarified names;
wenzelm
parents: 71881
diff changeset
    23
  \<^scala>\<open>bibtex_check_database\<close> database
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    24
  |> YXML.parse_body
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    25
  |> let open XML.Decode in pair (list (pair string properties)) (list (pair string properties)) end
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    26
  |> (apply2 o map o apsnd) (fn pos => Position.of_properties (pos @ Position.get_props pos0));
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    27
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    28
fun check_database_output pos0 database =
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    29
  let val (errors, warnings) = check_database pos0 database in
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    30
    errors |> List.app (fn (msg, pos) =>
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    31
      Output.error_message ("Bibtex error" ^ Position.here pos ^ ":\n  " ^ msg));
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    32
    warnings |> List.app (fn (msg, pos) =>
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    33
      warning ("Bibtex warning" ^ Position.here pos ^ ":\n  " ^ msg))
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    34
  end;
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    35
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    36
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    37
(* document antiquotations *)
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    38
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 65032
diff changeset
    39
val cite_macro = Attrib.setup_config_string \<^binding>\<open>cite_macro\<close> (K "cite");
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    40
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    41
val _ =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    42
  Theory.setup
67386
998e01d6f8fd clarified modules;
wenzelm
parents: 67301
diff changeset
    43
   (Document_Antiquotation.setup_option \<^binding>\<open>cite_macro\<close> (Config.put cite_macro) #>
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67386
diff changeset
    44
    Thy_Output.antiquotation_raw \<^binding>\<open>cite\<close>
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    45
      (Scan.lift
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 67463
diff changeset
    46
        (Scan.option (Parse.verbatim || Parse.cartouche) -- Parse.and_list1 Args.name_position))
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67386
diff changeset
    47
      (fn ctxt => fn (opt, citations) =>
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    48
        let
67297
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    49
          val thy = Proof_Context.theory_of ctxt;
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    50
          val bibtex_entries = Present.get_bibtex_entries thy;
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    51
          val _ =
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    52
            if null bibtex_entries andalso Context.theory_name thy <> Context.PureN then ()
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    53
            else
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    54
              citations |> List.app (fn (name, pos) =>
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    55
                if member (op =) bibtex_entries name then ()
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    56
                else error ("Unknown Bibtex entry " ^ quote name ^ Position.here pos));
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 58544
diff changeset
    57
          val _ =
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 58544
diff changeset
    58
            Context_Position.reports ctxt
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 58544
diff changeset
    59
              (map (fn (name, pos) => (pos, Markup.citation name)) citations);
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    60
          val opt_arg = (case opt of NONE => "" | SOME s => "[" ^ s ^ "]");
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 58544
diff changeset
    61
          val arg = "{" ^ space_implode "," (map #1 citations) ^ "}";
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67386
diff changeset
    62
        in Latex.string ("\\" ^ Config.get ctxt cite_macro ^ opt_arg ^ arg) end));
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    63
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    64
end;