src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Wed Apr 30 22:34:11 2014 +0200 (2014-04-30)
changeset 56801 8dd9df88f647
parent 56666 229309cbc508
child 56823 37be55461dbe
permissions -rw-r--r--
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm@56208
     1
/*  Title:      Tools/jEdit/src/jedit_resources.scala
wenzelm@44577
     2
    Author:     Makarius
wenzelm@44577
     3
wenzelm@56208
     4
Resources for theories and auxiliary files, based on jEdit buffer
wenzelm@56208
     5
content and virtual file-systems.
wenzelm@44577
     6
*/
wenzelm@44577
     7
wenzelm@44577
     8
package isabelle.jedit
wenzelm@44577
     9
wenzelm@44577
    10
wenzelm@44577
    11
import isabelle._
wenzelm@44577
    12
wenzelm@56502
    13
import java.io.{File => JFile, ByteArrayOutputStream}
wenzelm@44577
    14
import javax.swing.text.Segment
wenzelm@44577
    15
wenzelm@56502
    16
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
wenzelm@44577
    17
import org.gjt.sp.jedit.MiscUtilities
wenzelm@55134
    18
import org.gjt.sp.jedit.{jEdit, View, Buffer}
wenzelm@54511
    19
import org.gjt.sp.jedit.bufferio.BufferIORequest
wenzelm@44577
    20
wenzelm@56208
    21
wenzelm@56801
    22
class JEdit_Resources(
wenzelm@56801
    23
    loaded_theories: Set[String],
wenzelm@56801
    24
    known_theories: Map[String, Document.Node.Name],
wenzelm@56801
    25
    base_syntax: Outer_Syntax)
wenzelm@56801
    26
  extends Resources(loaded_theories, known_theories, base_syntax)
wenzelm@44577
    27
{
wenzelm@50566
    28
  /* document node names */
wenzelm@50566
    29
wenzelm@54509
    30
  def node_name(buffer: Buffer): Document.Node.Name =
wenzelm@50566
    31
  {
wenzelm@54515
    32
    val node = JEdit_Lib.buffer_name(buffer)
wenzelm@54515
    33
    val theory = Thy_Header.thy_name(node).getOrElse("")
wenzelm@54515
    34
    val master_dir = if (theory == "") "" else buffer.getDirectory
wenzelm@54515
    35
    Document.Node.Name(node, master_dir, theory)
wenzelm@54509
    36
  }
wenzelm@54509
    37
wenzelm@54509
    38
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@54509
    39
  {
wenzelm@54509
    40
    val name = node_name(buffer)
wenzelm@54509
    41
    if (name.is_theory) Some(name) else None
wenzelm@50566
    42
  }
wenzelm@50566
    43
wenzelm@50566
    44
wenzelm@50566
    45
  /* file-system operations */
wenzelm@50566
    46
wenzelm@44615
    47
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    48
  {
wenzelm@44577
    49
    val path = source_path.expand
wenzelm@54517
    50
    if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@56502
    51
    else if (path.is_current) dir
wenzelm@44577
    52
    else {
wenzelm@44615
    53
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    54
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    55
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    56
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    57
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    58
    }
wenzelm@44577
    59
  }
wenzelm@44577
    60
wenzelm@56502
    61
  override def with_thy_text[A](name: Document.Node.Name, consume: CharSequence => A): A =
wenzelm@48885
    62
  {
wenzelm@48885
    63
    Swing_Thread.now {
wenzelm@56457
    64
      JEdit_Lib.jedit_buffer(name) match {
wenzelm@48885
    65
        case Some(buffer) =>
wenzelm@49406
    66
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@56502
    67
            Some(consume(buffer.getSegment(0, buffer.getLength)))
wenzelm@48885
    68
          }
wenzelm@48885
    69
        case None => None
wenzelm@48885
    70
      }
wenzelm@48885
    71
    } getOrElse {
wenzelm@56502
    72
      if (Url.is_wellformed(name.node)) consume(Url.read(name.node))
wenzelm@56502
    73
      else {
wenzelm@56502
    74
        val file = new JFile(name.node)
wenzelm@56502
    75
        if (file.isFile) consume(File.read(file))
wenzelm@56502
    76
        else error("No such file: " + quote(file.toString))
wenzelm@56502
    77
      }
wenzelm@48885
    78
    }
wenzelm@48885
    79
  }
wenzelm@48885
    80
wenzelm@56502
    81
  def check_file(view: View, file: String): Boolean =
wenzelm@44963
    82
    try {
wenzelm@56502
    83
      if (Url.is_wellformed(file)) Url.is_readable(file)
wenzelm@56502
    84
      else (new JFile(file)).isFile
wenzelm@44963
    85
    }
wenzelm@56502
    86
    catch { case ERROR(_) => false }
wenzelm@54511
    87
wenzelm@54511
    88
wenzelm@54511
    89
  /* file content */
wenzelm@54511
    90
wenzelm@56666
    91
  private class File_Content_Output(buffer: Buffer) extends
wenzelm@56666
    92
    ByteArrayOutputStream(buffer.getLength + 1)
wenzelm@56666
    93
  {
wenzelm@56666
    94
    def content(): Bytes = Bytes(this.buf, 0, this.count)
wenzelm@56666
    95
  }
wenzelm@56666
    96
wenzelm@56666
    97
  private class File_Content(buffer: Buffer) extends
wenzelm@56666
    98
    BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
wenzelm@54511
    99
  {
wenzelm@56666
   100
    def _run() { }
wenzelm@56666
   101
    def content(): Bytes =
wenzelm@56666
   102
    {
wenzelm@56666
   103
      val out = new File_Content_Output(buffer)
wenzelm@56666
   104
      write(buffer, out)
wenzelm@56666
   105
      out.content()
wenzelm@56666
   106
    }
wenzelm@54511
   107
  }
wenzelm@55134
   108
wenzelm@56666
   109
  def file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
wenzelm@56666
   110
wenzelm@55134
   111
wenzelm@55134
   112
  /* theory text edits */
wenzelm@55134
   113
wenzelm@56316
   114
  override def commit(change: Session.Change)
wenzelm@56316
   115
  {
wenzelm@56316
   116
    if (change.syntax_changed) Swing_Thread.later { jEdit.propertiesChanged() }
wenzelm@56316
   117
  }
wenzelm@44577
   118
}
wenzelm@44577
   119