Debian Codegraph @

extensions/CodeEditor/modules/lib/ace/keybinding-emacs.js

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