src/Pure/System/session.scala
author wenzelm
Sat, 14 Aug 2010 21:25:20 +0200
changeset 38413 224efb14f258
parent 38374 7eb0f6991e25
child 38414 49f1f657adc2
permissions -rw-r--r--
Keyword.status: always suppress position;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36676
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     1
/*  Title:      Pure/System/session.scala
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     2
    Author:     Makarius
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
     3
    Options:    :folding=explicit:collapseFolds=1:
36676
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     4
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     5
Isabelle session, potentially with running prover.
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     6
*/
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
     7
34871
e596a0b71f3c incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents: 34859
diff changeset
     8
package isabelle
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
     9
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    10
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
    11
import scala.actors.TIMEOUT
38226
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
    12
import scala.actors.Actor
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    13
import scala.actors.Actor._
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    14
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    15
34791
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    16
object Session
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    17
{
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    18
  /* events */
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    19
34791
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    20
  case object Global_Settings
37849
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    21
  case object Perspective
38360
53224a4d2f0e specific Session.Commands_Changed;
wenzelm
parents: 38359
diff changeset
    22
  case class Commands_Changed(set: Set[Command])
34791
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    23
}
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    24
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    25
34851
304a86164dd2 provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents: 34848
diff changeset
    26
class Session(system: Isabelle_System)
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    27
{
37849
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    28
  /* real time parameters */  // FIXME properties or settings (!?)
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    29
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    30
  // user input (e.g. text edits, cursor movement)
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    31
  val input_delay = 300
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    32
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    33
  // prover output (markup, common messages)
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    34
  val output_delay = 100
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    35
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    36
  // GUI layout updates
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    37
  val update_delay = 500
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    38
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    39
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    40
  /* pervasive event buses */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    41
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    42
  val global_settings = new Event_Bus[Session.Global_Settings.type]
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    43
  val raw_results = new Event_Bus[Isabelle_Process.Result]
37065
2a73253b5898 separate event bus and dockable for raw output (stdout);
wenzelm
parents: 37063
diff changeset
    44
  val raw_output = new Event_Bus[Isabelle_Process.Result]
38360
53224a4d2f0e specific Session.Commands_Changed;
wenzelm
parents: 38359
diff changeset
    45
  val commands_changed = new Event_Bus[Session.Commands_Changed]
37849
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
    46
  val perspective = new Event_Bus[Session.Perspective.type]
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    47
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    48
34778
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    49
  /* unique ids */
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    50
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38360
diff changeset
    51
  private var id_count: Document.ID = 0
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38360
diff changeset
    52
  def create_id(): Document.ID = synchronized {
38355
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38260
diff changeset
    53
    require(id_count > java.lang.Long.MIN_VALUE)
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38260
diff changeset
    54
    id_count -= 1
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38260
diff changeset
    55
    id_count
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38260
diff changeset
    56
  }
34778
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    57
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    58
34826
6b38fc0b3406 eliminated redundant session documents database;
wenzelm
parents: 34825
diff changeset
    59
6b38fc0b3406 eliminated redundant session documents database;
wenzelm
parents: 34825
diff changeset
    60
  /** main actor **/
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    61
34851
304a86164dd2 provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents: 34848
diff changeset
    62
  @volatile private var syntax = new Outer_Syntax(system.symbols)
34819
86cb7f8e5a0d tuned signature;
wenzelm
parents: 34818
diff changeset
    63
  def current_syntax: Outer_Syntax = syntax
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    64
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    65
  @volatile private var global_state = Document.State.init
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    66
  private def change_state(f: Document.State => Document.State) { global_state = f(global_state) }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    67
  def current_state(): Document.State = global_state
34816
d33312514220 maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents: 34815
diff changeset
    68
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
    69
  private case class Started(timeout: Int, args: List[String])
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    70
  private case object Stop
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    71
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
    72
  private lazy val session_actor = actor {
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    73
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    74
    var prover: Isabelle_Process with Isar_Document = null
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    75
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    76
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    77
    /* document changes */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    78
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38226
diff changeset
    79
    def handle_change(change: Document.Change)
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
    80
    //{{{
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    81
    {
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
    82
      require(change.is_finished)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    83
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    84
      val old_doc = change.prev.join
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    85
      val (node_edits, doc) = change.result.join
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
    86
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    87
      var former_assignment = current_state().the_assignment(old_doc).join
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    88
      for {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    89
        (name, Some(cmd_edits)) <- node_edits
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    90
        (prev, None) <- cmd_edits
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    91
        removed <- old_doc.nodes(name).commands.get_after(prev)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    92
      } former_assignment -= removed
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    93
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    94
      val id_edits =
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    95
        node_edits map {
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    96
          case (name, None) => (name, None)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    97
          case (name, Some(cmd_edits)) =>
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
    98
            val ids =
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    99
              cmd_edits map {
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   100
                case (c1, c2) =>
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   101
                  val id1 = c1.map(_.id)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   102
                  val id2 =
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   103
                    c2 match {
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   104
                      case None => None
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   105
                      case Some(command) =>
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   106
                        if (current_state().lookup_command(command.id).isEmpty) {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   107
                          change_state(_.define_command(command))
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   108
                          prover.define_command(command.id, system.symbols.encode(command.source))
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   109
                        }
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   110
                        Some(command.id)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   111
                    }
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   112
                  (id1, id2)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   113
              }
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   114
            (name -> Some(ids))
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   115
        }
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   116
      change_state(_.define_document(doc, former_assignment))
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   117
      prover.edit_document(old_doc.id, doc.id, id_edits)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   118
    }
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   119
    //}}}
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   120
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   121
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   122
    /* prover results */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   123
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   124
    def bad_result(result: Isabelle_Process.Result)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   125
    {
37041
dae419819a80 bad_result: report fully explicit message;
wenzelm
parents: 36947
diff changeset
   126
      System.err.println("Ignoring prover result: " + result.message.toString)
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   127
    }
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   128
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   129
    def handle_result(result: Isabelle_Process.Result)
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   130
    //{{{
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   131
    {
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   132
      raw_results.event(result)
34799
0330a4284a9b just one variable for outer syntax keywords and completion;
wenzelm
parents: 34795
diff changeset
   133
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   134
      Position.get_id(result.properties) match {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   135
        case Some(target_id) =>
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   136
          try {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   137
            val (st, state) = global_state.accumulate(target_id, result.message)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   138
            global_state = state
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   139
            indicate_command_change(st.command)  // FIXME forward Command.State (!?)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   140
          }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   141
          catch {
38373
wenzelm
parents: 38370
diff changeset
   142
            case _: Document.State.Fail =>
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   143
              if (result.is_status) {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   144
                result.body match {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   145
                  case List(Isar_Document.Assign(edits)) =>
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   146
                    try { change_state(_.assign(target_id, edits)) }
38373
wenzelm
parents: 38370
diff changeset
   147
                    catch { case _: Document.State.Fail => bad_result(result) }
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   148
                  case _ => bad_result(result)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   149
                }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   150
              }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   151
              else bad_result(result)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   152
          }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   153
        case None =>
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   154
          if (result.is_status) {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   155
            result.body match {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   156
              case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   157
              case List(Keyword.Keyword_Decl(name)) => syntax += name
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   158
              case _ => if (!result.is_ready) bad_result(result)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   159
            }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   160
          }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   161
          else if (result.kind == Markup.EXIT) prover = null
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   162
          else if (result.is_raw) raw_output.event(result)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   163
          else if (!result.is_system) bad_result(result)  // FIXME syslog for system messages (!?)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   164
        }
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   165
    }
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   166
    //}}}
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   167
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   168
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   169
    /* prover startup */
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   170
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   171
    def startup_error(): String =
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   172
    {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   173
      val buf = new StringBuilder
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   174
      while (
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   175
        receiveWithin(0) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   176
          case result: Isabelle_Process.Result =>
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   177
            if (result.is_raw) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   178
              for (text <- XML.content(result.message))
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   179
                buf.append(text)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   180
            }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   181
            true
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   182
          case TIMEOUT => false
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   183
        }) {}
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   184
      buf.toString
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   185
    }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   186
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   187
    def prover_startup(timeout: Int): Option[String] =
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   188
    {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   189
      receiveWithin(timeout) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   190
        case result: Isabelle_Process.Result
37689
628eabe2213a simplified Isabelle_Process.Result: use markup directly;
wenzelm
parents: 37132
diff changeset
   191
          if result.kind == Markup.INIT =>
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   192
          while (receive {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   193
            case result: Isabelle_Process.Result =>
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   194
              handle_result(result); !result.is_ready
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   195
            }) {}
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   196
          None
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   197
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   198
        case result: Isabelle_Process.Result
37689
628eabe2213a simplified Isabelle_Process.Result: use markup directly;
wenzelm
parents: 37132
diff changeset
   199
          if result.kind == Markup.EXIT =>
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   200
          Some(startup_error())
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   201
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   202
        case TIMEOUT =>  // FIXME clarify
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   203
          prover.kill; Some(startup_error())
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   204
      }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   205
    }
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   206
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   207
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   208
    /* main loop */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   209
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   210
    val xml_cache = new XML.Cache(131071)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   211
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   212
    loop {
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   213
      react {
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   214
        case Started(timeout, args) =>
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   215
          if (prover == null) {
34851
304a86164dd2 provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents: 34848
diff changeset
   216
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   217
            val origin = sender
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   218
            val opt_err = prover_startup(timeout)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   219
            if (opt_err.isDefined) prover = null
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   220
            origin ! opt_err
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   221
          }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   222
          else reply(None)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   223
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   224
        case Stop =>  // FIXME clarify; synchronous
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   225
          if (prover != null) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   226
            prover.kill
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   227
            prover = null
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   228
          }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   229
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38226
diff changeset
   230
        case change: Document.Change if prover != null =>
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   231
          handle_change(change)
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   232
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   233
        case result: Isabelle_Process.Result =>
34795
c97335b7e8c3 cache results;
wenzelm
parents: 34792
diff changeset
   234
          handle_result(result.cache(xml_cache))
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   235
36782
0499d05663dd ignore spurious TIMEOUT messages, maybe caused by change of actor semantics in scala-2.8;
wenzelm
parents: 36682
diff changeset
   236
        case TIMEOUT =>  // FIXME clarify!
0499d05663dd ignore spurious TIMEOUT messages, maybe caused by change of actor semantics in scala-2.8;
wenzelm
parents: 36682
diff changeset
   237
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   238
        case bad if prover != null =>
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   239
          System.err.println("session_actor: ignoring bad message " + bad)
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   240
      }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   241
    }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   242
  }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   243
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   244
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   245
38226
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   246
  /** buffered command changes (delay_first discipline) **/
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   247
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   248
  private lazy val command_change_buffer = actor
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   249
  //{{{
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   250
  {
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   251
    import scala.compat.Platform.currentTime
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   252
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   253
    var changed: Set[Command] = Set()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   254
    var flush_time: Option[Long] = None
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   255
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   256
    def flush_timeout: Long =
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   257
      flush_time match {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   258
        case None => 5000L
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   259
        case Some(time) => (time - currentTime) max 0
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   260
      }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   261
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   262
    def flush()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   263
    {
38360
53224a4d2f0e specific Session.Commands_Changed;
wenzelm
parents: 38359
diff changeset
   264
      if (!changed.isEmpty) commands_changed.event(Session.Commands_Changed(changed))
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   265
      changed = Set()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   266
      flush_time = None
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   267
    }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   268
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   269
    def invoke()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   270
    {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   271
      val now = currentTime
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   272
      flush_time match {
37849
4f9de312cc23 Session: predefined real time parameters;
wenzelm
parents: 37689
diff changeset
   273
        case None => flush_time = Some(now + output_delay)
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   274
        case Some(time) => if (now >= time) flush()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   275
      }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   276
    }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   277
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   278
    loop {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   279
      reactWithin(flush_timeout) {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   280
        case command: Command => changed += command; invoke()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   281
        case TIMEOUT => flush()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   282
        case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   283
      }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   284
    }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   285
  }
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   286
  //}}}
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   287
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   288
  def indicate_command_change(command: Command)
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   289
  {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   290
    command_change_buffer ! command
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   291
  }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   292
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   293
38226
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   294
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   295
  /** editor history **/
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   296
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   297
  private case class Edit_Document(edits: List[Document.Node_Text_Edit])
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   298
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   299
  private val editor_history = new Actor
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   300
  {
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   301
    @volatile private var history = List(Document.Change.init)
38365
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   302
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   303
    def snapshot(name: String, pending_edits: List[Text_Edit]): Document.Snapshot =
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   304
    {
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   305
      val state_snapshot = current_state()
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   306
      val history_snapshot = history
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   307
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   308
      val found_stable = history_snapshot.find(change =>
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   309
        change.is_finished && state_snapshot.is_assigned(change.document.join))
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   310
      require(found_stable.isDefined)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   311
      val stable = found_stable.get
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   312
      val latest = history_snapshot.head
38365
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   313
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   314
      val edits =
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   315
        (pending_edits /: history_snapshot.takeWhile(_ != stable))((edits, change) =>
38365
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   316
            (for ((a, eds) <- change.edits if a == name) yield eds).flatten ::: edits)
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   317
      lazy val reverse_edits = edits.reverse
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   318
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   319
      new Document.Snapshot {
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   320
        val document = stable.document.join
38365
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   321
        val node = document.nodes(name)
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   322
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
38365
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   323
        def convert(offset: Int): Int = (offset /: edits)((i, edit) => edit.convert(i))
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   324
        def revert(offset: Int): Int = (offset /: reverse_edits)((i, edit) => edit.revert(i))
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   325
        def lookup_command(id: Document.Command_ID): Option[Command] =
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   326
          state_snapshot.lookup_command(id)
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   327
        def state(command: Command): Command.State =
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   328
          state_snapshot.command_state(document, command)
38365
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   329
      }
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   330
    }
38226
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   331
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   332
    def act
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   333
    {
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   334
      loop {
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   335
        react {
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   336
          case Edit_Document(edits) =>
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   337
            val history_snapshot = history
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   338
            require(!history_snapshot.isEmpty)
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   339
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   340
            val prev = history_snapshot.head.document
38226
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   341
            val result: isabelle.Future[(List[Document.Edit[Command]], Document)] =
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   342
              isabelle.Future.fork {
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   343
                val old_doc = prev.join
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38366
diff changeset
   344
                val former_assignment = current_state().the_assignment(old_doc).join  // FIXME async!?
38374
7eb0f6991e25 moved Document.text_edits to Thy_Syntax;
wenzelm
parents: 38373
diff changeset
   345
                Thy_Syntax.text_edits(Session.this, old_doc, edits)
38226
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   346
              }
38366
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   347
            val new_change = new Document.Change(prev, edits, result)
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   348
            history ::= new_change
fea82d1add74 simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents: 38365
diff changeset
   349
            new_change.document.map(_ => session_actor ! new_change)
38260
d4a1c7a19be3 edit_document: synchronous reply to ensure consistent state wrt. calling (AWT) thread;
wenzelm
parents: 38230
diff changeset
   350
            reply(())
38226
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   351
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   352
          case bad => System.err.println("editor_model: ignoring bad message " + bad)
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   353
        }
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   354
      }
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   355
    }
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   356
  }
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   357
  editor_history.start
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   358
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   359
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   360
9d8848d70b0a maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents: 38222
diff changeset
   361
  /** main methods **/
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   362
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   363
  def started(timeout: Int, args: List[String]): Option[String] =
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   364
    (session_actor !? Started(timeout, args)).asInstanceOf[Option[String]]
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   365
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   366
  def stop() { session_actor ! Stop }
38221
e0f00f0945b4 misc tuning and clarification;
wenzelm
parents: 38150
diff changeset
   367
38359
96b22dfeb56a consider snapshot as service of Session, not Document.Change;
wenzelm
parents: 38355
diff changeset
   368
  def snapshot(name: String, pending_edits: List[Text_Edit]): Document.Snapshot =
38365
7c6254a9c432 moved snapshot to Session (cf. 96b22dfeb56a);
wenzelm
parents: 38364
diff changeset
   369
    editor_history.snapshot(name, pending_edits)
38260
d4a1c7a19be3 edit_document: synchronous reply to ensure consistent state wrt. calling (AWT) thread;
wenzelm
parents: 38230
diff changeset
   370
d4a1c7a19be3 edit_document: synchronous reply to ensure consistent state wrt. calling (AWT) thread;
wenzelm
parents: 38230
diff changeset
   371
  def edit_document(edits: List[Document.Node_Text_Edit]) { editor_history !? Edit_Document(edits) }
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   372
}