src/Pure/System/session.scala
author wenzelm
Thu Aug 02 12:36:54 2012 +0200 (2012-08-02)
changeset 48646 91281e9472d8
parent 48422 9613780a805b
child 48707 ba531af91148
permissions -rw-r--r--
more official command specifications, including source position;
wenzelm@36676
     1
/*  Title:      Pure/System/session.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@38221
     3
    Options:    :folding=explicit:collapseFolds=1:
wenzelm@36676
     4
wenzelm@43651
     5
Main Isabelle/Scala session, potentially with running prover process.
wenzelm@36676
     6
*/
wenzelm@34777
     7
wenzelm@34871
     8
package isabelle
wenzelm@34777
     9
wenzelm@44733
    10
wenzelm@43520
    11
import java.lang.System
wenzelm@44733
    12
import java.util.{Timer, TimerTask}
wenzelm@34777
    13
wenzelm@44733
    14
import scala.collection.mutable
wenzelm@46771
    15
import scala.collection.immutable.Queue
wenzelm@34820
    16
import scala.actors.TIMEOUT
wenzelm@34777
    17
import scala.actors.Actor._
wenzelm@34777
    18
wenzelm@34815
    19
wenzelm@34791
    20
object Session
wenzelm@34791
    21
{
wenzelm@34813
    22
  /* events */
wenzelm@34813
    23
wenzelm@43716
    24
  //{{{
wenzelm@34791
    25
  case object Global_Settings
wenzelm@44805
    26
  case object Caret_Focus
wenzelm@47027
    27
  case class Commands_Changed(
wenzelm@47027
    28
    assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command])
wenzelm@39630
    29
wenzelm@39630
    30
  sealed abstract class Phase
wenzelm@39630
    31
  case object Inactive extends Phase
wenzelm@39701
    32
  case object Startup extends Phase  // transient
wenzelm@39701
    33
  case object Failed extends Phase
wenzelm@39630
    34
  case object Ready extends Phase
wenzelm@39701
    35
  case object Shutdown extends Phase  // transient
wenzelm@43716
    36
  //}}}
wenzelm@34791
    37
}
wenzelm@34791
    38
wenzelm@34777
    39
wenzelm@48422
    40
