src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Fri May 02 13:52:45 2014 +0200 (2014-05-02)
changeset 56823 37be55461dbe
parent 56774 2d39c313f39e
child 56930 42b5d216dc8c
permissions -rw-r--r--
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
prefer non-strict Symbol.decode, since Reader[Char] may present symbols in either way;
wenzelm@49406
     1
/*  Title:      Tools/jEdit/src/jedit_lib.scala
wenzelm@49406
     2
    Author:     Makarius
wenzelm@49406
     3
wenzelm@49406
     4
Misc library functions for jEdit.
wenzelm@49406
     5
*/
wenzelm@49406
     6
wenzelm@49406
     7
package isabelle.jedit
wenzelm@49406
     8
wenzelm@49406
     9
wenzelm@49406
    10
import isabelle._
wenzelm@49406
    11
wenzelm@53712
    12
import java.awt.{Component, Container, GraphicsEnvironment, Point, Rectangle, Dimension}
wenzelm@53784
    13
import java.awt.event.{InputEvent, KeyEvent, KeyListener}
wenzelm@53247
    14
import javax.swing.{Icon, ImageIcon, JWindow, SwingUtilities}
wenzelm@49710
    15
wenzelm@49710
    16
import scala.annotation.tailrec
wenzelm@56823
    17
import scala.util.parsing.input.CharSequenceReader
wenzelm@49406
    18
wenzelm@53784
    19
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities, Debug}
wenzelm@53226
    20
import org.gjt.sp.jedit.gui.KeyEventWorkaround
wenzelm@49406
    21
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@51492
    22
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter}
wenzelm@49406
    23
wenzelm@49406
    24
wenzelm@49406
    25
