forked from codemirror/codemirror5
/
tex-input-hint.js
162 lines (127 loc) · 4.78 KB
/
tex-input-hint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
// TeX-style completion, written by Emilio J. Gallego Arias.
// List of open issues before a merge can be considered:
//
// - Make the unicode table a parameter.
//
// - Review if the way we capture '\' is comforming to CM coding standards.
//
// - We register a helper for "hint.coq", but we would like this
// helper to be available to all modes.
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
var Pos = CodeMirror.Pos;
// XXX: Generate automatically...
var unicodePreTable = [
// { text: "\\", symbol: "\\"},
{ text: "\\_1", symbol: "₁" },
{ text: "\\_2", symbol: "₂" },
{ text: "\\alpha", symbol: "α" },
{ text: "\\beta", symbol: "β" },
{ text: "\\delta", symbol: "δ" },
{ text: "\\epsilon", symbol: "ε" },
{ text: "\\exists", symbol: "∃" },
{ text: "\\forall", symbol: "∀" },
{ text: "\\gamma", symbol: "γ" },
{ text: "\\lambda", symbol: "λ" },
{ text: "\\land", symbol: "∧" },
{ text: "\\llbracket", symbol: "〚"},
{ text: "\\lnot", symbol: "¬" },
{ text: "\\lor", symbol: "∨" },
{ text: "\\mid", symbol: "∣" },
{ text: "\\models", symbol: "⊧" },
{ text: "\\oplus", symbol: "⊕" },
{ text: "\\otimes", symbol: "⊗" },
{ text: "\\pi", symbol: "π" },
{ text: "\\phi", symbol: "φ" },
{ text: "\\psi", symbol: "ψ" },
{ text: "\\rrbracket", symbol: "〛"},
{ text: "\\sigma", symbol: "σ" },
{ text: "\\times", symbol: "×" },
{ text: "\\to", symbol: "→" },
{ text: "\\vdash", symbol: "⊢" },
{ text: "\\Delta", symbol: "Δ" },
{ text: "\\Gamma", symbol: "Γ" },
{ text: "\\Lambda", symbol: "Λ" },
{ text: "\\Pi", symbol: "Π" },
{ text: "\\Sigma", symbol: "Σ" }
];
/* How our TeX-style completion works:
We always complete on a press of "\":
- We assume the current token starts starts with "\", but we stop
at the cursor, this allows to insert unicode in the middle of a
token.
[XXX: Should we scan back to the point "\" appears?]
- We generate the table, and add a handler to insert the proper
unicode.
*/
function TeX_input_hint(editor, _options) {
var cur = editor.getCursor(), token = editor.getTokenAt(cur);
// IMPORTANT: We match from the beginning of the token up to the
// cursor only!
// Thus, our end position is always cur.pos, and we must trim the
// token.
var curPos = Pos(cur.line, cur.ch);
// XXX: Scan back to first "\" ?
var matchStart = Pos(cur.line, token.start);
var matchEnd = curPos;
var tokenToMatch = token.string.substr(0, matchEnd.ch - matchStart.ch);
// console.log('cur/tok', cur, token, tokenToMatch);
// Replace the current token !
var insertFun = function(cm, _self, data) {
cm.replaceRange(data.symbol, matchStart, matchEnd);
};
var rList = [];
// Build of our table
unicodePreTable.map( function(obj) {
// console.log('Considering: ', obj, ' for ', tokenToMatch);
if ( obj.text.startsWith(tokenToMatch) ) {
// XXX: This can be improved for sure.
obj.displayText = obj.symbol + ' ' + obj.text;
obj.hint = insertFun;
rList.push(obj);
}
});
return { list: rList,
from: matchStart,
to: matchEnd
}
};
// We bind '\\'
function initTexInput (CodeMirror) {
// We bind slash to the latex autocomplete symbol.
// We also bind Space to insert current hint.
CodeMirror.defineInitHook(function (cm) {
// XXX: Do we want to hook on "_" and "^", etc... ?
cm.addKeyMap({"\\": function(cm)
{
cm.replaceSelection("\\");
cm.execCommand("autocomplete");
}});
// We need to update the local keymap to the hints.
var extraHintKeyMap = { Space: function(cm) {
var cA = cm.state.completionActive;
if (cA) {
cA.widget.pick();
// XXX: Ummmm, not if I want this...
// cm.replaceSelection(" ");
} } };
var cmplOpt = cm.getOption("hintOptions");
cmplOpt = cmplOpt || {};
cmplOpt['extraKeys'] = extraHintKeyMap;
cm.setOption("hintOptions", cmplOpt);
});
CodeMirror.registerHelper("hint", "coq", TeX_input_hint);
}
initTexInput(CodeMirror);
});
// Local Variables:
// js-indent-level: 2
// End: