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