keybinding-emacs.js 39 KB

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