src/Pure/System/isar_document.scala
author wenzelm
Sun Aug 15 14:18:52 2010 +0200 (2010-08-15 ago)
changeset 38417 b8922ae21111
parent 38414 49f1f657adc2
child 38418 9a7af64d71bb
permissions -rw-r--r--
renamed class Document to Document.Version etc.;
renamed Change.prev to Change.previous, and Change.document to Change.current;
tuned;
wenzelm@38412
     1
/*  Title:      Pure/System/isar_document.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@38412
     4
Interactive Isar documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@38412
    10
object Isar_Document
wenzelm@38412
    11
{
wenzelm@38412
    12
  /* protocol messages */
wenzelm@38412
    13
wenzelm@38412
    14
  object Assign {
wenzelm@38414
    15
    def unapply(msg: XML.Tree)
wenzelm@38414
    16
        : Option[(Document.Version_ID, List[(Document.Command_ID, Document.Exec_ID)])] =
wenzelm@38412
    17
      msg match {
wenzelm@38414
    18
        case XML.Elem(Markup(Markup.ASSIGN, List((Markup.VERSION, Document.ID(id)))), edits) =>
wenzelm@38412
    19
          val id_edits = edits.map(Edit.unapply)
wenzelm@38414
    20
          if (id_edits.forall(_.isDefined)) Some((id, id_edits.map(_.get)))
wenzelm@38412
    21
          else None
wenzelm@38412
    22
        case _ => None
wenzelm@38412
    23
      }
wenzelm@38412
    24
  }
wenzelm@38412
    25
wenzelm@38412
    26
  object Edit {
wenzelm@38412
    27
    def unapply(msg: XML.Tree): Option[(Document.Command_ID, Document.Exec_ID)] =
wenzelm@38412
    28
      msg match {
wenzelm@38414
    29
        case XML.Elem(
wenzelm@38414
    30
          Markup(Markup.EDIT,
wenzelm@38414
    31
            List((Markup.ID, Document.ID(i)), (Markup.EXEC, Document.ID(j)))), Nil) => Some((i, j))
wenzelm@38412
    32
        case _ => None
wenzelm@38412
    33
      }
wenzelm@38412
    34
  }
wenzelm@38412
    35
}
wenzelm@38412
    36
wenzelm@38412
    37
wenzelm@38412
    38
trait Isar_Document extends Isabelle_Process
wenzelm@38412
    39
{
wenzelm@38412
    40
  import Isar_Document._
wenzelm@38412
    41
wenzelm@38412
    42
wenzelm@38412
    43
  /* commands */
wenzelm@38412
    44
wenzelm@38412
    45
  def define_command(id: Document.Command_ID, text: String): Unit =
wenzelm@38414
    46
    input("Isar_Document.define_command", Document.ID(id), text)
wenzelm@38412
    47
wenzelm@38412
    48
wenzelm@38417
    49
  /* document versions */
wenzelm@38412
    50
wenzelm@38417
    51
  def edit_version(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@38412
    52
      edits: List[Document.Edit[Document.Command_ID]])
wenzelm@38412
    53
  {
wenzelm@38412
    54
    def make_id1(id1: Option[Document.Command_ID]): XML.Body =
wenzelm@38412
    55
      XML_Data.make_long(id1 getOrElse Document.NO_ID)
wenzelm@38412
    56
wenzelm@38412
    57
    val arg =
wenzelm@38412
    58
      XML_Data.make_list(
wenzelm@38412
    59
        XML_Data.make_pair(XML_Data.make_string)(
wenzelm@38412
    60
          XML_Data.make_option(XML_Data.make_list(
wenzelm@38412
    61
              XML_Data.make_pair(make_id1)(XML_Data.make_option(XML_Data.make_long))))))(edits)
wenzelm@38412
    62
wenzelm@38417
    63
    input("Isar_Document.edit_version",
wenzelm@38414
    64
      Document.ID(old_id), Document.ID(new_id), YXML.string_of_body(arg))
wenzelm@38412
    65
  }
wenzelm@38412
    66
}