src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23)
changeset 57612 990ffb84489b
parent 56823 37be55461dbe
child 57622 2da79fca5708
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
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@56823
    16
import scala.util.parsing.input.Reader
wenzelm@56823
    17
wenzelm@56502
    18
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
wenzelm@44577
    19
import org.gjt.sp.jedit.MiscUtilities
wenzelm@55134
    20
import org.gjt.sp.jedit.{jEdit, View, Buffer}
wenzelm@54511
    21
import org.gjt.sp.jedit.bufferio.BufferIORequest
wenzelm@44577
    22
wenzelm@56208
    23
wenzelm@56801
    24
class JEdit_Resources(
wenzelm@56801
    25
    loaded_theories: Set[String],
wenzelm@56801
    26
    known_theories: Map[String, Document.Node.Name],
wenzelm@56801
    27
    base_syntax: Outer_Syntax)
wenzelm@56801
    28
  extends Resources(loaded_theories, known_theories, base_syntax)
wenzelm@44577
    29
{
wenzelm@50566
    30
  /* document node names */
wenzelm@50566
    31
wenzelm@54509
    32
  def node_name(buffer: Buffer): Document.Node.Name =
wenzelm@50566
    33
  {
wenzelm@54515
    34
    val node = JEdit_Lib.buffer_name(buffer)
wenzelm@54515
    35
    val theory = Thy_Header.thy_name(node).getOrElse("")
wenzelm@54515
    36
    val master_dir = if (theory == "") "" else buffer.getDirectory
wenzelm@54515
    37
    Document.Node.Name(node, master_dir, theory)
wenzelm@54509
    38
  }
wenzelm@54509
    39
wenzelm@54509
    40
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@54509
    41
  {
wenzelm@54509
    42
    val name = node_name(buffer)
wenzelm@54509
    43
    if (name.is_theory) Some(name) else None
wenzelm@50566
    44
  }
wenzelm@50566
    45
wenzelm@50566
    46
wenzelm@50566
    47
  /* file-system operations */
wenzelm@50566
    48
wenzelm@44615
    49
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    50
  {
wenzelm@44577
    51
    val path = source_path.expand
wenzelm@54517
    52
    if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@56502
    53
    else if (path.is_current) dir
wenzelm@44577
    54
    else {
wenzelm@44615
    55
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    56
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    57
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    58
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    59
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    60
    }
wenzelm@44577
    61
  }
wenzelm@44577
    62
wenzelm@56823
    63
  override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@48885
    64
  {
wenzelm@57612
    65
    GUI_Thread.now {
wenzelm@56457
    66
      JEdit_Lib.jedit_buffer(name) match {
wenzelm@48885
    67
        case Some(buffer) =>
wenzelm@56823
    68
          JEdit_Lib.buffer_lock(buffer) { Some(f(JEdit_Lib.buffer_reader(buffer))) }
wenzelm@48885
    69
        case None => None
wenzelm@48885
    70
      }
wenzelm@48885
    71
    } getOrElse {
wenzelm@56823
    72
      if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
wenzelm@56502
    73
      else {
wenzelm@56502
    74
        val file = new JFile(name.node)
wenzelm@56823
    75
        if (file.isFile) f(Scan.byte_reader(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@57612
   116
    if (change.syntax_changed) GUI_Thread.later { jEdit.propertiesChanged() }
wenzelm@56316
   117
  }
wenzelm@44577
   118
}
wenzelm@44577
   119