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