author | wenzelm |
Mon, 26 Nov 2012 16:16:47 +0100 | |
changeset 50215 | 97959912840a |
parent 50195 | 863b1dfc396c |
child 50363 | 2f8dc9e65401 |
permissions | -rw-r--r-- |
49406 | 1 |
/* Title: Tools/jEdit/src/jedit_lib.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Misc library functions for jEdit. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
49712 | 12 |
import java.awt.{Component, Container, Frame, Window} |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
13 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
14 |
import scala.annotation.tailrec |
49406 | 15 |
|
16 |
import org.gjt.sp.jedit.{jEdit, Buffer, View} |
|
17 |
import org.gjt.sp.jedit.buffer.JEditBuffer |
|
18 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea} |
|
19 |
||
20 |
||
21 |
object JEdit_Lib |
|
22 |
{ |
|
49712 | 23 |
/* GUI components */ |
24 |
||
25 |
def get_parent(component: Component): Option[Container] = |
|
26 |
component.getParent match { |
|
27 |
case null => None |
|
28 |
case parent => Some(parent) |
|
29 |
} |
|
30 |
||
31 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
32 |
private var next_elem = get_parent(component) |
|
33 |
def hasNext(): Boolean = next_elem.isDefined |
|
34 |
def next(): Container = |
|
35 |
next_elem match { |
|
36 |
case Some(parent) => |
|
37 |
next_elem = get_parent(parent) |
|
38 |
parent |
|
39 |
case None => Iterator.empty.next() |
|
40 |
} |
|
41 |
} |
|
42 |
||
43 |
def parent_window(component: Component): Option[Window] = |
|
44 |
ancestors(component).find(_.isInstanceOf[Window]).map(_.asInstanceOf[Window]) |
|
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
45 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
46 |
def parent_frame(component: Component): Option[Frame] = |
49712 | 47 |
ancestors(component).find(_.isInstanceOf[Frame]).map(_.asInstanceOf[Frame]) |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
48 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
49 |
|
50186
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
50 |
/* basic tooltips, with multi-line support */ |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
51 |
|
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
52 |
def wrap_tooltip(text: String): String = |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
53 |
if (text == "") null |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
54 |
else "<html><pre>" + HTML.encode(text) + "</pre></html>" |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
55 |
|
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
56 |
|
49406 | 57 |
/* buffers */ |
58 |
||
59 |
def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
60 |
Swing_Thread.now { buffer_lock(buffer) { body } } |
|
61 |
||
62 |
def buffer_text(buffer: JEditBuffer): String = |
|
63 |
buffer_lock(buffer) { buffer.getText(0, buffer.getLength) } |
|
64 |
||
65 |
def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath |
|
66 |
||
67 |
def buffer_node_dummy(buffer: Buffer): Option[Document.Node.Name] = |
|
68 |
Some(Document.Node.Name(buffer_name(buffer), buffer.getDirectory, buffer.getName)) |
|
69 |
||
70 |
def buffer_node_name(buffer: Buffer): Option[Document.Node.Name] = |
|
71 |
{ |
|
72 |
val name = buffer_name(buffer) |
|
73 |
Thy_Header.thy_name(name).map(theory => Document.Node.Name(name, buffer.getDirectory, theory)) |
|
74 |
} |
|
75 |
||
76 |
||
77 |
/* main jEdit components */ |
|
78 |
||
79 |
def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator |
|
80 |
||
81 |
def jedit_buffer(name: String): Option[Buffer] = |
|
82 |
jedit_buffers().find(buffer => buffer_name(buffer) == name) |
|
83 |
||
84 |
def jedit_views(): Iterator[View] = jEdit.getViews().iterator |
|
85 |
||
86 |
def jedit_text_areas(view: View): Iterator[JEditTextArea] = |
|
87 |
view.getEditPanes().iterator.map(_.getTextArea) |
|
88 |
||
89 |
def jedit_text_areas(): Iterator[JEditTextArea] = |
|
90 |
jedit_views().flatMap(jedit_text_areas(_)) |
|
91 |
||
92 |
def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] = |
|
93 |
jedit_text_areas().filter(_.getBuffer == buffer) |
|
94 |
||
95 |
def buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
96 |
{ |
|
97 |
try { buffer.readLock(); body } |
|
98 |
finally { buffer.readUnlock() } |
|
99 |
} |
|
49407 | 100 |
|
50195
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
101 |
def buffer_edit[A](buffer: JEditBuffer)(body: => A): A = |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
102 |
{ |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
103 |
try { buffer.beginCompoundEdit(); body } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
104 |
finally { buffer.endCompoundEdit() } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
105 |
} |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
106 |
|
49407 | 107 |
|
50215 | 108 |
/* get text */ |
109 |
||
110 |
def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] = |
|
111 |
try { Some(buffer.getText(range.start, range.length)) } |
|
112 |
catch { case _: ArrayIndexOutOfBoundsException => None } |
|
113 |
||
114 |
||
49407 | 115 |
/* point range */ |
116 |
||
117 |
def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range = |
|
118 |
buffer_lock(buffer) { |
|
119 |
def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0) |
|
120 |
try { |
|
121 |
val c = text(offset) |
|
122 |
if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1))) |
|
123 |
Text.Range(offset, offset + 2) |
|
124 |
else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1))) |
|
125 |
Text.Range(offset - 1, offset + 1) |
|
126 |
else Text.Range(offset, offset + 1) |
|
127 |
} |
|
128 |
catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) } |
|
129 |
} |
|
49408 | 130 |
|
131 |
||
132 |
/* visible text range */ |
|
133 |
||
134 |
def visible_range(text_area: TextArea): Option[Text.Range] = |
|
135 |
{ |
|
136 |
val buffer = text_area.getBuffer |
|
137 |
val n = text_area.getVisibleLines |
|
138 |
if (n > 0) { |
|
139 |
val start = text_area.getScreenLineStartOffset(0) |
|
140 |
val raw_end = text_area.getScreenLineEndOffset(n - 1) |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
141 |
val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength |
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
142 |
Some(Text.Range(start, end)) |
49408 | 143 |
} |
144 |
else None |
|
145 |
} |
|
146 |
||
147 |
def invalidate_range(text_area: TextArea, range: Text.Range) |
|
148 |
{ |
|
149 |
val buffer = text_area.getBuffer |
|
150 |
text_area.invalidateLineRange( |
|
151 |
buffer.getLineOfOffset(range.start), |
|
152 |
buffer.getLineOfOffset(range.stop)) |
|
153 |
} |
|
49409 | 154 |
|
155 |
||
156 |
/* char width */ |
|
157 |
||
158 |
def char_width(text_area: TextArea): Int = |
|
159 |
{ |
|
160 |
val painter = text_area.getPainter |
|
161 |
val font = painter.getFont |
|
162 |
val font_context = painter.getFontRenderContext |
|
163 |
font.getStringBounds(" ", font_context).getWidth.round.toInt |
|
164 |
} |
|
165 |
||
166 |
||
167 |
/* graphics range */ |
|
168 |
||
50115 | 169 |
case class Gfx_Range(val x: Int, val y: Int, val length: Int) |
49409 | 170 |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
171 |
// NB: jEdit always normalizes \r\n and \r to \n |
49409 | 172 |
// NB: last line lacks \n |
173 |
def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] = |
|
174 |
{ |
|
175 |
val buffer = text_area.getBuffer |
|
176 |
||
177 |
val p = text_area.offsetToXY(range.start) |
|
178 |
||
179 |
val end = buffer.getLength |
|
180 |
val stop = range.stop |
|
181 |
val (q, r) = |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
182 |
if (stop >= end) (text_area.offsetToXY(end), char_width(text_area) * (stop - end)) |
49409 | 183 |
else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n") |
184 |
(text_area.offsetToXY(stop - 1), char_width(text_area)) |
|
185 |
else (text_area.offsetToXY(stop), 0) |
|
186 |
||
187 |
if (p != null && q != null && p.x < q.x + r && p.y == q.y) |
|
50115 | 188 |
Some(Gfx_Range(p.x, p.y, q.x + r - p.x)) |
49409 | 189 |
else None |
190 |
} |
|
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
191 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
192 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
193 |
/* pixel range */ |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
194 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
195 |
def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] = |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
196 |
{ |
50116
88b971fca902
more accurate pixel_range -- do not round offset here;
wenzelm
parents:
50115
diff
changeset
|
197 |
val range = point_range(text_area.getBuffer, text_area.xyToOffset(x, y, false)) |
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
198 |
gfx_range(text_area, range) match { |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
199 |
case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range) |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
200 |
case _ => None |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
201 |
} |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
202 |
} |
49406 | 203 |
} |
204 |