src/Pure/Isar/isar_document.scala
author wenzelm
Wed Aug 11 22:41:26 2010 +0200 (2010-08-11)
changeset 38355 8cb265fb12fe
parent 38271 36187e8443dd
child 38363 af7f41a8a0a8
permissions -rw-r--r--
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm@29553
     1
/*  Title:      Pure/Isar/isar_document.scala
wenzelm@29553
     2
    Author:     Makarius
wenzelm@29553
     3
wenzelm@29553
     4
Interactive Isar documents.
wenzelm@29553
     5
*/
wenzelm@29553
     6
wenzelm@29553
     7
package isabelle
wenzelm@29553
     8
wenzelm@32450
     9
wenzelm@32467
    10
object Isar_Document
wenzelm@32450
    11
{
wenzelm@38236
    12
  /* protocol messages */
wenzelm@36682
    13
wenzelm@36682
    14
  object Assign {
wenzelm@36682
    15
    def unapply(msg: XML.Tree): Option[List[XML.Tree]] =
wenzelm@36682
    16
      msg match {
wenzelm@38231
    17
        case XML.Elem(Markup.Assign, edits) => Some(edits)
wenzelm@36682
    18
        case _ => None
wenzelm@36682
    19
      }
wenzelm@36682
    20
  }
wenzelm@36682
    21
wenzelm@36682
    22
  object Edit {
wenzelm@38150
    23
    def unapply(msg: XML.Tree): Option[(Document.Command_ID, Document.State_ID)] =
wenzelm@36682
    24
      msg match {
wenzelm@38230
    25
        case XML.Elem(Markup(Markup.EDIT, List((Markup.ID, cmd_id), (Markup.STATE, state_id))), Nil) =>
wenzelm@38355
    26
          (Markup.parse_long(cmd_id), Markup.parse_long(state_id)) match {
wenzelm@38355
    27
            case (Some(i), Some(j)) => Some((i, j))
wenzelm@38355
    28
            case _ => None
wenzelm@38355
    29
          }
wenzelm@36682
    30
        case _ => None
wenzelm@36682
    31
      }
wenzelm@36682
    32
  }
wenzelm@29645
    33
}
wenzelm@29645
    34
wenzelm@32450
    35
wenzelm@32467
    36
trait Isar_Document extends Isabelle_Process
wenzelm@29645
    37
{
wenzelm@32467
    38
  import Isar_Document._
wenzelm@29553
    39
wenzelm@29553
    40
wenzelm@29553
    41
  /* commands */
wenzelm@29553
    42
wenzelm@38271
    43
  def define_command(id: Document.Command_ID, text: String): Unit =
wenzelm@38355
    44
    input("Isar_Document.define_command", Document.print_id(id), text)
wenzelm@29553
    45
wenzelm@29553
    46
wenzelm@29553
    47
  /* documents */
wenzelm@29553
    48
wenzelm@38150
    49
  def edit_document(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@38150
    50
      edits: List[Document.Edit[Document.Command_ID]])
wenzelm@29553
    51
  {
wenzelm@38271
    52
    def make_id1(id1: Option[Document.Command_ID]): XML.Body =
wenzelm@38355
    53
      XML_Data.make_long(id1 getOrElse Document.NO_ID)
wenzelm@38355
    54
wenzelm@38271
    55
    val arg =
wenzelm@38271
    56
      XML_Data.make_list(
wenzelm@38271
    57
        XML_Data.make_pair(XML_Data.make_string)(
wenzelm@38271
    58
          XML_Data.make_option(XML_Data.make_list(
wenzelm@38355
    59
              XML_Data.make_pair(make_id1)(XML_Data.make_option(XML_Data.make_long))))))(edits)
wenzelm@29553
    60
wenzelm@38355
    61
    input("Isar_Document.edit_document",
wenzelm@38355
    62
      Document.print_id(old_id), Document.print_id(new_id), YXML.string_of_body(arg))
wenzelm@29553
    63
  }
wenzelm@29553
    64
}