src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Tue, 24 Jun 2025 21:05:48 +0200
changeset 82747 00828818a607
parent 82720 956ecf2c07a0
child 82758 414ebfd5389b
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_editor.scala
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     3
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     4
PIDE editor operations for jEdit.
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     5
*/
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     6
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     8
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
     9
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    10
import isabelle._
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    11
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    12
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
    13
import org.gjt.sp.jedit.{jEdit, View, Buffer}
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
    14
import org.gjt.sp.jedit.browser.VFSBrowser
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
    15
import org.gjt.sp.jedit.textarea.TextArea
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
    16
import org.gjt.sp.jedit.io.{VFSManager, VFSFile}
82427
1c646ad68bd8 eliminated patch: imitate jEdit.gotoMarker more directly;
wenzelm
parents: 82426
diff changeset
    17
import org.gjt.sp.util.AwtRunnableQueue
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    18
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    19
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
    20
class JEdit_Editor extends Editor[View] {
76794
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    21
  /* PIDE session and document model */
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    22
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    23
  override def session: Session = PIDE.session
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    24
66084
7f8eeff20f7a tuned signature;
wenzelm
parents: 66083
diff changeset
    25
  def flush_edits(hidden: Boolean = false, purge: Boolean = false): Unit =
64817
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    26
    GUI_Thread.require {
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64841
diff changeset
    27
      val (doc_blobs, edits) = Document_Model.flush_edits(hidden, purge)
64817
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    28
      session.update(doc_blobs, edits)
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    29
    }
66084
7f8eeff20f7a tuned signature;
wenzelm
parents: 66083
diff changeset
    30
  override def flush(): Unit = flush_edits()
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73224
diff changeset
    31
  def purge(): Unit = flush_edits(purge = true)
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64841
diff changeset
    32
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    33
  private val delay_input: Delay =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 76044
diff changeset
    34
    Delay.last(PIDE.session.input_delay, gui = true) { flush() }
54461
6c360a6ade0e centralized management of pending buffer edits;
wenzelm
parents: 54325
diff changeset
    35
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    36
  private val delay_generated_input: Delay =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 76044
diff changeset
    37
    Delay.first(PIDE.session.generated_input_delay, gui = true) { flush() }
64521
1aef5a0e18d7 delay_first for machine generated editor events: avoid starvation, e.g. when operating on big sessions;
wenzelm
parents: 62248
diff changeset
    38
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    39
  def invoke(): Unit = delay_input.invoke()
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    40
  def invoke_generated(): Unit = { delay_input.invoke(); delay_generated_input.invoke() }
54461
6c360a6ade0e centralized management of pending buffer edits;
wenzelm
parents: 54325
diff changeset
    41
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    42
  def shutdown(): Unit =
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    43
    GUI_Thread.require {
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    44
      delay_input.revoke()
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    45
      delay_generated_input.revoke()
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    46
      Document_Model.flush_edits(hidden = false, purge = false)
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    47
    }
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    48
64799
c0c648911f1a misc tuning and clarification;
wenzelm
parents: 64682
diff changeset
    49
  def visible_node(name: Document.Node.Name): Boolean =
64840
wenzelm
parents: 64835
diff changeset
    50
    (for {
wenzelm
parents: 64835
diff changeset
    51
      text_area <- JEdit_Lib.jedit_text_areas()
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64867
diff changeset
    52
      doc_view <- Document_View.get(text_area)
64840
wenzelm
parents: 64835
diff changeset
    53
    } yield doc_view.model.node_name).contains(name)
64799
c0c648911f1a misc tuning and clarification;
wenzelm
parents: 64682
diff changeset
    54
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    55
76794
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    56
  override def get_models(): Iterable[Document.Model] = Document_Model.get_models()
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    57
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    58
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    59
  /* global changes */
76705
ddf5764684dd proper state change, e.g. on open/close of "Document" panel;
wenzelm
parents: 76610
diff changeset
    60
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    61
  def state_changed(): Unit = {
76708
2368724cde54 proper thread context;
wenzelm
parents: 76706
diff changeset
    62
    GUI_Thread.later { flush() }
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    63
    PIDE.plugin.deps_changed()
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    64
    session.global_options.post(Session.Global_Options(PIDE.options.value))
76705
ddf5764684dd proper state change, e.g. on open/close of "Document" panel;
wenzelm
parents: 76610
diff changeset
    65
  }
ddf5764684dd proper state change, e.g. on open/close of "Document" panel;
wenzelm
parents: 76610
diff changeset
    66
76715
bf5ff407f32f clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents: 76708
diff changeset
    67
  override def document_state_changed(): Unit = state_changed()
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    68
76705
ddf5764684dd proper state change, e.g. on open/close of "Document" panel;
wenzelm
parents: 76610
diff changeset
    69
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    70
  /* current situation */
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    71
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    72
  override def current_node(view: View): Option[Document.Node.Name] =
76768
40c8275f0131 tuned signature: follow terminology of VSCode_Resources;
wenzelm
parents: 76765
diff changeset
    73
    GUI_Thread.require { Document_Model.get_model(view.getBuffer).map(_.node_name) }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    74
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    75
  override def current_node_snapshot(view: View): Option[Document.Snapshot] =
76765
c654103e9c9d more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents: 76715
diff changeset
    76
    GUI_Thread.require { Document_Model.get_snapshot(view.getBuffer) }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    77
76765
c654103e9c9d more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents: 76715
diff changeset
    78
  override def node_snapshot(name: Document.Node.Name): Document.Snapshot =
c654103e9c9d more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents: 76715
diff changeset
    79
    GUI_Thread.require { Document_Model.get_snapshot(name) getOrElse session.snapshot(name) }
52974
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
    80
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
    81
  override def current_command(view: View, snapshot: Document.Snapshot): Option[Command] = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
    82
    GUI_Thread.require {}
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    83
54325
2c4155003352 clarified Editor.current_command: allow outdated snapshot;
wenzelm
parents: 53845
diff changeset
    84
    val text_area = view.getTextArea
54528
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    85
    val buffer = view.getBuffer
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    86
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64867
diff changeset
    87
    Document_View.get(text_area) match {
55432
9c53198dbb1c maintain multiple command chunks and markup trees: for main chunk and loaded files;
wenzelm
parents: 54706
diff changeset
    88
      case Some(doc_view) if doc_view.model.is_theory =>
65199
6bd7081f8319 clarified current_command: index refers to node content, negative index means first command;
wenzelm
parents: 65196
diff changeset
    89
        snapshot.current_command(doc_view.model.node_name, text_area.getCaretPosition)
55432
9c53198dbb1c maintain multiple command chunks and markup trees: for main chunk and loaded files;
wenzelm
parents: 54706
diff changeset
    90
      case _ =>
76768
40c8275f0131 tuned signature: follow terminology of VSCode_Resources;
wenzelm
parents: 76765
diff changeset
    91
        Document_Model.get_model(buffer) match {
54531
8330faaeebd5 restrict node_required status and Theories panel to actual theories;
wenzelm
parents: 54528
diff changeset
    92
          case Some(model) if !model.is_theory =>
65187
wenzelm
parents: 64882
diff changeset
    93
            snapshot.version.nodes.commands_loading(model.node_name).headOption
54528
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    94
          case _ => None
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    95
        }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    96
    }
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    97
  }
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    98
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    99
66101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   100
  /* overlays */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   102
  override def node_overlays(name: Document.Node.Name): Document.Node.Overlays =
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   103
    Document_Model.node_overlays(name)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   104
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   105
  override def insert_overlay(command: Command, fn: String, args: List[String]): Unit =
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   106
    Document_Model.insert_overlay(command, fn, args)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   107
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   108
  override def remove_overlay(command: Command, fn: String, args: List[String]): Unit =
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   109
    Document_Model.remove_overlay(command, fn, args)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   110
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   111
55877
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   112
  /* navigation */
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   113
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   114
  def goto_file(
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   115
    focus: Boolean,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   116
    view: View,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   117
    name: String,
82431
64eeb82f8b02 tuned signature;
wenzelm
parents: 82429
diff changeset
   118
    line: Int = -1,
82437
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   119
    offset: Text.Offset = -1,
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   120
    at_target: (Buffer, Text.Offset) => Unit = (_, _) => ()
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   121
  ): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
   122
    GUI_Thread.require {}
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   123
82419
955c5d6c1acf remove goto_buffer in favour of uniform goto_file;
wenzelm
parents: 82418
diff changeset
   124
    PIDE.plugin.navigator.record(view)