object JEdit_Lib
wenzelm@49406
    26
{
wenzelm@53247
    27
  /* location within multi-screen environment */
wenzelm@53247
    28
wenzelm@53247
    29
  final case class Screen_Location(point: Point, bounds: Rectangle)
wenzelm@53247
    30
  {
wenzelm@53247
    31
    def relative(parent: Component, size: Dimension): Point =
wenzelm@53247
    32
    {
wenzelm@53247
    33
      val w = size.width
wenzelm@53247
    34
      val h = size.height
wenzelm@50554
    35
wenzelm@53247
    36
      val x0 = parent.getLocationOnScreen.x
wenzelm@53247
    37
      val y0 = parent.getLocationOnScreen.y
wenzelm@53247
    38
      val x1 = x0 + parent.getWidth - w
wenzelm@53247
    39
      val y1 = y0 + parent.getHeight - h
wenzelm@53247
    40
      val x2 = point.x min (bounds.x + bounds.width - w)
wenzelm@53247
    41
      val y2 = point.y min (bounds.y + bounds.height - h)
wenzelm@53247
    42
wenzelm@53247
    43
      val location = new Point((x2 min x1) max x0, (y2 min y1) max y0)
wenzelm@53247
    44
      SwingUtilities.convertPointFromScreen(location, parent)
wenzelm@53247
    45
      location
wenzelm@53247
    46
    }
wenzelm@53247
    47
  }
wenzelm@53247
    48
wenzelm@53247
    49
  def screen_location(component: Component, point: Point): Screen_Location =
wenzelm@50554
    50
  {
wenzelm@53247
    51
    val screen_point = new Point(point.x, point.y)
wenzelm@53247
    52
    SwingUtilities.convertPointToScreen(screen_point, component)
wenzelm@53247
    53
wenzelm@50554
    54
    val ge = GraphicsEnvironment.getLocalGraphicsEnvironment
wenzelm@53247
    55
    val screen_bounds =
wenzelm@53247
    56
      (for {
wenzelm@53247
    57
        device <- ge.getScreenDevices.iterator
wenzelm@53247
    58
        config <- device.getConfigurations.iterator
wenzelm@53247
    59
        bounds = config.getBounds
wenzelm@53247
    60
      } yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds
wenzelm@53247
    61
wenzelm@53247
    62
    Screen_Location(screen_point, screen_bounds)
wenzelm@50554
    63
  }
wenzelm@50554
    64
wenzelm@50554
    65
wenzelm@53019
    66
  /* window geometry measurement */
wenzelm@53019
    67
wenzelm@53019
    68
  private lazy val dummy_window = new JWindow
wenzelm@53019
    69
wenzelm@53019
    70
  final case class Window_Geometry(width: Int, height: Int, inner_width: Int, inner_height: Int)
wenzelm@53019
    71
  {
wenzelm@53019
    72
    def deco_width: Int = width - inner_width
wenzelm@53019
    73
    def deco_height: Int = height - inner_height
wenzelm@53019
    74
  }
wenzelm@53019
    75
wenzelm@53019
    76
  def window_geometry(outer: Container, inner: Component): Window_Geometry =
wenzelm@53019
    77
  {
wenzelm@56662
    78
    Swing_Thread.require {}
wenzelm@53019
    79
wenzelm@53019
    80
    val old_content = dummy_window.getContentPane
wenzelm@53019
    81
wenzelm@53019
    82
    dummy_window.setContentPane(outer)
wenzelm@53019
    83
    dummy_window.pack
wenzelm@53019
    84
    dummy_window.revalidate
wenzelm@53019
    85
wenzelm@53019
    86
    val geometry =
wenzelm@53019
    87
      Window_Geometry(
wenzelm@53019
    88
        dummy_window.getWidth, dummy_window.getHeight, inner.getWidth, inner.getHeight)
wenzelm@53019
    89
wenzelm@53019
    90
    dummy_window.setContentPane(old_content)
wenzelm@53019
    91
wenzelm@53019
    92
    geometry
wenzelm@53019
    93
  }
wenzelm@53019
    94
wenzelm@53019
    95
wenzelm@49406
    96
  /* buffers */
wenzelm@49406
    97
wenzelm@49406
    98
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@49406
    99
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@49406
   100
wenzelm@56823
   101
  def buffer_reader(buffer: JEditBuffer): CharSequenceReader =
wenzelm@56823
   102
    new CharSequenceReader(buffer.getSegment(0, buffer.getLength))
wenzelm@56823
   103
wenzelm@53274
   104
  def buffer_mode(buffer: JEditBuffer): String =
wenzelm@53274
   105
  {
wenzelm@53274
   106
    val mode = buffer.getMode
wenzelm@53274
   107
    if (mode == null) ""
wenzelm@53274
   108
    else {
wenzelm@53274
   109
      val name = mode.getName
wenzelm@53274
   110
      if (name == null) "" else name
wenzelm@53274
   111
    }
wenzelm@53274
   112
  }
wenzelm@53274
   113
wenzelm@49406
   114
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
wenzelm@49406
   115
wenzelm@49406
   116
wenzelm@49406
   117
  /* main jEdit components */
wenzelm@49406
   118
wenzelm@49406
   119
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@49406
   120
wenzelm@49406
   121
  def jedit_buffer(name: String): Option[Buffer] =
wenzelm@49406
   122
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
wenzelm@49406
   123
wenzelm@56457
   124
  def jedit_buffer(name: Document.Node.Name): Option[Buffer] =
wenzelm@56457
   125
    jedit_buffer(name.node)
wenzelm@56457
   126
wenzelm@49406
   127
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@49406
   128
wenzelm@49406
   129
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@56587
   130
    if (view == null) Iterator.empty
wenzelm@56587
   131
    else view.getEditPanes().iterator.filter(_ != null).map(_.getTextArea).filter(_ != null)
wenzelm@49406
   132
wenzelm@49406
   133
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@49406
   134
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@49406
   135
wenzelm@49406
   136
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@49406
   137
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@49406
   138
wenzelm@49406
   139
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
   140
  {
wenzelm@49406
   141
    try { buffer.readLock(); body }
wenzelm@49406
   142
    finally { buffer.readUnlock() }
wenzelm@49406
   143
  }
wenzelm@49407
   144
wenzelm@50195
   145
  def buffer_edit[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@50195
   146
  {
wenzelm@50195
   147
    try { buffer.beginCompoundEdit(); body }
wenzelm@50195
   148
    finally { buffer.endCompoundEdit() }
wenzelm@50195
   149
  }
wenzelm@50195
   150
wenzelm@49407
   151
wenzelm@50215
   152
  /* get text */
wenzelm@50215
   153
wenzelm@50215
   154
  def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] =
wenzelm@50215
   155
    try { Some(buffer.getText(range.start, range.length)) }
wenzelm@50215
   156
    catch { case _: ArrayIndexOutOfBoundsException => None }
wenzelm@50215
   157
wenzelm@53784
   158
  def try_get_text(text: String, range: Text.Range): Option[String] =
wenzelm@53784
   159
    try { Some(text.substring(range.start, range.stop)) }
wenzelm@53784
   160
    catch { case _: IndexOutOfBoundsException => None }
wenzelm@53784
   161
wenzelm@50215
   162
wenzelm@49407
   163
  /* point range */
wenzelm@49407
   164
wenzelm@49407
   165
  def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range =
wenzelm@56592
   166
    if (offset < 0) Text.Range.offside
wenzelm@56592
   167
    else
wenzelm@56592
   168
      buffer_lock(buffer) {
wenzelm@56592
   169
        def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0)
wenzelm@56592
   170
        try {
wenzelm@56592
   171
          val c = text(offset)
wenzelm@56592
   172
          if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1)))
