author | wenzelm |
Mon, 05 Aug 2013 22:54:50 +0200 | |
changeset 52873 | 9e934d4fff00 |
parent 52478 | 0a1db0d02628 |
child 52874 | 91032244e4ca |
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 |
||
50658 | 12 |
import java.awt.{Component, Container, Window, GraphicsEnvironment, Point, Rectangle} |
52873 | 13 |
import javax.swing.Icon |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
14 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
15 |
import scala.annotation.tailrec |
49406 | 16 |
|
52873 | 17 |
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities} |
49406 | 18 |
import org.gjt.sp.jedit.buffer.JEditBuffer |
51492
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
19 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter} |
49406 | 20 |
|
21 |
||
22 |
object JEdit_Lib |
|
23 |
{ |
|
50554
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
24 |
/* bounds within multi-screen environment */ |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
25 |
|
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
26 |
def screen_bounds(screen_point: Point): Rectangle = |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
27 |
{ |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
28 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
29 |
(for { |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
30 |
device <- ge.getScreenDevices.iterator |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
31 |
config <- device.getConfigurations.iterator |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
32 |
bounds = config.getBounds |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
33 |
} yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
34 |
} |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
35 |
|
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
36 |
|
49712 | 37 |
/* GUI components */ |
38 |
||
39 |
def get_parent(component: Component): Option[Container] = |
|
40 |
component.getParent match { |
|
41 |
case null => None |
|
42 |
case parent => Some(parent) |
|
43 |
} |
|
44 |
||
45 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
46 |
private var next_elem = get_parent(component) |
|
47 |
def hasNext(): Boolean = next_elem.isDefined |
|
48 |
def next(): Container = |
|
49 |
next_elem match { |
|
50 |
case Some(parent) => |
|
51 |
next_elem = get_parent(parent) |
|
52 |
parent |
|
53 |
case None => Iterator.empty.next() |
|
54 |
} |
|
55 |
} |
|
56 |
||
57 |
def parent_window(component: Component): Option[Window] = |
|
52478
0a1db0d02628
manage popup windows via PopupFactory, which prefers light-weight JComponent, but might fall back on JWindow (despite JPanel of 0f88591478e6);
wenzelm
parents:
51507
diff
changeset
|
58 |
ancestors(component).collectFirst({ case x: Window => x }) |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
59 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
60 |
|
50186
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
61 |
/* 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
|
62 |
|
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
63 |
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
|
64 |
if (text == "") null |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
65 |
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
|
66 |
|
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
67 |
|
49406 | 68 |
/* buffers */ |
69 |
||
70 |
def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
71 |
Swing_Thread.now { buffer_lock(buffer) { body } } |
|
72 |
||
73 |
def buffer_text(buffer: JEditBuffer): String = |
|
74 |
buffer_lock(buffer) { buffer.getText(0, buffer.getLength) } |
|
75 |
||
76 |
def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath |
|
77 |
||
78 |
||
79 |
/* main jEdit components */ |
|
80 |
||
81 |
def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator |
|
82 |
||
83 |
def jedit_buffer(name: String): Option[Buffer] = |
|
84 |
jedit_buffers().find(buffer => buffer_name(buffer) == name) |
|
85 |
||
86 |
def jedit_views(): Iterator[View] = jEdit.getViews().iterator |
|
87 |
||
88 |
def jedit_text_areas(view: View): Iterator[JEditTextArea] = |
|
89 |
view.getEditPanes().iterator.map(_.getTextArea) |
|
90 |
||
91 |
def jedit_text_areas(): Iterator[JEditTextArea] = |
|
92 |
jedit_views().flatMap(jedit_text_areas(_)) |
|
93 |
||
94 |
def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] = |
|
95 |
jedit_text_areas().filter(_.getBuffer == buffer) |
|
96 |
||
97 |
def buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
98 |
{ |
|
99 |
try { buffer.readLock(); body } |
|
100 |
finally { buffer.readUnlock() } |
|
101 |
} |
|
49407 | 102 |
|
50195
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
103 |
def buffer_edit[A](buffer: JEditBuffer)(body: => A): A = |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
104 |
{ |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
105 |
try { buffer.beginCompoundEdit(); body } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
106 |
finally { buffer.endCompoundEdit() } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
107 |
} |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
108 |
|
49407 | 109 |
|
50215 | 110 |
/* get text */ |
111 |
||
112 |
def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] = |
|
113 |
try { Some(buffer.getText(range.start, range.length)) } |
|
114 |
catch { case _: ArrayIndexOutOfBoundsException => None } |
|
115 |
||
116 |
||
50363
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
117 |
/* buffer range */ |
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
118 |
|
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
119 |
def buffer_range(buffer: JEditBuffer): Text.Range = |
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
120 |
Text.Range(0, (buffer.getLength - 1) max 0) |
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
121 |
|
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
122 |
|
49407 | 123 |
/* point range */ |
124 |
||
125 |
def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range = |
|
126 |
buffer_lock(buffer) { |
|
127 |
def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0) |
|
128 |
try { |
|
129 |
val c = text(offset) |
|
130 |
if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1))) |
|
131 |
Text.Range(offset, offset + 2) |
|
132 |
else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1))) |
|
133 |
Text.Range(offset - 1, offset + 1) |
|
134 |
else Text.Range(offset, offset + 1) |
|
135 |
} |
|
136 |
catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) } |
|
137 |
} |
|
49408 | 138 |
|
139 |
||
140 |
/* visible text range */ |
|
141 |
||
142 |
def visible_range(text_area: TextArea): Option[Text.Range] = |
|
143 |
{ |
|
144 |
val buffer = text_area.getBuffer |
|
145 |
val n = text_area.getVisibleLines |
|
146 |
if (n > 0) { |
|
147 |
val start = text_area.getScreenLineStartOffset(0) |
|
148 |
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
|
149 |
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
|
150 |
Some(Text.Range(start, end)) |
49408 | 151 |
} |
152 |
else None |
|
153 |
} |
|
154 |
||
155 |
def invalidate_range(text_area: TextArea, range: Text.Range) |
|
156 |
{ |
|
157 |
val buffer = text_area.getBuffer |
|
158 |
text_area.invalidateLineRange( |
|
159 |
buffer.getLineOfOffset(range.start), |
|
160 |
buffer.getLineOfOffset(range.stop)) |
|
161 |
} |
|
49409 | 162 |
|
163 |
||
164 |
/* graphics range */ |
|
165 |
||
50115 | 166 |
case class Gfx_Range(val x: Int, val y: Int, val length: Int) |
49409 | 167 |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
168 |
// NB: jEdit always normalizes \r\n and \r to \n |
49409 | 169 |
// NB: last line lacks \n |
170 |
def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] = |
|
171 |
{ |
|
51507 | 172 |
val metric = pretty_metric(text_area.getPainter) |
51492
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
173 |
val char_width = (metric.unit * metric.average).round.toInt |
50849 | 174 |
|
49409 | 175 |
val buffer = text_area.getBuffer |
176 |
||
177 |
val end = buffer.getLength |
|
178 |
val stop = range.stop |
|
51078
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
179 |
|
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
180 |
val (p, q, r) = |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
181 |
try { |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
182 |
val p = text_area.offsetToXY(range.start) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
183 |
val (q, r) = |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
184 |
if (stop >= end) (text_area.offsetToXY(end), char_width * (stop - end)) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
185 |
else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n") |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
186 |
(text_area.offsetToXY(stop - 1), char_width) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
187 |
else (text_area.offsetToXY(stop), 0) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
188 |
(p, q, r) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
189 |
} |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
190 |
catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) } |
49409 | 191 |
|
192 |
if (p != null && q != null && p.x < q.x + r && p.y == q.y) |
|
50115 | 193 |
Some(Gfx_Range(p.x, p.y, q.x + r - p.x)) |
49409 | 194 |
else None |
195 |
} |
|
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
196 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
197 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
198 |
/* pixel range */ |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
199 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
200 |
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
|
201 |
{ |
50116
88b971fca902
more accurate pixel_range -- do not round offset here;
wenzelm
parents:
50115
diff
changeset
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
case _ => None |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
206 |
} |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
207 |
} |
51492
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
208 |
|
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
209 |
|
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
210 |
/* pretty text metric */ |
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
211 |
|
51507 | 212 |
abstract class Pretty_Metric extends Pretty.Metric |
213 |
{ |
|
214 |
def average: Double |
|
215 |
} |
|
51492
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
216 |
|
51507 | 217 |
def pretty_metric(painter: TextAreaPainter): Pretty_Metric = |
218 |
new Pretty_Metric { |
|
219 |
def string_width(s: String): Double = |
|
220 |
painter.getFont.getStringBounds(s, painter.getFontRenderContext).getWidth |
|
221 |
||
222 |
val unit = string_width(Pretty.space) |
|
223 |
val average = string_width("mix") / (3 * unit) |
|
224 |
def apply(s: String): Double = if (s == "\n") 1.0 else string_width(s) / unit |
|
51492
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
225 |
} |
52873 | 226 |
|
227 |
||
228 |
/* icons */ |
|
229 |
||
230 |
def load_icon(name: String): Icon = |
|
231 |
{ |
|
232 |
val name1 = |
|
233 |
if (name.startsWith("idea-icons/")) { |
|
234 |
val file = |
|
235 |
Isabelle_System.platform_file_url(Path.explode("$JEDIT_HOME/dist/jars/idea-icons.jar")) |
|
236 |
"jar:" + file + "!/" + name |
|
237 |
} |
|
238 |
else name |
|
239 |
val icon = GUIUtilities.loadIcon(name1) |
|
240 |
if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name) |
|
241 |
else icon |
|
242 |
} |
|
49406 | 243 |
} |
244 |