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