wenzelm@56592
   173
            Text.Range(offset, offset + 2)
wenzelm@56592
   174
          else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1)))
wenzelm@56592
   175
            Text.Range(offset - 1, offset + 1)
wenzelm@56592
   176
          else
wenzelm@56592
   177
            Text.Range(offset, offset + 1)
wenzelm@56592
   178
        }
wenzelm@56592
   179
        catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
wenzelm@49407
   180
      }
wenzelm@49408
   181
wenzelm@49408
   182
wenzelm@55812
   183
  /* text ranges */
wenzelm@55812
   184
wenzelm@55812
   185
  def buffer_range(buffer: JEditBuffer): Text.Range =
wenzelm@55812
   186
    Text.Range(0, buffer.getLength)
wenzelm@49408
   187
wenzelm@56587
   188
  def line_range(buffer: JEditBuffer, line: Int): Text.Range =
wenzelm@56589
   189
    Text.Range(buffer.getLineStartOffset(line), buffer.getLineEndOffset(line) min buffer.getLength)
wenzelm@56587
   190
wenzelm@56592
   191
  def caret_range(text_area: TextArea): Text.Range =
wenzelm@56592
   192
    point_range(text_area.getBuffer, text_area.getCaretPosition)
wenzelm@56592
   193
wenzelm@56593
   194
  def before_caret_range(text_area: TextArea, rendering: Rendering): Text.Range =
wenzelm@56587
   195
  {
wenzelm@56587
   196
    val snapshot = rendering.snapshot
wenzelm@56587
   197
    val former_caret = snapshot.revert(text_area.getCaretPosition)
wenzelm@56593
   198
    snapshot.convert(Text.Range(former_caret - 1, former_caret))
wenzelm@56587
   199
  }
wenzelm@56587
   200
wenzelm@49408
   201
  def visible_range(text_area: TextArea): Option[Text.Range] =
wenzelm@49408
   202
  {
wenzelm@49408
   203
    val buffer = text_area.getBuffer
wenzelm@49408
   204
    val n = text_area.getVisibleLines
wenzelm@49408
   205
    if (n > 0) {
wenzelm@49408
   206
      val start = text_area.getScreenLineStartOffset(0)
wenzelm@49408
   207
      val raw_end = text_area.getScreenLineEndOffset(n - 1)
wenzelm@49843
   208
      val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength
wenzelm@49843
   209
      Some(Text.Range(start, end))
wenzelm@49408
   210
    }
wenzelm@49408
   211
    else None
wenzelm@49408
   212
  }
wenzelm@49408
   213
wenzelm@49408
   214
  def invalidate_range(text_area: TextArea, range: Text.Range)
wenzelm@49408
   215
  {
wenzelm@49408
   216
    val buffer = text_area.getBuffer
wenzelm@55812
   217
    buffer_range(buffer).try_restrict(range) match {
wenzelm@55812
   218
      case Some(range1) if !range1.is_singularity =>
wenzelm@56699
   219
        try {
wenzelm@56699
   220
          text_area.invalidateLineRange(
wenzelm@56699
   221
            buffer.getLineOfOffset(range1.start),
wenzelm@56699
   222
            buffer.getLineOfOffset(range1.stop))
wenzelm@56699
   223
        }
wenzelm@56699
   224
        catch { case _: ArrayIndexOutOfBoundsException => }
wenzelm@55812
   225
      case _ =>
wenzelm@55812
   226
    }
wenzelm@49408
   227
  }
