src/Tools/jEdit/src/isabelle.scala
author wenzelm
Tue Feb 18 20:20:42 2014 +0100 (2014-02-18)
changeset 55558 298274c970b6
parent 55557 aa1adeca714b
child 55616 25a7a998852a
permissions -rw-r--r--
more uniform treatment of dockables and their standard actions;
wenzelm@50208
     1
/*  Title:      Tools/jEdit/src/isabelle.scala
wenzelm@50183
     2
    Author:     Makarius
wenzelm@50183
     3
wenzelm@53277
     4
Global configuration and convenience operations for Isabelle/jEdit.
wenzelm@50183
     5
*/
wenzelm@50183
     6
wenzelm@50183
     7
package isabelle.jedit
wenzelm@50183
     8
wenzelm@50183
     9
wenzelm@50183
    10
import isabelle._
wenzelm@50183
    11
wenzelm@53715
    12
import scala.swing.CheckBox
wenzelm@53715
    13
import scala.swing.event.ButtonClicked
wenzelm@53715
    14
wenzelm@50198
    15
import org.gjt.sp.jedit.{jEdit, View, Buffer}
wenzelm@50183
    16
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@53293
    17
import org.gjt.sp.jedit.gui.{DockableWindowManager, CompleteWord}
wenzelm@50183
    18
wenzelm@50183
    19
wenzelm@50208
    20
object Isabelle
wenzelm@50183
    21
{
wenzelm@53274
    22
  /* editor modes */
wenzelm@53274
    23
wenzelm@53277
    24
  val modes =
wenzelm@53277
    25
    List(
wenzelm@53277
    26
      "isabelle",         // theory source
wenzelm@55500
    27
      "isabelle-ml",      // ML source
wenzelm@53281
    28
      "isabelle-markup",  // SideKick markup tree
wenzelm@53277
    29
      "isabelle-news",    // NEWS
wenzelm@53277
    30
      "isabelle-options", // etc/options
wenzelm@53277
    31
      "isabelle-output",  // pretty text area output
wenzelm@53277
    32
      "isabelle-root")    // session ROOT
wenzelm@53274
    33
wenzelm@55500
    34
  private lazy val symbols_syntax = Outer_Syntax.init().no_tokens
wenzelm@53276
    35
wenzelm@53274
    36
  def mode_syntax(name: String): Option[Outer_Syntax] =
wenzelm@53274
    37
    name match {
wenzelm@53281
    38
      case "isabelle" | "isabelle-markup" =>
wenzelm@53277
    39
        val syntax = PIDE.session.recent_syntax
wenzelm@53277
    40
        if (syntax == Outer_Syntax.empty) None else Some(syntax)
wenzelm@53274
    41
      case "isabelle-options" => Some(Options.options_syntax)
wenzelm@53274
    42
      case "isabelle-root" => Some(Build.root_syntax)
wenzelm@55500
    43
      case "isabelle-ml" | "isabelle-news" => Some(symbols_syntax)
wenzelm@53277
    44
      case "isabelle-output" => None
wenzelm@53274
    45
      case _ => None
wenzelm@53274
    46
    }
wenzelm@53274
    47
wenzelm@53274
    48
wenzelm@53277
    49
  /* token markers */
wenzelm@53277
    50
wenzelm@53277
    51
  private val markers =
wenzelm@53280
    52
    Map(modes.map(name => (name, new Token_Markup.Marker(name))): _*)
wenzelm@53277
    53
wenzelm@53277
    54
  def token_marker(name: String): Option[Token_Markup.Marker] = markers.get(name)
wenzelm@53277
    55
wenzelm@53277
    56
wenzelm@50208
    57
  /* dockable windows */
wenzelm@50208
    58
wenzelm@50208
    59
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@50208
    60
wenzelm@55558
    61
  def documentation_dockable(view: View): Option[Documentation_Dockable] =
wenzelm@55558
    62
    wm(view).getDockableWindow("isabelle-documentation") match {
wenzelm@55558
    63
      case dockable: Documentation_Dockable => Some(dockable)
wenzelm@50208
    64
      case _ => None
wenzelm@50208
    65
    }
wenzelm@50208
    66
wenzelm@55558
    67
  def find_dockable(view: View): Option[Find_Dockable] =
wenzelm@55558
    68
    wm(view).getDockableWindow("isabelle-find") match {
wenzelm@55558
    69
      case dockable: Find_Dockable => Some(dockable)
wenzelm@51533
    70
      case _ => None
wenzelm@51533
    71
    }
wenzelm@51533
    72
wenzelm@55558
    73
  def monitor_dockable(view: View): Option[Monitor_Dockable] =
wenzelm@55558
    74
    wm(view).getDockableWindow("isabelle-monitor") match {
wenzelm@55558
    75
      case dockable: Monitor_Dockable => Some(dockable)
wenzelm@55558
    76
      case _ => None
wenzelm@55558
    77
    }
wenzelm@55558
    78
wenzelm@55558
    79
  def output_dockable(view: View): Option[Output_Dockable] =
wenzelm@50208
    80
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@50208
    81
      case dockable: Output_Dockable => Some(dockable)
wenzelm@50208
    82
      case _ => None
wenzelm@50208
    83
    }
