1/*  Title:      Tools/jEdit/src/jedit_resources.scala
2    Author:     Makarius
3
4Resources for theories and auxiliary files, based on jEdit buffer
5content and virtual file-systems.
6*/
7
8package isabelle.jedit
9
10
11import isabelle._
12
13import java.io.{File => JFile, ByteArrayOutputStream}
14import javax.swing.text.Segment
15
16import scala.util.parsing.input.Reader
17
18import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
19import org.gjt.sp.jedit.MiscUtilities
20import org.gjt.sp.jedit.{jEdit, View, Buffer}
21import org.gjt.sp.jedit.bufferio.BufferIORequest
22
23
24object JEdit_Resources
25{
26  def apply(options: Options): JEdit_Resources =
27    new JEdit_Resources(JEdit_Sessions.session_base_info(options))
28}
29
30class JEdit_Resources private(val session_base_info: Sessions.Base_Info)
31  extends Resources(session_base_info.base.platform_path)
32{
33  def session_name: String = session_base_info.session
34  def session_errors: List[String] = session_base_info.errors
35
36
37  /* document node name */
38
39  def known_file(path: String): Option[Document.Node.Name] =
40    JEdit_Lib.check_file(path).flatMap(session_base.known.get_file(_, bootstrap = true))
41
42  def node_name(path: String): Document.Node.Name =
43    known_file(path) getOrElse {
44      val vfs = VFSManager.getVFSForPath(path)
45      val node = if (vfs.isInstanceOf[FileVFS]) MiscUtilities.resolveSymlinks(path) else path
46      val theory = theory_name(Sessions.DRAFT, Thy_Header.theory_name(node))
47      if (session_base.loaded_theory(theory)) Document.Node.Name.loaded_theory(theory)
48      else {
49        val master_dir = vfs.getParentOfPath(path)
50        Document.Node.Name(node, master_dir, theory)
51      }
52    }
53
54  def node_name(buffer: Buffer): Document.Node.Name =
55    node_name(JEdit_Lib.buffer_name(buffer))
56
57  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
58  {
59    val name = node_name(buffer)
60    if (name.is_theory) Some(name) else None
61  }
62
63  override def append(dir: String, source_path: Path): String =
64  {
65    val path = source_path.expand
66    if (dir == "" || path.is_absolute)
67      MiscUtilities.resolveSymlinks(File.platform_path(path))
68    else if (path.is_current) dir
69    else {
70      val vfs = VFSManager.getVFSForPath(dir)
71      if (vfs.isInstanceOf[FileVFS])
72        MiscUtilities.resolveSymlinks(
73          vfs.constructPath(dir, File.platform_path(path)))
74      else vfs.constructPath(dir, File.standard_path(path))
75    }
76  }
77
78
79  /* file content */
80
81  def read_file_content(node_name: Document.Node.Name): Option[String] =
82  {
83    Bibtex.make_theory_content(node_name.theory) orElse {
84      val name = node_name.node
85      try {
86        val text =
87          if (Url.is_wellformed(name)) Url.read(Url(name))
88          else File.read(new JFile(name))
89        Some(Symbol.decode(Line.normalize(text)))
90      }
91      catch { case ERROR(_) => None }
92    }
93  }
94
95  def get_file_content(node_name: Document.Node.Name): Option[String] =
96    Document_Model.get(node_name) match {
97      case Some(model: Buffer_Model) => Some(JEdit_Lib.buffer_text(model.buffer))
98      case Some(model: File_Model) => Some(model.content.text)
99      case None => read_file_content(node_name)
100    }
101
102  override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
103  {
104    GUI_Thread.now {
105      Document_Model.get(name) match {
106        case Some(model: Buffer_Model) =>
107          JEdit_Lib.buffer_lock(model.buffer) { Some(f(JEdit_Lib.buffer_reader(model.buffer))) }
108        case Some(model: File_Model) => Some(f(Scan.char_reader(model.content.text)))
109        case None => None
110      }
111    } getOrElse {
112      if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
113      else {
114        val file = new JFile(name.node)
115        if (file.isFile) f(Scan.byte_reader(file))
116        else error("No such file: " + quote(file.toString))
117      }
118    }
119  }
120
121
122  private class File_Content_Output(buffer: Buffer) extends
123    ByteArrayOutputStream(buffer.getLength + 1)
124  {
125    def content(): Bytes = Bytes(this.buf, 0, this.count)
126  }
127
128  private class File_Content(buffer: Buffer) extends
129    BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
130  {
131    def _run() { }
132    def content(): Bytes =
133    {
134      val out = new File_Content_Output(buffer)
135      write(buffer, out)
136      out.content()
137    }
138  }
139
140  def make_file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
141
142
143  /* theory text edits */
144
145  override def commit(change: Session.Change)
146  {
147    if (change.syntax_changed.nonEmpty)
148      GUI_Thread.later { Document_Model.syntax_changed(change.syntax_changed) }
149    if (change.deps_changed ||
150        PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
151      PIDE.plugin.deps_changed()
152  }
153}
154