src/Tools/jEdit/src/jedit_bibtex.scala
author wenzelm
Fri, 01 Apr 2022 17:06:10 +0200
changeset 75393 87ebf5a50283
parent 73987 fc363a3b690a
child 75435 c8087e6bd2ce
permissions -rw-r--r--
clarified formatting, for the sake of scala3;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66120
e03ff7e831cc clarified modules;
wenzelm
parents: 66118
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_bibtex.scala
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     3
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     4
BibTeX support in Isabelle/jEdit.
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     5
*/
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     6
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     8
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     9
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    10
import isabelle._
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    11
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    12
58547
6080615b8b96 clarified modules;
wenzelm
parents: 58546
diff changeset
    13
import scala.collection.mutable
6080615b8b96 clarified modules;
wenzelm
parents: 58546
diff changeset
    14
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    15
import java.awt.event.{ActionListener, ActionEvent}
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    16
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    17
import javax.swing.text.Segment
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    18
import javax.swing.tree.DefaultMutableTreeNode
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    19
import javax.swing.{JMenu, JMenuItem}
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    20
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    21
import org.gjt.sp.jedit.Buffer
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    22
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
73617
20d0abffee99 more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents: 67290
diff changeset
    23
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler}
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    24
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    25
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    26
object JEdit_Bibtex {
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    27
  /** context menu **/
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    28
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    29
  def context_menu(text_area0: JEditTextArea): List[JMenuItem] = {
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    30
    text_area0 match {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    31
      case text_area: TextArea =>
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    32
        text_area.getBuffer match {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    33
          case buffer: Buffer
67290
98b6cd12f963 implicit thy_load context for bibtex files;
wenzelm
parents: 66152
diff changeset
    34
          if (Bibtex.is_bibtex(JEdit_Lib.buffer_name(buffer)) && buffer.isEditable) =>
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    35
            val menu = new JMenu("BibTeX entries")
66150
c2e19b9e1398 tuned signature;
wenzelm
parents: 66120
diff changeset
    36
            for (entry <- Bibtex.known_entries) {
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    37
              val item = new JMenuItem(entry.kind)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    38
              item.addActionListener(new ActionListener {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    39
                def actionPerformed(evt: ActionEvent): Unit =
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    40
                  Isabelle.insert_line_padding(text_area, entry.template)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    41
              })
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    42
              menu.add(item)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    43
            }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    44
            List(menu)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    45
          case _ => Nil
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    46
        }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    47
      case _ => Nil
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    48
    }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    49
  }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    50
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    51
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    52
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    53
  /** token markup **/
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    54
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    55
  /* token style */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    56
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    57
  private def token_style(context: String, token: Bibtex.Token): Byte =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    58
    token.kind match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    59
      case Bibtex.Token.Kind.COMMAND => JEditToken.KEYWORD2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    60
      case Bibtex.Token.Kind.ENTRY => JEditToken.KEYWORD1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    61
      case Bibtex.Token.Kind.KEYWORD => JEditToken.OPERATOR
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    62
      case Bibtex.Token.Kind.NAT => JEditToken.LITERAL2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    63
      case Bibtex.Token.Kind.STRING => JEditToken.LITERAL1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    64
      case Bibtex.Token.Kind.NAME => JEditToken.LABEL
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    65
      case Bibtex.Token.Kind.IDENT =>
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    66
        if (Bibtex.is_month(token.source)) JEditToken.LITERAL3
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    67
        else
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    68
          Bibtex.get_entry(context) match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    69
            case Some(entry) if entry.is_required(token.source) => JEditToken.KEYWORD3
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    70
            case Some(entry) if entry.is_optional(token.source) => JEditToken.KEYWORD4
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    71
            case _ => JEditToken.DIGIT
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    72
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    73
      case Bibtex.Token.Kind.SPACE => JEditToken.NULL
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    74
      case Bibtex.Token.Kind.COMMENT => JEditToken.COMMENT1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    75
      case Bibtex.Token.Kind.ERROR => JEditToken.INVALID
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    76
    }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    77
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    78
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    79
  /* line context */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    80
73617
20d0abffee99 more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents: 67290
diff changeset
    81
  private val mode_rule_set = Token_Markup.mode_rule_set("bibtex")
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    82
58699
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    83
  private class Line_Context(val context: Option[Bibtex.Line_Context])
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    84
  extends TokenMarker.LineContext(mode_rule_set, null) {
58699
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    85
    override def hashCode: Int = context.hashCode
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    86
    override def equals(that: Any): Boolean =
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    87
      that match {
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    88
        case other: Line_Context => context == other.context
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    89
        case _ => false
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    90
      }
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    91
  }
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    92
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    93
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    94
  /* token marker */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    95
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    96
  class Token_Marker extends TokenMarker {
73617
20d0abffee99 more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents: 67290
diff changeset
    97
    addRuleSet(mode_rule_set)
20d0abffee99 more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents: 67290
diff changeset
    98
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    99
    override def markTokens(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   100
      context: TokenMarker.LineContext,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   101
      handler: TokenHandler,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   102
      raw_line: Segment
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   103
    ): TokenMarker.LineContext = {
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   104
      val line_ctxt =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   105
        context match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   106
          case c: Line_Context => c.context
58589
d9350ec0937e tuned signature;
wenzelm
parents: 58548
diff changeset
   107
          case _ => Some(Bibtex.Ignored)
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   108
        }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   109
      val line = if (raw_line == null) new Segment else raw_line
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   110
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   111
      def no_markup = {
58595
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   112
        val styled_token = (JEditToken.NULL, line.subSequence(0, line.count).toString)
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   113
        (List(styled_token), new Line_Context(None))
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   114
      }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   115
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   116
      val context1 = {
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   117
        val (styled_tokens, context1) =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   118
          line_ctxt match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   119
            case Some(ctxt) =>
58595
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   120
              try {
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   121
                val (chunks, ctxt1) = Bibtex.parse_line(line, ctxt)
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   122
                val styled_tokens =
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   123
                  for { chunk <- chunks; tok <- chunk.tokens }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   124
                  yield (token_style(chunk.kind, tok), tok.source)
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   125
                (styled_tokens, new Line_Context(Some(ctxt1)))
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   126
              }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   127
              catch { case ERROR(msg) => Output.warning(msg); no_markup }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   128
            case None => no_markup
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   129
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   130
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   131
        var offset = 0
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   132
        for ((style, token) <- styled_tokens) {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   133
          val length = token.length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   134
          val end_offset = offset + length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   135
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   136
          if ((offset until end_offset).exists(i => line.charAt(i) == '\t')) {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   137
            for (i <- offset until end_offset)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   138
              handler.handleToken(line, style, i, 1, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   139
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   140
          else handler.handleToken(line, style, offset, length, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   141
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   142
          offset += length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   143
        }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   144
        handler.handleToken(line, JEditToken.END, line.count, 0, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   145
        context1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   146
      }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   147
      val context2 = context1.intern
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   148
      handler.setLineContext(context2)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   149
      context2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   150
    }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   151
  }
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
   152
}