keybinding-emacs.js 39 KB

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