src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Sun Sep 18 13:47:12 2011 +0200 (2011-09-18)
changeset 44963 4662dddc2fd8
parent 44953 cdfe42f1267c
child 45007 cc86edb97c2c
permissions -rw-r--r--
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
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@44963
    12
import java.io.{File, 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@44577
    20
class JEdit_Thy_Load extends Thy_Load
wenzelm@44577
    21
{
wenzelm@44615
    22
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    23
  {
wenzelm@44577
    24
    val path = source_path.expand
wenzelm@44577
    25
    if (path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@44577
    26
    else {
wenzelm@44615
    27
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    28
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    29
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    30
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    31
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    32
    }
wenzelm@44577
    33
  }
wenzelm@44577
    34
wenzelm@44963
    35
  def check_file(view: View, path: String): Boolean =
wenzelm@44963
    36
  {
wenzelm@44963
    37
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@44963
    38
    val session = vfs.createVFSSession(path, view)
wenzelm@44963
    39
wenzelm@44963
    40
    try {
wenzelm@44963
    41
      session != null && {
wenzelm@44963
    42
        try {
wenzelm@44963
    43
          val file = vfs._getFile(session, path, view)
wenzelm@44963
    44
          file != null && file.isReadable && file.getType == VFSFile.FILE
wenzelm@44963
    45
        }
wenzelm@44963
    46
        catch { case _: IOException => false }
wenzelm@44963
    47
      }
wenzelm@44963
    48
    }
wenzelm@44963
    49
    finally {
wenzelm@44963
    50
      try { vfs._endVFSSession(session, view) }
wenzelm@44963
    51
      catch { case _: IOException => }
wenzelm@44963
    52
    }
wenzelm@44963
    53
  }
wenzelm@44963
    54
wenzelm@44615
    55
  override def check_thy(name: Document.Node.Name): Thy_Header =
wenzelm@44577
    56
  {
wenzelm@44577
    57
    Swing_Thread.now {
wenzelm@44615
    58
      Isabelle.jedit_buffer(name.node) match {
wenzelm@44577
    59
        case Some(buffer) =>
wenzelm@44577
    60
          Isabelle.buffer_lock(buffer) {
wenzelm@44577
    61
            val text = new Segment
wenzelm@44577
    62
            buffer.getText(0, buffer.getLength, text)
wenzelm@44577
    63
            Some(Thy_Header.read(text))
wenzelm@44577
    64
          }
wenzelm@44577
    65
        case None => None
wenzelm@44577
    66
      }
wenzelm@44577
    67
    } getOrElse {
wenzelm@44615
    68
      val file = new File(name.node)  // FIXME load URL via jEdit VFS (!?)
wenzelm@44577
    69
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@44577
    70
      Thy_Header.read(file)
wenzelm@44577
    71
    }
wenzelm@44577
    72
  }
wenzelm@44577
    73
}
wenzelm@44577
    74