src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Tue Aug 21 12:15:25 2012 +0200 (2012-08-21)
changeset 48870 4accee106f0f
parent 48422 9613780a805b
child 48882 61dc7d5d150a
permissions -rw-r--r--
clarified initialization of Thy_Load, Thy_Info, Session;
     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(loaded_theories: Set[String] = Set.empty, base_syntax: Outer_Syntax)
    21   extends Thy_Load(loaded_theories, base_syntax)
    22 {
    23   override def append(dir: String, source_path: Path): String =
    24   {
    25     val path = source_path.expand
    26     if (path.is_absolute) Isabelle_System.platform_path(path)
    27     else {
    28       val vfs = VFSManager.getVFSForPath(dir)
    29       if (vfs.isInstanceOf[FileVFS])
    30         MiscUtilities.resolveSymlinks(
    31           vfs.constructPath(dir, Isabelle_System.platform_path(path)))
    32       else vfs.constructPath(dir, Isabelle_System.standard_path(path))
    33     }
    34   }
    35 
    36   def check_file(view: View, path: String): Boolean =
    37   {
    38     val vfs = VFSManager.getVFSForPath(path)
    39     val session = vfs.createVFSSession(path, view)
    40 
    41     try {
    42       session != null && {
    43         try {
    44           val file = vfs._getFile(session, path, view)
    45           file != null && file.isReadable && file.getType == VFSFile.FILE
    46         }
    47         catch { case _: IOException => false }
    48       }
    49     }
    50     finally {
    51       try { vfs._endVFSSession(session, view) }
    52       catch { case _: IOException => }
    53     }
    54   }
    55 
    56   override def read_header(name: Document.Node.Name): Thy_Header =
    57   {
    58     Swing_Thread.now {
    59       Isabelle.jedit_buffer(name.node) match {
    60         case Some(buffer) =>
    61           Isabelle.buffer_lock(buffer) {
    62             val text = new Segment
    63             buffer.getText(0, buffer.getLength, text)
    64             Some(Thy_Header.read(text))
    65           }
    66         case None => None
    67       }
    68     } getOrElse {
    69       val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
    70       if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
    71       Thy_Header.read(file)
    72     }
    73   }
    74 }
    75