src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Wed, 02 Apr 2025 23:18:12 +0200
changeset 82418 6898054035d6
parent 82416 7b98d2a8ee6e
child 82419 955c5d6c1acf
permissions -rw-r--r--
support goto_file / hyperlink_file with offset; clarified 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}
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    17
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    18
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
    19
class JEdit_Editor extends Editor[View] {
76794
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    20
  /* PIDE session and document model */
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    21
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    22
  override def session: Session = PIDE.session
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    23
66084
7f8eeff20f7a tuned signature;
wenzelm
parents: 66083
diff changeset
    24
  def flush_edits(hidden: Boolean = false, purge: Boolean = false): Unit =
64817
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    25
    GUI_Thread.require {
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64841
diff changeset
    26
      val (doc_blobs, edits) = Document_Model.flush_edits(hidden, purge)
64817
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    27
      session.update(doc_blobs, edits)
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    28
    }
66084
7f8eeff20f7a tuned signature;
wenzelm
parents: 66083
diff changeset
    29
  override def flush(): Unit = flush_edits()
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73224
diff changeset
    30
  def purge(): Unit = flush_edits(purge = true)
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64841
diff changeset
    31
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    32
  private val delay_input: Delay =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 76044
diff changeset
    33
    Delay.last(PIDE.session.input_delay, gui = true) { flush() }
54461
6c360a6ade0e centralized management of pending buffer edits;
wenzelm
parents: 54325
diff changeset
    34
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    35
  private val delay_generated_input: Delay =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 76044
diff changeset
    36
    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
    37
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    38
  def invoke(): Unit = delay_input.invoke()
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    39
  def invoke_generated(): Unit = { delay_input.invoke(); delay_generated_input.invoke() }
54461
6c360a6ade0e centralized management of pending buffer edits;
wenzelm
parents: 54325
diff changeset
    40
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    41
  def shutdown(): Unit =
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    42
    GUI_Thread.require {
76044
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    43
      delay_input.revoke()
c90799513ed0 tuned signature;
wenzelm
parents: 75393
diff changeset
    44
      delay_generated_input.revoke()
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    45
      Document_Model.flush_edits(hidden = false, purge = false)
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    46
    }
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    47
64799
c0c648911f1a misc tuning and clarification;
wenzelm
parents: 64682
diff changeset
    48
  def visible_node(name: Document.Node.Name): Boolean =
64840
wenzelm
parents: 64835
diff changeset
    49
    (for {
wenzelm
parents: 64835
diff changeset
    50
      text_area <- JEdit_Lib.jedit_text_areas()
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64867
diff changeset
    51
      doc_view <- Document_View.get(text_area)
64840
wenzelm
parents: 64835
diff changeset
    52
    } yield doc_view.model.node_name).contains(name)
64799
c0c648911f1a misc tuning and clarification;
wenzelm
parents: 64682
diff changeset
    53
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    54
76794
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    55
  override def get_models(): Iterable[Document.Model] = Document_Model.get_models()
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    56
941d4f527091 clarified modules: avoid duplication;
wenzelm
parents: 76768
diff changeset
    57
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    58
  /* global changes */
76705
ddf5764684dd proper state change, e.g. on open/close of "Document" panel;
wenzelm
parents: 76610
diff changeset
    59
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    60
  def state_changed(): Unit = {
76708
2368724cde54 proper thread context;
wenzelm
parents: 76706
diff changeset
    61
    GUI_Thread.later { flush() }
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    62
    PIDE.plugin.deps_changed()
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    63
    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
    64
  }
ddf5764684dd proper state change, e.g. on open/close of "Document" panel;
wenzelm
parents: 76610
diff changeset
    65
76715
bf5ff407f32f clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents: 76708
diff changeset
    66
  override def document_state_changed(): Unit = state_changed()
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76705
diff changeset
    67
76705
ddf5764684dd proper state change, e.g. on open/close of "Document" panel;
wenzelm
parents: 76610
diff changeset
    68
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    69
  /* current situation */
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    70
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    71
  override def current_node(view: View): Option[Document.Node.Name] =
76768
40c8275f0131 tuned signature: follow terminology of VSCode_Resources;
wenzelm
parents: 76765
diff changeset
    72
    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
    73
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    74
  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
    75
    GUI_Thread.require { Document_Model.get_snapshot(view.getBuffer) }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    76
76765
c654103e9c9d more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents: 76715
diff changeset
    77
  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
    78
    GUI_Thread.require { Document_Model.get_snapshot(name) getOrElse session.snapshot(name) }
52974
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
    79
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
    80
  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
    81
    GUI_Thread.require {}
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    82
54325
2c4155003352 clarified Editor.current_command: allow outdated snapshot;
wenzelm
parents: 53845
diff changeset
    83
    val text_area = view.getTextArea
54528
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    84
    val buffer = view.getBuffer
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    85
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64867
diff changeset
    86
    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
    87
      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
    88
        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
    89
      case _ =>
76768
40c8275f0131 tuned signature: follow terminology of VSCode_Resources;
wenzelm
parents: 76765
diff changeset
    90
        Document_Model.get_model(buffer) match {
54531
8330faaeebd5 restrict node_required status and Theories panel to actual theories;
wenzelm
parents: 54528
diff changeset
    91
          case Some(model) if !model.is_theory =>
65187
wenzelm
parents: 64882
diff changeset
    92
            snapshot.version.nodes.commands_loading(model.node_name).headOption
54528
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    93
          case _ => None
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    94
        }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    95
    }
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    96
  }
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    97
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    98
66101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
    99
  /* overlays */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   100
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   101
  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
   102
    Document_Model.node_overlays(name)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   103
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   104
  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
   105
    Document_Model.insert_overlay(command, fn, args)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   106
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   107
  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
   108
    Document_Model.remove_overlay(command, fn, args)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   109
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   110
55877
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   111
  /* navigation */
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   112
82413
a6046b6d23b4 more accurate navigator position;
wenzelm
parents: 82193
diff changeset
   113
  def record_position(view: View): Unit = {
a6046b6d23b4 more accurate navigator position;
wenzelm
parents: 82193
diff changeset
   114
    PIDE.plugin.navigator.record(view)
56413
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   115
    val navigator = jEdit.getPlugin("ise.plugin.nav.NavigatorPlugin")
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   116
    if (navigator != null) {
59080
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 58545
diff changeset
   117
      try { Untyped.method(navigator.getClass, "pushPosition", view.getClass).invoke(null, view) }
56413
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   118
      catch { case _: NoSuchMethodException => }
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   119
    }
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   120
  }
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   121
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   122
  def goto_buffer(focus: Boolean, view: View, buffer: Buffer, offset: Text.Offset): Unit = {
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   123
    GUI_Thread.require {}
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   124
82413
a6046b6d23b4 more accurate navigator position;
wenzelm
parents: 82193
diff changeset
   125
    record_position(view)
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   126
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   127
    if (focus) view.goToBuffer(buffer) else view.showBuffer(buffer)
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   128
    try { view.getTextArea.moveCaretPosition(offset) }
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   129
    catch {
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   130
      case _: ArrayIndexOutOfBoundsException =>
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   131
      case _: IllegalArgumentException =>
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   132
    }
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   133
  }
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   134
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   135
  def goto_file(
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   136
    focus: Boolean,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   137
    view: View,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   138
    name: String,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   139
    offset: Text.Offset = -1,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   140
    line: Int = -1,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   141
    column: Int = -1
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   142
  ): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
   143
    GUI_Thread.require {}
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   144
82413
a6046b6d23b4 more accurate navigator position;
wenzelm
parents: 82193
diff changeset
   145
    record_position(view)
