src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Mon Nov 26 16:16:47 2012 +0100 (2012-11-26 ago)
changeset 50215 97959912840a
parent 50195 863b1dfc396c
child 50363 2f8dc9e65401
permissions -rw-r--r--
more general sendback properties;
support for padding of line boundary, e.g. for ad-hoc insertion of commands via 'help';
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@49712
    12
import java.awt.{Component, Container, Frame, Window}
wenzelm@49710
    13
wenzelm@49710
    14
import scala.annotation.tailrec
wenzelm@49406
    15
wenzelm@49406
    16
import org.gjt.sp.jedit.{jEdit, Buffer, View}
wenzelm@49406
    17
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@49406
    18
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@49406
    19
wenzelm@49406
    20
wenzelm@49406
    21
object JEdit_Lib
wenzelm@49406
    22
{
wenzelm@49712
    23
  /* GUI components */
wenzelm@49712
    24
wenzelm@49712
    25
  def get_parent(component: Component): Option[Container] =
wenzelm@49712
    26
    component.getParent match {
wenzelm@49712
    27
      case null => None
wenzelm@49712
    28
      case parent => Some(parent)
wenzelm@49712
    29
    }
wenzelm@49712
    30
wenzelm@49712
    31
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
wenzelm@49712
    32
    private var next_elem = get_parent(component)
wenzelm@49712
    33
    def hasNext(): Boolean = next_elem.isDefined
wenzelm@49712
    34
    def next(): Container =
wenzelm@49712
    35
      next_elem match {
wenzelm@49712
    36
        case Some(parent) =>
wenzelm@49712
    37
          next_elem = get_parent(parent)
wenzelm@49712
    38
          parent
wenzelm@49712
    39
        case None => Iterator.empty.next()
wenzelm@49712
    40
      }
wenzelm@49712
    41
  }
wenzelm@49712
    42
wenzelm@49712
    43
  def parent_window(component: Component): Option[Window] =
wenzelm@49712
    44
    ancestors(component).find(_.isInstanceOf[Window]).map(_.asInstanceOf[Window])
wenzelm@49710
    45
wenzelm@49710
    46
  def parent_frame(component: Component): Option[Frame] =
wenzelm@49712
    47
    ancestors(component).find(_.isInstanceOf[Frame]).map(_.asInstanceOf[Frame])
wenzelm@49710
    48
wenzelm@49710
    49
wenzelm@50186
    50
  /* basic tooltips, with multi-line support */
wenzelm@50186
    51
wenzelm@50186
    52
  def wrap_tooltip(text: String): String =
wenzelm@50186
    53
    if (text == "") null
wenzelm@50186
    54
    else "<html><pre>" + HTML.encode(text) + "</pre></html>"
wenzelm@50186
    55
wenzelm@50186
    56
wenzelm@49406
    57
  /* buffers */
wenzelm@49406
    58
wenzelm@49406
    59
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
    60
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@49406
    61
wenzelm@49406
    62
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@49406
    63
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@49406
    64
wenzelm@49406
    65
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
wenzelm@49406
    66
wenzelm@49406
    67
  def buffer_node_dummy(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@49406
    68
    Some(Document.Node.Name(buffer_name(buffer), buffer.getDirectory, buffer.getName))
wenzelm@49406
    69
wenzelm@49406
    70
  def buffer_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@49406
    71
  {
wenzelm@49406
    72
    val name = buffer_name(buffer)
wenzelm@49406
    73
    Thy_Header.thy_name(name).map(theory => Document.Node.Name(name, buffer.getDirectory, theory))
wenzelm@49406
    74
  }
wenzelm@49406
    75
wenzelm@49406
    76
wenzelm@49406
    77
  /* main jEdit components */
wenzelm@49406
    78
wenzelm@49406
    79
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@49406
    80
wenzelm@49406
    81
  def jedit_buffer(name: String): Option[Buffer] =
wenzelm@49406
    82
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
wenzelm@49406
    83
wenzelm@49406
    84
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@49406
    85
wenzelm@49406
    86
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@49406
    87
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@49406
    88
wenzelm@49406
    89
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@49406
    90
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@49406
    91
wenzelm@49406
    92
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@49406
    93
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@49406
    94
wenzelm@49406
    95
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
    96
  {
wenzelm@49406
    97
    try { buffer.readLock(); body }
wenzelm@49406
    98
    finally { buffer.readUnlock() }
wenzelm@49406
    99
  }
wenzelm@49407
   100
wenzelm@50195
   101
  def buffer_edit[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@50195
   102
  {
wenzelm@50195
   103
    try { buffer.beginCompoundEdit(); body }
wenzelm@50195
   104
    finally { buffer.endCompoundEdit() }
wenzelm@50195
   105
  }
wenzelm@50195
   106
wenzelm@49407
   107
wenzelm@50215
   108
  /* get text */
wenzelm@50215
   109
wenzelm@50215
   110
  def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] =
wenzelm@50215
   111
    try { Some(buffer.getText(range.start, range.length)) }
wenzelm@50215
   112
    catch { case _: ArrayIndexOutOfBoundsException => None }
wenzelm@50215
   113
wenzelm@50215
   114
wenzelm@49407
   115
  /* point range */
wenzelm@49407
   116
wenzelm@49407
   117
  def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range =
wenzelm@49407
   118
    buffer_lock(buffer) {
wenzelm@49407
   119
      def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0)
wenzelm@49407
   120
      try {
wenzelm@49407
   121
        val c = text(offset)
wenzelm@49407
   122
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1)))
wenzelm@49407
   123
          Text.Range(offset, offset + 2)
