src/Pure/PIDE/command.scala
author wenzelm
Tue Aug 02 18:45:34 2016 +0200 (2016-08-02 ago)
changeset 63584 68751fe1c036
parent 62969 9f394a16c557
child 64471 c40c2975fb02
permissions -rw-r--r--
tuned signature -- prover-independence is presently theoretical;
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@55649
   153
    private def add_status(st: Markup): State =
wenzelm@55649
   154
      copy(status = st :: status)
wenzelm@55432
   155
wenzelm@56746
   156
    private def add_markup(
wenzelm@56746
   157
      status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
wenzelm@55649
   158
    {
wenzelm@55649
   159
      val markups1 =
wenzelm@56395
   160
        if (status || Protocol.liberal_status_elements(m.info.name))
wenzelm@56462
   161
          markups.add(Markup_Index(true, chunk_name), m)
wenzelm@55649
   162
        else markups
wenzelm@56462
   163
      copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
wenzelm@55649
   164
    }
wenzelm@38361
   165
wenzelm@56470
   166
    def accumulate(
wenzelm@56470
   167
        self_id: Document_ID.Generic => Boolean,
wenzelm@56746
   168
        other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
wenzelm@56470
   169
        message: XML.Elem): State =
wenzelm@38361
   170
      message match {
wenzelm@50201
   171
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
   172
          (this /: msgs)((state, msg) =>
wenzelm@38714
   173
            msg match {
wenzelm@46152
   174
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@55649
   175
                state.
wenzelm@55649
   176
                  add_status(markup).
wenzelm@56746
   177
                  add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
wenzelm@52536
   178
              case _ =>
wenzelm@56782
   179
                Output.warning("Ignored status message: " + msg)
wenzelm@52536
   180
                state
wenzelm@38714
   181
            })
wenzelm@38581
   182
wenzelm@50201
   183
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   184
          (this /: msgs)((state, msg) =>
wenzelm@55432
   185
            {
wenzelm@56782
   186
              def bad(): Unit = Output.warning("Ignored report message: " + msg)
wenzelm@55432
   187
wenzelm@55432
   188
              msg match {
wenzelm@57911
   189
                case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
wenzelm@56470
   190
wenzelm@56470
   191
                  val target =
wenzelm@56470
   192
                    if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
wenzelm@56470
   193
                      Some((chunk_name, command.chunks(chunk_name)))
wenzelm@56746
   194
                    else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
wenzelm@56470
   195
                    else None
wenzelm@56470
   196
wenzelm@57911
   197
                  (target, atts) match {
wenzelm@57911
   198
                    case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
wenzelm@56470
   199
                      target_chunk.incorporate(symbol_range) match {
wenzelm@55550
   200
                        case Some(range) =>
wenzelm@55822
   201
                          val props = Position.purge(atts)
wenzelm@55822
   202
                          val info = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56470
   203
                          state.add_markup(false, target_name, info)
wenzelm@55550
   204
                        case None => bad(); state
wenzelm@55432
   205
                      }
wenzelm@57911
   206
                    case _ =>
wenzelm@56514
   207
                      // silently ignore excessive reports
wenzelm@56476
   208
                      state
wenzelm@55432
   209
                  }
wenzelm@55432
   210
wenzelm@55432
   211
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   212
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   213
                  val range = command.proper_range
wenzelm@55432
   214
                  val props = Position.purge(atts)
wenzelm@55432
   215
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56746
   216
                  state.add_markup(false, Symbol.Text_Chunk.Default, info)
wenzelm@55432
   217
wenzelm@56470
   218
                case _ => bad(); state
wenzelm@55432
   219
              }
wenzelm@38361
   220
            })
wenzelm@52930
   221
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   222
          props match {
wenzelm@50201
   223
            case Markup.Serial(i) =>
wenzelm@50201
   224
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   225
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   226
wenzelm@55433
   227
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   228
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   229
                for {
wenzelm@56469
   230
                  (chunk_name, chunk) <- command.chunks.iterator
wenzelm@59713
   231
                  range <- Protocol_Message.positions(
wenzelm@59735
   232
                    self_id, command.span.position, chunk_name, chunk, message)
wenzelm@56462
   233
                } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
wenzelm@55433
   234
              }
wenzelm@55433
   235
              st
wenzelm@55432
   236
wenzelm@52536
   237
            case _ =>
wenzelm@56782
   238
              Output.warning("Ignored message without serial number: " + message)
wenzelm@52536
   239
              this
wenzelm@38872
   240
          }
wenzelm@56295
   241
    }
wenzelm@38361
   242
  }
wenzelm@38367
   243
wenzelm@38367
   244
wenzelm@55431
   245
wenzelm@55431
   246
  /** static content **/
wenzelm@55431
   247
wenzelm@45644
   248
  /* make commands */
wenzelm@45644
   249
