src/Pure/System/session.scala
author wenzelm
Tue Aug 21 12:15:25 2012 +0200 (2012-08-21)
changeset 48870 4accee106f0f
parent 48794 8d2a026e576b
child 48884 963b50ec6d73
permissions -rw-r--r--
clarified initialization of Thy_Load, Thy_Info, Session;
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@48870
    40
class Session(val thy_load: 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@48870
   111
              Thy_Syntax.text_edits(thy_load.base_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@46771
   128
  private val syslog = Volatile(Queue.empty[XML.Elem])
wenzelm@46771
   129
  def current_syslog(): String = cat_lines(syslog().iterator.map(msg => XML.content(msg).mkString))
wenzelm@39626
   130
wenzelm@39630
   131
  @volatile private var _phase: Session.Phase = Session.Inactive
wenzelm@39633
   132
  private def phase_=(new_phase: Session.Phase)
wenzelm@39630
   133
  {
wenzelm@39630
   134
    _phase = new_phase
wenzelm@39701
   135
    phase_changed.event(new_phase)
wenzelm@39630
   136
  }
wenzelm@43716
   137
  def phase = _phase
wenzelm@43553
   138
  def is_ready: Boolean = phase == Session.Ready
wenzelm@39630
   139
wenzelm@45248
   140
  private val global_state = Volatile(Document.State.init)
wenzelm@43719
   141
  def current_state(): Document.State = global_state()
wenzelm@46944
   142
wenzelm@46944
   143
  def recent_syntax(): Outer_Syntax =
wenzelm@46944
   144
  {
wenzelm@46944
   145
    val version = current_state().recent_finished.version.get_finished
wenzelm@48870
   146
    if (version.is_init) thy_load.base_syntax
wenzelm@46944
   147
    else version.syntax
wenzelm@46944
   148
  }
wenzelm@48713
   149
  def get_recent_syntax(): Option[Outer_Syntax] =
wenzelm@48713
   150
    if (is_ready) Some(recent_syntax)
wenzelm@48713
   151
    else None
wenzelm@34816
   152
wenzelm@44960
   153
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@44960
   154
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@43719
   155
    global_state().snapshot(name, pending_edits)
wenzelm@43716
   156
wenzelm@43651
   157
wenzelm@43651
   158
  /* theory files */
wenzelm@43651
   159
wenzelm@48707
   160
  def header_edit(name: Document.Node.Name, header: Document.Node.Header): Document.Edit_Text =
wenzelm@44442
   161
  {
wenzelm@48707
   162
    val header1 =
wenzelm@48870
   163
      if (thy_load.loaded_theories(name.theory))
wenzelm@48707
   164
        header.error("Attempt to update loaded theory " + quote(name.theory))
wenzelm@48707
   165
      else header
wenzelm@48707
   166
    (name, Document.Node.Deps(header1))
wenzelm@44442
   167
  }
wenzelm@44442
   168
wenzelm@43651
   169
wenzelm@43651
   170
  /* actor messages */
wenzelm@43651
   171
wenzelm@48870
   172
  private case class Start(args: List[String])
wenzelm@44775
   173
  private case object Cancel_Execution
wenzelm@47629
   174
  private case class Edit(edits: List[Document.Edit_Text])
wenzelm@47629
   175
  private case class Change(
wenzelm@43722
   176
    doc_edits: List[Document.Edit_Command],
wenzelm@43722
   177
    previous: Document.Version,
wenzelm@43722
   178
    version: Document.Version)
wenzelm@44733
   179
  private case class Messages(msgs: List[Isabelle_Process.Message])
wenzelm@41534
   180
wenzelm@39572
   181
  private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   182
  {
wenzelm@43648
   183
    val this_actor = self
wenzelm@34809
   184
wenzelm@44676
   185
    var prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   186
wenzelm@34809
   187
wenzelm@44733
   188
    /* buffered prover messages */
wenzelm@44733
   189
wenzelm@44733
   190
    object receiver
wenzelm@44733
   191
    {
wenzelm@44733
   192
      private var buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   193
wenzelm@44733
   194
      def flush(): Unit = synchronized {
wenzelm@44733
   195
        if (!buffer.isEmpty) {
wenzelm@44733
   196
          val msgs = buffer.toList
wenzelm@44733
   197
          this_actor ! Messages(msgs)
wenzelm@44733
   198
          buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   199
        }
wenzelm@44733
   200
      }
wenzelm@44733
   201
      def invoke(msg: Isabelle_Process.Message): Unit = synchronized {
wenzelm@44733
   202
        buffer += msg
wenzelm@44733
   203
        msg match {
wenzelm@46772
   204
          case output: Isabelle_Process.Output =>
wenzelm@46772
   205
            if (output.is_syslog)
wenzelm@46771
   206
              syslog >> (queue =>
wenzelm@46771
   207
                {
wenzelm@46772
   208
                  val queue1 = queue.enqueue(output.message)
wenzelm@46771
   209
                  if (queue1.length > syslog_limit) queue1.dequeue._2 else queue1
wenzelm@46771
   210
                })
wenzelm@46774
   211
            if (output.is_protocol) flush()
wenzelm@44733
   212
          case _ =>
wenzelm@44733
   213
        }
wenzelm@44733
   214
      }
wenzelm@44733
   215
wenzelm@44733
   216
      private val timer = new Timer("session_actor.receiver", true)
wenzelm@44733
   217
      timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms)
wenzelm@44733
   218
wenzelm@44733
   219
      def cancel() { timer.cancel() }
wenzelm@44733
   220
    }
wenzelm@44733
   221
wenzelm@45709
   222
    var prover: Option[Isabelle_Process with Protocol] = None
wenzelm@44733
   223
wenzelm@44733
   224
wenzelm@44722
   225
    /* delayed command changes */
wenzelm@44722
   226
wenzelm@46570
   227
    object delay_commands_changed
wenzelm@44722
   228
    {
wenzelm@47027
   229
      private var changed_assignment: Boolean = false
wenzelm@44722
   230
      private var changed_nodes: Set[Document.Node.Name] = Set.empty
wenzelm@44722
   231
      private var changed_commands: Set[Command] = Set.empty
wenzelm@44722
   232
wenzelm@44722
   233
      private var flush_time: Option[Long] = None
wenzelm@44722
   234
wenzelm@44722
   235
      def flush_timeout: Long =
wenzelm@44722
   236
        flush_time match {
wenzelm@44722
   237
          case None => 5000L
wenzelm@44722
   238
          case Some(time) => (time - System.currentTimeMillis()) max 0
wenzelm@44722
   239
        }
wenzelm@44722
   240
wenzelm@44722
   241
      def flush()
wenzelm@44722
   242
      {
wenzelm@47027
   243
        if (changed_assignment || !changed_nodes.isEmpty || !changed_commands.isEmpty)
wenzelm@47027
   244
          commands_changed_buffer !
wenzelm@47027
   245
            Session.Commands_Changed(changed_assignment, changed_nodes, changed_commands)
wenzelm@47027
   246
        changed_assignment = false
wenzelm@44722
   247
        changed_nodes = Set.empty
wenzelm@44722
   248
        changed_commands = Set.empty
wenzelm@44722
   249
        flush_time = None
wenzelm@44722
   250
      }
wenzelm@44722
   251
wenzelm@47027
   252
      def invoke(assign: Boolean, commands: List[Command])
wenzelm@44722
   253
      {
wenzelm@47027
   254
        changed_assignment |= assign
wenzelm@47027
   255
        for (command <- commands) {
wenzelm@47027
   256
          changed_nodes += command.node_name
wenzelm@47027
   257
          changed_commands += command
wenzelm@47027
   258
        }
wenzelm@44722
   259
        val now = System.currentTimeMillis()
wenzelm@44722
   260
        flush_time match {
wenzelm@44722
   261
          case None => flush_time = Some(now + output_delay.ms)
wenzelm@44722
   262
          case Some(time) => if (now >= time) flush()
wenzelm@44722
   263
        }
wenzelm@44722
   264
      }
wenzelm@44722
   265
    }
wenzelm@44722
   266
wenzelm@44722
   267
wenzelm@43716
   268
    /* resulting changes */
wenzelm@34809
   269
wenzelm@47629
   270
    def handle_change(change: Change)
wenzelm@38221
   271
    //{{{
wenzelm@34809
   272
    {
wenzelm@43722
   273
      val previous = change.previous
wenzelm@43722
   274
      val version = change.version
wenzelm@43722
   275
      val doc_edits = change.doc_edits
wenzelm@38366
   276
wenzelm@44383
   277
      def id_command(command: Command)
wenzelm@43720
   278
      {
wenzelm@44582
   279
        if (!global_state().defined_command(command.id)) {
wenzelm@46687
   280
          global_state >> (_.define_command(command))
wenzelm@44644
   281
          prover.get.define_command(command)
wenzelm@43720
   282
        }
wenzelm@43720
   283
      }
wenzelm@44383
   284
      doc_edits foreach {
wenzelm@44383
   285
        case (_, edit) =>
wenzelm@44383
   286
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   287
      }
wenzelm@43722
   288
wenzelm@44479
   289
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@46687
   290
      global_state >> (_.define_version(version, assignment))
wenzelm@44481
   291
      prover.get.update(previous.id, version.id, doc_edits)
wenzelm@34809
   292
    }
wenzelm@38221
   293
    //}}}
wenzelm@34809
   294
wenzelm@34809
   295
wenzelm@46772
   296
    /* prover output */
wenzelm@34809
   297
wenzelm@46772
   298
    def handle_output(output: Isabelle_Process.Output)
wenzelm@38221
   299
    //{{{
wenzelm@34809
   300
    {
wenzelm@46772
   301
      def bad_output(output: Isabelle_Process.Output)
wenzelm@43716
   302
      {
wenzelm@43716
   303
        if (verbose)
wenzelm@46772
   304
          System.err.println("Ignoring prover output: " + output.message.toString)
wenzelm@43716
   305
      }
wenzelm@43716
   306
wenzelm@46772
   307
      output.properties match {
wenzelm@46122
   308
wenzelm@46774
   309
        case Position.Id(state_id) if !output.is_protocol =>
wenzelm@38370
   310
          try {
wenzelm@46772
   311
            val st = global_state >>> (_.accumulate(state_id, output.message))
wenzelm@47027
   312
            delay_commands_changed.invoke(false, List(st.command))
wenzelm@38370
   313
          }
wenzelm@43748
   314
          catch {
wenzelm@46772
   315
            case _: Document.State.Fail => bad_output(output)
wenzelm@43748
   316
          }
wenzelm@46122
   317
wenzelm@46774
   318
        case Isabelle_Markup.Assign_Execs if output.is_protocol =>
wenzelm@46772
   319
          XML.content(output.body).mkString match {
wenzelm@45709
   320
            case Protocol.Assign(id, assign) =>
wenzelm@47629
   321
              try {
wenzelm@47629
   322
                val cmds = global_state >>> (_.assign(id, assign))
wenzelm@47629
   323
                delay_commands_changed.invoke(true, cmds)
wenzelm@47629
   324
              }
wenzelm@46772
   325
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   326
            case _ => bad_output(output)
wenzelm@44661
   327
          }
wenzelm@44676
   328
          // FIXME separate timeout event/message!?
wenzelm@44676
   329
          if (prover.isDefined && System.currentTimeMillis() > prune_next) {
wenzelm@46687
   330
            val old_versions = global_state >>> (_.prune_history(prune_size))
wenzelm@44676
   331
            if (!old_versions.isEmpty) prover.get.remove_versions(old_versions)
wenzelm@44676
   332
            prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   333
          }
wenzelm@46122
   334
wenzelm@46774
   335
        case Isabelle_Markup.Removed_Versions if output.is_protocol =>
wenzelm@46772
   336
          XML.content(output.body).mkString match {
wenzelm@45709
   337
            case Protocol.Removed(removed) =>
wenzelm@47629
   338
              try {
wenzelm@47629
   339
                global_state >> (_.removed_versions(removed))
wenzelm@47629
   340
              }
wenzelm@46772
   341
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   342
            case _ => bad_output(output)
wenzelm@44676
   343
          }
wenzelm@46122
   344
wenzelm@46774
   345
        case Isabelle_Markup.Invoke_Scala(name, id) if output.is_protocol =>
wenzelm@43748
   346
          Future.fork {
wenzelm@46772
   347
            val arg = XML.content(output.body).mkString
wenzelm@43748
   348
            val (tag, res) = Invoke_Scala.method(name, arg)
wenzelm@43748
   349
            prover.get.invoke_scala(id, tag, res)
wenzelm@43748
   350
          }
wenzelm@46122
   351
wenzelm@46774
   352
        case Isabelle_Markup.Cancel_Scala(id) if output.is_protocol =>
wenzelm@46121
   353
          System.err.println("cancel_scala " + id)  // FIXME actually cancel JVM task
wenzelm@46122
   354
wenzelm@48712
   355
        case _ if output.is_init =>
wenzelm@46121
   356
            phase = Session.Ready
wenzelm@46122
   357
wenzelm@48016
   358
        case Isabelle_Markup.Return_Code(rc) if output.is_exit =>
wenzelm@48016
   359
          if (rc == 0) phase = Session.Inactive
wenzelm@48016
   360
          else phase = Session.Failed
wenzelm@48016
   361
wenzelm@48712
   362
        case _ if output.is_stdout =>
wenzelm@48712
   363
wenzelm@48712
   364
        case _ => bad_output(output)
wenzelm@43748
   365
      }
wenzelm@34809
   366
    }
wenzelm@38221
   367
    //}}}
wenzelm@34809
   368
wenzelm@34820
   369
wenzelm@39630
   370
    /* main loop */
wenzelm@34809
   371
wenzelm@43716
   372
    //{{{
wenzelm@38639
   373
    var finished = false
wenzelm@38639
   374
    while (!finished) {
wenzelm@46570
   375
      receiveWithin(delay_commands_changed.flush_timeout) {
wenzelm@46570
   376
        case TIMEOUT => delay_commands_changed.flush()
wenzelm@44722
   377
wenzelm@48870
   378
        case Start(args) if prover.isEmpty =>
wenzelm@39701
   379
          if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@39701
   380
            phase = Session.Startup
wenzelm@48020
   381
            prover = Some(new Isabelle_Process(receiver.invoke _, args) with Protocol)
wenzelm@39701
   382
          }
wenzelm@38841
   383
wenzelm@39701
   384
        case Stop =>
wenzelm@39701
   385
          if (phase == Session.Ready) {
wenzelm@46687
   386
            global_state >> (_ => Document.State.init)  // FIXME event bus!?
wenzelm@39701
   387
            phase = Session.Shutdown
wenzelm@43649
   388
            prover.get.terminate
wenzelm@43717
   389
            prover = None
wenzelm@39701
   390
            phase = Session.Inactive
wenzelm@39701
   391
          }
wenzelm@39630
   392
          finished = true
wenzelm@44733
   393
          receiver.cancel()
wenzelm@39630
   394
          reply(())
wenzelm@38850
   395
wenzelm@44775
   396
        case Cancel_Execution if prover.isDefined =>
wenzelm@44775
   397
          prover.get.cancel_execution()
wenzelm@43716
   398
wenzelm@47629
   399
        case Edit(edits) if prover.isDefined =>
wenzelm@47629
   400
          prover.get.discontinue_execution()
wenzelm@43716
   401
wenzelm@47629
   402
          val previous = global_state().history.tip.version
wenzelm@47629
   403
          val version = Future.promise[Document.Version]
wenzelm@47629
   404
          val change = global_state >>> (_.continue_history(previous, edits, version))
wenzelm@47629
   405
          change_parser ! Text_Edits(previous, edits, version)
wenzelm@47629
   406
wenzelm@43718
   407
          reply(())
wenzelm@43718
   408
wenzelm@44733
   409
        case Messages(msgs) =>
wenzelm@44733
   410
          msgs foreach {
wenzelm@44733
   411
            case input: Isabelle_Process.Input =>
wenzelm@46774
   412
              all_messages.event(input)
wenzelm@43721
   413
wenzelm@46772
   414
            case output: Isabelle_Process.Output =>
wenzelm@46772
   415
              handle_output(output)
wenzelm@46772
   416
              if (output.is_syslog) syslog_messages.event(output)
wenzelm@46772
   417
              if (output.is_stdout || output.is_stderr) raw_output_messages.event(output)
wenzelm@46774
   418
              all_messages.event(output)
wenzelm@44733
   419
          }
wenzelm@43716
   420
wenzelm@47629
   421
        case change: Change
wenzelm@45635
   422
        if prover.isDefined && global_state().is_assigned(change.previous) =>
wenzelm@45635
   423
          handle_change(change)
wenzelm@45635
   424
wenzelm@47629
   425
        case bad if !bad.isInstanceOf[Change] =>
wenzelm@45635
   426
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   427
      }
wenzelm@34777
   428
    }
wenzelm@43716
   429
    //}}}
wenzelm@34777
   430
  }
