src/Tools/jEdit/src/bibtex_jedit.scala
author wenzelm
Mon, 19 Jun 2017 20:32:06 +0200
changeset 66117 e6f808d1307c
parent 66115 135bf45026ea
child 66118 03dd799fe042
permissions -rw-r--r--
tuned signature; clarified modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/bibtex_jedit.scala
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}
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    23
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler, ParserRuleSet}
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    24
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    25
import sidekick.{SideKickParser, SideKickParsedData}
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    26
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    27
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    28
object Bibtex_JEdit
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
    29
{
64829
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64828
diff changeset
    30
  /** completion **/
58592
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    31
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    32
  def complete(name: String): List[String] =
64829
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64828
diff changeset
    33
    (for {
64831
4792ee012e94 tuned signature;
wenzelm
parents: 64829
diff changeset
    34
      Text.Info(_, (entry, _)) <- Document_Model.bibtex_entries_iterator()
64829
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64828
diff changeset
    35
      if entry.toLowerCase.containsSlice(name.toLowerCase)
07f209e957bc refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents: 64828
diff changeset
    36
    } yield entry).toList
58592
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    37
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    38
  def completion(
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    39
    history: Completion.History,
66117
e6f808d1307c tuned signature;
wenzelm
parents: 66115
diff changeset
    40
    rendering: JEdit_Rendering,
e6f808d1307c tuned signature;
wenzelm
parents: 66115
diff changeset
    41
    caret: Text.Offset): Option[Completion.Result] =
58592
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    42
  {
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    43
    for {
66117
e6f808d1307c tuned signature;
wenzelm
parents: 66115
diff changeset
    44
      Text.Info(r, name) <- rendering.citation(rendering.before_caret_range(caret))
61100
4d9efd5004c8 clean name as in ML Completion.make;
wenzelm
parents: 61099
diff changeset
    45
      name1 <- Completion.clean_name(name)
4d9efd5004c8 clean name as in ML Completion.make;
wenzelm
parents: 61099
diff changeset
    46
66115
wenzelm
parents: 64831
diff changeset
    47
      original <- rendering.model.try_get_text(r)
61100
4d9efd5004c8 clean name as in ML Completion.make;
wenzelm
parents: 61099
diff changeset
    48
      original1 <- Completion.clean_name(Library.perhaps_unquote(original))
4d9efd5004c8 clean name as in ML Completion.make;
wenzelm
parents: 61099
diff changeset
    49
4d9efd5004c8 clean name as in ML Completion.make;
wenzelm
parents: 61099
diff changeset
    50
      entries = complete(name1).filter(_ != original1)
59319
wenzelm
parents: 59062
diff changeset
    51
      if entries.nonEmpty
61100
4d9efd5004c8 clean name as in ML Completion.make;
wenzelm
parents: 61099
diff changeset
    52
58592
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    53
      items =
61099
64dcd8609962 use alphabetic order before history order;
wenzelm
parents: 60215
diff changeset
    54
        entries.sorted.map({
58592
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    55
          case entry =>
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    56
            val full_name = Long_Name.qualify(Markup.CITATION, entry)
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    57
            val description = List(entry, "(BibTeX entry)")
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    58
            val replacement = quote(entry)
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    59
          Completion.Item(r, original, full_name, description, replacement, 0, false)
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    60
        }).sorted(history.ordering).take(PIDE.options.int("completion_limit"))
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    61
    } yield Completion.Result(r, original, false, items)
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    62
  }
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    63
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 58589
diff changeset
    64
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    65
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    66
  /** context menu **/
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    67
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    68
  def context_menu(text_area0: JEditTextArea): List[JMenuItem] =
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    69
  {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    70
    text_area0 match {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    71
      case text_area: TextArea =>
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    72
        text_area.getBuffer match {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    73
          case buffer: Buffer
64828
e837f6bf653c clarified modules;
wenzelm
parents: 64817
diff changeset
    74
          if (Bibtex.check_name(JEdit_Lib.buffer_name(buffer)) && buffer.isEditable) =>
58548
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    75
            val menu = new JMenu("BibTeX entries")
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    76
            for (entry <- Bibtex.entries) {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    77
              val item = new JMenuItem(entry.kind)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    78
              item.addActionListener(new ActionListener {
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    79
                def actionPerformed(evt: ActionEvent): Unit =
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    80
                  Isabelle.insert_line_padding(text_area, entry.template)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    81
              })
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    82
              menu.add(item)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    83
            }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    84
            List(menu)
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    85
          case _ => Nil
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    86
        }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    87
      case _ => Nil
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    88
    }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    89
  }
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    90
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    91
d0ee64efd624 clarified modules;
wenzelm
parents: 58547
diff changeset
    92
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    93
  /** token markup **/
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    94
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    95
  /* token style */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    96
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    97
  private def token_style(context: String, token: Bibtex.Token): Byte =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    98
    token.kind match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
    99
      case Bibtex.Token.Kind.COMMAND => JEditToken.KEYWORD2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   100
      case Bibtex.Token.Kind.ENTRY => JEditToken.KEYWORD1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   101
      case Bibtex.Token.Kind.KEYWORD => JEditToken.OPERATOR
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   102
      case Bibtex.Token.Kind.NAT => JEditToken.LITERAL2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   103
      case Bibtex.Token.Kind.STRING => JEditToken.LITERAL1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   104
      case Bibtex.Token.Kind.NAME => JEditToken.LABEL
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   105
      case Bibtex.Token.Kind.IDENT =>
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   106
        if (Bibtex.is_month(token.source)) JEditToken.LITERAL3
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   107
        else
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   108
          Bibtex.get_entry(context) match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   109
            case Some(entry) if entry.is_required(token.source) => JEditToken.KEYWORD3
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   110
            case Some(entry) if entry.is_optional(token.source) => JEditToken.KEYWORD4
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   111
            case _ => JEditToken.DIGIT
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   112
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   113
      case Bibtex.Token.Kind.SPACE => JEditToken.NULL
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   114
      case Bibtex.Token.Kind.COMMENT => JEditToken.COMMENT1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   115
      case Bibtex.Token.Kind.ERROR => JEditToken.INVALID
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   116
    }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   117
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   118
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   119
  /* line context */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   120
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   121
  private val context_rules = new ParserRuleSet("bibtex", "MAIN")
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   122
58699
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   123
  private class Line_Context(val context: Option[Bibtex.Line_Context])
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   124
    extends TokenMarker.LineContext(context_rules, null)
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   125
  {
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   126
    override def hashCode: Int = context.hashCode
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   127
    override def equals(that: Any): Boolean =
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   128
      that match {
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   129
        case other: Line_Context => context == other.context
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   130
        case _ => false
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   131
      }
e46afcceb781 tuned signature;
wenzelm
parents: 58698
diff changeset
   132
  }
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   133
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   134
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   135
  /* token marker */
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   136
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   137
  class Token_Marker extends TokenMarker
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   138
  {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   139
    override def markTokens(context: TokenMarker.LineContext,
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   140
        handler: TokenHandler, raw_line: Segment): TokenMarker.LineContext =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   141
    {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   142
      val line_ctxt =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   143
        context match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   144
          case c: Line_Context => c.context
58589
d9350ec0937e tuned signature;
wenzelm
parents: 58548
diff changeset
   145
          case _ => Some(Bibtex.Ignored)
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   146
        }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   147
      val line = if (raw_line == null) new Segment else raw_line
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   148
58595
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   149
      def no_markup =
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   150
      {
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   151
        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
   152
        (List(styled_token), new Line_Context(None))
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   153
      }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   154
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   155
      val context1 =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   156
      {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   157
        val (styled_tokens, context1) =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   158
          line_ctxt match {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   159
            case Some(ctxt) =>
58595
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   160
              try {
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   161
                val (chunks, ctxt1) = Bibtex.parse_line(line, ctxt)
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   162
                val styled_tokens =
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   163
                  for { chunk <- chunks; tok <- chunk.tokens }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   164
                  yield (token_style(chunk.kind, tok), tok.source)
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   165
                (styled_tokens, new Line_Context(Some(ctxt1)))
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   166
              }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   167
              catch { case ERROR(msg) => Output.warning(msg); no_markup }
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   168
            case None => no_markup
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   169
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   170
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   171
        var offset = 0
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   172
        for ((style, token) <- styled_tokens) {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   173
          val length = token.length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   174
          val end_offset = offset + length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   175
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   176
          if ((offset until end_offset).exists(i => line.charAt(i) == '\t')) {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   177
            for (i <- offset until end_offset)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   178
              handler.handleToken(line, style, i, 1, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   179
          }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   180
          else handler.handleToken(line, style, offset, length, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   181
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   182
          offset += length
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   183
        }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   184
        handler.handleToken(line, JEditToken.END, line.count, 0, context1)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   185
        context1
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   186
      }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   187
      val context2 = context1.intern
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   188
      handler.setLineContext(context2)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   189
      context2
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   190
    }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   191
  }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   192
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   193
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   194
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   195
  /** Sidekick parser **/
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   196
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   197
  class Sidekick_Parser extends SideKickParser("bibtex")
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   198
  {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   199
    override def supportsCompletion = false
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   200
58747
c680f181b32e tuned rendering;
wenzelm
parents: 58699
diff changeset
   201
    private class Asset(label: String, label_html: String, range: Text.Range, source: String)
c680f181b32e tuned rendering;
wenzelm
parents: 58699
diff changeset
   202
      extends Isabelle_Sidekick.Asset(label, range) {
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   203
        override def getShortString: String = label_html
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   204
        override def getLongString: String = source
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   205
      }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   206
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   207
    def parse(buffer: Buffer, error_source: errorlist.DefaultErrorSource): SideKickParsedData =
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   208
    {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   209
      val data = Isabelle_Sidekick.root_data(buffer)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   210
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   211
      try {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   212
        var offset = 0
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   213
        for (chunk <- Bibtex.parse(JEdit_Lib.buffer_text(buffer))) {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   214
          val kind = chunk.kind
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   215
          val name = chunk.name
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   216
          val source = chunk.source
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   217
          if (kind != "") {
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   218
            val label = kind + (if (name == "") "" else " " + name)
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   219
            val label_html =
62113
16de2a9b5b3d tuned -- according to ML version;
wenzelm
parents: 61100
diff changeset
   220
              "<html><b>" + HTML.output(kind) + "</b>" +
16de2a9b5b3d tuned -- according to ML version;
wenzelm
parents: 61100
diff changeset
   221
              (if (name == "") "" else " " + HTML.output(name)) + "</html>"
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59319
diff changeset
   222
            val range = Text.Range(offset, offset + source.length)
58747
c680f181b32e tuned rendering;
wenzelm
parents: 58699
diff changeset
   223
            val asset = new Asset(label, label_html, range, source)
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   224
            data.root.add(new DefaultMutableTreeNode(asset))
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   225
          }
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59319
diff changeset
   226
          offset += source.length
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   227
        }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   228
        data
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   229
      }
58595
127f192b755c more defensive error handling -- token marker must not crash;
wenzelm
parents: 58592
diff changeset
   230
      catch { case ERROR(msg) => Output.warning(msg); null }
58546
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   231
    }
72e2b2a609c4 clarified modules;
wenzelm
parents: 58545
diff changeset
   232
  }
58545
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
   233
}
30b75b7958d6 citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff changeset
   234