class Session(thy_load: Thy_Load = new Thy_Load())
wenzelm@34777
    41
{
wenzelm@47653
    42
  /* global flags */
wenzelm@47653
    43
wenzelm@47653
    44
  @volatile var timing: Boolean = false
wenzelm@47653
    45
  @volatile var verbose: Boolean = false
wenzelm@47653
    46
wenzelm@47653
    47
wenzelm@46771
    48
  /* tuning parameters */  // FIXME properties or settings (!?)
wenzelm@37849
    49
wenzelm@44733
    50
  val message_delay = Time.seconds(0.01)  // prover messages
wenzelm@43716
    51
  val input_delay = Time.seconds(0.3)  // user input (e.g. text edits, cursor movement)
wenzelm@43716
    52
  val output_delay = Time.seconds(0.1)  // prover output (markup, common messages)
wenzelm@43716
    53
  val update_delay = Time.seconds(0.5)  // GUI layout updates
wenzelm@44573
    54
  val load_delay = Time.seconds(0.5)  // file load operations (new buffers etc.)
wenzelm@44676
    55
  val prune_delay = Time.seconds(60.0)  // prune history -- delete old versions
wenzelm@44676
    56
  val prune_size = 0  // size of retained history
wenzelm@46771
    57
  val syslog_limit = 100
wenzelm@37849
    58
wenzelm@37849
    59
wenzelm@34809
    60
  /* pervasive event buses */
wenzelm@34809
    61
wenzelm@34809
    62
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@44805
    63
  val caret_focus = new Event_Bus[Session.Caret_Focus.type]
wenzelm@43716
    64
  val commands_changed = new Event_Bus[Session.Commands_Changed]
wenzelm@43716
    65
  val phase_changed = new Event_Bus[Session.Phase]
wenzelm@46772
    66
  val syslog_messages = new Event_Bus[Isabelle_Process.Output]
wenzelm@46772
    67
  val raw_output_messages = new Event_Bus[Isabelle_Process.Output]
wenzelm@46774
    68
  val all_messages = new Event_Bus[Isabelle_Process.Message]  // potential bottle-neck
wenzelm@43716
    69
wenzelm@34809
    70
wenzelm@34809
    71
wenzelm@38841
    72
  /** buffered command changes (delay_first discipline) **/
wenzelm@38841
    73
wenzelm@43716
    74
  //{{{
wenzelm@38841
    75
  private case object Stop
wenzelm@38841
    76
wenzelm@44722
    77
  private val (_, commands_changed_buffer) =
wenzelm@44722
    78
    Simple_Thread.actor("commands_changed_buffer", daemon = true)
wenzelm@38841
    79
  {
wenzelm@38841
    80
    var finished = false
wenzelm@38841
    81
    while (!finished) {
wenzelm@44722
    82
      receive {
wenzelm@39630
    83
        case Stop => finished = true; reply(())
wenzelm@44722
    84
        case changed: Session.Commands_Changed => commands_changed.event(changed)
wenzelm@44722
    85
        case bad => System.err.println("commands_changed_buffer: ignoring bad message " + bad)
wenzelm@38841
    86
      }
wenzelm@38841
    87
    }
wenzelm@38841
    88
  }
wenzelm@38841
    89
  //}}}
wenzelm@38841
    90
wenzelm@38841
    91
wenzelm@45635
    92
  /** pipelined change parsing **/
wenzelm@45635
    93
wenzelm@45635
    94
  //{{{
wenzelm@45635
    95
  private case class Text_Edits(
wenzelm@45635
    96
    previous: Future[Document.Version],
wenzelm@45635
    97
    text_edits: List[Document.Edit_Text],
wenzelm@45635
    98
    version_result: Promise[Document.Version])
wenzelm@45635
    99
wenzelm@45635
   100
  private val (_, change_parser) = Simple_Thread.actor("change_parser", daemon = true)
wenzelm@45635
   101
  {
wenzelm@45635
   102
    var finished = false
wenzelm@45635
   103
    while (!finished) {
wenzelm@45635
   104
      receive {
wenzelm@45635
   105
        case Stop => finished = true; reply(())
wenzelm@45635
   106
wenzelm@47629
   107
        case Text_Edits(previous, text_edits, version_result) =>
wenzelm@45635
   108
          val prev = previous.get_finished
wenzelm@47653
   109
          val (doc_edits, version) =
wenzelm@47653
   110
            Timing.timeit("Thy_Syntax.text_edits", timing) {
wenzelm@47653
   111
              Thy_Syntax.text_edits(prover_syntax, prev, text_edits)
wenzelm@47653
   112
            }
wenzelm@45635
   113
          version_result.fulfill(version)
wenzelm@47629
   114
          sender ! Change(doc_edits, prev, version)
wenzelm@45635
   115
wenzelm@45635
   116
        case bad => System.err.println("change_parser: ignoring bad message " + bad)
wenzelm@45635
   117
      }
wenzelm@45635
   118
    }
wenzelm@45635
   119
  }
wenzelm@45635
   120
  //}}}
wenzelm@45635
   121
wenzelm@38841
   122
wenzelm@47653
   123
wenzelm@38841
   124
  /** main protocol actor **/
wenzelm@34809
   125
wenzelm@43651
   126
  /* global state */
wenzelm@43644
   127
wenzelm@46941
   128
  @volatile private var prover_syntax =
wenzelm@46941
   129
    Outer_Syntax.init() +
wenzelm@46941
   130
      // FIXME avoid hardwired stuff!?
wenzelm@46941
   131
      ("hence", Keyword.PRF_ASM_GOAL, "then have") +
wenzelm@46941
   132
      ("thus", Keyword.PRF_ASM_GOAL, "then show")
wenzelm@34809
   133
wenzelm@46771
   134
  private val syslog = Volatile(Queue.empty[XML.Elem])
wenzelm@46771
   135
  def current_syslog(): String = cat_lines(syslog().iterator.map(msg => XML.content(msg).mkString))
wenzelm@39626
   136
wenzelm@39630
   137
  @volatile private var _phase: Session.Phase = Session.Inactive
wenzelm@39633
   138
  private def phase_=(new_phase: Session.Phase)
wenzelm@39630
   139
  {
wenzelm@39630
   140
    _phase = new_phase
wenzelm@39701
   141
    phase_changed.event(new_phase)
wenzelm@39630
   142
  }
wenzelm@43716
   143
  def phase = _phase
wenzelm@43553
   144
  def is_ready: Boolean = phase == Session.Ready
wenzelm@39630
   145
wenzelm@45248
   146
  private val global_state = Volatile(Document.State.init)
wenzelm@43719
   147
  def current_state(): Document.State = global_state()
wenzelm@46944
   148
wenzelm@46944
   149
  def recent_syntax(): Outer_Syntax =
wenzelm@46944
   150
  {
wenzelm@46944
   151
    val version = current_state().recent_finished.version.get_finished
wenzelm@46944
   152
    if (version.is_init) prover_syntax
wenzelm@46944
   153
    else version.syntax
wenzelm@46944
   154
  }
wenzelm@34816
   155
wenzelm@44960
   156
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@44960
   157
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@43719
   158
    global_state().snapshot(name, pending_edits)
wenzelm@43716
   159
wenzelm@43651
   160
wenzelm@43651
   161
  /* theory files */
wenzelm@43651
   162
wenzelm@44615
   163
  def header_edit(name: Document.Node.Name, header: Document.Node_Header): Document.Edit_Text =
wenzelm@44442
   164
  {
wenzelm@44442
   165
    val header1: Document.Node_Header =
wenzelm@44442
   166
      header match {
wenzelm@46737
   167
        case Exn.Res(_) if (thy_load.is_loaded(name.theory)) =>
wenzelm@46737
   168
          Exn.Exn(ERROR("Attempt to update loaded theory " + quote(name.theory)))
wenzelm@46737
   169
        case _ => header
wenzelm@44442
   170
      }
wenzelm@44442
   171
    (name, Document.Node.Header(header1))
wenzelm@44442
   172
  }
wenzelm@44442
   173
wenzelm@43651
   174
wenzelm@43651
   175
  /* actor messages */
wenzelm@43651
   176
wenzelm@48020
   177
  private case class Start(args: List[String])
wenzelm@44775
   178
  private case object Cancel_Execution
wenzelm@47629
   179
  private case class Edit(edits: List[Document.Edit_Text])
wenzelm@47629
   180
  private case class Change(
wenzelm@43722
   181
    doc_edits: List[Document.Edit_Command],
wenzelm@43722
   182
    previous: Document.Version,
wenzelm@43722
   183
    version: Document.Version)
wenzelm@44733
   184
  private case class Messages(msgs: List[Isabelle_Process.Message])
wenzelm@41534
   185
wenzelm@39572
   186
  private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   187
  {
wenzelm@43648
   188
    val this_actor = self
wenzelm@34809
   189
wenzelm@44676
   190
    var prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   191
wenzelm@34809
   192
wenzelm@44733
   193
    /* buffered prover messages */
wenzelm@44733
   194
wenzelm@44733
   195
    object receiver
wenzelm@44733
   196
    {
wenzelm@44733
   197
      private var buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   198
wenzelm@44733
   199
      def flush(): Unit = synchronized {
wenzelm@44733
   200
        if (!buffer.isEmpty) {
wenzelm@44733
   201
          val msgs = buffer.toList
wenzelm@44733
   202
          this_actor ! Messages(msgs)
wenzelm@44733
   203
          buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   204
        }
wenzelm@44733
   205
      }
wenzelm@44733
   206
      def invoke(msg: Isabelle_Process.Message): Unit = synchronized {
wenzelm@44733
   207
        buffer += msg
wenzelm@44733
   208
        msg match {
wenzelm@46772
   209
          case output: Isabelle_Process.Output =>
wenzelm@46772
   210
            if (output.is_syslog)
wenzelm@46771
   211
              syslog >> (queue =>
wenzelm@46771
   212
                {
wenzelm@46772
   213
                  val queue1 = queue.enqueue(output.message)
wenzelm@46771
   214
                  if (queue1.length > syslog_limit) queue1.dequeue._2 else queue1
wenzelm@46771
   215
                })
wenzelm@46774
   216
            if (output.is_protocol) flush()
wenzelm@44733
   217
          case _ =>
wenzelm@44733
   218
        }
wenzelm@44733
   219
      }
wenzelm@44733
   220
wenzelm@44733
   221
      private val timer = new Timer("session_actor.receiver", true)
wenzelm@44733
   222
      timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms)
wenzelm@44733
   223
wenzelm@44733
   224
      def cancel() { timer.cancel() }
wenzelm@44733
   225
    }
wenzelm@44733
   226
wenzelm@45709
   227
    var prover: Option[Isabelle_Process with Protocol] = None
wenzelm@44733
   228
wenzelm@44733
   229
wenzelm@44722
   230
    /* delayed command changes */
wenzelm@44722
   231
wenzelm@46570
   232
    object delay_commands_changed
wenzelm@44722
   233
    {
wenzelm@47027
   234
      private var changed_assignment: Boolean = false
wenzelm@44722
   235
      private var changed_nodes: Set[Document.Node.Name] = Set.empty
wenzelm@44722
   236
      private var changed_commands: Set[Command] = Set.empty
wenzelm@44722
   237
wenzelm@44722
   238
      private var flush_time: Option[Long] = None
wenzelm@44722
   239
wenzelm@44722
   240
      def flush_timeout: Long =
wenzelm@44722
   241
        flush_time match {
wenzelm@44722
   242
          case None => 5000L
wenzelm@44722
   243
          case Some(time) => (time - System.currentTimeMillis()) max 0
wenzelm@44722
   244
        }
wenzelm@44722
   245
wenzelm@44722
   246
      def flush()
wenzelm@44722
   247
      {
wenzelm@47027
   248
        if (changed_assignment || !changed_nodes.isEmpty || !changed_commands.isEmpty)
wenzelm@47027
   249
          commands_changed_buffer !
wenzelm@47027
   250
            Session.Commands_Changed(changed_assignment, changed_nodes, changed_commands)
wenzelm@47027
   251
        changed_assignment = false
wenzelm@44722
   252
        changed_nodes = Set.empty
wenzelm@44722
   253
        changed_commands = Set.empty
wenzelm@44722
   254
        flush_time = None
wenzelm@44722
   255
      }
wenzelm@44722
   256
wenzelm@47027
   257
      def invoke(assign: Boolean, commands: List[Command])
wenzelm@44722
   258
      {
wenzelm@47027
   259
        changed_assignment |= assign
wenzelm@47027
   260
        for (command <- commands) {
wenzelm@47027
   261
          changed_nodes += command.node_name
wenzelm@47027
   262
          changed_commands += command
wenzelm@47027
   263
        }
wenzelm@44722
   264
        val now = System.currentTimeMillis()
wenzelm@44722
   265
        flush_time match {
wenzelm@44722
   266
          case None => flush_time = Some(now + output_delay.ms)
wenzelm@44722
   267
          case Some(time) => if (now >= time) flush()
wenzelm@44722
   268
        }
wenzelm@44722
   269
      }
wenzelm@44722
   270
    }
wenzelm@44722
   271
wenzelm@44722
   272
wenzelm@43716
   273
    /* resulting changes */
wenzelm@34809
   274
wenzelm@47629
   275
    def handle_change(change: Change)
wenzelm@38221
   276
    //{{{
wenzelm@34809
   277
    {
wenzelm@43722
   278
      val previous = change.previous
wenzelm@43722
   279
      val version = change.version
wenzelm@43722
   280
      val doc_edits = change.doc_edits
wenzelm@38366
   281
wenzelm@44383
   282
      def id_command(command: Command)
wenzelm@43720
   283
      {
wenzelm@44582
   284
        if (!global_state().defined_command(command.id)) {
wenzelm@46687
   285
          global_state >> (_.define_command(command))
wenzelm@44644
   286
          prover.get.define_command(command)
wenzelm@43720
   287
        }
wenzelm@43720
   288
      }
wenzelm@44383
   289
      doc_edits foreach {
wenzelm@44383
   290
        case (_, edit) =>
wenzelm@44383
   291
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   292
      }
wenzelm@43722
   293
wenzelm@44479
   294
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@46687
   295
      global_state >> (_.define_version(version, assignment))
wenzelm@44481
   296
      prover.get.update(previous.id, version.id, doc_edits)
wenzelm@34809
   297
    }
wenzelm@38221
   298
    //}}}
wenzelm@34809
   299
wenzelm@34809
   300
wenzelm@46772
   301
    /* prover output */
wenzelm@34809
   302
wenzelm@46772
   303
    def handle_output(output: Isabelle_Process.Output)
wenzelm@38221
   304
    //{{{
wenzelm@34809
   305
    {
wenzelm@46772
   306
      def bad_output(output: Isabelle_Process.Output)
wenzelm@43716
   307
      {
wenzelm@43716
   308
        if (verbose)
wenzelm@46772
   309
          System.err.println("Ignoring prover output: " + output.message.toString)
wenzelm@43716
   310
      }
wenzelm@43716
   311
wenzelm@46772
   312
      output.properties match {
wenzelm@46122
   313
wenzelm@46774
   314
        case Position.Id(state_id) if !output.is_protocol =>
wenzelm@38370
   315
          try {
wenzelm@46772
   316
            val st = global_state >>> (_.accumulate(state_id, output.message))
wenzelm@47027
   317
            delay_commands_changed.invoke(false, List(st.command))
wenzelm@38370
   318
          }
wenzelm@43748
   319
          catch {
wenzelm@46772
   320
            case _: Document.State.Fail => bad_output(output)
wenzelm@43748
   321
          }
wenzelm@46122
   322
wenzelm@46774
   323
        case Isabelle_Markup.Assign_Execs if output.is_protocol =>
wenzelm@46772
   324
          XML.content(output.body).mkString match {
wenzelm@45709
   325
            case Protocol.Assign(id, assign) =>
wenzelm@47629
   326
              try {
wenzelm@47629
   327
                val cmds = global_state >>> (_.assign(id, assign))
wenzelm@47629
   328
                delay_commands_changed.invoke(true, cmds)
wenzelm@47629
   329
              }
wenzelm@46772
   330
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   331
            case _ => bad_output(output)
wenzelm@44661
   332
          }
wenzelm@44676
   333
          // FIXME separate timeout event/message!?
wenzelm@44676
   334
          if (prover.isDefined && System.currentTimeMillis() > prune_next) {
wenzelm@46687
   335
            val old_versions = global_state >>> (_.prune_history(prune_size))
wenzelm@44676
   336
            if (!old_versions.isEmpty) prover.get.remove_versions(old_versions)
wenzelm@44676
   337
            prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   338
          }
wenzelm@46122
   339
wenzelm@46774
   340
        case Isabelle_Markup.Removed_Versions if output.is_protocol =>
wenzelm@46772
   341
          XML.content(output.body).mkString match {
wenzelm@45709
   342
            case Protocol.Removed(removed) =>
wenzelm@47629
   343
              try {
wenzelm@47629
   344
                global_state >> (_.removed_versions(removed))
wenzelm@47629
   345
              }
wenzelm@46772
   346
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   347
            case _ => bad_output(output)
wenzelm@44676
   348
          }
wenzelm@46122
   349
wenzelm@46774
   350
        case Isabelle_Markup.Invoke_Scala(name, id) if output.is_protocol =>
wenzelm@43748
   351
          Future.fork {
wenzelm@46772
   352
            val arg = XML.content(output.body).mkString
wenzelm@43748
   353
            val (tag, res) = Invoke_Scala.method(name, arg)
wenzelm@43748
   354
            prover.get.invoke_scala(id, tag, res)
wenzelm@43748
   355
          }
wenzelm@46122
   356
wenzelm@46774
   357
        case Isabelle_Markup.Cancel_Scala(id) if output.is_protocol =>
wenzelm@46121
   358
          System.err.println("cancel_scala " + id)  // FIXME actually cancel JVM task
wenzelm@46122
   359
wenzelm@46774
   360
        case Isabelle_Markup.Ready if output.is_protocol =>
wenzelm@46121
   361
            phase = Session.Ready
wenzelm@46122
   362
wenzelm@46774
   363
        case Isabelle_Markup.Loaded_Theory(name) if output.is_protocol =>
wenzelm@46122
   364
          thy_load.register_thy(name)
wenzelm@46122
   365
wenzelm@46774
   366
        case Isabelle_Markup.Command_Decl(name, kind) if output.is_protocol =>
wenzelm@46941
   367
          prover_syntax += (name, kind)
wenzelm@46123
   368
wenzelm@46774
   369
        case Isabelle_Markup.Keyword_Decl(name) if output.is_protocol =>
wenzelm@46941
   370
          prover_syntax += name
wenzelm@46123
   371
wenzelm@48016
   372
        case Isabelle_Markup.Return_Code(rc) if output.is_exit =>
wenzelm@48016
   373
          if (rc == 0) phase = Session.Inactive
wenzelm@48016
   374
          else phase = Session.Failed
wenzelm@48016
   375
wenzelm@38722
   376
        case _ =>
wenzelm@48016
   377
          if (output.is_init || output.is_stdout) { }
wenzelm@46772
   378
          else bad_output(output)
wenzelm@43748
   379
      }
wenzelm@34809
   380
    }
wenzelm@38221
   381
    //}}}
wenzelm@34809
   382
wenzelm@34820
   383
wenzelm@39630
   384
    /* main loop */
wenzelm@34809
   385
wenzelm@43716
   386
    //{{{
wenzelm@38639
   387
    var finished = false
wenzelm@38639
   388
    while (!finished) {
wenzelm@46570
   389
      receiveWithin(delay_commands_changed.flush_timeout) {
wenzelm@46570
   390
        case TIMEOUT => delay_commands_changed.flush()
wenzelm@44722
   391
wenzelm@48020
   392
        case Start(args) if prover.isEmpty =>
wenzelm@39701
   393
          if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@39701
   394
            phase = Session.Startup
wenzelm@48020
   395
            prover = Some(new Isabelle_Process(receiver.invoke _, args) with Protocol)
wenzelm@39701
   396
          }
wenzelm@38841
   397
wenzelm@39701
   398
        case Stop =>
wenzelm@39701
   399
          if (phase == Session.Ready) {
wenzelm@46687
   400
            global_state >> (_ => Document.State.init)  // FIXME event bus!?
wenzelm@39701
   401
            phase = Session.Shutdown
wenzelm@43649
   402
            prover.get.terminate
wenzelm@43717
   403
            prover = None
wenzelm@39701
   404
            phase = Session.Inactive
wenzelm@39701
   405
          }
wenzelm@39630
   406
          finished = true
wenzelm@44733
   407
          receiver.cancel()
wenzelm@39630
   408
          reply(())
wenzelm@38850
   409
wenzelm@44775
   410
        case Cancel_Execution if prover.isDefined =>
wenzelm@44775
   411
          prover.get.cancel_execution()
wenzelm@43716
   412
wenzelm@47629
   413
        case Edit(edits) if prover.isDefined =>
wenzelm@47629
   414
          prover.get.discontinue_execution()
wenzelm@43716
   415
wenzelm@47629
   416
          val previous = global_state().history.tip.version
wenzelm@47629
   417
          val version = Future.promise[Document.Version]
wenzelm@47629
   418
          val change = global_state >>> (_.continue_history(previous, edits, version))
wenzelm@47629
   419
          change_parser ! Text_Edits(previous, edits, version)
wenzelm@47629
   420
wenzelm@43718
   421
          reply(())
wenzelm@43718
   422
wenzelm@44733
   423
        case Messages(msgs) =>
wenzelm@44733
   424
          msgs foreach {
wenzelm@44733
   425
            case input: Isabelle_Process.Input =>
wenzelm@46774
   426
              all_messages.event(input)
wenzelm@43721
   427
wenzelm@46772
   428
            case output: Isabelle_Process.Output =>
wenzelm@46772
   429
              handle_output(output)
wenzelm@46772
   430
              if (output.is_syslog) syslog_messages.event(output)
wenzelm@46772
   431
              if (output.is_stdout || output.is_stderr) raw_output_messages.event(output)
wenzelm@46774
   432
              all_messages.event(output)
wenzelm@44733
   433
          }
wenzelm@43716
   434
wenzelm@47629
   435
        case change: Change
wenzelm@45635
   436
        if prover.isDefined && global_state().is_assigned(change.previous) =>
wenzelm@45635
   437
          handle_change(change)
wenzelm@45635
   438
wenzelm@47629
   439
        case bad if !bad.isInstanceOf[Change] =>
wenzelm@45635
   440
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   441
      }
wenzelm@34777
   442
    }
