src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Tue Nov 19 13:39:12 2013 +0100 (2013-11-19)
changeset 54517 044bee8c5e69
parent 54515 570ba266f5b5
child 55134 1b67b17cdad5
permissions -rw-r--r--
proper Thy_Load.append of auxiliary file names;
inlined errors;
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@54511
    12
import java.io.{File => JFile, IOException, ByteArrayOutputStream}
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@54511
    18
import org.gjt.sp.jedit.bufferio.BufferIORequest
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@54509
    25
  def node_name(buffer: Buffer): Document.Node.Name =
wenzelm@50566
    26
  {
wenzelm@54515
    27
    val node = JEdit_Lib.buffer_name(buffer)
wenzelm@54515
    28
    val theory = Thy_Header.thy_name(node).getOrElse("")
wenzelm@54515
    29
    val master_dir = if (theory == "") "" else buffer.getDirectory
wenzelm@54515
    30
    Document.Node.Name(node, master_dir, theory)
wenzelm@54509
    31
  }
wenzelm@54509
    32
wenzelm@54509
    33
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@54509
    34
  {
wenzelm@54509
    35
    val name = node_name(buffer)
wenzelm@54509
    36
    if (name.is_theory) Some(name) else None
wenzelm@50566
    37
  }
wenzelm@50566
    38
wenzelm@50566
    39
wenzelm@50566
    40
  /* file-system operations */
wenzelm@50566
    41
wenzelm@44615
    42
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    43
  {
wenzelm@44577
    44
    val path = source_path.expand
wenzelm@54517
    45
    if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@44577
    46
    else {
wenzelm@44615
    47
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    48
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    49
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    50
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    51
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    52
    }
wenzelm@44577
    53
  }
wenzelm@44577
    54
wenzelm@48885
    55
  override def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
wenzelm@48885
    56
  {
wenzelm@48885
    57
    Swing_Thread.now {
wenzelm@49406
    58
      JEdit_Lib.jedit_buffer(name.node) match {
wenzelm@48885
    59
        case Some(buffer) =>
wenzelm@49406
    60
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@48885
    61
            Some(f(buffer.getSegment(0, buffer.getLength)))
wenzelm@48885
    62
          }
wenzelm@48885
    63
        case None => None
wenzelm@48885
    64
      }
wenzelm@48885
    65
    } getOrElse {
wenzelm@48885
    66
      val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
wenzelm@48885
    67
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@48885
    68
      f(File.read(file))
wenzelm@48885
    69
    }
wenzelm@48885
    70
  }
wenzelm@48885
    71
wenzelm@44963
    72
  def check_file(view: View, path: String): Boolean =
wenzelm@44963
    73
  {
wenzelm@44963
    74
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@44963
    75
    val session = vfs.createVFSSession(path, view)
wenzelm@44963
    76
wenzelm@44963
    77
    try {
wenzelm@44963
    78
      session != null && {
wenzelm@44963
    79
        try {
wenzelm@44963
    80
          val file = vfs._getFile(session, path, view)
wenzelm@44963
    81
          file != null && file.isReadable && file.getType == VFSFile.FILE
wenzelm@44963
    82
        }
wenzelm@44963
    83
        catch { case _: IOException => false }
wenzelm@44963
    84
      }
wenzelm@44963
    85
    }
wenzelm@44963
    86
    finally {
wenzelm@44963
    87
      try { vfs._endVFSSession(session, view) }
wenzelm@44963
    88
      catch { case _: IOException => }
wenzelm@44963
    89
    }
wenzelm@44963
    90
  }
wenzelm@54511
    91
wenzelm@54511
    92
wenzelm@54511
    93
  /* file content */
wenzelm@54511
    94
wenzelm@54511
    95
  def file_content(buffer: Buffer): Bytes =
wenzelm@54511
    96
  {
wenzelm@54511
    97
    val path = buffer.getPath
wenzelm@54511
    98
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@54511
    99
    val content =
wenzelm@54511
   100
      new BufferIORequest(null, buffer, null, vfs, path) {
wenzelm@54511
   101
        def _run() { }
wenzelm@54511
   102
        def apply(): Bytes =
wenzelm@54511
   103
        {
wenzelm@54511
   104
          val out =
wenzelm@54511
   105
            new ByteArrayOutputStream(buffer.getLength + 1) {
wenzelm@54511
   106
              def content(): Bytes = Bytes(this.buf, 0, this.count)
wenzelm@54511
   107
            }
wenzelm@54511
   108
          write(buffer, out)
wenzelm@54511
   109
          out.content()
wenzelm@54511
   110
        }
wenzelm@54511
   111
      }
wenzelm@54511
   112
    content()
wenzelm@54511
   113
  }
wenzelm@44577
   114
}
wenzelm@44577
   115