src/Tools/jEdit/src/isabelle.scala
author wenzelm
Sat Feb 15 16:27:58 2014 +0100 (2014-02-15)
changeset 55500 cdbbaa3074a8
parent 55316 885500f4aa6a
child 55557 aa1adeca714b
permissions -rw-r--r--
isabelle-ml mode with separate token marker;
clarified ML_Lex.gap_start: end-of-input counts as single newline;
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@50299
    61
  def docked_theories(view: View): Option[Theories_Dockable] =
wenzelm@50299
    62
    wm(view).getDockableWindow("isabelle-theories") match {
wenzelm@50299
    63
      case dockable: Theories_Dockable => Some(dockable)
wenzelm@50208
    64
      case _ => None
wenzelm@50208
    65
    }
wenzelm@50208
    66
wenzelm@51533
    67
  def docked_timing(view: View): Option[Timing_Dockable] =
wenzelm@51533
    68
    wm(view).getDockableWindow("isabelle-timing") match {
wenzelm@51533
    69
      case dockable: Timing_Dockable => Some(dockable)
wenzelm@51533
    70
      case _ => None
wenzelm@51533
    71
    }
wenzelm@51533
    72
wenzelm@50208
    73
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@50208
    74
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@50208
    75
      case dockable: Output_Dockable => Some(dockable)
wenzelm@50208
    76
      case _ => None
wenzelm@50208
    77
    }
wenzelm@50208
    78
wenzelm@50208
    79
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@50208
    80
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@50208
    81
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@50208
    82
      case _ => None
wenzelm@50208
    83
    }
wenzelm@50208
    84
lars@55316
    85
  def docked_simplifier_trace(view: View): Option[Simplifier_Trace_Dockable] =
lars@55316
    86
    wm(view).getDockableWindow("isabelle-simp-trace") match {
lars@55316
    87
      case dockable: Simplifier_Trace_Dockable => Some(dockable)
lars@55316
    88
      case _ => None
lars@55316
    89
    }
lars@55316
    90
wenzelm@50208
    91
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@50208
    92
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@50208
    93
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@50208
    94
      case _ => None
wenzelm@50208
    95
    }
wenzelm@50208
    96
wenzelm@50433
    97
  def docked_monitor(view: View): Option[Monitor_Dockable] =
wenzelm@50433
    98
    wm(view).getDockableWindow("isabelle-monitor") match {
wenzelm@50433
    99
      case dockable: Monitor_Dockable => Some(dockable)
wenzelm@50433
   100
      case _ => None
wenzelm@50433
   101
    }
wenzelm@50433
   102
wenzelm@50208
   103
wenzelm@52815
   104
  /* continuous checking */
wenzelm@52815
   105
wenzelm@52815
   106
  private val CONTINUOUS_CHECKING = "editor_continuous_checking"
wenzelm@52815
   107
wenzelm@52815
   108
  def continuous_checking: Boolean = PIDE.options.bool(CONTINUOUS_CHECKING)
wenzelm@52815
   109
wenzelm@52815
   110
  def continuous_checking_=(b: Boolean)
wenzelm@52815
   111
  {
wenzelm@52815
   112
    Swing_Thread.require()
wenzelm@52815
   113
wenzelm@52815
   114
    if (continuous_checking != b) {
wenzelm@52815
   115
      PIDE.options.bool(CONTINUOUS_CHECKING) = b
wenzelm@52815
   116
      PIDE.options_changed()
wenzelm@52974
   117
      PIDE.editor.flush()
wenzelm@52815
   118
    }
wenzelm@52815
   119
  }
wenzelm@52815
   120
wenzelm@52815
   121
  def set_continuous_checking() { continuous_checking = true }
wenzelm@52815
   122
  def reset_continuous_checking() { continuous_checking = false }
wenzelm@52815
   123
  def toggle_continuous_checking() { continuous_checking = !continuous_checking }
