src/Tools/jEdit/src/pretty_text_area.scala
author wenzelm
Wed, 13 Nov 2024 11:53:02 +0100
changeset 81435 839c4b2b01fa
parent 81434 1935ed4fe9c2
child 81445 82110cbcf9a1
permissions -rw-r--r--
more ambitious scrolling: retain original scroll position if possible;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/pretty_text_area.scala
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
     3
50160
a29be9d067d2 tuned comment;
wenzelm
parents: 49700
diff changeset
     4
GUI component for pretty-printed text with markup, rendered like jEdit
a29be9d067d2 tuned comment;
wenzelm
parents: 49700
diff changeset
     5
text area.
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
     6
*/
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
     7
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
     8
package isabelle.jedit
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
     9
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    10
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    11
import isabelle._
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    12
58766
5bab56d3dcd4 tuned imports;
wenzelm
parents: 57612
diff changeset
    13
import java.awt.{Color, Font, Toolkit, Window}
71501
248402f42cac clarified modifier: avoid confusion of CS+a as C+a;
wenzelm
parents: 71500
diff changeset
    14
import java.awt.event.{InputEvent, KeyEvent}
62214
451bd09b8277 bypass input method for better imitation of read-only mode (cf. f26a4d5e82b5): e.g. relevant for composition of ALT-u u on Mac OS X;
wenzelm
parents: 61570
diff changeset
    15
import java.awt.im.InputMethodRequests
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
    16
import javax.swing.JTextField
56885
3020f6bbd119 clarified GUI events, e.g. relevant for insert via completion;
wenzelm
parents: 56883
diff changeset
    17
import javax.swing.event.{DocumentListener, DocumentEvent}
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
    18
56907
0f3c375fd27c enable "PIDE" docking framework by default, and rely on its "Detach" menu item;
wenzelm
parents: 56885
diff changeset
    19
import scala.swing.{Label, Component}
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
    20
import scala.util.matching.Regex
56881
15e18540df10 more uniform detach button;
wenzelm
parents: 56880
diff changeset
    21
61570
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
    22
import org.gjt.sp.jedit.{jEdit, View, Registers, JEditBeanShellAction}
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
    23
import org.gjt.sp.jedit.input.{DefaultInputHandlerProvider, TextAreaInputHandler}
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    24
import org.gjt.sp.jedit.textarea.{AntiAlias, JEditEmbeddedTextArea}
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    25
import org.gjt.sp.jedit.syntax.SyntaxStyle
61570
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
    26
import org.gjt.sp.jedit.gui.KeyEventTranslator
50640
b35bd8778754 more informative error;
wenzelm
parents: 50542
diff changeset
    27
import org.gjt.sp.util.{SyntaxUtilities, Log}
49412
4cac648e0f85 Pretty_Text_Area is based on Rich_Text_Area;
wenzelm
parents: 49398
diff changeset
    28
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    29
50538
48cb76b785da init gutter according to view properties, which improves symmetry of windows and allows use of folds etc;
wenzelm
parents: 50507
diff changeset
    30