wenzelm@43716
   443
    //}}}
wenzelm@34777
   444
  }
wenzelm@34777
   445
wenzelm@34809
   446
wenzelm@43716
   447
  /* actions */
wenzelm@34809
   448
wenzelm@48020
   449
  def start(args: List[String]) { session_actor ! Start(args) }
wenzelm@45076
   450
wenzelm@45635
   451
  def stop() { commands_changed_buffer !? Stop; change_parser !? Stop; session_actor !? Stop }
wenzelm@38841
   452
wenzelm@44775
   453
  def cancel_execution() { session_actor ! Cancel_Execution }
wenzelm@43648
   454
wenzelm@47629
   455
  def edit(edits: List[Document.Edit_Text])
wenzelm@47629
   456
  { session_actor !? Edit(edits) }
wenzelm@47629
   457
wenzelm@44615
   458
  def init_node(name: Document.Node.Name,
wenzelm@44385
   459
    header: Document.Node_Header, perspective: Text.Perspective, text: String)
wenzelm@47629
   460
  {
wenzelm@47629
   461
    edit(List(header_edit(name, header),
wenzelm@47629
   462
      name -> Document.Node.Clear(),    // FIXME diff wrt. existing node
wenzelm@47629
   463
      name -> Document.Node.Edits(List(Text.Edit.insert(0, text))),
wenzelm@47629
   464
      name -> Document.Node.Perspective(perspective)))
wenzelm@47629
   465
  }
wenzelm@38221
   466
wenzelm@44615
   467
  def edit_node(name: Document.Node.Name,
wenzelm@47629
   468
    header: Document.Node_Header, perspective: Text.Perspective, text_edits: List[Text.Edit])
wenzelm@47629
   469
  {
wenzelm@47629
   470
    edit(List(header_edit(name, header),
wenzelm@47629
   471
      name -> Document.Node.Edits(text_edits),
wenzelm@47629
   472
      name -> Document.Node.Perspective(perspective)))
wenzelm@47629
   473
  }
wenzelm@34777
   474
}