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