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