src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Sun, 10 Nov 2024 16:04:56 +0100
changeset 81426 56bab51e02c1
parent 81423 056657540039
child 81434 1935ed4fe9c2
permissions -rw-r--r--
performance tuning: more incremental update of buffer content;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_lib.scala
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     3
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     4
Misc library functions for jEdit.
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     5
*/
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     6
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     8
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     9
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    10
import isabelle._
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    11
65469
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    12
import java.io.{File => JFile}
72899
8732315dfafa tuned signature;
wenzelm
parents: 71861
diff changeset
    13
import java.awt.{Component, Container, GraphicsEnvironment, Point, Rectangle, Dimension, Toolkit}
53784
322a3ff42b33 completion popup for history text field;
wenzelm
parents: 53712
diff changeset
    14
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
    15
import javax.swing.{Icon, ImageIcon, JWindow, SwingUtilities}
49710
21d88a631fcc refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents: 49409
diff changeset
    16
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
73354
79b120d1c1a3 tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    18
import scala.jdk.CollectionConverters._
81426
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
    19
import scala.annotation.tailrec
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    20
62264
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 61865
diff changeset
    21
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities, Debug, EditPane}
72927
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
    22
import org.gjt.sp.jedit.io.{FileVFS, VFSManager}
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
    23
import org.gjt.sp.jedit.gui.{KeyEventWorkaround, KeyEventTranslator}
61192
98eba31c51f8 tuned signature;
wenzelm
parents: 60992
diff changeset
    24
import org.gjt.sp.jedit.buffer.{JEditBuffer, LineManager}
81299
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
    25
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter, Selection}
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    26
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    27
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    28
object JEdit_Lib {
71861
1330fa4a2b85 clarified signature;
wenzelm
parents: 71601
diff changeset
    29
  /* jEdit directories */
1330fa4a2b85 clarified signature;
wenzelm
parents: 71601
diff changeset
    30
1330fa4a2b85 clarified signature;
wenzelm
parents: 71601
diff changeset
    31
  def directories: List[JFile] =
1330fa4a2b85 clarified signature;
wenzelm
parents: 71601
diff changeset
    32
    (Option(jEdit.getSettingsDirectory).toList ::: List(jEdit.getJEditHome)).map(new JFile(_))
1330fa4a2b85 clarified signature;
wenzelm
parents: 71601
diff changeset
    33
1330fa4a2b85 clarified signature;
wenzelm
parents: 71601
diff changeset
    34
53019
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    35
  /* window geometry measurement */
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    36
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    37
  private lazy val dummy_window = new JWindow
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    38
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    39
  final case class Window_Geometry(width: Int, height: Int, inner_width: Int, inner_height: Int) {
53019
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    40
    def deco_width: Int = width - inner_width
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    41
    def deco_height: Int = height - inner_height
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    42
  }
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    43
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    44
  def window_geometry(outer: Container, inner: Component): Window_Geometry = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56930
diff changeset
    45
    GUI_Thread.require {}
53019
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    46
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    47
    val old_content = dummy_window.getContentPane
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    48
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    49
    dummy_window.setContentPane(outer)
81401
874e12fb6c3d tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81340
diff changeset
    50
    dummy_window.pack()
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73354
diff changeset
    51
    dummy_window.revalidate()
53019
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    52
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    53
    val geometry =
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    54
      Window_Geometry(
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    55
        dummy_window.getWidth, dummy_window.getHeight, inner.getWidth, inner.getHeight)
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    56
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    57
    dummy_window.setContentPane(old_content)
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    58
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    59
    geometry
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    60
  }
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    61
be9e94594b96 more general window_geometry;
wenzelm
parents: 53003
diff changeset
    62
76782
a62a609b5db2 more robust;
wenzelm
parents: 75701
diff changeset
    63
  /* plain files */
65469
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    64
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    65
  def is_file(name: String): Boolean =
76782
a62a609b5db2 more robust;
wenzelm
parents: 75701
diff changeset
    66
    name != null && name.nonEmpty && VFSManager.getVFSForPath(name).isInstanceOf[FileVFS]
