src/Tools/jEdit/src/isabelle.scala
author wenzelm
Tue Jan 08 18:24:52 2013 +0100 (2013-01-08)
changeset 50775 8c1cda8ad833
parent 50433 9131dadb2bf7
child 51533 3f6280aedbcc
permissions -rw-r--r--
upper bound for font size;
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@50433
    47
  def docked_monitor(view: View): Option[Monitor_Dockable] =
wenzelm@50433
    48
    wm(view).getDockableWindow("isabelle-monitor") match {
wenzelm@50433
    49
      case dockable: Monitor_Dockable => Some(dockable)
wenzelm@50433
    50
      case _ => None
wenzelm@50433
    51
    }
wenzelm@50433
    52
wenzelm@50208
    53
wenzelm@50198
    54
  /* font size */
wenzelm@50198
    55
wenzelm@50198
    56
  def change_font_size(view: View, change: Int => Int)
wenzelm@50198
    57
  {
wenzelm@50775
    58
    val size = change(jEdit.getIntegerProperty("view.fontsize", 16)) max 5 min 250
wenzelm@50206
    59
    jEdit.setIntegerProperty("view.fontsize", size)
wenzelm@50198
    60
    jEdit.propertiesChanged()
wenzelm@50198
    61
    jEdit.saveSettings()
wenzelm@50198
    62
    view.getStatus.setMessageAndClear("Text font size: " + size)
wenzelm@50198
    63
  }
wenzelm@50198
    64
wenzelm@50198
    65
  def increase_font_size(view: View): Unit = change_font_size(view, i => i + ((i / 10) max 1))
wenzelm@50198
    66
  def decrease_font_size(view: View): Unit = change_font_size(view, i => i - ((i / 10) max 1))
wenzelm@50198
    67
wenzelm@50198
    68
wenzelm@50341
    69
  /* structured insert */
wenzelm@50341
    70
wenzelm@50341
    71
  def insert_line_padding(text_area: JEditTextArea, text: String)
wenzelm@50341
    72
  {
wenzelm@50341
    73
    val buffer = text_area.getBuffer
wenzelm@50341
    74
    JEdit_Lib.buffer_edit(buffer) {
wenzelm@50341
    75
      val text1 =
wenzelm@50341
    76
        if (text_area.getSelectionCount == 0) {
wenzelm@50341
    77
          def pad(range: Text.Range): String =
wenzelm@50341
    78
            if (JEdit_Lib.try_get_text(buffer, range) == Some("\n")) "" else "\n"
wenzelm@50341
    79
wenzelm@50341
    80
          val caret = JEdit_Lib.point_range(buffer, text_area.getCaretPosition)
wenzelm@50341
    81
          val before_caret = JEdit_Lib.point_range(buffer, caret.start - 1)
wenzelm@50341
    82
          pad(before_caret) + text + pad(caret)
wenzelm@50341
    83
        }
wenzelm@50341
    84
        else text
wenzelm@50341
    85
      text_area.setSelectedText(text1)
wenzelm@50341
    86
    }
wenzelm@50341
    87
  }
wenzelm@50341
    88
wenzelm@50341
    89
wenzelm@50183
    90
  /* control styles */
wenzelm@50183
    91
wenzelm@50183
    92
  def control_sub(text_area: JEditTextArea)
wenzelm@50187
    93
  { Token_Markup.edit_control_style(text_area, Symbol.sub_decoded) }
wenzelm@50183
    94
wenzelm@50183
    95
  def control_sup(text_area: JEditTextArea)
wenzelm@50187
    96
  { Token_Markup.edit_control_style(text_area, Symbol.sup_decoded) }
wenzelm@50183
    97
wenzelm@50183
    98
  def control_isub(text_area: JEditTextArea)
wenzelm@50187
    99
  { Token_Markup.edit_control_style(text_area, Symbol.isub_decoded) }
wenzelm@50183
   100
wenzelm@50183
   101
  def control_isup(text_area: JEditTextArea)
wenzelm@50187
   102
  { Token_Markup.edit_control_style(text_area, Symbol.isup_decoded) }
wenzelm@50183
   103
wenzelm@50183
   104
  def control_bold(text_area: JEditTextArea)
wenzelm@50187
   105
  { Token_Markup.edit_control_style(text_area, Symbol.bold_decoded) }
wenzelm@50183
   106
wenzelm@50183
   107
  def control_reset(text_area: JEditTextArea)
wenzelm@50187
   108
  { Token_Markup.edit_control_style(text_area, "") }
wenzelm@50183
   109
wenzelm@50183
   110
wenzelm@50183
   111
  /* block styles */
wenzelm@50183
   112
wenzelm@50183
   113
  private def enclose_input(text_area: JEditTextArea, s1: String, s2: String)
wenzelm@50183
   114
  {
wenzelm@50183
   115
    s1.foreach(text_area.userInput(_))
wenzelm@50183
   116
    s2.foreach(text_area.userInput(_))
wenzelm@50183
   117
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@50183
   118
  }
wenzelm@50183
   119
wenzelm@50183
   120
  def input_bsub(text_area: JEditTextArea)
wenzelm@50183
   121
  { enclose_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded) }
wenzelm@50183
   122
wenzelm@50183
   123
  def input_bsup(text_area: JEditTextArea)
wenzelm@50183
   124
  { enclose_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded) }
wenzelm@50183
   125
}
wenzelm@50183
   126