src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Mon Aug 05 23:57:29 2013 +0200 (2013-08-05)
changeset 52874 91032244e4ca
parent 52873 9e934d4fff00
child 53003 39da27fc6101
permissions -rw-r--r--
query process animation;
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@50658
    12
import java.awt.{Component, Container, Window, GraphicsEnvironment, Point, Rectangle}
wenzelm@52874
    13
import javax.swing.{Icon, ImageIcon}
wenzelm@49710
    14
wenzelm@49710
    15
import scala.annotation.tailrec
wenzelm@49406
    16
wenzelm@52873
    17
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities}
wenzelm@49406
    18
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@51492
    19
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter}
wenzelm@49406
    20
wenzelm@49406
    21
wenzelm@49406
    22
object JEdit_Lib
wenzelm@49406
    23
{
wenzelm@50554
    24
  /* bounds within multi-screen environment */
wenzelm@50554
    25
wenzelm@50554
    26
  def screen_bounds(screen_point: Point): Rectangle =
wenzelm@50554
    27
  {
wenzelm@50554
    28
    val ge = GraphicsEnvironment.getLocalGraphicsEnvironment
wenzelm@50554
    29
    (for {
wenzelm@50554
    30
      device <- ge.getScreenDevices.iterator
wenzelm@50554
    31
      config <- device.getConfigurations.iterator
wenzelm@50554
    32
      bounds = config.getBounds
wenzelm@50554
    33
    } yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds
wenzelm@50554
    34
  }
wenzelm@50554
    35
wenzelm@50554
    36
wenzelm@49712
    37
  /* GUI components */
wenzelm@49712
    38
wenzelm@49712
    39
  def get_parent(component: Component): Option[Container] =
wenzelm@49712
    40
    component.getParent match {
wenzelm@49712
    41
      case null => None
wenzelm@49712
    42
      case parent => Some(parent)
wenzelm@49712
    43
    }
wenzelm@49712
    44
wenzelm@49712
    45
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
wenzelm@49712
    46
    private var next_elem = get_parent(component)
wenzelm@49712
    47
    def hasNext(): Boolean = next_elem.isDefined
wenzelm@49712
    48
    def next(): Container =
wenzelm@49712
    49
      next_elem match {
wenzelm@49712
    50
        case Some(parent) =>
wenzelm@49712
    51
          next_elem = get_parent(parent)
wenzelm@49712
    52
          parent
wenzelm@49712
    53
        case None => Iterator.empty.next()
wenzelm@49712
    54
      }
wenzelm@49712
    55
  }
wenzelm@49712
    56
wenzelm@49712
    57
  def parent_window(component: Component): Option[Window] =
wenzelm@52478
    58
    ancestors(component).collectFirst({ case x: Window => x })
wenzelm@49710
    59
wenzelm@49710
    60
wenzelm@50186
    61
  /* basic tooltips, with multi-line support */
wenzelm@50186
    62
wenzelm@50186
    63
  def wrap_tooltip(text: String): String =
wenzelm@50186
    64
    if (text == "") null
wenzelm@50186
    65
    else "<html><pre>" + HTML.encode(text) + "</pre></html>"
wenzelm@50186
    66
wenzelm@50186
    67
wenzelm@49406
    68
  /* buffers */
wenzelm@49406
    69
wenzelm@49406
    70
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
    71
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@49406
    72
wenzelm@49406
    73
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@49406
    74
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@49406
    75
wenzelm@49406
    76
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
wenzelm@49406
    77
wenzelm@49406
    78
wenzelm@49406
    79
  /* main jEdit components */
wenzelm@49406
    80
wenzelm@49406
    81
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@49406
    82
wenzelm@49406
    83
  def jedit_buffer(name: String): Option[Buffer] =
wenzelm@49406
    84
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
wenzelm@49406
    85
wenzelm@49406
    86
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@49406
    87
wenzelm@49406
    88
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@49406
    89
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@49406
    90
wenzelm@49406
    91
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@49406
    92
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@49406
    93
wenzelm@49406
    94
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@49406
    95
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@49406
    96
wenzelm@49406
    97
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
    98
  {
wenzelm@49406
    99
    try { buffer.readLock(); body }
wenzelm@49406
   100
    finally { buffer.readUnlock() }
wenzelm@49406
   101
  }
wenzelm@49407
   102
wenzelm@50195
   103
  def buffer_edit[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@50195
   104
  {
wenzelm@50195
   105
    try { buffer.beginCompoundEdit(); body }
wenzelm@50195
   106
    finally { buffer.endCompoundEdit() }
wenzelm@50195
   107
  }
wenzelm@50195
   108
wenzelm@49407
   109
wenzelm@50215
   110
  /* get text */
wenzelm@50215
   111
wenzelm@50215
   112
  def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] =
wenzelm@50215
   113
    try { Some(buffer.getText(range.start, range.length)) }
wenzelm@50215
   114
    catch { case _: ArrayIndexOutOfBoundsException => None }
wenzelm@50215
   115
wenzelm@50215
   116
wenzelm@50363
   117
  /* buffer range */
wenzelm@50363
   118
wenzelm@50363
   119
  def buffer_range(buffer: JEditBuffer): Text.Range =
wenzelm@50363
   120
    Text.Range(0, (buffer.getLength - 1) max 0)
wenzelm@50363
   121
wenzelm@50363
   122
wenzelm@49407
   123
  /* point range */
wenzelm@49407
   124
wenzelm@49407
   125
  def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range =
wenzelm@49407
   126
    buffer_lock(buffer) {
wenzelm@49407
   127
      def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0)
wenzelm@49407
   128
      try {
wenzelm@49407
   129
        val c = text(offset)
wenzelm@49407
   130
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1)))
wenzelm@49407
   131
          Text.Range(offset, offset + 2)