65469
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    67
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    68
  def check_file(name: String): Option[JFile] =
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    69
    if (is_file(name)) Some(new JFile(name)) else None
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    70
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    71
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    72
  /* buffers */
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    73
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    74
  def buffer_text(buffer: JEditBuffer): String =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    75
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    76
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56774
diff changeset
    77
  def buffer_reader(buffer: JEditBuffer): CharSequenceReader =
64824
330ec9bc4b75 tuned signature;
wenzelm
parents: 64770
diff changeset
    78
    Scan.char_reader(buffer.getSegment(0, buffer.getLength))
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56774
diff changeset
    79
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    80
  def buffer_mode(buffer: JEditBuffer): String = {
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    81
    val mode = buffer.getMode
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    82
    if (mode == null) ""
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    83
    else {
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    84
      val name = mode.getName
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    85
      if (name == null) "" else name
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    86
    }
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    87
  }
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53247
diff changeset
    88
65469
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    89
  def buffer_line_manager(buffer: JEditBuffer): LineManager =
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    90
    Untyped.get[LineManager](buffer, "lineMgr")
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    91
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    92
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    93
65469
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 64824
diff changeset
    94
  def buffer_file(buffer: Buffer): Option[JFile] = check_file(buffer_name(buffer))
61192
98eba31c51f8 tuned signature;
wenzelm
parents: 60992
diff changeset
    95
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    96
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    97
  /* main jEdit components */
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    98
73354
79b120d1c1a3 tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    99
  def jedit_buffers(): Iterator[Buffer] =
79b120d1c1a3 tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   100
    jEdit.getBufferManager().getBuffers().asScala.iterator
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   101
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   102
  def jedit_buffer(name: String): Option[Buffer] =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   103
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   104
56457
eea4bbe15745 tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents: 55812
diff changeset
   105
  def jedit_buffer(name: Document.Node.Name): Option[Buffer] =
eea4bbe15745 tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents: 55812
diff changeset
   106
    jedit_buffer(name.node)
eea4bbe15745 tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents: 55812
diff changeset
   107
73354
79b120d1c1a3 tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   108
  def jedit_views(): Iterator[View] =
79b120d1c1a3 tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   109
    jEdit.getViewManager().getViews().asScala.iterator
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   110
69636
dd1e0e1570d2 clarified signature;
wenzelm
parents: 69380
diff changeset
   111
  def jedit_view(view: View = null): View =
dd1e0e1570d2 clarified signature;
wenzelm
parents: 69380
diff changeset
   112
    if (view == null) jEdit.getActiveView() else view
dd1e0e1570d2 clarified signature;
wenzelm
parents: 69380
diff changeset
   113
62264
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 61865
diff changeset
   114
  def jedit_edit_panes(view: View): Iterator[EditPane] =
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 61865
diff changeset
   115
    if (view == null) Iterator.empty
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 61865
diff changeset
   116
    else view.getEditPanes().iterator.filter(_ != null)
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 61865
diff changeset
   117
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   118
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
56587
83777a91f5de clarified before_caret_range: prevent continuation on next line;
wenzelm
parents: 56574
diff changeset
   119
    if (view == null) Iterator.empty
83777a91f5de clarified before_caret_range: prevent continuation on next line;
wenzelm
parents: 56574
diff changeset
   120
    else view.getEditPanes().iterator.filter(_ != null).map(_.getTextArea).filter(_ != null)
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   121
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   122
  def jedit_text_areas(): Iterator[JEditTextArea] =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71502
diff changeset
   123
    jedit_views().flatMap(jedit_text_areas)
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   124
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   125
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   126
    jedit_text_areas().filter(_.getBuffer == buffer)
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   127
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   128
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A = {
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   129
    try { buffer.readLock(); body }
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   130
    finally { buffer.readUnlock() }
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   131
  }
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   132
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   133
  def buffer_edit[A](buffer: JEditBuffer)(body: => A): A = {
50195
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   134
    try { buffer.beginCompoundEdit(); body }
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   135
    finally { buffer.endCompoundEdit() }
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   136
  }
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   137
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   138
81423
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   139
  /* buffer text */
