keybinding-emacs.js 40 KB

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