browser/devtools/sourceeditor/codemirror/keymap/emacs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function(mod) {
michael@0 2 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 3 mod(require("../lib/codemirror"));
michael@0 4 else if (typeof define == "function" && define.amd) // AMD
michael@0 5 define(["../lib/codemirror"], mod);
michael@0 6 else // Plain browser env
michael@0 7 mod(CodeMirror);
michael@0 8 })(function(CodeMirror) {
michael@0 9 "use strict";
michael@0 10
michael@0 11 var Pos = CodeMirror.Pos;
michael@0 12 function posEq(a, b) { return a.line == b.line && a.ch == b.ch; }
michael@0 13
michael@0 14 // Kill 'ring'
michael@0 15
michael@0 16 var killRing = [];
michael@0 17 function addToRing(str) {
michael@0 18 killRing.push(str);
michael@0 19 if (killRing.length > 50) killRing.shift();
michael@0 20 }
michael@0 21 function growRingTop(str) {
michael@0 22 if (!killRing.length) return addToRing(str);
michael@0 23 killRing[killRing.length - 1] += str;
michael@0 24 }
michael@0 25 function getFromRing(n) { return killRing[killRing.length - (n ? Math.min(n, 1) : 1)] || ""; }
michael@0 26 function popFromRing() { if (killRing.length > 1) killRing.pop(); return getFromRing(); }
michael@0 27
michael@0 28 var lastKill = null;
michael@0 29
michael@0 30 function kill(cm, from, to, mayGrow, text) {
michael@0 31 if (text == null) text = cm.getRange(from, to);
michael@0 32
michael@0 33 if (mayGrow && lastKill && lastKill.cm == cm && posEq(from, lastKill.pos) && cm.isClean(lastKill.gen))
michael@0 34 growRingTop(text);
michael@0 35 else
michael@0 36 addToRing(text);
michael@0 37 cm.replaceRange("", from, to, "+delete");
michael@0 38
michael@0 39 if (mayGrow) lastKill = {cm: cm, pos: from, gen: cm.changeGeneration()};
michael@0 40 else lastKill = null;
michael@0 41 }
michael@0 42
michael@0 43 // Boundaries of various units
michael@0 44
michael@0 45 function byChar(cm, pos, dir) {
michael@0 46 return cm.findPosH(pos, dir, "char", true);
michael@0 47 }
michael@0 48
michael@0 49 function byWord(cm, pos, dir) {
michael@0 50 return cm.findPosH(pos, dir, "word", true);
michael@0 51 }
michael@0 52
michael@0 53 function byLine(cm, pos, dir) {
michael@0 54 return cm.findPosV(pos, dir, "line", cm.doc.sel.goalColumn);
michael@0 55 }
michael@0 56
michael@0 57 function byPage(cm, pos, dir) {
michael@0 58 return cm.findPosV(pos, dir, "page", cm.doc.sel.goalColumn);
michael@0 59 }
michael@0 60
michael@0 61 function byParagraph(cm, pos, dir) {
michael@0 62 var no = pos.line, line = cm.getLine(no);
michael@0 63 var sawText = /\S/.test(dir < 0 ? line.slice(0, pos.ch) : line.slice(pos.ch));
michael@0 64 var fst = cm.firstLine(), lst = cm.lastLine();
michael@0 65 for (;;) {
michael@0 66 no += dir;
michael@0 67 if (no < fst || no > lst)
michael@0 68 return cm.clipPos(Pos(no - dir, dir < 0 ? 0 : null));
michael@0 69 line = cm.getLine(no);
michael@0 70 var hasText = /\S/.test(line);
michael@0 71 if (hasText) sawText = true;
michael@0 72 else if (sawText) return Pos(no, 0);
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 function bySentence(cm, pos, dir) {
michael@0 77 var line = pos.line, ch = pos.ch;
michael@0 78 var text = cm.getLine(pos.line), sawWord = false;
michael@0 79 for (;;) {
michael@0 80 var next = text.charAt(ch + (dir < 0 ? -1 : 0));
michael@0 81 if (!next) { // End/beginning of line reached
michael@0 82 if (line == (dir < 0 ? cm.firstLine() : cm.lastLine())) return Pos(line, ch);
michael@0 83 text = cm.getLine(line + dir);
michael@0 84 if (!/\S/.test(text)) return Pos(line, ch);
michael@0 85 line += dir;
michael@0 86 ch = dir < 0 ? text.length : 0;
michael@0 87 continue;
michael@0 88 }
michael@0 89 if (sawWord && /[!?.]/.test(next)) return Pos(line, ch + (dir > 0 ? 1 : 0));
michael@0 90 if (!sawWord) sawWord = /\w/.test(next);
michael@0 91 ch += dir;
michael@0 92 }
michael@0 93 }
michael@0 94
michael@0 95 function byExpr(cm, pos, dir) {
michael@0 96 var wrap;
michael@0 97 if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, true))
michael@0 98 && wrap.match && (wrap.forward ? 1 : -1) == dir)
michael@0 99 return dir > 0 ? Pos(wrap.to.line, wrap.to.ch + 1) : wrap.to;
michael@0 100
michael@0 101 for (var first = true;; first = false) {
michael@0 102 var token = cm.getTokenAt(pos);
michael@0 103 var after = Pos(pos.line, dir < 0 ? token.start : token.end);
michael@0 104 if (first && dir > 0 && token.end == pos.ch || !/\w/.test(token.string)) {
michael@0 105 var newPos = cm.findPosH(after, dir, "char");
michael@0 106 if (posEq(after, newPos)) return pos;
michael@0 107 else pos = newPos;
michael@0 108 } else {
michael@0 109 return after;
michael@0 110 }
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 // Prefixes (only crudely supported)
michael@0 115
michael@0 116 function getPrefix(cm, precise) {
michael@0 117 var digits = cm.state.emacsPrefix;
michael@0 118 if (!digits) return precise ? null : 1;
michael@0 119 clearPrefix(cm);
michael@0 120 return digits == "-" ? -1 : Number(digits);
michael@0 121 }
michael@0 122
michael@0 123 function repeated(cmd) {
michael@0 124 var f = typeof cmd == "string" ? function(cm) { cm.execCommand(cmd); } : cmd;
michael@0 125 return function(cm) {
michael@0 126 var prefix = getPrefix(cm);
michael@0 127 f(cm);
michael@0 128 for (var i = 1; i < prefix; ++i) f(cm);
michael@0 129 };
michael@0 130 }
michael@0 131
michael@0 132 function findEnd(cm, by, dir) {
michael@0 133 var pos = cm.getCursor(), prefix = getPrefix(cm);
michael@0 134 if (prefix < 0) { dir = -dir; prefix = -prefix; }
michael@0 135 for (var i = 0; i < prefix; ++i) {
michael@0 136 var newPos = by(cm, pos, dir);
michael@0 137 if (posEq(newPos, pos)) break;
michael@0 138 pos = newPos;
michael@0 139 }
michael@0 140 return pos;
michael@0 141 }
michael@0 142
michael@0 143 function move(by, dir) {
michael@0 144 var f = function(cm) {
michael@0 145 cm.extendSelection(findEnd(cm, by, dir));
michael@0 146 };
michael@0 147 f.motion = true;
michael@0 148 return f;
michael@0 149 }
michael@0 150
michael@0 151 function killTo(cm, by, dir) {
michael@0 152 kill(cm, cm.getCursor(), findEnd(cm, by, dir), true);
michael@0 153 }
michael@0 154
michael@0 155 function addPrefix(cm, digit) {
michael@0 156 if (cm.state.emacsPrefix) {
michael@0 157 if (digit != "-") cm.state.emacsPrefix += digit;
michael@0 158 return;
michael@0 159 }
michael@0 160 // Not active yet
michael@0 161 cm.state.emacsPrefix = digit;
michael@0 162 cm.on("keyHandled", maybeClearPrefix);
michael@0 163 cm.on("inputRead", maybeDuplicateInput);
michael@0 164 }
michael@0 165
michael@0 166 var prefixPreservingKeys = {"Alt-G": true, "Ctrl-X": true, "Ctrl-Q": true, "Ctrl-U": true};
michael@0 167
michael@0 168 function maybeClearPrefix(cm, arg) {
michael@0 169 if (!cm.state.emacsPrefixMap && !prefixPreservingKeys.hasOwnProperty(arg))
michael@0 170 clearPrefix(cm);
michael@0 171 }
michael@0 172
michael@0 173 function clearPrefix(cm) {
michael@0 174 cm.state.emacsPrefix = null;
michael@0 175 cm.off("keyHandled", maybeClearPrefix);
michael@0 176 cm.off("inputRead", maybeDuplicateInput);
michael@0 177 }
michael@0 178
michael@0 179 function maybeDuplicateInput(cm, event) {
michael@0 180 var dup = getPrefix(cm);
michael@0 181 if (dup > 1 && event.origin == "+input") {
michael@0 182 var one = event.text.join("\n"), txt = "";
michael@0 183 for (var i = 1; i < dup; ++i) txt += one;
michael@0 184 cm.replaceSelection(txt);
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 function addPrefixMap(cm) {
michael@0 189 cm.state.emacsPrefixMap = true;
michael@0 190 cm.addKeyMap(prefixMap);
michael@0 191 cm.on("keyHandled", maybeRemovePrefixMap);
michael@0 192 cm.on("inputRead", maybeRemovePrefixMap);
michael@0 193 }
michael@0 194
michael@0 195 function maybeRemovePrefixMap(cm, arg) {
michael@0 196 if (typeof arg == "string" && (/^\d$/.test(arg) || arg == "Ctrl-U")) return;
michael@0 197 cm.removeKeyMap(prefixMap);
michael@0 198 cm.state.emacsPrefixMap = false;
michael@0 199 cm.off("keyHandled", maybeRemovePrefixMap);
michael@0 200 cm.off("inputRead", maybeRemovePrefixMap);
michael@0 201 }
michael@0 202
michael@0 203 // Utilities
michael@0 204
michael@0 205 function setMark(cm) {
michael@0 206 cm.setCursor(cm.getCursor());
michael@0 207 cm.setExtending(!cm.getExtending());
michael@0 208 cm.on("change", function() { cm.setExtending(false); });
michael@0 209 }
michael@0 210
michael@0 211 function clearMark(cm) {
michael@0 212 cm.setExtending(false);
michael@0 213 cm.setCursor(cm.getCursor());
michael@0 214 }
michael@0 215
michael@0 216 function getInput(cm, msg, f) {
michael@0 217 if (cm.openDialog)
michael@0 218 cm.openDialog(msg + ": <input type=\"text\" style=\"width: 10em\"/>", f, {bottom: true});
michael@0 219 else
michael@0 220 f(prompt(msg, ""));
michael@0 221 }
michael@0 222
michael@0 223 function operateOnWord(cm, op) {
michael@0 224 var start = cm.getCursor(), end = cm.findPosH(start, 1, "word");
michael@0 225 cm.replaceRange(op(cm.getRange(start, end)), start, end);
michael@0 226 cm.setCursor(end);
michael@0 227 }
michael@0 228
michael@0 229 function toEnclosingExpr(cm) {
michael@0 230 var pos = cm.getCursor(), line = pos.line, ch = pos.ch;
michael@0 231 var stack = [];
michael@0 232 while (line >= cm.firstLine()) {
michael@0 233 var text = cm.getLine(line);
michael@0 234 for (var i = ch == null ? text.length : ch; i > 0;) {
michael@0 235 var ch = text.charAt(--i);
michael@0 236 if (ch == ")")
michael@0 237 stack.push("(");
michael@0 238 else if (ch == "]")
michael@0 239 stack.push("[");
michael@0 240 else if (ch == "}")
michael@0 241 stack.push("{");
michael@0 242 else if (/[\(\{\[]/.test(ch) && (!stack.length || stack.pop() != ch))
michael@0 243 return cm.extendSelection(Pos(line, i));
michael@0 244 }
michael@0 245 --line; ch = null;
michael@0 246 }
michael@0 247 }
michael@0 248
michael@0 249 function quit(cm) {
michael@0 250 cm.execCommand("clearSearch");
michael@0 251 clearMark(cm);
michael@0 252 }
michael@0 253
michael@0 254 // Actual keymap
michael@0 255
michael@0 256 var keyMap = CodeMirror.keyMap.emacs = {
michael@0 257 "Ctrl-W": function(cm) {kill(cm, cm.getCursor("start"), cm.getCursor("end"));},
michael@0 258 "Ctrl-K": repeated(function(cm) {
michael@0 259 var start = cm.getCursor(), end = cm.clipPos(Pos(start.line));
michael@0 260 var text = cm.getRange(start, end);
michael@0 261 if (!/\S/.test(text)) {
michael@0 262 text += "\n";
michael@0 263 end = Pos(start.line + 1, 0);
michael@0 264 }
michael@0 265 kill(cm, start, end, true, text);
michael@0 266 }),
michael@0 267 "Alt-W": function(cm) {
michael@0 268 addToRing(cm.getSelection());
michael@0 269 clearMark(cm);
michael@0 270 },
michael@0 271 "Ctrl-Y": function(cm) {
michael@0 272 var start = cm.getCursor();
michael@0 273 cm.replaceRange(getFromRing(getPrefix(cm)), start, start, "paste");
michael@0 274 cm.setSelection(start, cm.getCursor());
michael@0 275 },
michael@0 276 "Alt-Y": function(cm) {cm.replaceSelection(popFromRing(), "around", "paste");},
michael@0 277
michael@0 278 "Ctrl-Space": setMark, "Ctrl-Shift-2": setMark,
michael@0 279
michael@0 280 "Ctrl-F": move(byChar, 1), "Ctrl-B": move(byChar, -1),
michael@0 281 "Right": move(byChar, 1), "Left": move(byChar, -1),
michael@0 282 "Ctrl-D": function(cm) { killTo(cm, byChar, 1); },
michael@0 283 "Delete": function(cm) { killTo(cm, byChar, 1); },
michael@0 284 "Ctrl-H": function(cm) { killTo(cm, byChar, -1); },
michael@0 285 "Backspace": function(cm) { killTo(cm, byChar, -1); },
michael@0 286
michael@0 287 "Alt-F": move(byWord, 1), "Alt-B": move(byWord, -1),
michael@0 288 "Alt-D": function(cm) { killTo(cm, byWord, 1); },
michael@0 289 "Alt-Backspace": function(cm) { killTo(cm, byWord, -1); },
michael@0 290
michael@0 291 "Ctrl-N": move(byLine, 1), "Ctrl-P": move(byLine, -1),
michael@0 292 "Down": move(byLine, 1), "Up": move(byLine, -1),
michael@0 293 "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd",
michael@0 294 "End": "goLineEnd", "Home": "goLineStart",
michael@0 295
michael@0 296 "Alt-V": move(byPage, -1), "Ctrl-V": move(byPage, 1),
michael@0 297 "PageUp": move(byPage, -1), "PageDown": move(byPage, 1),
michael@0 298
michael@0 299 "Ctrl-Up": move(byParagraph, -1), "Ctrl-Down": move(byParagraph, 1),
michael@0 300
michael@0 301 "Alt-A": move(bySentence, -1), "Alt-E": move(bySentence, 1),
michael@0 302 "Alt-K": function(cm) { killTo(cm, bySentence, 1); },
michael@0 303
michael@0 304 "Ctrl-Alt-K": function(cm) { killTo(cm, byExpr, 1); },
michael@0 305 "Ctrl-Alt-Backspace": function(cm) { killTo(cm, byExpr, -1); },
michael@0 306 "Ctrl-Alt-F": move(byExpr, 1), "Ctrl-Alt-B": move(byExpr, -1),
michael@0 307
michael@0 308 "Shift-Ctrl-Alt-2": function(cm) {
michael@0 309 cm.setSelection(findEnd(cm, byExpr, 1), cm.getCursor());
michael@0 310 },
michael@0 311 "Ctrl-Alt-T": function(cm) {
michael@0 312 var leftStart = byExpr(cm, cm.getCursor(), -1), leftEnd = byExpr(cm, leftStart, 1);
michael@0 313 var rightEnd = byExpr(cm, leftEnd, 1), rightStart = byExpr(cm, rightEnd, -1);
michael@0 314 cm.replaceRange(cm.getRange(rightStart, rightEnd) + cm.getRange(leftEnd, rightStart) +
michael@0 315 cm.getRange(leftStart, leftEnd), leftStart, rightEnd);
michael@0 316 },
michael@0 317 "Ctrl-Alt-U": repeated(toEnclosingExpr),
michael@0 318
michael@0 319 "Alt-Space": function(cm) {
michael@0 320 var pos = cm.getCursor(), from = pos.ch, to = pos.ch, text = cm.getLine(pos.line);
michael@0 321 while (from && /\s/.test(text.charAt(from - 1))) --from;
michael@0 322 while (to < text.length && /\s/.test(text.charAt(to))) ++to;
michael@0 323 cm.replaceRange(" ", Pos(pos.line, from), Pos(pos.line, to));
michael@0 324 },
michael@0 325 "Ctrl-O": repeated(function(cm) { cm.replaceSelection("\n", "start"); }),
michael@0 326 "Ctrl-T": repeated(function(cm) {
michael@0 327 var pos = cm.getCursor();
michael@0 328 if (pos.ch < cm.getLine(pos.line).length) pos = Pos(pos.line, pos.ch + 1);
michael@0 329 var from = cm.findPosH(pos, -2, "char");
michael@0 330 var range = cm.getRange(from, pos);
michael@0 331 if (range.length != 2) return;
michael@0 332 cm.setSelection(from, pos);
michael@0 333 cm.replaceSelection(range.charAt(1) + range.charAt(0), null, "+transpose");
michael@0 334 }),
michael@0 335
michael@0 336 "Alt-C": repeated(function(cm) {
michael@0 337 operateOnWord(cm, function(w) {
michael@0 338 var letter = w.search(/\w/);
michael@0 339 if (letter == -1) return w;
michael@0 340 return w.slice(0, letter) + w.charAt(letter).toUpperCase() + w.slice(letter + 1).toLowerCase();
michael@0 341 });
michael@0 342 }),
michael@0 343 "Alt-U": repeated(function(cm) {
michael@0 344 operateOnWord(cm, function(w) { return w.toUpperCase(); });
michael@0 345 }),
michael@0 346 "Alt-L": repeated(function(cm) {
michael@0 347 operateOnWord(cm, function(w) { return w.toLowerCase(); });
michael@0 348 }),
michael@0 349
michael@0 350 "Alt-;": "toggleComment",
michael@0 351
michael@0 352 "Ctrl-/": repeated("undo"), "Shift-Ctrl--": repeated("undo"),
michael@0 353 "Ctrl-Z": repeated("undo"), "Cmd-Z": repeated("undo"),
michael@0 354 "Shift-Alt-,": "goDocStart", "Shift-Alt-.": "goDocEnd",
michael@0 355 "Ctrl-S": "findNext", "Ctrl-R": "findPrev", "Ctrl-G": quit, "Shift-Alt-5": "replace",
michael@0 356 "Alt-/": "autocomplete",
michael@0 357 "Ctrl-J": "newlineAndIndent", "Enter": false, "Tab": "indentAuto",
michael@0 358
michael@0 359 "Alt-G": function(cm) {cm.setOption("keyMap", "emacs-Alt-G");},
michael@0 360 "Ctrl-X": function(cm) {cm.setOption("keyMap", "emacs-Ctrl-X");},
michael@0 361 "Ctrl-Q": function(cm) {cm.setOption("keyMap", "emacs-Ctrl-Q");},
michael@0 362 "Ctrl-U": addPrefixMap
michael@0 363 };
michael@0 364
michael@0 365 CodeMirror.keyMap["emacs-Ctrl-X"] = {
michael@0 366 "Tab": function(cm) {
michael@0 367 cm.indentSelection(getPrefix(cm, true) || cm.getOption("indentUnit"));
michael@0 368 },
michael@0 369 "Ctrl-X": function(cm) {
michael@0 370 cm.setSelection(cm.getCursor("head"), cm.getCursor("anchor"));
michael@0 371 },
michael@0 372
michael@0 373 "Ctrl-S": "save", "Ctrl-W": "save", "S": "saveAll", "F": "open", "U": repeated("undo"), "K": "close",
michael@0 374 "Delete": function(cm) { kill(cm, cm.getCursor(), bySentence(cm, cm.getCursor(), 1), true); },
michael@0 375 auto: "emacs", nofallthrough: true, disableInput: true
michael@0 376 };
michael@0 377
michael@0 378 CodeMirror.keyMap["emacs-Alt-G"] = {
michael@0 379 "G": function(cm) {
michael@0 380 var prefix = getPrefix(cm, true);
michael@0 381 if (prefix != null && prefix > 0) return cm.setCursor(prefix - 1);
michael@0 382
michael@0 383 getInput(cm, "Goto line", function(str) {
michael@0 384 var num;
michael@0 385 if (str && !isNaN(num = Number(str)) && num == num|0 && num > 0)
michael@0 386 cm.setCursor(num - 1);
michael@0 387 });
michael@0 388 },
michael@0 389 auto: "emacs", nofallthrough: true, disableInput: true
michael@0 390 };
michael@0 391
michael@0 392 CodeMirror.keyMap["emacs-Ctrl-Q"] = {
michael@0 393 "Tab": repeated("insertTab"),
michael@0 394 auto: "emacs", nofallthrough: true
michael@0 395 };
michael@0 396
michael@0 397 var prefixMap = {"Ctrl-G": clearPrefix};
michael@0 398 function regPrefix(d) {
michael@0 399 prefixMap[d] = function(cm) { addPrefix(cm, d); };
michael@0 400 keyMap["Ctrl-" + d] = function(cm) { addPrefix(cm, d); };
michael@0 401 prefixPreservingKeys["Ctrl-" + d] = true;
michael@0 402 }
michael@0 403 for (var i = 0; i < 10; ++i) regPrefix(String(i));
michael@0 404 regPrefix("-");
michael@0 405 });

mercurial