annotate rhodecode/public/js/codemirror.js @ 1305:166317d464f3 beta

Added server side file editing with commit
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 03 May 2011 14:13:37 +0200
parents
children a30689fc4f61
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1305
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 // All functions that need access to the editor's state live inside
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 // the CodeMirror function. Below that, at the bottom of the file,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 // some utilities are defined.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 // CodeMirror is the only global var we claim
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 var CodeMirror = (function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 // This is the function that produces an editor instance. It's
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 // closure is used to store the editor state.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 function CodeMirror(place, givenOptions) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 // Determine effective options based on given values and defaults.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 var options = {}, defaults = CodeMirror.defaults;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 for (var opt in defaults)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 if (defaults.hasOwnProperty(opt))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 // The element in which the editor lives. Takes care of scrolling
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 // (if enabled).
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 var wrapper = document.createElement("div");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 wrapper.className = "CodeMirror";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 // This mess creates the base DOM structure for the editor.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 wrapper.innerHTML =
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 '<div style="position: relative">' + // Set to the height of the text, causes scrolling
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 '<pre style="position: relative; height: 0; visibility: hidden; overflow: hidden;">' + // To measure line/char size
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 '<span>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span></pre>' +
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 '<div style="position: relative">' + // Moved around its parent to cover visible view
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 '<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 '<div style="overflow: hidden; position: absolute; width: 0; left: 0">' + // Wraps and hides input textarea
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 '<textarea style="height: 1px; position: absolute; width: 1px;" wrap="off"></textarea></div>' +
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 // Provides positioning relative to (visible) text origin
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 '<div class="CodeMirror-lines"><div style="position: relative">' +
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 '<pre class="CodeMirror-cursor">&#160;</pre>' + // Absolutely positioned blinky cursor
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 '<div></div></div></div></div></div>'; // This DIV contains the actual code
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 if (place.appendChild) place.appendChild(wrapper); else place(wrapper);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 // I've never seen more elegant code in my life.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 var code = wrapper.firstChild, measure = code.firstChild, mover = measure.nextSibling,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 gutter = mover.firstChild, gutterText = gutter.firstChild,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 inputDiv = gutter.nextSibling, input = inputDiv.firstChild,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 lineSpace = inputDiv.nextSibling.firstChild, cursor = lineSpace.firstChild, lineDiv = cursor.nextSibling;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 if (options.tabindex != null) input.tabindex = options.tabindex;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 if (!options.gutter && !options.lineNumbers) gutter.style.display = "none";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 // Delayed object wrap timeouts, making sure only one is active. blinker holds an interval.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 var poll = new Delayed(), highlight = new Delayed(), blinker;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 // mode holds a mode API object. lines an array of Line objects
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 // (see Line constructor), work an array of lines that should be
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 // parsed, and history the undo history (instance of History
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 // constructor).
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 var mode, lines = [new Line("")], work, history = new History(), focused;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 loadMode();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 // The selection. These are always maintained to point at valid
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 // positions. Inverted is used to remember that the user is
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 // selecting bottom-to-top.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 var sel = {from: {line: 0, ch: 0}, to: {line: 0, ch: 0}, inverted: false};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 // Selection-related flags. shiftSelecting obviously tracks
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 // whether the user is holding shift. reducedSelection is a hack
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 // to get around the fact that we can't create inverted
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 // selections. See below.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 var shiftSelecting, reducedSelection;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 // Variables used by startOperation/endOperation to track what
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 // happened during the operation.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 var updateInput, changes, textChanged, selectionChanged, leaveInputAlone;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 // Current visible range (may be bigger than the view window).
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 var showingFrom = 0, showingTo = 0, lastHeight = 0, curKeyId = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 // editing will hold an object describing the things we put in the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 // textarea, to help figure out whether something changed.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 // bracketHighlighted is used to remember that a backet has been
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 // marked.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 var editing, bracketHighlighted;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 // Initialize the content. Somewhat hacky (delayed prepareInput)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 // to work around browser issues.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 operation(function(){setValue(options.value || ""); updateInput = false;})();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 setTimeout(prepareInput, 20);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 // Register our event handlers.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 connect(wrapper, "mousedown", operation(onMouseDown));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 // Gecko browsers fire contextmenu *after* opening the menu, at
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 // which point we can't mess with it anymore. Context menu is
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 // handled in onMouseDown for Gecko.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 if (!gecko) connect(wrapper, "contextmenu", operation(onContextMenu));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 connect(code, "dblclick", operation(onDblClick));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 connect(wrapper, "scroll", function() {updateDisplay([]); if (options.onScroll) options.onScroll(instance);});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 connect(window, "resize", function() {updateDisplay(true);});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 connect(input, "keyup", operation(onKeyUp));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 connect(input, "keydown", operation(onKeyDown));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 connect(input, "keypress", operation(onKeyPress));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 connect(input, "focus", onFocus);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 connect(input, "blur", onBlur);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 connect(wrapper, "dragenter", function(e){e.stop();});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 connect(wrapper, "dragover", function(e){e.stop();});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 connect(wrapper, "drop", operation(onDrop));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 connect(wrapper, "paste", function(){input.focus(); fastPoll();});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 connect(input, "paste", function(){fastPoll();});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 connect(input, "cut", function(){fastPoll();});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 if (document.activeElement == input) onFocus();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 else onBlur();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 function isLine(l) {return l >= 0 && l < lines.length;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 // The instance object that we'll return. Mostly calls out to
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 // local functions in the CodeMirror function. Some do some extra
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 // range checking and/or clipping. operation is used to wrap the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 // call so that changes it makes are tracked, and the display is
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 // updated afterwards.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 var instance = {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 getValue: getValue,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 setValue: operation(setValue),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 getSelection: getSelection,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 replaceSelection: operation(replaceSelection),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 focus: function(){input.focus(); onFocus(); fastPoll();},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 setOption: function(option, value) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 options[option] = value;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 if (option == "lineNumbers" || option == "gutter") gutterChanged();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 else if (option == "mode" || option == "indentUnit") loadMode();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 getOption: function(option) {return options[option];},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 undo: operation(undo),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 redo: operation(redo),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 indentLine: operation(function(n) {if (isLine(n)) indentLine(n, "smart");}),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 historySize: function() {return {undo: history.done.length, redo: history.undone.length};},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 matchBrackets: operation(function(){matchBrackets(true);}),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 getTokenAt: function(pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 pos = clipPos(pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 return lines[pos.line].getTokenAt(mode, getStateBefore(pos.line), pos.ch);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 cursorCoords: function(start){
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 if (start == null) start = sel.inverted;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 return pageCoords(start ? sel.from : sel.to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 charCoords: function(pos){return pageCoords(clipPos(pos));},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 coordsChar: function(coords) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 var off = eltOffset(lineSpace);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 var line = Math.min(showingTo - 1, showingFrom + Math.floor(coords.y / lineHeight()));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 return clipPos({line: line, ch: charFromX(clipLine(line), coords.x)});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 getSearchCursor: function(query, pos, caseFold) {return new SearchCursor(query, pos, caseFold);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 markText: operation(function(a, b, c){return operation(markText(a, b, c));}),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 setMarker: addGutterMarker,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 clearMarker: removeGutterMarker,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 setLineClass: operation(setLineClass),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 lineInfo: lineInfo,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 addWidget: function(pos, node, scroll) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 var pos = localCoords(clipPos(pos), true);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 node.style.top = (showingFrom * lineHeight() + pos.yBot + paddingTop()) + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 node.style.left = (pos.x + paddingLeft()) + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 code.appendChild(node);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 if (scroll)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 scrollIntoView(pos.x, pos.yBot, pos.x + node.offsetWidth, pos.yBot + node.offsetHeight);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 lineCount: function() {return lines.length;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 getCursor: function(start) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 if (start == null) start = sel.inverted;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 return copyPos(start ? sel.from : sel.to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 somethingSelected: function() {return !posEq(sel.from, sel.to);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 setCursor: operation(function(line, ch) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 if (ch == null && typeof line.line == "number") setCursor(line.line, line.ch);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 else setCursor(line, ch);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 }),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 setSelection: operation(function(from, to) {setSelection(clipPos(from), clipPos(to || from));}),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 getLine: function(line) {if (isLine(line)) return lines[line].text;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 setLine: operation(function(line, text) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 if (isLine(line)) replaceRange(text, {line: line, ch: 0}, {line: line, ch: lines[line].text.length});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 }),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 removeLine: operation(function(line) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 if (isLine(line)) replaceRange("", {line: line, ch: 0}, clipPos({line: line+1, ch: 0}));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 }),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 replaceRange: operation(replaceRange),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 operation: function(f){return operation(f)();},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 refresh: function(){updateDisplay(true);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 getInputField: function(){return input;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 getWrapperElement: function(){return wrapper;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 function setValue(code) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 history = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 var top = {line: 0, ch: 0};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 updateLines(top, {line: lines.length - 1, ch: lines[lines.length-1].text.length},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 splitLines(code), top, top);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 history = new History();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 function getValue(code) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 var text = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 for (var i = 0, l = lines.length; i < l; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 text.push(lines[i].text);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 return text.join("\n");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194 function onMouseDown(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 // First, see if this is a click in the gutter
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 for (var n = e.target(); n != wrapper; n = n.parentNode)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 if (n.parentNode == gutterText) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198 if (options.onGutterClick)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 options.onGutterClick(instance, indexOf(gutterText.childNodes, n) + showingFrom);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200 return e.stop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 if (gecko && e.button() == 3) onContextMenu(e);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204 if (e.button() != 1) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 // For button 1, if it was clicked inside the editor
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 // (posFromMouse returning non-null), we have to adjust the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 // selection.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 var start = posFromMouse(e), last = start, going;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 if (!start) {if (e.target() == wrapper) e.stop(); return;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 setCursor(start.line, start.ch, false);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212 if (!focused) onFocus();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213 e.stop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214 // And then we have to see if it's a drag event, in which case
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215 // the dragged-over text must be selected.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 function end() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217 input.focus();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 updateInput = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 move(); up();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221 function extend(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222 var cur = posFromMouse(e, true);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223 if (cur && !posEq(cur, last)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
224 if (!focused) onFocus();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
225 last = cur;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226 setSelection(start, cur);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227 updateInput = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 var visible = visibleLines();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229 if (cur.line >= visible.to || cur.line < visible.from)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 going = setTimeout(operation(function(){extend(e);}), 150);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 var move = connect(document, "mousemove", operation(function(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 clearTimeout(going);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 e.stop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237 extend(e);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 }), true);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 var up = connect(document, "mouseup", operation(function(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240 clearTimeout(going);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241 var cur = posFromMouse(e);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242 if (cur) setSelection(start, cur);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243 e.stop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 end();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 }), true);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 function onDblClick(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248 var pos = posFromMouse(e);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
249 if (!pos) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 selectWordAt(pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 e.stop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 function onDrop(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254 var pos = posFromMouse(e, true), files = e.e.dataTransfer.files;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255 if (!pos || options.readOnly) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 if (files && files.length && window.FileReader && window.File) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257 var n = files.length, text = Array(n), read = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 for (var i = 0; i < n; ++i) loadFile(files[i], i);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 function loadFile(file, i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 var reader = new FileReader;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261 reader.onload = function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262 text[i] = reader.result;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263 if (++read == n) replaceRange(text.join(""), clipPos(pos), clipPos(pos));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
264 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
265 reader.readAsText(file);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 try {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270 var text = e.e.dataTransfer.getData("Text");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271 if (text) replaceRange(text, pos, pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273 catch(e){}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276 function onKeyDown(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277 if (!focused) onFocus();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 var code = e.e.keyCode;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 // Tries to detect ctrl on non-mac, cmd on mac.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 var mod = (mac ? e.e.metaKey : e.e.ctrlKey) && !e.e.altKey, anyMod = e.e.ctrlKey || e.e.altKey || e.e.metaKey;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282 if (code == 16 || e.e.shiftKey) shiftSelecting = shiftSelecting || (sel.inverted ? sel.to : sel.from);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
283 else shiftSelecting = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284 // First give onKeyEvent option a chance to handle this.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285 if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e.e))) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 if (code == 33 || code == 34) {scrollPage(code == 34); return e.stop();} // page up/down
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 if (mod && (code == 36 || code == 35)) {scrollEnd(code == 36); return e.stop();} // ctrl-home/end
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
289 if (mod && code == 65) {selectAll(); return e.stop();} // ctrl-a
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290 if (!options.readOnly) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291 if (!anyMod && code == 13) {return;} // enter
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
292 if (!anyMod && code == 9 && handleTab(e.e.shiftKey)) return e.stop(); // tab
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293 if (mod && code == 90) {undo(); return e.stop();} // ctrl-z
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
294 if (mod && ((e.e.shiftKey && code == 90) || code == 89)) {redo(); return e.stop();} // ctrl-shift-z, ctrl-y
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
295 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
296
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
297 // Key id to use in the movementKeys map. We also pass it to
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298 // fastPoll in order to 'self learn'. We need this because
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299 // reducedSelection, the hack where we collapse the selection to
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 // its start when it is inverted and a movement key is pressed
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 // (and later restore it again), shouldn't be used for
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 // non-movement keys.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 curKeyId = (mod ? "c" : "") + code;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 if (sel.inverted && movementKeys.hasOwnProperty(curKeyId)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305 var range = selRange(input);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 if (range) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307 reducedSelection = {anchor: range.start};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 setSelRange(input, range.start, range.start);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
310 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
311 fastPoll(curKeyId);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313 function onKeyUp(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 if (reducedSelection) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315 reducedSelection = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316 updateInput = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318 if (e.e.keyCode == 16) shiftSelecting = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 function onKeyPress(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e.e))) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322 if (options.electricChars && mode.electricChars) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 var ch = String.fromCharCode(e.e.charCode == null ? e.e.keyCode : e.e.charCode);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324 if (mode.electricChars.indexOf(ch) > -1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325 setTimeout(operation(function() {indentLine(sel.to.line, "smart");}), 50);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 var code = e.e.keyCode;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328 // Re-stop tab and enter. Necessary on some browsers.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 if (code == 13) {handleEnter(); e.stop();}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330 else if (code == 9 && options.tabMode != "default") e.stop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331 else fastPoll(curKeyId);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 function onFocus() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 if (!focused && options.onFocus) options.onFocus(instance);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336 focused = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337 slowPoll();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338 if (wrapper.className.search(/\bCodeMirror-focused\b/) == -1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339 wrapper.className += " CodeMirror-focused";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340 restartBlink();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342 function onBlur() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 if (focused && options.onBlur) options.onBlur(instance);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344 clearInterval(blinker);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 shiftSelecting = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 focused = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347 wrapper.className = wrapper.className.replace(" CodeMirror-focused", "");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 // Replace the range from from to to by the strings in newText.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351 // Afterwards, set the selection to selFrom, selTo.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352 function updateLines(from, to, newText, selFrom, selTo) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 if (history) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
354 var old = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
355 for (var i = from.line, e = to.line + 1; i < e; ++i) old.push(lines[i].text);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
356 history.addChange(from.line, newText.length, old);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357 while (history.done.length > options.undoDepth) history.done.shift();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
358 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359 updateLinesNoUndo(from, to, newText, selFrom, selTo);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
361 function unredoHelper(from, to) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
362 var change = from.pop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
363 if (change) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
364 var replaced = [], end = change.start + change.added;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
365 for (var i = change.start; i < end; ++i) replaced.push(lines[i].text);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
366 to.push({start: change.start, added: change.old.length, old: replaced});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
367 var pos = clipPos({line: change.start + change.old.length - 1,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
368 ch: editEnd(replaced[replaced.length-1], change.old[change.old.length-1])});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
369 updateLinesNoUndo({line: change.start, ch: 0}, {line: end - 1, ch: lines[end-1].text.length}, change.old, pos, pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
370 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
371 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
372 function undo() {unredoHelper(history.done, history.undone);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
373 function redo() {unredoHelper(history.undone, history.done);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
374
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
375 function updateLinesNoUndo(from, to, newText, selFrom, selTo) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
376 var nlines = to.line - from.line, firstLine = lines[from.line], lastLine = lines[to.line];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
377 // First adjust the line structure, taking some care to leave highlighting intact.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
378 if (firstLine == lastLine) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
379 if (newText.length == 1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
380 firstLine.replace(from.ch, to.ch, newText[0]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
381 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
382 lastLine = firstLine.split(to.ch, newText[newText.length-1]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
383 var spliceargs = [from.line + 1, nlines];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
384 firstLine.replace(from.ch, firstLine.text.length, newText[0]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
385 for (var i = 1, e = newText.length - 1; i < e; ++i) spliceargs.push(new Line(newText[i]));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
386 spliceargs.push(lastLine);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
387 lines.splice.apply(lines, spliceargs);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
388 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
389 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
390 else if (newText.length == 1) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
391 firstLine.replace(from.ch, firstLine.text.length, newText[0] + lastLine.text.slice(to.ch));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
392 lines.splice(from.line + 1, nlines);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
393 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
394 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
395 var spliceargs = [from.line + 1, nlines - 1];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
396 firstLine.replace(from.ch, firstLine.text.length, newText[0]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
397 lastLine.replace(0, to.ch, newText[newText.length-1]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
398 for (var i = 1, e = newText.length - 1; i < e; ++i) spliceargs.push(new Line(newText[i]));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
399 lines.splice.apply(lines, spliceargs);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
400 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
401
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
402 // Add these lines to the work array, so that they will be
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
403 // highlighted. Adjust work lines if lines were added/removed.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
404 var newWork = [], lendiff = newText.length - nlines - 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
405 for (var i = 0, l = work.length; i < l; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
406 var task = work[i];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
407 if (task < from.line) newWork.push(task);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
408 else if (task > to.line) newWork.push(task + lendiff);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
409 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
410 if (newText.length) newWork.push(from.line);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
411 work = newWork;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
412 startWorker(100);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
413 // Remember that these lines changed, for updating the display
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
414 changes.push({from: from.line, to: to.line + 1, diff: lendiff});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
415 textChanged = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
416
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
417 // Update the selection
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
418 function updateLine(n) {return n <= Math.min(to.line, to.line + lendiff) ? n : n + lendiff;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
419 setSelection(selFrom, selTo, updateLine(sel.from.line), updateLine(sel.to.line));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
420
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
421 // Make sure the scroll-size div has the correct height.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
422 code.style.height = (lines.length * lineHeight() + 2 * paddingTop()) + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
423 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
424
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
425 function replaceRange(code, from, to) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
426 from = clipPos(from);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
427 if (!to) to = from; else to = clipPos(to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
428 code = splitLines(code);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
429 function adjustPos(pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
430 if (posLess(pos, from)) return pos;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
431 if (!posLess(to, pos)) return end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
432 var line = pos.line + code.length - (to.line - from.line) - 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
433 var ch = pos.ch;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
434 if (pos.line == to.line)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
435 ch += code[code.length-1].length - (to.ch - (to.line == from.line ? from.ch : 0));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
436 return {line: line, ch: ch};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
437 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
438 var end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
439 replaceRange1(code, from, to, function(end1) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
440 end = end1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
441 return {from: adjustPos(sel.from), to: adjustPos(sel.to)};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
442 });
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
443 return end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
444 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
445 function replaceSelection(code, collapse) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
446 replaceRange1(splitLines(code), sel.from, sel.to, function(end) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
447 if (collapse == "end") return {from: end, to: end};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
448 else if (collapse == "start") return {from: sel.from, to: sel.from};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
449 else return {from: sel.from, to: end};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
450 });
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
451 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
452 function replaceRange1(code, from, to, computeSel) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
453 var endch = code.length == 1 ? code[0].length + from.ch : code[code.length-1].length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
454 var newSel = computeSel({line: from.line + code.length - 1, ch: endch});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
455 updateLines(from, to, code, newSel.from, newSel.to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
456 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
457
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
458 function getRange(from, to) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
459 var l1 = from.line, l2 = to.line;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
460 if (l1 == l2) return lines[l1].text.slice(from.ch, to.ch);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
461 var code = [lines[l1].text.slice(from.ch)];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
462 for (var i = l1 + 1; i < l2; ++i) code.push(lines[i].text);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
463 code.push(lines[l2].text.slice(0, to.ch));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
464 return code.join("\n");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
465 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
466 function getSelection() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
467 return getRange(sel.from, sel.to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
468 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
469
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
470 var pollingFast = false; // Ensures slowPoll doesn't cancel fastPoll
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
471 function slowPoll() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
472 if (pollingFast) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
473 poll.set(2000, function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
474 startOperation();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
475 readInput();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
476 if (focused) slowPoll();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
477 endOperation();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
478 });
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
479 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
480 function fastPoll(keyId) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
481 var missed = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
482 pollingFast = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
483 function p() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
484 startOperation();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
485 var changed = readInput();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
486 if (changed == "moved" && keyId) movementKeys[keyId] = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
487 if (!changed && !missed) {missed = true; poll.set(80, p);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
488 else {pollingFast = false; slowPoll();}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
489 endOperation();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
490 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
491 poll.set(20, p);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
492 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
493
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
494 // Inspects the textarea, compares its state (content, selection)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
495 // to the data in the editing variable, and updates the editor
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
496 // content or cursor if something changed.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
497 function readInput() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
498 var changed = false, text = input.value, sr = selRange(input);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
499 if (!sr) return false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
500 var changed = editing.text != text, rs = reducedSelection;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
501 var moved = changed || sr.start != editing.start || sr.end != (rs ? editing.start : editing.end);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
502 if (reducedSelection && !moved && sel.from.line == 0 && sel.from.ch == 0)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
503 reducedSelection = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
504 else if (!moved) return false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
505 if (changed) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
506 shiftSelecting = reducedSelection = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
507 if (options.readOnly) {updateInput = true; return "changed";}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
508 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
509
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
510 // Compute selection start and end based on start/end offsets in textarea
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
511 function computeOffset(n, startLine) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
512 var pos = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
513 for (;;) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
514 var found = text.indexOf("\n", pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
515 if (found == -1 || (text.charAt(found-1) == "\r" ? found - 1 : found) >= n)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
516 return {line: startLine, ch: n - pos};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
517 ++startLine;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
518 pos = found + 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
519 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
520 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
521 var from = computeOffset(sr.start, editing.from),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
522 to = computeOffset(sr.end, editing.from);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
523 // Here we have to take the reducedSelection hack into account,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
524 // so that you can, for example, press shift-up at the start of
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
525 // your selection and have the right thing happen.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
526 if (rs) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
527 from = sr.start == rs.anchor ? to : from;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
528 to = shiftSelecting ? sel.to : sr.start == rs.anchor ? from : to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
529 if (!posLess(from, to)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
530 reducedSelection = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
531 sel.inverted = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
532 var tmp = from; from = to; to = tmp;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
533 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
534 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
535
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
536 // In some cases (cursor on same line as before), we don't have
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
537 // to update the textarea content at all.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
538 if (from.line == to.line && from.line == sel.from.line && from.line == sel.to.line && !shiftSelecting)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
539 updateInput = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
540
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
541 // Magic mess to extract precise edited range from the changed
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
542 // string.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
543 if (changed) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
544 var start = 0, end = text.length, len = Math.min(end, editing.text.length);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
545 var c, line = editing.from, nl = -1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
546 while (start < len && (c = text.charAt(start)) == editing.text.charAt(start)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
547 ++start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
548 if (c == "\n") {line++; nl = start;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
549 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
550 var ch = nl > -1 ? start - nl : start, endline = editing.to - 1, edend = editing.text.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
551 for (;;) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
552 c = editing.text.charAt(edend);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
553 if (c == "\n") endline--;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
554 if (text.charAt(end) != c) {++end; ++edend; break;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
555 if (edend <= start || end <= start) break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
556 --end; --edend;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
557 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
558 var nl = editing.text.lastIndexOf("\n", edend - 1), endch = nl == -1 ? edend : edend - nl - 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
559 updateLines({line: line, ch: ch}, {line: endline, ch: endch}, splitLines(text.slice(start, end)), from, to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
560 if (line != endline || from.line != line) updateInput = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
561 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
562 else setSelection(from, to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
563
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
564 editing.text = text; editing.start = sr.start; editing.end = sr.end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
565 return changed ? "changed" : moved ? "moved" : false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
566 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
567
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
568 // Set the textarea content and selection range to match the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
569 // editor state.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
570 function prepareInput() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
571 var text = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
572 var from = Math.max(0, sel.from.line - 1), to = Math.min(lines.length, sel.to.line + 2);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
573 for (var i = from; i < to; ++i) text.push(lines[i].text);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
574 text = input.value = text.join(lineSep);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
575 var startch = sel.from.ch, endch = sel.to.ch;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
576 for (var i = from; i < sel.from.line; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
577 startch += lineSep.length + lines[i].text.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
578 for (var i = from; i < sel.to.line; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
579 endch += lineSep.length + lines[i].text.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
580 editing = {text: text, from: from, to: to, start: startch, end: endch};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
581 setSelRange(input, startch, reducedSelection ? startch : endch);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
582 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
583
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
584 function scrollCursorIntoView() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
585 var cursor = localCoords(sel.inverted ? sel.from : sel.to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
586 return scrollIntoView(cursor.x, cursor.y, cursor.x, cursor.yBot);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
587 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
588 function scrollIntoView(x1, y1, x2, y2) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
589 var pl = paddingLeft(), pt = paddingTop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
590 y1 += pt; y2 += pt; x1 += pl; x2 += pl;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
591 var screen = wrapper.clientHeight, screentop = wrapper.scrollTop, scrolled = false, result = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
592 if (y1 < screentop) {wrapper.scrollTop = Math.max(0, y1 - 10); scrolled = true;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
593 else if (y2 > screentop + screen) {wrapper.scrollTop = y2 + 10 - screen; scrolled = true;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
594
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
595 var screenw = wrapper.clientWidth, screenleft = wrapper.scrollLeft;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
596 if (x1 < screenleft) {wrapper.scrollLeft = Math.max(0, x1 - 10); scrolled = true;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
597 else if (x2 > screenw + screenleft) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
598 wrapper.scrollLeft = x2 + 10 - screenw;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
599 scrolled = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
600 if (x2 > code.clientWidth) result = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
601 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
602 if (scrolled && options.onScroll) options.onScroll(instance);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
603 return result;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
604 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
605
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
606 function visibleLines() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
607 var lh = lineHeight(), top = wrapper.scrollTop - paddingTop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
608 return {from: Math.min(lines.length, Math.max(0, Math.floor(top / lh))),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
609 to: Math.min(lines.length, Math.ceil((top + wrapper.clientHeight) / lh))};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
610 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
611 // Uses a set of changes plus the current scroll position to
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
612 // determine which DOM updates have to be made, and makes the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
613 // updates.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
614 function updateDisplay(changes) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
615 if (!wrapper.clientWidth) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
616 showingFrom = showingTo = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
617 return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
618 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
619 // First create a range of theoretically intact lines, and punch
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
620 // holes in that using the change info.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
621 var intact = changes === true ? [] : [{from: showingFrom, to: showingTo, domStart: 0}];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
622 for (var i = 0, l = changes.length || 0; i < l; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
623 var change = changes[i], intact2 = [], diff = change.diff || 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
624 for (var j = 0, l2 = intact.length; j < l2; ++j) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
625 var range = intact[j];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
626 if (change.to <= range.from)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
627 intact2.push({from: range.from + diff, to: range.to + diff, domStart: range.domStart});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
628 else if (range.to <= change.from)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
629 intact2.push(range);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
630 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
631 if (change.from > range.from)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
632 intact2.push({from: range.from, to: change.from, domStart: range.domStart})
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
633 if (change.to < range.to)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
634 intact2.push({from: change.to + diff, to: range.to + diff,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
635 domStart: range.domStart + (change.to - range.from)});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
636 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
637 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
638 intact = intact2;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
639 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
640
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
641 // Then, determine which lines we'd want to see, and which
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
642 // updates have to be made to get there.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
643 var visible = visibleLines();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
644 var from = Math.min(showingFrom, Math.max(visible.from - 3, 0)),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
645 to = Math.min(lines.length, Math.max(showingTo, visible.to + 3)),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
646 updates = [], domPos = 0, domEnd = showingTo - showingFrom, pos = from, changedLines = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
647
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
648 for (var i = 0, l = intact.length; i < l; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
649 var range = intact[i];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
650 if (range.to <= from) continue;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
651 if (range.from >= to) break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
652 if (range.domStart > domPos || range.from > pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
653 updates.push({from: pos, to: range.from, domSize: range.domStart - domPos, domStart: domPos});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
654 changedLines += range.from - pos;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
655 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
656 pos = range.to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
657 domPos = range.domStart + (range.to - range.from);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
658 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
659 if (domPos != domEnd || pos != to) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
660 changedLines += Math.abs(to - pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
661 updates.push({from: pos, to: to, domSize: domEnd - domPos, domStart: domPos});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
662 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
663
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
664 if (!updates.length) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
665 lineDiv.style.display = "none";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
666 // If more than 30% of the screen needs update, just do a full
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
667 // redraw (which is quicker than patching)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
668 if (changedLines > (visible.to - visible.from) * .3)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
669 refreshDisplay(from = Math.max(visible.from - 10, 0), to = Math.min(visible.to + 7, lines.length));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
670 // Otherwise, only update the stuff that needs updating.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
671 else
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
672 patchDisplay(updates);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
673 lineDiv.style.display = "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
674
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
675 // Position the mover div to align with the lines it's supposed
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
676 // to be showing (which will cover the visible display)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
677 var different = from != showingFrom || to != showingTo || lastHeight != wrapper.clientHeight;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
678 showingFrom = from; showingTo = to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
679 mover.style.top = (from * lineHeight()) + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
680 if (different) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
681 lastHeight = wrapper.clientHeight;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
682 code.style.height = (lines.length * lineHeight() + 2 * paddingTop()) + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
683 updateGutter();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
684 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
685
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
686 // Since this is all rather error prone, it is honoured with the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
687 // only assertion in the whole file.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
688 if (lineDiv.childNodes.length != showingTo - showingFrom)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
689 throw new Error("BAD PATCH! " + JSON.stringify(updates) + " size=" + (showingTo - showingFrom) +
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
690 " nodes=" + lineDiv.childNodes.length);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
691 updateCursor();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
692 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
693
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
694 function refreshDisplay(from, to) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
695 var html = [], start = {line: from, ch: 0}, inSel = posLess(sel.from, start) && !posLess(sel.to, start);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
696 for (var i = from; i < to; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
697 var ch1 = null, ch2 = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
698 if (inSel) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
699 ch1 = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
700 if (sel.to.line == i) {inSel = false; ch2 = sel.to.ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
701 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
702 else if (sel.from.line == i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
703 if (sel.to.line == i) {ch1 = sel.from.ch; ch2 = sel.to.ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
704 else {inSel = true; ch1 = sel.from.ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
705 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
706 html.push(lines[i].getHTML(ch1, ch2, true));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
707 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
708 lineDiv.innerHTML = html.join("");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
709 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
710 function patchDisplay(updates) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
711 // Slightly different algorithm for IE (badInnerHTML), since
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
712 // there .innerHTML on PRE nodes is dumb, and discards
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
713 // whitespace.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
714 var sfrom = sel.from.line, sto = sel.to.line, off = 0,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
715 scratch = badInnerHTML && document.createElement("div");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
716 for (var i = 0, e = updates.length; i < e; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
717 var rec = updates[i];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
718 var extra = (rec.to - rec.from) - rec.domSize;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
719 var nodeAfter = lineDiv.childNodes[rec.domStart + rec.domSize + off] || null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
720 if (badInnerHTML)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
721 for (var j = Math.max(-extra, rec.domSize); j > 0; --j)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
722 lineDiv.removeChild(nodeAfter ? nodeAfter.previousSibling : lineDiv.lastChild);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
723 else if (extra) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
724 for (var j = Math.max(0, extra); j > 0; --j)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
725 lineDiv.insertBefore(document.createElement("pre"), nodeAfter);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
726 for (var j = Math.max(0, -extra); j > 0; --j)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
727 lineDiv.removeChild(nodeAfter ? nodeAfter.previousSibling : lineDiv.lastChild);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
728 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
729 var node = lineDiv.childNodes[rec.domStart + off], inSel = sfrom < rec.from && sto >= rec.from;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
730 for (var j = rec.from; j < rec.to; ++j) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
731 var ch1 = null, ch2 = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
732 if (inSel) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
733 ch1 = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
734 if (sto == j) {inSel = false; ch2 = sel.to.ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
735 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
736 else if (sfrom == j) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
737 if (sto == j) {ch1 = sel.from.ch; ch2 = sel.to.ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
738 else {inSel = true; ch1 = sel.from.ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
739 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
740 if (badInnerHTML) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
741 scratch.innerHTML = lines[j].getHTML(ch1, ch2, true);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
742 lineDiv.insertBefore(scratch.firstChild, nodeAfter);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
743 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
744 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
745 node.innerHTML = lines[j].getHTML(ch1, ch2, false);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
746 node.className = lines[j].className || "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
747 node = node.nextSibling;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
748 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
749 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
750 off += extra;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
751 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
752 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
753
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
754 function updateGutter() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
755 if (!options.gutter && !options.lineNumbers) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
756 var hText = mover.offsetHeight, hEditor = wrapper.clientHeight;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
757 gutter.style.height = (hText - hEditor < 2 ? hEditor : hText) + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
758 var html = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
759 for (var i = showingFrom; i < showingTo; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
760 var marker = lines[i].gutterMarker;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
761 var text = options.lineNumbers ? i + options.firstLineNumber : null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
762 if (marker && marker.text)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
763 text = marker.text.replace("%N%", text != null ? text : "");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
764 else if (text == null)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
765 text = "\u00a0";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
766 html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text, "</pre>");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
767 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
768 gutter.style.display = "none";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
769 gutterText.innerHTML = html.join("");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
770 var minwidth = String(lines.length).length, firstNode = gutterText.firstChild, val = eltText(firstNode), pad = "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
771 while (val.length + pad.length < minwidth) pad += "\u00a0";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
772 if (pad) firstNode.insertBefore(document.createTextNode(pad), firstNode.firstChild);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
773 gutter.style.display = "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
774 lineSpace.style.marginLeft = gutter.offsetWidth + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
775 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
776 function updateCursor() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
777 var head = sel.inverted ? sel.from : sel.to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
778 var x = charX(head.line, head.ch) + "px", y = (head.line - showingFrom) * lineHeight() + "px";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
779 inputDiv.style.top = y; inputDiv.style.left = x;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
780 if (posEq(sel.from, sel.to)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
781 cursor.style.top = y; cursor.style.left = x;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
782 cursor.style.display = "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
783 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
784 else cursor.style.display = "none";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
785 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
786
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
787 // Update the selection. Last two args are only used by
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
788 // updateLines, since they have to be expressed in the line
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
789 // numbers before the update.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
790 function setSelection(from, to, oldFrom, oldTo) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
791 if (posEq(sel.from, from) && posEq(sel.to, to)) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
792 var sh = shiftSelecting && clipPos(shiftSelecting);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
793 if (posLess(to, from)) {var tmp = to; to = from; from = tmp;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
794 if (sh) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
795 if (posLess(sh, from)) from = sh;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
796 else if (posLess(to, sh)) to = sh;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
797 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
798
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
799 var startEq = posEq(sel.to, to), endEq = posEq(sel.from, from);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
800 if (posEq(from, to)) sel.inverted = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
801 else if (startEq && !endEq) sel.inverted = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
802 else if (endEq && !startEq) sel.inverted = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
803
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
804 // Some ugly logic used to only mark the lines that actually did
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
805 // see a change in selection as changed, rather than the whole
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
806 // selected range.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
807 if (oldFrom == null) {oldFrom = sel.from.line; oldTo = sel.to.line;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
808 if (posEq(from, to)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
809 if (!posEq(sel.from, sel.to))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
810 changes.push({from: oldFrom, to: oldTo + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
811 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
812 else if (posEq(sel.from, sel.to)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
813 changes.push({from: from.line, to: to.line + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
814 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
815 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
816 if (!posEq(from, sel.from)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
817 if (from.line < oldFrom)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
818 changes.push({from: from.line, to: Math.min(to.line, oldFrom) + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
819 else
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
820 changes.push({from: oldFrom, to: Math.min(oldTo, from.line) + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
821 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
822 if (!posEq(to, sel.to)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
823 if (to.line < oldTo)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
824 changes.push({from: Math.max(oldFrom, from.line), to: oldTo + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
825 else
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
826 changes.push({from: Math.max(from.line, oldTo), to: to.line + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
827 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
828 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
829 sel.from = from; sel.to = to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
830 selectionChanged = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
831 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
832 function setCursor(line, ch) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
833 var pos = clipPos({line: line, ch: ch || 0});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
834 setSelection(pos, pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
835 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
836
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
837 function clipLine(n) {return Math.max(0, Math.min(n, lines.length-1));}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
838 function clipPos(pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
839 if (pos.line < 0) return {line: 0, ch: 0};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
840 if (pos.line >= lines.length) return {line: lines.length-1, ch: lines[lines.length-1].text.length};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
841 var ch = pos.ch, linelen = lines[pos.line].text.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
842 if (ch == null || ch > linelen) return {line: pos.line, ch: linelen};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
843 else if (ch < 0) return {line: pos.line, ch: 0};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
844 else return pos;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
845 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
846
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
847 function scrollPage(down) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
848 var linesPerPage = Math.floor(wrapper.clientHeight / lineHeight()), head = sel.inverted ? sel.from : sel.to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
849 setCursor(head.line + (Math.max(linesPerPage - 1, 1) * (down ? 1 : -1)), head.ch);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
850 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
851 function scrollEnd(top) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
852 setCursor(top ? 0 : lines.length - 1);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
853 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
854 function selectAll() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
855 var endLine = lines.length - 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
856 setSelection({line: 0, ch: 0}, {line: endLine, ch: lines[endLine].text.length});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
857 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
858 function selectWordAt(pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
859 var line = lines[pos.line].text;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
860 var start = pos.ch, end = pos.ch;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
861 while (start > 0 && /\w/.test(line.charAt(start - 1))) --start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
862 while (end < line.length - 1 && /\w/.test(line.charAt(end))) ++end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
863 setSelection({line: pos.line, ch: start}, {line: pos.line, ch: end});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
864 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
865 function handleEnter() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
866 replaceSelection("\n", "end");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
867 if (options.enterMode != "flat")
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
868 indentLine(sel.from.line, options.enterMode == "keep" ? "prev" : "smart");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
869 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
870 function handleTab(shift) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
871 shiftSelecting = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
872 switch (options.tabMode) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
873 case "default":
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
874 return false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
875 case "indent":
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
876 for (var i = sel.from.line, e = sel.to.line; i <= e; ++i) indentLine(i, "smart");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
877 break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
878 case "classic":
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
879 if (posEq(sel.from, sel.to)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
880 if (shift) indentLine(sel.from.line, "smart");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
881 else replaceSelection("\t", "end");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
882 break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
883 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
884 case "shift":
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
885 for (var i = sel.from.line, e = sel.to.line; i <= e; ++i) indentLine(i, shift ? "subtract" : "add");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
886 break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
887 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
888 return true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
889 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
890
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
891 function indentLine(n, how) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
892 if (how == "smart") {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
893 if (!mode.indent) how = "prev";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
894 else var state = getStateBefore(n);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
895 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
896
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
897 var line = lines[n], curSpace = line.indentation(), curSpaceString = line.text.match(/^\s*/)[0], indentation;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
898 if (how == "prev") {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
899 if (n) indentation = lines[n-1].indentation();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
900 else indentation = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
901 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
902 else if (how == "smart") indentation = mode.indent(state, line.text.slice(curSpaceString.length));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
903 else if (how == "add") indentation = curSpace + options.indentUnit;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
904 else if (how == "subtract") indentation = curSpace - options.indentUnit;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
905 indentation = Math.max(0, indentation);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
906 var diff = indentation - curSpace;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
907
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
908 if (!diff) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
909 if (sel.from.line != n && sel.to.line != n) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
910 var indentString = curSpaceString;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
911 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
912 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
913 var indentString = "", pos = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
914 if (options.indentWithTabs)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
915 for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
916 while (pos < indentation) {++pos; indentString += " ";}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
917 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
918
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
919 replaceRange(indentString, {line: n, ch: 0}, {line: n, ch: curSpaceString.length});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
920 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
921
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
922 function loadMode() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
923 mode = CodeMirror.getMode(options, options.mode);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
924 for (var i = 0, l = lines.length; i < l; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
925 lines[i].stateAfter = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
926 work = [0];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
927 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
928 function gutterChanged() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
929 var visible = options.gutter || options.lineNumbers;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
930 gutter.style.display = visible ? "" : "none";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
931 if (visible) updateGutter();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
932 else lineDiv.parentNode.style.marginLeft = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
933 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
934
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
935 function markText(from, to, className) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
936 from = clipPos(from); to = clipPos(to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
937 var accum = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
938 function add(line, from, to, className) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
939 var line = lines[line], mark = line.addMark(from, to, className);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
940 mark.line = line;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
941 accum.push(mark);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
942 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
943 if (from.line == to.line) add(from.line, from.ch, to.ch, className);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
944 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
945 add(from.line, from.ch, null, className);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
946 for (var i = from.line + 1, e = to.line; i < e; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
947 add(i, 0, null, className);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
948 add(to.line, 0, to.ch, className);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
949 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
950 changes.push({from: from.line, to: to.line + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
951 return function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
952 var start, end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
953 for (var i = 0; i < accum.length; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
954 var mark = accum[i], found = indexOf(lines, mark.line);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
955 mark.line.removeMark(mark);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
956 if (found > -1) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
957 if (start == null) start = found;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
958 end = found;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
959 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
960 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
961 if (start != null) changes.push({from: start, to: end + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
962 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
963 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
964
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
965 function addGutterMarker(line, text, className) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
966 if (typeof line == "number") line = lines[clipLine(line)];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
967 line.gutterMarker = {text: text, style: className};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
968 updateGutter();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
969 return line;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
970 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
971 function removeGutterMarker(line) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
972 if (typeof line == "number") line = lines[clipLine(line)];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
973 line.gutterMarker = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
974 updateGutter();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
975 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
976 function setLineClass(line, className) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
977 if (typeof line == "number") {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
978 var no = line;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
979 line = lines[clipLine(line)];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
980 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
981 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
982 var no = indexOf(lines, line);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
983 if (no == -1) return null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
984 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
985 line.className = className;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
986 changes.push({from: no, to: no + 1});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
987 return line;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
988 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
989
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
990 function lineInfo(line) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
991 if (typeof line == "number") {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
992 var n = line;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
993 line = lines[line];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
994 if (!line) return null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
995 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
996 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
997 var n = indexOf(lines, line);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
998 if (n == -1) return null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
999 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1000 var marker = line.gutterMarker;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1001 return {line: n, text: line.text, markerText: marker && marker.text, markerClass: marker && marker.style};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1002 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1003
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1004 // These are used to go from pixel positions to character
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1005 // positions, taking tabs into account.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1006 function charX(line, pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1007 var text = lines[line].text, span = measure.firstChild;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1008 if (text.lastIndexOf("\t", pos) == -1) return pos * charWidth();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1009 var old = span.firstChild.nodeValue;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1010 try {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1011 span.firstChild.nodeValue = text.slice(0, pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1012 return span.offsetWidth;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1013 } finally {span.firstChild.nodeValue = old;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1014 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1015 function charFromX(line, x) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1016 var text = lines[line].text, cw = charWidth();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1017 if (x <= 0) return 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1018 if (text.indexOf("\t") == -1) return Math.min(text.length, Math.round(x / cw));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1019 var mspan = measure.firstChild, mtext = mspan.firstChild, old = mtext.nodeValue;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1020 try {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1021 mtext.nodeValue = text;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1022 var from = 0, fromX = 0, to = text.length, toX = mspan.offsetWidth;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1023 if (x > toX) return to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1024 for (;;) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1025 if (to - from <= 1) return (toX - x > x - fromX) ? from : to;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1026 var middle = Math.ceil((from + to) / 2);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1027 mtext.nodeValue = text.slice(0, middle);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1028 var curX = mspan.offsetWidth;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1029 if (curX > x) {to = middle; toX = curX;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1030 else {from = middle; fromX = curX;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1031 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1032 } finally {mtext.nodeValue = old;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1033 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1034
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1035 function localCoords(pos, inLineWrap) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1036 var lh = lineHeight(), line = pos.line - (inLineWrap ? showingFrom : 0);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1037 return {x: charX(pos.line, pos.ch), y: line * lh, yBot: (line + 1) * lh};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1038 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1039 function pageCoords(pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1040 var local = localCoords(pos, true), off = eltOffset(lineSpace);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1041 return {x: off.left + local.x, y: off.top + local.y, yBot: off.top + local.yBot};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1042 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1043
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1044 function lineHeight() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1045 var nlines = lineDiv.childNodes.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1046 if (nlines) return lineDiv.offsetHeight / nlines;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1047 else return measure.firstChild.offsetHeight || 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1048 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1049 function charWidth() {return (measure.firstChild.offsetWidth || 320) / 40;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1050 function paddingTop() {return lineSpace.offsetTop;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1051 function paddingLeft() {return lineSpace.offsetLeft;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1052
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1053 function posFromMouse(e, liberal) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1054 var off = eltOffset(lineSpace),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1055 x = e.pageX() - off.left,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1056 y = e.pageY() - off.top;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1057 if (!liberal && e.target() != lineSpace.parentNode && !(e.target() == wrapper && y > (lines.length * lineHeight())))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1058 for (var n = e.target(); n != lineDiv && n != cursor; n = n.parentNode)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1059 if (!n || n == wrapper) return null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1060 var line = showingFrom + Math.floor(y / lineHeight());
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1061 return clipPos({line: line, ch: charFromX(clipLine(line), x)});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1062 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1063 function onContextMenu(e) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1064 var pos = posFromMouse(e);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1065 if (!pos || window.opera) return; // Opera is difficult.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1066 if (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1067 setCursor(pos.line, pos.ch);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1068
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1069 var oldCSS = input.style.cssText;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1070 input.style.cssText = "position: fixed; width: 30px; height: 30px; top: " + (e.pageY() - 1) +
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1071 "px; left: " + (e.pageX() - 1) + "px; z-index: 1000; background: white; " +
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1072 "border-width: 0; outline: none; overflow: hidden;";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1073 var val = input.value = getSelection();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1074 input.focus();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1075 setSelRange(input, 0, val.length);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1076 if (gecko) e.stop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1077 leaveInputAlone = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1078 setTimeout(function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1079 if (input.value != val) operation(replaceSelection)(input.value, "end");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1080 input.style.cssText = oldCSS;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1081 leaveInputAlone = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1082 prepareInput();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1083 slowPoll();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1084 }, 50);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1085 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1086
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1087 // Cursor-blinking
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1088 function restartBlink() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1089 clearInterval(blinker);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1090 var on = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1091 cursor.style.visibility = "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1092 blinker = setInterval(function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1093 cursor.style.visibility = (on = !on) ? "" : "hidden";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1094 }, 650);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1095 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1096
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1097 var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1098 function matchBrackets(autoclear) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1099 var head = sel.inverted ? sel.from : sel.to, line = lines[head.line], pos = head.ch - 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1100 var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1101 if (!match) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1102 var ch = match.charAt(0), forward = match.charAt(1) == ">", d = forward ? 1 : -1, st = line.styles;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1103 for (var off = pos + 1, i = 0, e = st.length; i < e; i+=2)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1104 if ((off -= st[i].length) <= 0) {var style = st[i+1]; break;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1105
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1106 var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1107 function scan(line, from, to) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1108 if (!line.text) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1109 var st = line.styles, pos = forward ? 0 : line.text.length - 1, cur;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1110 for (var i = forward ? 0 : st.length - 2, e = forward ? st.length : -2; i != e; i += 2*d) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1111 var text = st[i];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1112 if (st[i+1] != null && st[i+1] != style) {pos += d * text.length; continue;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1113 for (var j = forward ? 0 : text.length - 1, te = forward ? text.length : -1; j != te; j += d, pos+=d) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1114 if (pos >= from && pos < to && re.test(cur = text.charAt(j))) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1115 var match = matching[cur];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1116 if (match.charAt(1) == ">" == forward) stack.push(cur);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1117 else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1118 else if (!stack.length) return {pos: pos, match: true};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1119 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1120 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1121 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1122 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1123 for (var i = head.line, e = forward ? Math.min(i + 50, lines.length) : Math.max(0, i - 50); i != e; i+=d) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1124 var line = lines[i], first = i == head.line;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1125 var found = scan(line, first && forward ? pos + 1 : 0, first && !forward ? pos : line.text.length);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1126 if (found) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1127 var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1128 var one = markText({line: head.line, ch: pos}, {line: head.line, ch: pos+1}, style),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1129 two = markText({line: i, ch: found.pos}, {line: i, ch: found.pos + 1}, style);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1130 var clear = operation(function(){one(); two();});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1131 if (autoclear) setTimeout(clear, 800);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1132 else bracketHighlighted = clear;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1133 break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1134 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1135 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1136 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1137
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1138 // Finds the line to start with when starting a parse. Tries to
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1139 // find a line with a stateAfter, so that it can start with a
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1140 // valid state. If that fails, it returns the line with the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1141 // smallest indentation, which tends to need the least context to
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1142 // parse correctly.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1143 function findStartLine(n) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1144 var minindent, minline;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1145 for (var search = n, lim = n - 40; search > lim; --search) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1146 if (search == 0) return 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1147 var line = lines[search-1];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1148 if (line.stateAfter) return search;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1149 var indented = line.indentation();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1150 if (minline == null || minindent > indented) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1151 minline = search;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1152 minindent = indented;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1153 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1154 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1155 return minline;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1156 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1157 function getStateBefore(n) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1158 var start = findStartLine(n), state = start && lines[start-1].stateAfter;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1159 if (!state) state = startState(mode);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1160 else state = copyState(mode, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1161 for (var i = start; i < n; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1162 var line = lines[i];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1163 line.highlight(mode, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1164 line.stateAfter = copyState(mode, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1165 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1166 if (!lines[n].stateAfter) work.push(n);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1167 return state;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1168 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1169 function highlightWorker() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1170 var end = +new Date + options.workTime;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1171 while (work.length) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1172 if (!lines[showingFrom].stateAfter) var task = showingFrom;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1173 else var task = work.pop();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1174 if (task >= lines.length) continue;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1175 var start = findStartLine(task), state = start && lines[start-1].stateAfter;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1176 if (state) state = copyState(mode, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1177 else state = startState(mode);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1178
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1179 for (var i = start, l = lines.length; i < l; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1180 var line = lines[i], hadState = line.stateAfter;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1181 if (+new Date > end) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1182 work.push(i);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1183 startWorker(options.workDelay);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1184 changes.push({from: task, to: i});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1185 return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1186 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1187 var changed = line.highlight(mode, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1188 line.stateAfter = copyState(mode, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1189 if (hadState && !changed && line.text) break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1190 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1191 changes.push({from: task, to: i});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1192 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1193 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1194 function startWorker(time) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1195 if (!work.length) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1196 highlight.set(time, operation(highlightWorker));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1197 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1198
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1199 // Operations are used to wrap changes in such a way that each
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1200 // change won't have to update the cursor and display (which would
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1201 // be awkward, slow, and error-prone), but instead updates are
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1202 // batched and then all combined and executed at once.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1203 function startOperation() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1204 updateInput = null; changes = []; textChanged = selectionChanged = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1205 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1206 function endOperation() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1207 var reScroll = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1208 if (selectionChanged) reScroll = !scrollCursorIntoView();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1209 if (changes.length) updateDisplay(changes);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1210 else if (selectionChanged) updateCursor();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1211 if (reScroll) scrollCursorIntoView();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1212 if (selectionChanged) restartBlink();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1213
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1214 // updateInput can be set to a boolean value to force/prevent an
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1215 // update.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1216 if (!leaveInputAlone && (updateInput === true || (updateInput !== false && selectionChanged)))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1217 prepareInput();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1218
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1219 if (selectionChanged && options.onCursorActivity)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1220 options.onCursorActivity(instance);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1221 if (textChanged && options.onChange)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1222 options.onChange(instance);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1223 if (selectionChanged && options.matchBrackets)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1224 setTimeout(operation(function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1225 if (bracketHighlighted) {bracketHighlighted(); bracketHighlighted = null;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1226 matchBrackets(false);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1227 }), 20);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1228 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1229 var nestedOperation = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1230 function operation(f) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1231 return function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1232 if (!nestedOperation++) startOperation();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1233 try {var result = f.apply(this, arguments);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1234 finally {if (!--nestedOperation) endOperation();}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1235 return result;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1236 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1237 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1238
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1239 function SearchCursor(query, pos, caseFold) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1240 this.atOccurrence = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1241 if (caseFold == null) caseFold = typeof query == "string" && query == query.toLowerCase();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1242
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1243 if (pos && typeof pos == "object") pos = clipPos(pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1244 else pos = {line: 0, ch: 0};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1245 this.pos = {from: pos, to: pos};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1246
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1247 // The matches method is filled in based on the type of query.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1248 // It takes a position and a direction, and returns an object
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1249 // describing the next occurrence of the query, or null if no
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1250 // more matches were found.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1251 if (typeof query != "string") // Regexp match
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1252 this.matches = function(reverse, pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1253 if (reverse) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1254 var line = lines[pos.line].text.slice(0, pos.ch), match = line.match(query), start = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1255 while (match) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1256 var ind = line.indexOf(match[0]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1257 start += ind;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1258 line = line.slice(ind + 1);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1259 var newmatch = line.match(query);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1260 if (newmatch) match = newmatch;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1261 else break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1262 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1263 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1264 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1265 var line = lines[pos.line].text.slice(pos.ch), match = line.match(query),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1266 start = match && pos.ch + line.indexOf(match[0]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1267 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1268 if (match)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1269 return {from: {line: pos.line, ch: start},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1270 to: {line: pos.line, ch: start + match[0].length},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1271 match: match};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1272 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1273 else { // String query
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1274 if (caseFold) query = query.toLowerCase();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1275 var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1276 var target = query.split("\n");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1277 // Different methods for single-line and multi-line queries
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1278 if (target.length == 1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1279 this.matches = function(reverse, pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1280 var line = fold(lines[pos.line].text), len = query.length, match;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1281 if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1282 : (match = line.indexOf(query, pos.ch)) != -1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1283 return {from: {line: pos.line, ch: match},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1284 to: {line: pos.line, ch: match + len}};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1285 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1286 else
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1287 this.matches = function(reverse, pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1288 var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(lines[ln].text);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1289 var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1290 if (reverse ? offsetA >= pos.ch || offsetA != match.length
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1291 : offsetA <= pos.ch || offsetA != line.length - match.length)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1292 return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1293 for (;;) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1294 if (reverse ? !ln : ln == lines.length - 1) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1295 line = fold(lines[ln += reverse ? -1 : 1].text);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1296 match = target[reverse ? --idx : ++idx];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1297 if (idx > 0 && idx < target.length - 1) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1298 if (line != match) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1299 else continue;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1300 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1301 var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1302 if (reverse ? offsetB != line.length - match.length : offsetB != match.length)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1303 return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1304 var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1305 return {from: reverse ? end : start, to: reverse ? start : end};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1306 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1307 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1308 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1309 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1310
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1311 SearchCursor.prototype = {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1312 findNext: function() {return this.find(false);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1313 findPrevious: function() {return this.find(true);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1314
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1315 find: function(reverse) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1316 var self = this, pos = clipPos(reverse ? this.pos.from : this.pos.to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1317 function savePosAndFail(line) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1318 var pos = {line: line, ch: 0};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1319 self.pos = {from: pos, to: pos};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1320 self.atOccurrence = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1321 return false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1322 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1323
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1324 for (;;) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1325 if (this.pos = this.matches(reverse, pos)) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1326 this.atOccurrence = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1327 return this.pos.match || true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1328 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1329 if (reverse) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1330 if (!pos.line) return savePosAndFail(0);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1331 pos = {line: pos.line-1, ch: lines[pos.line-1].text.length};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1332 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1333 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1334 if (pos.line == lines.length - 1) return savePosAndFail(lines.length);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1335 pos = {line: pos.line+1, ch: 0};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1336 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1337 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1338 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1339
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1340 from: function() {if (this.atOccurrence) return copyPos(this.pos.from);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1341 to: function() {if (this.atOccurrence) return copyPos(this.pos.to);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1342 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1343
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1344 return instance;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1345 } // (end of function CodeMirror)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1346
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1347 // The default configuration options.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1348 CodeMirror.defaults = {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1349 value: "",
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1350 mode: null,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1351 indentUnit: 2,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1352 indentWithTabs: false,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1353 tabMode: "classic",
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1354 enterMode: "indent",
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1355 electricChars: true,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1356 onKeyEvent: null,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1357 lineNumbers: false,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1358 gutter: false,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1359 firstLineNumber: 1,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1360 readOnly: false,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1361 onChange: null,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1362 onCursorActivity: null,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1363 onGutterClick: null,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1364 onFocus: null, onBlur: null, onScroll: null,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1365 matchBrackets: false,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1366 workTime: 100,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1367 workDelay: 200,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1368 undoDepth: 40,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1369 tabindex: null
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1370 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1371
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1372 // Known modes, by name and by MIME
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1373 var modes = {}, mimeModes = {};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1374 CodeMirror.defineMode = function(name, mode) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1375 if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1376 modes[name] = mode;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1377 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1378 CodeMirror.defineMIME = function(mime, spec) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1379 mimeModes[mime] = spec;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1380 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1381 CodeMirror.getMode = function(options, spec) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1382 if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1383 spec = mimeModes[spec];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1384 if (typeof spec == "string")
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1385 var mname = spec, config = {};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1386 else
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1387 var mname = spec.name, config = spec;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1388 var mfactory = modes[mname];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1389 if (!mfactory) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1390 if (window.console) console.warn("No mode " + mname + " found, falling back to plain text.");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1391 return CodeMirror.getMode(options, "text/plain");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1392 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1393 return mfactory(options, config);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1394 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1395 CodeMirror.listModes = function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1396 var list = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1397 for (var m in modes)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1398 if (modes.propertyIsEnumerable(m)) list.push(m);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1399 return list;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1400 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1401 CodeMirror.listMIMEs = function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1402 var list = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1403 for (var m in mimeModes)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1404 if (mimeModes.propertyIsEnumerable(m)) list.push(m);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1405 return list;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1406 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1407
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1408 CodeMirror.fromTextArea = function(textarea, options) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1409 if (!options) options = {};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1410 options.value = textarea.value;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1411 if (!options.tabindex && textarea.tabindex)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1412 options.tabindex = textarea.tabindex;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1413
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1414 function save() {textarea.value = instance.getValue();}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1415 if (textarea.form) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1416 // Deplorable hack to make the submit method do the right thing.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1417 var rmSubmit = connect(textarea.form, "submit", save, true);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1418 if (typeof textarea.form.submit == "function") {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1419 var realSubmit = textarea.form.submit;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1420 function wrappedSubmit() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1421 save();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1422 textarea.form.submit = realSubmit;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1423 textarea.form.submit();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1424 textarea.form.submit = wrappedSubmit;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1425 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1426 textarea.form.submit = wrappedSubmit;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1427 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1428 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1429
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1430 textarea.style.display = "none";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1431 var instance = CodeMirror(function(node) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1432 textarea.parentNode.insertBefore(node, textarea.nextSibling);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1433 }, options);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1434 instance.save = save;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1435 instance.toTextArea = function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1436 save();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1437 textarea.parentNode.removeChild(instance.getWrapperElement());
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1438 textarea.style.display = "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1439 if (textarea.form) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1440 rmSubmit();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1441 if (typeof textarea.form.submit == "function")
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1442 textarea.form.submit = realSubmit;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1443 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1444 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1445 return instance;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1446 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1447
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1448 // Utility functions for working with state. Exported because modes
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1449 // sometimes need to do this.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1450 function copyState(mode, state) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1451 if (state === true) return state;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1452 if (mode.copyState) return mode.copyState(state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1453 var nstate = {};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1454 for (var n in state) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1455 var val = state[n];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1456 if (val instanceof Array) val = val.concat([]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1457 nstate[n] = val;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1458 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1459 return nstate;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1460 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1461 CodeMirror.startState = startState;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1462 function startState(mode, a1, a2) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1463 return mode.startState ? mode.startState(a1, a2) : true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1464 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1465 CodeMirror.copyState = copyState;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1466
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1467 // The character stream used by a mode's parser.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1468 function StringStream(string) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1469 this.pos = this.start = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1470 this.string = string;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1471 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1472 StringStream.prototype = {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1473 eol: function() {return this.pos >= this.string.length;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1474 sol: function() {return this.pos == 0;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1475 peek: function() {return this.string.charAt(this.pos);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1476 next: function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1477 if (this.pos < this.string.length)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1478 return this.string.charAt(this.pos++);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1479 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1480 eat: function(match) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1481 var ch = this.string.charAt(this.pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1482 if (typeof match == "string") var ok = ch == match;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1483 else var ok = ch && (match.test ? match.test(ch) : match(ch));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1484 if (ok) {++this.pos; return ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1485 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1486 eatWhile: function(match) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1487 var start = this.start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1488 while (this.eat(match)){}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1489 return this.pos > start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1490 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1491 eatSpace: function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1492 var start = this.pos;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1493 while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1494 return this.pos > start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1495 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1496 skipToEnd: function() {this.pos = this.string.length;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1497 skipTo: function(ch) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1498 var found = this.string.indexOf(ch, this.pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1499 if (found > -1) {this.pos = found; return true;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1500 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1501 backUp: function(n) {this.pos -= n;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1502 column: function() {return countColumn(this.string, this.start);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1503 indentation: function() {return countColumn(this.string);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1504 match: function(pattern, consume, caseInsensitive) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1505 if (typeof pattern == "string") {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1506 function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1507 if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1508 if (consume !== false) this.pos += pattern.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1509 return true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1510 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1511 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1512 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1513 var match = this.string.slice(this.pos).match(pattern);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1514 if (match && consume !== false) this.pos += match[0].length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1515 return match;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1516 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1517 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1518 current: function(){return this.string.slice(this.start, this.pos);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1519 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1520
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1521 // Line objects. These hold state related to a line, including
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1522 // highlighting info (the styles array).
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1523 function Line(text, styles) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1524 this.styles = styles || [text, null];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1525 this.stateAfter = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1526 this.text = text;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1527 this.marked = this.gutterMarker = this.className = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1528 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1529 Line.prototype = {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1530 // Replace a piece of a line, keeping the styles around it intact.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1531 replace: function(from, to, text) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1532 var st = [], mk = this.marked;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1533 copyStyles(0, from, this.styles, st);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1534 if (text) st.push(text, null);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1535 copyStyles(to, this.text.length, this.styles, st);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1536 this.styles = st;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1537 this.text = this.text.slice(0, from) + text + this.text.slice(to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1538 this.stateAfter = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1539 if (mk) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1540 var diff = text.length - (to - from), end = this.text.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1541 function fix(n) {return n <= Math.min(to, to + diff) ? n : n + diff;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1542 for (var i = 0; i < mk.length; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1543 var mark = mk[i], del = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1544 if (mark.from >= end) del = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1545 else {mark.from = fix(mark.from); if (mark.to != null) mark.to = fix(mark.to);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1546 if (del || mark.from >= mark.to) {mk.splice(i, 1); i--;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1547 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1548 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1549 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1550 // Split a line in two, again keeping styles intact.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1551 split: function(pos, textBefore) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1552 var st = [textBefore, null];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1553 copyStyles(pos, this.text.length, this.styles, st);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1554 return new Line(textBefore + this.text.slice(pos), st);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1555 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1556 addMark: function(from, to, style) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1557 var mk = this.marked, mark = {from: from, to: to, style: style};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1558 if (this.marked == null) this.marked = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1559 this.marked.push(mark);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1560 this.marked.sort(function(a, b){return a.from - b.from;});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1561 return mark;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1562 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1563 removeMark: function(mark) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1564 var mk = this.marked;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1565 if (!mk) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1566 for (var i = 0; i < mk.length; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1567 if (mk[i] == mark) {mk.splice(i, 1); break;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1568 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1569 // Run the given mode's parser over a line, update the styles
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1570 // array, which contains alternating fragments of text and CSS
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1571 // classes.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1572 highlight: function(mode, state) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1573 var stream = new StringStream(this.text), st = this.styles, pos = 0, changed = false;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1574 while (!stream.eol()) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1575 var style = mode.token(stream, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1576 var substr = this.text.slice(stream.start, stream.pos);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1577 stream.start = stream.pos;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1578 if (pos && st[pos-1] == style)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1579 st[pos-2] += substr;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1580 else if (substr) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1581 if (!changed && st[pos] != substr || st[pos+1] != style) changed = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1582 st[pos++] = substr; st[pos++] = style;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1583 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1584 // Give up when line is ridiculously long
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1585 if (stream.pos > 5000) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1586 st[pos++] = this.text.slice(stream.pos); st[pos++] = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1587 break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1588 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1589 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1590 if (st.length != pos) {st.length = pos; changed = true;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1591 return changed;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1592 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1593 // Fetch the parser token for a given character. Useful for hacks
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1594 // that want to inspect the mode state (say, for completion).
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1595 getTokenAt: function(mode, state, ch) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1596 var txt = this.text, stream = new StringStream(txt);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1597 while (stream.pos < ch && !stream.eol()) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1598 stream.start = stream.pos;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1599 var style = mode.token(stream, state);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1600 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1601 return {start: stream.start,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1602 end: stream.pos,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1603 string: stream.current(),
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1604 className: style || null,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1605 state: state};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1606 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1607 indentation: function() {return countColumn(this.text);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1608 // Produces an HTML fragment for the line, taking selection,
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1609 // marking, and highlighting into account.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1610 getHTML: function(sfrom, sto, includePre) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1611 var html = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1612 if (includePre)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1613 html.push(this.className ? '<pre class="' + this.className + '">': "<pre>");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1614 function span(text, style) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1615 if (!text) return;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1616 if (style) html.push('<span class="', style, '">', htmlEscape(text), "</span>");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1617 else html.push(htmlEscape(text));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1618 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1619 var st = this.styles, allText = this.text, marked = this.marked;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1620 if (sfrom == sto) sfrom = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1621
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1622 if (!allText)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1623 span(" ", sfrom != null && sto == null ? "CodeMirror-selected" : null);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1624 else if (!marked && sfrom == null)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1625 for (var i = 0, e = st.length; i < e; i+=2) span(st[i], st[i+1]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1626 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1627 var pos = 0, i = 0, text = "", style, sg = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1628 var markpos = -1, mark = null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1629 function nextMark() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1630 if (marked) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1631 markpos += 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1632 mark = (markpos < marked.length) ? marked[markpos] : null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1633 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1634 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1635 nextMark();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1636 while (pos < allText.length) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1637 var upto = allText.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1638 var extraStyle = "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1639 if (sfrom != null) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1640 if (sfrom > pos) upto = sfrom;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1641 else if (sto == null || sto > pos) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1642 extraStyle = " CodeMirror-selected";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1643 if (sto != null) upto = Math.min(upto, sto);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1644 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1645 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1646 while (mark && mark.to != null && mark.to <= pos) nextMark();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1647 if (mark) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1648 if (mark.from > pos) upto = Math.min(upto, mark.from);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1649 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1650 extraStyle += " " + mark.style;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1651 if (mark.to != null) upto = Math.min(upto, mark.to);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1652 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1653 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1654 for (;;) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1655 var end = pos + text.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1656 var apliedStyle = style;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1657 if (extraStyle) apliedStyle = style ? style + extraStyle : extraStyle;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1658 span(end > upto ? text.slice(0, upto - pos) : text, apliedStyle);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1659 if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1660 pos = end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1661 text = st[i++]; style = st[i++];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1662 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1663 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1664 if (sfrom != null && sto == null) span(" ", "CodeMirror-selected");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1665 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1666 if (includePre) html.push("</pre>");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1667 return html.join("");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1668 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1669 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1670 // Utility used by replace and split above
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1671 function copyStyles(from, to, source, dest) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1672 for (var i = 0, pos = 0, state = 0; pos < to; i+=2) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1673 var part = source[i], end = pos + part.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1674 if (state == 0) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1675 if (end > from) dest.push(part.slice(from - pos, Math.min(part.length, to - pos)), source[i+1]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1676 if (end >= from) state = 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1677 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1678 else if (state == 1) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1679 if (end > to) dest.push(part.slice(0, to - pos), source[i+1]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1680 else dest.push(part, source[i+1]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1681 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1682 pos = end;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1683 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1684 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1685
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1686 // The history object 'chunks' changes that are made close together
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1687 // and at almost the same time into bigger undoable units.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1688 function History() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1689 this.time = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1690 this.done = []; this.undone = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1691 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1692 History.prototype = {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1693 addChange: function(start, added, old) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1694 this.undone.length = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1695 var time = +new Date, last = this.done[this.done.length - 1];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1696 if (time - this.time > 400 || !last ||
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1697 last.start > start + added || last.start + last.added < start - last.added + last.old.length)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1698 this.done.push({start: start, added: added, old: old});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1699 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1700 var oldoff = 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1701 if (start < last.start) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1702 for (var i = last.start - start - 1; i >= 0; --i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1703 last.old.unshift(old[i]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1704 last.added += last.start - start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1705 last.start = start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1706 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1707 else if (last.start < start) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1708 oldoff = start - last.start;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1709 added += oldoff;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1710 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1711 for (var i = last.added - oldoff, e = old.length; i < e; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1712 last.old.push(old[i]);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1713 if (last.added < added) last.added = added;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1714 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1715 this.time = time;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1716 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1717 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1718
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1719 // Event stopping compatibility wrapper.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1720 function stopEvent() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1721 if (this.preventDefault) {this.preventDefault(); this.stopPropagation();}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1722 else {this.returnValue = false; this.cancelBubble = true;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1723 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1724 // Ensure an event has a stop method.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1725 function addStop(event) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1726 if (!event.stop) event.stop = stopEvent;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1727 return event;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1728 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1729
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1730 // Event wrapper, exposing the few operations we need.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1731 function Event(orig) {this.e = orig;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1732 Event.prototype = {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1733 stop: function() {stopEvent.call(this.e);},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1734 target: function() {return this.e.target || this.e.srcElement;},
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1735 button: function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1736 if (this.e.which) return this.e.which;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1737 else if (this.e.button & 1) return 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1738 else if (this.e.button & 2) return 3;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1739 else if (this.e.button & 4) return 2;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1740 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1741 pageX: function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1742 if (this.e.pageX != null) return this.e.pageX;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1743 else return this.e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1744 },
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1745 pageY: function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1746 if (this.e.pageY != null) return this.e.pageY;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1747 else return this.e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1748 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1749 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1750
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1751 // Event handler registration. If disconnect is true, it'll return a
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1752 // function that unregisters the handler.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1753 function connect(node, type, handler, disconnect) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1754 function wrapHandler(event) {handler(new Event(event || window.event));}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1755 if (typeof node.addEventListener == "function") {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1756 node.addEventListener(type, wrapHandler, false);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1757 if (disconnect) return function() {node.removeEventListener(type, wrapHandler, false);};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1758 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1759 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1760 node.attachEvent("on" + type, wrapHandler);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1761 if (disconnect) return function() {node.detachEvent("on" + type, wrapHandler);};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1762 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1763 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1764
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1765 function Delayed() {this.id = null;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1766 Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1767
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1768 // Some IE versions don't preserve whitespace when setting the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1769 // innerHTML of a PRE tag.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1770 var badInnerHTML = (function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1771 var pre = document.createElement("pre");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1772 pre.innerHTML = " "; return !pre.innerHTML;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1773 })();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1774
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1775 var gecko = /gecko\/\d{7}/i.test(navigator.userAgent);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1776
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1777 var lineSep = "\n";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1778 // Feature-detect whether newlines in textareas are converted to \r\n
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1779 (function () {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1780 var te = document.createElement("textarea");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1781 te.value = "foo\nbar";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1782 if (te.value.indexOf("\r") > -1) lineSep = "\r\n";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1783 }());
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1784
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1785 var tabSize = 8;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1786 var mac = /Mac/.test(navigator.platform);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1787 var movementKeys = {};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1788 for (var i = 35; i <= 40; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1789 movementKeys[i] = movementKeys["c" + i] = true;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1790
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1791 // Counts the column offset in a string, taking tabs into account.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1792 // Used mostly to find indentation.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1793 function countColumn(string, end) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1794 if (end == null) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1795 end = string.search(/[^\s\u00a0]/);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1796 if (end == -1) end = string.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1797 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1798 for (var i = 0, n = 0; i < end; ++i) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1799 if (string.charAt(i) == "\t") n += tabSize - (n % tabSize);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1800 else ++n;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1801 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1802 return n;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1803 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1804
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1805 // Find the position of an element by following the offsetParent chain.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1806 function eltOffset(node) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1807 var x = 0, y = 0, n2 = node;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1808 for (var n = node; n; n = n.offsetParent) {x += n.offsetLeft; y += n.offsetTop;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1809 for (var n = node; n != document.body; n = n.parentNode) {x -= n.scrollLeft; y -= n.scrollTop;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1810 return {left: x, top: y};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1811 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1812 // Get a node's text content.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1813 function eltText(node) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1814 return node.textContent || node.innerText || node.nodeValue || "";
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1815 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1816
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1817 // Operations on {line, ch} objects.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1818 function posEq(a, b) {return a.line == b.line && a.ch == b.ch;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1819 function posLess(a, b) {return a.line < b.line || (a.line == b.line && a.ch < b.ch);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1820 function copyPos(x) {return {line: x.line, ch: x.ch};}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1821
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1822 function htmlEscape(str) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1823 return str.replace(/[<&]/g, function(str) {return str == "&" ? "&amp;" : "&lt;";});
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1824 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1825
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1826 // Used to position the cursor after an undo/redo by finding the
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1827 // last edited character.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1828 function editEnd(from, to) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1829 if (!to) return from ? from.length : 0;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1830 if (!from) return to.length;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1831 for (var i = from.length, j = to.length; i >= 0 && j >= 0; --i, --j)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1832 if (from.charAt(i) != to.charAt(j)) break;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1833 return j + 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1834 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1835
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1836 function indexOf(collection, elt) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1837 if (collection.indexOf) return collection.indexOf(elt);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1838 for (var i = 0, e = collection.length; i < e; ++i)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1839 if (collection[i] == elt) return i;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1840 return -1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1841 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1842
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1843 // See if "".split is the broken IE version, if so, provide an
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1844 // alternative way to split lines.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1845 if ("\n\nb".split(/\n/).length != 3)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1846 var splitLines = function(string) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1847 var pos = 0, nl, result = [];
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1848 while ((nl = string.indexOf("\n", pos)) > -1) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1849 result.push(string.slice(pos, string.charAt(nl-1) == "\r" ? nl - 1 : nl));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1850 pos = nl + 1;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1851 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1852 result.push(string.slice(pos));
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1853 return result;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1854 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1855 else
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1856 var splitLines = function(string){return string.split(/\r?\n/);};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1857
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1858 // Sane model of finding and setting the selection in a textarea
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1859 if (window.getSelection) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1860 var selRange = function(te) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1861 try {return {start: te.selectionStart, end: te.selectionEnd};}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1862 catch(e) {return null;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1863 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1864 var setSelRange = function(te, start, end) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1865 try {te.setSelectionRange(start, end);}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1866 catch(e) {} // Fails on Firefox when textarea isn't part of the document
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1867 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1868 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1869 // IE model. Don't ask.
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1870 else {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1871 var selRange = function(te) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1872 try {var range = document.selection.createRange();}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1873 catch(e) {return null;}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1874 if (!range || range.parentElement() != te) return null;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1875 var val = te.value, len = val.length, localRange = te.createTextRange();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1876 localRange.moveToBookmark(range.getBookmark());
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1877 var endRange = te.createTextRange();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1878 endRange.collapse(false);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1879
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1880 if (localRange.compareEndPoints("StartToEnd", endRange) > -1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1881 return {start: len, end: len};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1882
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1883 var start = -localRange.moveStart("character", -len);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1884 for (var i = val.indexOf("\r"); i > -1 && i < start; i = val.indexOf("\r", i+1), start++) {}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1885
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1886 if (localRange.compareEndPoints("EndToEnd", endRange) > -1)
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1887 return {start: start, end: len};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1888
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1889 var end = -localRange.moveEnd("character", -len);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1890 for (var i = val.indexOf("\r"); i > -1 && i < end; i = val.indexOf("\r", i+1), end++) {}
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1891 return {start: start, end: end};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1892 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1893 var setSelRange = function(te, start, end) {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1894 var range = te.createTextRange();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1895 range.collapse(true);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1896 var endrange = range.duplicate();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1897 var newlines = 0, txt = te.value;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1898 for (var pos = txt.indexOf("\n"); pos > -1 && pos < start; pos = txt.indexOf("\n", pos + 1))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1899 ++newlines;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1900 range.move("character", start - newlines);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1901 for (; pos > -1 && pos < end; pos = txt.indexOf("\n", pos + 1))
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1902 ++newlines;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1903 endrange.move("character", end - newlines);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1904 range.setEndPoint("EndToEnd", endrange);
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1905 range.select();
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1906 };
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1907 }
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1908
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1909 CodeMirror.defineMode("null", function() {
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1910 return {token: function(stream) {stream.skipToEnd();}};
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1911 });
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1912 CodeMirror.defineMIME("text/plain", "null");
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1913
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1914 return CodeMirror;
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1915 })();