class Pretty_Text_Area(
48cb76b785da init gutter according to view properties, which improves symmetry of windows and allows use of folds etc;
wenzelm
parents: 50507
diff changeset
    31
  view: View,
50915
12de8ea66f54 close tooltip after Active.action, to make it look more interactive (notably due to lack of dynamic update);
wenzelm
parents: 50849
diff changeset
    32
  close_action: () => Unit = () => (),
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    33
  propagate_keys: Boolean = false
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    34
) extends JEditEmbeddedTextArea {
81400
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
    35
  pretty_text_area =>
49446
b8d8f738bf63 more direct GUI component;
wenzelm
parents: 49422
diff changeset
    36
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57042
diff changeset
    37
  GUI_Thread.require {}
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    38
55826
wenzelm
parents: 55825
diff changeset
    39
  private var current_font_info: Font_Info = Font_Info.main()
81398
f92ea68473f2 clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents: 81390
diff changeset
    40
  private var current_output: List[XML.Elem] = Nil
52972
8fd8e1c14988 tuned signature;
wenzelm
parents: 52548
diff changeset
    41
  private var current_base_snapshot = Document.Snapshot.init
50507
9605b0d93d1e more formal class Command.Results;
wenzelm
parents: 50501
diff changeset
    42
  private var current_base_results = Command.Results.empty
81432
85fc3b482924 clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents: 81428
diff changeset
    43
  private var current_rendering: JEdit_Rendering =
85fc3b482924 clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents: 81428
diff changeset
    44
    JEdit_Rendering(current_base_snapshot, Nil, Command.Results.empty)
61561
f35786faee6c prefer Isabelle/Scala Future;
wenzelm
parents: 61556
diff changeset
    45
  private var future_refresh: Option[Future[Unit]] = None
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    46
50306
b655d2d0406d updated to jedit-5.0.0;
wenzelm
parents: 50216
diff changeset
    47
  private val rich_text_area =
81400
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
    48
    new Rich_Text_Area(view, pretty_text_area, () => current_rendering, close_action,
63028
5fb352275db3 more thorough update;
wenzelm
parents: 62214
diff changeset
    49
      get_search_pattern _, () => (), caret_visible = false, enable_hovering = true)
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
    50
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
    51
  private var current_search_pattern: Option[Regex] = None
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57042
diff changeset
    52
  def get_search_pattern(): Option[Regex] = GUI_Thread.require { current_search_pattern }
49416
1053a564dd25 some actual rich text markup via XML.content_markup;
wenzelm
parents: 49414
diff changeset
    53
51451
e4203ebfe750 recovered special background handling from 8d6e478934dc, particularly relevant for gutter border;
wenzelm
parents: 51449
diff changeset
    54
  def get_background(): Option[Color] = None
e4203ebfe750 recovered special background handling from 8d6e478934dc, particularly relevant for gutter border;
wenzelm
parents: 51449
diff changeset
    55
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    56
  def refresh(): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57042
diff changeset
    57
    GUI_Thread.require {}
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    58
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents: 53277
diff changeset
    59
    val font = current_font_info.font
50168
4a575ef46466 always refresh font metrics, to help window size calculation (amending 2585c81d840a);
wenzelm
parents: 50166
diff changeset
    60
    getPainter.setFont(font)
4a575ef46466 always refresh font metrics, to help window size calculation (amending 2585c81d840a);
wenzelm
parents: 50166
diff changeset
    61
    getPainter.setAntiAlias(new AntiAlias(jEdit.getProperty("view.antiAlias")))
51497
7e8968c9a549 no censorship of "view.fracFontMetrics", although it often degrades rendering quality;
wenzelm
parents: 51495
diff changeset
    62
    getPainter.setFractionalFontMetricsEnabled(jEdit.getBooleanProperty("view.fracFontMetrics"))
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents: 53277
diff changeset
    63
    getPainter.setStyles(
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents: 53277
diff changeset
    64
      SyntaxUtilities.loadStyles(current_font_info.family, current_font_info.size.round))
56789
f377ddf1cc52 tuned whitespace;
wenzelm
parents: 56730
diff changeset
    65
    getPainter.setLineExtraSpacing(jEdit.getIntegerProperty("options.textarea.lineSpacing", 0))
50168
4a575ef46466 always refresh font metrics, to help window size calculation (amending 2585c81d840a);
wenzelm
parents: 50166
diff changeset
    66
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    67
    val fold_line_style = new Array[SyntaxStyle](4)
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    68
    for (i <- 0 to 3) {
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    69
      fold_line_style(i) =
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    70
        SyntaxUtilities.parseStyle(
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    71
          jEdit.getProperty("view.style.foldLine." + i),
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents: 53277
diff changeset
    72
          current_font_info.family, current_font_info.size.round, true)
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    73
    }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    74
    getPainter.setFoldLineStyle(fold_line_style)
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents: 50538
diff changeset
    75
51439
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    76
    getGutter.setForeground(jEdit.getColorProperty("view.gutter.fgColor"))
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    77
    getGutter.setBackground(jEdit.getColorProperty("view.gutter.bgColor"))
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    78
    get_background().foreach { bg => getPainter.setBackground(bg); getGutter.setBackground(bg) }
51439
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    79
    getGutter.setHighlightedForeground(jEdit.getColorProperty("view.gutter.highlightColor"))
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    80
    getGutter.setFoldColor(jEdit.getColorProperty("view.gutter.foldColor"))
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    81
    getGutter.setFont(jEdit.getFontProperty("view.gutter.font"))
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    82
    getGutter.setBorder(0,
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    83
      jEdit.getColorProperty("view.gutter.focusBorderColor"),
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    84
      jEdit.getColorProperty("view.gutter.noFocusBorderColor"),
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    85
      getPainter.getBackground)
60250
baf2c8fddaa4 proper fold painter according to jEdit options, not the hardwired default of JEditEmbeddedTextArea;
wenzelm
parents: 59286
diff changeset
    86
    getGutter.setFoldPainter(view.getTextArea.getFoldPainter)
51439
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    87
    getGutter.setGutterEnabled(jEdit.getBooleanProperty("view.gutter.enabled"))
b10b64679c5b more precise tooltip window size (NB: dimensions are known after layout pack, before making content visible);
wenzelm
parents: 50915
diff changeset
    88
50166
2585c81d840a take component width as indication if it is already visible/layed-out, to avoid multiple formatting with minimal margin;
wenzelm
parents: 50160
diff changeset
    89
    if (getWidth > 0) {
81340
30f7eb65d679 clarified signature;
wenzelm
parents: 76610
diff changeset
    90
      val metric = JEdit_Lib.font_metric(getPainter)
81417
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
    91
      val margin = Rich_Text.component_margin(metric, getPainter)
81411
84cf218e052a performance tuning: prefer asynchronous Pretty.formatted, which actually takes longer than Command.rich_text (see also 97964515a676, where Pretty.formatted was on the GUI thread, maybe for the sake of java.awt.FontMetrics at that time);
wenzelm
parents: 81406
diff changeset
    92
      val output = current_output
72718
59a7f82a7180 clarified signature;
wenzelm
parents: 71704
diff changeset
    93
      val snapshot = current_base_snapshot
59a7f82a7180 clarified signature;
wenzelm
parents: 71704
diff changeset
    94
      val results = current_base_results
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
    95
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    96
      future_refresh.foreach(_.cancel())
61193
wenzelm
parents: 60250
diff changeset
    97
      future_refresh =
61561
f35786faee6c prefer Isabelle/Scala Future;
wenzelm
parents: 61556
diff changeset
    98
        Some(Future.fork {
81421
8c1680ac4160 clarified signature and data storage: incremental lazy values;
wenzelm
parents: 81418
diff changeset
    99
          val (rich_texts, rendering) =
81414
ed4ff84e9b21 Document.Snapshot: support for multiple snippet_commands;
wenzelm
parents: 81413
diff changeset
   100
            try {
81433
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 81432
diff changeset
   101
              val rich_texts = Rich_Text.format(output, margin, metric, cache = PIDE.cache)
81432
85fc3b482924 clarified persistent values: Command.Results does not suitable for caching, because it contains all other messages;
wenzelm
parents: 81428
diff changeset
   102
              val rendering = JEdit_Rendering(snapshot, rich_texts, results)
81421
8c1680ac4160 clarified signature and data storage: incremental lazy values;
wenzelm
parents: 81418
diff changeset
   103
              (rich_texts, rendering)
81414
ed4ff84e9b21 Document.Snapshot: support for multiple snippet_commands;
wenzelm
parents: 81413
diff changeset
   104
            }
81415
1e3dfb722ee6 more reactive interrupts (via Future.cancel);
wenzelm
parents: 81414
diff changeset
   105
            catch {
1e3dfb722ee6 more reactive interrupts (via Future.cancel);
wenzelm
parents: 81414
diff changeset
   106
              case exn: Throwable if !Exn.is_interrupt(exn) =>
1e3dfb722ee6 more reactive interrupts (via Future.cancel);
wenzelm
parents: 81414
diff changeset
   107
                Log.log(Log.ERROR, this, exn)
1e3dfb722ee6 more reactive interrupts (via Future.cancel);
wenzelm
parents: 81414
diff changeset
   108
                throw exn
1e3dfb722ee6 more reactive interrupts (via Future.cancel);
wenzelm
parents: 81414
diff changeset
   109
            }
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   110
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57042
diff changeset
   111
          GUI_Thread.later {
81417
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
   112
            val current_metric = JEdit_Lib.font_metric(getPainter)
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
   113
            val current_margin = Rich_Text.component_margin(current_metric, getPainter)
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
   114
            if (metric == current_metric &&
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
   115
                margin == current_margin &&
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
   116
                output == current_output &&
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
   117
                snapshot == current_base_snapshot &&
964b85e04f1f clarified margin operations (again, reverting 4794576828df);
wenzelm
parents: 81416
diff changeset
   118
                results == current_base_results
81413
2d9b6e32632d more robust: make double-sure that this is the correct output, not a different version from concurrent GUI_Thread.later;
wenzelm
parents: 81412
diff changeset
   119
            ) {
2d9b6e32632d more robust: make double-sure that this is the correct output, not a different version from concurrent GUI_Thread.later;
wenzelm
parents: 81412
diff changeset
   120
              current_rendering = rendering
81435
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   121
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   122
              val scroll_bottom = JEdit_Lib.scrollbar_bottom(pretty_text_area)
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   123
              val scroll_start = JEdit_Lib.scrollbar_start(pretty_text_area)
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   124
              val update_start =
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   125
                JEdit_Lib.buffer_edit(getBuffer) {
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   126
                  rich_text_area.active_reset()
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   127
                  getBuffer.setFoldHandler(new Fold_Handling.Document_Fold_Handler(rendering))
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   128
                  JEdit_Lib.set_text(getBuffer, rich_texts.map(_.text))
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   129
                }
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   130
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   131
              setCaretPosition(
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   132
                if (scroll_bottom) JEdit_Lib.bottom_line_offset(getBuffer)
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   133
                else if (scroll_start < update_start) scroll_start
839c4b2b01fa more ambitious scrolling: retain original scroll position if possible;
wenzelm
parents: 81434
diff changeset
   134
                else 0)
81434
1935ed4fe9c2 more ambitious scrolling: retain bottom position after output;
wenzelm
parents: 81433
diff changeset
   135
              JEdit_Lib.scroll_to_caret(pretty_text_area)
51498
979592b765f8 reverted most of 5944b20c41bf -- tends to cause race condition of synchronous vs. asynchronous version;
wenzelm
parents: 51497
diff changeset
   136
            }
979592b765f8 reverted most of 5944b20c41bf -- tends to cause race condition of synchronous vs. asynchronous version;
wenzelm
parents: 51497
diff changeset
   137
          }
56730
e723f041b6d0 tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents: 56711
diff changeset
   138
        })
50166
2585c81d840a take component width as indication if it is already visible/layed-out, to avoid multiple formatting with minimal margin;
wenzelm
parents: 50160
diff changeset
   139
    }
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   140
  }
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   141
81384
wenzelm
parents: 81383
diff changeset
   142
  def resize(font_info: Font_Info): Unit = GUI_Thread.require {
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents: 53277
diff changeset
   143
    current_font_info = font_info
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   144
    refresh()
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   145
  }
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   146
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73122
diff changeset
   147
  def update(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   148
    base_snapshot: Document.Snapshot,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   149
    base_results: Command.Results,
81398
f92ea68473f2 clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents: 81390
diff changeset
   150
    output: List[XML.Elem]
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   151
  ): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57042