56413
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   146
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
   147
    JEdit_Lib.jedit_buffer(name) match {
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   148
      case Some(buffer) =>
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   149
        if (focus) view.goToBuffer(buffer) else view.showBuffer(buffer)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   150
        val text_area = view.getTextArea
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   151
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   152
        try {
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   153
          if (offset >= 0) text_area.moveCaretPosition(offset)
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   154
          else if (line >= 0) {
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   155
            val line_start = text_area.getBuffer.getLineStartOffset(line)
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   156
            text_area.moveCaretPosition(line_start)
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   157
            if (column > 0) text_area.moveCaretPosition(line_start + column)
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   158
          }
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   159
        }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   160
        catch {
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   161
          case _: ArrayIndexOutOfBoundsException =>
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   162
          case _: IllegalArgumentException =>
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   163
        }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   164
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   165
      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
   166
        val is_dir =
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   167
          try {
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   168
            val vfs = VFSManager.getVFSForPath(name)
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   169
            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
   170
            vfs_file != null && vfs_file.getType == VFSFile.DIRECTORY
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   171
          }
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   172
          catch { case ERROR(_) => false }
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
   173
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
   174
        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
   175
        else if (!Isabelle_System.open_external_file(name)) {
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   176
          val args =
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   177
            if (offset >= 0) Array(name, "+offset:" + offset)
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   178
            else if (line <= 0) Array(name)
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   179
            else if (column <= 0) Array(name, "+line:" + (line + 1))
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   180
            else Array(name, "+line:" + (line + 1) + "," + (column + 1))
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   181
          jEdit.openFiles(view, null, args)
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   182
        }
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   183
    }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   184
  }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   185
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   186
  def goto_doc(view: View, path: Path): Unit = {
75120
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   187
    if (path.is_pdf) Doc.view(path)
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   188
    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
   189
  }
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   190
55877
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   191
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   192
  /* hyperlinks */
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   193
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   194
  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
   195
    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
   196
      new Hyperlink {
1818358373e2 misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents: 80225
diff changeset
   197
        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
   198
        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
   199
        override def toString: String = "doc " + quote(name)
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75115
diff changeset
   200
      })
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   201
56461
wenzelm
parents: 56458
diff changeset
   202
  def hyperlink_url(name: String): Hyperlink =
