merged
authorpaulson
Thu, 11 Apr 2019 22:38:02 +0100
changeset 70132 4ce88d646767
parent 70130 c7866e763e9f (diff)
parent 70131 c6e1a4806f49 (current diff)
child 70133 4f19b92ab6d7
merged
--- a/src/Pure/Thy/thy_output.ML	Thu Apr 11 22:37:49 2019 +0100
+++ b/src/Pure/Thy/thy_output.ML	Thu Apr 11 22:38:02 2019 +0100
@@ -228,8 +228,8 @@
 
 local
 
-fun err_bad_nesting pos =
-  error ("Bad nesting of commands in presentation" ^ pos);
+fun err_bad_nesting here =
+  error ("Bad nesting of commands in presentation" ^ here);
 
 fun edge which f (x: string option, y) =
   if x = y then I
@@ -240,6 +240,22 @@
 fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
 fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
 
+fun document_tag cmd_pos state state' tag_stack =
+  let
+    val ctxt' = Toplevel.presentation_context state';
+    val nesting = Toplevel.level state' - Toplevel.level state;
+
+    val (tag, tags) = tag_stack;
+    val tag' = try hd (fold (update (op =)) (Document_Source.get_tags ctxt') (the_list tag));
+    val tag_stack' =
+      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
+      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
+      else
+        (case drop (~ nesting - 1) tags of
+          tg :: tgs => (tg, tgs)
+        | [] => err_bad_nesting (Position.here cmd_pos));
+  in (tag', tag_stack') end;
+
 fun read_tag s =
   (case space_explode "%" s of
     ["", b] => (SOME b, NONE)
@@ -255,27 +271,22 @@
     val document_tags_default = map_filter #1 document_tags;
     val document_tags_command = map_filter #2 document_tags;
   in
-    fn {cmd_name, cmd_tags, tag, active_tag} => fn state => fn state' =>
+    fn cmd_name => fn state => fn state' => fn active_tag =>
       let
-        val tag' = try hd (fold (update (op =)) cmd_tags (the_list tag));
-
         val keyword_tags =
           if cmd_name = "end" andalso Toplevel.is_end_theory state' then ["theory"]
           else Keyword.command_tags keywords cmd_name;
         val command_tags =
           the_list (AList.lookup (op =) document_tags_command cmd_name) @
           keyword_tags @ document_tags_default;
-
         val active_tag' =
-          if is_some tag' then tag'
-          else
-            (case command_tags of
-              default_tag :: _ => SOME default_tag
-            | [] =>
-                if Keyword.is_vacuous keywords cmd_name andalso Toplevel.is_proof state
-                then active_tag
-                else NONE);
-      in {tag' = tag', active_tag' = active_tag'} end
+          (case command_tags of
+            default_tag :: _ => SOME default_tag
+          | [] =>
+              if Keyword.is_vacuous keywords cmd_name andalso Toplevel.is_proof state
+              then active_tag
+              else NONE);
+      in active_tag' end
   end;
 
 fun present_span command_tag span state state'
@@ -288,27 +299,14 @@
       | _ => I);
 
     val Span ((cmd_name, cmd_pos), srcs, span_newline) = span;
-    val cmd_tags = Document_Source.get_tags ctxt';
 
-    val (tag, tags) = tag_stack;
-    val {tag', active_tag'} =
-      command_tag {cmd_name = cmd_name, cmd_tags = cmd_tags, tag = tag, active_tag = active_tag}
-        state state';
+    val (tag', tag_stack') = document_tag cmd_pos state state' tag_stack;
+    val active_tag' = if is_some tag' then tag' else command_tag cmd_name state state' active_tag;
     val edge = (active_tag, active_tag');
 
-    val nesting = Toplevel.level state' - Toplevel.level state;
-
     val newline' =
       if is_none active_tag' then span_newline else newline;
 
-    val tag_stack' =
-      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
-      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
-      else
-        (case drop (~ nesting - 1) tags of
-          tg :: tgs => (tg, tgs)
-        | [] => err_bad_nesting (Position.here cmd_pos));
-
     val latex' =
       latex
       |> end_tag edge