src/Tools/jEdit/src/isabelle.scala
author wenzelm
Thu Aug 29 13:14:00 2013 +0200 (2013-08-29)
changeset 53276 cbed0aa0b0db
parent 53274 1760c01f1c78
child 53277 6aa348237973
permissions -rw-r--r--
syntax for isabelle-news with symbol completion;
wenzelm@50208
     1
/*  Title:      Tools/jEdit/src/isabelle.scala
wenzelm@50183
     2
    Author:     Makarius
wenzelm@50183
     3
wenzelm@50208
     4
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@50198
    12
import org.gjt.sp.jedit.{jEdit, View, Buffer}
wenzelm@50183
    13
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@50208
    14
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@50183
    15
wenzelm@50183
    16
wenzelm@50208
    17
object Isabelle
wenzelm@50183
    18
{
wenzelm@53274
    19
  /* editor modes */
wenzelm@53274
    20
wenzelm@53274
    21
  val modes = List("isabelle", "isabelle-options", "isabelle-root", "isabelle-news")
wenzelm@53274
    22
wenzelm@53276
    23
  private lazy val news_syntax = Outer_Syntax.init()
wenzelm@53276
    24
wenzelm@53274
    25
  def mode_syntax(name: String): Option[Outer_Syntax] =
wenzelm@53274
    26
    name match {
wenzelm@53274
    27
      case "isabelle" | "isabelle-raw" => PIDE.get_recent_syntax
wenzelm@53274
    28
      case "isabelle-options" => Some(Options.options_syntax)
wenzelm@53274
    29
      case "isabelle-root" => Some(Build.root_syntax)
wenzelm@53276
    30
      case "isabelle-news" => Some(news_syntax)
wenzelm@53274
    31
      case _ => None
wenzelm@53274
    32
    }
wenzelm@53274
    33
wenzelm@53274
    34
wenzelm@50208
    35
  /* dockable windows */
wenzelm@50208
    36
wenzelm@50208
    37
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@50208
    38
wenzelm@50299
    39
  def docked_theories(view: View): Option[Theories_Dockable] =
wenzelm@50299
    40
    wm(view).getDockableWindow("isabelle-theories") match {
wenzelm@50299
    41
      case dockable: Theories_Dockable => Some(dockable)
wenzelm@50208
    42
      case _ => None
wenzelm@50208
    43
    }
wenzelm@50208
    44
wenzelm@51533
    45
  def docked_timing(view: View): Option[Timing_Dockable] =
wenzelm@51533
    46
    wm(view).getDockableWindow("isabelle-timing") match {
wenzelm@51533
    47
      case dockable: Timing_Dockable => Some(dockable)
wenzelm@51533
    48
      case _ => None
wenzelm@51533
    49
    }
wenzelm@51533
    50
wenzelm@50208
    51
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@50208
    52
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@50208
    53
      case dockable: Output_Dockable => Some(dockable)
wenzelm@50208
    54
      case _ => None
wenzelm@50208
    55
    }
wenzelm@50208
    56
wenzelm@50208
    57
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@50208
    58
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@50208
    59
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@50208
    60
      case _ => None
wenzelm@50208
    61
    }
wenzelm@50208
    62
wenzelm@50208
    63
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@50208
    64
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@50208
    65
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@50208
    66
      case _ => None
wenzelm@50208
    67
    }
wenzelm@50208
    68
wenzelm@50433
    69
  def docked_monitor(view: View): Option[Monitor_Dockable] =
wenzelm@50433
    70
    wm(view).getDockableWindow("isabelle-monitor") match {
wenzelm@50433
    71
      case dockable: Monitor_Dockable => Some(dockable)
wenzelm@50433
    72
      case _ => None
wenzelm@50433
    73
    }
wenzelm@50433
    74
wenzelm@50208
    75
wenzelm@52815
    76
  /* continuous checking */
wenzelm@52815
    77
wenzelm@52815
    78
  private val CONTINUOUS_CHECKING = "editor_continuous_checking"
wenzelm@52815
    79
wenzelm@52815
    80
  def continuous_checking: Boolean = PIDE.options.bool(CONTINUOUS_CHECKING)
wenzelm@52815
    81
wenzelm@52815
    82
  def continuous_checking_=(b: Boolean)
wenzelm@52815
    83
  {
wenzelm@52815
    84
    Swing_Thread.require()
wenzelm@52815
    85
wenzelm@52815
    86
    if (continuous_checking != b) {
wenzelm@52815
    87
      PIDE.options.bool(CONTINUOUS_CHECKING) = b
wenzelm@52815
    88
      PIDE.options_changed()
wenzelm@52974
    89
      PIDE.editor.flush()
wenzelm@52815
    90
    }
wenzelm@52815
    91
  }
wenzelm@52815
    92
wenzelm@52815
    93
  def set_continuous_checking() { continuous_checking = true }
wenzelm@52815
    94
  def reset_continuous_checking() { continuous_checking = false }
wenzelm@52815
    95
  def toggle_continuous_checking() { continuous_checking = !continuous_checking }
