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;
wenzelm@45007
     1
/*  Title:      Tools/jEdit/src/jedit_thy_load.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@48409
    12
import java.io.{File => JFile, IOException}
wenzelm@44577
    13
import javax.swing.text.Segment
wenzelm@44577
    14
wenzelm@44963
    15
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSFile, VFSManager}
wenzelm@44577
    16
import org.gjt.sp.jedit.MiscUtilities
wenzelm@44963
    17
import org.gjt.sp.jedit.View
wenzelm@44577
    18
wenzelm@44577
    19
wenzelm@48870
    20
class JEdit_Thy_Load(loaded_theories: Set[String] = Set.empty, base_syntax: Outer_Syntax)
wenzelm@48870
    21
  extends Thy_Load(loaded_theories, base_syntax)
wenzelm@44577
    22
{
wenzelm@44615
    23
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    24
  {
wenzelm@44577
    25
    val path = source_path.expand
wenzelm@44577
    26
    if (path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@44577
    27
    else {
wenzelm@44615
    28
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    29
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    30
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    31
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    32
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    33
    }
wenzelm@44577
    34
  }
wenzelm@44577
    35
wenzelm@44963
    36
  def check_file(view: View, path: String): Boolean =
wenzelm@44963
    37
  {
wenzelm@44963
    38
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@44963
    39
    val session = vfs.createVFSSession(path, view)
wenzelm@44963
    40
wenzelm@44963
    41
    try {
wenzelm@44963
    42
      session != null && {
wenzelm@44963
    43
        try {
wenzelm@44963
    44
          val file = vfs._getFile(session, path, view)
wenzelm@44963
    45
          file != null && file.isReadable && file.getType == VFSFile.FILE
wenzelm@44963
    46
        }
wenzelm@44963
    47
        catch { case _: IOException => false }
wenzelm@44963
    48
      }
wenzelm@44963
    49
    }
wenzelm@44963
    50
    finally {
wenzelm@44963
    51
      try { vfs._endVFSSession(session, view) }
wenzelm@44963
    52
      catch { case _: IOException => }
wenzelm@44963
    53
    }
wenzelm@44963
    54
  }
wenzelm@44963
    55
wenzelm@46737
    56
  override def read_header(name: Document.Node.Name): Thy_Header =
wenzelm@44577
    57
  {
wenzelm@44577
    58
    Swing_Thread.now {
wenzelm@44615
    59
      Isabelle.jedit_buffer(name.node) match {
wenzelm@44577
    60
        case Some(buffer) =>
wenzelm@44577
    61
          Isabelle.buffer_lock(buffer) {
wenzelm@44577
    62
            val text = new Segment
wenzelm@44577
    63
            buffer.getText(0, buffer.getLength, text)
wenzelm@44577
    64
            Some(Thy_Header.read(text))
wenzelm@44577
    65
          }
wenzelm@44577
    66
        case None => None
wenzelm@44577
    67
      }
wenzelm@44577
    68
    } getOrElse {
wenzelm@48409
    69
      val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
wenzelm@44577
    70
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@44577
    71
      Thy_Header.read(file)
wenzelm@44577
    72
    }
wenzelm@44577
    73
  }
wenzelm@44577
    74
}
wenzelm@44577
    75