src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Sun Jul 22 00:00:22 2012 +0200 (2012-07-22)
changeset 48422 9613780a805b
parent 48409 0d2114eb412a
child 48870 4accee106f0f
permissions -rw-r--r--
determine source dependencies, relatively to preloaded theories;
tuned signature;
     1 /*  Title:      Tools/jEdit/src/jedit_thy_load.scala
     2     Author:     Makarius
     3 
     4 Primitives for loading theory files, based on jEdit buffer content.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.io.{File => JFile, IOException}
    13 import javax.swing.text.Segment
    14 
    15 import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSFile, VFSManager}
    16 import org.gjt.sp.jedit.MiscUtilities
    17 import org.gjt.sp.jedit.View
    18 
    19 
    20 class JEdit_Thy_Load extends Thy_Load()
    21 {
    22   override def append(dir: String, source_path: Path): String =
    23   {
    24     val path = source_path.expand
    25     if (path.is_absolute) Isabelle_System.platform_path(path)
    26     else {
    27       val vfs = VFSManager.getVFSForPath(dir)
    28       if (vfs.isInstanceOf[FileVFS])
    29         MiscUtilities.resolveSymlinks(
    30           vfs.constructPath(dir, Isabelle_System.platform_path(path)))
    31       else vfs.constructPath(dir, Isabelle_System.standard_path(path))
    32     }
    33   }
    34 
    35   def check_file(view: View, path: String): Boolean =
    36   {
    37     val vfs = VFSManager.getVFSForPath(path)
    38     val session = vfs.createVFSSession(path, view)
    39 
    40     try {
    41       session != null && {
    42         try {
    43           val file = vfs._getFile(session, path, view)
    44           file != null && file.isReadable && file.getType == VFSFile.FILE
    45         }
    46         catch { case _: IOException => false }
    47       }
    48     }
    49     finally {
    50       try { vfs._endVFSSession(session, view) }
    51       catch { case _: IOException => }
    52     }
    53   }
    54 
    55   override def read_header(name: Document.Node.Name): Thy_Header =
    56   {
    57     Swing_Thread.now {
    58       Isabelle.jedit_buffer(name.node) match {
    59         case Some(buffer) =>
    60           Isabelle.buffer_lock(buffer) {
    61             val text = new Segment
    62             buffer.getText(0, buffer.getLength, text)
    63             Some(Thy_Header.read(text))
    64           }
    65         case None => None
    66       }
    67     } getOrElse {
    68       val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
    69       if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
    70       Thy_Header.read(file)
    71     }
    72   }
    73 }
    74