keybinding-emacs.js 36 KB

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