corrade-nucleus-nucleons – Blame information for rev 20

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