src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Sat Sep 17 17:55:39 2011 +0200 (2011-09-17)
changeset 44953 cdfe42f1267c
parent 44615 a4ff8a787202
child 44963 4662dddc2fd8
permissions -rw-r--r--
sane default for class Thy_Load;
wenzelm@44577
     1
/*  Title:      Tools/jEdit/src/plugin.scala
wenzelm@44577
     2
    Author:     Makarius
wenzelm@44577
     3
wenzelm@44577
     4
Primitives for loading theory files, based on jEdit buffer content.
wenzelm@44577
     5
*/
wenzelm@44577
     6
wenzelm@44577
     7
package isabelle.jedit
wenzelm@44577
     8
wenzelm@44577
     9
wenzelm@44577
    10
import isabelle._
wenzelm@44577
    11
wenzelm@44577
    12
import java.io.File
wenzelm@44577
    13
import javax.swing.text.Segment
wenzelm@44577
    14
wenzelm@44577
    15
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
wenzelm@44577
    16
import org.gjt.sp.jedit.MiscUtilities
wenzelm@44577
    17
wenzelm@44577
    18
wenzelm@44577
    19
class JEdit_Thy_Load extends Thy_Load
wenzelm@44577
    20
{
wenzelm@44615
    21
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    22
  {
wenzelm@44577
    23
    val path = source_path.expand
wenzelm@44577
    24
    if (path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@44577
    25
    else {
wenzelm@44615
    26
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    27
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    28
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    29
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    30
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    31
    }
wenzelm@44577
    32
  }
wenzelm@44577
    33
wenzelm@44615
    34
  override def check_thy(name: Document.Node.Name): Thy_Header =
wenzelm@44577
    35
  {
wenzelm@44577
    36
    Swing_Thread.now {
wenzelm@44615
    37
      Isabelle.jedit_buffer(name.node) match {
wenzelm@44577
    38
        case Some(buffer) =>
wenzelm@44577
    39
          Isabelle.buffer_lock(buffer) {
wenzelm@44577
    40
            val text = new Segment
wenzelm@44577
    41
            buffer.getText(0, buffer.getLength, text)
wenzelm@44577
    42
            Some(Thy_Header.read(text))
wenzelm@44577
    43
          }
wenzelm@44577
    44
        case None => None
wenzelm@44577
    45
      }
wenzelm@44577
    46
    } getOrElse {
wenzelm@44615
    47
      val file = new File(name.node)  // FIXME load URL via jEdit VFS (!?)
wenzelm@44577
    48
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@44577
    49
      Thy_Header.read(file)
wenzelm@44577
    50
    }
wenzelm@44577
    51
  }
wenzelm@44577
    52
}
wenzelm@44577
    53