wenzelm@50208
    84
wenzelm@55558
    85
  def protocol_dockable(view: View): Option[Protocol_Dockable] =
wenzelm@55558
    86
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@55558
    87
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@55558
    88
      case _ => None
wenzelm@55558
    89
    }
wenzelm@55558
    90
wenzelm@55558
    91
  def raw_output_dockable(view: View): Option[Raw_Output_Dockable] =
wenzelm@50208
    92
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@50208
    93
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@50208
    94
      case _ => None
wenzelm@50208
    95
    }
wenzelm@50208
    96
wenzelm@55558
    97
  def simplifier_trace_dockable(view: View): Option[Simplifier_Trace_Dockable] =
wenzelm@55557
    98
    wm(view).getDockableWindow("isabelle-simplifier-trace") match {
lars@55316
    99
      case dockable: Simplifier_Trace_Dockable => Some(dockable)
lars@55316
   100
      case _ => None
lars@55316
   101
    }
lars@55316
   102
wenzelm@55558
   103
  def sledgehammer_dockable(view: View): Option[Sledgehammer_Dockable] =
wenzelm@55558
   104
    wm(view).getDockableWindow("isabelle-sledgehammer") match {
wenzelm@55558
   105
      case dockable: Sledgehammer_Dockable => Some(dockable)
wenzelm@55558
   106
      case _ => None
wenzelm@55558
   107
    }
wenzelm@55558
   108
wenzelm@55558
   109
  def symbols_dockable(view: View): Option[Symbols_Dockable] =
wenzelm@55558
   110
    wm(view).getDockableWindow("isabelle-symbols") match {
wenzelm@55558
   111
      case dockable: Symbols_Dockable => Some(dockable)
wenzelm@50208
   112
      case _ => None
wenzelm@50208
   113
    }
wenzelm@50208
   114
wenzelm@55558
   115
  def syslog_dockable(view: View): Option[Syslog_Dockable] =
wenzelm@55558
   116
    wm(view).getDockableWindow("isabelle-syslog") match {
wenzelm@55558
   117
      case dockable: Syslog_Dockable => Some(dockable)
wenzelm@55558
   118
      case _ => None
wenzelm@55558
   119
    }
wenzelm@55558
   120
wenzelm@55558
   121
  def theories_dockable(view: View): Option[Theories_Dockable] =
wenzelm@55558
   122
    wm(view).getDockableWindow("isabelle-theories") match {
wenzelm@55558
   123
      case dockable: Theories_Dockable => Some(dockable)
wenzelm@55558
   124
      case _ => None
wenzelm@55558
   125
    }
wenzelm@55558
   126
wenzelm@55558
   127
  def timing_dockable(view: View): Option[Timing_Dockable] =
wenzelm@55558
   128
    wm(view).getDockableWindow("isabelle-timing") match {
wenzelm@55558
   129
      case dockable: Timing_Dockable => Some(dockable)
wenzelm@50433
   130
      case _ => None
wenzelm@50433
   131
    }
wenzelm@50433
   132
wenzelm@50208
   133
wenzelm@52815
   134
  /* continuous checking */
wenzelm@52815
   135
wenzelm@52815
   136
  private val CONTINUOUS_CHECKING = "editor_continuous_checking"
wenzelm@52815
   137
wenzelm@52815
   138
  def continuous_checking: Boolean = PIDE.options.bool(CONTINUOUS_CHECKING)
wenzelm@52815
   139