wenzelm@52815
   124
wenzelm@53715
   125
  class Continuous_Checking extends CheckBox("Continuous checking")
wenzelm@53715
   126
  {
wenzelm@53715
   127
    tooltip = "Continuous checking of proof document (visible and required parts)"
wenzelm@53715
   128
    reactions += { case ButtonClicked(_) => continuous_checking = selected }
wenzelm@53715
   129
    def load() { selected = continuous_checking }
wenzelm@53715
   130
    load()
wenzelm@53715
   131
  }
wenzelm@53715
   132
wenzelm@52815
   133
wenzelm@52815
   134
  /* required document nodes */
wenzelm@52815
   135
wenzelm@52815
   136
  private def node_required_update(view: View, toggle: Boolean = false, set: Boolean = false)
wenzelm@52815
   137
  {
wenzelm@52815
   138
    Swing_Thread.require()
wenzelm@52815
   139
    PIDE.document_model(view.getBuffer) match {
wenzelm@52815
   140
      case Some(model) =>
wenzelm@52816
   141
        model.node_required = (if (toggle) !model.node_required else set)
wenzelm@52815
   142
      case None =>
wenzelm@52815
   143
    }
wenzelm@52815
   144
  }
wenzelm@52815
   145
wenzelm@52815
   146
  def set_node_required(view: View) { node_required_update(view, set = true) }
wenzelm@52815
   147
  def reset_node_required(view: View) { node_required_update(view, set = false) }
wenzelm@52815
   148
  def toggle_node_required(view: View) { node_required_update(view, toggle = true) }
wenzelm@52815
   149
wenzelm@52815
   150
wenzelm@50198
   151
  /* font size */
wenzelm@50198
   152
wenzelm@53272
   153
  def reset_font_size(view: View): Unit =
wenzelm@53272
   154
    Rendering.font_size_change(view, _ => PIDE.options.int("jedit_reset_font_size"))
wenzelm@50198
   155
wenzelm@53272
   156
  def increase_font_size(view: View): Unit =
wenzelm@53272
   157
    Rendering.font_size_change(view, i => i + ((i / 10) max 1))
wenzelm@53272
   158
wenzelm@53272
   159
  def decrease_font_size(view: View): Unit =
wenzelm@53272
   160
    Rendering.font_size_change(view, i => i - ((i / 10) max 1))
wenzelm@50198
   161
wenzelm@50198
   162
wenzelm@53497
   163
  /* structured edits */
wenzelm@50341
   164
wenzelm@50341
   165
  def insert_line_padding(text_area: JEditTextArea, text: String)
wenzelm@50341
   166
  {
wenzelm@50341
   167
    val buffer = text_area.getBuffer
wenzelm@50341
   168
    JEdit_Lib.buffer_edit(buffer) {
wenzelm@50341
   169
      val text1 =
wenzelm@50341
   170
        if (text_area.getSelectionCount == 0) {
wenzelm@50341
   171
          def pad(range: Text.Range): String =
wenzelm@50341
   172
            if (JEdit_Lib.try_get_text(buffer, range) == Some("\n")) "" else "\n"
wenzelm@50341
   173
wenzelm@50341
   174
          val caret = JEdit_Lib.point_range(buffer, text_area.getCaretPosition)
wenzelm@50341
   175
          val before_caret = JEdit_Lib.point_range(buffer, caret.start - 1)
wenzelm@50341
   176
          pad(before_caret) + text + pad(caret)
wenzelm@50341
   177
        }
wenzelm@50341
   178
        else text
wenzelm@50341
   179
      text_area.setSelectedText(text1)
wenzelm@50341
   180
    }
wenzelm@50341
   181
  }
wenzelm@50341
   182
wenzelm@53497
   183
  def edit_command(
wenzelm@53497
   184
    snapshot: Document.Snapshot,
wenzelm@53497
   185
    buffer: Buffer,
wenzelm@53497
   186
    padding: Boolean,
wenzelm@54640
   187
    id: Document_ID.Generic,
wenzelm@53497
   188
    s: String)
