src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Sun Dec 16 18:44:27 2012 +0100 (2012-12-16 ago)
changeset 50566 b43c4f660320
parent 49406 38db4832b210
child 54509 1f77110c94ef
permissions -rw-r--r--
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
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@50566
    17
import org.gjt.sp.jedit.{View, Buffer}
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@50566
    23
  /* document node names */
wenzelm@50566
    24
wenzelm@50566
    25
  def buffer_node_dummy(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@50566
    26
    Some(Document.Node.Name(JEdit_Lib.buffer_name(buffer), buffer.getDirectory, buffer.getName))
wenzelm@50566
    27
wenzelm@50566
    28
  def buffer_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@50566
    29
  {
wenzelm@50566
    30
    val name = JEdit_Lib.buffer_name(buffer)
wenzelm@50566
    31
    Thy_Header.thy_name(name).map(theory => Document.Node.Name(name, buffer.getDirectory, theory))
wenzelm@50566
    32
  }
wenzelm@50566
    33
wenzelm@50566
    34
wenzelm@50566
    35
  /* file-system operations */
wenzelm@50566
    36
wenzelm@44615
    37
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    38
  {
wenzelm@44577
    39
    val path = source_path.expand
wenzelm@44577
    40
    if (path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@44577
    41
    else {
wenzelm@44615
    42
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    43
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    44
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    45
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    46
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    47
    }
wenzelm@44577
    48
  }
wenzelm@44577
    49
wenzelm@48885
    50
  override def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
wenzelm@48885
    51
  {
wenzelm@48885
    52
    Swing_Thread.now {
wenzelm@49406
    53
      JEdit_Lib.jedit_buffer(name.node) match {
wenzelm@48885
    54
        case Some(buffer) =>
wenzelm@49406
    55
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@48885
    56
            Some(f(buffer.getSegment(0, buffer.getLength)))
wenzelm@48885
    57
          }
wenzelm@48885
    58
        case None => None
wenzelm@48885
    59
      }
wenzelm@48885
    60
    } getOrElse {
wenzelm@48885
    61
      val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
wenzelm@48885
    62
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@48885
    63
      f(File.read(file))
wenzelm@48885
    64
    }
wenzelm@48885
    65
  }
wenzelm@48885
    66
wenzelm@44963
    67
  def check_file(view: View, path: String): Boolean =
wenzelm@44963
    68
  {
wenzelm@44963
    69
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@44963
    70
    val session = vfs.createVFSSession(path, view)
wenzelm@44963
    71
wenzelm@44963
    72
    try {
wenzelm@44963
    73
      session != null && {
wenzelm@44963
    74
        try {
wenzelm@44963
    75
          val file = vfs._getFile(session, path, view)
wenzelm@44963
    76
          file != null && file.isReadable && file.getType == VFSFile.FILE
wenzelm@44963
    77
        }
wenzelm@44963
    78
        catch { case _: IOException => false }
wenzelm@44963
    79
      }
wenzelm@44963
    80
    }
wenzelm@44963
    81
    finally {
wenzelm@44963
    82
      try { vfs._endVFSSession(session, view) }
wenzelm@44963
    83
      catch { case _: IOException => }
wenzelm@44963
    84
    }
wenzelm@44963
    85
  }
wenzelm@44577
    86
}
wenzelm@44577
    87