src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Thu Jan 08 20:56:39 2015 +0100 (2015-01-08 ago)
changeset 59319 677615cba30d
parent 59078 cf255dc2b48f
child 59691 f6ff19188842
permissions -rw-r--r--
tuned;
     1 /*  Title:      Tools/jEdit/src/jedit_resources.scala
     2     Author:     Makarius
     3 
     4 Resources for theories and auxiliary files, based on jEdit buffer
     5 content and virtual file-systems.
     6 */
     7 
     8 package isabelle.jedit
     9 
    10 
    11 import isabelle._
    12 
    13 import java.io.{File => JFile, ByteArrayOutputStream}
    14 import javax.swing.text.Segment
    15 
    16 import scala.util.parsing.input.Reader
    17 
    18 import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
    19 import org.gjt.sp.jedit.MiscUtilities
    20 import org.gjt.sp.jedit.{jEdit, View, Buffer}
    21 import org.gjt.sp.jedit.bufferio.BufferIORequest
    22 
    23 
    24 class JEdit_Resources(
    25     loaded_theories: Set[String],
    26     known_theories: Map[String, Document.Node.Name],
    27     base_syntax: Outer_Syntax)
    28   extends Resources(loaded_theories, known_theories, base_syntax)
    29 {
    30   /* document node names */
    31 
    32   def node_name(buffer: Buffer): Document.Node.Name =
    33   {
    34     val node = JEdit_Lib.buffer_name(buffer)
    35     val theory = Thy_Header.thy_name(node).getOrElse("")
    36     val master_dir = if (theory == "") "" else buffer.getDirectory
    37     Document.Node.Name(node, master_dir, theory)
    38   }
    39 
    40   def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
    41   {
    42     val name = node_name(buffer)
    43     if (name.is_theory) Some(name) else None
    44   }
    45 
    46 
    47   /* file-system operations */
    48 
    49   override def append(dir: String, source_path: Path): String =
    50   {
    51     val path = source_path.expand
    52     if (dir == "" || path.is_absolute)
    53       MiscUtilities.resolveSymlinks(Isabelle_System.platform_path(path))
    54     else if (path.is_current) dir
    55     else {
    56       val vfs = VFSManager.getVFSForPath(dir)
    57       if (vfs.isInstanceOf[FileVFS])
    58         MiscUtilities.resolveSymlinks(
    59           vfs.constructPath(dir, Isabelle_System.platform_path(path)))
    60       else vfs.constructPath(dir, Isabelle_System.standard_path(path))
    61     }
    62   }
    63 
    64   override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    65   {
    66     GUI_Thread.now {
    67       JEdit_Lib.jedit_buffer(name) match {
    68         case Some(buffer) =>
    69           JEdit_Lib.buffer_lock(buffer) { Some(f(JEdit_Lib.buffer_reader(buffer))) }
    70         case None => None
    71       }
    72     } getOrElse {
    73       if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
    74       else {
    75         val file = new JFile(name.node)
    76         if (file.isFile) f(Scan.byte_reader(file))
    77         else error("No such file: " + quote(file.toString))
    78       }
    79     }
    80   }
    81 
    82   def check_file(view: View, file: String): Boolean =
    83     try {
    84       if (Url.is_wellformed(file)) Url.is_readable(file)
    85       else (new JFile(file)).isFile
    86     }
    87     catch { case ERROR(_) => false }
    88 
    89 
    90   /* file content */
    91 
    92   private class File_Content_Output(buffer: Buffer) extends
    93     ByteArrayOutputStream(buffer.getLength + 1)
    94   {
    95     def content(): Bytes = Bytes(this.buf, 0, this.count)
    96   }
    97 
    98   private class File_Content(buffer: Buffer) extends
    99     BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
   100   {
   101     def _run() { }
   102     def content(): Bytes =
   103     {
   104       val out = new File_Content_Output(buffer)
   105       write(buffer, out)
   106       out.content()
   107     }
   108   }
   109 
   110   def file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
   111 
   112 
   113   /* theory text edits */
   114 
   115   override def commit(change: Session.Change)
   116   {
   117     if (change.syntax_changed.nonEmpty)
   118       GUI_Thread.later {
   119         val changed = change.syntax_changed.toSet
   120         for {
   121           buffer <- JEdit_Lib.jedit_buffers()
   122           model <- PIDE.document_model(buffer)
   123           if changed(model.node_name)
   124         } model.syntax_changed()
   125       }
   126     if (change.deps_changed && PIDE.options.bool("jedit_auto_load")) PIDE.deps_changed()
   127   }
   128 }
   129