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