src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Wed, 03 Feb 2021 20:18:34 +0100
changeset 73224 49686e3b1909
parent 72823 ab1a49ac456b
child 73340 0ffcad1f6130
permissions -rw-r--r--
clarified links to external files, e.g. .pdf within .thy source document;
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
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
    13
import java.io.{File => JFile}
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
    14
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
    15
import org.gjt.sp.jedit.{jEdit, View, Buffer}
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
    16
import org.gjt.sp.jedit.browser.VFSBrowser
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
    17
import org.gjt.sp.jedit.io.{VFSManager, VFSFile}
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
66083
wenzelm
parents: 66082
diff changeset
    20
class JEdit_Editor extends Editor[View]
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    21
{
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    22
  /* session */
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    23
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    24
  override def session: Session = PIDE.session
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    25
66084
7f8eeff20f7a tuned signature;
wenzelm
parents: 66083
diff changeset
    26
  def flush_edits(hidden: Boolean = false, purge: Boolean = false): Unit =
64817
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    27
    GUI_Thread.require {
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64841
diff changeset
    28
      val (doc_blobs, edits) = Document_Model.flush_edits(hidden, purge)
64817
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    29
      session.update(doc_blobs, edits)
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    30
    }
66084
7f8eeff20f7a tuned signature;
wenzelm
parents: 66083
diff changeset
    31
  override def flush(): Unit = flush_edits()
7f8eeff20f7a tuned signature;
wenzelm
parents: 66083
diff changeset
    32
  def purge() { flush_edits(purge = true) }
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64841
diff changeset
    33
64524
e6a3c55b929b explicit option editor_generated_input_delay, which is more aggressive by default;
wenzelm
parents: 64521
diff changeset
    34
  private val delay1_flush =
71704
b9a5eb0f3b43 clarified modules;
wenzelm
parents: 71692
diff changeset
    35
    Delay.last(PIDE.options.seconds("editor_input_delay"), gui = true) { flush() }
54461
6c360a6ade0e centralized management of pending buffer edits;
wenzelm
parents: 54325
diff changeset
    36
64524
e6a3c55b929b explicit option editor_generated_input_delay, which is more aggressive by default;
wenzelm
parents: 64521
diff changeset
    37
  private val delay2_flush =
71704
b9a5eb0f3b43 clarified modules;
wenzelm
parents: 71692
diff changeset
    38
    Delay.first(PIDE.options.seconds("editor_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
    39
64524
e6a3c55b929b explicit option editor_generated_input_delay, which is more aggressive by default;
wenzelm
parents: 64521
diff changeset
    40
  def invoke(): Unit = delay1_flush.invoke()
e6a3c55b929b explicit option editor_generated_input_delay, which is more aggressive by default;
wenzelm
parents: 64521
diff changeset
    41
  def invoke_generated(): Unit = { delay1_flush.invoke(); delay2_flush.invoke() }
54461
6c360a6ade0e centralized management of pending buffer edits;
wenzelm
parents: 54325
diff changeset
    42
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    43
  def shutdown(): Unit =
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    44
    GUI_Thread.require {
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    45
      delay1_flush.revoke()
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    46
      delay2_flush.revoke()
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    47
      Document_Model.flush_edits(hidden = false, purge = false)
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    48
    }
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66101
diff changeset
    49
64799
c0c648911f1a misc tuning and clarification;
wenzelm
parents: 64682
diff changeset
    50
  def visible_node(name: Document.Node.Name): Boolean =
64840
wenzelm
parents: 64835
diff changeset
    51
    (for {
wenzelm
parents: 64835
diff changeset
    52
      text_area <- JEdit_Lib.jedit_text_areas()
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64867
diff changeset
    53
      doc_view <- Document_View.get(text_area)
64840
wenzelm
parents: 64835
diff changeset
    54
    } yield doc_view.model.node_name).contains(name)
64799
c0c648911f1a misc tuning and clarification;
wenzelm
parents: 64682
diff changeset
    55
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    56
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    57
  /* current situation */
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    58
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    59
  override def current_node(view: View): Option[Document.Node.Name] =
64813
7283f41d05ab manage buffer models as explicit global state;
wenzelm
parents: 64799
diff changeset
    60
    GUI_Thread.require { Document_Model.get(view.getBuffer).map(_.node_name) }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    61
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    62
  override def current_node_snapshot(view: View): Option[Document.Snapshot] =
64813
7283f41d05ab manage buffer models as explicit global state;
wenzelm
parents: 64799
diff changeset
    63
    GUI_Thread.require { Document_Model.get(view.getBuffer).map(_.snapshot()) }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    64
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    65
  override def node_snapshot(name: Document.Node.Name): Document.Snapshot =
52974
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
    66
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
    67
    GUI_Thread.require {}
64817
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    68
    Document_Model.get(name) match {
0bb6b582bb4f separate Buffer_Model vs. File_Model;
wenzelm
parents: 64813
diff changeset
    69
      case Some(model) => model.snapshot
52974
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
    70
      case None => session.snapshot(name)
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
    71
    }
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
    72
  }
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
    73
53844
71f103629327 skip ignored commands, similar to former proper_command_at (see d68ea01d5084) -- relevant to Output, Query_Operation etc.;
wenzelm
parents: 52980
diff changeset
    74
  override def current_command(view: View, snapshot: Document.Snapshot): Option[Command] =
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    75
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
    76
    GUI_Thread.require {}
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    77
54325
2c4155003352 clarified Editor.current_command: allow outdated snapshot;
wenzelm
parents: 53845
diff changeset
    78
    val text_area = view.getTextArea
54528
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    79
    val buffer = view.getBuffer
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    80
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64867
diff changeset
    81
    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
    82
      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
    83
        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
    84
      case _ =>
64813
7283f41d05ab manage buffer models as explicit global state;
wenzelm
parents: 64799
diff changeset
    85
        Document_Model.get(buffer) match {
54531
8330faaeebd5 restrict node_required status and Theories panel to actual theories;
wenzelm
parents: 54528
diff changeset
    86
          case Some(model) if !model.is_theory =>
65187
wenzelm
parents: 64882
diff changeset
    87
            snapshot.version.nodes.commands_loading(model.node_name).headOption
54528
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    88
          case _ => None
842adea880a4 refer to thy_load command of auxiliary file;
wenzelm
parents: 54522
diff changeset
    89
        }
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    90
    }
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents:
diff changeset
    91
  }
