-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodejar.emacs.js
315 lines (294 loc) · 6.59 KB
/
codejar.emacs.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
window.onbeforeunload = (e) => {
// Cancel the event
e.preventDefault();
// Chrome requires returnValue to be set
e.returnValue = "error: CRTL+W keybinding doesn't work";
};
function moveCursorForward(editor, jar) {
const pos = jar.save();
if (pos.start < editor.textContent.length) {
pos.start++;
pos.end = pos.start;
jar.restore(pos);
}
}
function moveCursorBackward(editor, jar) {
const pos = jar.save();
if (pos.start > 0) {
pos.start--;
pos.end = pos.start;
jar.restore(pos);
}
}
function moveCursorWordForward(editor, jar) {
const pos = jar.save();
const text = editor.textContent;
const nextWordPos = text.slice(pos.start).search(/\W\w/);
if (nextWordPos !== -1) {
pos.start += nextWordPos + 1;
pos.end = pos.start;
jar.restore(pos);
}
}
function moveCursorWordBackward(editor, jar) {
const pos = jar.save();
const text = editor.textContent.slice(0, pos.start);
const prevWordPos = text.search(/\w\W*$/);
if (prevWordPos !== -1) {
pos.start = prevWordPos;
pos.end = pos.start;
jar.restore(pos);
}
}
function deleteNextWord(editor, jar) {
const pos = jar.save();
const text = editor.textContent;
const nextWordPos = text.slice(pos.start).search(/\W\w/);
if (nextWordPos !== -1) {
const end = pos.start + nextWordPos + 1;
editor.textContent = text.slice(0, pos.start) + text.slice(end);
jar.updateCode(editor.textContent); // Updates the CodeJar instance
}
}
function deletePreviousWord(editor, jar) {
const pos = jar.save();
const text = editor.textContent.slice(0, pos.start);
const prevWordPos = text.search(/\w\W*$/);
if (prevWordPos !== -1) {
editor.textContent =
text.slice(0, prevWordPos) + editor.textContent.slice(pos.start);
jar.updateCode(editor.textContent); // Updates the CodeJar instance
}
}
function transposeCharacters(editor, jar) {
const pos = jar.save();
if (pos.start > 0 && pos.start < editor.textContent.length) {
const text = editor.textContent;
const newPos = pos.start;
const newText =
text.slice(0, newPos - 1) +
text[newPos] +
text[newPos - 1] +
text.slice(newPos + 1);
editor.textContent = newText;
jar.updateCode(editor.textContent); // Updates the CodeJar instance
pos.start = newPos + 1;
pos.end = pos.start;
jar.restore(pos);
}
}
function moveCursorUp(editor, jar) {
const pos = jar.save();
const lines = editor.textContent.split("\n");
let currentLineStart = 0;
let lineIndex = 0;
// Find the start of the current line
for (let i = 0; i < lines.length; i++) {
const lineLength = lines[i].length + 1;
if (currentLineStart + lineLength > pos.start) {
lineIndex = i;
break;
}
currentLineStart += lineLength;
}
// If not the first line, move up
if (lineIndex > 0) {
const prevLineLength = lines[lineIndex - 1].length;
pos.start = pos.end =
Math.max(currentLineStart - prevLineLength - 1, 0) +
Math.min(pos.start - currentLineStart, prevLineLength);
jar.restore(pos);
}
}
function moveCursorDown(editor, jar) {
const pos = jar.save();
const lines = editor.textContent.split("\n");
let currentLineStart = 0;
let lineIndex = 0;
// Find the start of the current line
for (let i = 0; i < lines.length; i++) {
const lineLength = lines[i].length + 1;
if (currentLineStart + lineLength > pos.start) {
lineIndex = i;
break;
}
currentLineStart += lineLength;
}
// If not the last line, move down
if (lineIndex < lines.length - 1) {
const nextLineStart = currentLineStart + lines[lineIndex].length + 1;
const nextLineLength = lines[lineIndex + 1].length;
pos.start = pos.end =
nextLineStart + Math.min(pos.start - currentLineStart, nextLineLength);
jar.restore(pos);
}
}
const emacsKeybindings = [
{
key: "s",
ctrl: true,
name: "Save",
action: (editor) => {
console.log("Save command executed");
// Implement save logic here
},
},
{
key: "x",
ctrl: true,
name: "Close",
action: (editor) => {
console.log("Close command executed");
// Implement close logic here
},
},
{
key: "f",
ctrl: true,
name: "Move Cursor Forward",
action: moveCursorForward,
},
{
key: "b",
ctrl: true,
name: "Move Cursor Backward",
action: moveCursorBackward,
},
{
key: "a",
ctrl: true,
name: "Move Cursor to Beginning of Line",
action: (editor) => {
const s = getSelection();
const r = s.getRangeAt(0);
r.setStart(r.startContainer, 0);
r.setEnd(r.startContainer, 0);
s.removeAllRanges();
s.addRange(r);
},
},
{
key: "e",
ctrl: true,
name: "Move Cursor to End of Line",
action: (editor) => {
const s = getSelection();
const r = s.getRangeAt(0);
r.setStart(r.startContainer, r.startContainer.textContent.length);
r.setEnd(r.startContainer, r.startContainer.textContent.length);
s.removeAllRanges();
s.addRange(r);
},
},
{
key: "d",
ctrl: true,
name: "Delete Next Character",
action: (editor) => {
const s = getSelection();
const r = s.getRangeAt(0);
r.setEnd(r.startContainer, r.startOffset + 1);
r.deleteContents();
},
},
{
key: "h",
ctrl: true,
name: "Delete Previous Character",
action: (editor) => {
const s = getSelection();
const r = s.getRangeAt(0);
r.setStart(r.startContainer, r.startOffset - 1);
r.deleteContents();
},
},
{
key: "k",
ctrl: true,
name: "Kill (Cut) to End of Line",
action: (editor) => {
const s = getSelection();
const r = s.getRangeAt(0);
r.setEnd(r.startContainer, r.startContainer.textContent.length);
document.execCommand("cut");
},
},
{
key: "w",
ctrl: true,
name: "Copy",
action: (editor) => {
// This doesn't work
document.execCommand("copy");
},
},
{
key: "y",
ctrl: true,
name: "Paste",
action: (editor) => {
document.execCommand("paste");
},
},
{
key: "p",
ctrl: true,
name: "Move Cursor Up",
action: (editor) => {
moveCursorUp(editor);
},
},
{
key: "n",
ctrl: true,
name: "Move Cursor Down",
action: (editor) => {
moveCursorDown(editor);
},
},
{
key: "f",
ctrl: false,
alt: true,
name: "Move Cursor Forward One Word",
action: (editor) => {
moveCursorWordForward(editor);
},
},
{
key: "b",
ctrl: false,
alt: true,
name: "Move Cursor Backward One Word",
action: (editor) => {
moveCursorWordBackward(editor);
},
},
{
key: "d",
ctrl: false,
alt: true,
name: "Delete Next Word",
action: (editor) => {
deleteNextWord(editor);
},
},
{
key: "h",
ctrl: true,
alt: true,
name: "Delete Previous Word",
action: (editor) => {
deletePreviousWord(editor);
},
},
{
key: "t",
ctrl: true,
name: "Transpose Characters",
action: (editor) => {
transposeCharacters(editor);
},
},
];
export default emacsKeybindings;