wenzelm@34777
   431
wenzelm@34809
   432
wenzelm@43716
   433
  /* actions */
wenzelm@34809
   434
wenzelm@48870
   435
  def start(args: List[String])
wenzelm@48870
   436
  { session_actor ! Start(args) }
wenzelm@45076
   437
wenzelm@45635
   438
  def stop() { commands_changed_buffer !? Stop; change_parser !? Stop; session_actor !? Stop }
wenzelm@38841
   439
wenzelm@44775
   440
  def cancel_execution() { session_actor ! Cancel_Execution }
wenzelm@43648
   441
wenzelm@47629
   442
  def edit(edits: List[Document.Edit_Text])
wenzelm@47629
   443
  { session_actor !? Edit(edits) }
wenzelm@47629
   444
wenzelm@44615
   445
  def init_node(name: Document.Node.Name,
wenzelm@48707
   446
    header: Document.Node.Header, perspective: Text.Perspective, text: String)
wenzelm@47629
   447
  {
wenzelm@47629
   448
    edit(List(header_edit(name, header),
wenzelm@48755
   449
      name -> Document.Node.Clear(),
wenzelm@47629
   450
      name -> Document.Node.Edits(List(Text.Edit.insert(0, text))),
wenzelm@47629
   451
      name -> Document.Node.Perspective(perspective)))
wenzelm@47629
   452
  }
wenzelm@38221
   453
wenzelm@44615
   454
  def edit_node(name: Document.Node.Name,
wenzelm@48707
   455
    header: Document.Node.Header, perspective: Text.Perspective, text_edits: List[Text.Edit])
wenzelm@47629
   456
  {
wenzelm@47629
   457
    edit(List(header_edit(name, header),
wenzelm@47629
   458
      name -> Document.Node.Edits(text_edits),
wenzelm@47629
   459
      name -> Document.Node.Perspective(perspective)))
wenzelm@47629
   460
  }
wenzelm@34777
   461
}