author | wenzelm |
Fri, 19 Aug 2022 16:46:00 +0200 | |
changeset 75906 | 2167b9e3157a |
parent 75435 | c8087e6bd2ce |
child 77031 | 02738f4333ee |
permissions | -rw-r--r-- |
66120 | 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 | 13 |
import scala.collection.mutable |
14 |
||
58548 | 15 |
import java.awt.event.{ActionListener, ActionEvent} |
16 |
||
58546 | 17 |
import javax.swing.text.Segment |
18 |
import javax.swing.tree.DefaultMutableTreeNode |
|
58548 | 19 |
import javax.swing.{JMenu, JMenuItem} |
58546 | 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 | 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 | 24 |
|
58545
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
25 |
|
75393 | 26 |
object JEdit_Bibtex { |
58548 | 27 |
/** context menu **/ |
28 |
||
75435 | 29 |
def context_menu(text_area: JEditTextArea): List[JMenuItem] = { |
30 |
text_area.getBuffer match { |
|
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 | 33 |
val menu = new JMenu("BibTeX entries") |
34 |
for (entry <- Bibtex.known_entries) { |
|
35 |
val item = new JMenuItem(entry.kind) |
|
36 |
item.addActionListener(new ActionListener { |
|
37 |
def actionPerformed(evt: ActionEvent): Unit = |
|
38 |
Isabelle.insert_line_padding(text_area, entry.template) |
|
39 |
}) |
|
40 |
menu.add(item) |
|
58548 | 41 |
} |
75435 | 42 |
List(menu) |
58548 | 43 |
case _ => Nil |
44 |
} |
|
45 |
} |
|
46 |
||
47 |
||
48 |
||
58546 | 49 |
/** token markup **/ |
50 |
||
51 |
/* token style */ |
|
52 |
||
53 |
private def token_style(context: String, token: Bibtex.Token): Byte = |
|
54 |
token.kind match { |
|
55 |
case Bibtex.Token.Kind.COMMAND => JEditToken.KEYWORD2 |
|
56 |
case Bibtex.Token.Kind.ENTRY => JEditToken.KEYWORD1 |
|
57 |
case Bibtex.Token.Kind.KEYWORD => JEditToken.OPERATOR |
|
58 |
case Bibtex.Token.Kind.NAT => JEditToken.LITERAL2 |
|
59 |
case Bibtex.Token.Kind.STRING => JEditToken.LITERAL1 |
|
60 |
case Bibtex.Token.Kind.NAME => JEditToken.LABEL |
|
61 |
case Bibtex.Token.Kind.IDENT => |
|
62 |
if (Bibtex.is_month(token.source)) JEditToken.LITERAL3 |
|
63 |
else |
|
64 |
Bibtex.get_entry(context) match { |
|
65 |
case Some(entry) if entry.is_required(token.source) => JEditToken.KEYWORD3 |
|
66 |
case Some(entry) if entry.is_optional(token.source) => JEditToken.KEYWORD4 |
|
67 |
case _ => JEditToken.DIGIT |
|
68 |
} |
|
69 |
case Bibtex.Token.Kind.SPACE => JEditToken.NULL |
|
70 |
case Bibtex.Token.Kind.COMMENT => JEditToken.COMMENT1 |
|
71 |
case Bibtex.Token.Kind.ERROR => JEditToken.INVALID |
|
72 |
} |
|
73 |
||
74 |
||
75 |
/* line context */ |
|
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 | 78 |
|
58699 | 79 |
private class Line_Context(val context: Option[Bibtex.Line_Context]) |
75393 | 80 |
extends TokenMarker.LineContext(mode_rule_set, null) { |
58699 | 81 |
override def hashCode: Int = context.hashCode |
82 |
override def equals(that: Any): Boolean = |
|
83 |
that match { |
|
84 |
case other: Line_Context => context == other.context |
|
85 |
case _ => false |
|
86 |
} |
|
87 |
} |
|
58546 | 88 |
|
89 |
||
90 |
/* token marker */ |
|
91 |
||
75393 | 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 | 95 |
override def markTokens( |
96 |
context: TokenMarker.LineContext, |
|
97 |
handler: TokenHandler, |
|
98 |
raw_line: Segment |
|
99 |
): TokenMarker.LineContext = { |
|
58546 | 100 |
val line_ctxt = |
101 |
context match { |
|
102 |
case c: Line_Context => c.context |
|
58589 | 103 |
case _ => Some(Bibtex.Ignored) |
58546 | 104 |
} |
105 |
val line = if (raw_line == null) new Segment else raw_line |
|
106 |
||
75393 | 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 | 112 |
val context1 = { |
58546 | 113 |
val (styled_tokens, context1) = |
114 |
line_ctxt match { |
|
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 | 125 |
} |
126 |
||
127 |
var offset = 0 |
|
128 |
for ((style, token) <- styled_tokens) { |
|
129 |
val length = token.length |
|
130 |
val end_offset = offset + length |
|
131 |
||
132 |
if ((offset until end_offset).exists(i => line.charAt(i) == '\t')) { |
|
133 |
for (i <- offset until end_offset) |
|
134 |
handler.handleToken(line, style, i, 1, context1) |
|
135 |
} |
|
136 |
else handler.handleToken(line, style, offset, length, context1) |
|
137 |
||
138 |
offset += length |
|
139 |
} |
|
140 |
handler.handleToken(line, JEditToken.END, line.count, 0, context1) |
|
141 |
context1 |
|
142 |
} |
|
143 |
val context2 = context1.intern |
|
144 |
handler.setLineContext(context2) |
|
145 |
context2 |
|
146 |
} |
|
147 |
} |
|
58545
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
148 |
} |