1
0

sublime.js 20 KB

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