52977
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    92
15254e32d299 central management of Document.Overlays, independent of Document_Model;
wenzelm
parents: 52974
diff changeset
    93
66101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
    94
  /* overlays */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
    95
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
    96
  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
    97
    Document_Model.node_overlays(name)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
    98
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
    99
  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
   100
    Document_Model.insert_overlay(command, fn, args)
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 remove_overlay(command: Command, fn: String, args: List[String]): Unit =
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66094
diff changeset
   103
    Document_Model.remove_overlay(command, fn, args)
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
55877
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   106
  /* navigation */
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   107
56413
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   108
  def push_position(view: View)
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   109
  {
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   110
    val navigator = jEdit.getPlugin("ise.plugin.nav.NavigatorPlugin")
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   111
    if (navigator != null) {
59080
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 58545
diff changeset
   112
      try { Untyped.method(navigator.getClass, "pushPosition", view.getClass).invoke(null, view) }
56413
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   113
      catch { case _: NoSuchMethodException => }
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   114
    }
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   115
  }
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   116
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   117
  def goto_buffer(focus: Boolean, view: View, buffer: Buffer, offset: Text.Offset)
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   118
  {
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   119
    GUI_Thread.require {}
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   120
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   121
    push_position(view)
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   122
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   123
    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
   124
    try { view.getTextArea.moveCaretPosition(offset) }
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   125
    catch {
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   126
      case _: ArrayIndexOutOfBoundsException =>
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   127
      case _: IllegalArgumentException =>
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   128
    }
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   129
  }
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents: 57878
diff changeset
   130
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   131
  def goto_file(focus: Boolean, view: View, name: String): Unit =
66605
261dcd52c5a0 less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
wenzelm
parents: 66458
diff changeset
   132
    goto_file(focus, view, Line.Node_Position.offside(name))
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   133
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   134
  def goto_file(focus: Boolean, view: View, pos: Line.Node_Position)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   135
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
   136
    GUI_Thread.require {}
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   137
56413
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   138
    push_position(view)
