author | wenzelm |
Sat, 29 Mar 2014 10:49:32 +0100 | |
changeset 56316 | b1cf8ddc2e04 |
parent 56208 | 06cc31dff138 |
child 56457 | eea4bbe15745 |
permissions | -rw-r--r-- |
56208 | 1 |
/* Title: Tools/jEdit/src/jedit_resources.scala |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
3 |
|
56208 | 4 |
Resources for theories and auxiliary files, based on jEdit buffer |
5 |
content and virtual file-systems. |
|
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
6 |
*/ |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
7 |
|
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
8 |
package isabelle.jedit |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
9 |
|
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
10 |
|
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
11 |
import isabelle._ |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
12 |
|
54511 | 13 |
import java.io.{File => JFile, IOException, ByteArrayOutputStream} |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
14 |
import javax.swing.text.Segment |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
15 |
|
44963
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
16 |
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSFile, VFSManager} |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
17 |
import org.gjt.sp.jedit.MiscUtilities |
55134
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
54517
diff
changeset
|
18 |
import org.gjt.sp.jedit.{jEdit, View, Buffer} |
54511 | 19 |
import org.gjt.sp.jedit.bufferio.BufferIORequest |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
20 |
|
56208 | 21 |
|
22 |
class JEdit_Resources(loaded_theories: Set[String] = Set.empty, base_syntax: Outer_Syntax) |
|
23 |
extends Resources(loaded_theories, base_syntax) |
|
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
24 |
{ |
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
25 |
/* document node names */ |
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
26 |
|
54509
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents:
50566
diff
changeset
|
27 |
def node_name(buffer: Buffer): Document.Node.Name = |
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
28 |
{ |
54515 | 29 |
val node = JEdit_Lib.buffer_name(buffer) |
30 |
val theory = Thy_Header.thy_name(node).getOrElse("") |
|
31 |
val master_dir = if (theory == "") "" else buffer.getDirectory |
|
32 |
Document.Node.Name(node, master_dir, theory) |
|
54509
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents:
50566
diff
changeset
|
33 |
} |
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents:
50566
diff
changeset
|
34 |
|
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents:
50566
diff
changeset
|
35 |
def theory_node_name(buffer: Buffer): Option[Document.Node.Name] = |
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents:
50566
diff
changeset
|
36 |
{ |
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents:
50566
diff
changeset
|
37 |
val name = node_name(buffer) |
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents:
50566
diff
changeset
|
38 |
if (name.is_theory) Some(name) else None |
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
39 |
} |
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
40 |
|
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
41 |
|
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
42 |
/* file-system operations */ |
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
49406
diff
changeset
|
43 |
|
44615 | 44 |
override def append(dir: String, source_path: Path): String = |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
45 |
{ |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
46 |
val path = source_path.expand |
54517 | 47 |
if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path) |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
48 |
else { |
44615 | 49 |
val vfs = VFSManager.getVFSForPath(dir) |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
50 |
if (vfs.isInstanceOf[FileVFS]) |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
51 |
MiscUtilities.resolveSymlinks( |
44615 | 52 |
vfs.constructPath(dir, Isabelle_System.platform_path(path))) |
53 |
else vfs.constructPath(dir, Isabelle_System.standard_path(path)) |
|
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
54 |
} |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
55 |
} |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
56 |
|
48885 | 57 |
override def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A = |
58 |
{ |
|
59 |
Swing_Thread.now { |
|
49406 | 60 |
JEdit_Lib.jedit_buffer(name.node) match { |
48885 | 61 |
case Some(buffer) => |
49406 | 62 |
JEdit_Lib.buffer_lock(buffer) { |
48885 | 63 |
Some(f(buffer.getSegment(0, buffer.getLength))) |
64 |
} |
|
65 |
case None => None |
|
66 |
} |
|
67 |
} getOrElse { |
|
68 |
val file = new JFile(name.node) // FIXME load URL via jEdit VFS (!?) |
|
69 |
if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString)) |
|
70 |
f(File.read(file)) |
|
71 |
} |
|
72 |
} |
|
73 |
||
44963
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
74 |
def check_file(view: View, path: String): Boolean = |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
75 |
{ |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
76 |
val vfs = VFSManager.getVFSForPath(path) |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
77 |
val session = vfs.createVFSSession(path, view) |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
78 |
|
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
79 |
try { |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
80 |
session != null && { |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
81 |
try { |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
82 |
val file = vfs._getFile(session, path, view) |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
83 |
file != null && file.isReadable && file.getType == VFSFile.FILE |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
84 |
} |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
85 |
catch { case _: IOException => false } |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
86 |
} |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
87 |
} |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
88 |
finally { |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
89 |
try { vfs._endVFSSession(session, view) } |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
90 |
catch { case _: IOException => } |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
91 |
} |
4662dddc2fd8
explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents:
44953
diff
changeset
|
92 |
} |
54511 | 93 |
|
94 |
||
95 |
/* file content */ |
|
96 |
||
97 |
def file_content(buffer: Buffer): Bytes = |
|
98 |
{ |
|
99 |
val path = buffer.getPath |
|
100 |
val vfs = VFSManager.getVFSForPath(path) |
|
101 |
val content = |
|
102 |
new BufferIORequest(null, buffer, null, vfs, path) { |
|
103 |
def _run() { } |
|
104 |
def apply(): Bytes = |
|
105 |
{ |
|
106 |
val out = |
|
107 |
new ByteArrayOutputStream(buffer.getLength + 1) { |
|
108 |
def content(): Bytes = Bytes(this.buf, 0, this.count) |
|
109 |
} |
|
110 |
write(buffer, out) |
|
111 |
out.content() |
|
112 |
} |
|
113 |
} |
|
114 |
content() |
|
115 |
} |
|
55134
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
54517
diff
changeset
|
116 |
|
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
54517
diff
changeset
|
117 |
|
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
54517
diff
changeset
|
118 |
/* theory text edits */ |
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
54517
diff
changeset
|
119 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56208
diff
changeset
|
120 |
override def commit(change: Session.Change) |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56208
diff
changeset
|
121 |
{ |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56208
diff
changeset
|
122 |
if (change.syntax_changed) Swing_Thread.later { jEdit.propertiesChanged() } |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56208
diff
changeset
|
123 |
if (change.deps_changed) PIDE.deps_changed() |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56208
diff
changeset
|
124 |
} |
44577
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
125 |
} |
96b6388d06c4
separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff
changeset
|
126 |