src/Pure/Thy/latex.ML
author wenzelm
Wed May 24 19:09:36 2000 +0200 (2000-05-24)
changeset 8965 d46b36785c70
parent 8896 c80aba8c1d5e
child 9038 63d20536971f
permissions -rw-r--r--
proper token_translation for latex mode;
wenzelm@7726
     1
(*  Title:      Pure/Thy/latex.ML
wenzelm@7726
     2
    ID:         $Id$
wenzelm@7726
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8808
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@7726
     5
wenzelm@8460
     6
LaTeX presentation primitives (based on outer lexical syntax).
wenzelm@7726
     7
*)
wenzelm@7726
     8
wenzelm@7726
     9
signature LATEX =
wenzelm@7726
    10
sig
wenzelm@8660
    11
  datatype token = Basic of OuterLex.token | Markup of string * string |
wenzelm@8660
    12
    MarkupEnv of string * string | Verbatim of string
wenzelm@8499
    13
  val old_symbol_source: string -> Symbol.symbol list -> string
wenzelm@7756
    14
  val token_source: token list -> string
wenzelm@7726
    15
  val theory_entry: string -> string
wenzelm@8965
    16
  val setup: (theory -> theory) list
wenzelm@7726
    17
end;
wenzelm@7726
    18
wenzelm@7726
    19
structure Latex: LATEX =
wenzelm@7726
    20
struct
wenzelm@7726
    21
wenzelm@7726
    22
wenzelm@7726
    23
(* symbol output *)
wenzelm@7726
    24
wenzelm@7900
    25
local
wenzelm@7900
    26
wenzelm@7726
    27
val output_chr = fn
wenzelm@7726
    28
  " " => "~" |
wenzelm@7726
    29
  "\n" => "\\isanewline\n" |
wenzelm@7726
    30
  "$" => "\\$" |
wenzelm@7726
    31
  "&" => "\\&" |
wenzelm@7726
    32
  "%" => "\\%" |
wenzelm@7726
    33
  "#" => "\\#" |
wenzelm@7726
    34
  "_" => "\\_" |
wenzelm@7800
    35
  "{" => "{\\isabraceleft}" |
wenzelm@7800
    36
  "}" => "{\\isabraceright}" |
wenzelm@7800
    37
  "~" => "{\\isatilde}" |
wenzelm@7800
    38
  "^" => "{\\isacircum}" |
wenzelm@7752
    39
  "\"" => "{\"}" |
wenzelm@7800
    40
  "\\" => "{\\isabackslash}" |
wenzelm@7726
    41
  c => c;
wenzelm@7726
    42
wenzelm@7900
    43
fun output_sym s =
wenzelm@7900
    44
  if size s = 1 then output_chr s
wenzelm@7900
    45
  else
wenzelm@7900
    46
    (case explode s of
wenzelm@7900
    47
      cs as "\\" :: "<" :: "^" :: _ => implode (map output_chr cs)
wenzelm@7973
    48
    | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
wenzelm@7900
    49
    | _ => sys_error "output_sym");
wenzelm@7900
    50
wenzelm@7900
    51
in
wenzelm@7900
    52
wenzelm@8192
    53
val output_symbols = implode o map output_sym;
wenzelm@8192
    54
val output_syms = output_symbols o Symbol.explode;
wenzelm@7900
    55
wenzelm@7900
    56
end;
wenzelm@7726
    57
wenzelm@7726
    58
wenzelm@7726
    59
(* token output *)
wenzelm@7726
    60
wenzelm@7726
    61
structure T = OuterLex;
wenzelm@7726
    62
wenzelm@8660
    63
datatype token =
wenzelm@8660
    64
  Basic of T.token |
wenzelm@8660
    65
  Markup of string * string |
wenzelm@8660
    66
  MarkupEnv of string * string |
wenzelm@8660
    67
  Verbatim of string;
wenzelm@7756
    68
wenzelm@7756
    69
wenzelm@7756
    70
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
wenzelm@7756
    71
wenzelm@7756
    72
fun strip_blanks s =
wenzelm@7756
    73
  implode (#1 (Library.take_suffix Symbol.is_blank
wenzelm@7756
    74
    (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
wenzelm@7756
    75
wenzelm@7756
    76
fun output_tok (Basic tok) =
wenzelm@7752
    77
      let val s = T.val_of tok in
wenzelm@7756
    78
        if invisible_token tok then ""
wenzelm@7800
    79
        else if T.is_kind T.Command tok then
wenzelm@8896
    80
          if s = "{" then "{\\isabeginblock}"
wenzelm@8896
    81
          else if s = "}" then "{\\isaendblock}"
wenzelm@7900
    82
          else "\\isacommand{" ^ output_syms s ^ "}"
wenzelm@7800
    83
        else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
wenzelm@7900
    84
          "\\isakeyword{" ^ output_syms s ^ "}"
wenzelm@7900
    85
        else if T.is_kind T.String tok then output_syms (quote s)
wenzelm@7900
    86
        else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
wenzelm@7900
    87
        else output_syms s
wenzelm@7756
    88
      end
wenzelm@7852
    89
  | output_tok (Markup (cmd, txt)) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "}\n"
wenzelm@8660
    90
  | output_tok (MarkupEnv (cmd, txt)) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
wenzelm@8660
    91
      strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
wenzelm@7852
    92
  | output_tok (Verbatim txt) = "%\n" ^ strip_blanks txt ^ "\n";
wenzelm@7745
    93
wenzelm@7756
    94
wenzelm@7756
    95
val output_tokens = implode o map output_tok;
wenzelm@7726
    96
wenzelm@7726
    97
wenzelm@7726
    98
(* theory presentation *)
wenzelm@7726
    99
wenzelm@8460
   100
val isabelle_env = enclose "\\begin{isabelle}%\n" "\\end{isabelle}%\n";
wenzelm@8192
   101
wenzelm@8499
   102
fun old_symbol_source name syms =
wenzelm@8499
   103
  isabelle_env ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
wenzelm@8499
   104
wenzelm@8460
   105
val token_source = isabelle_env o output_tokens;
wenzelm@7726
   106
wenzelm@7726
   107
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n";
wenzelm@7726
   108
wenzelm@7726
   109
wenzelm@8460
   110
(* print mode *)
wenzelm@8460
   111
wenzelm@8965
   112
val latexN = "latex";
wenzelm@8965
   113
wenzelm@8460
   114
fun latex_output str =
wenzelm@8460
   115
  let val syms = Symbol.explode str
wenzelm@8460
   116
  in (output_symbols syms, real (Symbol.length syms)) end;
wenzelm@8460
   117
wenzelm@8965
   118
val token_translation = map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
wenzelm@8965
   119
wenzelm@8965
   120
val _ = Symbol.add_mode latexN latex_output;
wenzelm@8965
   121
val setup = [Theory.add_tokentrfuns token_translation];
wenzelm@8460
   122
wenzelm@8460
   123
wenzelm@7726
   124
end;