src/Pure/PIDE/command.scala
author wenzelm
Wed Aug 12 13:53:51 2015 +0200 (2015-08-12 ago)
changeset 60916 a6e2a667b0a8
parent 60215 5fb4990dfc73
child 61197 b9d69001824e
permissions -rw-r--r--
resolve undefined blobs by default, e.g. relevant for ML debugger to avoid reset of breakpoints after reload;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/command.scala
wenzelm@36676
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     3
    Author:     Makarius
wenzelm@36676
     4
wenzelm@52536
     5
Prover commands with accumulated results from execution.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34451
    10
wenzelm@45644
    11
import scala.collection.mutable
wenzelm@38872
    12
import scala.collection.immutable.SortedMap
wenzelm@59707
    13
import scala.util.parsing.input.CharSequenceReader
wenzelm@38872
    14
wenzelm@38872
    15
wenzelm@34637
    16
object Command
wenzelm@34637
    17
{
wenzelm@52849
    18
  type Edit = (Option[Command], Option[Command])
wenzelm@59704
    19
wenzelm@56746
    20
  type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, Symbol.Text_Chunk)])]
wenzelm@59704
    21
  type Blobs_Info = (List[Blob], Int)
wenzelm@59704
    22
  val no_blobs: Blobs_Info = (Nil, -1)
wenzelm@52849
    23
wenzelm@52849
    24
wenzelm@38361
    25
  /** accumulated results from prover **/
wenzelm@38361
    26
wenzelm@50507
    27
  /* results */
wenzelm@50507
    28
wenzelm@50507
    29
  object Results
wenzelm@50507
    30
  {
wenzelm@51496
    31
    type Entry = (Long, XML.Tree)
wenzelm@50507
    32
    val empty = new Results(SortedMap.empty)
wenzelm@59072
    33
    def make(args: TraversableOnce[Results.Entry]): Results = (empty /: args)(_ + _)
wenzelm@59072
    34
    def merge(args: TraversableOnce[Results]): Results = (empty /: args)(_ ++ _)
wenzelm@50507
    35
  }
wenzelm@50507
    36
wenzelm@51494
    37
  final class Results private(private val rep: SortedMap[Long, XML.Tree])
wenzelm@50507
    38
  {
wenzelm@50507
    39
    def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
wenzelm@50507
    40
    def get(serial: Long): Option[XML.Tree] = rep.get(serial)
wenzelm@56372
    41
    def iterator: Iterator[Results.Entry] = rep.iterator
wenzelm@50508
    42
wenzelm@51496
    43
    def + (entry: Results.Entry): Results =
wenzelm@50508
    44
      if (defined(entry._1)) this
wenzelm@50508
    45
      else new Results(rep + entry)
wenzelm@50508
    46
wenzelm@50508
    47
    def ++ (other: Results): Results =
wenzelm@50508
    48
      if (this eq other) this
wenzelm@50508
    49
      else if (rep.isEmpty) other
wenzelm@56372
    50
      else (this /: other.iterator)(_ + _)
wenzelm@50540
    51
wenzelm@51494
    52
    override def hashCode: Int = rep.hashCode
wenzelm@51494
    53
    override def equals(that: Any): Boolean =
wenzelm@51494
    54
      that match {
wenzelm@51494
    55
        case other: Results => rep == other.rep
wenzelm@51494
    56
        case _ => false
wenzelm@51494
    57
      }
wenzelm@56372
    58
    override def toString: String = iterator.mkString("Results(", ", ", ")")
wenzelm@50507
    59
  }
wenzelm@50507
    60
wenzelm@50507
    61
wenzelm@55649
    62
  /* markup */
wenzelm@55649
    63
wenzelm@55649
    64
  object Markup_Index
wenzelm@55649
    65
  {
wenzelm@56746
    66
    val markup: Markup_Index = Markup_Index(false, Symbol.Text_Chunk.Default)
wenzelm@55649
    67
  }