wenzelm
parents: 56458
diff changeset
   203
    new Hyperlink {
64663
wenzelm
parents: 64662
diff changeset
   204
      override val external = true
56729
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   205
      def follow(view: View): Unit =
71692
f8e52c0152fe clarified names;
wenzelm
parents: 71685
diff changeset
   206
        Isabelle_Thread.fork(name = "hyperlink_url") {
69901
20b32ade0024 clarified signature;
wenzelm
parents: 69650
diff changeset
   207
          try { Isabelle_System.open(Url.escape_name(name)) }
56461
wenzelm
parents: 56458
diff changeset
   208
          catch {
wenzelm
parents: 56458
diff changeset
   209
            case exn: Throwable =>
62062
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   210
              GUI_Thread.later {
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   211
                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
   212
              }
56729
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   213
          }
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   214
        }
56461
wenzelm
parents: 56458
diff changeset
   215
      override def toString: String = "URL " + quote(name)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   216
    }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   217
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   218
  def hyperlink_file(
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   219
    focus: Boolean,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   220
    name: String,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   221
    offset: Text.Offset = -1,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   222
    line: Int = -1,
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   223
    column: Int = -1
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   224
  ): Hyperlink =
56461
wenzelm
parents: 56458
diff changeset
   225
    new Hyperlink {
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   226
      def follow(view: View): Unit =
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   227
        goto_file(focus, view, name, offset = offset, line = line, column = column)
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   228
      override def toString: String = "file " + quote(name)
56461
wenzelm
parents: 56458
diff changeset
   229
    }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   230
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   231
  def hyperlink_source_file(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   232
    focus: Boolean,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   233
    source_name: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   234
    line1: Int,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   235
    offset: Symbol.Offset
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   236
  ) : Option[Hyperlink] = {
82193
355a2c1cdf40 tuned signature: emphasize semantics;
wenzelm
parents: 81350
diff changeset
   237
    for (platform_path <- PIDE.resources.source_file(source_name)) yield {
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   238
      def hyperlink(pos: Line.Position) =
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   239
        hyperlink_file(focus, platform_path, line = pos.line, column = pos.column)
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   240
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   241
      if (offset > 0) {
82193
355a2c1cdf40 tuned signature: emphasize semantics;
wenzelm
parents: 81350
diff changeset
   242
        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
   243
          case Some(text) =>
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   244
            hyperlink(
73358
78aa7846e91f tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   245
              Symbol.iterator(text).zipWithIndex.takeWhile(p => p._2 < offset - 1).map(_._1).
78aa7846e91f tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   246
                foldLeft(Line.Position.zero)(_.advance(_)))
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   247
          case None =>
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   248
            hyperlink(Line.Position((line1 - 1) max 0))
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   249
        }
55878
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   250
      }
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   251
      else hyperlink(Line.Position((line1 - 1) max 0))
55878
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   252
    }
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   253
  }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   254