diff changeset
   152
    GUI_Thread.require {}
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 72899
diff changeset
   153
    require(!base_snapshot.is_outdated, "document snapshot outdated")
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   154
49419
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 49416
diff changeset
   155
    current_base_snapshot = base_snapshot
50501
6f41f1646617 more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents: 50306
diff changeset
   156
    current_base_results = base_results
81414
ed4ff84e9b21 Document.Snapshot: support for multiple snippet_commands;
wenzelm
parents: 81413
diff changeset
   157
    current_output = output.map(Protocol_Message.provide_serial)
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   158
    refresh()
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   159
  }
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   160
75808
f1a89044a712 tuned signature;
wenzelm
parents: 75807
diff changeset
   161
  def detach(): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57042
diff changeset
   162
    GUI_Thread.require {}
81398
f92ea68473f2 clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents: 81390
diff changeset
   163
    Info_Dockable(view, current_base_snapshot, current_base_results, current_output)
56880
f8c1d2583699 tuned signature;
wenzelm
parents: 56860
diff changeset
   164
  }
f8c1d2583699 tuned signature;
wenzelm
parents: 56860
diff changeset
   165
56918
a442dc6d244d clarified detach_operation: ignore empty output;
wenzelm
parents: 56907
diff changeset
   166
  def detach_operation: Option[() => Unit] =
