src/Pure/PIDE/document.scala
author wenzelm
Mon Mar 13 21:37:35 2017 +0100 (2017-03-13 ago)
changeset 65214 a2ec0db555c7
parent 65199 6bd7081f8319
child 65221 6af51a47545b
permissions -rw-r--r--
clarified modules;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/document.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@36676
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@38418
     5
list of commands, associated with asynchronous execution process.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34760
    10
wenzelm@38150
    11
import scala.collection.mutable
wenzelm@37073
    12
wenzelm@37073
    13
wenzelm@34823
    14
object Document
wenzelm@34483
    15
{
wenzelm@38424
    16
  /** document structure **/
wenzelm@38424
    17
wenzelm@52977
    18
  /* overlays -- print functions with arguments */
wenzelm@52977
    19
wenzelm@52977
    20
  object Overlays
wenzelm@52977
    21
  {
wenzelm@52977
    22
    val empty = new Overlays(Map.empty)
wenzelm@52977
    23
  }
wenzelm@52977
    24
wenzelm@52977
    25
  final class Overlays private(rep: Map[Node.Name, Node.Overlays])
wenzelm@52977
    26
  {
wenzelm@64799
    27
    def apply(name: Node.Name): Node.Overlays =
wenzelm@52977
    28
      rep.getOrElse(name, Node.Overlays.empty)
wenzelm@52977
    29
wenzelm@52977
    30
    private def update(name: Node.Name, f: Node.Overlays => Node.Overlays): Overlays =
wenzelm@52977
    31
    {
wenzelm@52977
    32
      val node_overlays = f(apply(name))
wenzelm@52977
    33
      new Overlays(if (node_overlays.is_empty) rep - name else rep + (name -> node_overlays))
wenzelm@52977
    34
    }
wenzelm@52977
    35
wenzelm@52977
    36
    def insert(command: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
    37
      update(command.node_name, _.insert(command, fn, args))
wenzelm@52977
    38
wenzelm@52977
    39
    def remove(command: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
    40
      update(command.node_name, _.remove(command, fn, args))
wenzelm@55800
    41
wenzelm@55800
    42
    override def toString: String = rep.mkString("Overlays(", ",", ")")
wenzelm@52977
    43
  }
wenzelm@52977
    44
wenzelm@52977
    45
wenzelm@55783
    46
  /* document blobs: auxiliary files */
wenzelm@55783
    47
wenzelm@56746
    48
  sealed case class Blob(bytes: Bytes, chunk: Symbol.Text_Chunk, changed: Boolean)
wenzelm@56335
    49
  {
wenzelm@56335
    50
    def unchanged: Blob = copy(changed = false)
wenzelm@56335
    51
  }
wenzelm@55783
    52
wenzelm@55783
    53
  object Blobs
wenzelm@55783
    54
  {
wenzelm@56336
    55
    def apply(blobs: Map[Node.Name, Blob]): Blobs = new Blobs(blobs)
wenzelm@55783
    56
    val empty: Blobs = apply(Map.empty)
wenzelm@55783
    57
  }
wenzelm@55783
    58
wenzelm@56336
    59
  final class Blobs private(blobs: Map[Node.Name, Blob])
wenzelm@55783
    60
  {
wenzelm@56336
    61
    def get(name: Node.Name): Option[Blob] = blobs.get(name)
wenzelm@55783
    62
wenzelm@55783
    63
    def changed(name: Node.Name): Boolean =
wenzelm@55783
    64
      get(name) match {
wenzelm@55783
    65
        case Some(blob) => blob.changed
wenzelm@55783
    66
        case None => false
wenzelm@55783
    67
      }
wenzelm@55783
    68
wenzelm@55800
    69
    override def toString: String = blobs.mkString("Blobs(", ",", ")")
wenzelm@55783
    70
  }
wenzelm@55783
    71
wenzelm@55783
    72
wenzelm@55783
    73
  /* document nodes: theories and auxiliary files */
wenzelm@38424
    74
wenzelm@44615
    75
  type Edit[A, B] = (Node.Name, Node.Edit[A, B])
wenzelm@44384
    76
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
wenzelm@52849
    77
  type Edit_Command = Edit[Command.Edit, Command.Perspective]
wenzelm@38151
    78
wenzelm@38151
    79
  object Node
wenzelm@38151
    80
  {
wenzelm@52887
    81
    /* header and name */
wenzelm@52887
    82
wenzelm@48707
    83
    sealed case class Header(
wenzelm@63020
    84
      imports: List[(Name, Position.T)] = Nil,
wenzelm@63020
    85
      keywords: Thy_Header.Keywords = Nil,
wenzelm@63579
    86
      abbrevs: Thy_Header.Abbrevs = Nil,
wenzelm@63020
    87
      errors: List[String] = Nil)
wenzelm@48707
    88
    {
wenzelm@48707
    89
      def error(msg: String): Header = copy(errors = errors ::: List(msg))
wenzelm@54549
    90
wenzelm@54549
    91
      def cat_errors(msg2: String): Header =
wenzelm@62492
    92
        copy(errors = errors.map(msg1 => Exn.cat_message(msg1, msg2)))
wenzelm@48707
    93
    }
wenzelm@48707
    94
wenzelm@63020
    95
    val no_header = Header()
wenzelm@63020
    96
    def bad_header(msg: String): Header = Header(errors = List(msg))
wenzelm@46737
    97
wenzelm@44957
    98
    object Name
wenzelm@44957
    99
    {
wenzelm@54515
   100
      val empty = Name("")
wenzelm@46723
   101
wenzelm@46723
   102
      object Ordering extends scala.math.Ordering[Name]
wenzelm@46723
   103
      {
wenzelm@46723
   104
        def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
wenzelm@46723
   105
      }
wenzelm@44957
   106
    }
wenzelm@52887
   107
wenzelm@54515
   108
    sealed case class Name(node: String, master_dir: String = "", theory: String = "")
wenzelm@44615
   109
    {
wenzelm@44615
   110
      override def hashCode: Int = node.hashCode
wenzelm@44615
   111
      override def equals(that: Any): Boolean =
wenzelm@44615
   112
        that match {
wenzelm@44615
   113
          case other: Name => node == other.node
wenzelm@44615
   114
          case _ => false
wenzelm@44615
   115
        }
wenzelm@54509
   116
wenzelm@59319
   117
      def is_theory: Boolean = theory.nonEmpty
wenzelm@54510
   118
      override def toString: String = if (is_theory) theory else node
wenzelm@56801
   119
wenzelm@56801
   120
      def map(f: String => String): Name = copy(f(node), f(master_dir), theory)
wenzelm@44615
   121
    }
wenzelm@44615
   122
wenzelm@52887
   123
wenzelm@52977
   124
    /* node overlays */
wenzelm@52887
   125
wenzelm@52887
   126
    object Overlays
wenzelm@52887
   127
    {
wenzelm@52976
   128
      val empty = new Overlays(Multi_Map.empty)
wenzelm@52887
   129
    }
wenzelm@52887
   130
wenzelm@52977
   131
    final class Overlays private(rep: Multi_Map[Command, (String, List[String])])
wenzelm@52887
   132
    {
wenzelm@52887
   133
      def commands: Set[Command] = rep.keySet
wenzelm@52887
   134
      def is_empty: Boolean = rep.isEmpty
wenzelm@52977
   135
      def dest: List[(Command, (String, List[String]))] = rep.iterator.toList
wenzelm@52977
   136
      def insert(cmd: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
   137
        new Overlays(rep.insert(cmd, (fn, args)))
wenzelm@52977
   138
      def remove(cmd: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
   139
        new Overlays(rep.remove(cmd, (fn, args)))
wenzelm@55800
   140
wenzelm@55800
   141
      override def toString: String = rep.mkString("Node.Overlays(", ",", ")")
wenzelm@52887
   142
    }
wenzelm@52887
   143
wenzelm@52887
   144
wenzelm@52887
   145
    /* edits */
wenzelm@52887
   146
wenzelm@44384
   147
    sealed abstract class Edit[A, B]
wenzelm@44156
   148
    {
wenzelm@44383
   149
      def foreach(f: A => Unit)
wenzelm@44383
   150
      {
wenzelm@44156
   151
        this match {
wenzelm@44383
   152
          case Edits(es) => es.foreach(f)
wenzelm@44383
   153
          case _ =>
wenzelm@44156
   154
        }
wenzelm@44383
   155
      }
wenzelm@57621
   156
wenzelm@57621
   157
      def is_void: Boolean =
wenzelm@57621
   158
        this match {
wenzelm@57621
   159
          case Edits(Nil) => true
wenzelm@57621
   160
          case _ => false
wenzelm@57621
   161
        }
wenzelm@44156
   162
    }
wenzelm@56335
   163
    case class Blob[A, B](blob: Document.Blob) extends Edit[A, B]
wenzelm@54562
   164
wenzelm@44384
   165
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@48707
   166
    case class Deps[A, B](header: Header) extends Edit[A, B]
wenzelm@52849
   167
    case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B]
wenzelm@57610
   168
wenzelm@57610
   169
wenzelm@57610
   170
    /* perspective */
wenzelm@57610
   171
wenzelm@52808
   172
    type Perspective_Text = Perspective[Text.Edit, Text.Perspective]
wenzelm@52849
   173
    type Perspective_Command = Perspective[Command.Edit, Command.Perspective]
wenzelm@44156
   174
wenzelm@57615
   175
    val no_perspective_text: Perspective_Text =
wenzelm@57610
   176
      Perspective(false, Text.Perspective.empty, Overlays.empty)
wenzelm@57610
   177
wenzelm@57615
   178
    val no_perspective_command: Perspective_Command =
wenzelm@57614
   179
      Perspective(false, Command.Perspective.empty, Overlays.empty)
wenzelm@57614
   180
wenzelm@64867
   181
    def is_no_perspective_text(perspective: Perspective_Text): Boolean =
wenzelm@64867
   182
      !perspective.required &&
wenzelm@64867
   183
      perspective.visible.is_empty &&
wenzelm@64867
   184
      perspective.overlays.is_empty
wenzelm@64867
   185
wenzelm@57615
   186
    def is_no_perspective_command(perspective: Perspective_Command): Boolean =
wenzelm@57615
   187
      !perspective.required &&
wenzelm@57615
   188
      perspective.visible.is_empty &&
wenzelm@57615
   189
      perspective.overlays.is_empty
wenzelm@57615
   190
wenzelm@52887
   191
wenzelm@52887
   192
    /* commands */
wenzelm@52887
   193
wenzelm@52901
   194
    object Commands
wenzelm@52901
   195
    {
wenzelm@52901
   196
      def apply(commands: Linear_Set[Command]): Commands = new Commands(commands)
wenzelm@52901
   197
      val empty: Commands = apply(Linear_Set.empty)
wenzelm@52901
   198
wenzelm@52901
   199
      def starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@52901
   200
        : Iterator[(Command, Text.Offset)] =
wenzelm@52901
   201
      {
wenzelm@52901
   202
        var i = offset
wenzelm@52901
   203
        for (command <- commands) yield {
wenzelm@52901
   204
          val start = i
wenzelm@52901
   205
          i += command.length
wenzelm@52901
   206
          (command, start)
wenzelm@52901
   207
        }
wenzelm@52901
   208
      }
wenzelm@52901
   209
wenzelm@56398
   210
      private val block_size = 256
wenzelm@52901
   211
    }
wenzelm@52901
   212
wenzelm@52901
   213
    final class Commands private(val commands: Linear_Set[Command])
wenzelm@38227
   214
    {
wenzelm@56314
   215
      lazy val load_commands: List[Command] =
wenzelm@59319
   216
        commands.iterator.filter(cmd => cmd.blobs.nonEmpty).toList
wenzelm@54462
   217
wenzelm@52901
   218
      private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@52901
   219
      {
wenzelm@52901
   220
        val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@52901
   221
        var next_block = 0
wenzelm@52901
   222
        var last_stop = 0
wenzelm@52901
   223
        for ((command, start) <- Commands.starts(commands.iterator)) {
wenzelm@52901
   224
          last_stop = start + command.length
wenzelm@52901
   225
          while (last_stop + 1 > next_block) {
wenzelm@52901
   226
            blocks += (command -> start)
wenzelm@52901
   227
            next_block += Commands.block_size
wenzelm@52901
   228
          }
wenzelm@52901
   229
        }
wenzelm@52901
   230
        (blocks.toArray, Text.Range(0, last_stop))
wenzelm@52901
   231
      }
wenzelm@52901
   232
wenzelm@52901
   233
      private def full_range: Text.Range = full_index._2
wenzelm@52901
   234
wenzelm@56373
   235
      def iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@52901
   236
      {
wenzelm@59319
   237
        if (commands.nonEmpty && full_range.contains(i)) {
wenzelm@52901
   238
          val (cmd0, start0) = full_index._1(i / Commands.block_size)
wenzelm@52901
   239
          Node.Commands.starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@52901
   240
            case (cmd, start) => start + cmd.length <= i }
wenzelm@52901
   241
        }
wenzelm@52901
   242
        else Iterator.empty
wenzelm@38227
   243
      }
wenzelm@38227
   244
    }
wenzelm@57614
   245
wenzelm@57614
   246
    val empty: Node = new Node()
wenzelm@38151
   247
  }
wenzelm@38151
   248
wenzelm@46712
   249
  final class Node private(
wenzelm@56335
   250
    val get_blob: Option[Document.Blob] = None,
wenzelm@57615
   251
    val header: Node.Header = Node.no_header,
wenzelm@63584
   252
    val syntax: Option[Outer_Syntax] = None,
wenzelm@59372
   253
    val text_perspective: Text.Perspective = Text.Perspective.empty,
wenzelm@57615
   254
    val perspective: Node.Perspective_Command = Node.no_perspective_command,
wenzelm@52901
   255
    _commands: Node.Commands = Node.Commands.empty)
wenzelm@38151
   256
  {
wenzelm@57615
   257
    def is_empty: Boolean =
wenzelm@57615
   258
      get_blob.isEmpty &&
wenzelm@57615
   259
      header == Node.no_header &&
wenzelm@59372
   260
      text_perspective.is_empty &&
wenzelm@57615
   261
      Node.is_no_perspective_command(perspective) &&
wenzelm@57615
   262
      commands.isEmpty
wenzelm@57615
   263
wenzelm@59704
   264
    def has_header: Boolean = header != Node.no_header
wenzelm@59704
   265
wenzelm@57615
   266
    def commands: Linear_Set[Command] = _commands.commands
wenzelm@57615
   267
    def load_commands: List[Command] = _commands.load_commands
wenzelm@64799
   268
    def load_commands_changed(doc_blobs: Blobs): Boolean =
wenzelm@64799
   269
      load_commands.exists(_.blobs_changed(doc_blobs))
wenzelm@57615
   270
wenzelm@64799
   271
    def init_blob(blob: Blob): Node = new Node(get_blob = Some(blob.unchanged))
wenzelm@55435
   272
wenzelm@48707
   273
    def update_header(new_header: Node.Header): Node =
wenzelm@59372
   274
      new Node(get_blob, new_header, syntax, text_perspective, perspective, _commands)
wenzelm@59077
   275
wenzelm@63584
   276
    def update_syntax(new_syntax: Option[Outer_Syntax]): Node =
wenzelm@59372
   277
      new Node(get_blob, header, new_syntax, text_perspective, perspective, _commands)
wenzelm@59372
   278
wenzelm@59372
   279
    def update_perspective(
wenzelm@59372
   280
        new_text_perspective: Text.Perspective,
wenzelm@59372
   281
        new_perspective: Node.Perspective_Command): Node =
wenzelm@59372
   282
      new Node(get_blob, header, syntax, new_text_perspective, new_perspective, _commands)
wenzelm@46680
   283
wenzelm@59372
   284
    def edit_perspective: Node.Edit[Text.Edit, Text.Perspective] =
wenzelm@59372
   285
      Node.Perspective(perspective.required, text_perspective, perspective.overlays)
wenzelm@46680
   286
wenzelm@59372
   287
    def same_perspective(
wenzelm@59372
   288
        other_text_perspective: Text.Perspective,
wenzelm@59372
   289
        other_perspective: Node.Perspective_Command): Boolean =
wenzelm@59372
   290
      text_perspective == other_text_perspective &&
wenzelm@52849
   291
      perspective.required == other_perspective.required &&
wenzelm@52849
   292
      perspective.visible.same(other_perspective.visible) &&
wenzelm@52849
   293
      perspective.overlays == other_perspective.overlays
wenzelm@52808
   294
wenzelm@52901
   295
    def update_commands(new_commands: Linear_Set[Command]): Node =
wenzelm@52918
   296
      if (new_commands eq _commands.commands) this
wenzelm@59372
   297
      else
wenzelm@59372
   298
        new Node(get_blob, header, syntax, text_perspective, perspective,
wenzelm@59372
   299
          Node.Commands(new_commands))
wenzelm@38151
   300
wenzelm@56373
   301
    def command_iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   302
      _commands.iterator(i)
wenzelm@38151
   303
wenzelm@56373
   304
    def command_iterator(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   305
      command_iterator(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   306
wenzelm@38879
   307
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@52901
   308
      Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2)
wenzelm@38151
   309
  }
wenzelm@38151
   310
wenzelm@38151
   311
wenzelm@46723
   312
  /* development graph */
wenzelm@46723
   313
wenzelm@46723
   314
  object Nodes
wenzelm@46723
   315
  {
wenzelm@46723
   316
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
wenzelm@46723
   317
  }
wenzelm@46723
   318
wenzelm@46723
   319
  final class Nodes private(graph: Graph[Node.Name, Node])
wenzelm@46723
   320
  {
wenzelm@46723
   321
    def apply(name: Node.Name): Node =
wenzelm@46723
   322
      graph.default_node(name, Node.empty).get_node(name)
wenzelm@46723
   323
wenzelm@57619
   324
    def is_hidden(name: Node.Name): Boolean =
wenzelm@57619
   325
    {
wenzelm@57619
   326
      val graph1 = graph.default_node(name, Node.empty)
wenzelm@57619
   327
      graph1.is_maximal(name) && graph1.get_node(name).is_empty
wenzelm@57619
   328
    }
wenzelm@57617
   329
wenzelm@46723
   330
    def + (entry: (Node.Name, Node)): Nodes =
wenzelm@46723
   331
    {
wenzelm@46723
   332
      val (name, node) = entry
wenzelm@59695
   333
      val imports = node.header.imports.map(_._1)
wenzelm@46723
   334
      val graph1 =
wenzelm@46739
   335
        (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
wenzelm@46723
   336
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
wenzelm@46739
   337
      val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
wenzelm@57616
   338
      new Nodes(
wenzelm@57616
   339
        if (graph3.is_maximal(name) && node.is_empty) graph3.del_node(name)
wenzelm@57616
   340
        else graph3.map_node(name, _ => node)
wenzelm@57616
   341
      )
wenzelm@46723
   342
    }
wenzelm@46723
   343
wenzelm@56372
   344
    def iterator: Iterator[(Node.Name, Node)] =
wenzelm@56372
   345
      graph.iterator.map({ case (name, (node, _)) => (name, node) })
wenzelm@46723
   346
wenzelm@64799
   347
    def commands_loading(file_name: Node.Name): List[Command] =
wenzelm@54528
   348
      (for {
wenzelm@56372
   349
        (_, node) <- iterator
wenzelm@56314
   350
        cmd <- node.load_commands.iterator
wenzelm@54530
   351
        name <- cmd.blobs_names.iterator
wenzelm@54528
   352
        if name == file_name
wenzelm@54528
   353
      } yield cmd).toList
wenzelm@54528
   354
wenzelm@46942
   355
    def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
wenzelm@46723
   356
    def topological_order: List[Node.Name] = graph.topological_order
wenzelm@56337
   357
wenzelm@56337
   358
    override def toString: String = topological_order.mkString("Nodes(", ",", ")")
wenzelm@46723
   359
  }
wenzelm@46723
   360
wenzelm@46723
   361
wenzelm@38424
   362
wenzelm@38424
   363
  /** versioning **/
wenzelm@38424
   364
wenzelm@38424
   365
  /* particular document versions */
wenzelm@34485
   366
wenzelm@38417
   367
  object Version
wenzelm@38417
   368
  {
wenzelm@46681
   369
    val init: Version = new Version()
wenzelm@59077
   370
    def make(nodes: Nodes): Version = new Version(Document_ID.make(), nodes)
wenzelm@38417
   371
  }
wenzelm@38417
   372
wenzelm@46712
   373
  final class Version private(
wenzelm@52530
   374
    val id: Document_ID.Version = Document_ID.none,
wenzelm@46723
   375
    val nodes: Nodes = Nodes.empty)
wenzelm@46941
   376
  {
wenzelm@55777
   377
    override def toString: String = "Version(" + id + ")"
wenzelm@46941
   378
  }
immler@34660
   379
wenzelm@34859
   380
wenzelm@38424
   381
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   382
wenzelm@38227
   383
  object Change
wenzelm@38227
   384
  {
wenzelm@46678
   385
    val init: Change = new Change()
wenzelm@46678
   386
wenzelm@46678
   387
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
wenzelm@57620
   388
      new Change(Some(previous), edits.reverse, version)
wenzelm@38227
   389
  }
wenzelm@38227
   390
wenzelm@46712
   391
  final class Change private(
wenzelm@46677
   392
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm@57620
   393
    val rev_edits: List[Edit_Text] = Nil,
wenzelm@46677
   394
    val version: Future[Version] = Future.value(Version.init))
wenzelm@38227
   395
  {
wenzelm@44672
   396
    def is_finished: Boolean =
wenzelm@44672
   397
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   398
      version.is_finished
wenzelm@44672
   399
wenzelm@46678
   400
    def truncate: Change = new Change(None, Nil, version)
wenzelm@38227
   401
  }
wenzelm@38227
   402
wenzelm@38227
   403
wenzelm@38841
   404
  /* history navigation */
wenzelm@38841
   405
wenzelm@38841
   406
  object History
wenzelm@38841
   407
  {
wenzelm@46679
   408
    val init: History = new History()
wenzelm@38841
   409
  }
wenzelm@38841
   410
wenzelm@46712
   411
  final class History private(
wenzelm@46679
   412
    val undo_list: List[Change] = List(Change.init))  // non-empty list
wenzelm@38841
   413
  {
wenzelm@46679
   414
    def tip: Change = undo_list.head
wenzelm@46679
   415
    def + (change: Change): History = new History(change :: undo_list)
wenzelm@38841
   416
wenzelm@46679
   417
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
wenzelm@46679
   418
    {
wenzelm@46679
   419
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
wenzelm@46679
   420
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@46679
   421
wenzelm@46679
   422
      retained.splitAt(retained.length - 1) match {
wenzelm@46679
   423
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
wenzelm@46679
   424
        case _ => None
wenzelm@46679
   425
      }
wenzelm@46679
   426
    }
wenzelm@38841
   427
  }
wenzelm@38841
   428
wenzelm@38841
   429
wenzelm@55820
   430
  /* snapshot */
wenzelm@38424
   431
wenzelm@52972
   432
  object Snapshot
wenzelm@52972
   433
  {
wenzelm@52972
   434
    val init = State.init.snapshot()
wenzelm@52972
   435
  }
wenzelm@52972
   436
wenzelm@38424
   437
  abstract class Snapshot
wenzelm@38424
   438
  {
wenzelm@64798
   439
    def state: State
wenzelm@64798
   440
    def version: Version
wenzelm@64798
   441
    def is_outdated: Boolean
wenzelm@55649
   442
wenzelm@38427
   443
    def convert(i: Text.Offset): Text.Offset
wenzelm@52889
   444
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   445
    def convert(range: Text.Range): Text.Range
wenzelm@39177
   446
    def revert(range: Text.Range): Text.Range
wenzelm@52972
   447
wenzelm@64798
   448
    def node_name: Node.Name
wenzelm@64798
   449
    def node: Node
wenzelm@64799
   450
    def commands_loading: List[Command]
wenzelm@64799
   451
    def commands_loading_ranges(pred: Node.Name => Boolean): List[Text.Range]
wenzelm@65195
   452
    def command_results(command: Command): Command.Results
wenzelm@65199
   453
    def current_command(other_node_name: Node.Name, offset: Text.Offset): Option[Command]
wenzelm@55649
   454
wenzelm@64665
   455
    def find_command(id: Document_ID.Generic): Option[(Node, Command)]
wenzelm@64665
   456
    def find_command_position(id: Document_ID.Generic, offset: Symbol.Offset)
wenzelm@64665
   457
      : Option[Line.Node_Position]
wenzelm@64665
   458
wenzelm@55651
   459
    def cumulate[A](
wenzelm@55649
   460
      range: Text.Range,
wenzelm@55649
   461
      info: A,
wenzelm@56743
   462
      elements: Markup.Elements,
wenzelm@56354
   463
      result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   464
      status: Boolean = false): List[Text.Info[A]]
wenzelm@55651
   465
wenzelm@55651
   466
    def select[A](
wenzelm@55649
   467
      range: Text.Range,
wenzelm@56743
   468
      elements: Markup.Elements,
wenzelm@56354
   469
      result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   470
      status: Boolean = false): List[Text.Info[A]]
wenzelm@38424
   471
  }
wenzelm@38424
   472
wenzelm@55820
   473
wenzelm@64814
   474
  /* model */
wenzelm@64814
   475
wenzelm@64814
   476
  trait Model
wenzelm@64814
   477
  {
wenzelm@64814
   478
    def session: Session
wenzelm@64827
   479
    def is_stable: Boolean
wenzelm@64827
   480
    def snapshot(): Snapshot
wenzelm@64814
   481
wenzelm@64814
   482
    def node_name: Document.Node.Name
wenzelm@64814
   483
    def is_theory: Boolean = node_name.is_theory
wenzelm@64814
   484
    override def toString: String = node_name.toString
wenzelm@64814
   485
wenzelm@64815
   486
    def node_required: Boolean
wenzelm@64814
   487
    def get_blob: Option[Document.Blob]
wenzelm@64815
   488
wenzelm@64827
   489
    def node_edits(
wenzelm@64867
   490
      node_header: Node.Header,
wenzelm@64867
   491
      text_edits: List[Text.Edit],
wenzelm@64867
   492
      perspective: Node.Perspective_Text): List[Edit_Text] =
wenzelm@64827
   493
    {
wenzelm@64827
   494
      val edits: List[Node.Edit[Text.Edit, Text.Perspective]] =
wenzelm@64827
   495
        get_blob match {
wenzelm@64827
   496
          case None =>
wenzelm@64827
   497
            List(
wenzelm@64827
   498
              Document.Node.Deps(
wenzelm@64854
   499
                if (session.resources.base.loaded_theories(node_name.theory))
wenzelm@64827
   500
                  node_header.error("Cannot update finished theory " + quote(node_name.theory))
wenzelm@64827
   501
                else node_header),
wenzelm@64827
   502
              Document.Node.Edits(text_edits), perspective)
wenzelm@64827
   503
          case Some(blob) =>
wenzelm@64827
   504
            List(Document.Node.Blob(blob), Document.Node.Edits(text_edits))
wenzelm@64827
   505
        }
wenzelm@64827
   506
      edits.flatMap(edit => if (edit.is_void) None else Some(node_name -> edit))
wenzelm@64827
   507
    }
wenzelm@64814
   508
  }
wenzelm@64814
   509
wenzelm@64814
   510
wenzelm@55820
   511
  /** global state -- document structure, execution process, editing history **/
wenzelm@55820
   512
wenzelm@52563
   513
  type Assign_Update =
wenzelm@52563
   514
    List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
wenzelm@44476
   515
wenzelm@38370
   516
  object State
wenzelm@38370
   517
  {
wenzelm@38373
   518
    class Fail(state: State) extends Exception
wenzelm@38373
   519
wenzelm@44479
   520
    object Assignment
wenzelm@44479
   521
    {
wenzelm@46683
   522
      val init: Assignment = new Assignment()
wenzelm@44479
   523
    }
wenzelm@38370
   524
wenzelm@46712
   525
    final class Assignment private(
wenzelm@52530
   526
      val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
wenzelm@46677
   527
      val is_finished: Boolean = false)
wenzelm@38370
   528
    {
wenzelm@44477
   529
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@47388
   530
      def unfinished: Assignment = new Assignment(command_execs, false)
wenzelm@44479
   531
wenzelm@52563
   532
      def assign(update: Assign_Update): Assignment =
wenzelm@38370
   533
      {
wenzelm@38976
   534
        require(!is_finished)
wenzelm@44479
   535
        val command_execs1 =
wenzelm@52563
   536
          (command_execs /: update) {
wenzelm@52563
   537
            case (res, (command_id, exec_ids)) =>
wenzelm@52563
   538
              if (exec_ids.isEmpty) res - command_id
wenzelm@52563
   539
              else res + (command_id -> exec_ids)
wenzelm@44479
   540
          }
wenzelm@47388
   541
        new Assignment(command_execs1, true)
wenzelm@38150
   542
      }
wenzelm@38370
   543
    }
wenzelm@46682
   544
wenzelm@46682
   545
    val init: State =
wenzelm@52563
   546
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
wenzelm@38370
   547
  }
wenzelm@38370
   548
wenzelm@46712
   549
  final case class State private(
wenzelm@56470
   550
    /*reachable versions*/
wenzelm@60215
   551
    versions: Map[Document_ID.Version, Version] = Map.empty,
wenzelm@56470
   552
    /*inlined auxiliary files*/
wenzelm@60215
   553
    blobs: Set[SHA1.Digest] = Set.empty,
wenzelm@56470
   554
    /*static markup from define_command*/
wenzelm@60215
   555
    commands: Map[Document_ID.Command, Command.State] = Map.empty,
wenzelm@56470
   556
    /*dynamic markup from execution*/
wenzelm@60215
   557
    execs: Map[Document_ID.Exec, Command.State] = Map.empty,
wenzelm@56470
   558
    /*command-exec assignment for each version*/
wenzelm@60215
   559
    assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
wenzelm@56470
   560
    /*commands with markup produced by other commands (imm_succs)*/
wenzelm@60215
   561
    commands_redirection: Graph[Document_ID.Command, Unit] = Graph.long,
wenzelm@56470
   562
    /*explicit (linear) history*/
wenzelm@60215
   563
    history: History = History.init,
wenzelm@57976
   564
    /*intermediate state between remove_versions/removed_versions*/
wenzelm@60215
   565
    removing_versions: Boolean = false)
wenzelm@38370
   566
  {
wenzelm@38373
   567
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   568
wenzelm@44479
   569
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   570
    {
wenzelm@38417
   571
      val id = version.id
wenzelm@38417
   572
      copy(versions = versions + (id -> version),
wenzelm@44479
   573
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   574
    }
wenzelm@38370
   575
wenzelm@54519
   576
    def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
wenzelm@54519
   577
    def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
wenzelm@54519
   578
wenzelm@38373
   579
    def define_command(command: Command): State =
wenzelm@38373
   580
    {
wenzelm@38373
   581
      val id = command.id
wenzelm@49414
   582
      copy(commands = commands + (id -> command.init_state))
wenzelm@38373
   583
    }
wenzelm@38373
   584
wenzelm@52530
   585
    def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   586
wenzelm@52530
   587
    def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
wenzelm@52564
   588
    def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
wenzelm@52564
   589
    def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
wenzelm@52563
   590
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
wenzelm@38370
   591
wenzelm@56470
   592
    private def self_id(st: Command.State)(id: Document_ID.Generic): Boolean =
wenzelm@56295
   593
      id == st.command.id ||
wenzelm@56295
   594
      (execs.get(id) match { case Some(st1) => st1.command.id == st.command.id case None => false })
wenzelm@56295
   595
wenzelm@56746
   596
    private def other_id(id: Document_ID.Generic)
wenzelm@63032
   597
      : Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)] =
wenzelm@56470
   598
      (execs.get(id) orElse commands.get(id)).map(st =>
wenzelm@56746
   599
        ((Symbol.Text_Chunk.Id(st.command.id), st.command.chunk)))
wenzelm@56470
   600
wenzelm@56475
   601
    private def redirection(st: Command.State): Graph[Document_ID.Command, Unit] =
wenzelm@56475
   602
      (commands_redirection /: st.markups.redirection_iterator)({ case (graph, id) =>
wenzelm@56470
   603
        graph.default_node(id, ()).default_node(st.command.id, ()).add_edge(id, st.command.id) })
wenzelm@56470
   604
wenzelm@52531
   605
    def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
wenzelm@56470
   606
    {
wenzelm@38370
   607
      execs.get(id) match {
wenzelm@44446
   608
        case Some(st) =>
wenzelm@56470
   609
          val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   610
          val execs1 = execs + (id -> new_st)
wenzelm@56475
   611
          (new_st, copy(execs = execs1, commands_redirection = redirection(new_st)))
wenzelm@38370
   612
        case None =>
wenzelm@38370
   613
          commands.get(id) match {
wenzelm@38370
   614
            case Some(st) =>
wenzelm@56470
   615
              val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   616
              val commands1 = commands + (id -> new_st)
wenzelm@56475
   617
              (new_st, copy(commands = commands1, commands_redirection = redirection(new_st)))
wenzelm@38370
   618
            case None => fail
wenzelm@38370
   619
          }
wenzelm@38370
   620
      }
wenzelm@56470
   621
    }
wenzelm@38370
   622
wenzelm@52563
   623
    def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
wenzelm@38370
   624
    {
wenzelm@38417
   625
      val version = the_version(id)
wenzelm@38370
   626
wenzelm@52566
   627
      def upd(exec_id: Document_ID.Exec, st: Command.State)
wenzelm@52566
   628
          : Option[(Document_ID.Exec, Command.State)] =
wenzelm@52566
   629
        if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
wenzelm@52566
   630
wenzelm@44543
   631
      val (changed_commands, new_execs) =
wenzelm@52563
   632
        ((Nil: List[Command], execs) /: update) {
wenzelm@52566
   633
          case ((commands1, execs1), (command_id, exec)) =>
wenzelm@52566
   634
            val st = the_static_state(command_id)
wenzelm@52566
   635
            val command = st.command
wenzelm@52527
   636
            val commands2 = command :: commands1
wenzelm@44543
   637
            val execs2 =
wenzelm@44543
   638
              exec match {
wenzelm@52527
   639
                case Nil => execs1
wenzelm@52527
   640
                case eval_id :: print_ids =>
wenzelm@52566
   641
                  execs1 ++ upd(eval_id, st) ++
wenzelm@52566
   642
                    (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
wenzelm@44543
   643
              }
wenzelm@44543
   644
            (commands2, execs2)
wenzelm@38370
   645
        }
wenzelm@52563
   646
      val new_assignment = the_assignment(version).assign(update)
wenzelm@43722
   647
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   648
wenzelm@44543
   649
      (changed_commands, new_state)
wenzelm@38370
   650
    }
wenzelm@38370
   651
wenzelm@38417
   652
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   653
      assignments.get(version.id) match {
wenzelm@38370
   654
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   655
        case None => false
wenzelm@38370
   656
      }
wenzelm@38370
   657
wenzelm@44436
   658
    def is_stable(change: Change): Boolean =
wenzelm@44436
   659
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   660
wenzelm@46944
   661
    def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
wenzelm@44672
   662
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@60933
   663
    def stable_tip_version: Option[Version] =
wenzelm@60933
   664
      if (is_stable(history.tip)) Some(history.tip.version.get_finished) else None
wenzelm@44436
   665
wenzelm@44446
   666
    def continue_history(
wenzelm@56711
   667
      previous: Future[Version],
wenzelm@56711
   668
      edits: List[Edit_Text],
wenzelm@56711
   669
      version: Future[Version]): State =
wenzelm@38841
   670
    {
wenzelm@46678
   671
      val change = Change.make(previous, edits, version)
wenzelm@56711
   672
      copy(history = history + change)
wenzelm@38841
   673
    }
wenzelm@38841
   674
wenzelm@57976
   675
    def remove_versions(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   676
    {
wenzelm@46679
   677
      history.prune(is_stable, retain) match {
wenzelm@46679
   678
        case Some((dropped, history1)) =>
wenzelm@57976
   679
          val old_versions = dropped.map(change => change.version.get_finished)
wenzelm@59319
   680
          val removing = old_versions.nonEmpty
wenzelm@57976
   681
          val state1 = copy(history = history1, removing_versions = removing)
wenzelm@57976
   682
          (old_versions, state1)
wenzelm@46679
   683
        case None => fail
wenzelm@44672
   684
      }
wenzelm@44672
   685
    }
wenzelm@44672
   686
wenzelm@52530
   687
    def removed_versions(removed: List[Document_ID.Version]): State =
wenzelm@44676
   688
    {
wenzelm@44676
   689
      val versions1 = versions -- removed
wenzelm@44676
   690
      val assignments1 = assignments -- removed
wenzelm@54519
   691
      var blobs1 = Set.empty[SHA1.Digest]
wenzelm@52530
   692
      var commands1 = Map.empty[Document_ID.Command, Command.State]
wenzelm@52530
   693
      var execs1 = Map.empty[Document_ID.Exec, Command.State]
wenzelm@44676
   694
      for {
wenzelm@44676
   695
        (version_id, version) <- versions1.iterator
wenzelm@46997
   696
        command_execs = assignments1(version_id).command_execs
wenzelm@56372
   697
        (_, node) <- version.nodes.iterator
wenzelm@44676
   698
        command <- node.commands.iterator
wenzelm@44676
   699
      } {
wenzelm@57842
   700
        for ((_, digest) <- command.blobs_defined; if !blobs1.contains(digest))
wenzelm@54519
   701
          blobs1 += digest
wenzelm@54519
   702
wenzelm@52568
   703
        if (!commands1.isDefinedAt(command.id))
wenzelm@52568
   704
          commands.get(command.id).foreach(st => commands1 += (command.id -> st))
wenzelm@54519
   705
wenzelm@52568
   706
        for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
wenzelm@52527
   707
          if (!execs1.isDefinedAt(exec_id))
wenzelm@52527
   708
            execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
wenzelm@44676
   709
        }
wenzelm@44676
   710
      }
wenzelm@56470
   711
      copy(
wenzelm@56470
   712
        versions = versions1,
wenzelm@56470
   713
        blobs = blobs1,
wenzelm@56470
   714
        commands = commands1,
wenzelm@56470
   715
        execs = execs1,
wenzelm@56475
   716
        commands_redirection = commands_redirection.restrict(commands1.isDefinedAt(_)),
wenzelm@57976
   717
        assignments = assignments1,
wenzelm@57976
   718
        removing_versions = false)
wenzelm@44676
   719
    }
wenzelm@44676
   720
wenzelm@56470
   721
    private def command_states_self(version: Version, command: Command)
wenzelm@56470
   722
      : List[(Document_ID.Generic, Command.State)] =
wenzelm@44613
   723
    {
wenzelm@44613
   724
      require(is_assigned(version))
wenzelm@44613
   725
      try {
wenzelm@52527
   726
        the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
wenzelm@56470
   727
          .map(id => id -> the_dynamic_state(id)) match {
wenzelm@52527
   728
            case Nil => fail
wenzelm@56470
   729
            case res => res
wenzelm@52527
   730
          }
wenzelm@44613
   731
      }
wenzelm@47395
   732
      catch {
wenzelm@47395
   733
        case _: State.Fail =>
wenzelm@56470
   734
          try { List(command.id -> the_static_state(command.id)) }
wenzelm@56470
   735
          catch { case _: State.Fail => List(command.id -> command.init_state) }
wenzelm@47395
   736
      }
wenzelm@44613
   737
    }
wenzelm@44613
   738
wenzelm@56470
   739
    def command_states(version: Version, command: Command): List[Command.State] =
wenzelm@56470
   740
    {
wenzelm@56470
   741
      val self = command_states_self(version, command)
wenzelm@56470
   742
      val others =
wenzelm@56475
   743
        if (commands_redirection.defined(command.id)) {
wenzelm@56470
   744
          (for {
wenzelm@56475
   745
            command_id <- commands_redirection.imm_succs(command.id).iterator
wenzelm@56470
   746
            (id, st) <- command_states_self(version, the_static_state(command_id).command)
wenzelm@56470
   747
            if !self.exists(_._1 == id)
wenzelm@56470
   748
          } yield (id, st)).toMap.valuesIterator.toList
wenzelm@56470
   749
        }
wenzelm@56470
   750
        else Nil
wenzelm@56489
   751
      self.map(_._2) ::: others.flatMap(_.redirect(command))
wenzelm@56470
   752
    }
wenzelm@56470
   753
wenzelm@56299
   754
    def command_results(version: Version, command: Command): Command.Results =
wenzelm@56299
   755
      Command.State.merge_results(command_states(version, command))
wenzelm@56299
   756
wenzelm@56301
   757
    def command_markup(version: Version, command: Command, index: Command.Markup_Index,
wenzelm@56743
   758
        range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@56301
   759
      Command.State.merge_markup(command_states(version, command), index, range, elements)
wenzelm@56299
   760
wenzelm@56743
   761
    def markup_to_XML(version: Version, node: Node, elements: Markup.Elements): XML.Body =
wenzelm@56299
   762
      (for {
wenzelm@56299
   763
        command <- node.commands.iterator
wenzelm@56301
   764
        markup =
wenzelm@56301
   765
          command_markup(version, command, Command.Markup_Index.markup, command.range, elements)
wenzelm@56300
   766
        tree <- markup.to_XML(command.range, command.source, elements)
wenzelm@56299
   767
      } yield tree).toList
wenzelm@38841
   768
wenzelm@38841
   769
    // persistent user-view
wenzelm@49346
   770
    def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
wenzelm@49346
   771
      : Snapshot =
wenzelm@38370
   772
    {
wenzelm@44672
   773
      val stable = recent_stable
wenzelm@44436
   774
      val latest = history.tip
wenzelm@38841
   775
wenzelm@57620
   776
wenzelm@57620
   777
      /* pending edits and unstable changes */
wenzelm@57620
   778
wenzelm@57620
   779
      val rev_pending_changes =
wenzelm@57620
   780
        for {
wenzelm@57620
   781
          change <- history.undo_list.takeWhile(_ != stable)
wenzelm@57620
   782
          (a, edits) <- change.rev_edits
wenzelm@57620
   783
          if a == name
wenzelm@57620
   784
        } yield edits
wenzelm@57620
   785
wenzelm@38841
   786
      val edits =
wenzelm@64819
   787
        (pending_edits /: rev_pending_changes)({
wenzelm@64819
   788
          case (edits, Node.Edits(es)) => es ::: edits
wenzelm@64819
   789
          case (edits, _) => edits
wenzelm@64819
   790
        })
wenzelm@38841
   791
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   792
wenzelm@38841
   793
      new Snapshot
wenzelm@38841
   794
      {
wenzelm@52972
   795
        /* global information */
wenzelm@52972
   796
wenzelm@64798
   797
        val state: State = State.this
wenzelm@64798
   798
        val version: Version = stable.version.get_finished
wenzelm@64798
   799
        val is_outdated: Boolean = pending_edits.nonEmpty || latest != stable
wenzelm@38841
   800
wenzelm@55649
   801
wenzelm@55649
   802
        /* local node content */
wenzelm@55649
   803
wenzelm@64819
   804
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@64819
   805
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@57620
   806
wenzelm@57620
   807
        def convert(range: Text.Range) = range.map(convert(_))
wenzelm@57620
   808
        def revert(range: Text.Range) = range.map(revert(_))
wenzelm@39177
   809
wenzelm@64798
   810
        val node_name: Node.Name = name
wenzelm@64799
   811
        val node: Node = version.nodes(name)
wenzelm@52972
   812
wenzelm@64799
   813
        val commands_loading: List[Command] =
wenzelm@55432
   814
          if (node_name.is_theory) Nil
wenzelm@64799
   815
          else version.nodes.commands_loading(node_name)
wenzelm@55432
   816
wenzelm@64799
   817
        def commands_loading_ranges(pred: Node.Name => Boolean): List[Text.Range] =
wenzelm@64799
   818
          (for {
wenzelm@64799
   819
            cmd <- node.load_commands.iterator
wenzelm@64799
   820
            blob_name <- cmd.blobs_names.iterator
wenzelm@64799
   821
            if pred(blob_name)
wenzelm@64799
   822
            start <- node.command_start(cmd)
wenzelm@64799
   823
          } yield convert(cmd.proper_range + start)).toList
wenzelm@56176
   824
wenzelm@65195
   825
        def command_results(command: Command): Command.Results =
wenzelm@65195
   826
          state.command_results(version, command)
wenzelm@65195
   827
wenzelm@65199
   828
        def current_command(other_node_name: Node.Name, offset: Text.Offset): Option[Command] =
wenzelm@65199
   829
          if (other_node_name.is_theory) {
wenzelm@65199
   830
            val other_node = version.nodes(other_node_name)
wenzelm@65199
   831
            val iterator = other_node.command_iterator(revert(offset) max 0)
wenzelm@65199
   832
            if (iterator.hasNext) {
wenzelm@65199
   833
              val (command0, _) = iterator.next
wenzelm@65199
   834
              other_node.commands.reverse.iterator(command0).find(command => !command.is_ignored)
wenzelm@65199
   835
            }
wenzelm@65199
   836
            else other_node.commands.reverse.iterator.find(command => !command.is_ignored)
wenzelm@65199
   837
          }
wenzelm@65199
   838
          else version.nodes.commands_loading(other_node_name).headOption
wenzelm@65199
   839
wenzelm@55649
   840
wenzelm@64665
   841
        /* find command */
wenzelm@64665
   842
wenzelm@64665
   843
        def find_command(id: Document_ID.Generic): Option[(Node, Command)] =
wenzelm@64665
   844
          state.commands.get(id) orElse state.execs.get(id) match {
wenzelm@64665
   845
            case None => None
wenzelm@64665
   846
            case Some(st) =>
wenzelm@64665
   847
              val command = st.command
wenzelm@64665
   848
              val node = version.nodes(command.node_name)
wenzelm@64665
   849
              if (node.commands.contains(command)) Some((node, command)) else None
wenzelm@64665
   850
          }
wenzelm@64665
   851
wenzelm@64665
   852
        def find_command_position(id: Document_ID.Generic, offset: Symbol.Offset)
wenzelm@64665
   853
            : Option[Line.Node_Position] =
wenzelm@64665
   854
          for ((node, command) <- find_command(id))
wenzelm@64665
   855
          yield {
wenzelm@64665
   856
            val name = command.node_name.node
wenzelm@64665
   857
            val sources_iterator =
wenzelm@64665
   858
              node.commands.iterator.takeWhile(_ != command).map(_.source) ++
wenzelm@64665
   859
                (if (offset == 0) Iterator.empty
wenzelm@64665
   860
                 else Iterator.single(command.source(Text.Range(0, command.chunk.decode(offset)))))
wenzelm@65196
   861
            val pos = (Line.Position.zero /: sources_iterator)(_.advance(_))
wenzelm@64665
   862
            Line.Node_Position(name, pos)
wenzelm@64665
   863
          }
wenzelm@64665
   864
wenzelm@64665
   865
wenzelm@55649
   866
        /* cumulate markup */
wenzelm@55649
   867
wenzelm@55651
   868
        def cumulate[A](
wenzelm@55649
   869
          range: Text.Range,
wenzelm@55649
   870
          info: A,
wenzelm@56743
   871
          elements: Markup.Elements,
wenzelm@56354
   872
          result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   873
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@45459
   874
        {
wenzelm@56590
   875
          val former_range = revert(range).inflate_singularity
wenzelm@56462
   876
          val (chunk_name, command_iterator) =
wenzelm@65187
   877
            commands_loading.headOption match {
wenzelm@65187
   878
              case None => (Symbol.Text_Chunk.Default, node.command_iterator(former_range))
wenzelm@65187
   879
              case Some(command) => (Symbol.Text_Chunk.File(node_name.node), Iterator((command, 0)))
wenzelm@56309
   880
            }
wenzelm@56462
   881
          val markup_index = Command.Markup_Index(status, chunk_name)
wenzelm@56309
   882
          (for {
wenzelm@56373
   883
            (command, command_start) <- command_iterator
wenzelm@56462
   884
            chunk <- command.chunks.get(chunk_name).iterator
wenzelm@56309
   885
            states = state.command_states(version, command)
wenzelm@56354
   886
            res = result(states)
wenzelm@56590
   887
            markup_range <- (former_range - command_start).try_restrict(chunk.range).iterator
wenzelm@56590
   888
            markup = Command.State.merge_markup(states, markup_index, markup_range, elements)
wenzelm@56590
   889
            Text.Info(r0, a) <- markup.cumulate[A](markup_range, info, elements,
wenzelm@56309
   890
              {
wenzelm@56309
   891
                case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b))
wenzelm@56309
   892
              }).iterator
wenzelm@56590
   893
            r1 <- convert(r0 + command_start).try_restrict(range).iterator
wenzelm@56590
   894
          } yield Text.Info(r1, a)).toList
wenzelm@45459
   895
        }
wenzelm@45459
   896
wenzelm@55651
   897
        def select[A](
wenzelm@55651
   898
          range: Text.Range,
wenzelm@56743
   899
          elements: Markup.Elements,
wenzelm@56354
   900
          result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   901
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@38845
   902
        {
wenzelm@56354
   903
          def result1(states: List[Command.State]): (Option[A], Text.Markup) => Option[Option[A]] =
wenzelm@50500
   904
          {
wenzelm@56354
   905
            val res = result(states)
wenzelm@52889
   906
            (_: Option[A], x: Text.Markup) =>
wenzelm@52889
   907
              res(x) match {
wenzelm@52889
   908
                case None => None
wenzelm@52889
   909
                case some => Some(some)
wenzelm@52889
   910
              }
wenzelm@50500
   911
          }
wenzelm@55651
   912
          for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status))
wenzelm@46198
   913
            yield Text.Info(r, x)
wenzelm@38845
   914
        }
wenzelm@55800
   915
wenzelm@55800
   916
wenzelm@55800
   917
        /* output */
wenzelm@55800
   918
wenzelm@55800
   919
        override def toString: String =
wenzelm@55800
   920
          "Snapshot(node = " + node_name.node + ", version = " + version.id +
wenzelm@55800
   921
            (if (is_outdated) ", outdated" else "") + ")"
wenzelm@38841
   922
      }
wenzelm@38150
   923
    }
wenzelm@34485
   924
  }
wenzelm@34840
   925
}