wenzelm@55648
   250
  def apply(
wenzelm@55648
   251
    id: Document_ID.Command,
wenzelm@55648
   252
    node_name: Document.Node.Name,
wenzelm@59704
   253
    blobs_info: Blobs_Info,
wenzelm@57905
   254
    span: Command_Span.Span): Command =
wenzelm@55648
   255
  {
wenzelm@57901
   256
    val (source, span1) = span.compact_source
wenzelm@59704
   257
    new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
wenzelm@55648
   258
  }
wenzelm@49414
   259
wenzelm@57901
   260
  val empty: Command =
wenzelm@59704
   261
    Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
wenzelm@55648
   262
wenzelm@55648
   263
  def unparsed(
wenzelm@55648
   264
    id: Document_ID.Command,
wenzelm@55648
   265
    source: String,
wenzelm@55648
   266
    results: Results,
wenzelm@55648
   267
    markup: Markup_Tree): Command =
wenzelm@55648
   268
  {
wenzelm@57905
   269
    val (source1, span1) = Command_Span.unparsed(source).compact_source
wenzelm@59704
   270
    new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
wenzelm@55648
   271
  }
wenzelm@49414
   272
wenzelm@50501
   273
  def unparsed(source: String): Command =
wenzelm@52530
   274
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   275
wenzelm@52530
   276
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   277
  {
wenzelm@49466
   278
    val text = XML.content(body)
wenzelm@49466
   279
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   280
    unparsed(id, text, results, markup)
wenzelm@49414
   281
  }
wenzelm@49359
   282
wenzelm@44384
   283
wenzelm@44384
   284
  /* perspective */
wenzelm@44384
   285
wenzelm@44474
   286
  object Perspective
wenzelm@44474
   287
  {
wenzelm@44474
   288
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   289
  }
wenzelm@44385
   290
wenzelm@44474
   291
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   292
  {
wenzelm@57615
   293
    def is_empty: Boolean = commands.isEmpty
wenzelm@57615
   294
wenzelm@44474
   295
    def same(that: Perspective): Boolean =
wenzelm@44474
   296
    {
wenzelm@44474
   297
      val cmds1 = this.commands
wenzelm@44474
   298
      val cmds2 = that.commands
wenzelm@48754
   299
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   300
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   301
      cmds1.length == cmds2.length &&
wenzelm@44474
   302
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   303
    }
wenzelm@44385
   304
  }
wenzelm@59689
   305
wenzelm@59689
   306
wenzelm@59704
   307
  /* blobs: inlined errors and auxiliary files */
wenzelm@59689
   308
wenzelm@59689
   309
  private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
wenzelm@59689
   310
  {
wenzelm@61579
   311
    val markers = Set("%", "--", Symbol.comment, Symbol.comment_decoded)
wenzelm@59689
   312
    def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
wenzelm@59689
   313
      toks match {
wenzelm@59689
   314
        case (t1, i1) :: (t2, i2) :: rest =>
wenzelm@61579
   315
          if (t1.is_keyword && markers(t1.source)) clean(rest)
wenzelm@59689
   316
          else (t1, i1) :: clean((t2, i2) :: rest)
wenzelm@59689
   317
        case _ => toks
wenzelm@59689
   318
      }
wenzelm@59689
   319
    clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
wenzelm@59689
   320
  }
wenzelm@59689
   321
wenzelm@59689
   322
  private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
wenzelm@59924
   323
    if (tokens.exists({ case (t, _) => t.is_command })) {
wenzelm@59924
   324
      tokens.dropWhile({ case (t, _) => !t.is_command }).
wenzelm@62969
   325
        collectFirst({ case (t, i) if t.is_name => (t.content, i) })
wenzelm@59689
   326
    }
wenzelm@59924
   327
    else None
wenzelm@59689
   328
wenzelm@63584
   329
  def span_files(syntax: Outer_Syntax, span: Command_Span.Span): (List[String], Int) =
wenzelm@59735
   330
    syntax.load_command(span.name) match {
wenzelm@59735
   331
      case Some(exts) =>
wenzelm@59735
   332
        find_file(clean_tokens(span.content)) match {
wenzelm@59735
   333
          case Some((file, i)) =>
wenzelm@59735
   334
            if (exts.isEmpty) (List(file), i)
wenzelm@59735
   335
            else (exts.map(ext => file + "." + ext), i)
wenzelm@59689
   336
          case None => (Nil, -1)
wenzelm@59689
   337
        }
wenzelm@59735
   338
      case None => (Nil, -1)
wenzelm@59689
   339
    }
wenzelm@59689
   340
wenzelm@59704
   341
  def blobs_info(
wenzelm@59701
   342
    resources: Resources,
wenzelm@63584
   343
    syntax: Outer_Syntax,
wenzelm@59701
   344
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@59704
   345
    can_import: Document.Node.Name => Boolean,
wenzelm@59701
   346
    node_name: Document.Node.Name,
wenzelm@59704
   347
    span: Command_Span.Span): Blobs_Info =
