src/Tools/jEdit/src/jedit_bibtex.scala
author wenzelm
Fri, 19 Aug 2022 16:46:00 +0200
changeset 75906 2167b9e3157a
parent 75435 c8087e6bd2ce
child 77031 02738f4333ee
permissions -rw-r--r--
clarified signature: support for adhoc file types;
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
75435
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    29
  def context_menu(text_area: JEditTextArea): List[JMenuItem] = {
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    30
    text_area.getBuffer match {
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    31
      case buffer: Buffer
75906
2167b9e3157a clarified signature: support for adhoc file types;
wenzelm
parents: 75435
diff changeset
    32
      if File.is_bib(JEdit_Lib.buffer_name(buffer)) && buffer.isEditable =>
75435
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    33
        val menu = new JMenu("BibTeX entries")
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    34
        for (entry <- Bibtex.known_entries) {
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    35
          val item = new JMenuItem(entry.kind)
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    36
          item.addActionListener(new ActionListener {
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    37
            def actionPerformed(evt: ActionEvent): Unit =
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    38
              Isabelle.insert_line_padding(text_area, entry.template)
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    39
          })
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    40
          menu.add(item)
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    41
        }
75435
c8087e6bd2ce tuned --- avoid warnings in scala3;
wenzelm
parents: 75393
diff changeset
    42
        List(menu)
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    43
      case _ => Nil
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    44
    }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    45
  }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    46
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    47
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    48
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    49
  /** token markup **/
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    50
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    51
  /* token style */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    52
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    53
  private def token_style(context: String, token: Bibtex.Token): Byte =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    54
    token.kind match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    55
      case Bibtex.Token.Kind.COMMAND => JEditToken.KEYWORD2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    56
      case Bibtex.Token.Kind.ENTRY => JEditToken.KEYWORD1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    57
      case Bibtex.Token.Kind.KEYWORD => JEditToken.OPERATOR
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    58
      case Bibtex.Token.Kind.NAT => JEditToken.LITERAL2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    59
      case Bibtex.Token.Kind.STRING => JEditToken.LITERAL1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    60
      case Bibtex.Token.Kind.NAME => JEditToken.LABEL
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    61
      case Bibtex.Token.Kind.IDENT =>
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    62
        if (Bibtex.is_month(token.source)) JEditToken.LITERAL3
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    63
        else
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    64
          Bibtex.get_entry(context) match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    65
            case Some(entry) if entry.is_required(token.source) => JEditToken.KEYWORD3
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    66
            case Some(entry) if entry.is_optional(token.source) => JEditToken.KEYWORD4
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    67
            case _ => JEditToken.DIGIT
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    68
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    69
      case Bibtex.Token.Kind.SPACE => JEditToken.NULL
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    70
      case Bibtex.Token.Kind.COMMENT => JEditToken.COMMENT1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    71
      case Bibtex.Token.Kind.ERROR => JEditToken.INVALID
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    72
    }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    73
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    74
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    75
  /* line context */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    76
73617
20d0abffee99 more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents: 67290
diff changeset
    77
  private val mode_rule_set = Token_Markup.mode_rule_set("bibtex")
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    78
58699
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    79
  private class Line_Context(val context: Option[Bibtex.Line_Context])
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    80
  extends TokenMarker.LineContext(mode_rule_set, null) {
58699
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    81
    override def hashCode: Int = context.hashCode
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    82
    override def equals(that: Any): Boolean =
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    83
      that match {
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    84
        case other: Line_Context => context == other.context
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    85
        case _ => false
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    86
      }
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
    87
  }
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    88
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    89
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    90
  /* token marker */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    91
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    92
  class Token_Marker extends TokenMarker {
73617
20d0abffee99 more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents: 67290
diff changeset
    93
    addRuleSet(mode_rule_set)
20d0abffee99 more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents: 67290
diff changeset
    94
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    95
    override def markTokens(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    96
      context: TokenMarker.LineContext,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    97
      handler: TokenHandler,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    98
      raw_line: Segment
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    99
    ): TokenMarker.LineContext = {
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   100
      val line_ctxt =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   101
        context match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   102
          case c: Line_Context => c.context
58589
d9350ec0937e tuned signature;
wenzelm
parents: 58548
diff changeset
   103
          case _ => Some(Bibtex.Ignored)
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   104
        }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   105
      val line = if (raw_line == null) new Segment else raw_line
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   106
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   107
      def no_markup = {
58595
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   108
        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
   109
        (List(styled_token), new Line_Context(None))
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   110
      }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   111
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   112
      val context1 = {
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   113
        val (styled_tokens, context1) =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   114
          line_ctxt match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   115
            case Some(ctxt) =>
58595
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   116
              try {
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   117
                val (chunks, ctxt1) = Bibtex.parse_line(line, ctxt)
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   118
                val styled_tokens =
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   119
                  for { chunk <- chunks; tok <- chunk.tokens }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   120
                  yield (token_style(chunk.kind, tok), tok.source)
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   121
                (styled_tokens, new Line_Context(Some(ctxt1)))
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   122
              }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   123
              catch { case ERROR(msg) => Output.warning(msg); no_markup }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   124
            case None => no_markup
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   125
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   126
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   127
        var offset = 0
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   128
        for ((style, token) <- styled_tokens) {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   129
          val length = token.length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   130
          val end_offset = offset + length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   131
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   132
          if ((offset until end_offset).exists(i => line.charAt(i) == '\t')) {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   133
            for (i <- offset until end_offset)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   134
              handler.handleToken(line, style, i, 1, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   135
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   136
          else handler.handleToken(line, style, offset, length, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   137
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   138
          offset += length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   139
        }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   140
        handler.handleToken(line, JEditToken.END, line.count, 0, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   141
        context1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   142
      }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   143
      val context2 = context1.intern
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   144
      handler.setLineContext(context2)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   145
      context2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   146
    }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   147
  }
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
   148
}