2d4d9a5f68ff support for jEdit Navigator plugin;
wenzelm
parents: 56373
diff changeset
   139
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   140
    val name = pos.name
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   141
    val line = pos.line
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   142
    val column = pos.column
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   143
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
   144
    JEdit_Lib.jedit_buffer(name) match {
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   145
      case Some(buffer) =>
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   146
        if (focus) view.goToBuffer(buffer) else view.showBuffer(buffer)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   147
        val text_area = view.getTextArea
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   148
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   149
        try {
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   150
          val line_start = text_area.getBuffer.getLineStartOffset(line)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   151
          text_area.moveCaretPosition(line_start)
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   152
          if (column > 0) text_area.moveCaretPosition(line_start + column)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   153
        }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   154
        catch {
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   155
          case _: ArrayIndexOutOfBoundsException =>
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   156
          case _: IllegalArgumentException =>
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   157
        }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   158
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   159
      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
   160
        val is_dir =
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   161
          try {
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   162
            val vfs = VFSManager.getVFSForPath(name)
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   163
            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
   164
            vfs_file != null && vfs_file.getType == VFSFile.DIRECTORY
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   165
          }
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   166
          catch { case ERROR(_) => false }
54706
d3c656f0b7ab browse directory hyperlink as well;
wenzelm
parents: 54702
diff changeset
   167
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
   168
        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
   169
        else if (!Isabelle_System.open_external_file(name)) {
70016
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   170
          val args =
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   171
            if (line <= 0) Array(name)
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   172
            else if (column <= 0) Array(name, "+line:" + (line + 1))
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   173
            else Array(name, "+line:" + (line + 1) + "," + (column + 1))
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   174
          jEdit.openFiles(view, null, args)
a8142ac5e4b6 more PIDE markup and hyperlinks;
wenzelm
parents: 69901
diff changeset
   175
        }
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   176
    }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   177
  }
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   178
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   179
  def goto_doc(view: View, path: Path)
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   180
  {
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   181
    if (path.is_file)
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   182
      goto_file(true, view, File.platform_path(path))
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   183
    else {
71692
f8e52c0152fe clarified names;
wenzelm
parents: 71685
diff changeset
   184
      Isabelle_Thread.fork(name = "documentation") {
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   185
        try { Doc.view(path) }
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   186
        catch {
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   187
          case exn: Throwable =>
62062
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   188
            GUI_Thread.later {
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   189
              GUI.error_dialog(view,
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   190
                "Documentation 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
   191
            }
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   192
        }
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   193
      }
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   194
    }
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   195
  }
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   196
55877
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   197
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   198
  /* hyperlinks */
65c9968286d5 tuned signature;
wenzelm
parents: 55876
diff changeset
   199
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   200
  def hyperlink_doc(name: String): Option[Hyperlink] =
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   201
    Doc.contents().collectFirst({
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   202
      case doc: Doc.Text_File if doc.name == name => doc.path
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   203
      case doc: Doc.Doc if doc.name == name => doc.path}).map(path =>
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   204
        new Hyperlink {
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 70167
diff changeset
   205
          override val external: Boolean = !path.is_file
61660
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   206
          def follow(view: View): Unit = goto_doc(view, path)
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   207
          override def toString: String = "doc " + quote(name)
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   208
        })
78b371644654 added antiquotation @{doc}, e.g. useful for demonstration purposes;
wenzelm
parents: 61557
diff changeset
   209
56461
wenzelm
parents: 56458
diff changeset
   210
  def hyperlink_url(name: String): Hyperlink =
wenzelm
parents: 56458
diff changeset
   211
    new Hyperlink {
64663
wenzelm
parents: 64662
diff changeset
   212
      override val external = true
56729
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   213
      def follow(view: View): Unit =
71692
f8e52c0152fe clarified names;
wenzelm
parents: 71685
diff changeset
   214
        Isabelle_Thread.fork(name = "hyperlink_url") {
69901
20b32ade0024 clarified signature;
wenzelm
parents: 69650
diff changeset
   215
          try { Isabelle_System.open(Url.escape_name(name)) }
56461
wenzelm
parents: 56458
diff changeset
   216
          catch {
wenzelm
parents: 56458
diff changeset
   217
            case exn: Throwable =>
62062
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   218
              GUI_Thread.later {
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 61728
diff changeset
   219
                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
   220
              }
56729
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   221
          }
1da2272a06a4 prefer Isabelle/Scala operations;
wenzelm
parents: 56662
diff changeset
   222
        }
56461
wenzelm
parents: 56458
diff changeset
   223
      override def toString: String = "URL " + quote(name)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   224
    }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   225
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   226
  def hyperlink_file(focus: Boolean, name: String): Hyperlink =
66605
261dcd52c5a0 less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
wenzelm
parents: 66458
diff changeset
   227
    hyperlink_file(focus, Line.Node_Position.offside(name))
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   228
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   229
  def hyperlink_file(focus: Boolean, pos: Line.Node_Position): Hyperlink =
56461
wenzelm
parents: 56458
diff changeset
   230
    new Hyperlink {
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   231
      def follow(view: View): Unit = goto_file(focus, view, pos)
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   232
      override def toString: String = "file " + quote(pos.name)
56461
wenzelm
parents: 56458
diff changeset
   233
    }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   234
64829
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   235
  def hyperlink_model(focus: Boolean, model: Document_Model, offset: Text.Offset): Hyperlink =
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   236
    model match {
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   237
      case file_model: File_Model =>
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   238
        val pos =
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   239
          try { file_model.node_position(offset) }
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   240
          catch { case ERROR(_) => Line.Node_Position(file_model.node_name.node) }
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   241
        hyperlink_file(focus, pos)
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   242
      case buffer_model: Buffer_Model =>
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   243
        new Hyperlink {
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   244
          def follow(view: View): Unit = goto_buffer(focus, view, buffer_model.buffer, offset)
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   245
          override def toString: String = "buffer " + quote(model.node_name.node)
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   246
        }
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   247
    }
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64817
diff changeset
   248
64653
89c5bb2a2128 tuned -- use zero-based Line.Position;
wenzelm
parents: 64613
diff changeset
   249
  def hyperlink_source_file(focus: Boolean, source_name: String, line1: Int, offset: Symbol.Offset)
55878
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   250
    : Option[Hyperlink] =
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   251
  {
64657
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64653
diff changeset
   252
    for (name <- PIDE.resources.source_file(source_name)) yield {
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   253
      def hyperlink(pos: Line.Position) =
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   254
        hyperlink_file(focus, Line.Node_Position(name, pos))
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   255
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   256
      if (offset > 0) {
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   257
        PIDE.resources.get_file_content(PIDE.resources.node_name(name)) match {
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   258
          case Some(text) =>
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   259
            hyperlink(
64657
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64653
diff changeset
   260
              (Line.Position.zero /:
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   261
                (Symbol.iterator(text).
65196
e8760a98db78 discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
wenzelm
parents: 65187
diff changeset
   262
                  zipWithIndex.takeWhile(p => p._2 < offset - 1).map(_._1)))(_.advance(_)))
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   263
          case None =>
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   264
            hyperlink(Line.Position((line1 - 1) max 0))
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   265
        }
55878
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   266
      }
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64840
diff changeset
   267
      else hyperlink(Line.Position((line1 - 1) max 0))
55878
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   268
    }
6d092a5166f1 more precise navigation within open files;
wenzelm
parents: 55877
diff changeset
   269
  }
55876
142139457653 tuned signature;
wenzelm
parents: 55822
diff changeset
   270
56461
wenzelm
parents: 56458
diff changeset
   271
  override def hyperlink_command(
64664
wenzelm
parents: 64663
diff changeset
   272
    focus: Boolean, snapshot: Document.Snapshot, id: Document_ID.Generic, offset: Symbol.Offset = 0)
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   273
      : Option[Hyperlink] =
56461
wenzelm
parents: 56458
diff changeset
   274
  {
wenzelm
parents: 56458
diff changeset
   275
    if (snapshot.is_outdated) None
64665
wenzelm
parents: 64664
diff changeset
   276
    else snapshot.find_command_position(id, offset).map(hyperlink_file(focus, _))
56461
wenzelm
parents: 56458
diff changeset
   277
  }
wenzelm
parents: 56458
diff changeset
   278
61011
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   279
  def is_hyperlink_position(snapshot: Document.Snapshot,
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   280
    text_offset: Text.Offset, pos: Position.T): Boolean =
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   281
  {
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   282
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   283
      case Position.Item_Id(id, range) if range.start > 0 =>
64665
wenzelm
parents: 64664
diff changeset
   284
        snapshot.find_command(id) match {
72823
ab1a49ac456b tuned signature;
wenzelm
parents: 71704
diff changeset
   285
          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
   286
            node.command_start(command) match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   287
              case Some(start) => text_offset == start + command.chunk.decode(range.start)
61011
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   288
              case None => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   289
            }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   290
          case _ => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   291
        }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   292
      case _ => false
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   293
    }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   294
  }