50215
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   140
67014
e6a695d6a6b2 tuned signature;
wenzelm
parents: 66592
diff changeset
   141
  def get_text(buffer: JEditBuffer, range: Text.Range): Option[String] =
50215
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   142
    try { Some(buffer.getText(range.start, range.length)) }
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   143
    catch { case _: ArrayIndexOutOfBoundsException => None }
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   144
81423
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   145
  def set_text(buffer: JEditBuffer, text: List[String]): Unit = {
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   146
    val old = buffer.isUndoInProgress
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   147
    def set(b: Boolean): Unit = Untyped.set[Boolean](buffer, "undoInProgress", b)
81426
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   148
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   149
    val length = buffer.getLength
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   150
    var offset = 0
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   151
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   152
    @tailrec def drop_common_prefix(list: List[String]): List[String] =
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   153
      list match {
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   154
        case s :: rest
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   155
          if offset + s.length <= length &&
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   156
          CharSequence.compare(buffer.getSegment(offset, s.length), s) == 0 =>
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   157
            offset += s.length
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   158
            drop_common_prefix(rest)
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   159
        case _ => list
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   160
      }
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   161
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   162
    def insert(list: List[String]): Unit =
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   163
      for (s <- list) {
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   164
        buffer.insert(offset, s)
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   165
        offset += s.length
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   166
      }
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   167
81423
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   168
    try {
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   169
      set(true)
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   170
      buffer.beginCompoundEdit()
81426
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   171
      val rest = drop_common_prefix(text)
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   172
      if (offset < length) buffer.remove(offset, length - offset)
56bab51e02c1 performance tuning: more incremental update of buffer content;
wenzelm
parents: 81423
diff changeset
   173
      insert(rest)
81423
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   174
    }
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   175
    finally {
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   176
      buffer.endCompoundEdit()
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   177
      set(old)
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   178
    }
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   179
  }
056657540039 clarified signature: inline org.gjt.sp.jedit.textarea.TextArea.setText();
wenzelm
parents: 81417
diff changeset
   180
50215
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   181
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   182
  /* point range */
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   183
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   184
  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
   185
    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
   186
    else
5157f7615e99 prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents: 56589
diff changeset
   187
      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
   188
        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
   189
        try {
5157f7615e99 prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents: 56589
diff changeset
   190
          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
   191
          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
   192
            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
   193
          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
   194
            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
   195
          else
5157f7615e99 prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents: 56589
diff changeset
   196
            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
   197
        }
5157f7615e99 prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents: 56589
diff changeset
   198
        catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   199
      }
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   200
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   201
55812
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   202
  /* text ranges */
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   203
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   204
  def buffer_range(buffer: JEditBuffer): Text.Range =
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   205
    Text.Range(0, buffer.getLength)
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   206
56587
83777a91f5de clarified before_caret_range: prevent continuation on next line;
wenzelm
parents: 56574
diff changeset
   207
  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
   208
    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
   209
56592
5157f7615e99 prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents: 56589
diff changeset
   210
  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
   211
    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
   212
81299
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   213
  def selection_ranges(text_area: TextArea): List[Text.Range] = {
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   214
    val buffer = text_area.getBuffer
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   215
    text_area.getSelection.toList.flatMap(
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   216
      {
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   217
        case rect: Selection.Rect =>
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   218
          List.from(
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   219
            for {
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   220
              l <- (rect.getStartLine to rect.getEndLine).iterator
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   221
              r = Text.Range(rect.getStart(buffer, l), rect.getEnd(buffer, l))
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   222
              if !r.is_singularity
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   223
            } yield r)
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   224
        case sel: Selection => List(Text.Range(sel.getStart, sel.getEnd))
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   225
      })
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   226
  }
