keybinding-emacs.js 40 KB

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