sublime.js 21 KB

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