56413
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   125
82429
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   126
    def buffer_offset(buffer: Buffer): Text.Offset =
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   127
      ((if (line < 0) 0 else buffer.getLineStartOffset(line min buffer.getLineCount)) +
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   128
        (if (offset < 0) 0 else offset)) min buffer.getLength
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   129
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
   130
    JEdit_Lib.jedit_buffer(name) match {
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   131
      case Some(buffer) =>
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   132
        if (focus) view.goToBuffer(buffer) else view.showBuffer(buffer)
82437
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   133
        val target = buffer_offset(buffer)
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   134
        view.getTextArea.setCaretPosition(target)
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   135
        at_target(buffer, target)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   136
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   137
      case None =>
70167
b33f28c81ba9 clarified goto_file (again): treat bad entry as plain file to open empty buffer instead of error (amending a8142ac5e4b6);
wenzelm
parents: 70016
diff changeset
   138
        val is_dir =
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   139
          try {
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   140
            val vfs = VFSManager.getVFSForPath(name)
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   141
            val vfs_file = vfs._getFile((), name, view)
70167
b33f28c81ba9 clarified goto_file (again): treat bad entry as plain file to open empty buffer instead of error (amending a8142ac5e4b6);
wenzelm
parents: 70016
diff changeset
   142
            vfs_file != null && vfs_file.getType == VFSFile.DIRECTORY
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   143
          }
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   144
          catch { case ERROR(_) => false }
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
   145
70167
b33f28c81ba9 clarified goto_file (again): treat bad entry as plain file to open empty buffer instead of error (amending a8142ac5e4b6);
wenzelm
parents: 70016
diff changeset
   146
        if (is_dir) VFSBrowser.browseDirectory(view, name)
73224
49686e3b1909 clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents: 72823
diff changeset
   147
        else if (!Isabelle_System.open_external_file(name)) {
82426
wenzelm
parents: 82422
diff changeset
   148
          val buffer = jEdit.openFile(view, name)
82429
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   149
          if (buffer != null && (line >= 0 || offset >= 0)) {
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   150
            AwtRunnableQueue.INSTANCE.runAfterIoTasks({ () =>
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   151
              val target = buffer_offset(buffer)
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   152
              if (view.getBuffer == buffer) {
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   153
                view.getTextArea.setCaretPosition(target)
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   154
                buffer.setIntegerProperty(Buffer.CARET, target)
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   155
                buffer.setBooleanProperty(Buffer.CARET_POSITIONED, true)
82437
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   156
                at_target(buffer, target)
82429
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   157
              }
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   158
              else {
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   159
                buffer.setIntegerProperty(Buffer.CARET, target)
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   160
                buffer.setBooleanProperty(Buffer.CARET_POSITIONED, true)
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   161
                buffer.unsetProperty(Buffer.SCROLL_VERT)
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   162
              }
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   163
            })
82427
1c646ad68bd8 eliminated patch: imitate jEdit.gotoMarker more directly;
wenzelm
parents: 82426
diff changeset
   164
          }
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   165
        }
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   166
    }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   167
  }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   168
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   169
  def goto_doc(view: View, path: Path): Unit = {
75120
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   170
    if (path.is_pdf) Doc.view(path)
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   171
    else goto_file(true, view, File.platform_path(path))
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   172
  }
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   173
55877
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   174
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   175
  /* hyperlinks */
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   176
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   177
  def hyperlink_doc(name: String): Option[Hyperlink] =
