src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Wed Aug 22 18:04:30 2012 +0200 (2012-08-22 ago)
changeset 48885 d5fdaf7dd1f8
parent 48883 04cd2fddb4d9
child 49406 38db4832b210
permissions -rw-r--r--
find files via load commands within theory text;
clarified Thy_Load.with_thy_text, simplified Thy_Load.check_thy;
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@48885
    36
  override def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
wenzelm@48885
    37
  {
wenzelm@48885
    38
    Swing_Thread.now {
wenzelm@48885
    39
      Isabelle.jedit_buffer(name.node) match {
wenzelm@48885
    40
        case Some(buffer) =>
wenzelm@48885
    41
          Isabelle.buffer_lock(buffer) {
wenzelm@48885
    42
            Some(f(buffer.getSegment(0, buffer.getLength)))
wenzelm@48885
    43
          }
wenzelm@48885
    44
        case None => None
wenzelm@48885
    45
      }
wenzelm@48885
    46
    } getOrElse {
wenzelm@48885
    47
      val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
wenzelm@48885
    48
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@48885
    49
      f(File.read(file))
wenzelm@48885
    50
    }
wenzelm@48885
    51
  }
wenzelm@48885
    52
wenzelm@44963
    53
  def check_file(view: View, path: String): Boolean =
wenzelm@44963
    54
  {
wenzelm@44963
    55
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@44963
    56
    val session = vfs.createVFSSession(path, view)
wenzelm@44963
    57
wenzelm@44963
    58
    try {
wenzelm@44963
    59
      session != null && {
wenzelm@44963
    60
        try {
wenzelm@44963
    61
          val file = vfs._getFile(session, path, view)
wenzelm@44963
    62
          file != null && file.isReadable && file.getType == VFSFile.FILE
wenzelm@44963
    63
        }
wenzelm@44963
    64
        catch { case _: IOException => false }
wenzelm@44963
    65
      }
wenzelm@44963
    66
    }
wenzelm@44963
    67
    finally {
wenzelm@44963
    68
      try { vfs._endVFSSession(session, view) }
wenzelm@44963
    69
      catch { case _: IOException => }
wenzelm@44963
    70
    }
wenzelm@44963
    71
  }
wenzelm@44577
    72
}
wenzelm@44577
    73