src/Pure/PIDE/protocol.scala
author wenzelm
Wed Dec 03 14:04:38 2014 +0100 (2014-12-03 ago)
changeset 59083 88b0b1f28adc
parent 58015 2777096e0adf
child 59085 08a6901eb035
permissions -rw-r--r--
tuned signature;
wenzelm@45709
     1
/*  Title:      Pure/PIDE/protocol.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@45709
    10
object Protocol
wenzelm@38412
    11
{
wenzelm@38567
    12
  /* document editing */
wenzelm@38412
    13
wenzelm@52563
    14
  object Assign_Update
wenzelm@44476
    15
  {
wenzelm@52563
    16
    def unapply(text: String): Option[(Document_ID.Version, Document.Assign_Update)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@52527
    20
        Some(pair(long, list(pair(long, list(long))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@51987
    24
        case _: XML.Error => None
wenzelm@38412
    25
      }
wenzelm@38412
    26
  }
wenzelm@38567
    27
wenzelm@44676
    28
  object Removed
wenzelm@44676
    29
  {
wenzelm@52530
    30
    def unapply(text: String): Option[List[Document_ID.Version]] =
wenzelm@44676
    31
      try {
wenzelm@44676
    32
        import XML.Decode._
wenzelm@44676
    33
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    34
      }
wenzelm@44676
    35
      catch {
wenzelm@44676
    36
        case ERROR(_) => None
wenzelm@51987
    37
        case _: XML.Error => None
wenzelm@44676
    38
      }
wenzelm@44676
    39
  }
wenzelm@44676
    40
wenzelm@38567
    41
wenzelm@46209
    42
  /* command status */
wenzelm@38567
    43
wenzelm@56359
    44
  object Status
wenzelm@56359
    45
  {
wenzelm@56359
    46
    def make(markup_iterator: Iterator[Markup]): Status =
wenzelm@56359
    47
    {
wenzelm@56359
    48
      var touched = false
wenzelm@56359
    49
      var accepted = false
wenzelm@56395
    50
      var warned = false
wenzelm@56359
    51
      var failed = false
wenzelm@56359
    52
      var forks = 0
wenzelm@56359
    53
      var runs = 0
wenzelm@56359
    54
      for (markup <- markup_iterator) {
wenzelm@56359
    55
        markup.name match {
wenzelm@56359
    56
          case Markup.ACCEPTED => accepted = true
wenzelm@56359
    57
          case Markup.FORKED => touched = true; forks += 1
wenzelm@56359
    58
          case Markup.JOINED => forks -= 1
wenzelm@56359
    59
          case Markup.RUNNING => touched = true; runs += 1
wenzelm@56359
    60
          case Markup.FINISHED => runs -= 1
wenzelm@56395
    61
          case Markup.WARNING => warned = true
wenzelm@56395
    62
          case Markup.FAILED | Markup.ERROR => failed = true
wenzelm@56359
    63
          case _ =>
wenzelm@56359
    64
        }
wenzelm@56359
    65
      }
wenzelm@56395
    66
      Status(touched, accepted, warned, failed, forks, runs)
wenzelm@56359
    67
    }
wenzelm@56359
    68
wenzelm@56359
    69
    val empty = make(Iterator.empty)
wenzelm@56359
    70
wenzelm@56359
    71
    def merge(status_iterator: Iterator[Status]): Status =
wenzelm@56359
    72
      if (status_iterator.hasNext) {
wenzelm@56359
    73
        val status0 = status_iterator.next
wenzelm@56359
    74
        (status0 /: status_iterator)(_ + _)
wenzelm@56359
    75
      }
wenzelm@56359
    76
      else empty
wenzelm@56359
    77
  }
wenzelm@56359
    78
wenzelm@46166
    79
  sealed case class Status(
wenzelm@56352
    80
    private val touched: Boolean,
wenzelm@56352
    81
    private val accepted: Boolean,
wenzelm@56395
    82
    private val warned: Boolean,
wenzelm@56352
    83
    private val failed: Boolean,
wenzelm@56352
    84
    forks: Int,
wenzelm@56352
    85
    runs: Int)
wenzelm@38567
    86
  {
wenzelm@56359
    87
    def + (that: Status): Status =
wenzelm@56395
    88
      Status(
wenzelm@56395
    89
        touched || that.touched,
wenzelm@56395
    90
        accepted || that.accepted,
wenzelm@56395
    91
        warned || that.warned,
wenzelm@56395
    92
        failed || that.failed,
wenzelm@56395
    93
        forks + that.forks,
wenzelm@56395
    94
        runs + that.runs)
wenzelm@56359
    95
wenzelm@49036
    96
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    97
    def is_running: Boolean = runs != 0
wenzelm@56474
    98
    def is_warned: Boolean = warned
wenzelm@56474
    99
    def is_failed: Boolean = failed
wenzelm@49039
   100
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@46166
   101
  }
wenzelm@46166
   102
wenzelm@56395
   103
  val proper_status_elements =
wenzelm@56743
   104
    Markup.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@55646
   105
      Markup.FINISHED, Markup.FAILED)
wenzelm@55646
   106
wenzelm@56395
   107
  val liberal_status_elements =
wenzelm@56395
   108
    proper_status_elements + Markup.WARNING + Markup.ERROR
wenzelm@55646
   109
wenzelm@46209
   110
wenzelm@51818
   111
  /* command timing */
wenzelm@51818
   112
wenzelm@51818
   113
  object Command_Timing
wenzelm@51818
   114
  {
wenzelm@52531
   115
    def unapply(props: Properties.T): Option[(Document_ID.Generic, isabelle.Timing)] =
wenzelm@51818
   116
      props match {
wenzelm@51818
   117
        case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
wenzelm@51818
   118
          (args, args) match {
wenzelm@51818
   119
            case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
wenzelm@51818
   120
            case _ => None
wenzelm@51818
   121
          }
wenzelm@51818
   122
        case _ => None
wenzelm@51818
   123
      }
wenzelm@51818
   124
  }
wenzelm@51818
   125
wenzelm@51818
   126
wenzelm@46209
   127
  /* node status */
wenzelm@46209
   128
wenzelm@46688
   129
  sealed case class Node_Status(
wenzelm@56474
   130
    unprocessed: Int, running: Int, warned: Int, failed: Int, finished: Int)
wenzelm@44866
   131
  {
wenzelm@56474
   132
    def total: Int = unprocessed + running + warned + failed + finished
wenzelm@44866
   133
  }
wenzelm@44613
   134
wenzelm@44613
   135
  def node_status(
wenzelm@44613
   136
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
   137
  {
wenzelm@44613
   138
    var unprocessed = 0
wenzelm@44613
   139
    var running = 0
wenzelm@46688
   140
    var warned = 0
wenzelm@44613
   141
    var failed = 0
wenzelm@56474
   142
    var finished = 0
wenzelm@56356
   143
    for (command <- node.commands.iterator) {
wenzelm@56355
   144
      val states = state.command_states(version, command)
wenzelm@56359
   145
      val status = Status.merge(states.iterator.map(_.protocol_status))
wenzelm@56355
   146
wenzelm@56299
   147
      if (status.is_running) running += 1
wenzelm@57843
   148
      else if (status.is_failed) failed += 1
wenzelm@56395
   149
      else if (status.is_warned) warned += 1
wenzelm@56395
   150
      else if (status.is_finished) finished += 1
wenzelm@56299
   151
      else unprocessed += 1
wenzelm@56299
   152
    }
wenzelm@56474
   153
    Node_Status(unprocessed, running, warned, failed, finished)
wenzelm@44613
   154
  }
wenzelm@44613
   155
wenzelm@38887
   156
wenzelm@51533
   157
  /* node timing */
wenzelm@51533
   158
wenzelm@51533
   159
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   160
wenzelm@51533
   161
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   162
wenzelm@51533
   163
  def node_timing(
wenzelm@51533
   164
    state: Document.State,
wenzelm@51533
   165
    version: Document.Version,
wenzelm@51533
   166
    node: Document.Node,
wenzelm@51533
   167
    threshold: Double): Node_Timing =
wenzelm@51533
   168
  {
wenzelm@51533
   169
    var total = 0.0
wenzelm@51533
   170
    var commands = Map.empty[Command, Double]
wenzelm@51533
   171
    for {
wenzelm@51533
   172
      command <- node.commands.iterator
wenzelm@56299
   173
      st <- state.command_states(version, command)
wenzelm@56356
   174
    } {
wenzelm@56356
   175
      val command_timing =
wenzelm@51533
   176
        (0.0 /: st.status)({
wenzelm@51533
   177
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   178
          case (timing, _) => timing
wenzelm@51533
   179
        })
wenzelm@51533
   180
      total += command_timing
wenzelm@51533
   181
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   182
    }
wenzelm@51533
   183
    Node_Timing(total, commands)
wenzelm@51533
   184
  }
wenzelm@51533
   185
wenzelm@51533
   186
wenzelm@39441
   187
  /* result messages */
wenzelm@39439
   188
wenzelm@55820
   189
  private val clean_elements =
wenzelm@56743
   190
    Markup.Elements(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   191
wenzelm@39439
   192
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   193
    body filter {
wenzelm@55646
   194
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean_elements(name)
wenzelm@55646
   195
      case XML.Elem(Markup(name, _), _) => !clean_elements(name)
wenzelm@49445
   196
      case _ => true
wenzelm@49650
   197
    } map {
wenzelm@49650
   198
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   199
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   200
      case t => t
wenzelm@49650
   201
    }
wenzelm@39439
   202
wenzelm@49445
   203
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   204
    body flatMap {
wenzelm@50450
   205
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   206
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   207
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   208
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   209
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   210
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   211
      case XML.Text(_) => Nil
wenzelm@39441
   212
    }
wenzelm@39441
   213
wenzelm@39439
   214
wenzelm@39511
   215
  /* specific messages */
wenzelm@39511
   216
wenzelm@50500
   217
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   218
    msg match {
wenzelm@50500
   219
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   220
      case _ => false
wenzelm@50500
   221
    }
wenzelm@50500
   222
wenzelm@50157
   223
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   224
    msg match {
wenzelm@50201
   225
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   226
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   227
      case _ => false
wenzelm@39622
   228
    }
wenzelm@39622
   229
wenzelm@52650
   230
  def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
wenzelm@50500
   231
    msg match {
wenzelm@50500
   232
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@52650
   233
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   234
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@52650
   235
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   236
      case _ => false
wenzelm@50500
   237
    }
wenzelm@50500
   238
wenzelm@56495
   239
  def is_warning_markup(msg: XML.Tree, name: String): Boolean =
wenzelm@56495
   240
    msg match {
wenzelm@56495
   241
      case XML.Elem(Markup(Markup.WARNING, _),
wenzelm@56495
   242
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@56495
   243
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _),
wenzelm@56495
   244
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@56495
   245
      case _ => false
wenzelm@56495
   246
    }
wenzelm@52650
   247
wenzelm@39511
   248
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   249
    msg match {
wenzelm@50201
   250
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   251
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   252
      case _ => false
wenzelm@39511
   253
    }
wenzelm@39511
   254
wenzelm@39511
   255
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   256
    msg match {
wenzelm@50201
   257
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   258
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   259
      case _ => false
wenzelm@39511
   260
    }
wenzelm@38887
   261
wenzelm@56495
   262
  def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
wenzelm@56495
   263
  def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
wenzelm@56495
   264
  def is_legacy(msg: XML.Tree): Boolean = is_warning_markup(msg, Markup.LEGACY)
wenzelm@56495
   265
wenzelm@56495
   266
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@56495
   267
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@56495
   268
wenzelm@50500
   269
wenzelm@50500
   270
  /* dialogs */
wenzelm@50500
   271
wenzelm@50500
   272
  object Dialog_Args
wenzelm@50500
   273
  {
wenzelm@52531
   274
    def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   275
      (props, props, props) match {
wenzelm@50500
   276
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   277
          Some((id, serial, result))
wenzelm@50500
   278
        case _ => None
wenzelm@50500
   279
      }
wenzelm@50500
   280
  }
wenzelm@50500
   281
wenzelm@50500
   282
  object Dialog
wenzelm@50500
   283
  {
wenzelm@52531
   284
    def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   285
      tree match {
wenzelm@50500
   286
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   287
          Some((id, serial, result))
wenzelm@50500
   288
        case _ => None
wenzelm@50500
   289
      }
wenzelm@50500
   290
  }
wenzelm@50500
   291
wenzelm@50500
   292
  object Dialog_Result
wenzelm@50500
   293
  {
wenzelm@52531
   294
    def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
wenzelm@50501
   295
    {
wenzelm@50501
   296
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   297
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   298
    }
wenzelm@50500
   299
wenzelm@50501
   300
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   301
      tree match {
wenzelm@50501
   302
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   303
        case _ => None
wenzelm@50500
   304
      }
wenzelm@50500
   305
  }
wenzelm@39170
   306
wenzelm@39511
   307
wenzelm@39511
   308
  /* reported positions */
wenzelm@39511
   309
wenzelm@55646
   310
  private val position_elements =
wenzelm@56743
   311
    Markup.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   312
wenzelm@55433
   313
  def message_positions(
wenzelm@56470
   314
    self_id: Document_ID.Generic => Boolean,
wenzelm@57911
   315
    command_position: Position.T,
wenzelm@56746
   316
    chunk_name: Symbol.Text_Chunk.Name,
wenzelm@56746
   317
    chunk: Symbol.Text_Chunk,
wenzelm@55433
   318
    message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   319
  {
wenzelm@55433
   320
    def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
wenzelm@55433
   321
      props match {
wenzelm@57911
   322
        case Position.Identified(id, name) if self_id(id) && name == chunk_name =>
wenzelm@58015
   323
          val opt_range =
wenzelm@58015
   324
            Position.Range.unapply(props) orElse {
wenzelm@58015
   325
              if (name == Symbol.Text_Chunk.Default)
wenzelm@58015
   326
                Position.Range.unapply(command_position)
wenzelm@58015
   327
              else None
wenzelm@58015
   328
            }
wenzelm@58015
   329
          opt_range match {
wenzelm@57911
   330
            case Some(symbol_range) =>
wenzelm@57911
   331
              chunk.incorporate(symbol_range) match {
wenzelm@57911
   332
                case Some(range) => set + range
wenzelm@57911
   333
                case _ => set
wenzelm@57911
   334
              }
wenzelm@57911
   335
            case None => set
wenzelm@55550
   336
          }
wenzelm@55433
   337
        case _ => set
wenzelm@55433
   338
      }
wenzelm@50157
   339
wenzelm@39441
   340
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   341
      tree match {
wenzelm@55433
   342
        case XML.Wrapped_Elem(Markup(name, props), _, body) =>
wenzelm@55646
   343
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   344
        case XML.Elem(Markup(name, props), body) =>
wenzelm@55646
   345
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   346
        case XML.Text(_) => set
wenzelm@38887
   347
      }
wenzelm@50157
   348
wenzelm@39441
   349
    val set = positions(Set.empty, message)
wenzelm@55433
   350
    if (set.isEmpty) elem_positions(message.markup.properties, set)
wenzelm@39042
   351
    else set
wenzelm@38887
   352
  }
wenzelm@38412
   353
}
wenzelm@38412
   354
wenzelm@38412
   355
wenzelm@57916
   356
trait Protocol
wenzelm@38412
   357
{
wenzelm@57916
   358
  /* text */
wenzelm@57916
   359
wenzelm@57916
   360
  def encode(s: String): String
wenzelm@57916
   361
  def decode(s: String): String
wenzelm@57916
   362
wenzelm@57916
   363
  object Encode
wenzelm@57916
   364
  {
wenzelm@57916
   365
    val string: XML.Encode.T[String] = (s => XML.Encode.string(encode(s)))
wenzelm@57916
   366
  }
wenzelm@57916
   367
wenzelm@57916
   368
wenzelm@57916
   369
  /* protocol commands */
wenzelm@57916
   370
wenzelm@57916
   371
  def protocol_command_bytes(name: String, args: Bytes*): Unit
wenzelm@57916
   372
  def protocol_command(name: String, args: String*): Unit
wenzelm@57916
   373
wenzelm@57916
   374
wenzelm@56387
   375
  /* options */
wenzelm@56387
   376
wenzelm@56387
   377
  def options(opts: Options): Unit =
wenzelm@56387
   378
    protocol_command("Prover.options", YXML.string_of_body(opts.encode))
wenzelm@56387
   379
wenzelm@56387
   380
wenzelm@56387
   381
  /* interned items */
wenzelm@54519
   382
wenzelm@56335
   383
  def define_blob(digest: SHA1.Digest, bytes: Bytes): Unit =
wenzelm@56387
   384
    protocol_command_bytes("Document.define_blob", Bytes(digest.toString), bytes)
wenzelm@38412
   385
wenzelm@44644
   386
  def define_command(command: Command): Unit =
wenzelm@54519
   387
  {
wenzelm@54519
   388
    val blobs_yxml =
wenzelm@54519
   389
    { import XML.Encode._
wenzelm@54519
   390
      val encode_blob: T[Command.Blob] =
wenzelm@54519
   391
        variant(List(
wenzelm@54526
   392
          { case Exn.Res((a, b)) =>
wenzelm@56458
   393
              (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
wenzelm@54526
   394
          { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
wenzelm@54519
   395
      YXML.string_of_body(list(encode_blob)(command.blobs))
wenzelm@54519
   396
    }
wenzelm@52582
   397
    protocol_command("Document.define_command",
wenzelm@54519
   398
      Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
wenzelm@54519
   399
  }
wenzelm@38412
   400
wenzelm@38412
   401
wenzelm@52931
   402
  /* execution */
wenzelm@52931
   403
wenzelm@52931
   404
  def discontinue_execution(): Unit =
wenzelm@52931
   405
    protocol_command("Document.discontinue_execution")
wenzelm@38412
   406
wenzelm@52931
   407
  def cancel_exec(id: Document_ID.Exec): Unit =
wenzelm@52931
   408
    protocol_command("Document.cancel_exec", Document_ID(id))
wenzelm@52931
   409
wenzelm@52931
   410
wenzelm@52931
   411
  /* document versions */
wenzelm@47343
   412
wenzelm@52530
   413
  def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
wenzelm@44383
   414
    edits: List[Document.Edit_Command])
wenzelm@38412
   415
  {
wenzelm@44157
   416
    val edits_yxml =
wenzelm@43767
   417
    { import XML.Encode._
wenzelm@44383
   418
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   419
      def encode_edit(name: Document.Node.Name)
wenzelm@52849
   420
          : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
wenzelm@44979
   421
        variant(List(
wenzelm@44979
   422
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   423
          { case Document.Node.Deps(header) =>
wenzelm@56449
   424
              val master_dir = Isabelle_System.posix_path_url(name.master_dir)
wenzelm@56801
   425
              val theory = Long_Name.base_name(name.theory)
wenzelm@48707
   426
              val imports = header.imports.map(_.node)
wenzelm@50128
   427
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   428
              (Nil,
wenzelm@48707
   429
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   430
                  pair(list(pair(Encode.string,
wenzelm@48864
   431
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   432
                  list(Encode.string)))))(
wenzelm@56801
   433
                (master_dir, (theory, (imports, (keywords, header.errors)))))) },
wenzelm@52849
   434
          { case Document.Node.Perspective(a, b, c) =>
wenzelm@52849
   435
              (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
wenzelm@52862
   436
                list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
wenzelm@48705
   437
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   438
      {
wenzelm@44979
   439
        val (name, edit) = node_edit
wenzelm@46737
   440
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   441
      })
wenzelm@48705
   442
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@52582
   443
    protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
wenzelm@38412
   444
  }
wenzelm@43748
   445
wenzelm@44673
   446
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   447
  {
wenzelm@44673
   448
    val versions_yxml =
wenzelm@44673
   449
      { import XML.Encode._
wenzelm@44673
   450
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@52582
   451
    protocol_command("Document.remove_versions", versions_yxml)
wenzelm@44673
   452
  }
wenzelm@44673
   453
wenzelm@43748
   454
wenzelm@50498
   455
  /* dialog via document content */
wenzelm@50498
   456
wenzelm@52931
   457
  def dialog_result(serial: Long, result: String): Unit =
wenzelm@52582
   458
    protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@56616
   459
wenzelm@56616
   460
wenzelm@56616
   461
  /* use_theories */
wenzelm@56616
   462
wenzelm@56616
   463
  def use_theories(id: String, master_dir: Path, thys: List[Path]): Unit =
wenzelm@56616
   464
    protocol_command("use_theories", (id :: master_dir.implode :: thys.map(_.implode)): _*)
wenzelm@38412
   465
}