src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Mon Sep 17 18:14:54 2012 +0200 (2012-09-17)
changeset 49409 7140a738aa49
parent 49408 3cfc114acd05
child 49710 21d88a631fcc
permissions -rw-r--r--
tuned signature;
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@49406
    12
wenzelm@49406
    13
import org.gjt.sp.jedit.{jEdit, Buffer, View}
wenzelm@49406
    14
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@49406
    15
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@49406
    16
wenzelm@49406
    17
wenzelm@49406
    18
object JEdit_Lib
wenzelm@49406
    19
{
wenzelm@49406
    20
  /* buffers */
wenzelm@49406
    21
wenzelm@49406
    22
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
    23
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@49406
    24
wenzelm@49406
    25
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@49406
    26
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@49406
    27
wenzelm@49406
    28
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
wenzelm@49406
    29
wenzelm@49406
    30
  def buffer_node_dummy(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@49406
    31
    Some(Document.Node.Name(buffer_name(buffer), buffer.getDirectory, buffer.getName))
wenzelm@49406
    32
wenzelm@49406
    33
  def buffer_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@49406
    34
  {
wenzelm@49406
    35
    val name = buffer_name(buffer)
wenzelm@49406
    36
    Thy_Header.thy_name(name).map(theory => Document.Node.Name(name, buffer.getDirectory, theory))
wenzelm@49406
    37
  }
wenzelm@49406
    38
wenzelm@49406
    39
wenzelm@49406
    40
  /* main jEdit components */
wenzelm@49406
    41
wenzelm@49406
    42
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@49406
    43
wenzelm@49406
    44
  def jedit_buffer(name: String): Option[Buffer] =
wenzelm@49406
    45
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
wenzelm@49406
    46
wenzelm@49406
    47
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@49406
    48
wenzelm@49406
    49
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@49406
    50
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@49406
    51
wenzelm@49406
    52
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@49406
    53
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@49406
    54
wenzelm@49406
    55
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@49406
    56
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@49406
    57
wenzelm@49406
    58
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
    59
  {
wenzelm@49406
    60
    try { buffer.readLock(); body }
wenzelm@49406
    61
    finally { buffer.readUnlock() }
wenzelm@49406
    62
  }
wenzelm@49407
    63
wenzelm@49407
    64
wenzelm@49407
    65
  /* point range */
wenzelm@49407
    66
wenzelm@49407
    67
  def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range =
wenzelm@49407
    68
    buffer_lock(buffer) {
wenzelm@49407
    69
      def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0)
wenzelm@49407
    70
      try {
wenzelm@49407
    71
        val c = text(offset)
wenzelm@49407
    72
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1)))
wenzelm@49407
    73
          Text.Range(offset, offset + 2)
wenzelm@49407
    74
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1)))
wenzelm@49407
    75
          Text.Range(offset - 1, offset + 1)
wenzelm@49407
    76
        else Text.Range(offset, offset + 1)
wenzelm@49407
    77
      }
wenzelm@49407
    78
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
wenzelm@49407
    79
    }
wenzelm@49408
    80
wenzelm@49408
    81
wenzelm@49408
    82
  /* proper line range */
wenzelm@49408
    83
wenzelm@49408
    84
  // NB: TextArea.getScreenLineEndOffset of last line is beyond Buffer.getLength
wenzelm@49408
    85
  def proper_line_range(buffer: JEditBuffer, start: Text.Offset, end: Text.Offset): Text.Range =
wenzelm@49408
    86
    Text.Range(start, end min buffer.getLength)
wenzelm@49408
    87
wenzelm@49408
    88
wenzelm@49408
    89
  /* visible text range */
wenzelm@49408
    90
wenzelm@49408
    91
  def visible_range(text_area: TextArea): Option[Text.Range] =
wenzelm@49408
    92
  {
wenzelm@49408
    93
    val buffer = text_area.getBuffer
wenzelm@49408
    94
    val n = text_area.getVisibleLines
wenzelm@49408
    95
    if (n > 0) {
wenzelm@49408
    96
      val start = text_area.getScreenLineStartOffset(0)
wenzelm@49408
    97
      val raw_end = text_area.getScreenLineEndOffset(n - 1)
wenzelm@49408
    98
      Some(proper_line_range(buffer, start, if (raw_end >= 0) raw_end else buffer.getLength))
wenzelm@49408
    99
    }
wenzelm@49408
   100
    else None
wenzelm@49408
   101
  }
wenzelm@49408
   102
wenzelm@49408
   103
  def invalidate_range(text_area: TextArea, range: Text.Range)
wenzelm@49408
   104
  {
wenzelm@49408
   105
    val buffer = text_area.getBuffer
wenzelm@49408
   106
    text_area.invalidateLineRange(
wenzelm@49408
   107
      buffer.getLineOfOffset(range.start),
wenzelm@49408
   108
      buffer.getLineOfOffset(range.stop))
wenzelm@49408
   109
  }
wenzelm@49409
   110
wenzelm@49409
   111
wenzelm@49409
   112
  /* char width */
wenzelm@49409
   113
wenzelm@49409
   114
  def char_width(text_area: TextArea): Int =
wenzelm@49409
   115
  {
wenzelm@49409
   116
    val painter = text_area.getPainter
wenzelm@49409
   117
    val font = painter.getFont
wenzelm@49409
   118
    val font_context = painter.getFontRenderContext
wenzelm@49409
   119
    font.getStringBounds(" ", font_context).getWidth.round.toInt
wenzelm@49409
   120
  }
wenzelm@49409
   121
wenzelm@49409
   122
wenzelm@49409
   123
  /* graphics range */
wenzelm@49409
   124
wenzelm@49409
   125
  class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@49409
   126
wenzelm@49409
   127
  // NB: jEdit already normalizes \r\n and \r to \n
wenzelm@49409
   128
  // NB: last line lacks \n
wenzelm@49409
   129
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@49409
   130
  {
wenzelm@49409
   131
    val buffer = text_area.getBuffer
wenzelm@49409
   132
wenzelm@49409
   133
    val p = text_area.offsetToXY(range.start)
wenzelm@49409
   134
wenzelm@49409
   135
    val end = buffer.getLength
wenzelm@49409
   136
    val stop = range.stop
wenzelm@49409
   137
    val (q, r) =
wenzelm@49409
   138
      if (stop >= end) (text_area.offsetToXY(end), char_width(text_area))
wenzelm@49409
   139
      else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n")
wenzelm@49409
   140
        (text_area.offsetToXY(stop - 1), char_width(text_area))
wenzelm@49409
   141
      else (text_area.offsetToXY(stop), 0)
wenzelm@49409
   142
wenzelm@49409
   143
    if (p != null && q != null && p.x < q.x + r && p.y == q.y)
wenzelm@49409
   144
      Some(new Gfx_Range(p.x, p.y, q.x + r - p.x))
wenzelm@49409
   145
    else None
wenzelm@49409
   146
  }
wenzelm@49406
   147
}
wenzelm@49406
   148