sublime.js 25 KB

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