-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
167 lines (143 loc) · 4.07 KB
/
index.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
"use strict";
const {CompositeDisposable} = require("atom");
const childProcess = require("child_process");
const path = require("path");
/**
* Singleton class which manages the package's lifecycle.
* @hideconstructor
* @class
*/
class EmacsLisp{
/**
* Register the package's commands with Atom.
* @internal
*/
activate(){
this.disposables = new CompositeDisposable();
this.disposables.add(atom.commands.add("atom-text-editor", {
"language-emacs-lisp:run-selection": e => {
const text = this.getSelection();
if(text && !/^\s+$/.test(text))
this.eval(text)
.then(output => this.showOutput(output))
.catch(output => this.showOutput(output, true));
},
"language-emacs-lisp:run-file": e => {
const ed = atom.workspace.getActiveTextEditor();
if(ed) this.runFile(ed.getPath())
.then(output => this.showOutput(output))
.catch(output => this.showOutput(output, true));
},
}));
}
/**
* Free up memory when deactivating package.
* @internal
*/
deactivate(){
if(null !== this.disposables)
this.disposables.dispose();
this.disposables = null;
}
/**
* Evaluate a string of Emacs Lisp code.
*
* @example eval("(+ 5 5)") -> "10"
* @param {String} expr
* @return {Promise} Resolves with collected output.
*/
eval(expr){
return new Promise((resolve, reject) => {
expr = expr.replace(/\r(?=\n)/g, "");
if(!/^\s*\(message\s+".+?%.+?"\s(?:.|\n)+\)\s*$/.test(expr))
expr = `(message "%s" ${expr})`;
const emacs = childProcess.spawn("emacs", ["--batch", "--eval", expr]);
let output = "";
emacs.stderr.on("data", data => {
if(data) output += data.toString();
});
emacs.on("close", code => {
output = output.replace(/^\n+|\n+$/g, "");
code !== 0
? reject(output)
: resolve(output);
})
});
}
/**
* Run a Lisp file in Emacs.
*
* @example runFile("~/.emacs.d/lisp/script.el")
* @param {String} file - Path to file
* @return {Promise} Resolves with the script's output, if any.
*/
runFile(file){
return new Promise((resolve, reject) => {
const emacs = childProcess.spawn("emacs", ["--script", file]);
let output = "";
emacs.stderr.on("data", data => {
if(data) output += data.toString();
});
emacs.on("close", code => {
output = output.replace(/^\n+|\n+$/g, "");
code !== 0
? reject(output)
: resolve(output);
})
});
}
/**
* Show output in the notifications area.
*
* @param {String} text
* @param {Boolean} error
* @return {NotificationElement}
* @internal
*/
showOutput(text, error = false){
const msg = "**Emacs:**";
const opt = {dismissable: true, detail: text};
const view = atom.views.getView(error
? atom.notifications.addError(msg, opt)
: atom.notifications.addInfo(msg, opt));
// Select output when clicked to make copying easier
if(view && view.element){
const output = view.element.querySelector(".content > .detail.item");
output && Object.assign(output.style, {userSelect: "all", cursor: "text"});
}
return view;
}
/**
* Retrieve an editor's currently-selected text.
*
* If nothing's selected, the scope enclosing the cursor's
* current position is selected and returned instead.
*
* @param {TextEditor} ed - Defaults to current editor
* @return {String}
* @internal
*/
getSelection(ed){
ed = ed || atom.workspace.getActiveTextEditor();
if(!ed) return "";
let text = ed.getSelectedText();
// If the user hasn't made a selection, make one for 'em
if(!text){
if(atom.packages.activePackages["bracket-matcher"]){
const command = "bracket-matcher:select-inside-brackets";
atom.commands.dispatch(ed.element, command);
// Select containing brackets too
const range = ed.getSelectedBufferRange();
--range.start.column;
++range.end.column;
ed.setSelectedBufferRange(range);
}
// Fallback if bracket-matcher isn't available
else ed.selectWordsContainingCursors();
text = ed.getSelectedText();
}
return text;
}
}
EmacsLisp.prototype.disposables = null;
module.exports = new EmacsLisp();