src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Wed Apr 09 23:22:58 2014 +0200 (2014-04-09)
changeset 56502 db2836f65d42
parent 56460 af28fdd50690
child 56666 229309cbc508
permissions -rw-r--r--
improved support for external URLs, based on standard Java network operations;
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@56208
    22
class JEdit_Resources(loaded_theories: Set[String] = Set.empty, base_syntax: Outer_Syntax)
wenzelm@56208
    23
  extends Resources(loaded_theories, base_syntax)
wenzelm@44577
    24
{
wenzelm@50566
    25
  /* document node names */
wenzelm@50566
    26
wenzelm@54509
    27
  def node_name(buffer: Buffer): Document.Node.Name =
wenzelm@50566
    28
  {
wenzelm@54515
    29
    val node = JEdit_Lib.buffer_name(buffer)
wenzelm@54515
    30
    val theory = Thy_Header.thy_name(node).getOrElse("")
wenzelm@54515
    31
    val master_dir = if (theory == "") "" else buffer.getDirectory
wenzelm@54515
    32
    Document.Node.Name(node, master_dir, theory)
wenzelm@54509
    33
  }
wenzelm@54509
    34
wenzelm@54509
    35
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@54509
    36
  {
wenzelm@54509
    37
    val name = node_name(buffer)
wenzelm@54509
    38
    if (name.is_theory) Some(name) else None
wenzelm@50566
    39
  }
wenzelm@50566
    40
wenzelm@50566
    41
wenzelm@50566
    42
  /* file-system operations */
wenzelm@50566
    43
wenzelm@44615
    44
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    45
  {
wenzelm@44577
    46
    val path = source_path.expand
wenzelm@54517
    47
    if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@56502
    48
    else if (path.is_current) dir
wenzelm@44577
    49
    else {
wenzelm@44615
    50
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    51
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    52
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    53
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    54
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    55
    }
wenzelm@44577
    56
  }
wenzelm@44577
    57
wenzelm@56502
    58
  override def with_thy_text[A](name: Document.Node.Name, consume: CharSequence => A): A =
wenzelm@48885
    59
  {
wenzelm@48885
    60
    Swing_Thread.now {
wenzelm@56457
    61
      JEdit_Lib.jedit_buffer(name) match {
wenzelm@48885
    62
        case Some(buffer) =>
wenzelm@49406
    63
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@56502
    64
            Some(consume(buffer.getSegment(0, buffer.getLength)))
wenzelm@48885
    65
          }
wenzelm@48885
    66
        case None => None
wenzelm@48885
    67
      }
wenzelm@48885
    68
    } getOrElse {
wenzelm@56502
    69
      if (Url.is_wellformed(name.node)) consume(Url.read(name.node))
wenzelm@56502
    70
      else {
wenzelm@56502
    71
        val file = new JFile(name.node)
wenzelm@56502
    72
        if (file.isFile) consume(File.read(file))
wenzelm@56502
    73
        else error("No such file: " + quote(file.toString))
wenzelm@56502
    74
      }
wenzelm@48885
    75
    }
wenzelm@48885
    76
  }
wenzelm@48885
    77
wenzelm@56502
    78
  def check_file(view: View, file: String): Boolean =
wenzelm@44963
    79
    try {
wenzelm@56502
    80
      if (Url.is_wellformed(file)) Url.is_readable(file)
wenzelm@56502
    81
      else (new JFile(file)).isFile
wenzelm@44963
    82
    }
wenzelm@56502
    83
    catch { case ERROR(_) => false }
wenzelm@54511
    84
wenzelm@54511
    85
wenzelm@54511
    86
  /* file content */
wenzelm@54511
    87
wenzelm@54511
    88
  def file_content(buffer: Buffer): Bytes =
wenzelm@54511
    89
  {
wenzelm@54511
    90
    val path = buffer.getPath
wenzelm@54511
    91
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@54511
    92
    val content =
wenzelm@54511
    93
      new BufferIORequest(null, buffer, null, vfs, path) {
wenzelm@54511
    94
        def _run() { }
wenzelm@54511
    95
        def apply(): Bytes =
wenzelm@54511
    96
        {
wenzelm@54511
    97
          val out =
wenzelm@54511
    98
            new ByteArrayOutputStream(buffer.getLength + 1) {
wenzelm@54511
    99
              def content(): Bytes = Bytes(this.buf, 0, this.count)
wenzelm@54511
   100
            }
wenzelm@54511
   101
          write(buffer, out)
wenzelm@54511
   102
          out.content()
wenzelm@54511
   103
        }
wenzelm@54511
   104
      }
wenzelm@54511
   105
    content()
wenzelm@54511
   106
  }
wenzelm@55134
   107
wenzelm@55134
   108
wenzelm@55134
   109
  /* theory text edits */
wenzelm@55134
   110
wenzelm@56316
   111
  override def commit(change: Session.Change)
wenzelm@56316
   112
  {
wenzelm@56316
   113
    if (change.syntax_changed) Swing_Thread.later { jEdit.propertiesChanged() }
wenzelm@56316
   114
  }
wenzelm@44577
   115
}
wenzelm@44577
   116