-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
229 lines (201 loc) · 9.59 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Latex extension for brackets
* @author Patrick Oladimeji
* @date 11/29/13 9:20:10 AM
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, brackets*/
define(function (require, exports, module) {
"use strict";
var EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
CommandManager = brackets.getModule("command/CommandManager"),
KeyBindingManager = brackets.getModule("command/KeyBindingManager"),
Menu = brackets.getModule("command/Menus"),
LanguageManager = brackets.getModule("language/LanguageManager"),
NodeDomain = brackets.getModule("utils/NodeDomain"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
FileUtils = brackets.getModule("file/FileUtils"),
AppInit = brackets.getModule("utils/AppInit"),
SettingsDialog = require("SettingsDialog"),
ConsolePanel = require("ConsolePanel"),
LatexKeywordHint = require("codeHints/LatexKeywordHint"),
LatexCiteKeyHint = require("codeHints/LatexCiteKeyHint"),
LatexLabelHint = require("codeHints/LatexLabelHint"),
LatexDocumentParser = require("codeHints/LatexDocumentParser"),
CodeHintManager = brackets.getModule("editor/CodeHintManager"),
latexFold = require("foldhelpers/latex-fold"),
preferences = require("Preferences"),
PathUtils = brackets.getModule("thirdparty/path-utils/path-utils"),
MainViewManager = brackets.getModule("view/MainViewManager"),
CodeMirror = brackets.getModule("thirdparty/CodeMirror2/lib/codemirror");
var latexDomain,
latexIcon,
domainId = "bracketsLatex",
COMPILE = "compile",
LATEX_SETTINGS = "brackets-latex.settings",
texRelateFiledExtensions = ["sty", "tex", "bib", "cls", "bbl"],
Strings = require("i18n!nls/strings"),
TEX_ROOT = "%!TEX root=";
ExtensionUtils.loadStyleSheet(module, "less/brackets-latex.less");
function bibtex(options) {
if (!options) {
var editor = EditorManager.getCurrentFullEditor();
options = preferences.getAllValues();
options.projectRoot = ProjectManager.getProjectRoot().fullPath;
options.fileName = preferences.get("mainFile") ? options.projectRoot + preferences.get("mainFile") :
editor.document.file.fullPath;
options.compiler = "bibtex";
}
var compileMessage = options.compiler + ": " + Strings.COMPILING + " " + options.fileName + "\n";
ConsolePanel.clear()
.appendMessage(compileMessage);
latexDomain.exec("bibtex", options)
.done(function (res) {
latexIcon.addClass("on").removeClass("error");
console.log(res);
ConsolePanel.appendMessage(res.stdout.toString());
}).fail(function (err) {
latexIcon.addClass("error").removeClass("on");
console.log(err);
ConsolePanel.appendMessage("\n")
.appendMessage(err.stdout.toString());
});
}
function getTEXRoot(editor) {
var firstLine = editor._codeMirror.getLine(0);
if (firstLine.trim().indexOf(TEX_ROOT) === 0) {
var rootPath = firstLine.split("=")[1];
return rootPath.trim();
}
return null;
}
function showSettingsDialog() {
SettingsDialog.show();
}
function getMainFileDocument(path) {
return DocumentManager.getDocumentForPath(path);
}
function compile() {
var editor = EditorManager.getCurrentFullEditor();
var texRoot = getTEXRoot(editor);
var options = preferences.getAllValues();
options.projectRoot = ProjectManager.getProjectRoot().fullPath;
options.fileName = preferences.get("mainFile") ? options.projectRoot + preferences.get("mainFile") :
editor.document.file.fullPath;
//set bibfile name if the main file contains a \bibliography entry
getMainFileDocument(options.fileName)
.then(function (doc) {
options.bibFileName = LatexDocumentParser.getBibFileName(doc.getText());
}, function (err) {
console.log(err);
}).then(function () {
if (texRoot) {
options.texRoot = texRoot;
}
if (options.texBinDirectory.trim() === "") { //ensure the tex bin directory is set
showSettingsDialog();
ConsolePanel.clear().appendMessage(Strings.TEX_BIN_DIR_ERROR);
} else if (options.compiler === "bibtex") {
bibtex(options);
return;
} else {
var compileMessage = options.compiler + ": " + Strings.COMPILING + " " + options.fileName + "\n";
ConsolePanel.clear()
.appendMessage(compileMessage);
latexDomain.exec("compile", options)
.done(function (res) {
latexIcon.addClass("on").removeClass("error");
console.log(res);
ConsolePanel.appendMessage(res.stdout.toString());
}).fail(function (err) {
latexIcon.addClass("error").removeClass("on");
console.log(err);
ConsolePanel.appendMessage("\n")
.appendMessage(err.stdout.toString())
.appendMessage(err.stderr.toString())
.appendMessage(err.err ? JSON.stringify(err.err, null, " ") : "");
});
}
});
}
function activeFileIsTexRelated() {
var editor = EditorManager.getCurrentFullEditor();
if (editor) {
var ext = FileUtils.getFileExtension(editor.document.file.fullPath);
return texRelateFiledExtensions.indexOf(ext) > -1;
}
}
function _currentDocChangedHandler(event, file) {
// get programming language
var ext = file ? PathUtils.filenameExtension(file.fullPath).toLowerCase().substr(1) : ""; // delete the dot
// Only show Tex and the right bar, if it's a tex related file
if (texRelateFiledExtensions.indexOf(ext) !== -1) {
$("#latex-toolbar-icon").css('display', 'block');
} else {
$("#latex-toolbar-icon").css('display', 'none');
}
}
function registerCodeHints() {
var keywordHints = new LatexKeywordHint();
var labelHints = new LatexLabelHint();
var citeKeyHints = new LatexCiteKeyHint();
CodeHintManager.registerHintProvider(keywordHints, ["latex"], 0);
CodeHintManager.registerHintProvider(labelHints, ["latex"], 1);
CodeHintManager.registerHintProvider(citeKeyHints, ["latex"], 2);
}
function init() {
ConsolePanel.initialise();
latexIcon = $("<a id='latex-toolbar-icon' href='#'></a>").appendTo($("#main-toolbar .buttons")).addClass("disabled");
latexIcon.on("click", function () {
//toggle panel if the document type is tex related
if (activeFileIsTexRelated()) {
ConsolePanel.toggle();
}
});
LanguageManager.defineLanguage("latex", {
name: "LaTeX",
mode: ["stex", "text/x-stex"],
fileExtensions: ["tex", "bib", "cls", "ltx", "clo", "sty", "def"],
lineComment: ["%"]
});
CodeMirror.registerHelper("fold", "stex", latexFold);
EditorManager.on("activeEditorChange", function (event, current, previous) {
var consoleVisibilityMap = preferences.getConsoleVisibilityMap() || {};
if (current) {
if (activeFileIsTexRelated()) {
latexIcon.addClass("on").removeClass("disabled");
if (consoleVisibilityMap[current.document.file.fullPath] === false) {
ConsolePanel.hide();
} else {
ConsolePanel.show();
}
} else {
ConsolePanel.hide();
latexIcon.addClass("disabled").removeClass("on");
}
}
});
latexDomain = new NodeDomain(domainId, ExtensionUtils.getModulePath(module, "node/CompileLatex"));
latexDomain.on("progress", function (event, data) {
var timeString = new Date(data.ts).toTimeString();
ConsolePanel.appendMessage("\n[" + timeString + "]: " + data.message);
console.log(data);
});
CommandManager.register(Strings.TEX_SETTINGS + " ...", LATEX_SETTINGS, showSettingsDialog);
CommandManager.register(Strings.COMPILE, COMPILE, compile);
Menu.getMenu(Menu.AppMenuBar.FILE_MENU).addMenuItem(LATEX_SETTINGS);
KeyBindingManager.addBinding(COMPILE, "Ctrl-Alt-B");
}
exports.compile = compile;
exports.bibtex = bibtex;
exports.showSettings = showSettingsDialog;
// Add a document change handler
MainViewManager.on("currentFileChange", _currentDocChangedHandler);
AppInit.appReady(function () {
init();
_currentDocChangedHandler();
registerCodeHints();
});
});