e45d6575f893 more operations;
wenzelm
parents: 80554
diff changeset
   227
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   228
  def visible_range(text_area: TextArea): Option[Text.Range] = {
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   229
    val buffer = text_area.getBuffer
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   230
    val n = text_area.getVisibleLines
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   231
    if (n > 0) {
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   232
      val start = text_area.getScreenLineStartOffset(0)
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   233
      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
   234
      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
   235
      Some(Text.Range(start, end))
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   236
    }
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   237
    else None
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   238
  }
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   239
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   240
  def invalidate_range(text_area: TextArea, range: Text.Range): Unit = {
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   241
    val buffer = text_area.getBuffer
55812
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   242
    buffer_range(buffer).try_restrict(range) match {
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   243
      case Some(range1) if !range1.is_singularity =>
56699
60ad80f5cb62 further robustification wrt. unclear ranges;
wenzelm
parents: 56662
diff changeset
   244
        try {
60ad80f5cb62 further robustification wrt. unclear ranges;
wenzelm
parents: 56662
diff changeset
   245
          text_area.invalidateLineRange(
60ad80f5cb62 further robustification wrt. unclear ranges;
wenzelm
parents: 56662
diff changeset
   246
            buffer.getLineOfOffset(range1.start),
60ad80f5cb62 further robustification wrt. unclear ranges;
wenzelm
parents: 56662
diff changeset
   247
            buffer.getLineOfOffset(range1.stop))
60ad80f5cb62 further robustification wrt. unclear ranges;
wenzelm
parents: 56662
diff changeset
   248
        }
60ad80f5cb62 further robustification wrt. unclear ranges;
wenzelm
parents: 56662
diff changeset
   249
        catch { case _: ArrayIndexOutOfBoundsException => }
55812
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   250
      case _ =>
59fcd209cc0c more regular buffer_range, despite 47e8c8daccae;
wenzelm
parents: 55767
diff changeset
   251
    }
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   252
  }
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   253
80554
2ee3d05afb22 clarified signature: more arguments;
wenzelm
parents: 80551
diff changeset
   254
  def invalidate_screen(text_area: TextArea, start: Int = -1, end: Int = -1): Unit = {
62986
9d2fae6b31cc background color for entity def/ref focus;
wenzelm
parents: 62264
diff changeset
   255
    val visible_lines = text_area.getVisibleLines
80551
1638e980f737 clarified signature;
wenzelm
parents: 76782
diff changeset
   256
    if (visible_lines > 0) {
80554
2ee3d05afb22 clarified signature: more arguments;
wenzelm
parents: 80551
diff changeset
   257
      val start_line = if (start >= 0) start else 0
2ee3d05afb22 clarified signature: more arguments;
wenzelm
parents: 80551
diff changeset
   258
      val end_line = if (end >= 0) end else visible_lines
2ee3d05afb22 clarified signature: more arguments;
wenzelm
parents: 80551
diff changeset
   259
      text_area.invalidateScreenLineRange(start_line, end_line)
80551
1638e980f737 clarified signature;
wenzelm
parents: 76782
diff changeset
   260
    }
62986
9d2fae6b31cc background color for entity def/ref focus;
wenzelm
parents: 62264
diff changeset
   261
  }
9d2fae6b31cc background color for entity def/ref focus;
wenzelm
parents: 62264
diff changeset
   262
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   263
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   264
  /* graphics range */
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   265
81340
30f7eb65d679 clarified signature;
wenzelm
parents: 81299
diff changeset
   266
  def font_metric(painter: TextAreaPainter): Font_Metric =
81412
4794576828df clarified signature: include standard margin in object equality;
wenzelm
parents: 81401
diff changeset
   267
    new Font_Metric(
4794576828df clarified signature: include standard margin in object equality;
wenzelm
parents: 81401
diff changeset
   268
      font = painter.getFont,
81417
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81412
diff changeset
   269
      context = painter.getFontRenderContext)
81340
30f7eb65d679 clarified signature;
wenzelm
parents: 81299
diff changeset
   270
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59571
diff changeset
   271
  case class Gfx_Range(x: Int, y: Int, length: Int)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   272