wenzelm@52815
   140
  def continuous_checking_=(b: Boolean)
wenzelm@52815
   141
  {
wenzelm@52815
   142
    Swing_Thread.require()
wenzelm@52815
   143
wenzelm@52815
   144
    if (continuous_checking != b) {
wenzelm@52815
   145
      PIDE.options.bool(CONTINUOUS_CHECKING) = b
wenzelm@52815
   146
      PIDE.options_changed()
wenzelm@52974
   147
      PIDE.editor.flush()
wenzelm@52815
   148
    }
wenzelm@52815
   149
  }
wenzelm@52815
   150
wenzelm@52815
   151
  def set_continuous_checking() { continuous_checking = true }
wenzelm@52815
   152
  def reset_continuous_checking() { continuous_checking = false }
wenzelm@52815
   153
  def toggle_continuous_checking() { continuous_checking = !continuous_checking }
wenzelm@52815
   154
wenzelm@53715
   155
  class Continuous_Checking extends CheckBox("Continuous checking")
wenzelm@53715
   156
  {
wenzelm@53715
   157
    tooltip = "Continuous checking of proof document (visible and required parts)"
wenzelm@53715
   158
    reactions += { case ButtonClicked(_) => continuous_checking = selected }
wenzelm@53715
   159
    def load() { selected = continuous_checking }
wenzelm@53715
   160
    load()
wenzelm@53715
   161
  }
wenzelm@53715
   162
wenzelm@52815
   163
wenzelm@52815
   164
  /* required document nodes */
wenzelm@52815
   165
wenzelm@52815
   166
  private def node_required_update(view: View, toggle: Boolean = false, set: Boolean = false)
wenzelm@52815
   167
  {
wenzelm@52815
   168
    Swing_Thread.require()
wenzelm@52815
   169
    PIDE.document_model(view.getBuffer) match {
wenzelm@52815
   170
      case Some(model) =>
wenzelm@52816
   171
        model.node_required = (if (toggle) !model.node_required else set)
wenzelm@52815
   172
      case None =>
wenzelm@52815
   173
    }
wenzelm@52815
   174
  }
wenzelm@52815
   175
wenzelm@52815
   176
  def set_node_required(view: View) { node_required_update(view, set = true) }
wenzelm@52815
   177
  def reset_node_required(view: View) { node_required_update(view, set = false) }
wenzelm@52815
   178
  def toggle_node_required(view: View) { node_required_update(view, toggle = true) }
wenzelm@52815
   179
wenzelm@52815
   180
wenzelm@50198
   181
  /* font size */
wenzelm@50198
   182
wenzelm@53272
   183
  def reset_font_size(view: View): Unit =
wenzelm@53272
   184
    Rendering.font_size_change(view, _ => PIDE.options.int("jedit_reset_font_size"))
wenzelm@50198
   185
wenzelm@53272
   186
  def increase_font_size(view: View): Unit =
wenzelm@53272
   187
    Rendering.font_size_change(view, i => i + ((i / 10) max 1))
wenzelm@53272
   188
wenzelm@53272
   189
  def decrease_font_size(view: View): Unit =
wenzelm@53272
   190
    Rendering.font_size_change(view, i => i - ((i / 10) max 1))
wenzelm@50198
   191
wenzelm@50198
   192
wenzelm@53497
   193
  /* structured edits */
wenzelm@50341
   194
wenzelm@50341
   195
  def insert_line_padding(text_area: JEditTextArea, text: String)
wenzelm@50341
   196
  {
wenzelm@50341
   197
    val buffer = text_area.getBuffer
wenzelm@50341
   198
    JEdit_Lib.buffer_edit(buffer) {
wenzelm@50341
   199
      val text1 =
wenzelm@50341
   200
        if (text_area.getSelectionCount == 0) {
wenzelm@50341
   201
          def pad(range: Text.Range): String =
wenzelm@50341
   202
            if (JEdit_Lib.try_get_text(buffer, range) == Some("\n")) "" else "\n"
wenzelm@50341
   203
wenzelm@50341
   204
          val caret = JEdit_Lib.point_range(buffer, text_area.getCaretPosition)
wenzelm@50341
   205
          val before_caret = JEdit_Lib.point_range(buffer, caret.start - 1)
wenzelm@50341
   206
          pad(before_caret) + text + pad(caret)
wenzelm@50341
   207
        }
wenzelm@50341
   208
        else text
wenzelm@50341
   209
      text_area.setSelectedText(text1)
wenzelm@50341
   210
    }
wenzelm@50341
   211
  }