wenzelm@52815
    96
wenzelm@52815
    97
wenzelm@52815
    98
  /* required document nodes */
wenzelm@52815
    99
wenzelm@52815
   100
  private def node_required_update(view: View, toggle: Boolean = false, set: Boolean = false)
wenzelm@52815
   101
  {
wenzelm@52815
   102
    Swing_Thread.require()
wenzelm@52815
   103
    PIDE.document_model(view.getBuffer) match {
wenzelm@52815
   104
      case Some(model) =>
wenzelm@52816
   105
        model.node_required = (if (toggle) !model.node_required else set)
wenzelm@52815
   106
      case None =>
wenzelm@52815
   107
    }
wenzelm@52815
   108
  }
wenzelm@52815
   109
wenzelm@52815
   110
  def set_node_required(view: View) { node_required_update(view, set = true) }
wenzelm@52815
   111
  def reset_node_required(view: View) { node_required_update(view, set = false) }
wenzelm@52815
   112
  def toggle_node_required(view: View) { node_required_update(view, toggle = true) }
wenzelm@52815
   113
wenzelm@52815
   114
wenzelm@50198
   115
  /* font size */
wenzelm@50198
   116
wenzelm@53272
   117
  def reset_font_size(view: View): Unit =
wenzelm@53272
   118
    Rendering.font_size_change(view, _ => PIDE.options.int("jedit_reset_font_size"))
wenzelm@50198
   119
wenzelm@53272
   120
  def increase_font_size(view: View): Unit =
wenzelm@53272
   121
    Rendering.font_size_change(view, i => i + ((i / 10) max 1))
wenzelm@53272
   122
wenzelm@53272
   123
  def decrease_font_size(view: View): Unit =
wenzelm@53272
   124
    Rendering.font_size_change(view, i => i - ((i / 10) max 1))
wenzelm@50198
   125
wenzelm@50198
   126
wenzelm@50341
   127
  /* structured insert */
wenzelm@50341
   128
wenzelm@50341
   129
  def insert_line_padding(text_area: JEditTextArea, text: String)
wenzelm@50341
   130
  {
wenzelm@50341
   131
    val buffer = text_area.getBuffer
wenzelm@50341
   132
    JEdit_Lib.buffer_edit(buffer) {
wenzelm@50341
   133
      val text1 =
wenzelm@50341
   134
        if (text_area.getSelectionCount == 0) {
wenzelm@50341
   135
          def pad(range: Text.Range): String =
wenzelm@50341
   136
            if (JEdit_Lib.try_get_text(buffer, range) == Some("\n")) "" else "\n"
wenzelm@50341
   137
wenzelm@50341
   138
          val caret = JEdit_Lib.point_range(buffer, text_area.getCaretPosition)
wenzelm@50341
   139
          val before_caret = JEdit_Lib.point_range(buffer, caret.start - 1)
wenzelm@50341
   140
          pad(before_caret) + text + pad(caret)
wenzelm@50341
   141
        }
wenzelm@50341
   142
        else text
wenzelm@50341
   143
      text_area.setSelectedText(text1)
wenzelm@50341
   144
    }
wenzelm@50341
   145
  }
wenzelm@50341
   146
wenzelm@50341
   147
wenzelm@50183
   148
  /* control styles */
wenzelm@50183
   149
wenzelm@50183
   150
  def control_sub(text_area: JEditTextArea)
wenzelm@50187
   151
  { Token_Markup.edit_control_style(text_area, Symbol.sub_decoded) }
wenzelm@50183
   152
wenzelm@50183
   153
  def control_sup(text_area: JEditTextArea)
wenzelm@50187
   154
  { Token_Markup.edit_control_style(text_area, Symbol.sup_decoded) }
wenzelm@50183
   155
wenzelm@50183
   156
  def control_bold(text_area: JEditTextArea)
wenzelm@50187
   157
  { Token_Markup.edit_control_style(text_area, Symbol.bold_decoded) }
wenzelm@50183
   158
wenzelm@50183
   159
  def control_reset(text_area: JEditTextArea)
wenzelm@50187
   160
  { Token_Markup.edit_control_style(text_area, "") }
wenzelm@50183
   161
wenzelm@50183
   162
wenzelm@50183
   163
  /* block styles */
wenzelm@50183
   164
wenzelm@50183
   165
  private def enclose_input(text_area: JEditTextArea, s1: String, s2: String)
wenzelm@50183
   166
  {
wenzelm@50183
   167
    s1.foreach(text_area.userInput(_))
wenzelm@50183
   168
    s2.foreach(text_area.userInput(_))
wenzelm@50183
   169
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@50183
   170
  }
wenzelm@50183
   171
wenzelm@50183
   172
  def input_bsub(text_area: JEditTextArea)
wenzelm@50183
   173
  { enclose_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded) }
wenzelm@50183
   174
wenzelm@50183
   175
  def input_bsup(text_area: JEditTextArea)
wenzelm@50183
   176
  { enclose_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded) }
wenzelm@50183
   177
}
wenzelm@50183
   178