56461
wenzelm
parents: 56458
diff changeset
   255
  override def hyperlink_command(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   256
    focus: Boolean,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   257
    snapshot: Document.Snapshot,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   258
    id: Document_ID.Generic,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   259
    offset: Symbol.Offset = 0
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   260
  ) : Option[Hyperlink] = {
56461
wenzelm
parents: 56458
diff changeset
   261
    if (snapshot.is_outdated) None
82418
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   262
    else {
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   263
      snapshot.find_command_position(id, offset)
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   264
        .map(pos => hyperlink_file(focus, pos.name, line = pos.line))
6898054035d6 support goto_file / hyperlink_file with offset;
wenzelm
parents: 82416
diff changeset
   265
    }
56461
wenzelm
parents: 56458
diff changeset
   266
  }
wenzelm
parents: 56458
diff changeset
   267
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   268
  def is_hyperlink_position(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   269
    snapshot: Document.Snapshot,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   270
    text_offset: Text.Offset,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   271
    pos: Position.T
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75120
diff changeset
   272
  ): Boolean = {
61011
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   273
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   274
      case Position.Item_Id(id, range) if range.start > 0 =>
64665
wenzelm
parents: 64664
diff changeset
   275
        snapshot.find_command(id) match {
72823
ab1a49ac456b tuned signature;
wenzelm
parents: 71704
diff changeset
   276
          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
   277
            node.command_start(command) match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   278
              case Some(start) => text_offset == start + command.chunk.decode(range.start)
61011
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   279
              case None => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   280
            }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   281
          case _ => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   282
        }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   283
      case _ => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   284
    }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   285
  }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   286
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   287
  def hyperlink_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   288
      : Option[Hyperlink] =
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   289
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   290
      case Position.Item_File(name, line, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   291
        hyperlink_source_file(focus, name, line, range.start)
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   292
      case Position.Item_Id(id, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   293
        hyperlink_command(focus, snapshot, id, range.start)
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   294
      case _ => None
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   295
    }
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   296
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   297
  def hyperlink_def_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   298
      : Option[Hyperlink] =
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   299
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   300
      case Position.Item_Def_File(name, line, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   301
        hyperlink_source_file(focus, name, line, range.start)
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   302
      case Position.Item_Def_Id(id, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   303
        hyperlink_command(focus, snapshot, id, range.start)
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   304
      case _ => None
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   305
    }
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   306
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   307
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   308
  /* dispatcher thread */
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   309
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   310
  override def assert_dispatcher[A](body: => A): A = GUI_Thread.assert(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   311
  override def require_dispatcher[A](body: => A): A = GUI_Thread.require(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   312
  override def send_dispatcher(body: => Unit): Unit = GUI_Thread.later(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   313
  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
   314
}