wenzelm@50341
   212
wenzelm@53497
   213
  def edit_command(
wenzelm@53497
   214
    snapshot: Document.Snapshot,
wenzelm@53497
   215
    buffer: Buffer,
wenzelm@53497
   216
    padding: Boolean,
wenzelm@54640
   217
    id: Document_ID.Generic,
wenzelm@53497
   218
    s: String)
wenzelm@53497
   219
  {
wenzelm@54640
   220
    if (!snapshot.is_outdated) {
wenzelm@54640
   221
      snapshot.state.find_command(snapshot.version, id) match {
wenzelm@54640
   222
        case Some((node, command)) =>
wenzelm@54640
   223
          node.command_start(command) match {
wenzelm@54640
   224
            case Some(start) =>
wenzelm@54640
   225
              JEdit_Lib.buffer_edit(buffer) {
wenzelm@54640
   226
                val range = command.proper_range + start
wenzelm@54640
   227
                if (padding) {
wenzelm@54640
   228
                  buffer.insert(start + range.length, "\n" + s)
wenzelm@54640
   229
                }
wenzelm@54640
   230
                else {
wenzelm@54640
   231
                  buffer.remove(start, range.length)
wenzelm@54640
   232
                  buffer.insert(start, s)
wenzelm@54640
   233
                }
wenzelm@53497
   234
              }
wenzelm@54640
   235
            case None =>
wenzelm@54640
   236
          }
wenzelm@54640
   237
        case None =>
wenzelm@54640
   238
      }
wenzelm@53497
   239
    }
wenzelm@53497
   240
  }
wenzelm@53497
   241
wenzelm@50341
   242
wenzelm@53293
   243
  /* completion */
wenzelm@53293
   244
wenzelm@53293
   245
  def complete(view: View)
wenzelm@53293
   246
  {
wenzelm@53293
   247
    Completion_Popup.Text_Area(view.getTextArea) match {
wenzelm@53322
   248
      case Some(text_area_completion) =>
wenzelm@53322
   249
        text_area_completion.action(immediate = true, explicit = true)
wenzelm@53293
   250
      case None => CompleteWord.completeWord(view)
wenzelm@53293
   251
    }
wenzelm@53293
   252
  }
wenzelm@53293
   253
wenzelm@53293
   254
wenzelm@50183
   255
  /* control styles */
wenzelm@50183
   256
wenzelm@50183
   257
  def control_sub(text_area: JEditTextArea)
wenzelm@50187
   258
  { Token_Markup.edit_control_style(text_area, Symbol.sub_decoded) }
wenzelm@50183
   259
wenzelm@50183
   260
  def control_sup(text_area: JEditTextArea)
wenzelm@50187
   261
  { Token_Markup.edit_control_style(text_area, Symbol.sup_decoded) }
wenzelm@50183
   262
wenzelm@50183
   263
  def control_bold(text_area: JEditTextArea)
wenzelm@50187
   264
  { Token_Markup.edit_control_style(text_area, Symbol.bold_decoded) }
wenzelm@50183
   265
wenzelm@50183
   266
  def control_reset(text_area: JEditTextArea)
wenzelm@50187
   267
  { Token_Markup.edit_control_style(text_area, "") }
wenzelm@50183
   268
wenzelm@50183
   269
wenzelm@50183
   270
  /* block styles */
wenzelm@50183
   271
wenzelm@50183
   272
  private def enclose_input(text_area: JEditTextArea, s1: String, s2: String)
wenzelm@50183
   273
  {
wenzelm@50183
   274
    s1.foreach(text_area.userInput(_))
wenzelm@50183
   275
    s2.foreach(text_area.userInput(_))
wenzelm@50183
   276
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@50183
   277
  }
wenzelm@50183
   278
wenzelm@50183
   279
  def input_bsub(text_area: JEditTextArea)
wenzelm@50183
   280
  { enclose_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded) }
wenzelm@50183
   281
wenzelm@50183
   282
  def input_bsup(text_area: JEditTextArea)
wenzelm@50183
   283
  { enclose_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded) }
wenzelm@50183
   284
}
wenzelm@50183
   285