src/Pure/System/isar_document.scala
author wenzelm
Sat Aug 14 18:43:45 2010 +0200 (2010-08-14 ago)
changeset 38412 c23f3abbf42d
child 38414 49f1f657adc2
permissions -rw-r--r--
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
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@38412
    15
    def unapply(msg: XML.Tree): Option[List[(Document.Command_ID, Document.Exec_ID)]] =
wenzelm@38412
    16
      msg match {
wenzelm@38412
    17
        case XML.Elem(Markup.Assign, edits) =>
wenzelm@38412
    18
          val id_edits = edits.map(Edit.unapply)
wenzelm@38412
    19
          if (id_edits.forall(_.isDefined)) Some(id_edits.map(_.get))
wenzelm@38412
    20
          else None
wenzelm@38412
    21
        case _ => None
wenzelm@38412
    22
      }
wenzelm@38412
    23
  }
wenzelm@38412
    24
wenzelm@38412
    25
  object Edit {
wenzelm@38412
    26
    def unapply(msg: XML.Tree): Option[(Document.Command_ID, Document.Exec_ID)] =
wenzelm@38412
    27
      msg match {
wenzelm@38412
    28
        case XML.Elem(Markup(Markup.EDIT, List((Markup.ID, cmd_id), (Markup.STATE, state_id))), Nil) =>
wenzelm@38412
    29
          (Markup.parse_long(cmd_id), Markup.parse_long(state_id)) match {
wenzelm@38412
    30
            case (Some(i), Some(j)) => Some((i, j))
wenzelm@38412
    31
            case _ => None
wenzelm@38412
    32
          }
wenzelm@38412
    33
        case _ => None
wenzelm@38412
    34
      }
wenzelm@38412
    35
  }
wenzelm@38412
    36
}
wenzelm@38412
    37
wenzelm@38412
    38
wenzelm@38412
    39
trait Isar_Document extends Isabelle_Process
wenzelm@38412
    40
{
wenzelm@38412
    41
  import Isar_Document._
wenzelm@38412
    42
wenzelm@38412
    43
wenzelm@38412
    44
  /* commands */
wenzelm@38412
    45
wenzelm@38412
    46
  def define_command(id: Document.Command_ID, text: String): Unit =
wenzelm@38412
    47
    input("Isar_Document.define_command", Document.print_id(id), text)
wenzelm@38412
    48
wenzelm@38412
    49
wenzelm@38412
    50
  /* documents */
wenzelm@38412
    51
wenzelm@38412
    52
  def edit_document(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@38412
    53
      edits: List[Document.Edit[Document.Command_ID]])
wenzelm@38412
    54
  {
wenzelm@38412
    55
    def make_id1(id1: Option[Document.Command_ID]): XML.Body =
wenzelm@38412
    56
      XML_Data.make_long(id1 getOrElse Document.NO_ID)
wenzelm@38412
    57
wenzelm@38412
    58
    val arg =
wenzelm@38412
    59
      XML_Data.make_list(
wenzelm@38412
    60
        XML_Data.make_pair(XML_Data.make_string)(
wenzelm@38412
    61
          XML_Data.make_option(XML_Data.make_list(
wenzelm@38412
    62
              XML_Data.make_pair(make_id1)(XML_Data.make_option(XML_Data.make_long))))))(edits)
wenzelm@38412
    63
wenzelm@38412
    64
    input("Isar_Document.edit_document",
wenzelm@38412
    65
      Document.print_id(old_id), Document.print_id(new_id), YXML.string_of_body(arg))
wenzelm@38412
    66
  }
wenzelm@38412
    67
}