f6493d95eaf3c21810134f4fc28531c7.js 40 KB

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