49843
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49712
diff changeset
   273
  // NB: jEdit always normalizes \r\n and \r to \n
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   274
  // NB: last line lacks \n
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   275
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] = {
81340
30f7eb65d679 clarified signature;
wenzelm
parents: 81299
diff changeset
   276
    val metric = font_metric(text_area.getPainter)
30f7eb65d679 clarified signature;
wenzelm
parents: 81299
diff changeset
   277
    val char_width = metric.average_width.round.toInt
50849
70f7483df9cb more uniform Pretty.char_width;
wenzelm
parents: 50658
diff changeset
   278
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   279
    val buffer = text_area.getBuffer
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   280
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   281
    val end = buffer.getLength
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   282
    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
   283
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
   284
    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
   285
      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
   286
        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
   287
        val (q, r) =
81401
874e12fb6c3d tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81340
diff changeset
   288
          if (get_text(buffer, Text.Range(stop - 1, stop)).contains("\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
   289
            (text_area.offsetToXY(stop - 1), char_width)
81401
874e12fb6c3d tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81340
diff changeset
   290
          }
874e12fb6c3d tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81340
diff changeset
   291
          else if (stop >= end) {
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
   292
            (text_area.offsetToXY(end), char_width * (stop - end))
81401
874e12fb6c3d tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81340
diff changeset
   293
          }
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
   294
          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
   295
        (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
   296
      }
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
   297
      catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) }
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   298
81401
874e12fb6c3d tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81340
diff changeset
   299
    if (p != null && q != null && p.x < q.x + r && p.y == q.y) {
50115
8cde6f1a0106 tuned signature;
wenzelm
parents: 49941
diff changeset
   300
      Some(Gfx_Range(p.x, p.y, q.x + r - p.x))
81401
874e12fb6c3d tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81340
diff changeset
   301
    }
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   302
    else None
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   303
  }
49941
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   304
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   305
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   306
  /* pixel range */
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   307
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   308
  def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] = {
53183
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   309
    // coordinates wrt. inner painter component
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   310
    val painter = text_area.getPainter
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   311
    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
   312
      val offset = text_area.xyToOffset(x, y, false)
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   313
      if (offset >= 0) {
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   314
        val range = point_range(text_area.getBuffer, offset)
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   315
        gfx_range(text_area, range) match {
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59571
diff changeset
   316
          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
   317
          case _ => None
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   318
        }
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   319
      }
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   320
      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
   321
    }
53183
018d71bee930 strict checking of coordinates wrt. inner painter component;
wenzelm
parents: 53019
diff changeset
   322
    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
   323
  }
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
   324
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
   325
52873
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   326
  /* icons */
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   327
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   328
  def load_icon(name: String): Icon = {
52873
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   329
    val name1 =
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   330
      if (name.startsWith("idea-icons/")) {
75701
84990c95712d clarified signature;
wenzelm
parents: 75696
diff changeset
   331
        val file = File.uri(Path.explode("$ISABELLE_IDEA_ICONS")).toASCIIString
52873
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   332
        "jar:" + file + "!/" + name
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   333
      }
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   334
      else name
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   335
    val icon = GUIUtilities.loadIcon(name1)
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   336
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   337
    else icon
9e934d4fff00 tuned signature;
wenzelm
parents: 52478
diff changeset
   338
  }
52874
91032244e4ca query process animation;
wenzelm
parents: 52873
diff changeset
   339
91032244e4ca query process animation;
wenzelm
parents: 52873
diff changeset
   340
  def load_image_icon(name: String): ImageIcon =