wenzelm@59689
   348
  {
wenzelm@59735
   349
    span.name match {
wenzelm@59704
   350
      // inlined errors
wenzelm@59735
   351
      case Thy_Header.THEORY =>
wenzelm@59707
   352
        val header =
wenzelm@59707
   353
          resources.check_thy_reader("", node_name,
wenzelm@59735
   354
            new CharSequenceReader(Token.implode(span.content)), Token.Pos.command)
wenzelm@59715
   355
        val errors =
wenzelm@59710
   356
          for ((imp, pos) <- header.imports if !can_import(imp)) yield {
wenzelm@59715
   357
            val msg =
wenzelm@59715
   358
              "Bad theory import " +
wenzelm@59715
   359
                Markup.Path(imp.node).markup(quote(imp.toString)) + Position.here(pos)
wenzelm@59715
   360
            Exn.Exn(ERROR(msg)): Command.Blob
wenzelm@59710
   361
          }
wenzelm@59715
   362
        (errors, -1)
wenzelm@59704
   363
wenzelm@59704
   364
      // auxiliary files
wenzelm@59704
   365
      case _ =>
wenzelm@59704
   366
        val (files, index) = span_files(syntax, span)
wenzelm@59704
   367
        val blobs =
wenzelm@59704
   368
          files.map(file =>
wenzelm@59704
   369
            (Exn.capture {
wenzelm@59704
   370
              val name =
wenzelm@59704
   371
                Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file)))
wenzelm@59704
   372
              val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
wenzelm@59704
   373
              (name, blob)
wenzelm@59704
   374
            }).user_error)
wenzelm@59704
   375
        (blobs, index)
wenzelm@59704
   376
    }
wenzelm@59689
   377
  }
wenzelm@34318
   378
}
wenzelm@34318
   379
wenzelm@38361
   380
wenzelm@46712
   381
final class Command private(
wenzelm@52530
   382
    val id: Document_ID.Command,
wenzelm@44615
   383
    val node_name: Document.Node.Name,
wenzelm@59704
   384
    val blobs_info: Command.Blobs_Info,
wenzelm@57905
   385
    val span: Command_Span.Span,
wenzelm@49414
   386
    val source: String,
wenzelm@50501
   387
    val init_results: Command.Results,
wenzelm@49414
   388
    val init_markup: Markup_Tree)
wenzelm@34451
   389
{
wenzelm@57912
   390
  override def toString: String = id + "/" + span.kind.toString
wenzelm@34495
   391
wenzelm@57910
   392
wenzelm@57910
   393
  /* classification */
wenzelm@57910
   394
wenzelm@57905
   395
  def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
wenzelm@57905
   396
  def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
wenzelm@57904
   397
wenzelm@57904
   398
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@57904
   399
  val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
wenzelm@57904
   400
  val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
wenzelm@57904
   401
wenzelm@34859
   402
wenzelm@54519
   403
  /* blobs */
wenzelm@54519
   404
wenzelm@59704
   405
  def blobs: List[Command.Blob] = blobs_info._1
wenzelm@59704
   406
  def blobs_index: Int = blobs_info._2
wenzelm@59704
   407
wenzelm@54530
   408
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   409
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   410
wenzelm@60916
   411
  def blobs_undefined: List[Document.Node.Name] =
wenzelm@60916
   412
    for (Exn.Res((name, None)) <- blobs) yield name
wenzelm@60916
   413
wenzelm@57842
   414
  def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
wenzelm@57842
   415
    for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
wenzelm@57842
   416
wenzelm@57842
   417
  def blobs_changed(doc_blobs: Document.Blobs): Boolean =
wenzelm@57842
   418
    blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
wenzelm@54519
   419
wenzelm@55432
   420
wenzelm@56462
   421
  /* source chunks */
wenzelm@55431
   422
wenzelm@56746
   423
  val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
wenzelm@56473
   424
wenzelm@56746
   425
  val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
wenzelm@56746
   426
    ((Symbol.Text_Chunk.Default -> chunk) ::
wenzelm@56473
   427
      (for (Exn.Res((name, Some((_, file)))) <- blobs)
wenzelm@60215
   428
        yield Symbol.Text_Chunk.File(name.node) -> file)).toMap
wenzelm@56473
   429
wenzelm@46813
   430
  def length: Int = source.length
wenzelm@56473
   431
  def range: Text.Range = chunk.range
wenzelm@46813
   432
wenzelm@46813
   433
  val proper_range: Text.Range =
wenzelm@57901
   434
    Text.Range(0,
wenzelm@57901
   435
      (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   436
wenzelm@38426
   437
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   438
wenzelm@38370
   439
wenzelm@38370
   440
  /* accumulated results */
wenzelm@38370
   441
wenzelm@50501
   442
  val init_state: Command.State =
wenzelm@55649
   443
    Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
wenzelm@52527
   444
wenzelm@52527
   445
  val empty_state: Command.State = Command.State(this)
immler@34676
   446
}