您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

721 行
26KB

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