91032244e4ca query process animation;
wenzelm
parents: 52873
diff changeset
   341
    load_icon(name) match {
91032244e4ca query process animation;
wenzelm
parents: 52873
diff changeset
   342
      case icon: ImageIcon => icon
91032244e4ca query process animation;
wenzelm
parents: 52873
diff changeset
   343
      case _ => error("Bad image icon: " + name)
91032244e4ca query process animation;
wenzelm
parents: 52873
diff changeset
   344
    }
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   345
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   346
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
   347
  /* 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
   348
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   349
  def request_focus_view(alt_view: View = null): Unit = {
73987
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73652
diff changeset
   350
    val view = if (alt_view != null) alt_view else jEdit.getActiveView()
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73652
diff changeset
   351
    if (view != null) {
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73652
diff changeset
   352
      val text_area = view.getTextArea
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73652
diff changeset
   353
      if (text_area != null) text_area.requestFocus()
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73652
diff changeset
   354
    }
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73652
diff changeset
   355
  }
66592
cc93f86e005f tuned signature;
wenzelm
parents: 66591
diff changeset
   356
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   357
  def propagate_key(view: View, evt: KeyEvent): Unit = {
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
   358
    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
   359
      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
   360
  }
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   361
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   362
  def key_listener(
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   363
    key_typed: KeyEvent => Unit = _ => (),
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   364
    key_pressed: KeyEvent => Unit = _ => (),
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   365
    key_released: KeyEvent => Unit = _ => ()
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   366
  ): KeyListener = {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   367
    def process_key_event(evt0: KeyEvent, handle: KeyEvent => Unit): Unit = {
53237
6bfe54791591 enable jEdit KeyEventWorkaround uniformly;
wenzelm
parents: 53231
diff changeset
   368
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   369
      if (evt != null) handle(evt)
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   370
    }
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   371
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   372
    new KeyListener {
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72974
diff changeset
   373
      def keyTyped(evt: KeyEvent): Unit = process_key_event(evt, key_typed)
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72974
diff changeset
   374
      def keyPressed(evt: KeyEvent): Unit = process_key_event(evt, key_pressed)
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72974
diff changeset
   375
      def keyReleased(evt: KeyEvent): Unit = process_key_event(evt, key_released)
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   376
    }
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53183
diff changeset
   377
  }
53784
322a3ff42b33 completion popup for history text field;
wenzelm
parents: 53712
diff changeset
   378
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   379
  def special_key(evt: KeyEvent): Boolean = {
59571
1081f91c0662 updated to jedit-5.2.0;
wenzelm
parents: 59077
diff changeset
   380
    // cf. 5.2.0/jEdit/org/gjt/sp/jedit/gui/KeyEventWorkaround.java
71500
a3ed1b0a132f tuned -- avoid deprecated operations;
wenzelm
parents: 71499
diff changeset
   381
    val mod = evt.getModifiersEx
a3ed1b0a132f tuned -- avoid deprecated operations;
wenzelm
parents: 71499
diff changeset
   382
    (mod & InputEvent.CTRL_DOWN_MASK) != 0 && (mod & InputEvent.ALT_DOWN_MASK) == 0 ||
a3ed1b0a132f tuned -- avoid deprecated operations;
wenzelm
parents: 71499
diff changeset
   383
    (mod & InputEvent.CTRL_DOWN_MASK) == 0 && (mod & InputEvent.ALT_DOWN_MASK) != 0 &&
53784
322a3ff42b33 completion popup for history text field;
wenzelm
parents: 53712
diff changeset
   384
      !Debug.ALT_KEY_PRESSED_DISABLED ||
71500
a3ed1b0a132f tuned -- avoid deprecated operations;
wenzelm
parents: 71499
diff changeset
   385
    (mod & InputEvent.META_DOWN_MASK) != 0
53784
322a3ff42b33 completion popup for history text field;
wenzelm
parents: 53712
diff changeset
   386
  }
72899
8732315dfafa tuned signature;
wenzelm
parents: 71861
diff changeset
   387
8732315dfafa tuned signature;
wenzelm
parents: 71861
diff changeset
   388
  def command_modifier(evt: InputEvent): Boolean =
8732315dfafa tuned signature;
wenzelm
parents: 71861
diff changeset
   389
    (evt.getModifiersEx & Toolkit.getDefaultToolkit.getMenuShortcutKeyMaskEx) != 0
8732315dfafa tuned signature;
wenzelm
parents: 71861
diff changeset
   390
8732315dfafa tuned signature;
wenzelm
parents: 71861
diff changeset
   391
  def shift_modifier(evt: InputEvent): Boolean =
8732315dfafa tuned signature;
wenzelm
parents: 71861
diff changeset
   392
    (evt.getModifiersEx & InputEvent.SHIFT_DOWN_MASK) != 0
72927
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
   393
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
   394
  def modifier_string(evt: InputEvent): String =
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
   395
    KeyEventTranslator.getModifierString(evt) match {
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
   396
      case null => ""
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
   397
      case s => s
69f768aff611 clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents: 72899
diff changeset
   398
    }
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   399
}