wenzelm@53497
   189
  {
wenzelm@54640
   190
    if (!snapshot.is_outdated) {
wenzelm@54640
   191
      snapshot.state.find_command(snapshot.version, id) match {
wenzelm@54640
   192
        case Some((node, command)) =>
wenzelm@54640
   193
          node.command_start(command) match {
wenzelm@54640
   194
            case Some(start) =>
wenzelm@54640
   195
              JEdit_Lib.buffer_edit(buffer) {
wenzelm@54640
   196
                val range = command.proper_range + start
wenzelm@54640
   197
                if (padding) {
wenzelm@54640
   198
                  buffer.insert(start + range.length, "\n" + s)
wenzelm@54640
   199
                }
wenzelm@54640
   200
                else {
wenzelm@54640
   201
                  buffer.remove(start, range.length)
wenzelm@54640
   202
                  buffer.insert(start, s)
wenzelm@54640
   203
                }
wenzelm@53497
   204
              }
wenzelm@54640
   205
            case None =>
wenzelm@54640
   206
          }
wenzelm@54640
   207
        case None =>
wenzelm@54640
   208
      }
wenzelm@53497
   209
    }
wenzelm@53497
   210
  }
wenzelm@53497
   211
wenzelm@50341
   212
wenzelm@53293
   213
  /* completion */
wenzelm@53293
   214
wenzelm@53293
   215
  def complete(view: View)
wenzelm@53293
   216
  {
wenzelm@53293
   217
    Completion_Popup.Text_Area(view.getTextArea) match {
wenzelm@53322
   218
      case Some(text_area_completion) =>
wenzelm@53322
   219
        text_area_completion.action(immediate = true, explicit = true)
wenzelm@53293
   220
      case None => CompleteWord.completeWord(view)
wenzelm@53293
   221
    }
wenzelm@53293
   222
  }
wenzelm@53293
   223
wenzelm@53293
   224
wenzelm@50183
   225
  /* control styles */
wenzelm@50183
   226
wenzelm@50183
   227
  def control_sub(text_area: JEditTextArea)
wenzelm@50187
   228
  { Token_Markup.edit_control_style(text_area, Symbol.sub_decoded) }
wenzelm@50183
   229
wenzelm@50183
   230
  def control_sup(text_area: JEditTextArea)
wenzelm@50187
   231
  { Token_Markup.edit_control_style(text_area, Symbol.sup_decoded) }
wenzelm@50183
   232
wenzelm@50183
   233
  def control_bold(text_area: JEditTextArea)
wenzelm@50187
   234
  { Token_Markup.edit_control_style(text_area, Symbol.bold_decoded) }
wenzelm@50183
   235
wenzelm@50183
   236
  def control_reset(text_area: JEditTextArea)
wenzelm@50187
   237
  { Token_Markup.edit_control_style(text_area, "") }
wenzelm@50183
   238
wenzelm@50183
   239
wenzelm@50183
   240
  /* block styles */
wenzelm@50183
   241
wenzelm@50183
   242
  private def enclose_input(text_area: JEditTextArea, s1: String, s2: String)
wenzelm@50183
   243
  {
wenzelm@50183
   244
    s1.foreach(text_area.userInput(_))
wenzelm@50183
   245
    s2.foreach(text_area.userInput(_))
wenzelm@50183
   246
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@50183
   247
  }
wenzelm@50183
   248
wenzelm@50183
   249
  def input_bsub(text_area: JEditTextArea)
wenzelm@50183
   250
  { enclose_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded) }
wenzelm@50183
   251
wenzelm@50183
   252
  def input_bsup(text_area: JEditTextArea)
wenzelm@50183
   253
  { enclose_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded) }
wenzelm@50183
   254
}
wenzelm@50183
   255