wenzelm@55649
    68
wenzelm@56746
    69
  sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.Text_Chunk.Name)
wenzelm@55649
    70
wenzelm@55649
    71
  object Markups
wenzelm@55649
    72
  {
wenzelm@55649
    73
    val empty: Markups = new Markups(Map.empty)
wenzelm@55649
    74
wenzelm@55649
    75
    def init(markup: Markup_Tree): Markups =
wenzelm@55649
    76
      new Markups(Map(Markup_Index.markup -> markup))
wenzelm@55649
    77
  }
wenzelm@55649
    78
wenzelm@55649
    79
  final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
wenzelm@55649
    80
  {
wenzelm@56489
    81
    def is_empty: Boolean = rep.isEmpty
wenzelm@56489
    82
wenzelm@55649
    83
    def apply(index: Markup_Index): Markup_Tree =
wenzelm@55649
    84
      rep.getOrElse(index, Markup_Tree.empty)
wenzelm@55649
    85
wenzelm@55649
    86
    def add(index: Markup_Index, markup: Text.Markup): Markups =
wenzelm@55649
    87
      new Markups(rep + (index -> (this(index) + markup)))
wenzelm@55649
    88
wenzelm@56475
    89
    def redirection_iterator: Iterator[Document_ID.Generic] =
wenzelm@56746
    90
      for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
wenzelm@56475
    91
        yield id
wenzelm@56470
    92
wenzelm@56475
    93
    def redirect(other_id: Document_ID.Generic): Markups =
wenzelm@56489
    94
    {
wenzelm@56489
    95
      val rep1 =
wenzelm@56470
    96
        (for {
wenzelm@56746
    97
          (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
wenzelm@56470
    98
          if other_id == id
wenzelm@56746
    99
        } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
wenzelm@56489
   100
      if (rep1.isEmpty) Markups.empty else new Markups(rep1)
wenzelm@56489
   101
    }
wenzelm@56470
   102
wenzelm@55649
   103
    override def hashCode: Int = rep.hashCode
wenzelm@55649
   104
    override def equals(that: Any): Boolean =
wenzelm@55649
   105
      that match {
wenzelm@55649
   106
        case other: Markups => rep == other.rep
wenzelm@55649
   107
        case _ => false
wenzelm@55649
   108
      }
wenzelm@55649
   109
    override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
wenzelm@55649
   110
  }
wenzelm@55649
   111
wenzelm@55649
   112
wenzelm@50507
   113
  /* state */
wenzelm@50501
   114
wenzelm@56299
   115
  object State
wenzelm@56299
   116
  {
wenzelm@56299
   117
    def merge_results(states: List[State]): Command.Results =
wenzelm@56299
   118
      Results.merge(states.map(_.results))
wenzelm@56299
   119
wenzelm@56301
   120
    def merge_markup(states: List[State], index: Markup_Index,
wenzelm@56743
   121
        range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@56301
   122
      Markup_Tree.merge(states.map(_.markup(index)), range, elements)
wenzelm@56299
   123
  }
wenzelm@56299
   124
wenzelm@43714
   125
  sealed case class State(
wenzelm@50501
   126
    command: Command,
wenzelm@50501
   127
    status: List[Markup] = Nil,
wenzelm@50507
   128
    results: Results = Results.empty,
wenzelm@55649
   129
    markups: Markups = Markups.empty)
wenzelm@38361
   130
  {
wenzelm@56395
   131
    lazy val protocol_status: Protocol.Status =
wenzelm@56395
   132
    {
wenzelm@56395
   133
      val warnings =
wenzelm@59203
   134
        if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
wenzelm@56395
   135
          List(Markup(Markup.WARNING, Nil))
wenzelm@56395
   136
        else Nil
wenzelm@56395
   137
      val errors =
wenzelm@56395
   138
        if (results.iterator.exists(p => Protocol.is_error(p._2)))
wenzelm@56395
   139
          List(Markup(Markup.ERROR, Nil))
wenzelm@56395
   140
        else Nil
wenzelm@56395
   141
      Protocol.Status.make((warnings ::: errors ::: status).iterator)
wenzelm@56395
   142
    }
wenzelm@55432
   143
wenzelm@55650
   144
    def markup(index: Markup_Index): Markup_Tree = markups(index)
wenzelm@55432
   145
wenzelm@56489
   146
    def redirect(other_command: Command): Option[State] =
wenzelm@56489
   147
    {
wenzelm@56489
   148
      val markups1 = markups.redirect(other_command.id)
wenzelm@56489
   149
      if (markups1.is_empty) None
wenzelm@56489
   150
      else Some(new State(other_command, Nil, Results.empty, markups1))
wenzelm@56489
   151
    }
wenzelm@49614
   152
wenzelm@51494
   153
    def eq_content(other: State): Boolean =
wenzelm@51494
   154
      command.source == other.command.source &&
wenzelm@51494
   155
      status == other.status &&
wenzelm@51494
   156
      results == other.results &&
wenzelm@55434
   157
      markups == other.markups
wenzelm@38361
   158
wenzelm@55649
   159
    private def add_status(st: Markup): State =
wenzelm@55649
   160
      copy(status = st :: status)
wenzelm@55432
   161
wenzelm@56746
   162
    private def add_markup(
wenzelm@56746
   163
      status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
wenzelm@55649
   164
    {
wenzelm@55649
   165
      val markups1 =
wenzelm@56395
   166
        if (status || Protocol.liberal_status_elements(m.info.name))
wenzelm@56462
   167
          markups.add(Markup_Index(true, chunk_name), m)
wenzelm@55649
   168
        else markups
wenzelm@56462
   169
      copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
wenzelm@55649
   170
    }
wenzelm@38361
   171
wenzelm@56470
   172
    def accumulate(
wenzelm@56470
   173
        self_id: Document_ID.Generic => Boolean,
wenzelm@56746
   174
        other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
wenzelm@56470
   175
        message: XML.Elem): State =
wenzelm@38361
   176
      message match {
wenzelm@50201
   177
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
   178
          (this /: msgs)((state, msg) =>
wenzelm@38714
   179
            msg match {
wenzelm@46152
   180
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@55649
   181
                state.
wenzelm@55649
   182
                  add_status(markup).
wenzelm@56746
   183
                  add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
wenzelm@52536
   184
              case _ =>
wenzelm@56782
   185
                Output.warning("Ignored status message: " + msg)
wenzelm@52536
   186
                state
wenzelm@38714
   187
            })
wenzelm@38581
   188
wenzelm@50201
   189
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   190
          (this /: msgs)((state, msg) =>
wenzelm@55432
   191
            {
wenzelm@56782
   192
              def bad(): Unit = Output.warning("Ignored report message: " + msg)
wenzelm@55432
   193
wenzelm@55432
   194
              msg match {
wenzelm@57911
   195
                case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
wenzelm@56470
   196
wenzelm@56470
   197
                  val target =
wenzelm@56470
   198
                    if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
wenzelm@56470
   199
                      Some((chunk_name, command.chunks(chunk_name)))
wenzelm@56746
   200
                    else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
wenzelm@56470
   201
                    else None
wenzelm@56470
   202
wenzelm@57911
   203
                  (target, atts) match {
wenzelm@57911
   204
                    case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
wenzelm@56470
   205
                      target_chunk.incorporate(symbol_range) match {
wenzelm@55550
   206
                        case Some(range) =>
wenzelm@55822
   207
                          val props = Position.purge(atts)
wenzelm@55822
   208
                          val info = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56470
   209
                          state.add_markup(false, target_name, info)
wenzelm@55550
   210
                        case None => bad(); state
wenzelm@55432
   211
                      }
wenzelm@57911
   212
                    case _ =>
wenzelm@56514
   213
                      // silently ignore excessive reports
wenzelm@56476
   214
                      state
wenzelm@55432
   215
                  }
wenzelm@55432
   216
wenzelm@55432
   217
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   218
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   219
                  val range = command.proper_range
wenzelm@55432
   220
                  val props = Position.purge(atts)
wenzelm@55432
   221
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56746
   222
                  state.add_markup(false, Symbol.Text_Chunk.Default, info)
wenzelm@55432
   223
wenzelm@56470
   224
                case _ => bad(); state
wenzelm@55432
   225
              }
wenzelm@38361
   226
            })
wenzelm@52930
   227
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   228
          props match {
wenzelm@50201
   229
            case Markup.Serial(i) =>
wenzelm@50201
   230
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   231
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   232
wenzelm@55433
   233
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   234
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   235
                for {
wenzelm@56469
   236
                  (chunk_name, chunk) <- command.chunks.iterator
wenzelm@59713
   237
                  range <- Protocol_Message.positions(
wenzelm@59735
   238
                    self_id, command.span.position, chunk_name, chunk, message)
wenzelm@56462
   239
                } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
wenzelm@55433
   240
              }
wenzelm@55433
   241
              st
wenzelm@55432
   242
wenzelm@52536
   243
            case _ =>
wenzelm@56782
   244
              Output.warning("Ignored message without serial number: " + message)
wenzelm@52536
   245
              this
wenzelm@38872
   246
          }
wenzelm@56295
   247
    }
wenzelm@38361
   248
  }
wenzelm@38367
   249
wenzelm@38367
   250
wenzelm@55431
   251
wenzelm@55431
   252
  /** static content **/
wenzelm@55431
   253
wenzelm@45644
   254
  /* make commands */
wenzelm@45644
   255
wenzelm@55648
   256
  def apply(
wenzelm@55648
   257
    id: Document_ID.Command,
wenzelm@55648
   258
    node_name: Document.Node.Name,
wenzelm@59704
   259
    blobs_info: Blobs_Info,
wenzelm@57905
   260
    span: Command_Span.Span): Command =
wenzelm@55648
   261
  {
wenzelm@57901
   262
    val (source, span1) = span.compact_source
wenzelm@59704
   263
    new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
wenzelm@55648
   264
  }
wenzelm@49414
   265
wenzelm@57901
   266
  val empty: Command =
wenzelm@59704
   267
    Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
wenzelm@55648
   268
wenzelm@55648
   269
  def unparsed(
wenzelm@55648
   270
    id: Document_ID.Command,
wenzelm@55648
   271
    source: String,
wenzelm@55648
   272
    results: Results,
wenzelm@55648
   273
    markup: Markup_Tree): Command =
wenzelm@55648
   274
  {
wenzelm@57905
   275
    val (source1, span1) = Command_Span.unparsed(source).compact_source
wenzelm@59704
   276
    new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
wenzelm@55648
   277
  }
wenzelm@49414
   278
wenzelm@50501
   279
  def unparsed(source: String): Command =
wenzelm@52530
   280
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   281
wenzelm@52530
   282
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   283
  {
wenzelm@49466
   284
    val text = XML.content(body)
wenzelm@49466
   285
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   286
    unparsed(id, text, results, markup)
wenzelm@49414
   287
  }
wenzelm@49359
   288
wenzelm@44384
   289
wenzelm@44384
   290
  /* perspective */
wenzelm@44384
   291
wenzelm@44474
   292
  object Perspective
wenzelm@44474
   293
  {
wenzelm@44474
   294
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   295
  }
wenzelm@44385
   296
wenzelm@44474
   297
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   298
  {
wenzelm@57615
   299
    def is_empty: Boolean = commands.isEmpty
wenzelm@57615
   300
wenzelm@44474
   301
    def same(that: Perspective): Boolean =
wenzelm@44474
   302
    {
wenzelm@44474
   303
      val cmds1 = this.commands
wenzelm@44474
   304
      val cmds2 = that.commands
wenzelm@48754
   305
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   306
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   307
      cmds1.length == cmds2.length &&
wenzelm@44474
   308
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   309
    }
wenzelm@44385
   310
  }
wenzelm@59689
   311
wenzelm@59689
   312
wenzelm@59704
   313
  /* blobs: inlined errors and auxiliary files */
wenzelm@59689
   314
wenzelm@59689
   315
  private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
wenzelm@59689
   316
  {
wenzelm@59689
   317
    def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
wenzelm@59689
   318
      toks match {
wenzelm@59689
   319
        case (t1, i1) :: (t2, i2) :: rest =>
wenzelm@59689
   320
          if (t1.is_keyword && (t1.source == "%" || t1.source == "--")) clean(rest)
wenzelm@59689
   321
          else (t1, i1) :: clean((t2, i2) :: rest)
wenzelm@59689
   322
        case _ => toks
wenzelm@59689
   323
      }
wenzelm@59689
   324
    clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
wenzelm@59689
   325
  }
wenzelm@59689
   326
wenzelm@59689
   327
  private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
wenzelm@59924
   328
    if (tokens.exists({ case (t, _) => t.is_command })) {
wenzelm@59924
   329
      tokens.dropWhile({ case (t, _) => !t.is_command }).
wenzelm@59924
   330
        collectFirst({ case (t, i) if t.is_name => (t.content, i) })
wenzelm@59689
   331
    }
wenzelm@59924
   332
    else None
wenzelm@59689
   333
wenzelm@59689
   334
  def span_files(syntax: Prover.Syntax, span: Command_Span.Span): (List[String], Int) =
wenzelm@59735
   335
    syntax.load_command(span.name) match {
wenzelm@59735
   336
      case Some(exts) =>
wenzelm@59735
   337
        find_file(clean_tokens(span.content)) match {
wenzelm@59735
   338
          case Some((file, i)) =>
wenzelm@59735
   339
            if (exts.isEmpty) (List(file), i)
wenzelm@59735
   340
            else (exts.map(ext => file + "." + ext), i)
wenzelm@59689
   341
          case None => (Nil, -1)
wenzelm@59689
   342
        }
wenzelm@59735
   343
      case None => (Nil, -1)
wenzelm@59689
   344
    }
wenzelm@59689
   345
wenzelm@59704
   346
  def blobs_info(
wenzelm@59701
   347
    resources: Resources,
wenzelm@59701
   348
    syntax: Prover.Syntax,
wenzelm@59701
   349
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@59704
   350
    can_import: Document.Node.Name => Boolean,
wenzelm@59701
   351
    node_name: Document.Node.Name,
wenzelm@59704
   352
    span: Command_Span.Span): Blobs_Info =
wenzelm@59689
   353
  {
wenzelm@59735
   354
    span.name match {
wenzelm@59704
   355
      // inlined errors
wenzelm@59735
   356
      case Thy_Header.THEORY =>
wenzelm@59707
   357
        val header =
wenzelm@59707
   358
          resources.check_thy_reader("", node_name,
wenzelm@59735
   359
            new CharSequenceReader(Token.implode(span.content)), Token.Pos.command)
wenzelm@59715
   360
        val errors =
wenzelm@59710
   361
          for ((imp, pos) <- header.imports if !can_import(imp)) yield {
wenzelm@59715
   362
            val msg =
wenzelm@59715
   363
              "Bad theory import " +
wenzelm@59715
   364
                Markup.Path(imp.node).markup(quote(imp.toString)) + Position.here(pos)
wenzelm@59715
   365
            Exn.Exn(ERROR(msg)): Command.Blob
wenzelm@59710
   366
          }
wenzelm@59715
   367
        (errors, -1)
wenzelm@59704
   368
wenzelm@59704
   369
      // auxiliary files
wenzelm@59704
   370
      case _ =>
wenzelm@59704
   371
        val (files, index) = span_files(syntax, span)
wenzelm@59704
   372
        val blobs =
wenzelm@59704
   373
          files.map(file =>
wenzelm@59704
   374
            (Exn.capture {
wenzelm@59704
   375
              val name =
wenzelm@59704
   376
                Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file)))
wenzelm@59704
   377
              val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
wenzelm@59704
   378
              (name, blob)
wenzelm@59704
   379
            }).user_error)
wenzelm@59704
   380
        (blobs, index)
wenzelm@59704
   381
    }
wenzelm@59689
   382
  }
