author | wenzelm |
Sat, 19 Dec 2015 15:14:59 +0100 | |
changeset 61865 | 6dcc9e4f1aa6 |
parent 61192 | 98eba31c51f8 |
child 62264 | 340f98836fd9 |
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 |
||
53712 | 12 |
import java.awt.{Component, Container, GraphicsEnvironment, Point, Rectangle, Dimension} |
53784 | 13 |
import java.awt.event.{InputEvent, KeyEvent, KeyListener} |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
14 |
import javax.swing.{Icon, ImageIcon, JWindow, SwingUtilities} |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
15 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
16 |
import scala.annotation.tailrec |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56774
diff
changeset
|
17 |
import scala.util.parsing.input.CharSequenceReader |
49406 | 18 |
|
53784 | 19 |
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities, Debug} |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
20 |
import org.gjt.sp.jedit.gui.KeyEventWorkaround |
61192 | 21 |
import org.gjt.sp.jedit.buffer.{JEditBuffer, LineManager} |
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
|
22 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter} |
59076
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
57849
diff
changeset
|
23 |
import org.gjt.sp.jedit.syntax.TokenMarker |
49406 | 24 |
|
25 |
||
26 |
object JEdit_Lib |
|
27 |
{ |
|
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
28 |
/* location within multi-screen environment */ |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
29 |
|
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
30 |
final case class Screen_Location(point: Point, bounds: Rectangle) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
31 |
{ |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
32 |
def relative(parent: Component, size: Dimension): Point = |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
33 |
{ |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
34 |
val w = size.width |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
35 |
val h = size.height |
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
|
36 |
|
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
37 |
val x0 = parent.getLocationOnScreen.x |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
38 |
val y0 = parent.getLocationOnScreen.y |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
39 |
val x1 = x0 + parent.getWidth - w |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
40 |
val y1 = y0 + parent.getHeight - h |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
41 |
val x2 = point.x min (bounds.x + bounds.width - w) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
42 |
val y2 = point.y min (bounds.y + bounds.height - h) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
43 |
|
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
44 |
val location = new Point((x2 min x1) max x0, (y2 min y1) max y0) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
45 |
SwingUtilities.convertPointFromScreen(location, parent) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
46 |
location |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
47 |
} |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
48 |
} |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
49 |
|
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
50 |
def screen_location(component: Component, point: Point): Screen_Location = |
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
|
51 |
{ |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
52 |
val screen_point = new Point(point.x, point.y) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
53 |
SwingUtilities.convertPointToScreen(screen_point, component) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
54 |
|
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
|
55 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
56 |
val screen_bounds = |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
57 |
(for { |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
58 |
device <- ge.getScreenDevices.iterator |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
59 |
config <- device.getConfigurations.iterator |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
60 |
bounds = config.getBounds |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
61 |
} yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
62 |
|
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
63 |
Screen_Location(screen_point, screen_bounds) |
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
|
64 |
} |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
65 |
|
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
66 |
|
53019 | 67 |
/* window geometry measurement */ |
68 |
||
69 |
private lazy val dummy_window = new JWindow |
|
70 |
||
71 |
final case class Window_Geometry(width: Int, height: Int, inner_width: Int, inner_height: Int) |
|
72 |
{ |
|
73 |
def deco_width: Int = width - inner_width |
|
74 |
def deco_height: Int = height - inner_height |
|
75 |
} |
|
76 |
||
77 |
def window_geometry(outer: Container, inner: Component): Window_Geometry = |
|
78 |
{ |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56930
diff
changeset
|
79 |
GUI_Thread.require {} |
53019 | 80 |
|
81 |
val old_content = dummy_window.getContentPane |
|
82 |
||
83 |
dummy_window.setContentPane(outer) |
|
84 |
dummy_window.pack |
|
85 |
dummy_window.revalidate |
|
86 |
||
87 |
val geometry = |
|
88 |
Window_Geometry( |
|
89 |
dummy_window.getWidth, dummy_window.getHeight, inner.getWidth, inner.getHeight) |
|
90 |
||
91 |
dummy_window.setContentPane(old_content) |
|
92 |
||
93 |
geometry |
|
94 |
} |
|
95 |
||
96 |
||
49406 | 97 |
/* buffers */ |
98 |
||
99 |
def buffer_text(buffer: JEditBuffer): String = |
|
100 |
buffer_lock(buffer) { buffer.getText(0, buffer.getLength) } |
|
101 |
||
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56774
diff
changeset
|
102 |
def buffer_reader(buffer: JEditBuffer): CharSequenceReader = |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56774
diff
changeset
|
103 |
new CharSequenceReader(buffer.getSegment(0, buffer.getLength)) |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56774
diff
changeset
|
104 |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
105 |
def buffer_mode(buffer: JEditBuffer): String = |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
106 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
107 |
val mode = buffer.getMode |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
108 |
if (mode == null) "" |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
109 |
else { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
110 |
val name = mode.getName |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
111 |
if (name == null) "" else name |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
112 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
113 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
114 |
|
49406 | 115 |
def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath |
116 |
||
61192 | 117 |
def buffer_line_manager(buffer: JEditBuffer): LineManager = |
118 |
Untyped.get[LineManager](buffer, "lineMgr") |
|
119 |
||
49406 | 120 |
|
121 |
/* main jEdit components */ |
|
122 |
||
123 |
def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator |
|
124 |
||
125 |
def jedit_buffer(name: String): Option[Buffer] = |
|
126 |
jedit_buffers().find(buffer => buffer_name(buffer) == name) |
|
127 |
||
56457
eea4bbe15745
tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents:
55812
diff
changeset
|
128 |
def jedit_buffer(name: Document.Node.Name): Option[Buffer] = |
eea4bbe15745
tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents:
55812
diff
changeset
|
129 |
jedit_buffer(name.node) |
eea4bbe15745
tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents:
55812
diff
changeset
|
130 |
|
49406 | 131 |
def jedit_views(): Iterator[View] = jEdit.getViews().iterator |
132 |
||
133 |
def jedit_text_areas(view: View): Iterator[JEditTextArea] = |
|
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
134 |
if (view == null) Iterator.empty |
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
135 |
else view.getEditPanes().iterator.filter(_ != null).map(_.getTextArea).filter(_ != null) |
49406 | 136 |
|
137 |
def jedit_text_areas(): Iterator[JEditTextArea] = |
|
138 |
jedit_views().flatMap(jedit_text_areas(_)) |
|
139 |
||
140 |
def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] = |
|
141 |
jedit_text_areas().filter(_.getBuffer == buffer) |
|
142 |
||
143 |
def buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
144 |
{ |
|
145 |
try { buffer.readLock(); body } |
|
146 |
finally { buffer.readUnlock() } |
|
147 |
} |
|
49407 | 148 |
|
50195
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
149 |
def buffer_edit[A](buffer: JEditBuffer)(body: => A): A = |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
150 |
{ |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
151 |
try { buffer.beginCompoundEdit(); body } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
152 |
finally { buffer.endCompoundEdit() } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
153 |
} |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
154 |
|
49407 | 155 |
|
50215 | 156 |
/* get text */ |
157 |
||
158 |
def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] = |
|
159 |
try { Some(buffer.getText(range.start, range.length)) } |
|
160 |
catch { case _: ArrayIndexOutOfBoundsException => None } |
|
161 |
||
53784 | 162 |
def try_get_text(text: String, range: Text.Range): Option[String] = |
163 |
try { Some(text.substring(range.start, range.stop)) } |
|
164 |
catch { case _: IndexOutOfBoundsException => None } |
|
165 |
||
50215 | 166 |
|
49407 | 167 |
/* point range */ |
168 |
||
169 |
def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range = |
|
56592
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
170 |
if (offset < 0) Text.Range.offside |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
171 |
else |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
172 |
buffer_lock(buffer) { |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
173 |
def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
174 |
try { |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
175 |
val c = text(offset) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
176 |
if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1))) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
177 |
Text.Range(offset, offset + 2) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
178 |
else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1))) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
179 |
Text.Range(offset - 1, offset + 1) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
180 |
else |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
181 |
Text.Range(offset, offset + 1) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
182 |
} |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
183 |
catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) } |
49407 | 184 |
} |
49408 | 185 |
|
186 |
||
55812 | 187 |
/* text ranges */ |
188 |
||
189 |
def buffer_range(buffer: JEditBuffer): Text.Range = |
|
190 |
Text.Range(0, buffer.getLength) |
|
49408 | 191 |
|
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
192 |
def line_range(buffer: JEditBuffer, line: Int): Text.Range = |
56589
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
193 |
Text.Range(buffer.getLineStartOffset(line), buffer.getLineEndOffset(line) min buffer.getLength) |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
194 |
|
56592
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
195 |
def caret_range(text_area: TextArea): Text.Range = |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
196 |
point_range(text_area.getBuffer, text_area.getCaretPosition) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
197 |
|
56593
0ea7c84110ac
back to unrestricted before_caret_range, which is important for quick editing at the end of line (amending 83777a91f5de);
wenzelm
parents:
56592
diff
changeset
|
198 |
def before_caret_range(text_area: TextArea, rendering: Rendering): Text.Range = |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
199 |
{ |
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
200 |
val snapshot = rendering.snapshot |
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
201 |
val former_caret = snapshot.revert(text_area.getCaretPosition) |
56593
0ea7c84110ac
back to unrestricted before_caret_range, which is important for quick editing at the end of line (amending 83777a91f5de);
wenzelm
parents:
56592
diff
changeset
|
202 |
snapshot.convert(Text.Range(former_caret - 1, former_caret)) |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
203 |
} |
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
204 |
|
49408 | 205 |
def visible_range(text_area: TextArea): Option[Text.Range] = |
206 |
{ |
|
207 |
val buffer = text_area.getBuffer |
|
208 |
val n = text_area.getVisibleLines |
|
209 |
if (n > 0) { |
|
210 |
val start = text_area.getScreenLineStartOffset(0) |
|
211 |
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
|
212 |
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
|
213 |
Some(Text.Range(start, end)) |
49408 | 214 |
} |
215 |
else None |
|
216 |
} |
|
217 |
||
218 |
def invalidate_range(text_area: TextArea, range: Text.Range) |
|
219 |
{ |
|
220 |
val buffer = text_area.getBuffer |
|
55812 | 221 |
buffer_range(buffer).try_restrict(range) match { |
222 |
case Some(range1) if !range1.is_singularity => |
|
56699 | 223 |
try { |
224 |
text_area.invalidateLineRange( |
|
225 |
buffer.getLineOfOffset(range1.start), |
|
226 |
buffer.getLineOfOffset(range1.stop)) |
|
227 |
} |
|
228 |
catch { case _: ArrayIndexOutOfBoundsException => } |
|
55812 | 229 |
case _ => |
230 |
} |
|
49408 | 231 |
} |
49409 | 232 |
|
233 |
||
234 |
/* graphics range */ |
|
235 |
||
60215 | 236 |
case class Gfx_Range(x: Int, y: Int, length: Int) |
49409 | 237 |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
238 |
// NB: jEdit always normalizes \r\n and \r to \n |
49409 | 239 |
// NB: last line lacks \n |
240 |
def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] = |
|
241 |
{ |
|
51507 | 242 |
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
|
243 |
val char_width = (metric.unit * metric.average).round.toInt |
50849 | 244 |
|
49409 | 245 |
val buffer = text_area.getBuffer |
246 |
||
247 |
val end = buffer.getLength |
|
248 |
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
|
249 |
|
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
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
val (q, r) = |
55549
5c40782f68b3
clarified special eol treatment (amending 3d55ef732cd7): allow last line to be empty, which means stop == end for second-last line;
wenzelm
parents:
53786
diff
changeset
|
254 |
if (try_get_text(buffer, Text.Range(stop - 1, stop)) == Some("\n")) |
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
|
255 |
(text_area.offsetToXY(stop - 1), char_width) |
55549
5c40782f68b3
clarified special eol treatment (amending 3d55ef732cd7): allow last line to be empty, which means stop == end for second-last line;
wenzelm
parents:
53786
diff
changeset
|
256 |
else if (stop >= end) |
5c40782f68b3
clarified special eol treatment (amending 3d55ef732cd7): allow last line to be empty, which means stop == end for second-last line;
wenzelm
parents:
53786
diff
changeset
|
257 |
(text_area.offsetToXY(end), char_width * (stop - end)) |
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
|
258 |
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
|
259 |
(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
|
260 |
} |
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
|
261 |
catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) } |
49409 | 262 |
|
263 |
if (p != null && q != null && p.x < q.x + r && p.y == q.y) |
|
50115 | 264 |
Some(Gfx_Range(p.x, p.y, q.x + r - p.x)) |
49409 | 265 |
else None |
266 |
} |
|
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
267 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
268 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
269 |
/* pixel range */ |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
270 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
271 |
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
|
272 |
{ |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
273 |
// coordinates wrt. inner painter component |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
274 |
val painter = text_area.getPainter |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
275 |
if (0 <= x && x < painter.getWidth && 0 <= y && y < painter.getHeight) { |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
276 |
val offset = text_area.xyToOffset(x, y, false) |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
277 |
if (offset >= 0) { |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
278 |
val range = point_range(text_area.getBuffer, offset) |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
279 |
gfx_range(text_area, range) match { |
60215 | 280 |
case Some(g) if g.x <= x && x < g.x + g.length => Some(range) |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
281 |
case _ => None |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
282 |
} |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
283 |
} |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
284 |
else None |
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
285 |
} |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
286 |
else None |
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
287 |
} |
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
|
288 |
|
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
|
289 |
|
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
|
290 |
/* 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
|
291 |
|
51507 | 292 |
abstract class Pretty_Metric extends Pretty.Metric |
293 |
{ |
|
294 |
def average: Double |
|
295 |
} |
|
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
|
296 |
|
51507 | 297 |
def pretty_metric(painter: TextAreaPainter): Pretty_Metric = |
298 |
new Pretty_Metric { |
|
299 |
def string_width(s: String): Double = |
|
300 |
painter.getFont.getStringBounds(s, painter.getFontRenderContext).getWidth |
|
301 |
||
61865 | 302 |
val unit = string_width(Symbol.space) max 1.0 |
51507 | 303 |
val average = string_width("mix") / (3 * unit) |
304 |
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
|
305 |
} |
52873 | 306 |
|
307 |
||
308 |
/* icons */ |
|
309 |
||
310 |
def load_icon(name: String): Icon = |
|
311 |
{ |
|
312 |
val name1 = |
|
313 |
if (name.startsWith("idea-icons/")) { |
|
60992 | 314 |
val file = File.platform_url(Path.explode("$JEDIT_HOME/dist/jars/idea-icons.jar")) |
52873 | 315 |
"jar:" + file + "!/" + name |
316 |
} |
|
317 |
else name |
|
318 |
val icon = GUIUtilities.loadIcon(name1) |
|
319 |
if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name) |
|
320 |
else icon |
|
321 |
} |
|
52874 | 322 |
|
323 |
def load_image_icon(name: String): ImageIcon = |
|
324 |
load_icon(name) match { |
|
325 |
case icon: ImageIcon => icon |
|
326 |
case _ => error("Bad image icon: " + name) |
|
327 |
} |
|
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
328 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
329 |
|
53231
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
330 |
/* key event handling */ |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
331 |
|
56930 | 332 |
def request_focus_view(alt_view: View = null) |
53231
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
333 |
{ |
56930 | 334 |
val view = if (alt_view != null) alt_view else jEdit.getActiveView() |
53231
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
335 |
if (view != null) { |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
336 |
val text_area = view.getTextArea |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
337 |
if (text_area != null) text_area.requestFocus |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
338 |
} |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
339 |
} |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
340 |
|
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
341 |
def propagate_key(view: View, evt: KeyEvent) |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
342 |
{ |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
343 |
if (view != null && !evt.isConsumed) |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
344 |
view.getInputHandler().processKeyEvent(evt, View.ACTION_BAR, false) |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
345 |
} |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
346 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
347 |
def key_listener( |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
348 |
key_typed: KeyEvent => Unit = _ => (), |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
349 |
key_pressed: KeyEvent => Unit = _ => (), |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
350 |
key_released: KeyEvent => Unit = _ => ()): KeyListener = |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
351 |
{ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
352 |
def process_key_event(evt0: KeyEvent, handle: KeyEvent => Unit) |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
353 |
{ |
53237 | 354 |
val evt = KeyEventWorkaround.processKeyEvent(evt0) |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
355 |
if (evt != null) handle(evt) |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
356 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
357 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
358 |
new KeyListener |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
359 |
{ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
360 |
def keyTyped(evt: KeyEvent) { process_key_event(evt, key_typed) } |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
361 |
def keyPressed(evt: KeyEvent) { process_key_event(evt, key_pressed) } |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
362 |
def keyReleased(evt: KeyEvent) { process_key_event(evt, key_released) } |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
363 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
364 |
} |
53784 | 365 |
|
366 |
def special_key(evt: KeyEvent): Boolean = |
|
367 |
{ |
|
59571 | 368 |
// cf. 5.2.0/jEdit/org/gjt/sp/jedit/gui/KeyEventWorkaround.java |
53784 | 369 |
val mod = evt.getModifiers |
370 |
(mod & InputEvent.CTRL_MASK) != 0 && (mod & InputEvent.ALT_MASK) == 0 || |
|
371 |
(mod & InputEvent.CTRL_MASK) == 0 && (mod & InputEvent.ALT_MASK) != 0 && |
|
372 |
!Debug.ALT_KEY_PRESSED_DISABLED || |
|
373 |
(mod & InputEvent.META_MASK) != 0 |
|
374 |
} |
|
49406 | 375 |
} |