81398
f92ea68473f2 clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents: 81390
diff changeset
   167
    if (current_output.isEmpty) None else Some(() => detach())
56918
a442dc6d244d clarified detach_operation: ignore empty output;
wenzelm
parents: 56907
diff changeset
   168
49422
21f77309d93a minimal clipboard support (similar to org.lobobrowser.html.gui.HtmlBlockPanel);
wenzelm
parents: 49421
diff changeset
   169
81387
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   170
  /* search */
56881
15e18540df10 more uniform detach button;
wenzelm
parents: 56880
diff changeset
   171
81390
71e66ebbc632 tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81389
diff changeset
   172
  private val search_label: Component = new Label("Search:") {
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   173
    tooltip = "Search and highlight output via regular expression"
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   174
  }
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   175
81390
71e66ebbc632 tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81389
diff changeset
   176
  private val search_field: Component =
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   177
    Component.wrap(new Completion_Popup.History_Text_Field("isabelle-search") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   178
      private val input_delay =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 75839
diff changeset
   179
        Delay.last(PIDE.session.input_delay, gui = true) { search_action(this) }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   180
      getDocument.addDocumentListener(new DocumentListener {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   181
        def changedUpdate(e: DocumentEvent): Unit = input_delay.invoke()
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   182
        def insertUpdate(e: DocumentEvent): Unit = input_delay.invoke()
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   183
        def removeUpdate(e: DocumentEvent): Unit = input_delay.invoke()
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   184
      })
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   185
      setColumns(20)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   186
      setToolTipText(search_label.tooltip)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   187
      setFont(GUI.imitate_font(getFont, scale = 1.2))
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   188
    })
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   189
57042
5576d22abf3c tuned signature;
wenzelm
parents: 56918
diff changeset
   190
  private val search_field_foreground = search_field.foreground
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   191
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   192
  private def search_action(text_field: JTextField): Unit = {
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   193
    val (pattern, ok) =
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   194
      text_field.getText match {
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   195
        case null | "" => (None, true)
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   196
        case s =>
59224
wenzelm
parents: 59077
diff changeset
   197
          val re = Library.make_regex(s)
wenzelm
parents: 59077
diff changeset
   198
          (re, re.isDefined)
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   199
      }
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   200
    if (current_search_pattern != pattern) {
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   201
      current_search_pattern = pattern
81400
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
   202
      pretty_text_area.getPainter.repaint()
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   203
    }
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   204
    text_field.setForeground(
65911
f97d163479b9 clarified modules;
wenzelm
parents: 65240
diff changeset
   205
      if (ok) search_field_foreground
f97d163479b9 clarified modules;
wenzelm
parents: 65240
diff changeset
   206
      else current_rendering.color(Rendering.Color.error))
56883
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   207
  }
