more frugal access to theory text via Reader, reduced costs for I/O text decoding;
prefer non-strict Symbol.decode, since Reader[Char] may present symbols in either way;
1 /* Title: Tools/jEdit/src/jedit_resources.scala
4 Resources for theories and auxiliary files, based on jEdit buffer
5 content and virtual file-systems.
13 import java.io.{File => JFile, ByteArrayOutputStream}
14 import javax.swing.text.Segment
16 import scala.util.parsing.input.Reader
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
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)
30 /* document node names */
32 def node_name(buffer: Buffer): Document.Node.Name =
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)
40 def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
42 val name = node_name(buffer)
43 if (name.is_theory) Some(name) else None
47 /* file-system operations */
49 override def append(dir: String, source_path: Path): String =
51 val path = source_path.expand
52 if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path)
53 else if (path.is_current) dir
55 val vfs = VFSManager.getVFSForPath(dir)
56 if (vfs.isInstanceOf[FileVFS])
57 MiscUtilities.resolveSymlinks(
58 vfs.constructPath(dir, Isabelle_System.platform_path(path)))
59 else vfs.constructPath(dir, Isabelle_System.standard_path(path))
63 override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
66 JEdit_Lib.jedit_buffer(name) match {
68 JEdit_Lib.buffer_lock(buffer) { Some(f(JEdit_Lib.buffer_reader(buffer))) }
72 if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
74 val file = new JFile(name.node)
75 if (file.isFile) f(Scan.byte_reader(file))
76 else error("No such file: " + quote(file.toString))
81 def check_file(view: View, file: String): Boolean =
83 if (Url.is_wellformed(file)) Url.is_readable(file)
84 else (new JFile(file)).isFile
86 catch { case ERROR(_) => false }
91 private class File_Content_Output(buffer: Buffer) extends
92 ByteArrayOutputStream(buffer.getLength + 1)
94 def content(): Bytes = Bytes(this.buf, 0, this.count)
97 private class File_Content(buffer: Buffer) extends
98 BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
101 def content(): Bytes =
103 val out = new File_Content_Output(buffer)
109 def file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
112 /* theory text edits */
114 override def commit(change: Session.Change)
116 if (change.syntax_changed) Swing_Thread.later { jEdit.propertiesChanged() }