81350
1818358373e2 misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents: 80225
diff changeset
   178
    Doc.contents().entries(name = _ == name).headOption.map(entry =>
1818358373e2 misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents: 80225
diff changeset
   179
      new Hyperlink {
1818358373e2 misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents: 80225
diff changeset
   180
        override val external: Boolean = !entry.path.is_file
1818358373e2 misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents: 80225
diff changeset
   181
        def follow(view: View): Unit = goto_doc(view, entry.path)
1818358373e2 misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents: 80225
diff changeset
   182
        override def toString: String = "doc " + quote(name)
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75115
diff changeset
   183
      })
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   184
56461
wenzelm
parents: 56458
diff changeset
   185
  def hyperlink_url(name: String): Hyperlink =
wenzelm
parents: 56458
diff changeset
   186
    new Hyperlink {
64663
wenzelm
parents: 64662
diff changeset
   187
      override val external = true
56729
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   188
      def follow(view: View): Unit =
71692
f8e52c0152fe clarified names;
wenzelm
parents: 71685
diff changeset
   189
        Isabelle_Thread.fork(name = "hyperlink_url") {
69901
20b32ade0024 clarified signature;
wenzelm
parents: 69650
diff changeset
   190
          try { Isabelle_System.open(Url.escape_name(name)) }
56461
wenzelm
parents: 56458
diff changeset
   191
          catch {
wenzelm
parents: 56458
diff changeset
   192
            case exn: Throwable =>
62062
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   193
              GUI_Thread.later {
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   194
                GUI.error_dialog(view, "System error", GUI.scrollable_text(Exn.message(exn)))
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   195
              }
56729
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   196
          }
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   197
        }
56461
wenzelm
parents: 56458
diff changeset
   198
      override def toString: String = "URL " + quote(name)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   199
    }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   200
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   201
  def hyperlink_file(
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   202
    focus: Boolean,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   203
    name: String,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   204
    line: Int = -1,
82429
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   205
    offset: Text.Offset = -1
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   206
  ): Hyperlink =
