Skip to content

Commit 52678ab

Browse files
committed
command completion
1 parent 5a08216 commit 52678ab

2 files changed

Lines changed: 91 additions & 2 deletions

File tree

assets/scripts/latexcmds.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{
3+
"latex":[
4+
"documentclass",
5+
"begin",
6+
"begin{document}",
7+
"begin{enumerate}",
8+
"begin{itemize}",
9+
"begin{figure}",
10+
"begin{table}",
11+
"begin{equation}",
12+
"begin{equation*}",
13+
"end",
14+
"end{document}",
15+
"end{enumerate}",
16+
"end{itemize}",
17+
"end{figure}",
18+
"end{table}",
19+
"end{equation}",
20+
"end{equation*}",
21+
"frac",
22+
"item",
23+
"usepackage",
24+
"section",
25+
"subsection"
26+
],
27+
"book":[
28+
"chapter"
29+
],
30+
"amsmath": [
31+
"tfrac",
32+
"dfrac",
33+
"begin{align}",
34+
"begin{align*}",
35+
"begin{gather}",
36+
"begin{gather*}",
37+
"begin{aligned}",
38+
"end{align}",
39+
"end{align*}",
40+
"end{gather}",
41+
"end{gather*}",
42+
"end{aligned}"
43+
]
44+
}

assets/scripts/runlatex.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ runlatex.adddefaultpreamble=false;
2626
runlatex.adddefaultengine=false;
2727
runlatex.usecaptions=false;
2828
runlatex.minrunlines=0;
29+
runlatex.completionsURI="";
2930

3031
// debug by using https://httpbin.org/post
3132
// set to null to omit from interface
@@ -65,7 +66,43 @@ var packageregex = [
6566
[ /pspicture/, "\\usepackage{pstricks}\n" ]
6667
];
6768

69+
70+
var latexcompetions="";
71+
72+
73+
var customCompleter = {
74+
getCompletions: function(editor, session, pos, prefix, callback) {
75+
var startToken = session.getTokenAt(pos.row, pos.column).value;
76+
if (startToken.startsWith("\\")){
77+
var cmplts=[];
78+
var s=0;
79+
for (let pkg in latexcompletions) {
80+
var cs=latexcompletions[pkg];
81+
s=s-1;
82+
for(let i=0;i<cs.length;i++){
83+
if(cs[i].startsWith(prefix)){
84+
cmplts.push({name: cs[i], value:cs[i],score: s, meta: pkg});
85+
}
86+
}
87+
}
88+
callback(null, cmplts);
89+
} else {
90+
callback(null, []);
91+
return
92+
}
93+
}
94+
}
95+
6896
function llexamples() {
97+
if(runlatex.completionsURI != ""){
98+
let request = new XMLHttpRequest();
99+
request.open('GET', runlatex.completionsURI);
100+
request.responseType = 'json';
101+
request.onload = function() {
102+
latexcompletions = request.response;
103+
}
104+
request.send();
105+
}
69106
var p = document.getElementsByTagName("pre");
70107
var editor;
71108
var acemode;
@@ -127,13 +164,21 @@ function llexamples() {
127164
}
128165
}
129166
p[i].textContent=pretext.replace(/\s+$/,'');
130-
editor = ace.edit(p[i]);
131167
ace.config.set('basePath', 'https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12') ;
168+
editor = ace.edit(p[i]);
132169
editor.setTheme(rlacetheme);
133170
editor.getSession().setMode(acemode);
134-
editor.setOption("minLines",1);
171+
editor.setOption("minLines",runlatex);
135172
editor.setOption("maxLines",runlatex.editorlines);
136173
editor.setShowPrintMargin(false);
174+
if(runlatex.completionsURI != ""){
175+
langTools=ace.require("ace/ext/language_tools");
176+
langTools.setCompleters([customCompleter]);
177+
editor.setOptions({
178+
enableBasicAutocompletion: true,
179+
enableLiveAutocompletion: true
180+
});
181+
}
137182
editor.resize();
138183
editors["pre" + i]=editor;
139184
}

0 commit comments

Comments
 (0)