38c6b70e5e53 common support for search field, which is actually a light-weight Highlighter;
wenzelm
parents: 56881
diff changeset
   208
56881
15e18540df10 more uniform detach button;
wenzelm
parents: 56880
diff changeset
   209
81387
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   210
  /* zoom */
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   211
81389
5c8c94d5211a clarified signature: more accurate types;
wenzelm
parents: 81387
diff changeset
   212
  val zoom_component: Font_Info.Zoom =
81387
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   213
    new Font_Info.Zoom { override def changed(): Unit = zoom() }
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   214
81389
5c8c94d5211a clarified signature: more accurate types;
wenzelm
parents: 81387
diff changeset
   215
  def zoom(zoom: Font_Info.Zoom = zoom_component): Unit =
81387
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   216
    resize(Font_Info.main(scale = PIDE.options.real("jedit_font_scale"), zoom = zoom))
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   217
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   218
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   219
  /* common GUI components */
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   220
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   221
  def search_components: List[Component] = List(search_label, search_field)
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   222
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   223
  def search_zoom_components: List[Component] = List(search_label, search_field, zoom_component)
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   224
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 81384
diff changeset
   225
50726
27478c11f63c more elementary key handling: listen to low-level KEY_PRESSED events (without consuming);
wenzelm
parents: 50640
diff changeset
   226
  /* key handling */
49422
21f77309d93a minimal clipboard support (similar to org.lobobrowser.html.gui.HtmlBlockPanel);
wenzelm
parents: 49421
diff changeset
   227
62214
451bd09b8277 bypass input method for better imitation of read-only mode (cf. f26a4d5e82b5): e.g. relevant for composition of ALT-u u on Mac OS X;
wenzelm
parents: 61570
diff changeset
   228
  override def getInputMethodRequests: InputMethodRequests = null
451bd09b8277 bypass input method for better imitation of read-only mode (cf. f26a4d5e82b5): e.g. relevant for composition of ALT-u u on Mac OS X;
wenzelm
parents: 61570
diff changeset
   229
61570
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
   230
  inputHandlerProvider =
81400
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
   231
    new DefaultInputHandlerProvider(new TextAreaInputHandler(pretty_text_area) {
61570
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
   232
      override def getAction(action: String): JEditBeanShellAction =
81400
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
   233
        pretty_text_area.getActionContext.getAction(action)
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73122
diff changeset
   234
      override def processKeyEvent(evt: KeyEvent, from: Int, global: Boolean): Unit = {}
61570
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
   235
      override def handleKey(key: KeyEventTranslator.Key, dry_run: Boolean): Boolean = false
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
   236
    })
f26a4d5e82b5 dummy input handler to imitate former read-only mode, which has changed its meaning in jedit-5.3.0 as mere hint for saving;
wenzelm
parents: 61561
diff changeset
   237
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53179
diff changeset
   238
  addKeyListener(JEdit_Lib.key_listener(
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   239
    key_pressed = { (evt: KeyEvent) =>
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   240
      val strict_control =
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   241
        JEdit_Lib.command_modifier(evt) && !JEdit_Lib.shift_modifier(evt)
71501
248402f42cac clarified modifier: avoid confusion of CS+a as C+a;
wenzelm
parents: 71500
diff changeset
   242
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   243
      evt.getKeyCode match {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   244
        case KeyEvent.VK_C | KeyEvent.VK_INSERT
81400
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
   245
        if strict_control && pretty_text_area.getSelectionCount != 0 =>
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
   246
          Registers.copy(pretty_text_area, '$')
75807
b0394e7d43ea tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents: 75394
diff changeset
   247
          evt.consume()
53227
68cc55ceb7f6 more standard key handling according to jEdit (with workaround);
wenzelm
parents: 53226
diff changeset
   248
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   249
        case KeyEvent.VK_A
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   250
        if strict_control =>
81400
4a62c57fe745 tuned signature;
wenzelm
parents: 81398
diff changeset
   251
          pretty_text_area.selectAll()
75807
b0394e7d43ea tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents: 75394
diff changeset
   252
          evt.consume()
53227
68cc55ceb7f6 more standard key handling according to jEdit (with workaround);
wenzelm
parents: 53226
diff changeset
   253
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   254
        case KeyEvent.VK_ESCAPE =>
75807
b0394e7d43ea tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents: 75394
diff changeset
   255
          if (Isabelle.dismissed_popups(view)) evt.consume()
53227
68cc55ceb7f6 more standard key handling according to jEdit (with workaround);
wenzelm
parents: 53226
diff changeset
   256
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   257
        case _ =>
49422
21f77309d93a minimal clipboard support (similar to org.lobobrowser.html.gui.HtmlBlockPanel);
wenzelm
parents: 49421
diff changeset
   258
      }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   259
      if (propagate_keys) JEdit_Lib.propagate_key(view, evt)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   260
    },
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   261
    key_typed = { (evt: KeyEvent) =>
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   262
      if (propagate_keys) JEdit_Lib.propagate_key(view, evt)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
   263
    })
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53179
diff changeset
   264
  )
