author | wenzelm |
Sun, 18 Nov 2012 14:24:30 +0100 | |
changeset 50116 | 88b971fca902 |
parent 50115 | 8cde6f1a0106 |
child 50186 | f83cab68c788 |
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 |
|
49406 | 50 |
/* buffers */ |
51 |
||
52 |
def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
53 |
Swing_Thread.now { buffer_lock(buffer) { body } } |
|
54 |
||
55 |
def buffer_text(buffer: JEditBuffer): String = |
|
56 |
buffer_lock(buffer) { buffer.getText(0, buffer.getLength) } |
|
57 |
||
58 |
def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath |
|
59 |
||
60 |
def buffer_node_dummy(buffer: Buffer): Option[Document.Node.Name] = |
|
61 |
Some(Document.Node.Name(buffer_name(buffer), buffer.getDirectory, buffer.getName)) |
|
62 |
||
63 |
def buffer_node_name(buffer: Buffer): Option[Document.Node.Name] = |
|
64 |
{ |
|
65 |
val name = buffer_name(buffer) |
|
66 |
Thy_Header.thy_name(name).map(theory => Document.Node.Name(name, buffer.getDirectory, theory)) |
|
67 |
} |
|
68 |
||
69 |
||
70 |
/* main jEdit components */ |
|
71 |
||
72 |
def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator |
|
73 |
||
74 |
def jedit_buffer(name: String): Option[Buffer] = |
|
75 |
jedit_buffers().find(buffer => buffer_name(buffer) == name) |
|
76 |
||
77 |
def jedit_views(): Iterator[View] = jEdit.getViews().iterator |
|
78 |
||
79 |
def jedit_text_areas(view: View): Iterator[JEditTextArea] = |
|
80 |
view.getEditPanes().iterator.map(_.getTextArea) |
|
81 |
||
82 |
def jedit_text_areas(): Iterator[JEditTextArea] = |
|
83 |
jedit_views().flatMap(jedit_text_areas(_)) |
|
84 |
||
85 |
def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] = |
|
86 |
jedit_text_areas().filter(_.getBuffer == buffer) |
|
87 |
||
88 |
def buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
89 |
{ |
|
90 |
try { buffer.readLock(); body } |
|
91 |
finally { buffer.readUnlock() } |
|
92 |
} |
|
49407 | 93 |
|
94 |
||
95 |
/* point range */ |
|
96 |
||
97 |
def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range = |
|
98 |
buffer_lock(buffer) { |
|
99 |
def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0) |
|
100 |
try { |
|
101 |
val c = text(offset) |
|
102 |
if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1))) |
|
103 |
Text.Range(offset, offset + 2) |
|
104 |
else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1))) |
|
105 |
Text.Range(offset - 1, offset + 1) |
|
106 |
else Text.Range(offset, offset + 1) |
|
107 |
} |
|
108 |
catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) } |
|
109 |
} |
|
49408 | 110 |
|
111 |
||
112 |
/* visible text range */ |
|
113 |
||
114 |
def visible_range(text_area: TextArea): Option[Text.Range] = |
|
115 |
{ |
|
116 |
val buffer = text_area.getBuffer |
|
117 |
val n = text_area.getVisibleLines |
|
118 |
if (n > 0) { |
|
119 |
val start = text_area.getScreenLineStartOffset(0) |
|
120 |
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
|
121 |
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
|
122 |
Some(Text.Range(start, end)) |
49408 | 123 |
} |
124 |
else None |
|
125 |
} |
|
126 |
||
127 |
def invalidate_range(text_area: TextArea, range: Text.Range) |
|
128 |
{ |
|
129 |
val buffer = text_area.getBuffer |
|
130 |
text_area.invalidateLineRange( |
|
131 |
buffer.getLineOfOffset(range.start), |
|
132 |
buffer.getLineOfOffset(range.stop)) |
|
133 |
} |
|
49409 | 134 |
|
135 |
||
136 |
/* char width */ |
|
137 |
||
138 |
def char_width(text_area: TextArea): Int = |
|
139 |
{ |
|
140 |
val painter = text_area.getPainter |
|
141 |
val font = painter.getFont |
|
142 |
val font_context = painter.getFontRenderContext |
|
143 |
font.getStringBounds(" ", font_context).getWidth.round.toInt |
|
144 |
} |
|
145 |
||
146 |
||
147 |
/* graphics range */ |
|
148 |
||
50115 | 149 |
case class Gfx_Range(val x: Int, val y: Int, val length: Int) |
49409 | 150 |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
151 |
// NB: jEdit always normalizes \r\n and \r to \n |
49409 | 152 |
// NB: last line lacks \n |
153 |
def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] = |
|
154 |
{ |
|
155 |
val buffer = text_area.getBuffer |
|
156 |
||
157 |
val p = text_area.offsetToXY(range.start) |
|
158 |
||
159 |
val end = buffer.getLength |
|
160 |
val stop = range.stop |
|
161 |
val (q, r) = |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
162 |
if (stop >= end) (text_area.offsetToXY(end), char_width(text_area) * (stop - end)) |
49409 | 163 |
else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n") |
164 |
(text_area.offsetToXY(stop - 1), char_width(text_area)) |
|
165 |
else (text_area.offsetToXY(stop), 0) |
|
166 |
||
167 |
if (p != null && q != null && p.x < q.x + r && p.y == q.y) |
|
50115 | 168 |
Some(Gfx_Range(p.x, p.y, q.x + r - p.x)) |
49409 | 169 |
else None |
170 |
} |
|
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
171 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
172 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
173 |
/* pixel range */ |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
174 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
175 |
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
|
176 |
{ |
50116
88b971fca902
more accurate pixel_range -- do not round offset here;
wenzelm
parents:
50115
diff
changeset
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
case _ => None |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
181 |
} |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
182 |
} |
49406 | 183 |
} |
184 |