src/Tools/jEdit/src/isabelle.scala
author wenzelm
Tue Dec 04 11:06:51 2012 +0100 (2012-12-04)
changeset 50341 0c65a7cfc0f3
parent 50299 f70b3712040f
child 50433 9131dadb2bf7
permissions -rw-r--r--
provide general insert_line_padding as convenience operation, e.g. for BeanShell macros;
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@50208
    19
  /* dockable windows */
wenzelm@50208
    20
wenzelm@50208
    21
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@50208
    22
wenzelm@50299
    23
  def docked_theories(view: View): Option[Theories_Dockable] =
wenzelm@50299
    24
    wm(view).getDockableWindow("isabelle-theories") match {
wenzelm@50299
    25
      case dockable: Theories_Dockable => Some(dockable)
wenzelm@50208
    26
      case _ => None
wenzelm@50208
    27
    }
wenzelm@50208
    28
wenzelm@50208
    29
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@50208
    30
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@50208
    31
      case dockable: Output_Dockable => Some(dockable)
wenzelm@50208
    32
      case _ => None
wenzelm@50208
    33
    }
wenzelm@50208
    34
wenzelm@50208
    35
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@50208
    36
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@50208
    37
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@50208
    38
      case _ => None
wenzelm@50208
    39
    }
wenzelm@50208
    40
wenzelm@50208
    41
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@50208
    42
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@50208
    43
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@50208
    44
      case _ => None
wenzelm@50208
    45
    }
wenzelm@50208
    46
wenzelm@50208
    47
wenzelm@50198
    48
  /* font size */
wenzelm@50198
    49
wenzelm@50198
    50
  def change_font_size(view: View, change: Int => Int)
wenzelm@50198
    51
  {
wenzelm@50206
    52
    val size = change(jEdit.getIntegerProperty("view.fontsize", 16)) max 5
wenzelm@50206
    53
    jEdit.setIntegerProperty("view.fontsize", size)
wenzelm@50198
    54
    jEdit.propertiesChanged()
wenzelm@50198
    55
    jEdit.saveSettings()
wenzelm@50198
    56
    view.getStatus.setMessageAndClear("Text font size: " + size)
wenzelm@50198
    57
  }
wenzelm@50198
    58
wenzelm@50198
    59
  def increase_font_size(view: View): Unit = change_font_size(view, i => i + ((i / 10) max 1))
wenzelm@50198
    60
  def decrease_font_size(view: View): Unit = change_font_size(view, i => i - ((i / 10) max 1))
wenzelm@50198
    61
wenzelm@50198
    62
wenzelm@50341
    63
  /* structured insert */
wenzelm@50341
    64
wenzelm@50341
    65
  def insert_line_padding(text_area: JEditTextArea, text: String)
wenzelm@50341
    66
  {
wenzelm@50341
    67
    val buffer = text_area.getBuffer
wenzelm@50341
    68
    JEdit_Lib.buffer_edit(buffer) {
wenzelm@50341
    69
      val text1 =
wenzelm@50341
    70
        if (text_area.getSelectionCount == 0) {
wenzelm@50341
    71
          def pad(range: Text.Range): String =
wenzelm@50341
    72
            if (JEdit_Lib.try_get_text(buffer, range) == Some("\n")) "" else "\n"
wenzelm@50341
    73
wenzelm@50341
    74
          val caret = JEdit_Lib.point_range(buffer, text_area.getCaretPosition)
wenzelm@50341
    75
          val before_caret = JEdit_Lib.point_range(buffer, caret.start - 1)
wenzelm@50341
    76
          pad(before_caret) + text + pad(caret)
wenzelm@50341
    77
        }
wenzelm@50341
    78
        else text
wenzelm@50341
    79
      text_area.setSelectedText(text1)
wenzelm@50341
    80
    }
wenzelm@50341
    81
  }
wenzelm@50341
    82
wenzelm@50341
    83
wenzelm@50183
    84
  /* control styles */
wenzelm@50183
    85
wenzelm@50183
    86
  def control_sub(text_area: JEditTextArea)
wenzelm@50187
    87
  { Token_Markup.edit_control_style(text_area, Symbol.sub_decoded) }
wenzelm@50183
    88
wenzelm@50183
    89
  def control_sup(text_area: JEditTextArea)
wenzelm@50187
    90
  { Token_Markup.edit_control_style(text_area, Symbol.sup_decoded) }
wenzelm@50183
    91
wenzelm@50183
    92
  def control_isub(text_area: JEditTextArea)
wenzelm@50187
    93
  { Token_Markup.edit_control_style(text_area, Symbol.isub_decoded) }
wenzelm@50183
    94
wenzelm@50183
    95
  def control_isup(text_area: JEditTextArea)
wenzelm@50187
    96
  { Token_Markup.edit_control_style(text_area, Symbol.isup_decoded) }
wenzelm@50183
    97
wenzelm@50183
    98
  def control_bold(text_area: JEditTextArea)
wenzelm@50187
    99
  { Token_Markup.edit_control_style(text_area, Symbol.bold_decoded) }
wenzelm@50183
   100
wenzelm@50183
   101
  def control_reset(text_area: JEditTextArea)
wenzelm@50187
   102
  { Token_Markup.edit_control_style(text_area, "") }
wenzelm@50183
   103
wenzelm@50183
   104
wenzelm@50183
   105
  /* block styles */
wenzelm@50183
   106
wenzelm@50183
   107
  private def enclose_input(text_area: JEditTextArea, s1: String, s2: String)
wenzelm@50183
   108
  {
wenzelm@50183
   109
    s1.foreach(text_area.userInput(_))
wenzelm@50183
   110
    s2.foreach(text_area.userInput(_))
wenzelm@50183
   111
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@50183
   112
  }
wenzelm@50183
   113
wenzelm@50183
   114
  def input_bsub(text_area: JEditTextArea)
wenzelm@50183
   115
  { enclose_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded) }
wenzelm@50183
   116
wenzelm@50183
   117
  def input_bsup(text_area: JEditTextArea)
wenzelm@50183
   118
  { enclose_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded) }
wenzelm@50183
   119
}
wenzelm@50183
   120