src/Tools/jEdit/src/active.scala
author wenzelm
Thu, 07 Nov 2024 12:08:32 +0100
changeset 81382 5e8287d34295
parent 76765 c654103e9c9d
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50341
diff changeset
     1
/*  Title:      Tools/jEdit/src/active.scala
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
     3
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50341
diff changeset
     4
Active areas within the document.
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
     5
*/
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
     6
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
     8
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
     9
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    10
import isabelle._
71742
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    11
import org.gjt.sp.jedit.{ServiceManager, View}
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    12
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    14
object Active {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    15
  abstract class Handler {
71742
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    16
    def handle(
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    17
      view: View, text: String, elem: XML.Elem,
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    18
      doc_view: Document_View, snapshot: Document.Snapshot): Boolean
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    19
  }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    20
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    21
  def handlers: List[Handler] =
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    22
    ServiceManager.getServiceNames(classOf[Handler]).toList
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    23
      .map(ServiceManager.getService(classOf[Handler], _))
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    24
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    25
  def action(view: View, text: String, elem: XML.Elem): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57594
diff changeset
    26
    GUI_Thread.require {}
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    27
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64664
diff changeset
    28
    Document_View.get(view.getTextArea) match {
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    29
      case Some(doc_view) =>
71775
wenzelm
parents: 71742
diff changeset
    30
        doc_view.rich_text_area.robust_body(()) {
76765
c654103e9c9d more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents: 75393
diff changeset
    31
          val snapshot = Document_Model.snapshot(doc_view.model)
50164
77668b522ffe some support for implicit senback, meaning that it uses the caret position instead of explicit command exec_id;
wenzelm
parents: 49493
diff changeset
    32
          if (!snapshot.is_outdated) {
71742
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    33
            handlers.find(_.handle(view, text, elem, doc_view, snapshot))
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    34
          }
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    35
        }
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    36
      case None =>
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    37
    }
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
    38
  }
71742
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    39
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    40
  class Misc_Handler extends Active.Handler {
71742
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    41
    override def handle(
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    42
      view: View, text: String, elem: XML.Elem,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    43
      doc_view: Document_View, snapshot: Document.Snapshot
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    44
    ): Boolean = {
71742
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    45
      val text_area = doc_view.text_area
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    46
      val model = doc_view.model
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    47
      val buffer = model.buffer
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    48
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    49
      elem match {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    50
        case XML.Elem(Markup(Markup.BROWSER, _), body) =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    51
          Isabelle_Thread.fork(name = "browser") {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    52
            val graph_file = Isabelle_System.tmp_file("graph")
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    53
            File.write(graph_file, XML.content(body))
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    54
            Isabelle_System.bash("isabelle browser -c " + File.bash_path(graph_file) + " &")
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    55
          }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    56
          true
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    57
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    58
        case XML.Elem(Markup(Markup.THEORY_EXPORTS, props), _) =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    59
          GUI_Thread.later {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    60
            val name = Markup.Name.unapply(props) getOrElse ""
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    61
            PIDE.editor.hyperlink_file(true, Isabelle_Export.vfs_prefix + name).follow(view)
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    62
          }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    63
          true
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    64
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    65
        case XML.Elem(Markup(Markup.JEDIT_ACTION, _), body) =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    66
          GUI_Thread.later {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    67
            view.getInputHandler.invokeAction(XML.content(body))
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    68
          }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    69
          true
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    70
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    71
        case XML.Elem(Markup(Markup.SIMP_TRACE_PANEL, props), _) =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    72
          val link =
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    73
            props match {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    74
              case Position.Id(id) => PIDE.editor.hyperlink_command(true, snapshot, id)
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    75
              case _ => None
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    76
            }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    77
          GUI_Thread.later {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    78
            link.foreach(_.follow(view))
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    79
            view.getDockableWindowManager.showDockableWindow("isabelle-simplifier-trace")
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    80
          }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    81
          true
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    82
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    83
        case XML.Elem(Markup(Markup.SENDBACK, props), _) =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    84
          if (buffer.isEditable) {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    85
            props match {
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    86
              case Position.Id(id) =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    87
                Isabelle.edit_command(snapshot, text_area,
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    88
                  props.contains(Markup.PADDING_COMMAND), id, text)
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    89
              case _ =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    90
                if (props.contains(Markup.PADDING_LINE))
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    91
                  Isabelle.insert_line_padding(text_area, text)
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    92
                else text_area.setSelectedText(text)
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    93
            }
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    94
            text_area.requestFocus()
71742
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    95
          }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    96
          true
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    97
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    98
        case Protocol.Dialog(id, serial, result) =>
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
    99
          model.session.dialog_result(id, serial, result)
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
   100
          true
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
   101
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
   102
        case _ => false
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
   103
      }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
   104
    }
de37910974da avoid hard-wired stuff: configure via plugin services;
wenzelm
parents: 71692
diff changeset
   105
  }
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents:
diff changeset
   106
}