browser/devtools/sourceeditor/codemirror/keymap/sublime.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 // A rough approximation of Sublime Text's keybindings
michael@0 2 // Depends on addon/search/searchcursor.js and optionally addon/dialog/dialogs.js
michael@0 3
michael@0 4 (function(mod) {
michael@0 5 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 6 mod(require("../lib/codemirror"), require("../addon/search/searchcursor"), require("../addon/edit/matchbrackets"));
michael@0 7 else if (typeof define == "function" && define.amd) // AMD
michael@0 8 define(["../lib/codemirror", "../addon/search/searchcursor", "../addon/edit/matchbrackets"], mod);
michael@0 9 else // Plain browser env
michael@0 10 mod(CodeMirror);
michael@0 11 })(function(CodeMirror) {
michael@0 12 "use strict";
michael@0 13
michael@0 14 var map = CodeMirror.keyMap.sublime = {fallthrough: "default"};
michael@0 15 var cmds = CodeMirror.commands;
michael@0 16 var Pos = CodeMirror.Pos;
michael@0 17 var ctrl = CodeMirror.keyMap["default"] == CodeMirror.keyMap.pcDefault ? "Ctrl-" : "Cmd-";
michael@0 18
michael@0 19 // This is not exactly Sublime's algorithm. I couldn't make heads or tails of that.
michael@0 20 function findPosSubword(doc, start, dir) {
michael@0 21 if (dir < 0 && start.ch == 0) return doc.clipPos(Pos(start.line - 1));
michael@0 22 var line = doc.getLine(start.line);
michael@0 23 if (dir > 0 && start.ch >= line.length) return doc.clipPos(Pos(start.line + 1, 0));
michael@0 24 var state = "start", type;
michael@0 25 for (var pos = start.ch, e = dir < 0 ? 0 : line.length, i = 0; pos != e; pos += dir, i++) {
michael@0 26 var next = line.charAt(dir < 0 ? pos - 1 : pos);
michael@0 27 var cat = next != "_" && CodeMirror.isWordChar(next) ? "w" : "o";
michael@0 28 if (cat == "w" && next.toUpperCase() == next) cat = "W";
michael@0 29 if (state == "start") {
michael@0 30 if (cat != "o") { state = "in"; type = cat; }
michael@0 31 } else if (state == "in") {
michael@0 32 if (type != cat) {
michael@0 33 if (type == "w" && cat == "W" && dir < 0) pos--;
michael@0 34 if (type == "W" && cat == "w" && dir > 0) { type = "w"; continue; }
michael@0 35 break;
michael@0 36 }
michael@0 37 }
michael@0 38 }
michael@0 39 return Pos(start.line, pos);
michael@0 40 }
michael@0 41
michael@0 42 function moveSubword(cm, dir) {
michael@0 43 cm.extendSelectionsBy(function(range) {
michael@0 44 if (cm.display.shift || cm.doc.extend || range.empty())
michael@0 45 return findPosSubword(cm.doc, range.head, dir);
michael@0 46 else
michael@0 47 return dir < 0 ? range.from() : range.to();
michael@0 48 });
michael@0 49 }
michael@0 50
michael@0 51 cmds[map["Alt-Left"] = "goSubwordLeft"] = function(cm) { moveSubword(cm, -1); };
michael@0 52 cmds[map["Alt-Right"] = "goSubwordRight"] = function(cm) { moveSubword(cm, 1); };
michael@0 53
michael@0 54 cmds[map[ctrl + "Up"] = "scrollLineUp"] = function(cm) {
michael@0 55 cm.scrollTo(null, cm.getScrollInfo().top - cm.defaultTextHeight());
michael@0 56 };
michael@0 57 cmds[map[ctrl + "Down"] = "scrollLineDown"] = function(cm) {
michael@0 58 cm.scrollTo(null, cm.getScrollInfo().top + cm.defaultTextHeight());
michael@0 59 };
michael@0 60
michael@0 61 cmds[map["Shift-" + ctrl + "L"] = "splitSelectionByLine"] = function(cm) {
michael@0 62 var ranges = cm.listSelections(), lineRanges = [];
michael@0 63 for (var i = 0; i < ranges.length; i++) {
michael@0 64 var from = ranges[i].from(), to = ranges[i].to();
michael@0 65 for (var line = from.line; line <= to.line; ++line)
michael@0 66 if (!(to.line > from.line && line == to.line && to.ch == 0))
michael@0 67 lineRanges.push({anchor: line == from.line ? from : Pos(line, 0),
michael@0 68 head: line == to.line ? to : Pos(line)});
michael@0 69 }
michael@0 70 cm.setSelections(lineRanges, 0);
michael@0 71 };
michael@0 72
michael@0 73 map["Shift-Tab"] = "indentLess";
michael@0 74
michael@0 75 cmds[map["Esc"] = "singleSelectionTop"] = function(cm) {
michael@0 76 var range = cm.listSelections()[0];
michael@0 77 cm.setSelection(range.anchor, range.head, {scroll: false});
michael@0 78 };
michael@0 79
michael@0 80 cmds[map[ctrl + "L"] = "selectLine"] = function(cm) {
michael@0 81 var ranges = cm.listSelections(), extended = [];
michael@0 82 for (var i = 0; i < ranges.length; i++) {
michael@0 83 var range = ranges[i];
michael@0 84 extended.push({anchor: Pos(range.from().line, 0),
michael@0 85 head: Pos(range.to().line + 1, 0)});
michael@0 86 }
michael@0 87 cm.setSelections(extended);
michael@0 88 };
michael@0 89
michael@0 90 map["Shift-" + ctrl + "K"] = "deleteLine";
michael@0 91
michael@0 92 function insertLine(cm, above) {
michael@0 93 cm.operation(function() {
michael@0 94 var len = cm.listSelections().length, newSelection = [], last = -1;
michael@0 95 for (var i = 0; i < len; i++) {
michael@0 96 var head = cm.listSelections()[i].head;
michael@0 97 if (head.line <= last) continue;
michael@0 98 var at = Pos(head.line + (above ? 0 : 1), 0);
michael@0 99 cm.replaceRange("\n", at, null, "+insertLine");
michael@0 100 cm.indentLine(at.line, null, true);
michael@0 101 newSelection.push({head: at, anchor: at});
michael@0 102 last = head.line + 1;
michael@0 103 }
michael@0 104 cm.setSelections(newSelection);
michael@0 105 });
michael@0 106 }
michael@0 107
michael@0 108 cmds[map[ctrl + "Enter"] = "insertLineAfter"] = function(cm) { insertLine(cm, false); };
michael@0 109
michael@0 110 cmds[map["Shift-" + ctrl + "Enter"] = "insertLineBefore"] = function(cm) { insertLine(cm, true); };
michael@0 111
michael@0 112 function wordAt(cm, pos) {
michael@0 113 var start = pos.ch, end = start, line = cm.getLine(pos.line);
michael@0 114 while (start && CodeMirror.isWordChar(line.charAt(start - 1))) --start;
michael@0 115 while (end < line.length && CodeMirror.isWordChar(line.charAt(end))) ++end;
michael@0 116 return {from: Pos(pos.line, start), to: Pos(pos.line, end), word: line.slice(start, end)};
michael@0 117 }
michael@0 118
michael@0 119 cmds[map[ctrl + "D"] = "selectNextOccurrence"] = function(cm) {
michael@0 120 var from = cm.getCursor("from"), to = cm.getCursor("to");
michael@0 121 var fullWord = cm.state.sublimeFindFullWord == cm.doc.sel;
michael@0 122 if (CodeMirror.cmpPos(from, to) == 0) {
michael@0 123 var word = wordAt(cm, from);
michael@0 124 if (!word.word) return;
michael@0 125 cm.setSelection(word.from, word.to);
michael@0 126 fullWord = true;
michael@0 127 } else {
michael@0 128 var text = cm.getRange(from, to);
michael@0 129 var query = fullWord ? new RegExp("\\b" + text + "\\b") : text;
michael@0 130 var cur = cm.getSearchCursor(query, to);
michael@0 131 if (cur.findNext()) {
michael@0 132 cm.addSelection(cur.from(), cur.to());
michael@0 133 } else {
michael@0 134 cur = cm.getSearchCursor(query, Pos(cm.firstLine(), 0));
michael@0 135 if (cur.findNext())
michael@0 136 cm.addSelection(cur.from(), cur.to());
michael@0 137 }
michael@0 138 }
michael@0 139 if (fullWord)
michael@0 140 cm.state.sublimeFindFullWord = cm.doc.sel;
michael@0 141 };
michael@0 142
michael@0 143 var mirror = "(){}[]";
michael@0 144 function selectBetweenBrackets(cm) {
michael@0 145 var pos = cm.getCursor(), opening = cm.scanForBracket(pos, -1);
michael@0 146 if (!opening) return;
michael@0 147 for (;;) {
michael@0 148 var closing = cm.scanForBracket(pos, 1);
michael@0 149 if (!closing) return;
michael@0 150 if (closing.ch == mirror.charAt(mirror.indexOf(opening.ch) + 1)) {
michael@0 151 cm.setSelection(Pos(opening.pos.line, opening.pos.ch + 1), closing.pos, false);
michael@0 152 return true;
michael@0 153 }
michael@0 154 pos = Pos(closing.pos.line, closing.pos.ch + 1);
michael@0 155 }
michael@0 156 }
michael@0 157
michael@0 158 cmds[map["Shift-" + ctrl + "Space"] = "selectScope"] = function(cm) {
michael@0 159 selectBetweenBrackets(cm) || cm.execCommand("selectAll");
michael@0 160 };
michael@0 161 cmds[map["Shift-" + ctrl + "M"] = "selectBetweenBrackets"] = function(cm) {
michael@0 162 if (!selectBetweenBrackets(cm)) return CodeMirror.Pass;
michael@0 163 };
michael@0 164
michael@0 165 cmds[map[ctrl + "M"] = "goToBracket"] = function(cm) {
michael@0 166 cm.extendSelectionsBy(function(range) {
michael@0 167 var next = cm.scanForBracket(range.head, 1);
michael@0 168 if (next && CodeMirror.cmpPos(next.pos, range.head) != 0) return next.pos;
michael@0 169 var prev = cm.scanForBracket(range.head, -1);
michael@0 170 return prev && Pos(prev.pos.line, prev.pos.ch + 1) || range.head;
michael@0 171 });
michael@0 172 };
michael@0 173
michael@0 174 cmds[map["Shift-" + ctrl + "Up"] = "swapLineUp"] = function(cm) {
michael@0 175 var ranges = cm.listSelections(), linesToMove = [], at = cm.firstLine() - 1;
michael@0 176 for (var i = 0; i < ranges.length; i++) {
michael@0 177 var range = ranges[i], from = range.from().line - 1, to = range.to().line;
michael@0 178 if (from > at) linesToMove.push(from, to);
michael@0 179 else if (linesToMove.length) linesToMove[linesToMove.length - 1] = to;
michael@0 180 at = to;
michael@0 181 }
michael@0 182 cm.operation(function() {
michael@0 183 for (var i = 0; i < linesToMove.length; i += 2) {
michael@0 184 var from = linesToMove[i], to = linesToMove[i + 1];
michael@0 185 var line = cm.getLine(from);
michael@0 186 cm.replaceRange("", Pos(from, 0), Pos(from + 1, 0), "+swapLine");
michael@0 187 if (to > cm.lastLine()) {
michael@0 188 cm.replaceRange("\n" + line, Pos(cm.lastLine()), null, "+swapLine");
michael@0 189 var sels = cm.listSelections(), last = sels[sels.length - 1];
michael@0 190 var head = last.head.line == to ? Pos(to - 1) : last.head;
michael@0 191 var anchor = last.anchor.line == to ? Pos(to - 1) : last.anchor;
michael@0 192 cm.setSelections(sels.slice(0, sels.length - 1).concat([{head: head, anchor: anchor}]));
michael@0 193 } else {
michael@0 194 cm.replaceRange(line + "\n", Pos(to, 0), null, "+swapLine");
michael@0 195 }
michael@0 196 }
michael@0 197 cm.scrollIntoView();
michael@0 198 });
michael@0 199 };
michael@0 200
michael@0 201 cmds[map["Shift-" + ctrl + "Down"] = "swapLineDown"] = function(cm) {
michael@0 202 var ranges = cm.listSelections(), linesToMove = [], at = cm.lastLine() + 1;
michael@0 203 for (var i = ranges.length - 1; i >= 0; i--) {
michael@0 204 var range = ranges[i], from = range.to().line + 1, to = range.from().line;
michael@0 205 if (from < at) linesToMove.push(from, to);
michael@0 206 else if (linesToMove.length) linesToMove[linesToMove.length - 1] = to;
michael@0 207 at = to;
michael@0 208 }
michael@0 209 cm.operation(function() {
michael@0 210 for (var i = linesToMove.length - 2; i >= 0; i -= 2) {
michael@0 211 var from = linesToMove[i], to = linesToMove[i + 1];
michael@0 212 var line = cm.getLine(from);
michael@0 213 if (from == cm.lastLine())
michael@0 214 cm.replaceRange("", Pos(from - 1), Pos(from), "+swapLine");
michael@0 215 else
michael@0 216 cm.replaceRange("", Pos(from, 0), Pos(from + 1, 0), "+swapLine");
michael@0 217 cm.replaceRange(line + "\n", Pos(to, 0), null, "+swapLine");
michael@0 218 }
michael@0 219 cm.scrollIntoView();
michael@0 220 });
michael@0 221 };
michael@0 222
michael@0 223 map[ctrl + "/"] = "toggleComment";
michael@0 224
michael@0 225 cmds[map[ctrl + "J"] = "joinLines"] = function(cm) {
michael@0 226 var ranges = cm.listSelections(), joined = [];
michael@0 227 for (var i = 0; i < ranges.length; i++) {
michael@0 228 var range = ranges[i], from = range.from();
michael@0 229 var start = from.line, end = range.to().line;
michael@0 230 while (i < ranges.length - 1 && ranges[i + 1].from().line == end)
michael@0 231 end = ranges[++i].to().line;
michael@0 232 joined.push({start: start, end: end, anchor: !range.empty() && from});
michael@0 233 }
michael@0 234 cm.operation(function() {
michael@0 235 var offset = 0, ranges = [];
michael@0 236 for (var i = 0; i < joined.length; i++) {
michael@0 237 var obj = joined[i];
michael@0 238 var anchor = obj.anchor && Pos(obj.anchor.line - offset, obj.anchor.ch), head;
michael@0 239 for (var line = obj.start; line <= obj.end; line++) {
michael@0 240 var actual = line - offset;
michael@0 241 if (line == obj.end) head = Pos(actual, cm.getLine(actual).length + 1);
michael@0 242 if (actual < cm.lastLine()) {
michael@0 243 cm.replaceRange(" ", Pos(actual), Pos(actual + 1, /^\s*/.exec(cm.getLine(actual + 1))[0].length));
michael@0 244 ++offset;
michael@0 245 }
michael@0 246 }
michael@0 247 ranges.push({anchor: anchor || head, head: head});
michael@0 248 }
michael@0 249 cm.setSelections(ranges, 0);
michael@0 250 });
michael@0 251 };
michael@0 252
michael@0 253 cmds[map["Shift-" + ctrl + "D"] = "duplicateLine"] = function(cm) {
michael@0 254 cm.operation(function() {
michael@0 255 var rangeCount = cm.listSelections().length;
michael@0 256 for (var i = 0; i < rangeCount; i++) {
michael@0 257 var range = cm.listSelections()[i];
michael@0 258 if (range.empty())
michael@0 259 cm.replaceRange(cm.getLine(range.head.line) + "\n", Pos(range.head.line, 0));
michael@0 260 else
michael@0 261 cm.replaceRange(cm.getRange(range.from(), range.to()), range.from());
michael@0 262 }
michael@0 263 cm.scrollIntoView();
michael@0 264 });
michael@0 265 };
michael@0 266
michael@0 267 map[ctrl + "T"] = "transposeChars";
michael@0 268
michael@0 269 function sortLines(cm, caseSensitive) {
michael@0 270 var ranges = cm.listSelections(), toSort = [], selected;
michael@0 271 for (var i = 0; i < ranges.length; i++) {
michael@0 272 var range = ranges[i];
michael@0 273 if (range.empty()) continue;
michael@0 274 var from = range.from().line, to = range.to().line;
michael@0 275 while (i < ranges.length - 1 && ranges[i + 1].from().line == to)
michael@0 276 to = range[++i].to().line;
michael@0 277 toSort.push(from, to);
michael@0 278 }
michael@0 279 if (toSort.length) selected = true;
michael@0 280 else toSort.push(cm.firstLine(), cm.lastLine());
michael@0 281
michael@0 282 cm.operation(function() {
michael@0 283 var ranges = [];
michael@0 284 for (var i = 0; i < toSort.length; i += 2) {
michael@0 285 var from = toSort[i], to = toSort[i + 1];
michael@0 286 var start = Pos(from, 0), end = Pos(to);
michael@0 287 var lines = cm.getRange(start, end, false);
michael@0 288 if (caseSensitive)
michael@0 289 lines.sort();
michael@0 290 else
michael@0 291 lines.sort(function(a, b) {
michael@0 292 var au = a.toUpperCase(), bu = b.toUpperCase();
michael@0 293 if (au != bu) { a = au; b = bu; }
michael@0 294 return a < b ? -1 : a == b ? 0 : 1;
michael@0 295 });
michael@0 296 cm.replaceRange(lines, start, end);
michael@0 297 if (selected) ranges.push({anchor: start, head: end});
michael@0 298 }
michael@0 299 if (selected) cm.setSelections(ranges, 0);
michael@0 300 });
michael@0 301 }
michael@0 302
michael@0 303 cmds[map["F9"] = "sortLines"] = function(cm) { sortLines(cm, true); };
michael@0 304 cmds[map[ctrl + "F9"] = "sortLinesInsensitive"] = function(cm) { sortLines(cm, false); };
michael@0 305
michael@0 306 cmds[map["F2"] = "nextBookmark"] = function(cm) {
michael@0 307 var marks = cm.state.sublimeBookmarks;
michael@0 308 if (marks) while (marks.length) {
michael@0 309 var current = marks.shift();
michael@0 310 var found = current.find();
michael@0 311 if (found) {
michael@0 312 marks.push(current);
michael@0 313 return cm.setSelection(found.from, found.to);
michael@0 314 }
michael@0 315 }
michael@0 316 };
michael@0 317
michael@0 318 cmds[map["Shift-F2"] = "prevBookmark"] = function(cm) {
michael@0 319 var marks = cm.state.sublimeBookmarks;
michael@0 320 if (marks) while (marks.length) {
michael@0 321 marks.unshift(marks.pop());
michael@0 322 var found = marks[marks.length - 1].find();
michael@0 323 if (!found)
michael@0 324 marks.pop();
michael@0 325 else
michael@0 326 return cm.setSelection(found.from, found.to);
michael@0 327 }
michael@0 328 };
michael@0 329
michael@0 330 cmds[map[ctrl + "F2"] = "toggleBookmark"] = function(cm) {
michael@0 331 var ranges = cm.listSelections();
michael@0 332 var marks = cm.state.sublimeBookmarks || (cm.state.sublimeBookmarks = []);
michael@0 333 for (var i = 0; i < ranges.length; i++) {
michael@0 334 var from = ranges[i].from(), to = ranges[i].to();
michael@0 335 var found = cm.findMarks(from, to);
michael@0 336 for (var j = 0; j < found.length; j++) {
michael@0 337 if (found[j].sublimeBookmark) {
michael@0 338 found[j].clear();
michael@0 339 for (var k = 0; k < marks.length; k++)
michael@0 340 if (marks[k] == found[j])
michael@0 341 marks.splice(k--, 1);
michael@0 342 break;
michael@0 343 }
michael@0 344 }
michael@0 345 if (j == found.length)
michael@0 346 marks.push(cm.markText(from, to, {sublimeBookmark: true, clearWhenEmpty: false}));
michael@0 347 }
michael@0 348 };
michael@0 349
michael@0 350 cmds[map["Shift-" + ctrl + "F2"] = "clearBookmarks"] = function(cm) {
michael@0 351 var marks = cm.state.sublimeBookmarks;
michael@0 352 if (marks) for (var i = 0; i < marks.length; i++) marks[i].clear();
michael@0 353 marks.length = 0;
michael@0 354 };
michael@0 355
michael@0 356 cmds[map["Alt-F2"] = "selectBookmarks"] = function(cm) {
michael@0 357 var marks = cm.state.sublimeBookmarks, ranges = [];
michael@0 358 if (marks) for (var i = 0; i < marks.length; i++) {
michael@0 359 var found = marks[i].find();
michael@0 360 if (!found)
michael@0 361 marks.splice(i--, 0);
michael@0 362 else
michael@0 363 ranges.push({anchor: found.from, head: found.to});
michael@0 364 }
michael@0 365 if (ranges.length)
michael@0 366 cm.setSelections(ranges, 0);
michael@0 367 };
michael@0 368
michael@0 369 map["Alt-Q"] = "wrapLines";
michael@0 370
michael@0 371 var mapK = CodeMirror.keyMap["sublime-Ctrl-K"] = {auto: "sublime", nofallthrough: true};
michael@0 372
michael@0 373 map[ctrl + "K"] = function(cm) {cm.setOption("keyMap", "sublime-Ctrl-K");};
michael@0 374
michael@0 375 function modifyWordOrSelection(cm, mod) {
michael@0 376 cm.operation(function() {
michael@0 377 var ranges = cm.listSelections(), indices = [], replacements = [];
michael@0 378 for (var i = 0; i < ranges.length; i++) {
michael@0 379 var range = ranges[i];
michael@0 380 if (range.empty()) { indices.push(i); replacements.push(""); }
michael@0 381 else replacements.push(mod(cm.getRange(range.from(), range.to())));
michael@0 382 }
michael@0 383 cm.replaceSelections(replacements, "around", "case");
michael@0 384 for (var i = indices.length - 1, at; i >= 0; i--) {
michael@0 385 var range = ranges[indices[i]];
michael@0 386 if (at && CodeMirror.cmpPos(range.head, at) > 0) continue;
michael@0 387 var word = wordAt(cm, range.head);
michael@0 388 at = word.from;
michael@0 389 cm.replaceRange(mod(word.word), word.from, word.to);
michael@0 390 }
michael@0 391 });
michael@0 392 }
michael@0 393
michael@0 394 mapK[ctrl + "Backspace"] = "delLineLeft";
michael@0 395
michael@0 396 cmds[mapK[ctrl + "K"] = "delLineRight"] = function(cm) {
michael@0 397 cm.operation(function() {
michael@0 398 var ranges = cm.listSelections();
michael@0 399 for (var i = ranges.length - 1; i >= 0; i--)
michael@0 400 cm.replaceRange("", ranges[i].anchor, Pos(ranges[i].to().line), "+delete");
michael@0 401 cm.scrollIntoView();
michael@0 402 });
michael@0 403 };
michael@0 404
michael@0 405 cmds[mapK[ctrl + "U"] = "upcaseAtCursor"] = function(cm) {
michael@0 406 modifyWordOrSelection(cm, function(str) { return str.toUpperCase(); });
michael@0 407 };
michael@0 408 cmds[mapK[ctrl + "L"] = "downcaseAtCursor"] = function(cm) {
michael@0 409 modifyWordOrSelection(cm, function(str) { return str.toLowerCase(); });
michael@0 410 };
michael@0 411
michael@0 412 cmds[mapK[ctrl + "Space"] = "setSublimeMark"] = function(cm) {
michael@0 413 if (cm.state.sublimeMark) cm.state.sublimeMark.clear();
michael@0 414 cm.state.sublimeMark = cm.setBookmark(cm.getCursor());
michael@0 415 };
michael@0 416 cmds[mapK[ctrl + "A"] = "selectToSublimeMark"] = function(cm) {
michael@0 417 var found = cm.state.sublimeMark && cm.state.sublimeMark.find();
michael@0 418 if (found) cm.setSelection(cm.getCursor(), found);
michael@0 419 };
michael@0 420 cmds[mapK[ctrl + "W"] = "deleteToSublimeMark"] = function(cm) {
michael@0 421 var found = cm.state.sublimeMark && cm.state.sublimeMark.find();
michael@0 422 if (found) {
michael@0 423 var from = cm.getCursor(), to = found;
michael@0 424 if (CodeMirror.cmpPos(from, to) > 0) { var tmp = to; to = from; from = tmp; }
michael@0 425 cm.state.sublimeKilled = cm.getRange(from, to);
michael@0 426 cm.replaceRange("", from, to);
michael@0 427 }
michael@0 428 };
michael@0 429 cmds[mapK[ctrl + "X"] = "swapWithSublimeMark"] = function(cm) {
michael@0 430 var found = cm.state.sublimeMark && cm.state.sublimeMark.find();
michael@0 431 if (found) {
michael@0 432 cm.state.sublimeMark.clear();
michael@0 433 cm.state.sublimeMark = cm.setBookmark(cm.getCursor());
michael@0 434 cm.setCursor(found);
michael@0 435 }
michael@0 436 };
michael@0 437 cmds[mapK[ctrl + "Y"] = "sublimeYank"] = function(cm) {
michael@0 438 if (cm.state.sublimeKilled != null)
michael@0 439 cm.replaceSelection(cm.state.sublimeKilled, null, "paste");
michael@0 440 };
michael@0 441
michael@0 442 mapK[ctrl + "G"] = "clearBookmarks";
michael@0 443 cmds[mapK[ctrl + "C"] = "showInCenter"] = function(cm) {
michael@0 444 var pos = cm.cursorCoords(null, "local");
michael@0 445 cm.scrollTo(null, (pos.top + pos.bottom) / 2 - cm.getScrollInfo().clientHeight / 2);
michael@0 446 };
michael@0 447
michael@0 448 cmds[map["Shift-Alt-Up"] = "selectLinesUpward"] = function(cm) {
michael@0 449 cm.operation(function() {
michael@0 450 var ranges = cm.listSelections();
michael@0 451 for (var i = 0; i < ranges.length; i++) {
michael@0 452 var range = ranges[i];
michael@0 453 if (range.head.line > cm.firstLine())
michael@0 454 cm.addSelection(Pos(range.head.line - 1, range.head.ch));
michael@0 455 }
michael@0 456 });
michael@0 457 };
michael@0 458 cmds[map["Shift-Alt-Down"] = "selectLinesDownward"] = function(cm) {
michael@0 459 cm.operation(function() {
michael@0 460 var ranges = cm.listSelections();
michael@0 461 for (var i = 0; i < ranges.length; i++) {
michael@0 462 var range = ranges[i];
michael@0 463 if (range.head.line < cm.lastLine())
michael@0 464 cm.addSelection(Pos(range.head.line + 1, range.head.ch));
michael@0 465 }
michael@0 466 });
michael@0 467 };
michael@0 468
michael@0 469 function findAndGoTo(cm, forward) {
michael@0 470 var from = cm.getCursor("from"), to = cm.getCursor("to");
michael@0 471 if (CodeMirror.cmpPos(from, to) == 0) {
michael@0 472 var word = wordAt(cm, from);
michael@0 473 if (!word.word) return;
michael@0 474 from = word.from;
michael@0 475 to = word.to;
michael@0 476 }
michael@0 477
michael@0 478 var query = cm.getRange(from, to);
michael@0 479 var cur = cm.getSearchCursor(query, forward ? to : from);
michael@0 480
michael@0 481 if (forward ? cur.findNext() : cur.findPrevious()) {
michael@0 482 cm.setSelection(cur.from(), cur.to());
michael@0 483 } else {
michael@0 484 cur = cm.getSearchCursor(query, forward ? Pos(cm.firstLine(), 0)
michael@0 485 : cm.clipPos(Pos(cm.lastLine())));
michael@0 486 if (forward ? cur.findNext() : cur.findPrevious())
michael@0 487 cm.setSelection(cur.from(), cur.to());
michael@0 488 else if (word)
michael@0 489 cm.setSelection(from, to);
michael@0 490 }
michael@0 491 };
michael@0 492 cmds[map[ctrl + "F3"] = "findUnder"] = function(cm) { findAndGoTo(cm, true); };
michael@0 493 cmds[map["Shift-" + ctrl + "F3"] = "findUnderPrevious"] = function(cm) { findAndGoTo(cm,false); };
michael@0 494
michael@0 495 map["Shift-" + ctrl + "["] = "fold";
michael@0 496 map["Shift-" + ctrl + "]"] = "unfold";
michael@0 497 mapK[ctrl + "0"] = mapK[ctrl + "j"] = "unfoldAll";
michael@0 498
michael@0 499 map[ctrl + "I"] = "findIncremental";
michael@0 500 map["Shift-" + ctrl + "I"] = "findIncrementalReverse";
michael@0 501 map[ctrl + "H"] = "replace";
michael@0 502 map["F3"] = "findNext";
michael@0 503 map["Shift-F3"] = "findPrev";
michael@0 504
michael@0 505 });

mercurial