wenzelm@49407
   132
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1)))
wenzelm@49407
   133
          Text.Range(offset - 1, offset + 1)
wenzelm@49407
   134
        else Text.Range(offset, offset + 1)
wenzelm@49407
   135
      }
wenzelm@49407
   136
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
wenzelm@49407
   137
    }
wenzelm@49408
   138
wenzelm@49408
   139
wenzelm@49408
   140
  /* visible text range */
wenzelm@49408
   141
wenzelm@49408
   142
  def visible_range(text_area: TextArea): Option[Text.Range] =
wenzelm@49408
   143
  {
wenzelm@49408
   144
    val buffer = text_area.getBuffer
wenzelm@49408
   145
    val n = text_area.getVisibleLines
wenzelm@49408
   146
    if (n > 0) {
wenzelm@49408
   147
      val start = text_area.getScreenLineStartOffset(0)
wenzelm@49408
   148
      val raw_end = text_area.getScreenLineEndOffset(n - 1)
wenzelm@49843
   149
      val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength
wenzelm@49843
   150
      Some(Text.Range(start, end))
wenzelm@49408
   151
    }
wenzelm@49408
   152
    else None
wenzelm@49408
   153
  }
wenzelm@49408
   154
wenzelm@49408
   155
  def invalidate_range(text_area: TextArea, range: Text.Range)
wenzelm@49408
   156
  {
wenzelm@49408
   157
    val buffer = text_area.getBuffer
wenzelm@49408
   158
    text_area.invalidateLineRange(
wenzelm@49408
   159
      buffer.getLineOfOffset(range.start),
wenzelm@49408
   160
      buffer.getLineOfOffset(range.stop))
wenzelm@49408
   161
  }
wenzelm@49409
   162
wenzelm@49409
   163
wenzelm@49409
   164
  /* graphics range */
wenzelm@49409
   165
wenzelm@50115
   166
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@49409
   167
wenzelm@49843
   168
  // NB: jEdit always normalizes \r\n and \r to \n
wenzelm@49409
   169
  // NB: last line lacks \n