wenzelm@34318
   383
}
wenzelm@34318
   384
wenzelm@38361
   385
wenzelm@46712
   386
final class Command private(
wenzelm@52530
   387
    val id: Document_ID.Command,
wenzelm@44615
   388
    val node_name: Document.Node.Name,
wenzelm@59704
   389
    val blobs_info: Command.Blobs_Info,
wenzelm@57905
   390
    val span: Command_Span.Span,
wenzelm@49414
   391
    val source: String,
wenzelm@50501
   392
    val init_results: Command.Results,
wenzelm@49414
   393
    val init_markup: Markup_Tree)
wenzelm@34451
   394
{
wenzelm@57912
   395
  override def toString: String = id + "/" + span.kind.toString
wenzelm@34495
   396
wenzelm@57910
   397
wenzelm@57910
   398
  /* classification */
wenzelm@57910
   399
wenzelm@57905
   400
  def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
wenzelm@57905
   401
  def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
wenzelm@57904
   402
wenzelm@57904
   403
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@57904
   404
  val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
wenzelm@57904
   405
  val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
wenzelm@57904
   406
wenzelm@34859
   407
wenzelm@54519
   408
  /* blobs */
wenzelm@54519
   409
wenzelm@59704
   410
  def blobs: List[Command.Blob] = blobs_info._1
wenzelm@59704
   411
  def blobs_index: Int = blobs_info._2
wenzelm@59704
   412
wenzelm@54530
   413
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   414
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   415
wenzelm@60916
   416
  def blobs_undefined: List[Document.Node.Name] =
wenzelm@60916
   417
    for (Exn.Res((name, None)) <- blobs) yield name
wenzelm@60916
   418
wenzelm@57842
   419
  def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
wenzelm@57842
   420
    for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
wenzelm@57842
   421
wenzelm@57842
   422
  def blobs_changed(doc_blobs: Document.Blobs): Boolean =
wenzelm@57842
   423
    blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
wenzelm@54519
   424
wenzelm@55432
   425
wenzelm@56462
   426
  /* source chunks */
wenzelm@55431
   427
wenzelm@56746
   428
  val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
wenzelm@56473
   429
wenzelm@56746
   430
  val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
wenzelm@56746
   431
    ((Symbol.Text_Chunk.Default -> chunk) ::
wenzelm@56473
   432
      (for (Exn.Res((name, Some((_, file)))) <- blobs)
wenzelm@60215
   433
        yield Symbol.Text_Chunk.File(name.node) -> file)).toMap
wenzelm@56473
   434
wenzelm@46813
   435
  def length: Int = source.length
wenzelm@56473
   436
  def range: Text.Range = chunk.range
wenzelm@46813
   437
wenzelm@46813
   438
  val proper_range: Text.Range =
wenzelm@57901
   439
    Text.Range(0,
wenzelm@57901
   440
      (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   441
wenzelm@38426
   442
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   443
wenzelm@38370
   444
wenzelm@38370
   445
  /* accumulated results */
wenzelm@38370
   446
wenzelm@50501
   447
  val init_state: Command.State =
wenzelm@55649
   448
    Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
wenzelm@52527
   449
wenzelm@52527
   450
  val empty_state: Command.State = Command.State(this)
immler@34676
   451
}