56461
wenzelm
parents: 56458
diff changeset
   207
    new Hyperlink {
82437
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   208
      def follow(view: View): Unit = {
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   209
        import Isabelle_Navigator.Pos
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   210
        PIDE.plugin.navigator.record(Pos(view))
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   211
        goto_file(focus, view, name, line = line, offset = offset,
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   212
          at_target = (buffer, target) => Pos.make(JEdit_Lib.buffer_name(buffer), target))
5b12c2677d2e more navigator positions;
wenzelm
parents: 82434
diff changeset
   213
      }
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   214
      override def toString: String = "file " + quote(name)
56461
wenzelm
parents: 56458
diff changeset
   215
    }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   216
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   217
  def hyperlink_source_file(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   218
    focus: Boolean,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   219
    source_name: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   220
    line1: Int,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   221
    offset: Symbol.Offset
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   222
  ) : Option[Hyperlink] = {
82720
956ecf2c07a0 more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents: 82437
diff changeset
   223
    for (platform_path <- PIDE.resources.source_file(PIDE.ml_settings, source_name)) yield {
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   224
      def hyperlink(pos: Line.Position) =
82429
d37f279ae3bf clarified target position: line start + offset (or column);
wenzelm
parents: 82427
diff changeset
   225
        hyperlink_file(focus, platform_path, line = pos.line, offset = pos.column)
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   226
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   227
      if (offset > 0) {
82193
355a2c1cdf40 tuned signature: emphasize semantics;
wenzelm
parents: 81350
diff changeset
   228
        PIDE.resources.get_file_content(PIDE.resources.node_name(platform_path)) match {
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   229
          case Some(text) =>
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   230
            hyperlink(
73358
78aa7846e91f tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   231
              Symbol.iterator(text).zipWithIndex.takeWhile(p => p._2 < offset - 1).map(_._1).
78aa7846e91f tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   232
                foldLeft(Line.Position.zero)(_.advance(_)))
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   233
          case None =>
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   234
            hyperlink(Line.Position((line1 - 1) max 0))
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   235
        }
55878
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   236
      }
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   237
      else hyperlink(Line.Position((line1 - 1) max 0))
55878
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   238
    }
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   239
  }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   240
56461
wenzelm
parents: 56458
diff changeset
   241
  override def hyperlink_command(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   242
    focus: Boolean,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   243
    snapshot: Document.Snapshot,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   244
    id: Document_ID.Generic,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   245
    offset: Symbol.Offset = 0
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   246
  ) : Option[Hyperlink] = {
56461
wenzelm
parents: 56458
diff changeset
   247
    if (snapshot.is_outdated) None
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   248
    else {
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   249
      snapshot.find_command_position(id, offset)
82433
21f7f29ef9cb proper hyperlink column (amending 6898054035d6);
wenzelm
parents: 82431
diff changeset
   250
        .map(pos => hyperlink_file(focus, pos.name, line = pos.line, offset = pos.column))
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   251
    }
56461
wenzelm
parents: 56458
diff changeset
   252
  }
wenzelm
parents: 56458
diff changeset
   253
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   254
  def is_hyperlink_position(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   255
    snapshot: Document.Snapshot,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   256
    text_offset: Text.Offset,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   257
    pos: Position.T
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   258
  ): Boolean = {
61011
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   259
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   260
      case Position.Item_Id(id, range) if range.start > 0 =>
64665
wenzelm
parents: 64664
diff changeset
   261
        snapshot.find_command(id) match {
72823
ab1a49ac456b tuned signature;
wenzelm
parents: 71704
diff changeset
   262
          case Some((node, command)) if snapshot.get_node(command.node_name) eq node =>
61011
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   263
            node.command_start(command) match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   264
              case Some(start) => text_offset == start + command.chunk.decode(range.start)
61011
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   265
              case None => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   266
            }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   267
          case _ => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   268
        }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   269
      case _ => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   270
    }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   271
  }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   272
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   273
  def hyperlink_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   274
      : Option[Hyperlink] =
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   275
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   276
      case Position.Item_File(name, line, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   277
        hyperlink_source_file(focus, name, line, range.start)
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   278
      case Position.Item_Id(id, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   279
        hyperlink_command(focus, snapshot, id, range.start)
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   280
      case _ => None
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   281
    }
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   282
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   283
  def hyperlink_def_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   284
      : Option[Hyperlink] =
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   285
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   286
      case Position.Item_Def_File(name, line, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   287
        hyperlink_source_file(focus, name, line, range.start)
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   288
      case Position.Item_Def_Id(id, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   289
        hyperlink_command(focus, snapshot, id, range.start)
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   290
      case _ => None
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   291
    }
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   292
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   293
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   294
  /* dispatcher thread */
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   295
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   296
  override def assert_dispatcher[A](body: => A): A = GUI_Thread.assert(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   297
  override def require_dispatcher[A](body: => A): A = GUI_Thread.require(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   298
  override def send_dispatcher(body: => Unit): Unit = GUI_Thread.later(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   299
  override def send_wait_dispatcher(body: => Unit): Unit = GUI_Thread.now(body)
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
   300
}