018b0c996b54 more explicit debugger caret rendering;
wenzelm
parents: 60988
diff changeset
   295
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   296
  def hyperlink_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   297
      : Option[Hyperlink] =
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   298
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   299
      case Position.Item_File(name, line, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   300
        hyperlink_source_file(focus, name, line, range.start)
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   301
      case Position.Item_Id(id, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   302
        hyperlink_command(focus, snapshot, id, range.start)
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   303
      case _ => None
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   304
    }
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   305
60893
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   306
  def hyperlink_def_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
3c8b9b4b577c support hyperlinks with optional focus change;
wenzelm
parents: 60874
diff changeset
   307
      : Option[Hyperlink] =
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   308
    pos match {
64667
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   309
      case Position.Item_Def_File(name, line, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   310
        hyperlink_source_file(focus, name, line, range.start)
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   311
      case Position.Item_Def_Id(id, range) =>
cdb0d559a24b full range for Position.Item;
wenzelm
parents: 64665
diff changeset
   312
        hyperlink_command(focus, snapshot, id, range.start)
60874
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   313
      case _ => None
7865e03a7fc1 tuned signature;
wenzelm
parents: 59319
diff changeset
   314
    }
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   315
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   316
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   317
  /* dispatcher thread */
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   318
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   319
  override def assert_dispatcher[A](body: => A): A = GUI_Thread.assert(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   320
  override def require_dispatcher[A](body: => A): A = GUI_Thread.require(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   321
  override def send_dispatcher(body: => Unit): Unit = GUI_Thread.later(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66084
diff changeset
   322
  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
   323
}