wenzelm@49409
   228
wenzelm@49409
   229
wenzelm@49409
   230
  /* graphics range */
wenzelm@49409
   231
wenzelm@50115
   232
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@49409
   233
wenzelm@49843
   234
  // NB: jEdit always normalizes \r\n and \r to \n
wenzelm@49409
   235
  // NB: last line lacks \n
wenzelm@49409
   236
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@49409
   237
  {
wenzelm@51507
   238
    val metric = pretty_metric(text_area.getPainter)
wenzelm@51492
   239
    val char_width = (metric.unit * metric.average).round.toInt
wenzelm@50849
   240
wenzelm@49409
   241
    val buffer = text_area.getBuffer
wenzelm@49409
   242
wenzelm@49409
   243
    val end = buffer.getLength
wenzelm@49409
   244
    val stop = range.stop
wenzelm@51078
   245
wenzelm@51078
   246
    val (p, q, r) =
wenzelm@51078
   247
      try {
wenzelm@51078
   248
        val p = text_area.offsetToXY(range.start)
wenzelm@51078
   249
        val (q, r) =
wenzelm@55549
   250
          if (try_get_text(buffer, Text.Range(stop - 1, stop)) == Some("\n"))
wenzelm@51078
   251
            (text_area.offsetToXY(stop - 1), char_width)
wenzelm@55549
   252
          else if (stop >= end)
wenzelm@55549
   253
            (text_area.offsetToXY(end), char_width * (stop - end))
wenzelm@51078
   254
          else (text_area.offsetToXY(stop), 0)
wenzelm@51078
   255
        (p, q, r)
wenzelm@51078
   256
      }
wenzelm@51078
   257
      catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) }
wenzelm@49409
   258
wenzelm@49409
   259
    if (p != null && q != null && p.x < q.x + r && p.y == q.y)
wenzelm@50115
   260
      Some(Gfx_Range(p.x, p.y, q.x + r - p.x))
wenzelm@49409
   261
    else None
wenzelm@49409
   262
  }
wenzelm@49941
   263
wenzelm@49941
   264
wenzelm@49941
   265
  /* pixel range */
wenzelm@49941
   266
wenzelm@49941
   267
  def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] =
wenzelm@49941
   268
  {
wenzelm@53183
   269
    // coordinates wrt. inner painter component
wenzelm@53183
   270
    val painter = text_area.getPainter
wenzelm@53183
   271
    if (0 <= x && x < painter.getWidth && 0 <= y && y < painter.getHeight) {
wenzelm@53183
   272
      val offset = text_area.xyToOffset(x, y, false)
wenzelm@53183
   273
      if (offset >= 0) {
wenzelm@53183
   274
        val range = point_range(text_area.getBuffer, offset)
wenzelm@53183
   275
        gfx_range(text_area, range) match {
wenzelm@53183
   276
          case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range)
wenzelm@53183
   277
          case _ => None
wenzelm@53183
   278
        }
wenzelm@53183
   279
      }
wenzelm@53183
   280
      else None
wenzelm@49941
   281
    }
wenzelm@53183
   282
    else None
wenzelm@49941
   283
  }
wenzelm@51492
   284
wenzelm@51492
   285
wenzelm@51492
   286
  /* pretty text metric */
wenzelm@51492
   287
wenzelm@51507
   288
  abstract class Pretty_Metric extends Pretty.Metric
wenzelm@51507
   289
  {
wenzelm@51507
   290
    def average: Double
wenzelm@51507
   291
  }
wenzelm@51492
   292
wenzelm@51507
   293
  def pretty_metric(painter: TextAreaPainter): Pretty_Metric =
wenzelm@51507
   294
    new Pretty_Metric {
wenzelm@51507
   295
      def string_width(s: String): Double =
wenzelm@51507
   296
        painter.getFont.getStringBounds(s, painter.getFontRenderContext).getWidth
wenzelm@51507
   297
wenzelm@51507
   298
      val unit = string_width(Pretty.space)
wenzelm@51507
   299
      val average = string_width("mix") / (3 * unit)
wenzelm@51507
   300
      def apply(s: String): Double = if (s == "\n") 1.0 else string_width(s) / unit
wenzelm@51492
   301
    }
wenzelm@52873
   302
wenzelm@52873
   303
wenzelm@52873
   304
  /* icons */
