author | wenzelm |
Mon, 19 Jun 2017 20:32:06 +0200 | |
changeset 66117 | e6f808d1307c |
parent 66115 | 135bf45026ea |
child 66118 | 03dd799fe042 |
permissions | -rw-r--r-- |
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 | 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} |
58546 | 23 |
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler, ParserRuleSet} |
24 |
||
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 | 31 |
|
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 | 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 | 37 |
|
38 |
def completion( |
|
39 |
history: Completion.History, |
|
66117 | 40 |
rendering: JEdit_Rendering, |
41 |
caret: Text.Offset): Option[Completion.Result] = |
|
58592 | 42 |
{ |
43 |
for { |
|
66117 | 44 |
Text.Info(r, name) <- rendering.citation(rendering.before_caret_range(caret)) |
61100 | 45 |
name1 <- Completion.clean_name(name) |
46 |
||
66115 | 47 |
original <- rendering.model.try_get_text(r) |
61100 | 48 |
original1 <- Completion.clean_name(Library.perhaps_unquote(original)) |
49 |
||
50 |
entries = complete(name1).filter(_ != original1) |
|
59319 | 51 |
if entries.nonEmpty |
61100 | 52 |
|
58592 | 53 |
items = |
61099 | 54 |
entries.sorted.map({ |
58592 | 55 |
case entry => |
56 |
val full_name = Long_Name.qualify(Markup.CITATION, entry) |
|
57 |
val description = List(entry, "(BibTeX entry)") |
|
58 |
val replacement = quote(entry) |
|
59 |
Completion.Item(r, original, full_name, description, replacement, 0, false) |
|
60 |
}).sorted(history.ordering).take(PIDE.options.int("completion_limit")) |
|
61 |
} yield Completion.Result(r, original, false, items) |
|
62 |
} |
|
63 |
||
64 |
||
58546 | 65 |
|
58548 | 66 |
/** context menu **/ |
67 |
||
68 |
def context_menu(text_area0: JEditTextArea): List[JMenuItem] = |
|
69 |
{ |
|
70 |
text_area0 match { |
|
71 |
case text_area: TextArea => |
|
72 |
text_area.getBuffer match { |
|
73 |
case buffer: Buffer |
|
64828 | 74 |
if (Bibtex.check_name(JEdit_Lib.buffer_name(buffer)) && buffer.isEditable) => |
58548 | 75 |
val menu = new JMenu("BibTeX entries") |
76 |
for (entry <- Bibtex.entries) { |
|
77 |
val item = new JMenuItem(entry.kind) |
|
78 |
item.addActionListener(new ActionListener { |
|
79 |
def actionPerformed(evt: ActionEvent): Unit = |
|
80 |
Isabelle.insert_line_padding(text_area, entry.template) |
|
81 |
}) |
|
82 |
menu.add(item) |
|
83 |
} |
|
84 |
List(menu) |
|
85 |
case _ => Nil |
|
86 |
} |
|
87 |
case _ => Nil |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
||
92 |
||
58546 | 93 |
/** token markup **/ |
94 |
||
95 |
/* token style */ |
|
96 |
||
97 |
private def token_style(context: String, token: Bibtex.Token): Byte = |
|
98 |
token.kind match { |
|
99 |
case Bibtex.Token.Kind.COMMAND => JEditToken.KEYWORD2 |
|
100 |
case Bibtex.Token.Kind.ENTRY => JEditToken.KEYWORD1 |
|
101 |
case Bibtex.Token.Kind.KEYWORD => JEditToken.OPERATOR |
|
102 |
case Bibtex.Token.Kind.NAT => JEditToken.LITERAL2 |
|
103 |
case Bibtex.Token.Kind.STRING => JEditToken.LITERAL1 |
|
104 |
case Bibtex.Token.Kind.NAME => JEditToken.LABEL |
|
105 |
case Bibtex.Token.Kind.IDENT => |
|
106 |
if (Bibtex.is_month(token.source)) JEditToken.LITERAL3 |
|
107 |
else |
|
108 |
Bibtex.get_entry(context) match { |
|
109 |
case Some(entry) if entry.is_required(token.source) => JEditToken.KEYWORD3 |
|
110 |
case Some(entry) if entry.is_optional(token.source) => JEditToken.KEYWORD4 |
|
111 |
case _ => JEditToken.DIGIT |
|
112 |
} |
|
113 |
case Bibtex.Token.Kind.SPACE => JEditToken.NULL |
|
114 |
case Bibtex.Token.Kind.COMMENT => JEditToken.COMMENT1 |
|
115 |
case Bibtex.Token.Kind.ERROR => JEditToken.INVALID |
|
116 |
} |
|
117 |
||
118 |
||
119 |
/* line context */ |
|
120 |
||
121 |
private val context_rules = new ParserRuleSet("bibtex", "MAIN") |
|
122 |
||
58699 | 123 |
private class Line_Context(val context: Option[Bibtex.Line_Context]) |
124 |
extends TokenMarker.LineContext(context_rules, null) |
|
125 |
{ |
|
126 |
override def hashCode: Int = context.hashCode |
|
127 |
override def equals(that: Any): Boolean = |
|
128 |
that match { |
|
129 |
case other: Line_Context => context == other.context |
|
130 |
case _ => false |
|
131 |
} |
|
132 |
} |
|
58546 | 133 |
|
134 |
||
135 |
/* token marker */ |
|
136 |
||
137 |
class Token_Marker extends TokenMarker |
|
138 |
{ |
|
139 |
override def markTokens(context: TokenMarker.LineContext, |
|
140 |
handler: TokenHandler, raw_line: Segment): TokenMarker.LineContext = |
|
141 |
{ |
|
142 |
val line_ctxt = |
|
143 |
context match { |
|
144 |
case c: Line_Context => c.context |
|
58589 | 145 |
case _ => Some(Bibtex.Ignored) |
58546 | 146 |
} |
147 |
val line = if (raw_line == null) new Segment else raw_line |
|
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 | 155 |
val context1 = |
156 |
{ |
|
157 |
val (styled_tokens, context1) = |
|
158 |
line_ctxt match { |
|
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 | 169 |
} |
170 |
||
171 |
var offset = 0 |
|
172 |
for ((style, token) <- styled_tokens) { |
|
173 |
val length = token.length |
|
174 |
val end_offset = offset + length |
|
175 |
||
176 |
if ((offset until end_offset).exists(i => line.charAt(i) == '\t')) { |
|
177 |
for (i <- offset until end_offset) |
|
178 |
handler.handleToken(line, style, i, 1, context1) |
|
179 |
} |
|
180 |
else handler.handleToken(line, style, offset, length, context1) |
|
181 |
||
182 |
offset += length |
|
183 |
} |
|
184 |
handler.handleToken(line, JEditToken.END, line.count, 0, context1) |
|
185 |
context1 |
|
186 |
} |
|
187 |
val context2 = context1.intern |
|
188 |
handler.setLineContext(context2) |
|
189 |
context2 |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
||
194 |
||
195 |
/** Sidekick parser **/ |
|
196 |
||
197 |
class Sidekick_Parser extends SideKickParser("bibtex") |
|
198 |
{ |
|
199 |
override def supportsCompletion = false |
|
200 |
||
58747 | 201 |
private class Asset(label: String, label_html: String, range: Text.Range, source: String) |
202 |
extends Isabelle_Sidekick.Asset(label, range) { |
|
58546 | 203 |
override def getShortString: String = label_html |
204 |
override def getLongString: String = source |
|
205 |
} |
|
206 |
||
207 |
def parse(buffer: Buffer, error_source: errorlist.DefaultErrorSource): SideKickParsedData = |
|
208 |
{ |
|
209 |
val data = Isabelle_Sidekick.root_data(buffer) |
|
210 |
||
211 |
try { |
|
212 |
var offset = 0 |
|
213 |
for (chunk <- Bibtex.parse(JEdit_Lib.buffer_text(buffer))) { |
|
214 |
val kind = chunk.kind |
|
215 |
val name = chunk.name |
|
216 |
val source = chunk.source |
|
217 |
if (kind != "") { |
|
218 |
val label = kind + (if (name == "") "" else " " + name) |
|
219 |
val label_html = |
|
62113 | 220 |
"<html><b>" + HTML.output(kind) + "</b>" + |
221 |
(if (name == "") "" else " " + HTML.output(name)) + "</html>" |
|
60215 | 222 |
val range = Text.Range(offset, offset + source.length) |
58747 | 223 |
val asset = new Asset(label, label_html, range, source) |
58546 | 224 |
data.root.add(new DefaultMutableTreeNode(asset)) |
225 |
} |
|
60215 | 226 |
offset += source.length |
58546 | 227 |
} |
228 |
data |
|
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 | 231 |
} |
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 |