5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
var cmds = CodeMirror.commands;
15
var Pos = CodeMirror.Pos;
16
function posEq(a, b) { return a.line == b.line && a.ch == b.ch; }
21
function addToRing(str) {
23
if (killRing.length > 50) killRing.shift();
25
function growRingTop(str) {
26
if (!killRing.length) return addToRing(str);
27
killRing[killRing.length - 1] += str;
29
function getFromRing(n) { return killRing[killRing.length - (n ? Math.min(n, 1) : 1)] || ""; }
30
function popFromRing() { if (killRing.length > 1) killRing.pop(); return getFromRing(); }
35
function _kill(cm, from, to, ring, text) {
36
if (text == null) text = cm.getRange(from, to);
38
if (ring == "grow" && lastKill && lastKill.cm == cm && posEq(from, lastKill.pos) && cm.isClean(lastKill.gen))
40
else if (ring !== false)
42
cm.replaceRange("", from, to, "+delete");
44
if (ring == "grow") lastKill = {cm: cm, pos: from, gen: cm.changeGeneration()};
50
function byChar(cm, pos, dir) {
51
return cm.findPosH(pos, dir, "char", true);
54
function byWord(cm, pos, dir) {
55
return cm.findPosH(pos, dir, "word", true);
58
function byLine(cm, pos, dir) {
59
return cm.findPosV(pos, dir, "line", cm.doc.sel.goalColumn);
62
function byPage(cm, pos, dir) {
63
return cm.findPosV(pos, dir, "page", cm.doc.sel.goalColumn);
66
function byParagraph(cm, pos, dir) {
67
var no = pos.line, line = cm.getLine(no);
68
var sawText = /\S/.test(dir < 0 ? line.slice(0, pos.ch) : line.slice(pos.ch));
69
var fst = cm.firstLine(), lst = cm.lastLine();
72
if (no < fst || no > lst)
73
return cm.clipPos(Pos(no - dir, dir < 0 ? 0 : null));
74
line = cm.getLine(no);
75
var hasText = /\S/.test(line);
76
if (hasText) sawText = true;
77
else if (sawText) return Pos(no, 0);
81
function bySentence(cm, pos, dir) {
82
var line = pos.line, ch = pos.ch;
83
var text = cm.getLine(pos.line), sawWord = false;
85
var next = text.charAt(ch + (dir < 0 ? -1 : 0));
87
if (line == (dir < 0 ? cm.firstLine() : cm.lastLine())) return Pos(line, ch);
88
text = cm.getLine(line + dir);
89
if (!/\S/.test(text)) return Pos(line, ch);
91
ch = dir < 0 ? text.length : 0;
94
if (sawWord && /[!?.]/.test(next)) return Pos(line, ch + (dir > 0 ? 1 : 0));
95
if (!sawWord) sawWord = /\w/.test(next);
100
function byExpr(cm, pos, dir) {
102
if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, {strict: true}))
103
&& wrap.match && (wrap.forward ? 1 : -1) == dir)
104
return dir > 0 ? Pos(wrap.to.line, wrap.to.ch + 1) : wrap.to;
106
for (var first = true;; first = false) {
107
var token = cm.getTokenAt(pos);
108
var after = Pos(pos.line, dir < 0 ? token.start : token.end);
109
if (first && dir > 0 && token.end == pos.ch || !/\w/.test(token.string)) {
110
var newPos = cm.findPosH(after, dir, "char");
111
if (posEq(after, newPos)) return pos;
121
function getPrefix(cm, precise) {
122
var digits = cm.state.emacsPrefix;
123
if (!digits) return precise ? null : 1;
125
return digits == "-" ? -1 : Number(digits);
128
function repeated(cmd) {
129
var f = typeof cmd == "string" ? function(cm) { cm.execCommand(cmd); } : cmd;
130
return function(cm) {
131
var prefix = getPrefix(cm);
133
for (var i = 1; i < prefix; ++i) f(cm);
137
function findEnd(cm, pos, by, dir) {
138
var prefix = getPrefix(cm);
139
if (prefix < 0) { dir = -dir; prefix = -prefix; }
140
for (var i = 0; i < prefix; ++i) {
141
var newPos = by(cm, pos, dir);
142
if (posEq(newPos, pos)) break;
148
function move(by, dir) {
149
var f = function(cm) {
150
cm.extendSelection(findEnd(cm, cm.getCursor(), by, dir));
156
function killTo(cm, by, dir, ring) {
157
var selections = cm.listSelections(), cursor;
158
var i = selections.length;
160
cursor = selections[i].head;
161
_kill(cm, cursor, findEnd(cm, cursor, by, dir), ring);
165
function _killRegion(cm, ring) {
166
if (cm.somethingSelected()) {
167
var selections = cm.listSelections(), selection;
168
var i = selections.length;
170
selection = selections[i];
171
_kill(cm, selection.anchor, selection.head, ring);
177
function addPrefix(cm, digit) {
178
if (cm.state.emacsPrefix) {
179
if (digit != "-") cm.state.emacsPrefix += digit;
183
cm.state.emacsPrefix = digit;
184
cm.on("keyHandled", maybeClearPrefix);
185
cm.on("inputRead", maybeDuplicateInput);
188
var prefixPreservingKeys = {"Alt-G": true, "Ctrl-X": true, "Ctrl-Q": true, "Ctrl-U": true};
190
function maybeClearPrefix(cm, arg) {
191
if (!cm.state.emacsPrefixMap && !prefixPreservingKeys.hasOwnProperty(arg))
195
function clearPrefix(cm) {
196
cm.state.emacsPrefix = null;
197
cm.off("keyHandled", maybeClearPrefix);
198
cm.off("inputRead", maybeDuplicateInput);
201
function maybeDuplicateInput(cm, event) {
202
var dup = getPrefix(cm);
203
if (dup > 1 && event.origin == "+input") {
204
var one = event.text.join("\n"), txt = "";
205
for (var i = 1; i < dup; ++i) txt += one;
206
cm.replaceSelection(txt);
210
function maybeRemovePrefixMap(cm, arg) {
211
if (typeof arg == "string" && (/^\d$/.test(arg) || arg == "Ctrl-U")) return;
212
cm.removeKeyMap(prefixMap);
213
cm.state.emacsPrefixMap = false;
214
cm.off("keyHandled", maybeRemovePrefixMap);
215
cm.off("inputRead", maybeRemovePrefixMap);
220
cmds.setMark = function (cm) {
221
cm.setCursor(cm.getCursor());
222
cm.setExtending(!cm.getExtending());
223
cm.on("change", function() { cm.setExtending(false); });
226
function clearMark(cm) {
227
cm.setExtending(false);
228
cm.setCursor(cm.getCursor());
231
function makePrompt(msg) {
232
var fragment = document.createDocumentFragment();
233
var input = document.createElement("input");
234
input.setAttribute("type", "text");
235
input.style.width = "10em";
236
fragment.appendChild(document.createTextNode(msg + ": "));
237
fragment.appendChild(input);
241
function getInput(cm, msg, f) {
243
cm.openDialog(makePrompt(msg), f, {bottom: true});
248
function operateOnWord(cm, op) {
249
var start = cm.getCursor(), end = cm.findPosH(start, 1, "word");
250
cm.replaceRange(op(cm.getRange(start, end)), start, end);
254
function toEnclosingExpr(cm) {
255
var pos = cm.getCursor(), line = pos.line, ch = pos.ch;
257
while (line >= cm.firstLine()) {
258
var text = cm.getLine(line);
259
for (var i = ch == null ? text.length : ch; i > 0;) {
260
var ch = text.charAt(--i);
267
else if (/[\(\{\[]/.test(ch) && (!stack.length || stack.pop() != ch))
268
return cm.extendSelection(Pos(line, i));
277
cmds.killRegion = function(cm) {
278
_kill(cm, cm.getCursor("start"), cm.getCursor("end"), true);
282
cmds.killLineEmacs = repeated(function(cm) {
283
var start = cm.getCursor(), end = cm.clipPos(Pos(start.line));
284
var text = cm.getRange(start, end);
285
if (!/\S/.test(text)) {
287
end = Pos(start.line + 1, 0);
289
_kill(cm, start, end, "grow", text);
292
cmds.killRingSave = function(cm) {
293
addToRing(cm.getSelection());
297
cmds.yank = function(cm) {
298
var start = cm.getCursor();
299
cm.replaceRange(getFromRing(getPrefix(cm)), start, start, "paste");
300
cm.setSelection(start, cm.getCursor());
303
cmds.yankPop = function(cm) {
304
cm.replaceSelection(popFromRing(), "around", "paste");
307
cmds.forwardChar = move(byChar, 1);
309
cmds.backwardChar = move(byChar, -1)
311
cmds.deleteChar = function(cm) { killTo(cm, byChar, 1, false); };
313
cmds.deleteForwardChar = function(cm) {
314
_killRegion(cm, false) || killTo(cm, byChar, 1, false);
317
cmds.deleteBackwardChar = function(cm) {
318
_killRegion(cm, false) || killTo(cm, byChar, -1, false);
321
cmds.forwardWord = move(byWord, 1);
323
cmds.backwardWord = move(byWord, -1);
325
cmds.killWord = function(cm) { killTo(cm, byWord, 1, "grow"); };
327
cmds.backwardKillWord = function(cm) { killTo(cm, byWord, -1, "grow"); };
329
cmds.nextLine = move(byLine, 1);
331
cmds.previousLine = move(byLine, -1);
333
cmds.scrollDownCommand = move(byPage, -1);
335
cmds.scrollUpCommand = move(byPage, 1);
337
cmds.backwardParagraph = move(byParagraph, -1);
339
cmds.forwardParagraph = move(byParagraph, 1);
341
cmds.backwardSentence = move(bySentence, -1);
343
cmds.forwardSentence = move(bySentence, 1);
345
cmds.killSentence = function(cm) { killTo(cm, bySentence, 1, "grow"); };
347
cmds.backwardKillSentence = function(cm) {
348
_kill(cm, cm.getCursor(), bySentence(cm, cm.getCursor(), 1), "grow");
351
cmds.killSexp = function(cm) { killTo(cm, byExpr, 1, "grow"); };
353
cmds.backwardKillSexp = function(cm) { killTo(cm, byExpr, -1, "grow"); };
355
cmds.forwardSexp = move(byExpr, 1);
357
cmds.backwardSexp = move(byExpr, -1);
359
cmds.markSexp = function(cm) {
360
var cursor = cm.getCursor();
361
cm.setSelection(findEnd(cm, cursor, byExpr, 1), cursor);
364
cmds.transposeSexps = function(cm) {
365
var leftStart = byExpr(cm, cm.getCursor(), -1);
366
var leftEnd = byExpr(cm, leftStart, 1);
367
var rightEnd = byExpr(cm, leftEnd, 1);
368
var rightStart = byExpr(cm, rightEnd, -1);
369
cm.replaceRange(cm.getRange(rightStart, rightEnd) +
370
cm.getRange(leftEnd, rightStart) +
371
cm.getRange(leftStart, leftEnd), leftStart, rightEnd);
374
cmds.backwardUpList = repeated(toEnclosingExpr);
376
cmds.justOneSpace = function(cm) {
377
var pos = cm.getCursor(), from = pos.ch;
378
var to = pos.ch, text = cm.getLine(pos.line);
379
while (from && /\s/.test(text.charAt(from - 1))) --from;
380
while (to < text.length && /\s/.test(text.charAt(to))) ++to;
381
cm.replaceRange(" ", Pos(pos.line, from), Pos(pos.line, to));
384
cmds.openLine = repeated(function(cm) {
385
cm.replaceSelection("\n", "start");
389
cmds.transposeCharsRepeatable = repeated(function(cm) {
390
cm.execCommand("transposeChars");
393
cmds.capitalizeWord = repeated(function(cm) {
394
operateOnWord(cm, function(w) {
395
var letter = w.search(/\w/);
396
if (letter == -1) return w;
397
return w.slice(0, letter) + w.charAt(letter).toUpperCase() +
398
w.slice(letter + 1).toLowerCase();
402
cmds.upcaseWord = repeated(function(cm) {
403
operateOnWord(cm, function(w) { return w.toUpperCase(); });
406
cmds.downcaseWord = repeated(function(cm) {
407
operateOnWord(cm, function(w) { return w.toLowerCase(); });
411
cmds.undoRepeatable = repeated("undo");
413
cmds.keyboardQuit = function(cm) {
414
cm.execCommand("clearSearch");
418
cmds.newline = repeated(function(cm) { cm.replaceSelection("\n", "end"); });
420
cmds.gotoLine = function(cm) {
421
var prefix = getPrefix(cm, true);
422
if (prefix != null && prefix > 0) return cm.setCursor(prefix - 1);
424
getInput(cm, "Goto line", function(str) {
426
if (str && !isNaN(num = Number(str)) && num == (num|0) && num > 0)
427
cm.setCursor(num - 1);
431
cmds.indentRigidly = function(cm) {
432
cm.indentSelection(getPrefix(cm, true) || cm.getOption("indentUnit"));
435
cmds.exchangePointAndMark = function(cm) {
436
cm.setSelection(cm.getCursor("head"), cm.getCursor("anchor"));
439
cmds.quotedInsertTab = repeated("insertTab");
441
cmds.universalArgument = function addPrefixMap(cm) {
442
cm.state.emacsPrefixMap = true;
443
cm.addKeyMap(prefixMap);
444
cm.on("keyHandled", maybeRemovePrefixMap);
445
cm.on("inputRead", maybeRemovePrefixMap);
448
CodeMirror.emacs = {kill: _kill, killRegion: _killRegion, repeated: repeated};
451
var keyMap = CodeMirror.keyMap.emacs = CodeMirror.normalizeKeyMap({
452
"Ctrl-W": "killRegion",
453
"Ctrl-K": "killLineEmacs",
454
"Alt-W": "killRingSave",
457
"Ctrl-Space": "setMark",
458
"Ctrl-Shift-2": "setMark",
459
"Ctrl-F": "forwardChar",
460
"Ctrl-B": "backwardChar",
461
"Right": "forwardChar",
462
"Left": "backwardChar",
463
"Ctrl-D": "deleteChar",
464
"Delete": "deleteForwardChar",
465
"Ctrl-H": "deleteBackwardChar",
466
"Backspace": "deleteBackwardChar",
467
"Alt-F": "forwardWord",
468
"Alt-B": "backwardWord",
469
"Alt-Right": "forwardWord",
470
"Alt-Left": "backwardWord",
472
"Alt-Backspace": "backwardKillWord",
473
"Ctrl-N": "nextLine",
474
"Ctrl-P": "previousLine",
476
"Up": "previousLine",
477
"Ctrl-A": "goLineStart",
478
"Ctrl-E": "goLineEnd",
480
"Home": "goLineStart",
481
"Alt-V": "scrollDownCommand",
482
"Ctrl-V": "scrollUpCommand",
483
"PageUp": "scrollDownCommand",
484
"PageDown": "scrollUpCommand",
485
"Ctrl-Up": "backwardParagraph",
486
"Ctrl-Down": "forwardParagraph",
487
"Alt-{": "backwardParagraph",
488
"Alt-}": "forwardParagraph",
489
"Alt-A": "backwardSentence",
490
"Alt-E": "forwardSentence",
491
"Alt-K": "killSentence",
492
"Ctrl-X Delete": "backwardKillSentence",
493
"Ctrl-Alt-K": "killSexp",
494
"Ctrl-Alt-Backspace": "backwardKillSexp",
495
"Ctrl-Alt-F": "forwardSexp",
496
"Ctrl-Alt-B": "backwardSexp",
497
"Shift-Ctrl-Alt-2": "markSexp",
498
"Ctrl-Alt-T": "transposeSexps",
499
"Ctrl-Alt-U": "backwardUpList",
500
"Alt-Space": "justOneSpace",
501
"Ctrl-O": "openLine",
502
"Ctrl-T": "transposeCharsRepeatable",
503
"Alt-C": "capitalizeWord",
504
"Alt-U": "upcaseWord",
505
"Alt-L": "downcaseWord",
506
"Alt-;": "toggleComment",
507
"Ctrl-/": "undoRepeatable",
508
"Shift-Ctrl--": "undoRepeatable",
509
"Ctrl-Z": "undoRepeatable",
510
"Cmd-Z": "undoRepeatable",
511
"Ctrl-X U": "undoRepeatable",
512
"Shift-Ctrl-Z": "redo",
513
"Shift-Alt-,": "goDocStart",
514
"Shift-Alt-.": "goDocEnd",
515
"Ctrl-S": "findPersistentNext",
516
"Ctrl-R": "findPersistentPrev",
517
"Ctrl-G": "keyboardQuit",
518
"Shift-Alt-5": "replace",
519
"Alt-/": "autocomplete",
520
"Enter": "newlineAndIndent",
523
"Alt-G G": "gotoLine",
524
"Ctrl-X Tab": "indentRigidly",
525
"Ctrl-X Ctrl-X": "exchangePointAndMark",
526
"Ctrl-X Ctrl-S": "save",
527
"Ctrl-X Ctrl-W": "save",
528
"Ctrl-X S": "saveAll",
531
"Ctrl-X H": "selectAll",
532
"Ctrl-Q Tab": "quotedInsertTab",
533
"Ctrl-U": "universalArgument",
534
"fallthrough": "default"
537
var prefixMap = {"Ctrl-G": clearPrefix};
538
function regPrefix(d) {
539
prefixMap[d] = function(cm) { addPrefix(cm, d); };
540
keyMap["Ctrl-" + d] = function(cm) { addPrefix(cm, d); };
541
prefixPreservingKeys["Ctrl-" + d] = true;
543
for (var i = 0; i < 10; ++i) regPrefix(String(i));