wenzelm@49409
   170
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@49409
   171
  {
wenzelm@51507
   172
    val metric = pretty_metric(text_area.getPainter)
wenzelm@51492
   173
    val char_width = (metric.unit * metric.average).round.toInt
wenzelm@50849
   174
wenzelm@49409
   175
    val buffer = text_area.getBuffer
wenzelm@49409
   176
wenzelm@49409
   177
    val end = buffer.getLength
wenzelm@49409
   178
    val stop = range.stop
wenzelm@51078
   179
wenzelm@51078
   180
    val (p, q, r) =
wenzelm@51078
   181
      try {
wenzelm@51078
   182
        val p = text_area.offsetToXY(range.start)
wenzelm@51078
   183
        val (q, r) =
wenzelm@51078
   184
          if (stop >= end) (text_area.offsetToXY(end), char_width * (stop - end))
wenzelm@51078
   185
          else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n")
wenzelm@51078
   186
            (text_area.offsetToXY(stop - 1), char_width)
wenzelm@51078
   187
          else (text_area.offsetToXY(stop), 0)
wenzelm@51078
   188
        (p, q, r)
wenzelm@51078
   189
      }
wenzelm@51078
   190
      catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) }
wenzelm@49409
   191
wenzelm@49409
   192
    if (p != null && q != null && p.x < q.x + r && p.y == q.y)
wenzelm@50115
   193
      Some(Gfx_Range(p.x, p.y, q.x + r - p.x))
wenzelm@49409
   194
    else None
wenzelm@49409
   195
  }
wenzelm@49941
   196
wenzelm@49941
   197
wenzelm@49941
   198
  /* pixel range */
wenzelm@49941
   199
wenzelm@49941
   200
  def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] =
wenzelm@49941
   201
  {
wenzelm@50116
   202
    val range = point_range(text_area.getBuffer, text_area.xyToOffset(x, y, false))
wenzelm@49941
   203
    gfx_range(text_area, range) match {
wenzelm@49941
   204
      case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range)
wenzelm@49941
   205
      case _ => None
wenzelm@49941
   206
    }
wenzelm@49941
   207
  }
wenzelm@51492
   208
wenzelm@51492
   209
wenzelm@51492
   210
  /* pretty text metric */
wenzelm@51492
   211
wenzelm@51507
   212
  abstract class Pretty_Metric extends Pretty.Metric
wenzelm@51507
   213
  {
wenzelm@51507
   214
    def average: Double
wenzelm@51507
   215
  }
wenzelm@51492
   216
wenzelm@51507
   217
  def pretty_metric(painter: TextAreaPainter): Pretty_Metric =
wenzelm@51507
   218
    new Pretty_Metric {
wenzelm@51507
   219
      def string_width(s: String): Double =
wenzelm@51507
   220
        painter.getFont.getStringBounds(s, painter.getFontRenderContext).getWidth
wenzelm@51507
   221
wenzelm@51507
   222
      val unit = string_width(Pretty.space)
wenzelm@51507
   223
      val average = string_width("mix") / (3 * unit)
wenzelm@51507
   224
      def apply(s: String): Double = if (s == "\n") 1.0 else string_width(s) / unit
wenzelm@51492
   225
    }
wenzelm@52873
   226
wenzelm@52873
   227
wenzelm@52873
   228
  /* icons */
wenzelm@52873
   229
wenzelm@52873
   230
  def load_icon(name: String): Icon =
wenzelm@52873
   231
  {
wenzelm@52873
   232
    val name1 =
wenzelm@52873
   233
      if (name.startsWith("idea-icons/")) {
wenzelm@52873
   234
        val file =
wenzelm@52873
   235
          Isabelle_System.platform_file_url(Path.explode("$JEDIT_HOME/dist/jars/idea-icons.jar"))
wenzelm@52873
   236
        "jar:" + file + "!/" + name
wenzelm@52873
   237
      }
wenzelm@52873
   238
      else name
wenzelm@52873
   239
    val icon = GUIUtilities.loadIcon(name1)
wenzelm@52873
   240
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
wenzelm@52873
   241
    else icon
wenzelm@52873
   242
  }
wenzelm@52874
   243
wenzelm@52874
   244
  def load_image_icon(name: String): ImageIcon =
wenzelm@52874
   245
    load_icon(name) match {
wenzelm@52874
   246
      case icon: ImageIcon => icon
wenzelm@52874
   247
      case _ => error("Bad image icon: " + name)
wenzelm@52874
   248
    }
wenzelm@49406
   249
}
wenzelm@49406
   250