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