wenzelm@49407
   124
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1)))
wenzelm@49407
   125
          Text.Range(offset - 1, offset + 1)
wenzelm@49407
   126
        else Text.Range(offset, offset + 1)
wenzelm@49407
   127
      }
wenzelm@49407
   128
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
wenzelm@49407
   129
    }
wenzelm@49408
   130
wenzelm@49408
   131
wenzelm@49408
   132
  /* visible text range */
wenzelm@49408
   133
wenzelm@49408
   134
  def visible_range(text_area: TextArea): Option[Text.Range] =
wenzelm@49408
   135
  {
wenzelm@49408
   136
    val buffer = text_area.getBuffer
wenzelm@49408
   137
    val n = text_area.getVisibleLines
wenzelm@49408
   138
    if (n > 0) {
wenzelm@49408
   139
      val start = text_area.getScreenLineStartOffset(0)
wenzelm@49408
   140
      val raw_end = text_area.getScreenLineEndOffset(n - 1)
wenzelm@49843
   141
      val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength
wenzelm@49843
   142
      Some(Text.Range(start, end))
wenzelm@49408
   143
    }
wenzelm@49408
   144
    else None
wenzelm@49408
   145
  }
wenzelm@49408
   146
wenzelm@49408
   147
  def invalidate_range(text_area: TextArea, range: Text.Range)
wenzelm@49408
   148
  {
wenzelm@49408
   149
    val buffer = text_area.getBuffer
wenzelm@49408
   150
    text_area.invalidateLineRange(
wenzelm@49408
   151
      buffer.getLineOfOffset(range.start),
wenzelm@49408
   152
      buffer.getLineOfOffset(range.stop))
wenzelm@49408
   153
  }
wenzelm@49409
   154
wenzelm@49409
   155
wenzelm@49409
   156
  /* char width */
wenzelm@49409
   157
wenzelm@49409
   158
  def char_width(text_area: TextArea): Int =
wenzelm@49409
   159
  {
wenzelm@49409
   160
    val painter = text_area.getPainter
wenzelm@49409
   161
    val font = painter.getFont
wenzelm@49409
   162
    val font_context = painter.getFontRenderContext
wenzelm@49409
   163
    font.getStringBounds(" ", font_context).getWidth.round.toInt
wenzelm@49409
   164
  }
wenzelm@49409
   165
wenzelm@49409
   166
wenzelm@49409
   167
  /* graphics range */
wenzelm@49409
   168
wenzelm@50115
   169
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@49409
   170
wenzelm@49843
   171
  // NB: jEdit always normalizes \r\n and \r to \n
wenzelm@49409
   172
  // NB: last line lacks \n
wenzelm@49409
   173
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@49409
   174
  {
wenzelm@49409
   175
    val buffer = text_area.getBuffer
wenzelm@49409
   176
wenzelm@49409
   177
    val p = text_area.offsetToXY(range.start)
wenzelm@49409
   178
wenzelm@49409
   179
    val end = buffer.getLength
wenzelm@49409
   180
    val stop = range.stop
wenzelm@49409
   181
    val (q, r) =
wenzelm@49843
   182
      if (stop >= end) (text_area.offsetToXY(end), char_width(text_area) * (stop - end))
wenzelm@49409
   183
      else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n")
wenzelm@49409
   184
        (text_area.offsetToXY(stop - 1), char_width(text_area))
wenzelm@49409
   185
      else (text_area.offsetToXY(stop), 0)
wenzelm@49409
   186
wenzelm@49409
   187
    if (p != null && q != null && p.x < q.x + r && p.y == q.y)
wenzelm@50115
   188
      Some(Gfx_Range(p.x, p.y, q.x + r - p.x))
wenzelm@49409
   189
    else None
wenzelm@49409
   190
  }
wenzelm@49941
   191
wenzelm@49941
   192
wenzelm@49941
   193
  /* pixel range */
wenzelm@49941
   194
wenzelm@49941
   195
  def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] =
wenzelm@49941
   196
  {
wenzelm@50116
   197
    val range = point_range(text_area.getBuffer, text_area.xyToOffset(x, y, false))
wenzelm@49941
   198
    gfx_range(text_area, range) match {
wenzelm@49941
   199
      case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range)
wenzelm@49941
   200
      case _ => None
wenzelm@49941
   201
    }
wenzelm@49941
   202
  }
wenzelm@49406
   203
}
wenzelm@49406
   204