You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1181 lines
40 KiB

12 months ago
  1. ace.define("ace/occur",["require","exports","module","ace/lib/oop","ace/range","ace/search","ace/edit_session","ace/search_highlight","ace/lib/dom"], function(acequire, exports, module) {
  2. "use strict";
  3. var oop = acequire("./lib/oop");
  4. var Range = acequire("./range").Range;
  5. var Search = acequire("./search").Search;
  6. var EditSession = acequire("./edit_session").EditSession;
  7. var SearchHighlight = acequire("./search_highlight").SearchHighlight;
  8. function Occur() {}
  9. oop.inherits(Occur, Search);
  10. (function() {
  11. this.enter = function(editor, options) {
  12. if (!options.needle) return false;
  13. var pos = editor.getCursorPosition();
  14. this.displayOccurContent(editor, options);
  15. var translatedPos = this.originalToOccurPosition(editor.session, pos);
  16. editor.moveCursorToPosition(translatedPos);
  17. return true;
  18. };
  19. this.exit = function(editor, options) {
  20. var pos = options.translatePosition && editor.getCursorPosition();
  21. var translatedPos = pos && this.occurToOriginalPosition(editor.session, pos);
  22. this.displayOriginalContent(editor);
  23. if (translatedPos)
  24. editor.moveCursorToPosition(translatedPos);
  25. return true;
  26. };
  27. this.highlight = function(sess, regexp) {
  28. var hl = sess.$occurHighlight = sess.$occurHighlight || sess.addDynamicMarker(
  29. new SearchHighlight(null, "ace_occur-highlight", "text"));
  30. hl.setRegexp(regexp);
  31. sess._emit("changeBackMarker"); // force highlight layer redraw
  32. };
  33. this.displayOccurContent = function(editor, options) {
  34. this.$originalSession = editor.session;
  35. var found = this.matchingLines(editor.session, options);
  36. var lines = found.map(function(foundLine) { return foundLine.content; });
  37. var occurSession = new EditSession(lines.join('\n'));
  38. occurSession.$occur = this;
  39. occurSession.$occurMatchingLines = found;
  40. editor.setSession(occurSession);
  41. this.$useEmacsStyleLineStart = this.$originalSession.$useEmacsStyleLineStart;
  42. occurSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
  43. this.highlight(occurSession, options.re);
  44. occurSession._emit('changeBackMarker');
  45. };
  46. this.displayOriginalContent = function(editor) {
  47. editor.setSession(this.$originalSession);
  48. this.$originalSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
  49. };
  50. this.originalToOccurPosition = function(session, pos) {
  51. var lines = session.$occurMatchingLines;
  52. var nullPos = {row: 0, column: 0};
  53. if (!lines) return nullPos;
  54. for (var i = 0; i < lines.length; i++) {
  55. if (lines[i].row === pos.row)
  56. return {row: i, column: pos.column};
  57. }
  58. return nullPos;
  59. };
  60. this.occurToOriginalPosition = function(session, pos) {
  61. var lines = session.$occurMatchingLines;
  62. if (!lines || !lines[pos.row])
  63. return pos;
  64. return {row: lines[pos.row].row, column: pos.column};
  65. };
  66. this.matchingLines = function(session, options) {
  67. options = oop.mixin({}, options);
  68. if (!session || !options.needle) return [];
  69. var search = new Search();
  70. search.set(options);
  71. return search.findAll(session).reduce(function(lines, range) {
  72. var row = range.start.row;
  73. var last = lines[lines.length-1];
  74. return last && last.row === row ?
  75. lines :
  76. lines.concat({row: row, content: session.getLine(row)});
  77. }, []);
  78. };
  79. }).call(Occur.prototype);
  80. var dom = acequire('./lib/dom');
  81. dom.importCssString(".ace_occur-highlight {\n\
  82. border-radius: 4px;\n\
  83. background-color: rgba(87, 255, 8, 0.25);\n\
  84. position: absolute;\n\
  85. z-index: 4;\n\
  86. -moz-box-sizing: border-box;\n\
  87. -webkit-box-sizing: border-box;\n\
  88. box-sizing: border-box;\n\
  89. box-shadow: 0 0 4px rgb(91, 255, 50);\n\
  90. }\n\
  91. .ace_dark .ace_occur-highlight {\n\
  92. background-color: rgb(80, 140, 85);\n\
  93. box-shadow: 0 0 4px rgb(60, 120, 70);\n\
  94. }\n", "incremental-occur-highlighting");
  95. exports.Occur = Occur;
  96. });
  97. ace.define("ace/commands/occur_commands",["require","exports","module","ace/config","ace/occur","ace/keyboard/hash_handler","ace/lib/oop"], function(acequire, exports, module) {
  98. var config = acequire("../config"),
  99. Occur = acequire("../occur").Occur;
  100. var occurStartCommand = {
  101. name: "occur",
  102. exec: function(editor, options) {
  103. var alreadyInOccur = !!editor.session.$occur;
  104. var occurSessionActive = new Occur().enter(editor, options);
  105. if (occurSessionActive && !alreadyInOccur)
  106. OccurKeyboardHandler.installIn(editor);
  107. },
  108. readOnly: true
  109. };
  110. var occurCommands = [{
  111. name: "occurexit",
  112. bindKey: 'esc|Ctrl-G',
  113. exec: function(editor) {
  114. var occur = editor.session.$occur;
  115. if (!occur) return;
  116. occur.exit(editor, {});
  117. if (!editor.session.$occur) OccurKeyboardHandler.uninstallFrom(editor);
  118. },
  119. readOnly: true
  120. }, {
  121. name: "occuraccept",
  122. bindKey: 'enter',
  123. exec: function(editor) {
  124. var occur = editor.session.$occur;
  125. if (!occur) return;
  126. occur.exit(editor, {translatePosition: true});
  127. if (!editor.session.$occur) OccurKeyboardHandler.uninstallFrom(editor);
  128. },
  129. readOnly: true
  130. }];
  131. var HashHandler = acequire("../keyboard/hash_handler").HashHandler;
  132. var oop = acequire("../lib/oop");
  133. function OccurKeyboardHandler() {}
  134. oop.inherits(OccurKeyboardHandler, HashHandler);
  135. (function() {
  136. this.isOccurHandler = true;
  137. this.attach = function(editor) {
  138. HashHandler.call(this, occurCommands, editor.commands.platform);
  139. this.$editor = editor;
  140. };
  141. var handleKeyboard$super = this.handleKeyboard;
  142. this.handleKeyboard = function(data, hashId, key, keyCode) {
  143. var cmd = handleKeyboard$super.call(this, data, hashId, key, keyCode);
  144. return (cmd && cmd.command) ? cmd : undefined;
  145. };
  146. }).call(OccurKeyboardHandler.prototype);
  147. OccurKeyboardHandler.installIn = function(editor) {
  148. var handler = new this();
  149. editor.keyBinding.addKeyboardHandler(handler);
  150. editor.commands.addCommands(occurCommands);
  151. };
  152. OccurKeyboardHandler.uninstallFrom = function(editor) {
  153. editor.commands.removeCommands(occurCommands);
  154. var handler = editor.getKeyboardHandler();
  155. if (handler.isOccurHandler)
  156. editor.keyBinding.removeKeyboardHandler(handler);
  157. };
  158. exports.occurStartCommand = occurStartCommand;
  159. });
  160. ace.define("ace/commands/incremental_search_commands",["require","exports","module","ace/config","ace/lib/oop","ace/keyboard/hash_handler","ace/commands/occur_commands"], function(acequire, exports, module) {
  161. var config = acequire("../config");
  162. var oop = acequire("../lib/oop");
  163. var HashHandler = acequire("../keyboard/hash_handler").HashHandler;
  164. var occurStartCommand = acequire("./occur_commands").occurStartCommand;
  165. exports.iSearchStartCommands = [{
  166. name: "iSearch",
  167. bindKey: {win: "Ctrl-F", mac: "Command-F"},
  168. exec: function(editor, options) {
  169. config.loadModule(["core", "ace/incremental_search"], function(e) {
  170. var iSearch = e.iSearch = e.iSearch || new e.IncrementalSearch();
  171. iSearch.activate(editor, options.backwards);
  172. if (options.jumpToFirstMatch) iSearch.next(options);
  173. });
  174. },
  175. readOnly: true
  176. }, {
  177. name: "iSearchBackwards",
  178. exec: function(editor, jumpToNext) { editor.execCommand('iSearch', {backwards: true}); },
  179. readOnly: true
  180. }, {
  181. name: "iSearchAndGo",
  182. bindKey: {win: "Ctrl-K", mac: "Command-G"},
  183. exec: function(editor, jumpToNext) { editor.execCommand('iSearch', {jumpToFirstMatch: true, useCurrentOrPrevSearch: true}); },
  184. readOnly: true
  185. }, {
  186. name: "iSearchBackwardsAndGo",
  187. bindKey: {win: "Ctrl-Shift-K", mac: "Command-Shift-G"},
  188. exec: function(editor) { editor.execCommand('iSearch', {jumpToFirstMatch: true, backwards: true, useCurrentOrPrevSearch: true}); },
  189. readOnly: true
  190. }];
  191. exports.iSearchCommands = [{
  192. name: "restartSearch",
  193. bindKey: {win: "Ctrl-F", mac: "Command-F"},
  194. exec: function(iSearch) {
  195. iSearch.cancelSearch(true);
  196. }
  197. }, {
  198. name: "searchForward",
  199. bindKey: {win: "Ctrl-S|Ctrl-K", mac: "Ctrl-S|Command-G"},
  200. exec: function(iSearch, options) {
  201. options.useCurrentOrPrevSearch = true;
  202. iSearch.next(options);
  203. }
  204. }, {
  205. name: "searchBackward",
  206. bindKey: {win: "Ctrl-R|Ctrl-Shift-K", mac: "Ctrl-R|Command-Shift-G"},
  207. exec: function(iSearch, options) {
  208. options.useCurrentOrPrevSearch = true;
  209. options.backwards = true;
  210. iSearch.next(options);
  211. }
  212. }, {
  213. name: "extendSearchTerm",
  214. exec: function(iSearch, string) {
  215. iSearch.addString(string);
  216. }
  217. }, {
  218. name: "extendSearchTermSpace",
  219. bindKey: "space",
  220. exec: function(iSearch) { iSearch.addString(' '); }
  221. }, {
  222. name: "shrinkSearchTerm",
  223. bindKey: "backspace",
  224. exec: function(iSearch) {
  225. iSearch.removeChar();
  226. }
  227. }, {
  228. name: 'confirmSearch',
  229. bindKey: 'return',
  230. exec: function(iSearch) { iSearch.deactivate(); }
  231. }, {
  232. name: 'cancelSearch',
  233. bindKey: 'esc|Ctrl-G',
  234. exec: function(iSearch) { iSearch.deactivate(true); }
  235. }, {
  236. name: 'occurisearch',
  237. bindKey: 'Ctrl-O',
  238. exec: function(iSearch) {
  239. var options = oop.mixin({}, iSearch.$options);
  240. iSearch.deactivate();
  241. occurStartCommand.exec(iSearch.$editor, options);
  242. }
  243. }, {
  244. name: "yankNextWord",
  245. bindKey: "Ctrl-w",
  246. exec: function(iSearch) {
  247. var ed = iSearch.$editor,
  248. range = ed.selection.getRangeOfMovements(function(sel) { sel.moveCursorWordRight(); }),
  249. string = ed.session.getTextRange(range);
  250. iSearch.addString(string);
  251. }
  252. }, {
  253. name: "yankNextChar",
  254. bindKey: "Ctrl-Alt-y",
  255. exec: function(iSearch) {
  256. var ed = iSearch.$editor,
  257. range = ed.selection.getRangeOfMovements(function(sel) { sel.moveCursorRight(); }),
  258. string = ed.session.getTextRange(range);
  259. iSearch.addString(string);
  260. }
  261. }, {
  262. name: 'recenterTopBottom',
  263. bindKey: 'Ctrl-l',
  264. exec: function(iSearch) { iSearch.$editor.execCommand('recenterTopBottom'); }
  265. }, {
  266. name: 'selectAllMatches',
  267. bindKey: 'Ctrl-space',
  268. exec: function(iSearch) {
  269. var ed = iSearch.$editor,
  270. hl = ed.session.$isearchHighlight,
  271. ranges = hl && hl.cache ? hl.cache
  272. .reduce(function(ranges, ea) {
  273. return ranges.concat(ea ? ea : []); }, []) : [];
  274. iSearch.deactivate(false);
  275. ranges.forEach(ed.selection.addRange.bind(ed.selection));
  276. }
  277. }, {
  278. name: 'searchAsRegExp',
  279. bindKey: 'Alt-r',
  280. exec: function(iSearch) {
  281. iSearch.convertNeedleToRegExp();
  282. }
  283. }].map(function(cmd) {
  284. cmd.readOnly = true;
  285. cmd.isIncrementalSearchCommand = true;
  286. cmd.scrollIntoView = "animate-cursor";
  287. return cmd;
  288. });
  289. function IncrementalSearchKeyboardHandler(iSearch) {
  290. this.$iSearch = iSearch;
  291. }
  292. oop.inherits(IncrementalSearchKeyboardHandler, HashHandler);
  293. (function() {
  294. this.attach = function(editor) {
  295. var iSearch = this.$iSearch;
  296. HashHandler.call(this, exports.iSearchCommands, editor.commands.platform);
  297. this.$commandExecHandler = editor.commands.addEventListener('exec', function(e) {
  298. if (!e.command.isIncrementalSearchCommand)
  299. return iSearch.deactivate();
  300. e.stopPropagation();
  301. e.preventDefault();
  302. var scrollTop = editor.session.getScrollTop();
  303. var result = e.command.exec(iSearch, e.args || {});
  304. editor.renderer.scrollCursorIntoView(null, 0.5);
  305. editor.renderer.animateScrolling(scrollTop);
  306. return result;
  307. });
  308. };
  309. this.detach = function(editor) {
  310. if (!this.$commandExecHandler) return;
  311. editor.commands.removeEventListener('exec', this.$commandExecHandler);
  312. delete this.$commandExecHandler;
  313. };
  314. var handleKeyboard$super = this.handleKeyboard;
  315. this.handleKeyboard = function(data, hashId, key, keyCode) {
  316. if (((hashId === 1/*ctrl*/ || hashId === 8/*command*/) && key === 'v')
  317. || (hashId === 1/*ctrl*/ && key === 'y')) return null;
  318. var cmd = handleKeyboard$super.call(this, data, hashId, key, keyCode);
  319. if (cmd.command) { return cmd; }
  320. if (hashId == -1) {
  321. var extendCmd = this.commands.extendSearchTerm;
  322. if (extendCmd) { return {command: extendCmd, args: key}; }
  323. }
  324. return false;
  325. };
  326. }).call(IncrementalSearchKeyboardHandler.prototype);
  327. exports.IncrementalSearchKeyboardHandler = IncrementalSearchKeyboardHandler;
  328. });
  329. ace.define("ace/incremental_search",["require","exports","module","ace/lib/oop","ace/range","ace/search","ace/search_highlight","ace/commands/incremental_search_commands","ace/lib/dom","ace/commands/command_manager","ace/editor","ace/config"], function(acequire, exports, module) {
  330. "use strict";
  331. var oop = acequire("./lib/oop");
  332. var Range = acequire("./range").Range;
  333. var Search = acequire("./search").Search;
  334. var SearchHighlight = acequire("./search_highlight").SearchHighlight;
  335. var iSearchCommandModule = acequire("./commands/incremental_search_commands");
  336. var ISearchKbd = iSearchCommandModule.IncrementalSearchKeyboardHandler;
  337. function IncrementalSearch() {
  338. this.$options = {wrap: false, skipCurrent: false};
  339. this.$keyboardHandler = new ISearchKbd(this);
  340. }
  341. oop.inherits(IncrementalSearch, Search);
  342. function isRegExp(obj) {
  343. return obj instanceof RegExp;
  344. }
  345. function regExpToObject(re) {
  346. var string = String(re),
  347. start = string.indexOf('/'),
  348. flagStart = string.lastIndexOf('/');
  349. return {
  350. expression: string.slice(start+1, flagStart),
  351. flags: string.slice(flagStart+1)
  352. };
  353. }
  354. function stringToRegExp(string, flags) {
  355. try {
  356. return new RegExp(string, flags);
  357. } catch (e) { return string; }
  358. }
  359. function objectToRegExp(obj) {
  360. return stringToRegExp(obj.expression, obj.flags);
  361. }
  362. (function() {
  363. this.activate = function(ed, backwards) {
  364. this.$editor = ed;
  365. this.$startPos = this.$currentPos = ed.getCursorPosition();
  366. this.$options.needle = '';
  367. this.$options.backwards = backwards;
  368. ed.keyBinding.addKeyboardHandler(this.$keyboardHandler);
  369. this.$originalEditorOnPaste = ed.onPaste; ed.onPaste = this.onPaste.bind(this);
  370. this.$mousedownHandler = ed.addEventListener('mousedown', this.onMouseDown.bind(this));
  371. this.selectionFix(ed);
  372. this.statusMessage(true);
  373. };
  374. this.deactivate = function(reset) {
  375. this.cancelSearch(reset);
  376. var ed = this.$editor;
  377. ed.keyBinding.removeKeyboardHandler(this.$keyboardHandler);
  378. if (this.$mousedownHandler) {
  379. ed.removeEventListener('mousedown', this.$mousedownHandler);
  380. delete this.$mousedownHandler;
  381. }
  382. ed.onPaste = this.$originalEditorOnPaste;
  383. this.message('');
  384. };
  385. this.selectionFix = function(editor) {
  386. if (editor.selection.isEmpty() && !editor.session.$emacsMark) {
  387. editor.clearSelection();
  388. }
  389. };
  390. this.highlight = function(regexp) {
  391. var sess = this.$editor.session,
  392. hl = sess.$isearchHighlight = sess.$isearchHighlight || sess.addDynamicMarker(
  393. new SearchHighlight(null, "ace_isearch-result", "text"));
  394. hl.setRegexp(regexp);
  395. sess._emit("changeBackMarker"); // force highlight layer redraw
  396. };
  397. this.cancelSearch = function(reset) {
  398. var e = this.$editor;
  399. this.$prevNeedle = this.$options.needle;
  400. this.$options.needle = '';
  401. if (reset) {
  402. e.moveCursorToPosition(this.$startPos);
  403. this.$currentPos = this.$startPos;
  404. } else {
  405. e.pushEmacsMark && e.pushEmacsMark(this.$startPos, false);
  406. }
  407. this.highlight(null);
  408. return Range.fromPoints(this.$currentPos, this.$currentPos);
  409. };
  410. this.highlightAndFindWithNeedle = function(moveToNext, needleUpdateFunc) {
  411. if (!this.$editor) return null;
  412. var options = this.$options;
  413. if (needleUpdateFunc) {
  414. options.needle = needleUpdateFunc.call(this, options.needle || '') || '';
  415. }
  416. if (options.needle.length === 0) {
  417. this.statusMessage(true);
  418. return this.cancelSearch(true);
  419. }
  420. options.start = this.$currentPos;
  421. var session = this.$editor.session,
  422. found = this.find(session),
  423. shouldSelect = this.$editor.emacsMark ?
  424. !!this.$editor.emacsMark() : !this.$editor.selection.isEmpty();
  425. if (found) {
  426. if (options.backwards) found = Range.fromPoints(found.end, found.start);
  427. this.$editor.selection.setRange(Range.fromPoints(shouldSelect ? this.$startPos : found.end, found.end));
  428. if (moveToNext) this.$currentPos = found.end;
  429. this.highlight(options.re);
  430. }
  431. this.statusMessage(found);
  432. return found;
  433. };
  434. this.addString = function(s) {
  435. return this.highlightAndFindWithNeedle(false, function(needle) {
  436. if (!isRegExp(needle))
  437. return needle + s;
  438. var reObj = regExpToObject(needle);
  439. reObj.expression += s;
  440. return objectToRegExp(reObj);
  441. });
  442. };
  443. this.removeChar = function(c) {
  444. return this.highlightAndFindWithNeedle(false, function(needle) {
  445. if (!isRegExp(needle))
  446. return needle.substring(0, needle.length-1);
  447. var reObj = regExpToObject(needle);
  448. reObj.expression = reObj.expression.substring(0, reObj.expression.length-1);
  449. return objectToRegExp(reObj);
  450. });
  451. };
  452. this.next = function(options) {
  453. options = options || {};
  454. this.$options.backwards = !!options.backwards;
  455. this.$currentPos = this.$editor.getCursorPosition();
  456. return this.highlightAndFindWithNeedle(true, function(needle) {
  457. return options.useCurrentOrPrevSearch && needle.length === 0 ?
  458. this.$prevNeedle || '' : needle;
  459. });
  460. };
  461. this.onMouseDown = function(evt) {
  462. this.deactivate();
  463. return true;
  464. };
  465. this.onPaste = function(text) {
  466. this.addString(text);
  467. };
  468. this.convertNeedleToRegExp = function() {
  469. return this.highlightAndFindWithNeedle(false, function(needle) {
  470. return isRegExp(needle) ? needle : stringToRegExp(needle, 'ig');
  471. });
  472. };
  473. this.convertNeedleToString = function() {
  474. return this.highlightAndFindWithNeedle(false, function(needle) {
  475. return isRegExp(needle) ? regExpToObject(needle).expression : needle;
  476. });
  477. };
  478. this.statusMessage = function(found) {
  479. var options = this.$options, msg = '';
  480. msg += options.backwards ? 'reverse-' : '';
  481. msg += 'isearch: ' + options.needle;
  482. msg += found ? '' : ' (not found)';
  483. this.message(msg);
  484. };
  485. this.message = function(msg) {
  486. if (this.$editor.showCommandLine) {
  487. this.$editor.showCommandLine(msg);
  488. this.$editor.focus();
  489. } else {
  490. console.log(msg);
  491. }
  492. };
  493. }).call(IncrementalSearch.prototype);
  494. exports.IncrementalSearch = IncrementalSearch;
  495. var dom = acequire('./lib/dom');
  496. dom.importCssString && dom.importCssString("\
  497. .ace_marker-layer .ace_isearch-result {\
  498. position: absolute;\
  499. z-index: 6;\
  500. -moz-box-sizing: border-box;\
  501. -webkit-box-sizing: border-box;\
  502. box-sizing: border-box;\
  503. }\
  504. div.ace_isearch-result {\
  505. border-radius: 4px;\
  506. background-color: rgba(255, 200, 0, 0.5);\
  507. box-shadow: 0 0 4px rgb(255, 200, 0);\
  508. }\
  509. .ace_dark div.ace_isearch-result {\
  510. background-color: rgb(100, 110, 160);\
  511. box-shadow: 0 0 4px rgb(80, 90, 140);\
  512. }", "incremental-search-highlighting");
  513. var commands = acequire("./commands/command_manager");
  514. (function() {
  515. this.setupIncrementalSearch = function(editor, val) {
  516. if (this.usesIncrementalSearch == val) return;
  517. this.usesIncrementalSearch = val;
  518. var iSearchCommands = iSearchCommandModule.iSearchStartCommands;
  519. var method = val ? 'addCommands' : 'removeCommands';
  520. this[method](iSearchCommands);
  521. };
  522. }).call(commands.CommandManager.prototype);
  523. var Editor = acequire("./editor").Editor;
  524. acequire("./config").defineOptions(Editor.prototype, "editor", {
  525. useIncrementalSearch: {
  526. set: function(val) {
  527. this.keyBinding.$handlers.forEach(function(handler) {
  528. if (handler.setupIncrementalSearch) {
  529. handler.setupIncrementalSearch(this, val);
  530. }
  531. });
  532. this._emit('incrementalSearchSettingChanged', {isEnabled: val});
  533. }
  534. }
  535. });
  536. });
  537. ace.define("ace/keyboard/emacs",["require","exports","module","ace/lib/dom","ace/incremental_search","ace/commands/incremental_search_commands","ace/keyboard/hash_handler","ace/lib/keys"], function(acequire, exports, module) {
  538. "use strict";
  539. var dom = acequire("../lib/dom");
  540. acequire("../incremental_search");
  541. var iSearchCommandModule = acequire("../commands/incremental_search_commands");
  542. var screenToTextBlockCoordinates = function(x, y) {
  543. var canvasPos = this.scroller.getBoundingClientRect();
  544. var offsetX = x + this.scrollLeft - canvasPos.left - this.$padding;
  545. var col = Math.floor(offsetX / this.characterWidth);
  546. var row = Math.floor(
  547. (y + this.scrollTop - canvasPos.top) / this.lineHeight
  548. );
  549. return this.session.screenToDocumentPosition(row, col, offsetX);
  550. };
  551. var HashHandler = acequire("./hash_handler").HashHandler;
  552. exports.handler = new HashHandler();
  553. exports.handler.isEmacs = true;
  554. exports.handler.$id = "ace/keyboard/emacs";
  555. var initialized = false;
  556. var $formerLongWords;
  557. var $formerLineStart;
  558. exports.handler.attach = function(editor) {
  559. if (!initialized) {
  560. initialized = true;
  561. dom.importCssString('\
  562. .emacs-mode .ace_cursor{\
  563. border: 1px rgba(50,250,50,0.8) solid!important;\
  564. -moz-box-sizing: border-box!important;\
  565. -webkit-box-sizing: border-box!important;\
  566. box-sizing: border-box!important;\
  567. background-color: rgba(0,250,0,0.9);\
  568. opacity: 0.5;\
  569. }\
  570. .emacs-mode .ace_hidden-cursors .ace_cursor{\
  571. opacity: 1;\
  572. background-color: transparent;\
  573. }\
  574. .emacs-mode .ace_overwrite-cursors .ace_cursor {\
  575. opacity: 1;\
  576. background-color: transparent;\
  577. border-width: 0 0 2px 2px !important;\
  578. }\
  579. .emacs-mode .ace_text-layer {\
  580. z-index: 4\
  581. }\
  582. .emacs-mode .ace_cursor-layer {\
  583. z-index: 2\
  584. }', 'emacsMode'
  585. );
  586. }
  587. $formerLongWords = editor.session.$selectLongWords;
  588. editor.session.$selectLongWords = true;
  589. $formerLineStart = editor.session.$useEmacsStyleLineStart;
  590. editor.session.$useEmacsStyleLineStart = true;
  591. editor.session.$emacsMark = null; // the active mark
  592. editor.session.$emacsMarkRing = editor.session.$emacsMarkRing || [];
  593. editor.emacsMark = function() {
  594. return this.session.$emacsMark;
  595. };
  596. editor.setEmacsMark = function(p) {
  597. this.session.$emacsMark = p;
  598. };
  599. editor.pushEmacsMark = function(p, activate) {
  600. var prevMark = this.session.$emacsMark;
  601. if (prevMark)
  602. this.session.$emacsMarkRing.push(prevMark);
  603. if (!p || activate) this.setEmacsMark(p);
  604. else this.session.$emacsMarkRing.push(p);
  605. };
  606. editor.popEmacsMark = function() {
  607. var mark = this.emacsMark();
  608. if (mark) { this.setEmacsMark(null); return mark; }
  609. return this.session.$emacsMarkRing.pop();
  610. };
  611. editor.getLastEmacsMark = function(p) {
  612. return this.session.$emacsMark || this.session.$emacsMarkRing.slice(-1)[0];
  613. };
  614. editor.emacsMarkForSelection = function(replacement) {
  615. var sel = this.selection,
  616. multiRangeLength = this.multiSelect ?
  617. this.multiSelect.getAllRanges().length : 1,
  618. selIndex = sel.index || 0,
  619. markRing = this.session.$emacsMarkRing,
  620. markIndex = markRing.length - (multiRangeLength - selIndex),
  621. lastMark = markRing[markIndex] || sel.anchor;
  622. if (replacement) {
  623. markRing.splice(markIndex, 1,
  624. "row" in replacement && "column" in replacement ?
  625. replacement : undefined);
  626. }
  627. return lastMark;
  628. };
  629. editor.on("click", $resetMarkMode);
  630. editor.on("changeSession", $kbSessionChange);
  631. editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates;
  632. editor.setStyle("emacs-mode");
  633. editor.commands.addCommands(commands);
  634. exports.handler.platform = editor.commands.platform;
  635. editor.$emacsModeHandler = this;
  636. editor.addEventListener('copy', this.onCopy);
  637. editor.addEventListener('paste', this.onPaste);
  638. };
  639. exports.handler.detach = function(editor) {
  640. delete editor.renderer.screenToTextCoordinates;
  641. editor.session.$selectLongWords = $formerLongWords;
  642. editor.session.$useEmacsStyleLineStart = $formerLineStart;
  643. editor.removeEventListener("click", $resetMarkMode);
  644. editor.removeEventListener("changeSession", $kbSessionChange);
  645. editor.unsetStyle("emacs-mode");
  646. editor.commands.removeCommands(commands);
  647. editor.removeEventListener('copy', this.onCopy);
  648. editor.removeEventListener('paste', this.onPaste);
  649. editor.$emacsModeHandler = null;
  650. };
  651. var $kbSessionChange = function(e) {
  652. if (e.oldSession) {
  653. e.oldSession.$selectLongWords = $formerLongWords;
  654. e.oldSession.$useEmacsStyleLineStart = $formerLineStart;
  655. }
  656. $formerLongWords = e.session.$selectLongWords;
  657. e.session.$selectLongWords = true;
  658. $formerLineStart = e.session.$useEmacsStyleLineStart;
  659. e.session.$useEmacsStyleLineStart = true;
  660. if (!e.session.hasOwnProperty('$emacsMark'))
  661. e.session.$emacsMark = null;
  662. if (!e.session.hasOwnProperty('$emacsMarkRing'))
  663. e.session.$emacsMarkRing = [];
  664. };
  665. var $resetMarkMode = function(e) {
  666. e.editor.session.$emacsMark = null;
  667. };
  668. var keys = acequire("../lib/keys").KEY_MODS;
  669. var eMods = {C: "ctrl", S: "shift", M: "alt", CMD: "command"};
  670. var combinations = ["C-S-M-CMD",
  671. "S-M-CMD", "C-M-CMD", "C-S-CMD", "C-S-M",
  672. "M-CMD", "S-CMD", "S-M", "C-CMD", "C-M", "C-S",
  673. "CMD", "M", "S", "C"];
  674. combinations.forEach(function(c) {
  675. var hashId = 0;
  676. c.split("-").forEach(function(c) {
  677. hashId = hashId | keys[eMods[c]];
  678. });
  679. eMods[hashId] = c.toLowerCase() + "-";
  680. });
  681. exports.handler.onCopy = function(e, editor) {
  682. if (editor.$handlesEmacsOnCopy) return;
  683. editor.$handlesEmacsOnCopy = true;
  684. exports.handler.commands.killRingSave.exec(editor);
  685. editor.$handlesEmacsOnCopy = false;
  686. };
  687. exports.handler.onPaste = function(e, editor) {
  688. editor.pushEmacsMark(editor.getCursorPosition());
  689. };
  690. exports.handler.bindKey = function(key, command) {
  691. if (typeof key == "object")
  692. key = key[this.platform];
  693. if (!key)
  694. return;
  695. var ckb = this.commandKeyBinding;
  696. key.split("|").forEach(function(keyPart) {
  697. keyPart = keyPart.toLowerCase();
  698. ckb[keyPart] = command;
  699. var keyParts = keyPart.split(" ").slice(0,-1);
  700. keyParts.reduce(function(keyMapKeys, keyPart, i) {
  701. var prefix = keyMapKeys[i-1] ? keyMapKeys[i-1] + ' ' : '';
  702. return keyMapKeys.concat([prefix + keyPart]);
  703. }, []).forEach(function(keyPart) {
  704. if (!ckb[keyPart]) ckb[keyPart] = "null";
  705. });
  706. }, this);
  707. };
  708. exports.handler.getStatusText = function(editor, data) {
  709. var str = "";
  710. if (data.count)
  711. str += data.count;
  712. if (data.keyChain)
  713. str += " " + data.keyChain;
  714. return str;
  715. };
  716. exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
  717. if (keyCode === -1) return undefined;
  718. var editor = data.editor;
  719. editor._signal("changeStatus");
  720. if (hashId == -1) {
  721. editor.pushEmacsMark();
  722. if (data.count) {
  723. var str = new Array(data.count + 1).join(key);
  724. data.count = null;
  725. return {command: "insertstring", args: str};
  726. }
  727. }
  728. var modifier = eMods[hashId];
  729. if (modifier == "c-" || data.count) {
  730. var count = parseInt(key[key.length - 1]);
  731. if (typeof count === 'number' && !isNaN(count)) {
  732. data.count = Math.max(data.count, 0) || 0;
  733. data.count = 10 * data.count + count;
  734. return {command: "null"};
  735. }
  736. }
  737. if (modifier) key = modifier + key;
  738. if (data.keyChain) key = data.keyChain += " " + key;
  739. var command = this.commandKeyBinding[key];
  740. data.keyChain = command == "null" ? key : "";
  741. if (!command) return undefined;
  742. if (command === "null") return {command: "null"};
  743. if (command === "universalArgument") {
  744. data.count = -4;
  745. return {command: "null"};
  746. }
  747. var args;
  748. if (typeof command !== "string") {
  749. args = command.args;
  750. if (command.command) command = command.command;
  751. if (command === "goorselect") {
  752. command = editor.emacsMark() ? args[1] : args[0];
  753. args = null;
  754. }
  755. }
  756. if (typeof command === "string") {
  757. if (command === "insertstring" ||
  758. command === "splitline" ||
  759. command === "togglecomment") {
  760. editor.pushEmacsMark();
  761. }
  762. command = this.commands[command] || editor.commands.commands[command];
  763. if (!command) return undefined;
  764. }
  765. if (!command.readOnly && !command.isYank)
  766. data.lastCommand = null;
  767. if (!command.readOnly && editor.emacsMark())
  768. editor.setEmacsMark(null);
  769. if (data.count) {
  770. var count = data.count;
  771. data.count = 0;
  772. if (!command || !command.handlesCount) {
  773. return {
  774. args: args,
  775. command: {
  776. exec: function(editor, args) {
  777. for (var i = 0; i < count; i++)
  778. command.exec(editor, args);
  779. },
  780. multiSelectAction: command.multiSelectAction
  781. }
  782. };
  783. } else {
  784. if (!args) args = {};
  785. if (typeof args === 'object') args.count = count;
  786. }
  787. }
  788. return {command: command, args: args};
  789. };
  790. exports.emacsKeys = {
  791. "Up|C-p" : {command: "goorselect", args: ["golineup","selectup"]},
  792. "Down|C-n" : {command: "goorselect", args: ["golinedown","selectdown"]},
  793. "Left|C-b" : {command: "goorselect", args: ["gotoleft","selectleft"]},
  794. "Right|C-f" : {command: "goorselect", args: ["gotoright","selectright"]},
  795. "C-Left|M-b" : {command: "goorselect", args: ["gotowordleft","selectwordleft"]},
  796. "C-Right|M-f" : {command: "goorselect", args: ["gotowordright","selectwordright"]},
  797. "Home|C-a" : {command: "goorselect", args: ["gotolinestart","selecttolinestart"]},
  798. "End|C-e" : {command: "goorselect", args: ["gotolineend","selecttolineend"]},
  799. "C-Home|S-M-,": {command: "goorselect", args: ["gotostart","selecttostart"]},
  800. "C-End|S-M-." : {command: "goorselect", args: ["gotoend","selecttoend"]},
  801. "S-Up|S-C-p" : "selectup",
  802. "S-Down|S-C-n" : "selectdown",
  803. "S-Left|S-C-b" : "selectleft",
  804. "S-Right|S-C-f" : "selectright",
  805. "S-C-Left|S-M-b" : "selectwordleft",
  806. "S-C-Right|S-M-f" : "selectwordright",
  807. "S-Home|S-C-a" : "selecttolinestart",
  808. "S-End|S-C-e" : "selecttolineend",
  809. "S-C-Home" : "selecttostart",
  810. "S-C-End" : "selecttoend",
  811. "C-l" : "recenterTopBottom",
  812. "M-s" : "centerselection",
  813. "M-g": "gotoline",
  814. "C-x C-p": "selectall",
  815. "C-Down": {command: "goorselect", args: ["gotopagedown","selectpagedown"]},
  816. "C-Up": {command: "goorselect", args: ["gotopageup","selectpageup"]},
  817. "PageDown|C-v": {command: "goorselect", args: ["gotopagedown","selectpagedown"]},
  818. "PageUp|M-v": {command: "goorselect", args: ["gotopageup","selectpageup"]},
  819. "S-C-Down": "selectpagedown",
  820. "S-C-Up": "selectpageup",
  821. "C-s": "iSearch",
  822. "C-r": "iSearchBackwards",
  823. "M-C-s": "findnext",
  824. "M-C-r": "findprevious",
  825. "S-M-5": "replace",
  826. "Backspace": "backspace",
  827. "Delete|C-d": "del",
  828. "Return|C-m": {command: "insertstring", args: "\n"}, // "newline"
  829. "C-o": "splitline",
  830. "M-d|C-Delete": {command: "killWord", args: "right"},
  831. "C-Backspace|M-Backspace|M-Delete": {command: "killWord", args: "left"},
  832. "C-k": "killLine",
  833. "C-y|S-Delete": "yank",
  834. "M-y": "yankRotate",
  835. "C-g": "keyboardQuit",
  836. "C-w|C-S-W": "killRegion",
  837. "M-w": "killRingSave",
  838. "C-Space": "setMark",
  839. "C-x C-x": "exchangePointAndMark",
  840. "C-t": "transposeletters",
  841. "M-u": "touppercase", // Doesn't work
  842. "M-l": "tolowercase",
  843. "M-/": "autocomplete", // Doesn't work
  844. "C-u": "universalArgument",
  845. "M-;": "togglecomment",
  846. "C-/|C-x u|S-C--|C-z": "undo",
  847. "S-C-/|S-C-x u|C--|S-C-z": "redo", // infinite undo?
  848. "C-x r": "selectRectangularRegion",
  849. "M-x": {command: "focusCommandLine", args: "M-x "}
  850. };
  851. exports.handler.bindKeys(exports.emacsKeys);
  852. exports.handler.addCommands({
  853. recenterTopBottom: function(editor) {
  854. var renderer = editor.renderer;
  855. var pos = renderer.$cursorLayer.getPixelPosition();
  856. var h = renderer.$size.scrollerHeight - renderer.lineHeight;
  857. var scrollTop = renderer.scrollTop;
  858. if (Math.abs(pos.top - scrollTop) < 2) {
  859. scrollTop = pos.top - h;
  860. } else if (Math.abs(pos.top - scrollTop - h * 0.5) < 2) {
  861. scrollTop = pos.top;
  862. } else {
  863. scrollTop = pos.top - h * 0.5;
  864. }
  865. editor.session.setScrollTop(scrollTop);
  866. },
  867. selectRectangularRegion: function(editor) {
  868. editor.multiSelect.toggleBlockSelection();
  869. },
  870. setMark: {
  871. exec: function(editor, args) {
  872. if (args && args.count) {
  873. if (editor.inMultiSelectMode) editor.forEachSelection(moveToMark);
  874. else moveToMark();
  875. moveToMark();
  876. return;
  877. }
  878. var mark = editor.emacsMark(),
  879. ranges = editor.selection.getAllRanges(),
  880. rangePositions = ranges.map(function(r) { return {row: r.start.row, column: r.start.column}; }),
  881. transientMarkModeActive = true,
  882. hasNoSelection = ranges.every(function(range) { return range.isEmpty(); });
  883. if (transientMarkModeActive && (mark || !hasNoSelection)) {
  884. if (editor.inMultiSelectMode) editor.forEachSelection({exec: editor.clearSelection.bind(editor)});
  885. else editor.clearSelection();
  886. if (mark) editor.pushEmacsMark(null);
  887. return;
  888. }
  889. if (!mark) {
  890. rangePositions.forEach(function(pos) { editor.pushEmacsMark(pos); });
  891. editor.setEmacsMark(rangePositions[rangePositions.length-1]);
  892. return;
  893. }
  894. function moveToMark() {
  895. var mark = editor.popEmacsMark();
  896. mark && editor.moveCursorToPosition(mark);
  897. }
  898. },
  899. readOnly: true,
  900. handlesCount: true
  901. },
  902. exchangePointAndMark: {
  903. exec: function exchangePointAndMark$exec(editor, args) {
  904. var sel = editor.selection;
  905. if (!args.count && !sel.isEmpty()) { // just invert selection
  906. sel.setSelectionRange(sel.getRange(), !sel.isBackwards());
  907. return;
  908. }
  909. if (args.count) { // replace mark and point
  910. var pos = {row: sel.lead.row, column: sel.lead.column};
  911. sel.clearSelection();
  912. sel.moveCursorToPosition(editor.emacsMarkForSelection(pos));
  913. } else { // create selection to last mark
  914. sel.selectToPosition(editor.emacsMarkForSelection());
  915. }
  916. },
  917. readOnly: true,
  918. handlesCount: true,
  919. multiSelectAction: "forEach"
  920. },
  921. killWord: {
  922. exec: function(editor, dir) {
  923. editor.clearSelection();
  924. if (dir == "left")
  925. editor.selection.selectWordLeft();
  926. else
  927. editor.selection.selectWordRight();
  928. var range = editor.getSelectionRange();
  929. var text = editor.session.getTextRange(range);
  930. exports.killRing.add(text);
  931. editor.session.remove(range);
  932. editor.clearSelection();
  933. },
  934. multiSelectAction: "forEach"
  935. },
  936. killLine: function(editor) {
  937. editor.pushEmacsMark(null);
  938. editor.clearSelection();
  939. var range = editor.getSelectionRange();
  940. var line = editor.session.getLine(range.start.row);
  941. range.end.column = line.length;
  942. line = line.substr(range.start.column);
  943. var foldLine = editor.session.getFoldLine(range.start.row);
  944. if (foldLine && range.end.row != foldLine.end.row) {
  945. range.end.row = foldLine.end.row;
  946. line = "x";
  947. }
  948. if (/^\s*$/.test(line)) {
  949. range.end.row++;
  950. line = editor.session.getLine(range.end.row);
  951. range.end.column = /^\s*$/.test(line) ? line.length : 0;
  952. }
  953. var text = editor.session.getTextRange(range);
  954. if (editor.prevOp.command == this)
  955. exports.killRing.append(text);
  956. else
  957. exports.killRing.add(text);
  958. editor.session.remove(range);
  959. editor.clearSelection();
  960. },
  961. yank: function(editor) {
  962. editor.onPaste(exports.killRing.get() || '');
  963. editor.keyBinding.$data.lastCommand = "yank";
  964. },
  965. yankRotate: function(editor) {
  966. if (editor.keyBinding.$data.lastCommand != "yank")
  967. return;
  968. editor.undo();
  969. editor.session.$emacsMarkRing.pop(); // also undo recording mark
  970. editor.onPaste(exports.killRing.rotate());
  971. editor.keyBinding.$data.lastCommand = "yank";
  972. },
  973. killRegion: {
  974. exec: function(editor) {
  975. exports.killRing.add(editor.getCopyText());
  976. editor.commands.byName.cut.exec(editor);
  977. editor.setEmacsMark(null);
  978. },
  979. readOnly: true,
  980. multiSelectAction: "forEach"
  981. },
  982. killRingSave: {
  983. exec: function(editor) {
  984. editor.$handlesEmacsOnCopy = true;
  985. var marks = editor.session.$emacsMarkRing.slice(),
  986. deselectedMarks = [];
  987. exports.killRing.add(editor.getCopyText());
  988. setTimeout(function() {
  989. function deselect() {
  990. var sel = editor.selection, range = sel.getRange(),
  991. pos = sel.isBackwards() ? range.end : range.start;
  992. deselectedMarks.push({row: pos.row, column: pos.column});
  993. sel.clearSelection();
  994. }
  995. editor.$handlesEmacsOnCopy = false;
  996. if (editor.inMultiSelectMode) editor.forEachSelection({exec: deselect});
  997. else deselect();
  998. editor.session.$emacsMarkRing = marks.concat(deselectedMarks.reverse());
  999. }, 0);
  1000. },
  1001. readOnly: true
  1002. },
  1003. keyboardQuit: function(editor) {
  1004. editor.selection.clearSelection();
  1005. editor.setEmacsMark(null);
  1006. editor.keyBinding.$data.count = null;
  1007. },
  1008. focusCommandLine: function(editor, arg) {
  1009. if (editor.showCommandLine)
  1010. editor.showCommandLine(arg);
  1011. }
  1012. });
  1013. exports.handler.addCommands(iSearchCommandModule.iSearchStartCommands);
  1014. var commands = exports.handler.commands;
  1015. commands.yank.isYank = true;
  1016. commands.yankRotate.isYank = true;
  1017. exports.killRing = {
  1018. $data: [],
  1019. add: function(str) {
  1020. str && this.$data.push(str);
  1021. if (this.$data.length > 30)
  1022. this.$data.shift();
  1023. },
  1024. append: function(str) {
  1025. var idx = this.$data.length - 1;
  1026. var text = this.$data[idx] || "";
  1027. if (str) text += str;
  1028. if (text) this.$data[idx] = text;
  1029. },
  1030. get: function(n) {
  1031. n = n || 1;
  1032. return this.$data.slice(this.$data.length-n, this.$data.length).reverse().join('\n');
  1033. },
  1034. pop: function() {
  1035. if (this.$data.length > 1)
  1036. this.$data.pop();
  1037. return this.get();
  1038. },
  1039. rotate: function() {
  1040. this.$data.unshift(this.$data.pop());
  1041. return this.get();
  1042. }
  1043. };
  1044. });