Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

keybinding-emacs.js 40KB

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