wenzelm@52873
   305
wenzelm@52873
   306
  def load_icon(name: String): Icon =
wenzelm@52873
   307
  {
wenzelm@52873
   308
    val name1 =
wenzelm@52873
   309
      if (name.startsWith("idea-icons/")) {
wenzelm@52873
   310
        val file =
wenzelm@52873
   311
          Isabelle_System.platform_file_url(Path.explode("$JEDIT_HOME/dist/jars/idea-icons.jar"))
wenzelm@52873
   312
        "jar:" + file + "!/" + name
wenzelm@52873
   313
      }
wenzelm@52873
   314
      else name
wenzelm@52873
   315
    val icon = GUIUtilities.loadIcon(name1)
wenzelm@52873
   316
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
wenzelm@52873
   317
    else icon
wenzelm@52873
   318
  }
wenzelm@52874
   319
wenzelm@52874
   320
  def load_image_icon(name: String): ImageIcon =
wenzelm@52874
   321
    load_icon(name) match {
wenzelm@52874
   322
      case icon: ImageIcon => icon
wenzelm@52874
   323
      case _ => error("Bad image icon: " + name)
wenzelm@52874
   324
    }
wenzelm@53226
   325
wenzelm@53226
   326
wenzelm@53231
   327
  /* key event handling */
wenzelm@53231
   328
wenzelm@53231
   329
  def request_focus_view
wenzelm@53231
   330
  {
wenzelm@53231
   331
    val view = jEdit.getActiveView()
wenzelm@53231
   332
    if (view != null) {
wenzelm@53231
   333
      val text_area = view.getTextArea
wenzelm@53231
   334
      if (text_area != null) text_area.requestFocus
wenzelm@53231
   335
    }
wenzelm@53231
   336
  }
wenzelm@53231
   337
wenzelm@53231
   338
  def propagate_key(view: View, evt: KeyEvent)
wenzelm@53231
   339
  {
wenzelm@53231
   340
    if (view != null && !evt.isConsumed)
wenzelm@53231
   341
      view.getInputHandler().processKeyEvent(evt, View.ACTION_BAR, false)
wenzelm@53231
   342
  }
wenzelm@53226
   343
wenzelm@53226
   344
  def key_listener(
wenzelm@53226
   345
    key_typed: KeyEvent => Unit = _ => (),
wenzelm@53226
   346
    key_pressed: KeyEvent => Unit = _ => (),
wenzelm@53226
   347
    key_released: KeyEvent => Unit = _ => ()): KeyListener =
wenzelm@53226
   348
  {
wenzelm@53226
   349
    def process_key_event(evt0: KeyEvent, handle: KeyEvent => Unit)
wenzelm@53226
   350
    {
wenzelm@53237
   351
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53226
   352
      if (evt != null) handle(evt)
wenzelm@53226
   353
    }
wenzelm@53226
   354
wenzelm@53226
   355
    new KeyListener
wenzelm@53226
   356
    {
wenzelm@53226
   357
      def keyTyped(evt: KeyEvent) { process_key_event(evt, key_typed) }
wenzelm@53226
   358
      def keyPressed(evt: KeyEvent) { process_key_event(evt, key_pressed) }
wenzelm@53226
   359
      def keyReleased(evt: KeyEvent) { process_key_event(evt, key_released) }
wenzelm@53226
   360
    }
wenzelm@53226
   361
  }
wenzelm@53784
   362
wenzelm@53784
   363
  def special_key(evt: KeyEvent): Boolean =
wenzelm@53784
   364
  {
wenzelm@53784
   365
    // cf. 5.1.0/jEdit/org/gjt/sp/jedit/gui/KeyEventWorkaround.java
wenzelm@53784
   366
    val mod = evt.getModifiers
wenzelm@53784
   367
    (mod & InputEvent.CTRL_MASK) != 0 && (mod & InputEvent.ALT_MASK) == 0 ||
wenzelm@53784
   368
    (mod & InputEvent.CTRL_MASK) == 0 && (mod & InputEvent.ALT_MASK) != 0 &&
wenzelm@53784
   369
      !Debug.ALT_KEY_PRESSED_DISABLED ||
wenzelm@53784
   370
    (mod & InputEvent.META_MASK) != 0
wenzelm@53784
   371
  }
wenzelm@49406
   372
}
wenzelm@49406
   373