49422
21f77309d93a minimal clipboard support (similar to org.lobobrowser.html.gui.HtmlBlockPanel);
wenzelm
parents: 49421
diff changeset
   265
21f77309d93a minimal clipboard support (similar to org.lobobrowser.html.gui.HtmlBlockPanel);
wenzelm
parents: 49421
diff changeset
   266
21f77309d93a minimal clipboard support (similar to org.lobobrowser.html.gui.HtmlBlockPanel);
wenzelm
parents: 49421
diff changeset
   267
  /* init */
21f77309d93a minimal clipboard support (similar to org.lobobrowser.html.gui.HtmlBlockPanel);
wenzelm
parents: 49421
diff changeset
   268
49472
ba2c0d0cd429 no caret painting;
wenzelm
parents: 49471
diff changeset
   269
  getPainter.setStructureHighlightEnabled(false)
49475
8f3a3adadd5a tuned painter;
wenzelm
parents: 49472
diff changeset
   270
  getPainter.setLineHighlightEnabled(false)
8f3a3adadd5a tuned painter;
wenzelm
parents: 49472
diff changeset
   271
59076
65babcd8b0e6 clarified token marker / syntax for mode vs. buffer;
wenzelm
parents: 58835
diff changeset
   272
  getBuffer.setTokenMarker(Isabelle.mode_token_marker("isabelle-output").get)
63261
90a44d271683 proper noWordSep as in "isabelle" mode (cf. 5024d0c48e02);
wenzelm
parents: 63028
diff changeset
   273
  getBuffer.setStringProperty("noWordSep", "_'?⇩")
49475
8f3a3adadd5a tuned painter;
wenzelm
parents: 49472
diff changeset
   274
49412
4cac648e0f85 Pretty_Text_Area is based on Rich_Text_Area;
wenzelm
parents: 49398
diff changeset
   275
  rich_text_area.activate()
49398
0fa4389c04f9 alternative output panel, based on Pretty_Text_Area, based on JEditEmbeddedTextArea;
wenzelm
parents:
diff changeset
   276
}