Show More
This diff has been collapsed as it changes many lines, (2947 lines changed) Show them Hide them | |||||
@@ -4,7 +4,7 b'' | |||||
4 |
|
4 | |||
5 | // CodeMirror is the only global var we claim |
|
5 | // CodeMirror is the only global var we claim | |
6 | var CodeMirror = (function() { |
|
6 | var CodeMirror = (function() { | |
7 |
// This is the function that produces an editor instance. It |
|
7 | // This is the function that produces an editor instance. Its | |
8 | // closure is used to store the editor state. |
|
8 | // closure is used to store the editor state. | |
9 | function CodeMirror(place, givenOptions) { |
|
9 | function CodeMirror(place, givenOptions) { | |
10 | // Determine effective options based on given values and defaults. |
|
10 | // Determine effective options based on given values and defaults. | |
@@ -13,41 +13,65 b' var CodeMirror = (function() {' | |||||
13 | if (defaults.hasOwnProperty(opt)) |
|
13 | if (defaults.hasOwnProperty(opt)) | |
14 | options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt]; |
|
14 | options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt]; | |
15 |
|
15 | |||
16 | var targetDocument = options["document"]; |
|
|||
17 | // The element in which the editor lives. |
|
16 | // The element in which the editor lives. | |
18 |
var wrapper = |
|
17 | var wrapper = document.createElement("div"); | |
19 | wrapper.className = "CodeMirror"; |
|
18 | wrapper.className = "CodeMirror" + (options.lineWrapping ? " CodeMirror-wrap" : ""); | |
20 | // This mess creates the base DOM structure for the editor. |
|
19 | // This mess creates the base DOM structure for the editor. | |
21 | wrapper.innerHTML = |
|
20 | wrapper.innerHTML = | |
22 |
'<div style="overflow: hidden; position: relative; width: |
|
21 | '<div style="overflow: hidden; position: relative; width: 3px; height: 0px;">' + // Wraps and hides input textarea | |
23 |
'<textarea style="position: absolute; width: 1 |
|
22 | '<textarea style="position: absolute; padding: 0; width: 1px; height: 1em" wrap="off" ' + | |
24 | 'autocorrect="off" autocapitalize="off"></textarea></div>' + |
|
23 | 'autocorrect="off" autocapitalize="off"></textarea></div>' + | |
25 | '<div class="CodeMirror-scroll cm-s-' + options.theme + '">' + |
|
24 | '<div class="CodeMirror-scrollbar">' + // The vertical scrollbar. Horizontal scrolling is handled by the scroller itself. | |
|
25 | '<div class="CodeMirror-scrollbar-inner">' + // The empty scrollbar content, used solely for managing the scrollbar thumb. | |||
|
26 | '</div></div>' + // This must be before the scroll area because it's float-right. | |||
|
27 | '<div class="CodeMirror-scroll" tabindex="-1">' + | |||
26 | '<div style="position: relative">' + // Set to the height of the text, causes scrolling |
|
28 | '<div style="position: relative">' + // Set to the height of the text, causes scrolling | |
27 | '<div style="position: absolute; height: 0; width: 0; overflow: hidden;"></div>' + |
|
|||
28 | '<div style="position: relative">' + // Moved around its parent to cover visible view |
|
29 | '<div style="position: relative">' + // Moved around its parent to cover visible view | |
29 | '<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' + |
|
30 | '<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' + | |
30 | // Provides positioning relative to (visible) text origin |
|
31 | // Provides positioning relative to (visible) text origin | |
31 |
'<div class="CodeMirror-lines"><div style="position: relative |
|
32 | '<div class="CodeMirror-lines"><div style="position: relative; z-index: 0">' + | |
|
33 | // Used to measure text size | |||
|
34 | '<div style="position: absolute; width: 100%; height: 0; overflow: hidden; visibility: hidden;"></div>' + | |||
32 | '<pre class="CodeMirror-cursor"> </pre>' + // Absolutely positioned blinky cursor |
|
35 | '<pre class="CodeMirror-cursor"> </pre>' + // Absolutely positioned blinky cursor | |
33 | '<div></div>' + // This DIV contains the actual code |
|
36 | '<pre class="CodeMirror-cursor" style="visibility: hidden"> </pre>' + // Used to force a width | |
|
37 | '<div style="position: relative; z-index: -1"></div><div></div>' + // DIVs containing the selection and the actual code | |||
34 | '</div></div></div></div></div>'; |
|
38 | '</div></div></div></div></div>'; | |
35 | if (place.appendChild) place.appendChild(wrapper); else place(wrapper); |
|
39 | if (place.appendChild) place.appendChild(wrapper); else place(wrapper); | |
36 | // I've never seen more elegant code in my life. |
|
40 | // I've never seen more elegant code in my life. | |
37 | var inputDiv = wrapper.firstChild, input = inputDiv.firstChild, |
|
41 | var inputDiv = wrapper.firstChild, input = inputDiv.firstChild, | |
38 | scroller = wrapper.lastChild, code = scroller.firstChild, |
|
42 | scroller = wrapper.lastChild, code = scroller.firstChild, | |
39 |
me |
|
43 | mover = code.firstChild, gutter = mover.firstChild, gutterText = gutter.firstChild, | |
40 |
|
|
44 | lineSpace = gutter.nextSibling.firstChild, measure = lineSpace.firstChild, | |
41 | lineSpace = gutter.nextSibling.firstChild, |
|
45 | cursor = measure.nextSibling, widthForcer = cursor.nextSibling, | |
42 | cursor = lineSpace.firstChild, lineDiv = cursor.nextSibling; |
|
46 | selectionDiv = widthForcer.nextSibling, lineDiv = selectionDiv.nextSibling, | |
43 | if (options.tabindex != null) input.tabindex = options.tabindex; |
|
47 | scrollbar = inputDiv.nextSibling, scrollbarInner = scrollbar.firstChild; | |
|
48 | themeChanged(); keyMapChanged(); | |||
|
49 | // Needed to hide big blue blinking cursor on Mobile Safari | |||
|
50 | if (ios) input.style.width = "0px"; | |||
|
51 | if (!webkit) scroller.draggable = true; | |||
|
52 | lineSpace.style.outline = "none"; | |||
|
53 | if (options.tabindex != null) input.tabIndex = options.tabindex; | |||
|
54 | if (options.autofocus) focusInput(); | |||
44 | if (!options.gutter && !options.lineNumbers) gutter.style.display = "none"; |
|
55 | if (!options.gutter && !options.lineNumbers) gutter.style.display = "none"; | |
|
56 | // Needed to handle Tab key in KHTML | |||
|
57 | if (khtml) inputDiv.style.height = "1px", inputDiv.style.position = "absolute"; | |||
|
58 | ||||
|
59 | // Check for OS X >= 10.7. If so, we need to force a width on the scrollbar, and | |||
|
60 | // make it overlap the content. (But we only do this if the scrollbar doesn't already | |||
|
61 | // have a natural width. If the mouse is plugged in or the user sets the system pref | |||
|
62 | // to always show scrollbars, the scrollbar shouldn't overlap.) | |||
|
63 | if (mac_geLion) { | |||
|
64 | scrollbar.className += (overlapScrollbars() ? " cm-sb-overlap" : " cm-sb-nonoverlap"); | |||
|
65 | } else if (ie_lt8) { | |||
|
66 | // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8). | |||
|
67 | scrollbar.className += " cm-sb-ie7"; | |||
|
68 | } | |||
45 |
|
69 | |||
46 | // Check for problem with IE innerHTML not working when we have a |
|
70 | // Check for problem with IE innerHTML not working when we have a | |
47 | // P (or similar) parent node. |
|
71 | // P (or similar) parent node. | |
48 | try { stringWidth("x"); } |
|
72 | try { stringWidth("x"); } | |
49 | catch (e) { |
|
73 | catch (e) { | |
50 |
if (e.message.match(/ |
|
74 | if (e.message.match(/runtime/i)) | |
51 | e = new Error("A CodeMirror inside a P-style element does not work in Internet Explorer. (innerHTML bug)"); |
|
75 | e = new Error("A CodeMirror inside a P-style element does not work in Internet Explorer. (innerHTML bug)"); | |
52 | throw e; |
|
76 | throw e; | |
53 | } |
|
77 | } | |
@@ -55,34 +79,32 b' var CodeMirror = (function() {' | |||||
55 | // Delayed object wrap timeouts, making sure only one is active. blinker holds an interval. |
|
79 | // Delayed object wrap timeouts, making sure only one is active. blinker holds an interval. | |
56 | var poll = new Delayed(), highlight = new Delayed(), blinker; |
|
80 | var poll = new Delayed(), highlight = new Delayed(), blinker; | |
57 |
|
81 | |||
58 |
// mode holds a mode API object. |
|
82 | // mode holds a mode API object. doc is the tree of Line objects, | |
59 |
// |
|
83 | // work an array of lines that should be parsed, and history the | |
60 |
// |
|
84 | // undo history (instance of History constructor). | |
61 | // constructor). |
|
85 | var mode, doc = new BranchChunk([new LeafChunk([new Line("")])]), work, focused; | |
62 | var mode, lines = [new Line("")], work, focused; |
|
|||
63 | loadMode(); |
|
86 | loadMode(); | |
64 | // The selection. These are always maintained to point at valid |
|
87 | // The selection. These are always maintained to point at valid | |
65 | // positions. Inverted is used to remember that the user is |
|
88 | // positions. Inverted is used to remember that the user is | |
66 | // selecting bottom-to-top. |
|
89 | // selecting bottom-to-top. | |
67 | var sel = {from: {line: 0, ch: 0}, to: {line: 0, ch: 0}, inverted: false}; |
|
90 | var sel = {from: {line: 0, ch: 0}, to: {line: 0, ch: 0}, inverted: false}; | |
68 | // Selection-related flags. shiftSelecting obviously tracks |
|
91 | // Selection-related flags. shiftSelecting obviously tracks | |
69 |
// whether the user is holding shift. |
|
92 | // whether the user is holding shift. | |
70 | // to get around the fact that we can't create inverted |
|
93 | var shiftSelecting, lastClick, lastDoubleClick, lastScrollTop = 0, lastScrollLeft = 0, draggingText, | |
71 | // selections. See below. |
|
94 | overwrite = false, suppressEdits = false; | |
72 | var shiftSelecting, reducedSelection, lastClick, lastDoubleClick, draggingText; |
|
|||
73 | // Variables used by startOperation/endOperation to track what |
|
95 | // Variables used by startOperation/endOperation to track what | |
74 | // happened during the operation. |
|
96 | // happened during the operation. | |
75 |
var updateInput, changes, textChanged, selectionChanged, leaveInputAlone, |
|
97 | var updateInput, userSelChange, changes, textChanged, selectionChanged, leaveInputAlone, | |
|
98 | gutterDirty, callbacks; | |||
76 | // Current visible range (may be bigger than the view window). |
|
99 | // Current visible range (may be bigger than the view window). | |
77 |
var showingFrom = 0, showingTo = 0, last |
|
100 | var displayOffset = 0, showingFrom = 0, showingTo = 0, lastSizeC = 0; | |
78 | // editing will hold an object describing the things we put in the |
|
101 | // bracketHighlighted is used to remember that a bracket has been | |
79 | // textarea, to help figure out whether something changed. |
|
|||
80 | // bracketHighlighted is used to remember that a backet has been |
|
|||
81 | // marked. |
|
102 | // marked. | |
82 |
var |
|
103 | var bracketHighlighted; | |
83 | // Tracks the maximum line length so that the horizontal scrollbar |
|
104 | // Tracks the maximum line length so that the horizontal scrollbar | |
84 | // can be kept static when scrolling. |
|
105 | // can be kept static when scrolling. | |
85 | var maxLine = "", maxWidth; |
|
106 | var maxLine = "", updateMaxLine = false, maxLineChanged = true; | |
|
107 | var tabCache = {}; | |||
86 |
|
108 | |||
87 | // Initialize the content. |
|
109 | // Initialize the content. | |
88 | operation(function(){setValue(options.value || ""); updateInput = false;})(); |
|
110 | operation(function(){setValue(options.value || ""); updateInput = false;})(); | |
@@ -91,38 +113,53 b' var CodeMirror = (function() {' | |||||
91 | // Register our event handlers. |
|
113 | // Register our event handlers. | |
92 | connect(scroller, "mousedown", operation(onMouseDown)); |
|
114 | connect(scroller, "mousedown", operation(onMouseDown)); | |
93 | connect(scroller, "dblclick", operation(onDoubleClick)); |
|
115 | connect(scroller, "dblclick", operation(onDoubleClick)); | |
94 |
connect(lineSpace, " |
|
116 | connect(lineSpace, "selectstart", e_preventDefault); | |
95 | // Gecko browsers fire contextmenu *after* opening the menu, at |
|
117 | // Gecko browsers fire contextmenu *after* opening the menu, at | |
96 | // which point we can't mess with it anymore. Context menu is |
|
118 | // which point we can't mess with it anymore. Context menu is | |
97 | // handled in onMouseDown for Gecko. |
|
119 | // handled in onMouseDown for Gecko. | |
98 | if (!gecko) connect(scroller, "contextmenu", onContextMenu); |
|
120 | if (!gecko) connect(scroller, "contextmenu", onContextMenu); | |
99 |
connect(scroller, "scroll", |
|
121 | connect(scroller, "scroll", onScroll); | |
100 | updateDisplay([]); |
|
122 | connect(scrollbar, "scroll", onScroll); | |
101 | if (options.fixedGutter) gutter.style.left = scroller.scrollLeft + "px"; |
|
123 | connect(scrollbar, "mousedown", function() {setTimeout(focusInput, 0);}); | |
102 | if (options.onScroll) options.onScroll(instance); |
|
124 | connect(scroller, "mousewheel", onMouseWheel); | |
103 | }); |
|
125 | connect(scroller, "DOMMouseScroll", onMouseWheel); | |
104 | connect(window, "resize", function() {updateDisplay(true);}); |
|
126 | connect(window, "resize", function() {updateDisplay(true);}); | |
105 | connect(input, "keyup", operation(onKeyUp)); |
|
127 | connect(input, "keyup", operation(onKeyUp)); | |
106 |
connect(input, "input", |
|
128 | connect(input, "input", fastPoll); | |
107 | connect(input, "keydown", operation(onKeyDown)); |
|
129 | connect(input, "keydown", operation(onKeyDown)); | |
108 | connect(input, "keypress", operation(onKeyPress)); |
|
130 | connect(input, "keypress", operation(onKeyPress)); | |
109 | connect(input, "focus", onFocus); |
|
131 | connect(input, "focus", onFocus); | |
110 | connect(input, "blur", onBlur); |
|
132 | connect(input, "blur", onBlur); | |
111 |
|
133 | |||
112 | connect(scroller, "dragenter", e_stop); |
|
134 | if (options.dragDrop) { | |
113 |
connect(scroller, "drag |
|
135 | connect(scroller, "dragstart", onDragStart); | |
114 | connect(scroller, "drop", operation(onDrop)); |
|
136 | function drag_(e) { | |
|
137 | if (options.onDragEvent && options.onDragEvent(instance, addStop(e))) return; | |||
|
138 | e_stop(e); | |||
|
139 | } | |||
|
140 | connect(scroller, "dragenter", drag_); | |||
|
141 | connect(scroller, "dragover", drag_); | |||
|
142 | connect(scroller, "drop", operation(onDrop)); | |||
|
143 | } | |||
115 | connect(scroller, "paste", function(){focusInput(); fastPoll();}); |
|
144 | connect(scroller, "paste", function(){focusInput(); fastPoll();}); | |
116 |
connect(input, "paste", |
|
145 | connect(input, "paste", fastPoll); | |
117 |
connect(input, "cut", function(){ |
|
146 | connect(input, "cut", operation(function(){ | |
|
147 | if (!options.readOnly) replaceSelection(""); | |||
|
148 | })); | |||
|
149 | ||||
|
150 | // Needed to handle Tab key in KHTML | |||
|
151 | if (khtml) connect(code, "mouseup", function() { | |||
|
152 | if (document.activeElement == input) input.blur(); | |||
|
153 | focusInput(); | |||
|
154 | }); | |||
118 |
|
155 | |||
119 | // IE throws unspecified error in certain cases, when |
|
156 | // IE throws unspecified error in certain cases, when | |
120 | // trying to access activeElement before onload |
|
157 | // trying to access activeElement before onload | |
121 |
var hasFocus; try { hasFocus = ( |
|
158 | var hasFocus; try { hasFocus = (document.activeElement == input); } catch(e) { } | |
122 | if (hasFocus) setTimeout(onFocus, 20); |
|
159 | if (hasFocus || options.autofocus) setTimeout(onFocus, 20); | |
123 | else onBlur(); |
|
160 | else onBlur(); | |
124 |
|
161 | |||
125 |
function isLine(l) {return l >= 0 && l < |
|
162 | function isLine(l) {return l >= 0 && l < doc.size;} | |
126 | // The instance object that we'll return. Mostly calls out to |
|
163 | // The instance object that we'll return. Mostly calls out to | |
127 | // local functions in the CodeMirror function. Some do some extra |
|
164 | // local functions in the CodeMirror function. Some do some extra | |
128 | // range checking and/or clipping. operation is used to wrap the |
|
165 | // range checking and/or clipping. operation is used to wrap the | |
@@ -133,47 +170,74 b' var CodeMirror = (function() {' | |||||
133 | setValue: operation(setValue), |
|
170 | setValue: operation(setValue), | |
134 | getSelection: getSelection, |
|
171 | getSelection: getSelection, | |
135 | replaceSelection: operation(replaceSelection), |
|
172 | replaceSelection: operation(replaceSelection), | |
136 | focus: function(){focusInput(); onFocus(); fastPoll();}, |
|
173 | focus: function(){window.focus(); focusInput(); onFocus(); fastPoll();}, | |
137 | setOption: function(option, value) { |
|
174 | setOption: function(option, value) { | |
|
175 | var oldVal = options[option]; | |||
138 | options[option] = value; |
|
176 | options[option] = value; | |
139 | if (option == "lineNumbers" || option == "gutter" || option == "firstLineNumber") |
|
177 | if (option == "mode" || option == "indentUnit") loadMode(); | |
140 | operation(gutterChanged)(); |
|
178 | else if (option == "readOnly" && value == "nocursor") {onBlur(); input.blur();} | |
141 | else if (option == "mode" || option == "indentUnit") loadMode(); |
|
179 | else if (option == "readOnly" && !value) {resetInput(true);} | |
142 | else if (option == "readOnly" && value == "nocursor") input.blur(); |
|
180 | else if (option == "theme") themeChanged(); | |
143 | else if (option == "theme") scroller.className = scroller.className.replace(/cm-s-\w+/, "cm-s-" + value); |
|
181 | else if (option == "lineWrapping" && oldVal != value) operation(wrappingChanged)(); | |
|
182 | else if (option == "tabSize") updateDisplay(true); | |||
|
183 | else if (option == "keyMap") keyMapChanged(); | |||
|
184 | if (option == "lineNumbers" || option == "gutter" || option == "firstLineNumber" || option == "theme") { | |||
|
185 | gutterChanged(); | |||
|
186 | updateDisplay(true); | |||
|
187 | } | |||
144 | }, |
|
188 | }, | |
145 | getOption: function(option) {return options[option];}, |
|
189 | getOption: function(option) {return options[option];}, | |
146 | undo: operation(undo), |
|
190 | undo: operation(undo), | |
147 | redo: operation(redo), |
|
191 | redo: operation(redo), | |
148 | indentLine: operation(function(n, dir) { |
|
192 | indentLine: operation(function(n, dir) { | |
149 | if (isLine(n)) indentLine(n, dir == null ? "smart" : dir ? "add" : "subtract"); |
|
193 | if (typeof dir != "string") { | |
|
194 | if (dir == null) dir = options.smartIndent ? "smart" : "prev"; | |||
|
195 | else dir = dir ? "add" : "subtract"; | |||
|
196 | } | |||
|
197 | if (isLine(n)) indentLine(n, dir); | |||
150 | }), |
|
198 | }), | |
|
199 | indentSelection: operation(indentSelected), | |||
151 | historySize: function() {return {undo: history.done.length, redo: history.undone.length};}, |
|
200 | historySize: function() {return {undo: history.done.length, redo: history.undone.length};}, | |
152 | clearHistory: function() {history = new History();}, |
|
201 | clearHistory: function() {history = new History();}, | |
153 | matchBrackets: operation(function(){matchBrackets(true);}), |
|
202 | matchBrackets: operation(function(){matchBrackets(true);}), | |
154 | getTokenAt: function(pos) { |
|
203 | getTokenAt: operation(function(pos) { | |
155 | pos = clipPos(pos); |
|
204 | pos = clipPos(pos); | |
156 |
return |
|
205 | return getLine(pos.line).getTokenAt(mode, getStateBefore(pos.line), pos.ch); | |
157 | }, |
|
206 | }), | |
158 | getStateAfter: function(line) { |
|
207 | getStateAfter: function(line) { | |
159 |
line = clipLine(line == null ? |
|
208 | line = clipLine(line == null ? doc.size - 1: line); | |
160 | return getStateBefore(line + 1); |
|
209 | return getStateBefore(line + 1); | |
161 | }, |
|
210 | }, | |
162 | cursorCoords: function(start){ |
|
211 | cursorCoords: function(start, mode) { | |
163 | if (start == null) start = sel.inverted; |
|
212 | if (start == null) start = sel.inverted; | |
164 |
return |
|
213 | return this.charCoords(start ? sel.from : sel.to, mode); | |
165 | }, |
|
214 | }, | |
166 |
charCoords: function(pos){ |
|
215 | charCoords: function(pos, mode) { | |
|
216 | pos = clipPos(pos); | |||
|
217 | if (mode == "local") return localCoords(pos, false); | |||
|
218 | if (mode == "div") return localCoords(pos, true); | |||
|
219 | return pageCoords(pos); | |||
|
220 | }, | |||
167 | coordsChar: function(coords) { |
|
221 | coordsChar: function(coords) { | |
168 | var off = eltOffset(lineSpace); |
|
222 | var off = eltOffset(lineSpace); | |
169 | var line = clipLine(Math.min(lines.length - 1, showingFrom + Math.floor((coords.y - off.top) / lineHeight()))); |
|
223 | return coordsChar(coords.x - off.left, coords.y - off.top); | |
170 | return clipPos({line: line, ch: charFromX(clipLine(line), coords.x - off.left)}); |
|
|||
171 | }, |
|
224 | }, | |
172 | getSearchCursor: function(query, pos, caseFold) {return new SearchCursor(query, pos, caseFold);}, |
|
|||
173 | markText: operation(markText), |
|
225 | markText: operation(markText), | |
|
226 | setBookmark: setBookmark, | |||
|
227 | findMarksAt: findMarksAt, | |||
174 | setMarker: operation(addGutterMarker), |
|
228 | setMarker: operation(addGutterMarker), | |
175 | clearMarker: operation(removeGutterMarker), |
|
229 | clearMarker: operation(removeGutterMarker), | |
176 | setLineClass: operation(setLineClass), |
|
230 | setLineClass: operation(setLineClass), | |
|
231 | hideLine: operation(function(h) {return setLineHidden(h, true);}), | |||
|
232 | showLine: operation(function(h) {return setLineHidden(h, false);}), | |||
|
233 | onDeleteLine: function(line, f) { | |||
|
234 | if (typeof line == "number") { | |||
|
235 | if (!isLine(line)) return null; | |||
|
236 | line = getLine(line); | |||
|
237 | } | |||
|
238 | (line.handlers || (line.handlers = [])).push(f); | |||
|
239 | return line; | |||
|
240 | }, | |||
177 | lineInfo: lineInfo, |
|
241 | lineInfo: lineInfo, | |
178 | addWidget: function(pos, node, scroll, vert, horiz) { |
|
242 | addWidget: function(pos, node, scroll, vert, horiz) { | |
179 | pos = localCoords(clipPos(pos)); |
|
243 | pos = localCoords(clipPos(pos)); | |
@@ -182,7 +246,7 b' var CodeMirror = (function() {' | |||||
182 | code.appendChild(node); |
|
246 | code.appendChild(node); | |
183 | if (vert == "over") top = pos.y; |
|
247 | if (vert == "over") top = pos.y; | |
184 | else if (vert == "near") { |
|
248 | else if (vert == "near") { | |
185 |
var vspace = Math.max(scroller.offsetHeight, |
|
249 | var vspace = Math.max(scroller.offsetHeight, doc.height * textHeight()), | |
186 | hspace = Math.max(code.clientWidth, lineSpace.clientWidth) - paddingLeft(); |
|
250 | hspace = Math.max(code.clientWidth, lineSpace.clientWidth) - paddingLeft(); | |
187 | if (pos.yBot + node.offsetHeight > vspace && pos.y > node.offsetHeight) |
|
251 | if (pos.yBot + node.offsetHeight > vspace && pos.y > node.offsetHeight) | |
188 | top = pos.y - node.offsetHeight; |
|
252 | top = pos.y - node.offsetHeight; | |
@@ -203,20 +267,24 b' var CodeMirror = (function() {' | |||||
203 | scrollIntoView(left, top, left + node.offsetWidth, top + node.offsetHeight); |
|
267 | scrollIntoView(left, top, left + node.offsetWidth, top + node.offsetHeight); | |
204 | }, |
|
268 | }, | |
205 |
|
269 | |||
206 |
lineCount: function() {return |
|
270 | lineCount: function() {return doc.size;}, | |
|
271 | clipPos: clipPos, | |||
207 | getCursor: function(start) { |
|
272 | getCursor: function(start) { | |
208 | if (start == null) start = sel.inverted; |
|
273 | if (start == null) start = sel.inverted; | |
209 | return copyPos(start ? sel.from : sel.to); |
|
274 | return copyPos(start ? sel.from : sel.to); | |
210 | }, |
|
275 | }, | |
211 | somethingSelected: function() {return !posEq(sel.from, sel.to);}, |
|
276 | somethingSelected: function() {return !posEq(sel.from, sel.to);}, | |
212 | setCursor: operation(function(line, ch) { |
|
277 | setCursor: operation(function(line, ch, user) { | |
213 | if (ch == null && typeof line.line == "number") setCursor(line.line, line.ch); |
|
278 | if (ch == null && typeof line.line == "number") setCursor(line.line, line.ch, user); | |
214 | else setCursor(line, ch); |
|
279 | else setCursor(line, ch, user); | |
215 | }), |
|
280 | }), | |
216 |
setSelection: operation(function(from, to) { |
|
281 | setSelection: operation(function(from, to, user) { | |
217 | getLine: function(line) {if (isLine(line)) return lines[line].text;}, |
|
282 | (user ? setSelectionUser : setSelection)(clipPos(from), clipPos(to || from)); | |
|
283 | }), | |||
|
284 | getLine: function(line) {if (isLine(line)) return getLine(line).text;}, | |||
|
285 | getLineHandle: function(line) {if (isLine(line)) return getLine(line);}, | |||
218 | setLine: operation(function(line, text) { |
|
286 | setLine: operation(function(line, text) { | |
219 |
if (isLine(line)) replaceRange(text, {line: line, ch: 0}, {line: line, ch: |
|
287 | if (isLine(line)) replaceRange(text, {line: line, ch: 0}, {line: line, ch: getLine(line).text.length}); | |
220 | }), |
|
288 | }), | |
221 | removeLine: operation(function(line) { |
|
289 | removeLine: operation(function(line) { | |
222 | if (isLine(line)) replaceRange("", {line: line, ch: 0}, clipPos({line: line+1, ch: 0})); |
|
290 | if (isLine(line)) replaceRange("", {line: line, ch: 0}, clipPos({line: line+1, ch: 0})); | |
@@ -224,44 +292,99 b' var CodeMirror = (function() {' | |||||
224 | replaceRange: operation(replaceRange), |
|
292 | replaceRange: operation(replaceRange), | |
225 | getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));}, |
|
293 | getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));}, | |
226 |
|
294 | |||
227 | coordsFromIndex: function(index) { |
|
295 | triggerOnKeyDown: operation(onKeyDown), | |
228 | var total = lines.length, pos = 0, line, ch, len; |
|
296 | execCommand: function(cmd) {return commands[cmd](instance);}, | |
229 |
|
297 | // Stuff used by commands, probably not much use to outside code. | ||
230 | for (line = 0; line < total; line++) { |
|
298 | moveH: operation(moveH), | |
231 | len = lines[line].text.length + 1; |
|
299 | deleteH: operation(deleteH), | |
232 | if (pos + len > index) { ch = index - pos; break; } |
|
300 | moveV: operation(moveV), | |
233 | pos += len; |
|
301 | toggleOverwrite: function() { | |
|
302 | if(overwrite){ | |||
|
303 | overwrite = false; | |||
|
304 | cursor.className = cursor.className.replace(" CodeMirror-overwrite", ""); | |||
|
305 | } else { | |||
|
306 | overwrite = true; | |||
|
307 | cursor.className += " CodeMirror-overwrite"; | |||
234 | } |
|
308 | } | |
235 | return clipPos({line: line, ch: ch}); |
|
309 | }, | |
|
310 | ||||
|
311 | posFromIndex: function(off) { | |||
|
312 | var lineNo = 0, ch; | |||
|
313 | doc.iter(0, doc.size, function(line) { | |||
|
314 | var sz = line.text.length + 1; | |||
|
315 | if (sz > off) { ch = off; return true; } | |||
|
316 | off -= sz; | |||
|
317 | ++lineNo; | |||
|
318 | }); | |||
|
319 | return clipPos({line: lineNo, ch: ch}); | |||
|
320 | }, | |||
|
321 | indexFromPos: function (coords) { | |||
|
322 | if (coords.line < 0 || coords.ch < 0) return 0; | |||
|
323 | var index = coords.ch; | |||
|
324 | doc.iter(0, coords.line, function (line) { | |||
|
325 | index += line.text.length + 1; | |||
|
326 | }); | |||
|
327 | return index; | |||
|
328 | }, | |||
|
329 | scrollTo: function(x, y) { | |||
|
330 | if (x != null) scroller.scrollLeft = x; | |||
|
331 | if (y != null) scrollbar.scrollTop = y; | |||
|
332 | updateDisplay([]); | |||
|
333 | }, | |||
|
334 | getScrollInfo: function() { | |||
|
335 | return {x: scroller.scrollLeft, y: scrollbar.scrollTop, | |||
|
336 | height: scrollbar.scrollHeight, width: scroller.scrollWidth}; | |||
236 | }, |
|
337 | }, | |
237 |
|
338 | |||
238 | operation: function(f){return operation(f)();}, |
|
339 | operation: function(f){return operation(f)();}, | |
239 | refresh: function(){updateDisplay(true);}, |
|
340 | compoundChange: function(f){return compoundChange(f);}, | |
|
341 | refresh: function(){ | |||
|
342 | updateDisplay(true); | |||
|
343 | if (scrollbar.scrollHeight > lastScrollTop) | |||
|
344 | scrollbar.scrollTop = lastScrollTop; | |||
|
345 | }, | |||
240 | getInputField: function(){return input;}, |
|
346 | getInputField: function(){return input;}, | |
241 | getWrapperElement: function(){return wrapper;}, |
|
347 | getWrapperElement: function(){return wrapper;}, | |
242 | getScrollerElement: function(){return scroller;}, |
|
348 | getScrollerElement: function(){return scroller;}, | |
243 | getGutterElement: function(){return gutter;} |
|
349 | getGutterElement: function(){return gutter;} | |
244 | }; |
|
350 | }; | |
245 |
|
351 | |||
|
352 | function getLine(n) { return getLineAt(doc, n); } | |||
|
353 | function updateLineHeight(line, height) { | |||
|
354 | gutterDirty = true; | |||
|
355 | var diff = height - line.height; | |||
|
356 | for (var n = line; n; n = n.parent) n.height += diff; | |||
|
357 | } | |||
|
358 | ||||
246 | function setValue(code) { |
|
359 | function setValue(code) { | |
247 | var top = {line: 0, ch: 0}; |
|
360 | var top = {line: 0, ch: 0}; | |
248 |
updateLines(top, {line: |
|
361 | updateLines(top, {line: doc.size - 1, ch: getLine(doc.size-1).text.length}, | |
249 | splitLines(code), top, top); |
|
362 | splitLines(code), top, top); | |
250 | updateInput = true; |
|
363 | updateInput = true; | |
251 | } |
|
364 | } | |
252 |
function getValue( |
|
365 | function getValue() { | |
253 | var text = []; |
|
366 | var text = []; | |
254 | for (var i = 0, l = lines.length; i < l; ++i) |
|
367 | doc.iter(0, doc.size, function(line) { text.push(line.text); }); | |
255 | text.push(lines[i].text); |
|
|||
256 | return text.join("\n"); |
|
368 | return text.join("\n"); | |
257 | } |
|
369 | } | |
258 |
|
370 | |||
|
371 | function onScroll(e) { | |||
|
372 | if (lastScrollTop != scrollbar.scrollTop || lastScrollLeft != scroller.scrollLeft) { | |||
|
373 | lastScrollTop = scrollbar.scrollTop; | |||
|
374 | lastScrollLeft = scroller.scrollLeft; | |||
|
375 | updateDisplay([]); | |||
|
376 | if (options.fixedGutter) gutter.style.left = scroller.scrollLeft + "px"; | |||
|
377 | if (options.onScroll) options.onScroll(instance); | |||
|
378 | } | |||
|
379 | } | |||
|
380 | ||||
259 | function onMouseDown(e) { |
|
381 | function onMouseDown(e) { | |
|
382 | setShift(e_prop(e, "shiftKey")); | |||
260 | // Check whether this is a click in a widget |
|
383 | // Check whether this is a click in a widget | |
261 | for (var n = e_target(e); n != wrapper; n = n.parentNode) |
|
384 | for (var n = e_target(e); n != wrapper; n = n.parentNode) | |
262 | if (n.parentNode == code && n != mover) return; |
|
385 | if (n.parentNode == code && n != mover) return; | |
263 |
|
386 | |||
264 |
// |
|
387 | // See if this is a click in the gutter | |
265 | for (var n = e_target(e); n != wrapper; n = n.parentNode) |
|
388 | for (var n = e_target(e); n != wrapper; n = n.parentNode) | |
266 | if (n.parentNode == gutterText) { |
|
389 | if (n.parentNode == gutterText) { | |
267 | if (options.onGutterClick) |
|
390 | if (options.onGutterClick) | |
@@ -277,6 +400,8 b' var CodeMirror = (function() {' | |||||
277 | return; |
|
400 | return; | |
278 | case 2: |
|
401 | case 2: | |
279 | if (start) setCursor(start.line, start.ch, true); |
|
402 | if (start) setCursor(start.line, start.ch, true); | |
|
403 | setTimeout(focusInput, 20); | |||
|
404 | e_preventDefault(e); | |||
280 | return; |
|
405 | return; | |
281 | } |
|
406 | } | |
282 | // For button 1, if it was clicked inside the editor |
|
407 | // For button 1, if it was clicked inside the editor | |
@@ -287,29 +412,36 b' var CodeMirror = (function() {' | |||||
287 | if (!focused) onFocus(); |
|
412 | if (!focused) onFocus(); | |
288 |
|
413 | |||
289 | var now = +new Date; |
|
414 | var now = +new Date; | |
290 | if (lastDoubleClick > now - 400) { |
|
415 | if (lastDoubleClick && lastDoubleClick.time > now - 400 && posEq(lastDoubleClick.pos, start)) { | |
291 | e_preventDefault(e); |
|
416 | e_preventDefault(e); | |
|
417 | setTimeout(focusInput, 20); | |||
292 | return selectLine(start.line); |
|
418 | return selectLine(start.line); | |
293 | } else if (lastClick > now - 400) { |
|
419 | } else if (lastClick && lastClick.time > now - 400 && posEq(lastClick.pos, start)) { | |
294 | lastDoubleClick = now; |
|
420 | lastDoubleClick = {time: now, pos: start}; | |
295 | e_preventDefault(e); |
|
421 | e_preventDefault(e); | |
296 | return selectWordAt(start); |
|
422 | return selectWordAt(start); | |
297 | } else { lastClick = now; } |
|
423 | } else { lastClick = {time: now, pos: start}; } | |
298 |
|
424 | |||
299 | var last = start, going; |
|
425 | var last = start, going; | |
300 | if (dragAndDrop && !posEq(sel.from, sel.to) && |
|
426 | if (options.dragDrop && dragAndDrop && !options.readOnly && !posEq(sel.from, sel.to) && | |
301 | !posLess(start, sel.from) && !posLess(sel.to, start)) { |
|
427 | !posLess(start, sel.from) && !posLess(sel.to, start)) { | |
302 | // Let the drag handler handle this. |
|
428 | // Let the drag handler handle this. | |
303 | var up = connect(targetDocument, "mouseup", operation(function(e2) { |
|
429 | if (webkit) scroller.draggable = true; | |
|
430 | function dragEnd(e2) { | |||
|
431 | if (webkit) scroller.draggable = false; | |||
304 | draggingText = false; |
|
432 | draggingText = false; | |
305 | up(); |
|
433 | up(); drop(); | |
306 | if (Math.abs(e.clientX - e2.clientX) + Math.abs(e.clientY - e2.clientY) < 10) { |
|
434 | if (Math.abs(e.clientX - e2.clientX) + Math.abs(e.clientY - e2.clientY) < 10) { | |
307 | e_preventDefault(e2); |
|
435 | e_preventDefault(e2); | |
308 | setCursor(start.line, start.ch, true); |
|
436 | setCursor(start.line, start.ch, true); | |
309 | focusInput(); |
|
437 | focusInput(); | |
310 | } |
|
438 | } | |
311 |
} |
|
439 | } | |
|
440 | var up = connect(document, "mouseup", operation(dragEnd), true); | |||
|
441 | var drop = connect(scroller, "drop", operation(dragEnd), true); | |||
312 | draggingText = true; |
|
442 | draggingText = true; | |
|
443 | // IE's approach to draggable | |||
|
444 | if (scroller.dragDrop) scroller.dragDrop(); | |||
313 | return; |
|
445 | return; | |
314 | } |
|
446 | } | |
315 | e_preventDefault(e); |
|
447 | e_preventDefault(e); | |
@@ -328,12 +460,7 b' var CodeMirror = (function() {' | |||||
328 | } |
|
460 | } | |
329 | } |
|
461 | } | |
330 |
|
462 | |||
331 | var move = connect(targetDocument, "mousemove", operation(function(e) { |
|
463 | function done(e) { | |
332 | clearTimeout(going); |
|
|||
333 | e_preventDefault(e); |
|
|||
334 | extend(e); |
|
|||
335 | }), true); |
|
|||
336 | var up = connect(targetDocument, "mouseup", operation(function(e) { |
|
|||
337 | clearTimeout(going); |
|
464 | clearTimeout(going); | |
338 | var cur = posFromMouse(e); |
|
465 | var cur = posFromMouse(e); | |
339 | if (cur) setSelectionUser(start, cur); |
|
466 | if (cur) setSelectionUser(start, cur); | |
@@ -341,16 +468,26 b' var CodeMirror = (function() {' | |||||
341 | focusInput(); |
|
468 | focusInput(); | |
342 | updateInput = true; |
|
469 | updateInput = true; | |
343 | move(); up(); |
|
470 | move(); up(); | |
|
471 | } | |||
|
472 | var move = connect(document, "mousemove", operation(function(e) { | |||
|
473 | clearTimeout(going); | |||
|
474 | e_preventDefault(e); | |||
|
475 | if (!ie && !e_button(e)) done(e); | |||
|
476 | else extend(e); | |||
344 | }), true); |
|
477 | }), true); | |
|
478 | var up = connect(document, "mouseup", operation(done), true); | |||
345 | } |
|
479 | } | |
346 | function onDoubleClick(e) { |
|
480 | function onDoubleClick(e) { | |
|
481 | for (var n = e_target(e); n != wrapper; n = n.parentNode) | |||
|
482 | if (n.parentNode == gutterText) return e_preventDefault(e); | |||
347 | var start = posFromMouse(e); |
|
483 | var start = posFromMouse(e); | |
348 | if (!start) return; |
|
484 | if (!start) return; | |
349 | lastDoubleClick = +new Date; |
|
485 | lastDoubleClick = {time: +new Date, pos: start}; | |
350 | e_preventDefault(e); |
|
486 | e_preventDefault(e); | |
351 | selectWordAt(start); |
|
487 | selectWordAt(start); | |
352 | } |
|
488 | } | |
353 | function onDrop(e) { |
|
489 | function onDrop(e) { | |
|
490 | if (options.onDragEvent && options.onDragEvent(instance, addStop(e))) return; | |||
354 | e.preventDefault(); |
|
491 | e.preventDefault(); | |
355 | var pos = posFromMouse(e, true), files = e.dataTransfer.files; |
|
492 | var pos = posFromMouse(e, true), files = e.dataTransfer.files; | |
356 | if (!pos || options.readOnly) return; |
|
493 | if (!pos || options.readOnly) return; | |
@@ -360,102 +497,147 b' var CodeMirror = (function() {' | |||||
360 | reader.onload = function() { |
|
497 | reader.onload = function() { | |
361 | text[i] = reader.result; |
|
498 | text[i] = reader.result; | |
362 | if (++read == n) { |
|
499 | if (++read == n) { | |
363 |
|
|
500 | pos = clipPos(pos); | |
364 | var end = replaceRange(text.join(""), pos, pos); |
|
501 | operation(function() { | |
365 | setSelectionUser(pos, end); |
|
502 | var end = replaceRange(text.join(""), pos, pos); | |
366 | } |
|
503 | setSelectionUser(pos, end); | |
|
504 | })(); | |||
|
505 | } | |||
367 | }; |
|
506 | }; | |
368 | reader.readAsText(file); |
|
507 | reader.readAsText(file); | |
369 | } |
|
508 | } | |
370 | var n = files.length, text = Array(n), read = 0; |
|
509 | var n = files.length, text = Array(n), read = 0; | |
371 | for (var i = 0; i < n; ++i) loadFile(files[i], i); |
|
510 | for (var i = 0; i < n; ++i) loadFile(files[i], i); | |
372 | } |
|
511 | } else { | |
373 | else { |
|
512 | // Don't do a replace if the drop happened inside of the selected text. | |
|
513 | if (draggingText && !(posLess(pos, sel.from) || posLess(sel.to, pos))) return; | |||
374 | try { |
|
514 | try { | |
375 | var text = e.dataTransfer.getData("Text"); |
|
515 | var text = e.dataTransfer.getData("Text"); | |
376 | if (text) { |
|
516 | if (text) { | |
377 | var end = replaceRange(text, pos, pos); |
|
517 | compoundChange(function() { | |
378 |
|
|
518 | var curFrom = sel.from, curTo = sel.to; | |
379 |
|
|
519 | setSelectionUser(pos, pos); | |
380 | if (draggingText) replaceRange("", curFrom, curTo); |
|
520 | if (draggingText) replaceRange("", curFrom, curTo); | |
381 | focusInput(); |
|
521 | replaceSelection(text); | |
382 | } |
|
522 | focusInput(); | |
|
523 | }); | |||
|
524 | } | |||
383 | } |
|
525 | } | |
384 | catch(e){} |
|
526 | catch(e){} | |
385 | } |
|
527 | } | |
386 | } |
|
528 | } | |
387 | function onDragStart(e) { |
|
529 | function onDragStart(e) { | |
388 | var txt = getSelection(); |
|
530 | var txt = getSelection(); | |
389 | // This will reset escapeElement |
|
|||
390 | htmlEscape(txt); |
|
|||
391 | e.dataTransfer.setDragImage(escapeElement, 0, 0); |
|
|||
392 | e.dataTransfer.setData("Text", txt); |
|
531 | e.dataTransfer.setData("Text", txt); | |
|
532 | ||||
|
533 | // Use dummy image instead of default browsers image. | |||
|
534 | if (gecko || chrome || opera) { | |||
|
535 | var img = document.createElement('img'); | |||
|
536 | img.scr = 'data:image/gif;base64,R0lGODdhAgACAIAAAAAAAP///ywAAAAAAgACAAACAoRRADs='; //1x1 image | |||
|
537 | e.dataTransfer.setDragImage(img, 0, 0); | |||
|
538 | } | |||
393 | } |
|
539 | } | |
|
540 | ||||
|
541 | function doHandleBinding(bound, dropShift) { | |||
|
542 | if (typeof bound == "string") { | |||
|
543 | bound = commands[bound]; | |||
|
544 | if (!bound) return false; | |||
|
545 | } | |||
|
546 | var prevShift = shiftSelecting; | |||
|
547 | try { | |||
|
548 | if (options.readOnly) suppressEdits = true; | |||
|
549 | if (dropShift) shiftSelecting = null; | |||
|
550 | bound(instance); | |||
|
551 | } catch(e) { | |||
|
552 | if (e != Pass) throw e; | |||
|
553 | return false; | |||
|
554 | } finally { | |||
|
555 | shiftSelecting = prevShift; | |||
|
556 | suppressEdits = false; | |||
|
557 | } | |||
|
558 | return true; | |||
|
559 | } | |||
|
560 | function handleKeyBinding(e) { | |||
|
561 | // Handle auto keymap transitions | |||
|
562 | var startMap = getKeyMap(options.keyMap), next = startMap.auto; | |||
|
563 | clearTimeout(maybeTransition); | |||
|
564 | if (next && !isModifierKey(e)) maybeTransition = setTimeout(function() { | |||
|
565 | if (getKeyMap(options.keyMap) == startMap) { | |||
|
566 | options.keyMap = (next.call ? next.call(null, instance) : next); | |||
|
567 | } | |||
|
568 | }, 50); | |||
|
569 | ||||
|
570 | var name = keyNames[e_prop(e, "keyCode")], handled = false; | |||
|
571 | if (name == null || e.altGraphKey) return false; | |||
|
572 | if (e_prop(e, "altKey")) name = "Alt-" + name; | |||
|
573 | if (e_prop(e, "ctrlKey")) name = "Ctrl-" + name; | |||
|
574 | if (e_prop(e, "metaKey")) name = "Cmd-" + name; | |||
|
575 | ||||
|
576 | var stopped = false; | |||
|
577 | function stop() { stopped = true; } | |||
|
578 | ||||
|
579 | if (e_prop(e, "shiftKey")) { | |||
|
580 | handled = lookupKey("Shift-" + name, options.extraKeys, options.keyMap, | |||
|
581 | function(b) {return doHandleBinding(b, true);}, stop) | |||
|
582 | || lookupKey(name, options.extraKeys, options.keyMap, function(b) { | |||
|
583 | if (typeof b == "string" && /^go[A-Z]/.test(b)) return doHandleBinding(b); | |||
|
584 | }, stop); | |||
|
585 | } else { | |||
|
586 | handled = lookupKey(name, options.extraKeys, options.keyMap, doHandleBinding, stop); | |||
|
587 | } | |||
|
588 | if (stopped) handled = false; | |||
|
589 | if (handled) { | |||
|
590 | e_preventDefault(e); | |||
|
591 | restartBlink(); | |||
|
592 | if (ie) { e.oldKeyCode = e.keyCode; e.keyCode = 0; } | |||
|
593 | } | |||
|
594 | return handled; | |||
|
595 | } | |||
|
596 | function handleCharBinding(e, ch) { | |||
|
597 | var handled = lookupKey("'" + ch + "'", options.extraKeys, | |||
|
598 | options.keyMap, function(b) { return doHandleBinding(b, true); }); | |||
|
599 | if (handled) { | |||
|
600 | e_preventDefault(e); | |||
|
601 | restartBlink(); | |||
|
602 | } | |||
|
603 | return handled; | |||
|
604 | } | |||
|
605 | ||||
|
606 | var lastStoppedKey = null, maybeTransition; | |||
394 | function onKeyDown(e) { |
|
607 | function onKeyDown(e) { | |
395 | if (!focused) onFocus(); |
|
608 | if (!focused) onFocus(); | |
396 |
|
609 | if (ie && e.keyCode == 27) { e.returnValue = false; } | ||
397 | var code = e.keyCode; |
|
610 | if (pollingFast) { if (readInput()) pollingFast = false; } | |
|
611 | if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return; | |||
|
612 | var code = e_prop(e, "keyCode"); | |||
398 | // IE does strange things with escape. |
|
613 | // IE does strange things with escape. | |
399 | if (ie && code == 27) { e.returnValue = false; } |
|
614 | setShift(code == 16 || e_prop(e, "shiftKey")); | |
400 | // Tries to detect ctrl on non-mac, cmd on mac. |
|
|||
401 | var mod = (mac ? e.metaKey : e.ctrlKey) && !e.altKey, anyMod = e.ctrlKey || e.altKey || e.metaKey; |
|
|||
402 | if (code == 16 || e.shiftKey) shiftSelecting = shiftSelecting || (sel.inverted ? sel.to : sel.from); |
|
|||
403 | else shiftSelecting = null; |
|
|||
404 | // First give onKeyEvent option a chance to handle this. |
|
615 | // First give onKeyEvent option a chance to handle this. | |
405 | if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return; |
|
616 | var handled = handleKeyBinding(e); | |
406 |
|
617 | if (opera) { | ||
407 | if (code == 33 || code == 34) {scrollPage(code == 34); return e_preventDefault(e);} // page up/down |
|
618 | lastStoppedKey = handled ? code : null; | |
408 | if (mod && ((code == 36 || code == 35) || // ctrl-home/end |
|
619 | // Opera has no cut event... we try to at least catch the key combo | |
409 | mac && (code == 38 || code == 40))) { // cmd-up/down |
|
620 | if (!handled && code == 88 && e_prop(e, mac ? "metaKey" : "ctrlKey")) | |
410 | scrollEnd(code == 36 || code == 38); return e_preventDefault(e); |
|
621 | replaceSelection(""); | |
411 | } |
|
622 | } | |
412 | if (mod && code == 65) {selectAll(); return e_preventDefault(e);} // ctrl-a |
|
623 | } | |
413 | if (!options.readOnly) { |
|
624 | function onKeyPress(e) { | |
414 | if (!anyMod && code == 13) {return;} // enter |
|
625 | if (pollingFast) readInput(); | |
415 | if (!anyMod && code == 9 && handleTab(e.shiftKey)) return e_preventDefault(e); // tab |
|
626 | if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return; | |
416 | if (mod && code == 90) {undo(); return e_preventDefault(e);} // ctrl-z |
|
627 | var keyCode = e_prop(e, "keyCode"), charCode = e_prop(e, "charCode"); | |
417 | if (mod && ((e.shiftKey && code == 90) || code == 89)) {redo(); return e_preventDefault(e);} // ctrl-shift-z, ctrl-y |
|
628 | if (opera && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;} | |
|
629 | if (((opera && (!e.which || e.which < 10)) || khtml) && handleKeyBinding(e)) return; | |||
|
630 | var ch = String.fromCharCode(charCode == null ? keyCode : charCode); | |||
|
631 | if (options.electricChars && mode.electricChars && options.smartIndent && !options.readOnly) { | |||
|
632 | if (mode.electricChars.indexOf(ch) > -1) | |||
|
633 | setTimeout(operation(function() {indentLine(sel.to.line, "smart");}), 75); | |||
418 | } |
|
634 | } | |
419 | if (code == 36) { if (options.smartHome) { smartHome(); return e_preventDefault(e); } } |
|
635 | if (handleCharBinding(e, ch)) return; | |
420 |
|
636 | fastPoll(); | ||
421 | // Key id to use in the movementKeys map. We also pass it to |
|
|||
422 | // fastPoll in order to 'self learn'. We need this because |
|
|||
423 | // reducedSelection, the hack where we collapse the selection to |
|
|||
424 | // its start when it is inverted and a movement key is pressed |
|
|||
425 | // (and later restore it again), shouldn't be used for |
|
|||
426 | // non-movement keys. |
|
|||
427 | curKeyId = (mod ? "c" : "") + (e.altKey ? "a" : "") + code; |
|
|||
428 | if (sel.inverted && movementKeys[curKeyId] === true) { |
|
|||
429 | var range = selRange(input); |
|
|||
430 | if (range) { |
|
|||
431 | reducedSelection = {anchor: range.start}; |
|
|||
432 | setSelRange(input, range.start, range.start); |
|
|||
433 | } |
|
|||
434 | } |
|
|||
435 | // Don't save the key as a movementkey unless it had a modifier |
|
|||
436 | if (!mod && !e.altKey) curKeyId = null; |
|
|||
437 | fastPoll(curKeyId); |
|
|||
438 | } |
|
637 | } | |
439 | function onKeyUp(e) { |
|
638 | function onKeyUp(e) { | |
440 | if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return; |
|
639 | if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return; | |
441 | if (reducedSelection) { |
|
640 | if (e_prop(e, "keyCode") == 16) shiftSelecting = null; | |
442 | reducedSelection = null; |
|
|||
443 | updateInput = true; |
|
|||
444 | } |
|
|||
445 | if (e.keyCode == 16) shiftSelecting = null; |
|
|||
446 | } |
|
|||
447 | function onKeyPress(e) { |
|
|||
448 | if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return; |
|
|||
449 | if (options.electricChars && mode.electricChars) { |
|
|||
450 | var ch = String.fromCharCode(e.charCode == null ? e.keyCode : e.charCode); |
|
|||
451 | if (mode.electricChars.indexOf(ch) > -1) |
|
|||
452 | setTimeout(operation(function() {indentLine(sel.to.line, "smart");}), 50); |
|
|||
453 | } |
|
|||
454 | var code = e.keyCode; |
|
|||
455 | // Re-stop tab and enter. Necessary on some browsers. |
|
|||
456 | if (code == 13) {if (!options.readOnly) handleEnter(); e_preventDefault(e);} |
|
|||
457 | else if (!e.ctrlKey && !e.altKey && !e.metaKey && code == 9 && options.tabMode != "default") e_preventDefault(e); |
|
|||
458 | else fastPoll(curKeyId); |
|
|||
459 | } |
|
641 | } | |
460 |
|
642 | |||
461 | function onFocus() { |
|
643 | function onFocus() { | |
@@ -463,9 +645,9 b' var CodeMirror = (function() {' | |||||
463 | if (!focused) { |
|
645 | if (!focused) { | |
464 | if (options.onFocus) options.onFocus(instance); |
|
646 | if (options.onFocus) options.onFocus(instance); | |
465 | focused = true; |
|
647 | focused = true; | |
466 |
if ( |
|
648 | if (scroller.className.search(/\bCodeMirror-focused\b/) == -1) | |
467 |
|
|
649 | scroller.className += " CodeMirror-focused"; | |
468 |
if (!leaveInputAlone) |
|
650 | if (!leaveInputAlone) resetInput(true); | |
469 | } |
|
651 | } | |
470 | slowPoll(); |
|
652 | slowPoll(); | |
471 | restartBlink(); |
|
653 | restartBlink(); | |
@@ -474,90 +656,149 b' var CodeMirror = (function() {' | |||||
474 | if (focused) { |
|
656 | if (focused) { | |
475 | if (options.onBlur) options.onBlur(instance); |
|
657 | if (options.onBlur) options.onBlur(instance); | |
476 | focused = false; |
|
658 | focused = false; | |
477 | wrapper.className = wrapper.className.replace(" CodeMirror-focused", ""); |
|
659 | if (bracketHighlighted) | |
|
660 | operation(function(){ | |||
|
661 | if (bracketHighlighted) { bracketHighlighted(); bracketHighlighted = null; } | |||
|
662 | })(); | |||
|
663 | scroller.className = scroller.className.replace(" CodeMirror-focused", ""); | |||
478 | } |
|
664 | } | |
479 | clearInterval(blinker); |
|
665 | clearInterval(blinker); | |
480 | setTimeout(function() {if (!focused) shiftSelecting = null;}, 150); |
|
666 | setTimeout(function() {if (!focused) shiftSelecting = null;}, 150); | |
481 | } |
|
667 | } | |
482 |
|
668 | |||
|
669 | function chopDelta(delta) { | |||
|
670 | // Make sure we always scroll a little bit for any nonzero delta. | |||
|
671 | if (delta > 0.0 && delta < 1.0) return 1; | |||
|
672 | else if (delta > -1.0 && delta < 0.0) return -1; | |||
|
673 | else return Math.round(delta); | |||
|
674 | } | |||
|
675 | ||||
|
676 | function onMouseWheel(e) { | |||
|
677 | var deltaX = 0, deltaY = 0; | |||
|
678 | if (e.type == "DOMMouseScroll") { // Firefox | |||
|
679 | var delta = -e.detail * 8.0; | |||
|
680 | if (e.axis == e.HORIZONTAL_AXIS) deltaX = delta; | |||
|
681 | else if (e.axis == e.VERTICAL_AXIS) deltaY = delta; | |||
|
682 | } else if (e.wheelDeltaX !== undefined && e.wheelDeltaY !== undefined) { // WebKit | |||
|
683 | deltaX = e.wheelDeltaX / 3.0; | |||
|
684 | deltaY = e.wheelDeltaY / 3.0; | |||
|
685 | } else if (e.wheelDelta !== undefined) { // IE or Opera | |||
|
686 | deltaY = e.wheelDelta / 3.0; | |||
|
687 | } | |||
|
688 | ||||
|
689 | var scrolled = false; | |||
|
690 | deltaX = chopDelta(deltaX); | |||
|
691 | deltaY = chopDelta(deltaY); | |||
|
692 | if ((deltaX > 0 && scroller.scrollLeft > 0) || | |||
|
693 | (deltaX < 0 && scroller.scrollLeft + scroller.clientWidth < scroller.scrollWidth)) { | |||
|
694 | scroller.scrollLeft -= deltaX; | |||
|
695 | scrolled = true; | |||
|
696 | } | |||
|
697 | if ((deltaY > 0 && scrollbar.scrollTop > 0) || | |||
|
698 | (deltaY < 0 && scrollbar.scrollTop + scrollbar.clientHeight < scrollbar.scrollHeight)) { | |||
|
699 | scrollbar.scrollTop -= deltaY; | |||
|
700 | scrolled = true; | |||
|
701 | } | |||
|
702 | if (scrolled) e_stop(e); | |||
|
703 | } | |||
|
704 | ||||
483 | // Replace the range from from to to by the strings in newText. |
|
705 | // Replace the range from from to to by the strings in newText. | |
484 | // Afterwards, set the selection to selFrom, selTo. |
|
706 | // Afterwards, set the selection to selFrom, selTo. | |
485 | function updateLines(from, to, newText, selFrom, selTo) { |
|
707 | function updateLines(from, to, newText, selFrom, selTo) { | |
|
708 | if (suppressEdits) return; | |||
486 | if (history) { |
|
709 | if (history) { | |
487 | var old = []; |
|
710 | var old = []; | |
488 |
|
|
711 | doc.iter(from.line, to.line + 1, function(line) { old.push(line.text); }); | |
489 | history.addChange(from.line, newText.length, old); |
|
712 | history.addChange(from.line, newText.length, old); | |
490 | while (history.done.length > options.undoDepth) history.done.shift(); |
|
713 | while (history.done.length > options.undoDepth) history.done.shift(); | |
491 | } |
|
714 | } | |
492 | updateLinesNoUndo(from, to, newText, selFrom, selTo); |
|
715 | updateLinesNoUndo(from, to, newText, selFrom, selTo); | |
493 | } |
|
716 | } | |
494 | function unredoHelper(from, to) { |
|
717 | function unredoHelper(from, to) { | |
495 | var change = from.pop(); |
|
718 | if (!from.length) return; | |
496 | if (change) { |
|
719 | var set = from.pop(), out = []; | |
|
720 | for (var i = set.length - 1; i >= 0; i -= 1) { | |||
|
721 | var change = set[i]; | |||
497 | var replaced = [], end = change.start + change.added; |
|
722 | var replaced = [], end = change.start + change.added; | |
498 |
|
|
723 | doc.iter(change.start, end, function(line) { replaced.push(line.text); }); | |
499 |
t |
|
724 | out.push({start: change.start, added: change.old.length, old: replaced}); | |
500 |
var pos = |
|
725 | var pos = {line: change.start + change.old.length - 1, | |
501 |
|
|
726 | ch: editEnd(replaced[replaced.length-1], change.old[change.old.length-1])}; | |
502 |
updateLinesNoUndo({line: change.start, ch: 0}, {line: end - 1, ch: |
|
727 | updateLinesNoUndo({line: change.start, ch: 0}, {line: end - 1, ch: getLine(end-1).text.length}, change.old, pos, pos); | |
503 | updateInput = true; |
|
|||
504 | } |
|
728 | } | |
|
729 | updateInput = true; | |||
|
730 | to.push(out); | |||
505 | } |
|
731 | } | |
506 | function undo() {unredoHelper(history.done, history.undone);} |
|
732 | function undo() {unredoHelper(history.done, history.undone);} | |
507 | function redo() {unredoHelper(history.undone, history.done);} |
|
733 | function redo() {unredoHelper(history.undone, history.done);} | |
508 |
|
734 | |||
509 | function updateLinesNoUndo(from, to, newText, selFrom, selTo) { |
|
735 | function updateLinesNoUndo(from, to, newText, selFrom, selTo) { | |
|
736 | if (suppressEdits) return; | |||
510 | var recomputeMaxLength = false, maxLineLength = maxLine.length; |
|
737 | var recomputeMaxLength = false, maxLineLength = maxLine.length; | |
511 | for (var i = from.line; i <= to.line; ++i) { |
|
738 | if (!options.lineWrapping) | |
512 | if (lines[i].text.length == maxLineLength) {recomputeMaxLength = true; break;} |
|
739 | doc.iter(from.line, to.line + 1, function(line) { | |
513 | } |
|
740 | if (!line.hidden && line.text.length == maxLineLength) {recomputeMaxLength = true; return true;} | |
|
741 | }); | |||
|
742 | if (from.line != to.line || newText.length > 1) gutterDirty = true; | |||
514 |
|
743 | |||
515 |
var nlines = to.line - from.line, firstLine = |
|
744 | var nlines = to.line - from.line, firstLine = getLine(from.line), lastLine = getLine(to.line); | |
516 | // First adjust the line structure, taking some care to leave highlighting intact. |
|
745 | // First adjust the line structure, taking some care to leave highlighting intact. | |
517 | if (firstLine == lastLine) { |
|
746 | if (from.ch == 0 && to.ch == 0 && newText[newText.length - 1] == "") { | |
|
747 | // This is a whole-line replace. Treated specially to make | |||
|
748 | // sure line objects move the way they are supposed to. | |||
|
749 | var added = [], prevLine = null; | |||
|
750 | if (from.line) { | |||
|
751 | prevLine = getLine(from.line - 1); | |||
|
752 | prevLine.fixMarkEnds(lastLine); | |||
|
753 | } else lastLine.fixMarkStarts(); | |||
|
754 | for (var i = 0, e = newText.length - 1; i < e; ++i) | |||
|
755 | added.push(Line.inheritMarks(newText[i], prevLine)); | |||
|
756 | if (nlines) doc.remove(from.line, nlines, callbacks); | |||
|
757 | if (added.length) doc.insert(from.line, added); | |||
|
758 | } else if (firstLine == lastLine) { | |||
518 | if (newText.length == 1) |
|
759 | if (newText.length == 1) | |
519 | firstLine.replace(from.ch, to.ch, newText[0]); |
|
760 | firstLine.replace(from.ch, to.ch, newText[0]); | |
520 | else { |
|
761 | else { | |
521 | lastLine = firstLine.split(to.ch, newText[newText.length-1]); |
|
762 | lastLine = firstLine.split(to.ch, newText[newText.length-1]); | |
522 | var spliceargs = [from.line + 1, nlines]; |
|
|||
523 | firstLine.replace(from.ch, null, newText[0]); |
|
763 | firstLine.replace(from.ch, null, newText[0]); | |
|
764 | firstLine.fixMarkEnds(lastLine); | |||
|
765 | var added = []; | |||
524 | for (var i = 1, e = newText.length - 1; i < e; ++i) |
|
766 | for (var i = 1, e = newText.length - 1; i < e; ++i) | |
525 |
|
|
767 | added.push(Line.inheritMarks(newText[i], firstLine)); | |
526 |
|
|
768 | added.push(lastLine); | |
527 | lines.splice.apply(lines, spliceargs); |
|
769 | doc.insert(from.line + 1, added); | |
528 | } |
|
770 | } | |
529 | } |
|
771 | } else if (newText.length == 1) { | |
530 | else if (newText.length == 1) { |
|
|||
531 | firstLine.replace(from.ch, null, newText[0]); |
|
772 | firstLine.replace(from.ch, null, newText[0]); | |
532 | lastLine.replace(null, to.ch, ""); |
|
773 | lastLine.replace(null, to.ch, ""); | |
533 | firstLine.append(lastLine); |
|
774 | firstLine.append(lastLine); | |
534 |
|
|
775 | doc.remove(from.line + 1, nlines, callbacks); | |
535 | } |
|
776 | } else { | |
536 | else { |
|
777 | var added = []; | |
537 | var spliceargs = [from.line + 1, nlines - 1]; |
|
|||
538 | firstLine.replace(from.ch, null, newText[0]); |
|
778 | firstLine.replace(from.ch, null, newText[0]); | |
539 | lastLine.replace(null, to.ch, newText[newText.length-1]); |
|
779 | lastLine.replace(null, to.ch, newText[newText.length-1]); | |
|
780 | firstLine.fixMarkEnds(lastLine); | |||
540 | for (var i = 1, e = newText.length - 1; i < e; ++i) |
|
781 | for (var i = 1, e = newText.length - 1; i < e; ++i) | |
541 |
|
|
782 | added.push(Line.inheritMarks(newText[i], firstLine)); | |
542 | lines.splice.apply(lines, spliceargs); |
|
783 | if (nlines > 1) doc.remove(from.line + 1, nlines - 1, callbacks); | |
|
784 | doc.insert(from.line + 1, added); | |||
543 | } |
|
785 | } | |
544 |
|
786 | if (options.lineWrapping) { | ||
545 |
|
787 | var perLine = Math.max(5, scroller.clientWidth / charWidth() - 3); | ||
546 |
|
|
788 | doc.iter(from.line, from.line + newText.length, function(line) { | |
547 | var l = lines[i].text; |
|
789 | if (line.hidden) return; | |
548 | if (l.length > maxLineLength) { |
|
790 | var guess = Math.ceil(line.text.length / perLine) || 1; | |
549 | maxLine = l; maxLineLength = l.length; maxWidth = null; |
|
791 | if (guess != line.height) updateLineHeight(line, guess); | |
550 | recomputeMaxLength = false; |
|
792 | }); | |
551 |
|
|
793 | } else { | |
552 | } |
|
794 | doc.iter(from.line, from.line + newText.length, function(line) { | |
553 | if (recomputeMaxLength) { |
|
795 | var l = line.text; | |
554 | maxLineLength = 0; maxLine = ""; maxWidth = null; |
|
796 | if (!line.hidden && l.length > maxLineLength) { | |
555 | for (var i = 0, e = lines.length; i < e; ++i) { |
|
797 | maxLine = l; maxLineLength = l.length; maxLineChanged = true; | |
556 | var l = lines[i].text; |
|
798 | recomputeMaxLength = false; | |
557 | if (l.length > maxLineLength) { |
|
|||
558 | maxLineLength = l.length; maxLine = l; |
|
|||
559 | } |
|
799 | } | |
560 | } |
|
800 | }); | |
|
801 | if (recomputeMaxLength) updateMaxLine = true; | |||
561 | } |
|
802 | } | |
562 |
|
803 | |||
563 | // Add these lines to the work array, so that they will be |
|
804 | // Add these lines to the work array, so that they will be | |
@@ -568,24 +809,64 b' var CodeMirror = (function() {' | |||||
568 | if (task < from.line) newWork.push(task); |
|
809 | if (task < from.line) newWork.push(task); | |
569 | else if (task > to.line) newWork.push(task + lendiff); |
|
810 | else if (task > to.line) newWork.push(task + lendiff); | |
570 | } |
|
811 | } | |
571 | if (newText.length < 5) { |
|
812 | var hlEnd = from.line + Math.min(newText.length, 500); | |
572 |
|
|
813 | highlightLines(from.line, hlEnd); | |
573 |
|
|
814 | newWork.push(hlEnd); | |
574 | } else { |
|
|||
575 | newWork.push(from.line); |
|
|||
576 | } |
|
|||
577 | work = newWork; |
|
815 | work = newWork; | |
578 | startWorker(100); |
|
816 | startWorker(100); | |
579 | // Remember that these lines changed, for updating the display |
|
817 | // Remember that these lines changed, for updating the display | |
580 | changes.push({from: from.line, to: to.line + 1, diff: lendiff}); |
|
818 | changes.push({from: from.line, to: to.line + 1, diff: lendiff}); | |
581 |
|
|
819 | var changeObj = {from: from, to: to, text: newText}; | |
|
820 | if (textChanged) { | |||
|
821 | for (var cur = textChanged; cur.next; cur = cur.next) {} | |||
|
822 | cur.next = changeObj; | |||
|
823 | } else textChanged = changeObj; | |||
582 |
|
824 | |||
583 | // Update the selection |
|
825 | // Update the selection | |
584 | function updateLine(n) {return n <= Math.min(to.line, to.line + lendiff) ? n : n + lendiff;} |
|
826 | function updateLine(n) {return n <= Math.min(to.line, to.line + lendiff) ? n : n + lendiff;} | |
585 | setSelection(selFrom, selTo, updateLine(sel.from.line), updateLine(sel.to.line)); |
|
827 | setSelection(clipPos(selFrom), clipPos(selTo), | |
|
828 | updateLine(sel.from.line), updateLine(sel.to.line)); | |||
|
829 | } | |||
586 |
|
830 | |||
587 | // Make sure the scroll-size div has the correct height. |
|
831 | function updateVerticalScroll(scrollTop) { | |
588 | code.style.height = (lines.length * lineHeight() + 2 * paddingTop()) + "px"; |
|
832 | var th = textHeight(), virtualHeight = Math.floor(doc.height * th + 2 * paddingTop()), scrollbarHeight = scroller.clientHeight; | |
|
833 | scrollbar.style.height = scrollbarHeight + "px"; | |||
|
834 | if (scroller.clientHeight) | |||
|
835 | scrollbarInner.style.height = virtualHeight + "px"; | |||
|
836 | // Position the mover div to align with the current virtual scroll position | |||
|
837 | if (scrollTop != null) scrollbar.scrollTop = scrollTop; | |||
|
838 | mover.style.top = (displayOffset * th - scrollbar.scrollTop) + "px"; | |||
|
839 | scrollbar.style.display = (virtualHeight > scrollbarHeight) ? "block" : "none"; | |||
|
840 | } | |||
|
841 | ||||
|
842 | // On Mac OS X Lion and up, detect whether the mouse is plugged in by measuring | |||
|
843 | // the width of a div with a scrollbar in it. If the width is <= 1, then | |||
|
844 | // the mouse isn't plugged in and scrollbars should overlap the content. | |||
|
845 | function overlapScrollbars() { | |||
|
846 | var tmpSb = document.createElement('div'), | |||
|
847 | tmpSbInner = document.createElement('div'); | |||
|
848 | tmpSb.className = "CodeMirror-scrollbar"; | |||
|
849 | tmpSb.style.cssText = "position: absolute; left: -9999px; height: 100px;"; | |||
|
850 | tmpSbInner.className = "CodeMirror-scrollbar-inner"; | |||
|
851 | tmpSbInner.style.height = "200px"; | |||
|
852 | tmpSb.appendChild(tmpSbInner); | |||
|
853 | ||||
|
854 | document.body.appendChild(tmpSb); | |||
|
855 | var result = (tmpSb.offsetWidth <= 1); | |||
|
856 | document.body.removeChild(tmpSb); | |||
|
857 | return result; | |||
|
858 | } | |||
|
859 | ||||
|
860 | function computeMaxLength() { | |||
|
861 | var maxLineLength = 0; | |||
|
862 | maxLine = ""; maxLineChanged = true; | |||
|
863 | doc.iter(0, doc.size, function(line) { | |||
|
864 | var l = line.text; | |||
|
865 | if (!line.hidden && l.length > maxLineLength) { | |||
|
866 | maxLineLength = l.length; maxLine = l; | |||
|
867 | } | |||
|
868 | }); | |||
|
869 | updateMaxLine = false; | |||
589 | } |
|
870 | } | |
590 |
|
871 | |||
591 | function replaceRange(code, from, to) { |
|
872 | function replaceRange(code, from, to) { | |
@@ -623,10 +904,10 b' var CodeMirror = (function() {' | |||||
623 |
|
904 | |||
624 | function getRange(from, to) { |
|
905 | function getRange(from, to) { | |
625 | var l1 = from.line, l2 = to.line; |
|
906 | var l1 = from.line, l2 = to.line; | |
626 |
if (l1 == l2) return |
|
907 | if (l1 == l2) return getLine(l1).text.slice(from.ch, to.ch); | |
627 |
var code = [ |
|
908 | var code = [getLine(l1).text.slice(from.ch)]; | |
628 |
|
|
909 | doc.iter(l1 + 1, l2, function(line) { code.push(line.text); }); | |
629 |
code.push( |
|
910 | code.push(getLine(l2).text.slice(0, to.ch)); | |
630 | return code.join("\n"); |
|
911 | return code.join("\n"); | |
631 | } |
|
912 | } | |
632 | function getSelection() { |
|
913 | function getSelection() { | |
@@ -636,115 +917,56 b' var CodeMirror = (function() {' | |||||
636 | var pollingFast = false; // Ensures slowPoll doesn't cancel fastPoll |
|
917 | var pollingFast = false; // Ensures slowPoll doesn't cancel fastPoll | |
637 | function slowPoll() { |
|
918 | function slowPoll() { | |
638 | if (pollingFast) return; |
|
919 | if (pollingFast) return; | |
639 |
poll.set( |
|
920 | poll.set(options.pollInterval, function() { | |
640 | startOperation(); |
|
921 | startOperation(); | |
641 | readInput(); |
|
922 | readInput(); | |
642 | if (focused) slowPoll(); |
|
923 | if (focused) slowPoll(); | |
643 | endOperation(); |
|
924 | endOperation(); | |
644 | }); |
|
925 | }); | |
645 | } |
|
926 | } | |
646 |
function fastPoll( |
|
927 | function fastPoll() { | |
647 | var missed = false; |
|
928 | var missed = false; | |
648 | pollingFast = true; |
|
929 | pollingFast = true; | |
649 | function p() { |
|
930 | function p() { | |
650 | startOperation(); |
|
931 | startOperation(); | |
651 | var changed = readInput(); |
|
932 | var changed = readInput(); | |
652 | if (changed && keyId) { |
|
933 | if (!changed && !missed) {missed = true; poll.set(60, p);} | |
653 | if (changed == "moved" && movementKeys[keyId] == null) movementKeys[keyId] = true; |
|
|||
654 | if (changed == "changed") movementKeys[keyId] = false; |
|
|||
655 | } |
|
|||
656 | if (!changed && !missed) {missed = true; poll.set(80, p);} |
|
|||
657 | else {pollingFast = false; slowPoll();} |
|
934 | else {pollingFast = false; slowPoll();} | |
658 | endOperation(); |
|
935 | endOperation(); | |
659 | } |
|
936 | } | |
660 | poll.set(20, p); |
|
937 | poll.set(20, p); | |
661 | } |
|
938 | } | |
662 |
|
939 | |||
663 | // Inspects the textarea, compares its state (content, selection) |
|
940 | // Previnput is a hack to work with IME. If we reset the textarea | |
664 | // to the data in the editing variable, and updates the editor |
|
941 | // on every change, that breaks IME. So we look for changes | |
665 | // content or cursor if something changed. |
|
942 | // compared to the previous content instead. (Modern browsers have | |
|
943 | // events that indicate IME taking place, but these are not widely | |||
|
944 | // supported or compatible enough yet to rely on.) | |||
|
945 | var prevInput = ""; | |||
666 | function readInput() { |
|
946 | function readInput() { | |
667 | if (leaveInputAlone || !focused) return; |
|
947 | if (leaveInputAlone || !focused || hasSelection(input) || options.readOnly) return false; | |
668 | var changed = false, text = input.value, sr = selRange(input); |
|
948 | var text = input.value; | |
669 |
if ( |
|
949 | if (text == prevInput) return false; | |
670 | var changed = editing.text != text, rs = reducedSelection; |
|
950 | shiftSelecting = null; | |
671 | var moved = changed || sr.start != editing.start || sr.end != (rs ? editing.start : editing.end); |
|
951 | var same = 0, l = Math.min(prevInput.length, text.length); | |
672 | if (!moved && !rs) return false; |
|
952 | while (same < l && prevInput[same] == text[same]) ++same; | |
673 | if (changed) { |
|
953 | if (same < prevInput.length) | |
674 | shiftSelecting = reducedSelection = null; |
|
954 | sel.from = {line: sel.from.line, ch: sel.from.ch - (prevInput.length - same)}; | |
675 | if (options.readOnly) {updateInput = true; return "changed";} |
|
955 | else if (overwrite && posEq(sel.from, sel.to)) | |
676 | } |
|
956 | sel.to = {line: sel.to.line, ch: Math.min(getLine(sel.to.line).text.length, sel.to.ch + (text.length - same))}; | |
677 |
|
957 | replaceSelection(text.slice(same), "end"); | ||
678 | // Compute selection start and end based on start/end offsets in textarea |
|
958 | if (text.length > 1000) { input.value = prevInput = ""; } | |
679 | function computeOffset(n, startLine) { |
|
959 | else prevInput = text; | |
680 | var pos = 0; |
|
960 | return true; | |
681 | for (;;) { |
|
961 | } | |
682 | var found = text.indexOf("\n", pos); |
|
962 | function resetInput(user) { | |
683 | if (found == -1 || (text.charAt(found-1) == "\r" ? found - 1 : found) >= n) |
|
963 | if (!posEq(sel.from, sel.to)) { | |
684 | return {line: startLine, ch: n - pos}; |
|
964 | prevInput = ""; | |
685 | ++startLine; |
|
965 | input.value = getSelection(); | |
686 | pos = found + 1; |
|
966 | selectInput(input); | |
687 | } |
|
967 | } else if (user) prevInput = input.value = ""; | |
688 | } |
|
|||
689 | var from = computeOffset(sr.start, editing.from), |
|
|||
690 | to = computeOffset(sr.end, editing.from); |
|
|||
691 | // Here we have to take the reducedSelection hack into account, |
|
|||
692 | // so that you can, for example, press shift-up at the start of |
|
|||
693 | // your selection and have the right thing happen. |
|
|||
694 | if (rs) { |
|
|||
695 | var head = sr.start == rs.anchor ? to : from; |
|
|||
696 | var tail = shiftSelecting ? sel.to : sr.start == rs.anchor ? from : to; |
|
|||
697 | if (sel.inverted = posLess(head, tail)) { from = head; to = tail; } |
|
|||
698 | else { reducedSelection = null; from = tail; to = head; } |
|
|||
699 | } |
|
|||
700 |
|
||||
701 | // In some cases (cursor on same line as before), we don't have |
|
|||
702 | // to update the textarea content at all. |
|
|||
703 | if (from.line == to.line && from.line == sel.from.line && from.line == sel.to.line && !shiftSelecting) |
|
|||
704 | updateInput = false; |
|
|||
705 |
|
||||
706 | // Magic mess to extract precise edited range from the changed |
|
|||
707 | // string. |
|
|||
708 | if (changed) { |
|
|||
709 | var start = 0, end = text.length, len = Math.min(end, editing.text.length); |
|
|||
710 | var c, line = editing.from, nl = -1; |
|
|||
711 | while (start < len && (c = text.charAt(start)) == editing.text.charAt(start)) { |
|
|||
712 | ++start; |
|
|||
713 | if (c == "\n") {line++; nl = start;} |
|
|||
714 | } |
|
|||
715 | var ch = nl > -1 ? start - nl : start, endline = editing.to - 1, edend = editing.text.length; |
|
|||
716 | for (;;) { |
|
|||
717 | c = editing.text.charAt(edend); |
|
|||
718 | if (text.charAt(end) != c) {++end; ++edend; break;} |
|
|||
719 | if (c == "\n") endline--; |
|
|||
720 | if (edend <= start || end <= start) break; |
|
|||
721 | --end; --edend; |
|
|||
722 | } |
|
|||
723 | var nl = editing.text.lastIndexOf("\n", edend - 1), endch = nl == -1 ? edend : edend - nl - 1; |
|
|||
724 | updateLines({line: line, ch: ch}, {line: endline, ch: endch}, splitLines(text.slice(start, end)), from, to); |
|
|||
725 | if (line != endline || from.line != line) updateInput = true; |
|
|||
726 | } |
|
|||
727 | else setSelection(from, to); |
|
|||
728 |
|
||||
729 | editing.text = text; editing.start = sr.start; editing.end = sr.end; |
|
|||
730 | return changed ? "changed" : moved ? "moved" : false; |
|
|||
731 | } |
|
968 | } | |
732 |
|
969 | |||
733 | // Set the textarea content and selection range to match the |
|
|||
734 | // editor state. |
|
|||
735 | function prepareInput() { |
|
|||
736 | var text = []; |
|
|||
737 | var from = Math.max(0, sel.from.line - 1), to = Math.min(lines.length, sel.to.line + 2); |
|
|||
738 | for (var i = from; i < to; ++i) text.push(lines[i].text); |
|
|||
739 | text = input.value = text.join(lineSep); |
|
|||
740 | var startch = sel.from.ch, endch = sel.to.ch; |
|
|||
741 | for (var i = from; i < sel.from.line; ++i) |
|
|||
742 | startch += lineSep.length + lines[i].text.length; |
|
|||
743 | for (var i = from; i < sel.to.line; ++i) |
|
|||
744 | endch += lineSep.length + lines[i].text.length; |
|
|||
745 | editing = {text: text, from: from, to: to, start: startch, end: endch}; |
|
|||
746 | setSelRange(input, startch, reducedSelection ? startch : endch); |
|
|||
747 | } |
|
|||
748 | function focusInput() { |
|
970 | function focusInput() { | |
749 | if (options.readOnly != "nocursor") input.focus(); |
|
971 | if (options.readOnly != "nocursor") input.focus(); | |
750 | } |
|
972 | } | |
@@ -752,59 +974,156 b' var CodeMirror = (function() {' | |||||
752 | function scrollEditorIntoView() { |
|
974 | function scrollEditorIntoView() { | |
753 | if (!cursor.getBoundingClientRect) return; |
|
975 | if (!cursor.getBoundingClientRect) return; | |
754 | var rect = cursor.getBoundingClientRect(); |
|
976 | var rect = cursor.getBoundingClientRect(); | |
|
977 | // IE returns bogus coordinates when the instance sits inside of an iframe and the cursor is hidden | |||
|
978 | if (ie && rect.top == rect.bottom) return; | |||
755 | var winH = window.innerHeight || Math.max(document.body.offsetHeight, document.documentElement.offsetHeight); |
|
979 | var winH = window.innerHeight || Math.max(document.body.offsetHeight, document.documentElement.offsetHeight); | |
756 |
if (rect.top < 0 || rect.bottom > winH) cursor |
|
980 | if (rect.top < 0 || rect.bottom > winH) scrollCursorIntoView(); | |
757 | } |
|
981 | } | |
758 | function scrollCursorIntoView() { |
|
982 | function scrollCursorIntoView() { | |
|
983 | var coords = calculateCursorCoords(); | |||
|
984 | return scrollIntoView(coords.x, coords.y, coords.x, coords.yBot); | |||
|
985 | } | |||
|
986 | function calculateCursorCoords() { | |||
759 | var cursor = localCoords(sel.inverted ? sel.from : sel.to); |
|
987 | var cursor = localCoords(sel.inverted ? sel.from : sel.to); | |
760 | return scrollIntoView(cursor.x, cursor.y, cursor.x, cursor.yBot); |
|
988 | var x = options.lineWrapping ? Math.min(cursor.x, lineSpace.offsetWidth) : cursor.x; | |
|
989 | return {x: x, y: cursor.y, yBot: cursor.yBot}; | |||
761 | } |
|
990 | } | |
762 | function scrollIntoView(x1, y1, x2, y2) { |
|
991 | function scrollIntoView(x1, y1, x2, y2) { | |
763 | var pl = paddingLeft(), pt = paddingTop(), lh = lineHeight(); |
|
992 | var scrollPos = calculateScrollPos(x1, y1, x2, y2), scrolled = false; | |
|
993 | if (scrollPos.scrollLeft != null) {scroller.scrollLeft = scrollPos.scrollLeft; scrolled = true;} | |||
|
994 | if (scrollPos.scrollTop != null) {scrollbar.scrollTop = scrollPos.scrollTop; scrolled = true;} | |||
|
995 | if (scrolled && options.onScroll) options.onScroll(instance); | |||
|
996 | } | |||
|
997 | function calculateScrollPos(x1, y1, x2, y2) { | |||
|
998 | var pl = paddingLeft(), pt = paddingTop(); | |||
764 | y1 += pt; y2 += pt; x1 += pl; x2 += pl; |
|
999 | y1 += pt; y2 += pt; x1 += pl; x2 += pl; | |
765 |
var screen = scroller.clientHeight, screentop = scroll |
|
1000 | var screen = scroller.clientHeight, screentop = scrollbar.scrollTop, result = {}; | |
766 | if (y1 < screentop) {scroller.scrollTop = Math.max(0, y1 - 2*lh); scrolled = true;} |
|
1001 | var atTop = y1 < paddingTop() + 10; | |
767 | else if (y2 > screentop + screen) {scroller.scrollTop = y2 + lh - screen; scrolled = true;} |
|
1002 | if (y1 < screentop) result.scrollTop = atTop ? 0 : Math.max(0, y1); | |
|
1003 | else if (y2 > screentop + screen) result.scrollTop = y2 - screen; | |||
768 |
|
1004 | |||
769 | var screenw = scroller.clientWidth, screenleft = scroller.scrollLeft; |
|
1005 | var screenw = scroller.clientWidth, screenleft = scroller.scrollLeft; | |
770 | var gutterw = options.fixedGutter ? gutter.clientWidth : 0; |
|
1006 | var gutterw = options.fixedGutter ? gutter.clientWidth : 0; | |
771 | if (x1 < screenleft + gutterw) { |
|
1007 | var atLeft = x1 < gutterw + pl + 10; | |
772 | if (x1 < 50) x1 = 0; |
|
1008 | if (x1 < screenleft + gutterw || atLeft) { | |
773 | scroller.scrollLeft = Math.max(0, x1 - 10 - gutterw); |
|
1009 | if (atLeft) x1 = 0; | |
774 | scrolled = true; |
|
1010 | result.scrollLeft = Math.max(0, x1 - 10 - gutterw); | |
|
1011 | } else if (x2 > screenw + screenleft - 3) { | |||
|
1012 | result.scrollLeft = x2 + 10 - screenw; | |||
775 | } |
|
1013 | } | |
776 | else if (x2 > screenw + screenleft) { |
|
|||
777 | scroller.scrollLeft = x2 + 10 - screenw; |
|
|||
778 | scrolled = true; |
|
|||
779 | if (x2 > code.clientWidth) result = false; |
|
|||
780 | } |
|
|||
781 | if (scrolled && options.onScroll) options.onScroll(instance); |
|
|||
782 | return result; |
|
1014 | return result; | |
783 | } |
|
1015 | } | |
784 |
|
1016 | |||
785 | function visibleLines() { |
|
1017 | function visibleLines(scrollTop) { | |
786 |
var lh = |
|
1018 | var lh = textHeight(), top = (scrollTop != null ? scrollTop : scrollbar.scrollTop) - paddingTop(); | |
787 |
r |
|
1019 | var fromHeight = Math.max(0, Math.floor(top / lh)); | |
788 |
|
|
1020 | var toHeight = Math.ceil((top + scroller.clientHeight) / lh); | |
|
1021 | return {from: lineAtHeight(doc, fromHeight), | |||
|
1022 | to: lineAtHeight(doc, toHeight)}; | |||
789 | } |
|
1023 | } | |
790 | // Uses a set of changes plus the current scroll position to |
|
1024 | // Uses a set of changes plus the current scroll position to | |
791 | // determine which DOM updates have to be made, and makes the |
|
1025 | // determine which DOM updates have to be made, and makes the | |
792 | // updates. |
|
1026 | // updates. | |
793 | function updateDisplay(changes) { |
|
1027 | function updateDisplay(changes, suppressCallback, scrollTop) { | |
794 | if (!scroller.clientWidth) { |
|
1028 | if (!scroller.clientWidth) { | |
795 | showingFrom = showingTo = 0; |
|
1029 | showingFrom = showingTo = displayOffset = 0; | |
|
1030 | return; | |||
|
1031 | } | |||
|
1032 | // Compute the new visible window | |||
|
1033 | // If scrollTop is specified, use that to determine which lines | |||
|
1034 | // to render instead of the current scrollbar position. | |||
|
1035 | var visible = visibleLines(scrollTop); | |||
|
1036 | // Bail out if the visible area is already rendered and nothing changed. | |||
|
1037 | if (changes !== true && changes.length == 0 && visible.from > showingFrom && visible.to < showingTo) { | |||
|
1038 | updateVerticalScroll(scrollTop); | |||
|
1039 | return; | |||
|
1040 | } | |||
|
1041 | var from = Math.max(visible.from - 100, 0), to = Math.min(doc.size, visible.to + 100); | |||
|
1042 | if (showingFrom < from && from - showingFrom < 20) from = showingFrom; | |||
|
1043 | if (showingTo > to && showingTo - to < 20) to = Math.min(doc.size, showingTo); | |||
|
1044 | ||||
|
1045 | // Create a range of theoretically intact lines, and punch holes | |||
|
1046 | // in that using the change info. | |||
|
1047 | var intact = changes === true ? [] : | |||
|
1048 | computeIntact([{from: showingFrom, to: showingTo, domStart: 0}], changes); | |||
|
1049 | // Clip off the parts that won't be visible | |||
|
1050 | var intactLines = 0; | |||
|
1051 | for (var i = 0; i < intact.length; ++i) { | |||
|
1052 | var range = intact[i]; | |||
|
1053 | if (range.from < from) {range.domStart += (from - range.from); range.from = from;} | |||
|
1054 | if (range.to > to) range.to = to; | |||
|
1055 | if (range.from >= range.to) intact.splice(i--, 1); | |||
|
1056 | else intactLines += range.to - range.from; | |||
|
1057 | } | |||
|
1058 | if (intactLines == to - from && from == showingFrom && to == showingTo) { | |||
|
1059 | updateVerticalScroll(scrollTop); | |||
796 | return; |
|
1060 | return; | |
797 | } |
|
1061 | } | |
798 | // First create a range of theoretically intact lines, and punch |
|
1062 | intact.sort(function(a, b) {return a.domStart - b.domStart;}); | |
799 | // holes in that using the change info. |
|
1063 | ||
800 | var intact = changes === true ? [] : [{from: showingFrom, to: showingTo, domStart: 0}]; |
|
1064 | var th = textHeight(), gutterDisplay = gutter.style.display; | |
|
1065 | lineDiv.style.display = "none"; | |||
|
1066 | patchDisplay(from, to, intact); | |||
|
1067 | lineDiv.style.display = gutter.style.display = ""; | |||
|
1068 | ||||
|
1069 | var different = from != showingFrom || to != showingTo || lastSizeC != scroller.clientHeight + th; | |||
|
1070 | // This is just a bogus formula that detects when the editor is | |||
|
1071 | // resized or the font size changes. | |||
|
1072 | if (different) lastSizeC = scroller.clientHeight + th; | |||
|
1073 | showingFrom = from; showingTo = to; | |||
|
1074 | displayOffset = heightAtLine(doc, from); | |||
|
1075 | ||||
|
1076 | // Since this is all rather error prone, it is honoured with the | |||
|
1077 | // only assertion in the whole file. | |||
|
1078 | if (lineDiv.childNodes.length != showingTo - showingFrom) | |||
|
1079 | throw new Error("BAD PATCH! " + JSON.stringify(intact) + " size=" + (showingTo - showingFrom) + | |||
|
1080 | " nodes=" + lineDiv.childNodes.length); | |||
|
1081 | ||||
|
1082 | function checkHeights() { | |||
|
1083 | var curNode = lineDiv.firstChild, heightChanged = false; | |||
|
1084 | doc.iter(showingFrom, showingTo, function(line) { | |||
|
1085 | if (!line.hidden) { | |||
|
1086 | var height = Math.round(curNode.offsetHeight / th) || 1; | |||
|
1087 | if (line.height != height) { | |||
|
1088 | updateLineHeight(line, height); | |||
|
1089 | gutterDirty = heightChanged = true; | |||
|
1090 | } | |||
|
1091 | } | |||
|
1092 | curNode = curNode.nextSibling; | |||
|
1093 | }); | |||
|
1094 | return heightChanged; | |||
|
1095 | } | |||
|
1096 | ||||
|
1097 | if (options.lineWrapping) { | |||
|
1098 | // Guess whether we're going to need the scrollbar, so that we don't end up changing the linewrapping | |||
|
1099 | // after the scrollbar appears (during updateVerticalScroll()). Only do this if the scrollbar is | |||
|
1100 | // appearing (if it's disappearing, we don't have to worry about the scroll position, and there are | |||
|
1101 | // issues on IE7 if we turn it off too early). | |||
|
1102 | var virtualHeight = Math.floor(doc.height * th + 2 * paddingTop()), scrollbarHeight = scroller.clientHeight; | |||
|
1103 | if (virtualHeight > scrollbarHeight) scrollbar.style.display = "block"; | |||
|
1104 | checkHeights(); | |||
|
1105 | } | |||
|
1106 | ||||
|
1107 | gutter.style.display = gutterDisplay; | |||
|
1108 | if (different || gutterDirty) { | |||
|
1109 | // If the gutter grew in size, re-check heights. If those changed, re-draw gutter. | |||
|
1110 | updateGutter() && options.lineWrapping && checkHeights() && updateGutter(); | |||
|
1111 | } | |||
|
1112 | updateSelection(); | |||
|
1113 | updateVerticalScroll(scrollTop); | |||
|
1114 | if (!suppressCallback && options.onUpdate) options.onUpdate(instance); | |||
|
1115 | return true; | |||
|
1116 | } | |||
|
1117 | ||||
|
1118 | function computeIntact(intact, changes) { | |||
801 | for (var i = 0, l = changes.length || 0; i < l; ++i) { |
|
1119 | for (var i = 0, l = changes.length || 0; i < l; ++i) { | |
802 | var change = changes[i], intact2 = [], diff = change.diff || 0; |
|
1120 | var change = changes[i], intact2 = [], diff = change.diff || 0; | |
803 | for (var j = 0, l2 = intact.length; j < l2; ++j) { |
|
1121 | for (var j = 0, l2 = intact.length; j < l2; ++j) { | |
804 | var range = intact[j]; |
|
1122 | var range = intact[j]; | |
805 | if (change.to <= range.from) |
|
1123 | if (change.to <= range.from && change.diff) | |
806 |
intact2.push({from: range.from + diff, to: range.to + diff, |
|
1124 | intact2.push({from: range.from + diff, to: range.to + diff, | |
807 | else if (range.to <= change.from) |
|
1125 | domStart: range.domStart}); | |
|
1126 | else if (change.to <= range.from || change.from >= range.to) | |||
808 | intact2.push(range); |
|
1127 | intact2.push(range); | |
809 | else { |
|
1128 | else { | |
810 | if (change.from > range.from) |
|
1129 | if (change.from > range.from) | |
@@ -816,168 +1135,130 b' var CodeMirror = (function() {' | |||||
816 | } |
|
1135 | } | |
817 | intact = intact2; |
|
1136 | intact = intact2; | |
818 | } |
|
1137 | } | |
819 |
|
1138 | return intact; | ||
820 | // Then, determine which lines we'd want to see, and which |
|
|||
821 | // updates have to be made to get there. |
|
|||
822 | var visible = visibleLines(); |
|
|||
823 | var from = Math.min(showingFrom, Math.max(visible.from - 3, 0)), |
|
|||
824 | to = Math.min(lines.length, Math.max(showingTo, visible.to + 3)), |
|
|||
825 | updates = [], domPos = 0, domEnd = showingTo - showingFrom, pos = from, changedLines = 0; |
|
|||
826 |
|
||||
827 | for (var i = 0, l = intact.length; i < l; ++i) { |
|
|||
828 | var range = intact[i]; |
|
|||
829 | if (range.to <= from) continue; |
|
|||
830 | if (range.from >= to) break; |
|
|||
831 | if (range.domStart > domPos || range.from > pos) { |
|
|||
832 | updates.push({from: pos, to: range.from, domSize: range.domStart - domPos, domStart: domPos}); |
|
|||
833 | changedLines += range.from - pos; |
|
|||
834 | } |
|
|||
835 | pos = range.to; |
|
|||
836 | domPos = range.domStart + (range.to - range.from); |
|
|||
837 | } |
|
|||
838 | if (domPos != domEnd || pos != to) { |
|
|||
839 | changedLines += Math.abs(to - pos); |
|
|||
840 | updates.push({from: pos, to: to, domSize: domEnd - domPos, domStart: domPos}); |
|
|||
841 | if (to - pos != domEnd - domPos) gutterDirty = true; |
|
|||
842 | } |
|
|||
843 |
|
||||
844 | if (!updates.length) return; |
|
|||
845 | lineDiv.style.display = "none"; |
|
|||
846 | // If more than 30% of the screen needs update, just do a full |
|
|||
847 | // redraw (which is quicker than patching) |
|
|||
848 | if (changedLines > (visible.to - visible.from) * .3) |
|
|||
849 | refreshDisplay(from = Math.max(visible.from - 10, 0), to = Math.min(visible.to + 7, lines.length)); |
|
|||
850 | // Otherwise, only update the stuff that needs updating. |
|
|||
851 | else |
|
|||
852 | patchDisplay(updates); |
|
|||
853 | lineDiv.style.display = ""; |
|
|||
854 |
|
||||
855 | // Position the mover div to align with the lines it's supposed |
|
|||
856 | // to be showing (which will cover the visible display) |
|
|||
857 | var different = from != showingFrom || to != showingTo || lastHeight != scroller.clientHeight; |
|
|||
858 | showingFrom = from; showingTo = to; |
|
|||
859 | mover.style.top = (from * lineHeight()) + "px"; |
|
|||
860 | if (different) { |
|
|||
861 | lastHeight = scroller.clientHeight; |
|
|||
862 | code.style.height = (lines.length * lineHeight() + 2 * paddingTop()) + "px"; |
|
|||
863 | } |
|
|||
864 | if (different || gutterDirty) updateGutter(); |
|
|||
865 |
|
||||
866 | if (maxWidth == null) maxWidth = stringWidth(maxLine); |
|
|||
867 | if (maxWidth > scroller.clientWidth) { |
|
|||
868 | lineSpace.style.width = maxWidth + "px"; |
|
|||
869 | // Needed to prevent odd wrapping/hiding of widgets placed in here. |
|
|||
870 | code.style.width = ""; |
|
|||
871 | code.style.width = scroller.scrollWidth + "px"; |
|
|||
872 | } else { |
|
|||
873 | lineSpace.style.width = code.style.width = ""; |
|
|||
874 | } |
|
|||
875 |
|
||||
876 | // Since this is all rather error prone, it is honoured with the |
|
|||
877 | // only assertion in the whole file. |
|
|||
878 | if (lineDiv.childNodes.length != showingTo - showingFrom) |
|
|||
879 | throw new Error("BAD PATCH! " + JSON.stringify(updates) + " size=" + (showingTo - showingFrom) + |
|
|||
880 | " nodes=" + lineDiv.childNodes.length); |
|
|||
881 | updateCursor(); |
|
|||
882 | } |
|
1139 | } | |
883 |
|
1140 | |||
884 |
function |
|
1141 | function patchDisplay(from, to, intact) { | |
885 | var html = [], start = {line: from, ch: 0}, inSel = posLess(sel.from, start) && !posLess(sel.to, start); |
|
1142 | // The first pass removes the DOM nodes that aren't intact. | |
886 | for (var i = from; i < to; ++i) { |
|
1143 | if (!intact.length) lineDiv.innerHTML = ""; | |
887 | var ch1 = null, ch2 = null; |
|
1144 | else { | |
888 |
|
|
1145 | function killNode(node) { | |
889 | ch1 = 0; |
|
1146 | var tmp = node.nextSibling; | |
890 | if (sel.to.line == i) {inSel = false; ch2 = sel.to.ch;} |
|
1147 | node.parentNode.removeChild(node); | |
|
1148 | return tmp; | |||
891 | } |
|
1149 | } | |
892 | else if (sel.from.line == i) { |
|
1150 | var domPos = 0, curNode = lineDiv.firstChild, n; | |
893 | if (sel.to.line == i) {ch1 = sel.from.ch; ch2 = sel.to.ch;} |
|
1151 | for (var i = 0; i < intact.length; ++i) { | |
894 | else {inSel = true; ch1 = sel.from.ch;} |
|
1152 | var cur = intact[i]; | |
|
1153 | while (cur.domStart > domPos) {curNode = killNode(curNode); domPos++;} | |||
|
1154 | for (var j = 0, e = cur.to - cur.from; j < e; ++j) {curNode = curNode.nextSibling; domPos++;} | |||
895 | } |
|
1155 | } | |
896 | html.push(lines[i].getHTML(ch1, ch2, true)); |
|
1156 | while (curNode) curNode = killNode(curNode); | |
897 | } |
|
1157 | } | |
898 | lineDiv.innerHTML = html.join(""); |
|
1158 | // This pass fills in the lines that actually changed. | |
899 | } |
|
1159 | var nextIntact = intact.shift(), curNode = lineDiv.firstChild, j = from; | |
900 | function patchDisplay(updates) { |
|
1160 | var scratch = document.createElement("div"); | |
901 | // Slightly different algorithm for IE (badInnerHTML), since |
|
1161 | doc.iter(from, to, function(line) { | |
902 | // there .innerHTML on PRE nodes is dumb, and discards |
|
1162 | if (nextIntact && nextIntact.to == j) nextIntact = intact.shift(); | |
903 | // whitespace. |
|
1163 | if (!nextIntact || nextIntact.from > j) { | |
904 | var sfrom = sel.from.line, sto = sel.to.line, off = 0, |
|
1164 | if (line.hidden) var html = scratch.innerHTML = "<pre></pre>"; | |
905 | scratch = badInnerHTML && targetDocument.createElement("div"); |
|
1165 | else { | |
906 | for (var i = 0, e = updates.length; i < e; ++i) { |
|
1166 | var html = '<pre' + (line.className ? ' class="' + line.className + '"' : '') + '>' | |
907 | var rec = updates[i]; |
|
1167 | + line.getHTML(makeTab) + '</pre>'; | |
908 | var extra = (rec.to - rec.from) - rec.domSize; |
|
1168 | // Kludge to make sure the styled element lies behind the selection (by z-index) | |
909 | var nodeAfter = lineDiv.childNodes[rec.domStart + rec.domSize + off] || null; |
|
1169 | if (line.bgClassName) | |
910 | if (badInnerHTML) |
|
1170 | html = '<div style="position: relative"><pre class="' + line.bgClassName + | |
911 | for (var j = Math.max(-extra, rec.domSize); j > 0; --j) |
|
1171 | '" style="position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: -2"> </pre>' + html + "</div>"; | |
912 | lineDiv.removeChild(nodeAfter ? nodeAfter.previousSibling : lineDiv.lastChild); |
|
1172 | } | |
913 | else if (extra) { |
|
1173 | scratch.innerHTML = html; | |
914 | for (var j = Math.max(0, extra); j > 0; --j) |
|
1174 | lineDiv.insertBefore(scratch.firstChild, curNode); | |
915 | lineDiv.insertBefore(targetDocument.createElement("pre"), nodeAfter); |
|
1175 | } else { | |
916 | for (var j = Math.max(0, -extra); j > 0; --j) |
|
1176 | curNode = curNode.nextSibling; | |
917 | lineDiv.removeChild(nodeAfter ? nodeAfter.previousSibling : lineDiv.lastChild); |
|
|||
918 | } |
|
1177 | } | |
919 | var node = lineDiv.childNodes[rec.domStart + off], inSel = sfrom < rec.from && sto >= rec.from; |
|
1178 | ++j; | |
920 | for (var j = rec.from; j < rec.to; ++j) { |
|
1179 | }); | |
921 | var ch1 = null, ch2 = null; |
|
|||
922 | if (inSel) { |
|
|||
923 | ch1 = 0; |
|
|||
924 | if (sto == j) {inSel = false; ch2 = sel.to.ch;} |
|
|||
925 | } |
|
|||
926 | else if (sfrom == j) { |
|
|||
927 | if (sto == j) {ch1 = sel.from.ch; ch2 = sel.to.ch;} |
|
|||
928 | else {inSel = true; ch1 = sel.from.ch;} |
|
|||
929 | } |
|
|||
930 | if (badInnerHTML) { |
|
|||
931 | scratch.innerHTML = lines[j].getHTML(ch1, ch2, true); |
|
|||
932 | lineDiv.insertBefore(scratch.firstChild, nodeAfter); |
|
|||
933 | } |
|
|||
934 | else { |
|
|||
935 | node.innerHTML = lines[j].getHTML(ch1, ch2, false); |
|
|||
936 | node.className = lines[j].className || ""; |
|
|||
937 | node = node.nextSibling; |
|
|||
938 | } |
|
|||
939 | } |
|
|||
940 | off += extra; |
|
|||
941 | } |
|
|||
942 | } |
|
1180 | } | |
943 |
|
1181 | |||
944 | function updateGutter() { |
|
1182 | function updateGutter() { | |
945 | if (!options.gutter && !options.lineNumbers) return; |
|
1183 | if (!options.gutter && !options.lineNumbers) return; | |
946 | var hText = mover.offsetHeight, hEditor = scroller.clientHeight; |
|
1184 | var hText = mover.offsetHeight, hEditor = scroller.clientHeight; | |
947 | gutter.style.height = (hText - hEditor < 2 ? hEditor : hText) + "px"; |
|
1185 | gutter.style.height = (hText - hEditor < 2 ? hEditor : hText) + "px"; | |
948 | var html = []; |
|
1186 | var html = [], i = showingFrom, normalNode; | |
949 |
|
|
1187 | doc.iter(showingFrom, Math.max(showingTo, showingFrom + 1), function(line) { | |
950 | var marker = lines[i].gutterMarker; |
|
1188 | if (line.hidden) { | |
951 | var text = options.lineNumbers ? i + options.firstLineNumber : null; |
|
1189 | html.push("<pre></pre>"); | |
952 | if (marker && marker.text) |
|
1190 | } else { | |
953 | text = marker.text.replace("%N%", text != null ? text : ""); |
|
1191 | var marker = line.gutterMarker; | |
954 | else if (text == null) |
|
1192 | var text = options.lineNumbers ? i + options.firstLineNumber : null; | |
955 | text = "\u00a0"; |
|
1193 | if (marker && marker.text) | |
956 | html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text, "</pre>"); |
|
1194 | text = marker.text.replace("%N%", text != null ? text : ""); | |
957 | } |
|
1195 | else if (text == null) | |
|
1196 | text = "\u00a0"; | |||
|
1197 | html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text); | |||
|
1198 | for (var j = 1; j < line.height; ++j) html.push("<br/> "); | |||
|
1199 | html.push("</pre>"); | |||
|
1200 | if (!marker) normalNode = i; | |||
|
1201 | } | |||
|
1202 | ++i; | |||
|
1203 | }); | |||
958 | gutter.style.display = "none"; |
|
1204 | gutter.style.display = "none"; | |
959 | gutterText.innerHTML = html.join(""); |
|
1205 | gutterText.innerHTML = html.join(""); | |
960 | var minwidth = String(lines.length).length, firstNode = gutterText.firstChild, val = eltText(firstNode), pad = ""; |
|
1206 | // Make sure scrolling doesn't cause number gutter size to pop | |
961 | while (val.length + pad.length < minwidth) pad += "\u00a0"; |
|
1207 | if (normalNode != null && options.lineNumbers) { | |
962 | if (pad) firstNode.insertBefore(targetDocument.createTextNode(pad), firstNode.firstChild); |
|
1208 | var node = gutterText.childNodes[normalNode - showingFrom]; | |
|
1209 | var minwidth = String(doc.size).length, val = eltText(node.firstChild), pad = ""; | |||
|
1210 | while (val.length + pad.length < minwidth) pad += "\u00a0"; | |||
|
1211 | if (pad) node.insertBefore(document.createTextNode(pad), node.firstChild); | |||
|
1212 | } | |||
963 | gutter.style.display = ""; |
|
1213 | gutter.style.display = ""; | |
|
1214 | var resized = Math.abs((parseInt(lineSpace.style.marginLeft) || 0) - gutter.offsetWidth) > 2; | |||
964 | lineSpace.style.marginLeft = gutter.offsetWidth + "px"; |
|
1215 | lineSpace.style.marginLeft = gutter.offsetWidth + "px"; | |
965 | gutterDirty = false; |
|
1216 | gutterDirty = false; | |
|
1217 | return resized; | |||
966 | } |
|
1218 | } | |
967 |
function update |
|
1219 | function updateSelection() { | |
968 | var head = sel.inverted ? sel.from : sel.to, lh = lineHeight(); |
|
1220 | var collapsed = posEq(sel.from, sel.to); | |
969 | var x = charX(head.line, head.ch); |
|
1221 | var fromPos = localCoords(sel.from, true); | |
970 | var top = head.line * lh - scroller.scrollTop; |
|
1222 | var toPos = collapsed ? fromPos : localCoords(sel.to, true); | |
971 | inputDiv.style.top = Math.max(Math.min(top, scroller.offsetHeight), 0) + "px"; |
|
1223 | var headPos = sel.inverted ? fromPos : toPos, th = textHeight(); | |
972 | inputDiv.style.left = (x - scroller.scrollLeft) + "px"; |
|
1224 | var wrapOff = eltOffset(wrapper), lineOff = eltOffset(lineDiv); | |
973 | if (posEq(sel.from, sel.to)) { |
|
1225 | inputDiv.style.top = Math.max(0, Math.min(scroller.offsetHeight, headPos.y + lineOff.top - wrapOff.top)) + "px"; | |
974 | cursor.style.top = (head.line - showingFrom) * lh + "px"; |
|
1226 | inputDiv.style.left = Math.max(0, Math.min(scroller.offsetWidth, headPos.x + lineOff.left - wrapOff.left)) + "px"; | |
975 | cursor.style.left = x + "px"; |
|
1227 | if (collapsed) { | |
|
1228 | cursor.style.top = headPos.y + "px"; | |||
|
1229 | cursor.style.left = (options.lineWrapping ? Math.min(headPos.x, lineSpace.offsetWidth) : headPos.x) + "px"; | |||
976 | cursor.style.display = ""; |
|
1230 | cursor.style.display = ""; | |
|
1231 | selectionDiv.style.display = "none"; | |||
|
1232 | } else { | |||
|
1233 | var sameLine = fromPos.y == toPos.y, html = ""; | |||
|
1234 | var clientWidth = lineSpace.clientWidth || lineSpace.offsetWidth; | |||
|
1235 | var clientHeight = lineSpace.clientHeight || lineSpace.offsetHeight; | |||
|
1236 | function add(left, top, right, height) { | |||
|
1237 | var rstyle = quirksMode ? "width: " + (!right ? clientWidth : clientWidth - right - left) + "px" | |||
|
1238 | : "right: " + right + "px"; | |||
|
1239 | html += '<div class="CodeMirror-selected" style="position: absolute; left: ' + left + | |||
|
1240 | 'px; top: ' + top + 'px; ' + rstyle + '; height: ' + height + 'px"></div>'; | |||
|
1241 | } | |||
|
1242 | if (sel.from.ch && fromPos.y >= 0) { | |||
|
1243 | var right = sameLine ? clientWidth - toPos.x : 0; | |||
|
1244 | add(fromPos.x, fromPos.y, right, th); | |||
|
1245 | } | |||
|
1246 | var middleStart = Math.max(0, fromPos.y + (sel.from.ch ? th : 0)); | |||
|
1247 | var middleHeight = Math.min(toPos.y, clientHeight) - middleStart; | |||
|
1248 | if (middleHeight > 0.2 * th) | |||
|
1249 | add(0, middleStart, 0, middleHeight); | |||
|
1250 | if ((!sameLine || !sel.from.ch) && toPos.y < clientHeight - .5 * th) | |||
|
1251 | add(0, toPos.y, clientWidth - toPos.x, th); | |||
|
1252 | selectionDiv.innerHTML = html; | |||
|
1253 | cursor.style.display = "none"; | |||
|
1254 | selectionDiv.style.display = ""; | |||
977 | } |
|
1255 | } | |
978 | else cursor.style.display = "none"; |
|
|||
979 | } |
|
1256 | } | |
980 |
|
1257 | |||
|
1258 | function setShift(val) { | |||
|
1259 | if (val) shiftSelecting = shiftSelecting || (sel.inverted ? sel.to : sel.from); | |||
|
1260 | else shiftSelecting = null; | |||
|
1261 | } | |||
981 | function setSelectionUser(from, to) { |
|
1262 | function setSelectionUser(from, to) { | |
982 | var sh = shiftSelecting && clipPos(shiftSelecting); |
|
1263 | var sh = shiftSelecting && clipPos(shiftSelecting); | |
983 | if (sh) { |
|
1264 | if (sh) { | |
@@ -985,130 +1266,166 b' var CodeMirror = (function() {' | |||||
985 | else if (posLess(to, sh)) to = sh; |
|
1266 | else if (posLess(to, sh)) to = sh; | |
986 | } |
|
1267 | } | |
987 | setSelection(from, to); |
|
1268 | setSelection(from, to); | |
|
1269 | userSelChange = true; | |||
988 | } |
|
1270 | } | |
989 | // Update the selection. Last two args are only used by |
|
1271 | // Update the selection. Last two args are only used by | |
990 | // updateLines, since they have to be expressed in the line |
|
1272 | // updateLines, since they have to be expressed in the line | |
991 | // numbers before the update. |
|
1273 | // numbers before the update. | |
992 | function setSelection(from, to, oldFrom, oldTo) { |
|
1274 | function setSelection(from, to, oldFrom, oldTo) { | |
|
1275 | goalColumn = null; | |||
|
1276 | if (oldFrom == null) {oldFrom = sel.from.line; oldTo = sel.to.line;} | |||
993 | if (posEq(sel.from, from) && posEq(sel.to, to)) return; |
|
1277 | if (posEq(sel.from, from) && posEq(sel.to, to)) return; | |
994 | if (posLess(to, from)) {var tmp = to; to = from; from = tmp;} |
|
1278 | if (posLess(to, from)) {var tmp = to; to = from; from = tmp;} | |
995 |
|
1279 | |||
|
1280 | // Skip over hidden lines. | |||
|
1281 | if (from.line != oldFrom) { | |||
|
1282 | var from1 = skipHidden(from, oldFrom, sel.from.ch); | |||
|
1283 | // If there is no non-hidden line left, force visibility on current line | |||
|
1284 | if (!from1) setLineHidden(from.line, false); | |||
|
1285 | else from = from1; | |||
|
1286 | } | |||
|
1287 | if (to.line != oldTo) to = skipHidden(to, oldTo, sel.to.ch); | |||
|
1288 | ||||
996 | if (posEq(from, to)) sel.inverted = false; |
|
1289 | if (posEq(from, to)) sel.inverted = false; | |
997 | else if (posEq(from, sel.to)) sel.inverted = false; |
|
1290 | else if (posEq(from, sel.to)) sel.inverted = false; | |
998 | else if (posEq(to, sel.from)) sel.inverted = true; |
|
1291 | else if (posEq(to, sel.from)) sel.inverted = true; | |
999 |
|
1292 | |||
1000 | // Some ugly logic used to only mark the lines that actually did |
|
1293 | if (options.autoClearEmptyLines && posEq(sel.from, sel.to)) { | |
1001 | // see a change in selection as changed, rather than the whole |
|
1294 | var head = sel.inverted ? from : to; | |
1002 | // selected range. |
|
1295 | if (head.line != sel.from.line && sel.from.line < doc.size) { | |
1003 | if (oldFrom == null) {oldFrom = sel.from.line; oldTo = sel.to.line;} |
|
1296 | var oldLine = getLine(sel.from.line); | |
1004 | if (posEq(from, to)) { |
|
1297 | if (/^\s+$/.test(oldLine.text)) | |
1005 | if (!posEq(sel.from, sel.to)) |
|
1298 | setTimeout(operation(function() { | |
1006 | changes.push({from: oldFrom, to: oldTo + 1}); |
|
1299 | if (oldLine.parent && /^\s+$/.test(oldLine.text)) { | |
1007 | } |
|
1300 | var no = lineNo(oldLine); | |
1008 | else if (posEq(sel.from, sel.to)) { |
|
1301 | replaceRange("", {line: no, ch: 0}, {line: no, ch: oldLine.text.length}); | |
1009 | changes.push({from: from.line, to: to.line + 1}); |
|
1302 | } | |
1010 | } |
|
1303 | }, 10)); | |
1011 | else { |
|
|||
1012 | if (!posEq(from, sel.from)) { |
|
|||
1013 | if (from.line < oldFrom) |
|
|||
1014 | changes.push({from: from.line, to: Math.min(to.line, oldFrom) + 1}); |
|
|||
1015 | else |
|
|||
1016 | changes.push({from: oldFrom, to: Math.min(oldTo, from.line) + 1}); |
|
|||
1017 | } |
|
|||
1018 | if (!posEq(to, sel.to)) { |
|
|||
1019 | if (to.line < oldTo) |
|
|||
1020 | changes.push({from: Math.max(oldFrom, from.line), to: oldTo + 1}); |
|
|||
1021 | else |
|
|||
1022 | changes.push({from: Math.max(from.line, oldTo), to: to.line + 1}); |
|
|||
1023 | } |
|
1304 | } | |
1024 | } |
|
1305 | } | |
|
1306 | ||||
1025 | sel.from = from; sel.to = to; |
|
1307 | sel.from = from; sel.to = to; | |
1026 | selectionChanged = true; |
|
1308 | selectionChanged = true; | |
1027 | } |
|
1309 | } | |
|
1310 | function skipHidden(pos, oldLine, oldCh) { | |||
|
1311 | function getNonHidden(dir) { | |||
|
1312 | var lNo = pos.line + dir, end = dir == 1 ? doc.size : -1; | |||
|
1313 | while (lNo != end) { | |||
|
1314 | var line = getLine(lNo); | |||
|
1315 | if (!line.hidden) { | |||
|
1316 | var ch = pos.ch; | |||
|
1317 | if (toEnd || ch > oldCh || ch > line.text.length) ch = line.text.length; | |||
|
1318 | return {line: lNo, ch: ch}; | |||
|
1319 | } | |||
|
1320 | lNo += dir; | |||
|
1321 | } | |||
|
1322 | } | |||
|
1323 | var line = getLine(pos.line); | |||
|
1324 | var toEnd = pos.ch == line.text.length && pos.ch != oldCh; | |||
|
1325 | if (!line.hidden) return pos; | |||
|
1326 | if (pos.line >= oldLine) return getNonHidden(1) || getNonHidden(-1); | |||
|
1327 | else return getNonHidden(-1) || getNonHidden(1); | |||
|
1328 | } | |||
1028 | function setCursor(line, ch, user) { |
|
1329 | function setCursor(line, ch, user) { | |
1029 | var pos = clipPos({line: line, ch: ch || 0}); |
|
1330 | var pos = clipPos({line: line, ch: ch || 0}); | |
1030 | (user ? setSelectionUser : setSelection)(pos, pos); |
|
1331 | (user ? setSelectionUser : setSelection)(pos, pos); | |
1031 | } |
|
1332 | } | |
1032 |
|
1333 | |||
1033 |
function clipLine(n) {return Math.max(0, Math.min(n, |
|
1334 | function clipLine(n) {return Math.max(0, Math.min(n, doc.size-1));} | |
1034 | function clipPos(pos) { |
|
1335 | function clipPos(pos) { | |
1035 | if (pos.line < 0) return {line: 0, ch: 0}; |
|
1336 | if (pos.line < 0) return {line: 0, ch: 0}; | |
1036 |
if (pos.line >= |
|
1337 | if (pos.line >= doc.size) return {line: doc.size-1, ch: getLine(doc.size-1).text.length}; | |
1037 |
var ch = pos.ch, linelen = |
|
1338 | var ch = pos.ch, linelen = getLine(pos.line).text.length; | |
1038 | if (ch == null || ch > linelen) return {line: pos.line, ch: linelen}; |
|
1339 | if (ch == null || ch > linelen) return {line: pos.line, ch: linelen}; | |
1039 | else if (ch < 0) return {line: pos.line, ch: 0}; |
|
1340 | else if (ch < 0) return {line: pos.line, ch: 0}; | |
1040 | else return pos; |
|
1341 | else return pos; | |
1041 | } |
|
1342 | } | |
1042 |
|
1343 | |||
1043 |
function |
|
1344 | function findPosH(dir, unit) { | |
1044 | var linesPerPage = Math.floor(scroller.clientHeight / lineHeight()), head = sel.inverted ? sel.from : sel.to; |
|
1345 | var end = sel.inverted ? sel.from : sel.to, line = end.line, ch = end.ch; | |
1045 | setCursor(head.line + (Math.max(linesPerPage - 1, 1) * (down ? 1 : -1)), head.ch, true); |
|
1346 | var lineObj = getLine(line); | |
1046 | } |
|
1347 | function findNextLine() { | |
1047 | function scrollEnd(top) { |
|
1348 | for (var l = line + dir, e = dir < 0 ? -1 : doc.size; l != e; l += dir) { | |
1048 | var pos = top ? {line: 0, ch: 0} : {line: lines.length - 1, ch: lines[lines.length-1].text.length}; |
|
1349 | var lo = getLine(l); | |
1049 | setSelectionUser(pos, pos); |
|
1350 | if (!lo.hidden) { line = l; lineObj = lo; return true; } | |
|
1351 | } | |||
|
1352 | } | |||
|
1353 | function moveOnce(boundToLine) { | |||
|
1354 | if (ch == (dir < 0 ? 0 : lineObj.text.length)) { | |||
|
1355 | if (!boundToLine && findNextLine()) ch = dir < 0 ? lineObj.text.length : 0; | |||
|
1356 | else return false; | |||
|
1357 | } else ch += dir; | |||
|
1358 | return true; | |||
|
1359 | } | |||
|
1360 | if (unit == "char") moveOnce(); | |||
|
1361 | else if (unit == "column") moveOnce(true); | |||
|
1362 | else if (unit == "word") { | |||
|
1363 | var sawWord = false; | |||
|
1364 | for (;;) { | |||
|
1365 | if (dir < 0) if (!moveOnce()) break; | |||
|
1366 | if (isWordChar(lineObj.text.charAt(ch))) sawWord = true; | |||
|
1367 | else if (sawWord) {if (dir < 0) {dir = 1; moveOnce();} break;} | |||
|
1368 | if (dir > 0) if (!moveOnce()) break; | |||
|
1369 | } | |||
|
1370 | } | |||
|
1371 | return {line: line, ch: ch}; | |||
1050 | } |
|
1372 | } | |
1051 |
function |
|
1373 | function moveH(dir, unit) { | |
1052 | var endLine = lines.length - 1; |
|
1374 | var pos = dir < 0 ? sel.from : sel.to; | |
1053 | setSelection({line: 0, ch: 0}, {line: endLine, ch: lines[endLine].text.length}); |
|
1375 | if (shiftSelecting || posEq(sel.from, sel.to)) pos = findPosH(dir, unit); | |
|
1376 | setCursor(pos.line, pos.ch, true); | |||
|
1377 | } | |||
|
1378 | function deleteH(dir, unit) { | |||
|
1379 | if (!posEq(sel.from, sel.to)) replaceRange("", sel.from, sel.to); | |||
|
1380 | else if (dir < 0) replaceRange("", findPosH(dir, unit), sel.to); | |||
|
1381 | else replaceRange("", sel.from, findPosH(dir, unit)); | |||
|
1382 | userSelChange = true; | |||
1054 | } |
|
1383 | } | |
|
1384 | var goalColumn = null; | |||
|
1385 | function moveV(dir, unit) { | |||
|
1386 | var dist = 0, pos = localCoords(sel.inverted ? sel.from : sel.to, true); | |||
|
1387 | if (goalColumn != null) pos.x = goalColumn; | |||
|
1388 | if (unit == "page") dist = Math.min(scroller.clientHeight, window.innerHeight || document.documentElement.clientHeight); | |||
|
1389 | else if (unit == "line") dist = textHeight(); | |||
|
1390 | var target = coordsChar(pos.x, pos.y + dist * dir + 2); | |||
|
1391 | if (unit == "page") scrollbar.scrollTop += localCoords(target, true).y - pos.y; | |||
|
1392 | setCursor(target.line, target.ch, true); | |||
|
1393 | goalColumn = pos.x; | |||
|
1394 | } | |||
|
1395 | ||||
1055 | function selectWordAt(pos) { |
|
1396 | function selectWordAt(pos) { | |
1056 |
var line = |
|
1397 | var line = getLine(pos.line).text; | |
1057 | var start = pos.ch, end = pos.ch; |
|
1398 | var start = pos.ch, end = pos.ch; | |
1058 |
while (start > 0 && |
|
1399 | while (start > 0 && isWordChar(line.charAt(start - 1))) --start; | |
1059 |
while (end < line.length && |
|
1400 | while (end < line.length && isWordChar(line.charAt(end))) ++end; | |
1060 | setSelectionUser({line: pos.line, ch: start}, {line: pos.line, ch: end}); |
|
1401 | setSelectionUser({line: pos.line, ch: start}, {line: pos.line, ch: end}); | |
1061 | } |
|
1402 | } | |
1062 | function selectLine(line) { |
|
1403 | function selectLine(line) { | |
1063 |
setSelectionUser({line: line, ch: 0}, {line: line, ch: |
|
1404 | setSelectionUser({line: line, ch: 0}, clipPos({line: line + 1, ch: 0})); | |
1064 | } |
|
|||
1065 | function handleEnter() { |
|
|||
1066 | replaceSelection("\n", "end"); |
|
|||
1067 | if (options.enterMode != "flat") |
|
|||
1068 | indentLine(sel.from.line, options.enterMode == "keep" ? "prev" : "smart"); |
|
|||
1069 | } |
|
1405 | } | |
1070 |
function |
|
1406 | function indentSelected(mode) { | |
1071 | function indentSelected(mode) { |
|
1407 | if (posEq(sel.from, sel.to)) return indentLine(sel.from.line, mode); | |
1072 | if (posEq(sel.from, sel.to)) return indentLine(sel.from.line, mode); |
|
1408 | var e = sel.to.line - (sel.to.ch ? 0 : 1); | |
1073 | var e = sel.to.line - (sel.to.ch ? 0 : 1); |
|
1409 | for (var i = sel.from.line; i <= e; ++i) indentLine(i, mode); | |
1074 | for (var i = sel.from.line; i <= e; ++i) indentLine(i, mode); |
|
|||
1075 | } |
|
|||
1076 | shiftSelecting = null; |
|
|||
1077 | switch (options.tabMode) { |
|
|||
1078 | case "default": |
|
|||
1079 | return false; |
|
|||
1080 | case "indent": |
|
|||
1081 | indentSelected("smart"); |
|
|||
1082 | break; |
|
|||
1083 | case "classic": |
|
|||
1084 | if (posEq(sel.from, sel.to)) { |
|
|||
1085 | if (shift) indentLine(sel.from.line, "smart"); |
|
|||
1086 | else replaceSelection("\t", "end"); |
|
|||
1087 | break; |
|
|||
1088 | } |
|
|||
1089 | case "shift": |
|
|||
1090 | indentSelected(shift ? "subtract" : "add"); |
|
|||
1091 | break; |
|
|||
1092 | } |
|
|||
1093 | return true; |
|
|||
1094 | } |
|
|||
1095 | function smartHome() { |
|
|||
1096 | var firstNonWS = Math.max(0, lines[sel.from.line].text.search(/\S/)); |
|
|||
1097 | setCursor(sel.from.line, sel.from.ch <= firstNonWS && sel.from.ch ? 0 : firstNonWS, true); |
|
|||
1098 | } |
|
1410 | } | |
1099 |
|
1411 | |||
1100 | function indentLine(n, how) { |
|
1412 | function indentLine(n, how) { | |
|
1413 | if (!how) how = "add"; | |||
1101 | if (how == "smart") { |
|
1414 | if (how == "smart") { | |
1102 | if (!mode.indent) how = "prev"; |
|
1415 | if (!mode.indent) how = "prev"; | |
1103 | else var state = getStateBefore(n); |
|
1416 | else var state = getStateBefore(n); | |
1104 | } |
|
1417 | } | |
1105 |
|
1418 | |||
1106 | var line = lines[n], curSpace = line.indentation(), curSpaceString = line.text.match(/^\s*/)[0], indentation; |
|
1419 | var line = getLine(n), curSpace = line.indentation(options.tabSize), | |
|
1420 | curSpaceString = line.text.match(/^\s*/)[0], indentation; | |||
|
1421 | if (how == "smart") { | |||
|
1422 | indentation = mode.indent(state, line.text.slice(curSpaceString.length), line.text); | |||
|
1423 | if (indentation == Pass) how = "prev"; | |||
|
1424 | } | |||
1107 | if (how == "prev") { |
|
1425 | if (how == "prev") { | |
1108 |
if (n) indentation = |
|
1426 | if (n) indentation = getLine(n-1).indentation(options.tabSize); | |
1109 | else indentation = 0; |
|
1427 | else indentation = 0; | |
1110 | } |
|
1428 | } | |
1111 | else if (how == "smart") indentation = mode.indent(state, line.text.slice(curSpaceString.length)); |
|
|||
1112 | else if (how == "add") indentation = curSpace + options.indentUnit; |
|
1429 | else if (how == "add") indentation = curSpace + options.indentUnit; | |
1113 | else if (how == "subtract") indentation = curSpace - options.indentUnit; |
|
1430 | else if (how == "subtract") indentation = curSpace - options.indentUnit; | |
1114 | indentation = Math.max(0, indentation); |
|
1431 | indentation = Math.max(0, indentation); | |
@@ -1117,11 +1434,10 b' var CodeMirror = (function() {' | |||||
1117 | if (!diff) { |
|
1434 | if (!diff) { | |
1118 | if (sel.from.line != n && sel.to.line != n) return; |
|
1435 | if (sel.from.line != n && sel.to.line != n) return; | |
1119 | var indentString = curSpaceString; |
|
1436 | var indentString = curSpaceString; | |
1120 | } |
|
1437 | } else { | |
1121 | else { |
|
|||
1122 | var indentString = "", pos = 0; |
|
1438 | var indentString = "", pos = 0; | |
1123 | if (options.indentWithTabs) |
|
1439 | if (options.indentWithTabs) | |
1124 | for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";} |
|
1440 | for (var i = Math.floor(indentation / options.tabSize); i; --i) {pos += options.tabSize; indentString += "\t";} | |
1125 | while (pos < indentation) {++pos; indentString += " ";} |
|
1441 | while (pos < indentation) {++pos; indentString += " ";} | |
1126 | } |
|
1442 | } | |
1127 |
|
1443 | |||
@@ -1130,8 +1446,7 b' var CodeMirror = (function() {' | |||||
1130 |
|
1446 | |||
1131 | function loadMode() { |
|
1447 | function loadMode() { | |
1132 | mode = CodeMirror.getMode(options, options.mode); |
|
1448 | mode = CodeMirror.getMode(options, options.mode); | |
1133 | for (var i = 0, l = lines.length; i < l; ++i) |
|
1449 | doc.iter(0, doc.size, function(line) { line.stateAfter = null; }); | |
1134 | lines[i].stateAfter = null; |
|
|||
1135 | work = [0]; |
|
1450 | work = [0]; | |
1136 | startWorker(); |
|
1451 | startWorker(); | |
1137 | } |
|
1452 | } | |
@@ -1141,35 +1456,56 b' var CodeMirror = (function() {' | |||||
1141 | if (visible) gutterDirty = true; |
|
1456 | if (visible) gutterDirty = true; | |
1142 | else lineDiv.parentNode.style.marginLeft = 0; |
|
1457 | else lineDiv.parentNode.style.marginLeft = 0; | |
1143 | } |
|
1458 | } | |
1144 |
|
1459 | function wrappingChanged(from, to) { | ||
1145 | function markText(from, to, className) { |
|
1460 | if (options.lineWrapping) { | |
1146 | from = clipPos(from); to = clipPos(to); |
|
1461 | wrapper.className += " CodeMirror-wrap"; | |
1147 | var set = []; |
|
1462 | var perLine = scroller.clientWidth / charWidth() - 3; | |
1148 | function add(line, from, to, className) { |
|
1463 | doc.iter(0, doc.size, function(line) { | |
1149 | mark = lines[line].addMark(from, to, className, set); |
|
1464 | if (line.hidden) return; | |
|
1465 | var guess = Math.ceil(line.text.length / perLine) || 1; | |||
|
1466 | if (guess != 1) updateLineHeight(line, guess); | |||
|
1467 | }); | |||
|
1468 | lineSpace.style.width = code.style.width = ""; | |||
|
1469 | widthForcer.style.left = ""; | |||
|
1470 | } else { | |||
|
1471 | wrapper.className = wrapper.className.replace(" CodeMirror-wrap", ""); | |||
|
1472 | maxLine = ""; maxLineChanged = true; | |||
|
1473 | doc.iter(0, doc.size, function(line) { | |||
|
1474 | if (line.height != 1 && !line.hidden) updateLineHeight(line, 1); | |||
|
1475 | if (line.text.length > maxLine.length) maxLine = line.text; | |||
|
1476 | }); | |||
1150 | } |
|
1477 | } | |
1151 | if (from.line == to.line) add(from.line, from.ch, to.ch, className); |
|
1478 | changes.push({from: 0, to: doc.size}); | |
1152 | else { |
|
1479 | } | |
1153 | add(from.line, from.ch, null, className); |
|
1480 | function makeTab(col) { | |
1154 | for (var i = from.line + 1, e = to.line; i < e; ++i) |
|
1481 | var w = options.tabSize - col % options.tabSize, cached = tabCache[w]; | |
1155 | add(i, 0, null, className); |
|
1482 | if (cached) return cached; | |
1156 | add(to.line, 0, to.ch, className); |
|
1483 | for (var str = '<span class="cm-tab">', i = 0; i < w; ++i) str += " "; | |
1157 | } |
|
1484 | return (tabCache[w] = {html: str + "</span>", width: w}); | |
1158 | changes.push({from: from.line, to: to.line + 1}); |
|
1485 | } | |
1159 | return new TextMarker(set); |
|
1486 | function themeChanged() { | |
|
1487 | scroller.className = scroller.className.replace(/\s*cm-s-\S+/g, "") + | |||
|
1488 | options.theme.replace(/(^|\s)\s*/g, " cm-s-"); | |||
|
1489 | } | |||
|
1490 | function keyMapChanged() { | |||
|
1491 | var style = keyMap[options.keyMap].style; | |||
|
1492 | wrapper.className = wrapper.className.replace(/\s*cm-keymap-\S+/g, "") + | |||
|
1493 | (style ? " cm-keymap-" + style : ""); | |||
1160 | } |
|
1494 | } | |
1161 |
|
1495 | |||
1162 |
function TextMarker( |
|
1496 | function TextMarker() { this.set = []; } | |
1163 | TextMarker.prototype.clear = operation(function() { |
|
1497 | TextMarker.prototype.clear = operation(function() { | |
|
1498 | var min = Infinity, max = -Infinity; | |||
1164 | for (var i = 0, e = this.set.length; i < e; ++i) { |
|
1499 | for (var i = 0, e = this.set.length; i < e; ++i) { | |
1165 |
var |
|
1500 | var line = this.set[i], mk = line.marked; | |
1166 | for (var j = 0; j < mk.length; ++j) { |
|
1501 | if (!mk || !line.parent) continue; | |
1167 | if (mk[j].set == this.set) mk.splice(j--, 1); |
|
1502 | var lineN = lineNo(line); | |
1168 | } |
|
1503 | min = Math.min(min, lineN); max = Math.max(max, lineN); | |
|
1504 | for (var j = 0; j < mk.length; ++j) | |||
|
1505 | if (mk[j].marker == this) mk.splice(j--, 1); | |||
1169 | } |
|
1506 | } | |
1170 | // We don't know the exact lines that changed. Refreshing is |
|
1507 | if (min != Infinity) | |
1171 | // cheaper than finding them. |
|
1508 | changes.push({from: min, to: max + 1}); | |
1172 | changes.push({from: 0, to: lines.length}); |
|
|||
1173 | }); |
|
1509 | }); | |
1174 | TextMarker.prototype.find = function() { |
|
1510 | TextMarker.prototype.find = function() { | |
1175 | var from, to; |
|
1511 | var from, to; | |
@@ -1177,10 +1513,10 b' var CodeMirror = (function() {' | |||||
1177 | var line = this.set[i], mk = line.marked; |
|
1513 | var line = this.set[i], mk = line.marked; | |
1178 | for (var j = 0; j < mk.length; ++j) { |
|
1514 | for (var j = 0; j < mk.length; ++j) { | |
1179 | var mark = mk[j]; |
|
1515 | var mark = mk[j]; | |
1180 |
if (mark. |
|
1516 | if (mark.marker == this) { | |
1181 | if (mark.from != null || mark.to != null) { |
|
1517 | if (mark.from != null || mark.to != null) { | |
1182 |
var found = |
|
1518 | var found = lineNo(line); | |
1183 |
if (found |
|
1519 | if (found != null) { | |
1184 | if (mark.from != null) from = {line: found, ch: mark.from}; |
|
1520 | if (mark.from != null) from = {line: found, ch: mark.from}; | |
1185 | if (mark.to != null) to = {line: found, ch: mark.to}; |
|
1521 | if (mark.to != null) to = {line: found, ch: mark.to}; | |
1186 | } |
|
1522 | } | |
@@ -1191,45 +1527,113 b' var CodeMirror = (function() {' | |||||
1191 | return {from: from, to: to}; |
|
1527 | return {from: from, to: to}; | |
1192 | }; |
|
1528 | }; | |
1193 |
|
1529 | |||
|
1530 | function markText(from, to, className) { | |||
|
1531 | from = clipPos(from); to = clipPos(to); | |||
|
1532 | var tm = new TextMarker(); | |||
|
1533 | if (!posLess(from, to)) return tm; | |||
|
1534 | function add(line, from, to, className) { | |||
|
1535 | getLine(line).addMark(new MarkedText(from, to, className, tm)); | |||
|
1536 | } | |||
|
1537 | if (from.line == to.line) add(from.line, from.ch, to.ch, className); | |||
|
1538 | else { | |||
|
1539 | add(from.line, from.ch, null, className); | |||
|
1540 | for (var i = from.line + 1, e = to.line; i < e; ++i) | |||
|
1541 | add(i, null, null, className); | |||
|
1542 | add(to.line, null, to.ch, className); | |||
|
1543 | } | |||
|
1544 | changes.push({from: from.line, to: to.line + 1}); | |||
|
1545 | return tm; | |||
|
1546 | } | |||
|
1547 | ||||
|
1548 | function setBookmark(pos) { | |||
|
1549 | pos = clipPos(pos); | |||
|
1550 | var bm = new Bookmark(pos.ch); | |||
|
1551 | getLine(pos.line).addMark(bm); | |||
|
1552 | return bm; | |||
|
1553 | } | |||
|
1554 | ||||
|
1555 | function findMarksAt(pos) { | |||
|
1556 | pos = clipPos(pos); | |||
|
1557 | var markers = [], marked = getLine(pos.line).marked; | |||
|
1558 | if (!marked) return markers; | |||
|
1559 | for (var i = 0, e = marked.length; i < e; ++i) { | |||
|
1560 | var m = marked[i]; | |||
|
1561 | if ((m.from == null || m.from <= pos.ch) && | |||
|
1562 | (m.to == null || m.to >= pos.ch)) | |||
|
1563 | markers.push(m.marker || m); | |||
|
1564 | } | |||
|
1565 | return markers; | |||
|
1566 | } | |||
|
1567 | ||||
1194 | function addGutterMarker(line, text, className) { |
|
1568 | function addGutterMarker(line, text, className) { | |
1195 |
if (typeof line == "number") line = |
|
1569 | if (typeof line == "number") line = getLine(clipLine(line)); | |
1196 | line.gutterMarker = {text: text, style: className}; |
|
1570 | line.gutterMarker = {text: text, style: className}; | |
1197 | gutterDirty = true; |
|
1571 | gutterDirty = true; | |
1198 | return line; |
|
1572 | return line; | |
1199 | } |
|
1573 | } | |
1200 | function removeGutterMarker(line) { |
|
1574 | function removeGutterMarker(line) { | |
1201 |
if (typeof line == "number") line = |
|
1575 | if (typeof line == "number") line = getLine(clipLine(line)); | |
1202 | line.gutterMarker = null; |
|
1576 | line.gutterMarker = null; | |
1203 | gutterDirty = true; |
|
1577 | gutterDirty = true; | |
1204 | } |
|
1578 | } | |
1205 | function setLineClass(line, className) { |
|
1579 | ||
1206 | if (typeof line == "number") { |
|
1580 | function changeLine(handle, op) { | |
1207 |
|
|
1581 | var no = handle, line = handle; | |
1208 | line = lines[clipLine(line)]; |
|
1582 | if (typeof handle == "number") line = getLine(clipLine(handle)); | |
1209 | } |
|
1583 | else no = lineNo(handle); | |
1210 | else { |
|
1584 | if (no == null) return null; | |
1211 | var no = indexOf(lines, line); |
|
1585 | if (op(line, no)) changes.push({from: no, to: no + 1}); | |
1212 |
|
|
1586 | else return null; | |
1213 | } |
|
|||
1214 | if (line.className != className) { |
|
|||
1215 | line.className = className; |
|
|||
1216 | changes.push({from: no, to: no + 1}); |
|
|||
1217 | } |
|
|||
1218 | return line; |
|
1587 | return line; | |
1219 | } |
|
1588 | } | |
|
1589 | function setLineClass(handle, className, bgClassName) { | |||
|
1590 | return changeLine(handle, function(line) { | |||
|
1591 | if (line.className != className || line.bgClassName != bgClassName) { | |||
|
1592 | line.className = className; | |||
|
1593 | line.bgClassName = bgClassName; | |||
|
1594 | return true; | |||
|
1595 | } | |||
|
1596 | }); | |||
|
1597 | } | |||
|
1598 | function setLineHidden(handle, hidden) { | |||
|
1599 | return changeLine(handle, function(line, no) { | |||
|
1600 | if (line.hidden != hidden) { | |||
|
1601 | line.hidden = hidden; | |||
|
1602 | if (!options.lineWrapping) { | |||
|
1603 | var l = line.text; | |||
|
1604 | if (hidden && l.length == maxLine.length) { | |||
|
1605 | updateMaxLine = true; | |||
|
1606 | } else if (!hidden && l.length > maxLine.length) { | |||
|
1607 | maxLine = l; maxWidth = null; updateMaxLine = false; | |||
|
1608 | } | |||
|
1609 | } | |||
|
1610 | updateLineHeight(line, hidden ? 0 : 1); | |||
|
1611 | var fline = sel.from.line, tline = sel.to.line; | |||
|
1612 | if (hidden && (fline == no || tline == no)) { | |||
|
1613 | var from = fline == no ? skipHidden({line: fline, ch: 0}, fline, 0) : sel.from; | |||
|
1614 | var to = tline == no ? skipHidden({line: tline, ch: 0}, tline, 0) : sel.to; | |||
|
1615 | // Can't hide the last visible line, we'd have no place to put the cursor | |||
|
1616 | if (!to) return; | |||
|
1617 | setSelection(from, to); | |||
|
1618 | } | |||
|
1619 | return (gutterDirty = true); | |||
|
1620 | } | |||
|
1621 | }); | |||
|
1622 | } | |||
1220 |
|
1623 | |||
1221 | function lineInfo(line) { |
|
1624 | function lineInfo(line) { | |
1222 | if (typeof line == "number") { |
|
1625 | if (typeof line == "number") { | |
|
1626 | if (!isLine(line)) return null; | |||
1223 | var n = line; |
|
1627 | var n = line; | |
1224 |
line = |
|
1628 | line = getLine(line); | |
1225 | if (!line) return null; |
|
1629 | if (!line) return null; | |
1226 | } |
|
1630 | } else { | |
1227 | else { |
|
1631 | var n = lineNo(line); | |
1228 | var n = indexOf(lines, line); |
|
1632 | if (n == null) return null; | |
1229 | if (n == -1) return null; |
|
|||
1230 | } |
|
1633 | } | |
1231 | var marker = line.gutterMarker; |
|
1634 | var marker = line.gutterMarker; | |
1232 |
return {line: n, text: line.text, markerText: marker && marker.text, |
|
1635 | return {line: n, handle: line, text: line.text, markerText: marker && marker.text, | |
|
1636 | markerClass: marker && marker.style, lineClass: line.className, bgClass: line.bgClassName}; | |||
1233 | } |
|
1637 | } | |
1234 |
|
1638 | |||
1235 | function stringWidth(str) { |
|
1639 | function stringWidth(str) { | |
@@ -1239,21 +1643,15 b' var CodeMirror = (function() {' | |||||
1239 | } |
|
1643 | } | |
1240 | // These are used to go from pixel positions to character |
|
1644 | // These are used to go from pixel positions to character | |
1241 | // positions, taking varying character widths into account. |
|
1645 | // positions, taking varying character widths into account. | |
1242 | function charX(line, pos) { |
|
|||
1243 | if (pos == 0) return 0; |
|
|||
1244 | measure.innerHTML = "<pre><span>" + lines[line].getHTML(null, null, false, pos) + "</span></pre>"; |
|
|||
1245 | return measure.firstChild.firstChild.offsetWidth; |
|
|||
1246 | } |
|
|||
1247 | function charFromX(line, x) { |
|
1646 | function charFromX(line, x) { | |
1248 | if (x <= 0) return 0; |
|
1647 | if (x <= 0) return 0; | |
1249 |
var lineObj = |
|
1648 | var lineObj = getLine(line), text = lineObj.text; | |
1250 | function getX(len) { |
|
1649 | function getX(len) { | |
1251 | measure.innerHTML = "<pre><span>" + lineObj.getHTML(null, null, false, len) + "</span></pre>"; |
|
1650 | return measureLine(lineObj, len).left; | |
1252 | return measure.firstChild.firstChild.offsetWidth; |
|
|||
1253 | } |
|
1651 | } | |
1254 | var from = 0, fromX = 0, to = text.length, toX; |
|
1652 | var from = 0, fromX = 0, to = text.length, toX; | |
1255 | // Guess a suitable upper bound for our search. |
|
1653 | // Guess a suitable upper bound for our search. | |
1256 |
var estimated = Math.min(to, Math.ceil(x / |
|
1654 | var estimated = Math.min(to, Math.ceil(x / charWidth())); | |
1257 | for (;;) { |
|
1655 | for (;;) { | |
1258 | var estX = getX(estimated); |
|
1656 | var estX = getX(estimated); | |
1259 | if (estX <= x && estimated < to) estimated = Math.min(to, Math.ceil(estimated * 1.2)); |
|
1657 | if (estX <= x && estimated < to) estimated = Math.min(to, Math.ceil(estimated * 1.2)); | |
@@ -1272,20 +1670,95 b' var CodeMirror = (function() {' | |||||
1272 | } |
|
1670 | } | |
1273 | } |
|
1671 | } | |
1274 |
|
1672 | |||
|
1673 | var tempId = "CodeMirror-temp-" + Math.floor(Math.random() * 0xffffff).toString(16); | |||
|
1674 | function measureLine(line, ch) { | |||
|
1675 | if (ch == 0) return {top: 0, left: 0}; | |||
|
1676 | var wbr = options.lineWrapping && ch < line.text.length && | |||
|
1677 | spanAffectsWrapping.test(line.text.slice(ch - 1, ch + 1)); | |||
|
1678 | measure.innerHTML = "<pre>" + line.getHTML(makeTab, ch, tempId, wbr) + "</pre>"; | |||
|
1679 | var elt = document.getElementById(tempId); | |||
|
1680 | var top = elt.offsetTop, left = elt.offsetLeft; | |||
|
1681 | // Older IEs report zero offsets for spans directly after a wrap | |||
|
1682 | if (ie && top == 0 && left == 0) { | |||
|
1683 | var backup = document.createElement("span"); | |||
|
1684 | backup.innerHTML = "x"; | |||
|
1685 | elt.parentNode.insertBefore(backup, elt.nextSibling); | |||
|
1686 | top = backup.offsetTop; | |||
|
1687 | } | |||
|
1688 | return {top: top, left: left}; | |||
|
1689 | } | |||
1275 | function localCoords(pos, inLineWrap) { |
|
1690 | function localCoords(pos, inLineWrap) { | |
1276 |
var lh = |
|
1691 | var x, lh = textHeight(), y = lh * (heightAtLine(doc, pos.line) - (inLineWrap ? displayOffset : 0)); | |
1277 | return {x: charX(pos.line, pos.ch), y: line * lh, yBot: (line + 1) * lh}; |
|
1692 | if (pos.ch == 0) x = 0; | |
|
1693 | else { | |||
|
1694 | var sp = measureLine(getLine(pos.line), pos.ch); | |||
|
1695 | x = sp.left; | |||
|
1696 | if (options.lineWrapping) y += Math.max(0, sp.top); | |||
|
1697 | } | |||
|
1698 | return {x: x, y: y, yBot: y + lh}; | |||
|
1699 | } | |||
|
1700 | // Coords must be lineSpace-local | |||
|
1701 | function coordsChar(x, y) { | |||
|
1702 | if (y < 0) y = 0; | |||
|
1703 | var th = textHeight(), cw = charWidth(), heightPos = displayOffset + Math.floor(y / th); | |||
|
1704 | var lineNo = lineAtHeight(doc, heightPos); | |||
|
1705 | if (lineNo >= doc.size) return {line: doc.size - 1, ch: getLine(doc.size - 1).text.length}; | |||
|
1706 | var lineObj = getLine(lineNo), text = lineObj.text; | |||
|
1707 | var tw = options.lineWrapping, innerOff = tw ? heightPos - heightAtLine(doc, lineNo) : 0; | |||
|
1708 | if (x <= 0 && innerOff == 0) return {line: lineNo, ch: 0}; | |||
|
1709 | function getX(len) { | |||
|
1710 | var sp = measureLine(lineObj, len); | |||
|
1711 | if (tw) { | |||
|
1712 | var off = Math.round(sp.top / th); | |||
|
1713 | return Math.max(0, sp.left + (off - innerOff) * scroller.clientWidth); | |||
|
1714 | } | |||
|
1715 | return sp.left; | |||
|
1716 | } | |||
|
1717 | var from = 0, fromX = 0, to = text.length, toX; | |||
|
1718 | // Guess a suitable upper bound for our search. | |||
|
1719 | var estimated = Math.min(to, Math.ceil((x + innerOff * scroller.clientWidth * .9) / cw)); | |||
|
1720 | for (;;) { | |||
|
1721 | var estX = getX(estimated); | |||
|
1722 | if (estX <= x && estimated < to) estimated = Math.min(to, Math.ceil(estimated * 1.2)); | |||
|
1723 | else {toX = estX; to = estimated; break;} | |||
|
1724 | } | |||
|
1725 | if (x > toX) return {line: lineNo, ch: to}; | |||
|
1726 | // Try to guess a suitable lower bound as well. | |||
|
1727 | estimated = Math.floor(to * 0.8); estX = getX(estimated); | |||
|
1728 | if (estX < x) {from = estimated; fromX = estX;} | |||
|
1729 | // Do a binary search between these bounds. | |||
|
1730 | for (;;) { | |||
|
1731 | if (to - from <= 1) return {line: lineNo, ch: (toX - x > x - fromX) ? from : to}; | |||
|
1732 | var middle = Math.ceil((from + to) / 2), middleX = getX(middle); | |||
|
1733 | if (middleX > x) {to = middle; toX = middleX;} | |||
|
1734 | else {from = middle; fromX = middleX;} | |||
|
1735 | } | |||
1278 | } |
|
1736 | } | |
1279 | function pageCoords(pos) { |
|
1737 | function pageCoords(pos) { | |
1280 | var local = localCoords(pos, true), off = eltOffset(lineSpace); |
|
1738 | var local = localCoords(pos, true), off = eltOffset(lineSpace); | |
1281 | return {x: off.left + local.x, y: off.top + local.y, yBot: off.top + local.yBot}; |
|
1739 | return {x: off.left + local.x, y: off.top + local.y, yBot: off.top + local.yBot}; | |
1282 | } |
|
1740 | } | |
1283 |
|
1741 | |||
1284 | function lineHeight() { |
|
1742 | var cachedHeight, cachedHeightFor, measureText; | |
1285 | var nlines = lineDiv.childNodes.length; |
|
1743 | function textHeight() { | |
1286 | if (nlines) return (lineDiv.offsetHeight / nlines) || 1; |
|
1744 | if (measureText == null) { | |
1287 |
measure |
|
1745 | measureText = "<pre>"; | |
1288 | return measure.firstChild.offsetHeight || 1; |
|
1746 | for (var i = 0; i < 49; ++i) measureText += "x<br/>"; | |
|
1747 | measureText += "x</pre>"; | |||
|
1748 | } | |||
|
1749 | var offsetHeight = lineDiv.clientHeight; | |||
|
1750 | if (offsetHeight == cachedHeightFor) return cachedHeight; | |||
|
1751 | cachedHeightFor = offsetHeight; | |||
|
1752 | measure.innerHTML = measureText; | |||
|
1753 | cachedHeight = measure.firstChild.offsetHeight / 50 || 1; | |||
|
1754 | measure.innerHTML = ""; | |||
|
1755 | return cachedHeight; | |||
|
1756 | } | |||
|
1757 | var cachedWidth, cachedWidthFor = 0; | |||
|
1758 | function charWidth() { | |||
|
1759 | if (scroller.clientWidth == cachedWidthFor) return cachedWidth; | |||
|
1760 | cachedWidthFor = scroller.clientWidth; | |||
|
1761 | return (cachedWidth = stringWidth("x")); | |||
1289 | } |
|
1762 | } | |
1290 | function paddingTop() {return lineSpace.offsetTop;} |
|
1763 | function paddingTop() {return lineSpace.offsetTop;} | |
1291 | function paddingLeft() {return lineSpace.offsetLeft;} |
|
1764 | function paddingLeft() {return lineSpace.offsetLeft;} | |
@@ -1300,12 +1773,11 b' var CodeMirror = (function() {' | |||||
1300 | if (!liberal && (x - offW.left > scroller.clientWidth || y - offW.top > scroller.clientHeight)) |
|
1773 | if (!liberal && (x - offW.left > scroller.clientWidth || y - offW.top > scroller.clientHeight)) | |
1301 | return null; |
|
1774 | return null; | |
1302 | var offL = eltOffset(lineSpace, true); |
|
1775 | var offL = eltOffset(lineSpace, true); | |
1303 | var line = showingFrom + Math.floor((y - offL.top) / lineHeight()); |
|
1776 | return coordsChar(x - offL.left, y - offL.top); | |
1304 | return clipPos({line: line, ch: charFromX(clipLine(line), x - offL.left)}); |
|
|||
1305 | } |
|
1777 | } | |
1306 | function onContextMenu(e) { |
|
1778 | function onContextMenu(e) { | |
1307 | var pos = posFromMouse(e); |
|
1779 | var pos = posFromMouse(e), scrollPos = scrollbar.scrollTop; | |
1308 |
if (!pos || |
|
1780 | if (!pos || opera) return; // Opera is difficult. | |
1309 | if (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to)) |
|
1781 | if (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to)) | |
1310 | operation(setCursor)(pos.line, pos.ch); |
|
1782 | operation(setCursor)(pos.line, pos.ch); | |
1311 |
|
1783 | |||
@@ -1317,14 +1789,15 b' var CodeMirror = (function() {' | |||||
1317 | leaveInputAlone = true; |
|
1789 | leaveInputAlone = true; | |
1318 | var val = input.value = getSelection(); |
|
1790 | var val = input.value = getSelection(); | |
1319 | focusInput(); |
|
1791 | focusInput(); | |
1320 | setSelRange(input, 0, input.value.length); |
|
1792 | selectInput(input); | |
1321 | function rehide() { |
|
1793 | function rehide() { | |
1322 | var newVal = splitLines(input.value).join("\n"); |
|
1794 | var newVal = splitLines(input.value).join("\n"); | |
1323 | if (newVal != val) operation(replaceSelection)(newVal, "end"); |
|
1795 | if (newVal != val && !options.readOnly) operation(replaceSelection)(newVal, "end"); | |
1324 | inputDiv.style.position = "relative"; |
|
1796 | inputDiv.style.position = "relative"; | |
1325 | input.style.cssText = oldCSS; |
|
1797 | input.style.cssText = oldCSS; | |
|
1798 | if (ie_lt9) scrollbar.scrollTop = scrollPos; | |||
1326 | leaveInputAlone = false; |
|
1799 | leaveInputAlone = false; | |
1327 |
|
|
1800 | resetInput(true); | |
1328 | slowPoll(); |
|
1801 | slowPoll(); | |
1329 | } |
|
1802 | } | |
1330 |
|
1803 | |||
@@ -1334,8 +1807,7 b' var CodeMirror = (function() {' | |||||
1334 | mouseup(); |
|
1807 | mouseup(); | |
1335 | setTimeout(rehide, 20); |
|
1808 | setTimeout(rehide, 20); | |
1336 | }, true); |
|
1809 | }, true); | |
1337 | } |
|
1810 | } else { | |
1338 | else { |
|
|||
1339 | setTimeout(rehide, 50); |
|
1811 | setTimeout(rehide, 50); | |
1340 | } |
|
1812 | } | |
1341 | } |
|
1813 | } | |
@@ -1352,7 +1824,7 b' var CodeMirror = (function() {' | |||||
1352 |
|
1824 | |||
1353 | var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; |
|
1825 | var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; | |
1354 | function matchBrackets(autoclear) { |
|
1826 | function matchBrackets(autoclear) { | |
1355 |
var head = sel.inverted ? sel.from : sel.to, line = |
|
1827 | var head = sel.inverted ? sel.from : sel.to, line = getLine(head.line), pos = head.ch - 1; | |
1356 | var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; |
|
1828 | var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; | |
1357 | if (!match) return; |
|
1829 | if (!match) return; | |
1358 | var ch = match.charAt(0), forward = match.charAt(1) == ">", d = forward ? 1 : -1, st = line.styles; |
|
1830 | var ch = match.charAt(0), forward = match.charAt(1) == ">", d = forward ? 1 : -1, st = line.styles; | |
@@ -1365,7 +1837,7 b' var CodeMirror = (function() {' | |||||
1365 | var st = line.styles, pos = forward ? 0 : line.text.length - 1, cur; |
|
1837 | var st = line.styles, pos = forward ? 0 : line.text.length - 1, cur; | |
1366 | for (var i = forward ? 0 : st.length - 2, e = forward ? st.length : -2; i != e; i += 2*d) { |
|
1838 | for (var i = forward ? 0 : st.length - 2, e = forward ? st.length : -2; i != e; i += 2*d) { | |
1367 | var text = st[i]; |
|
1839 | var text = st[i]; | |
1368 |
if ( |
|
1840 | if (st[i+1] != style) {pos += d * text.length; continue;} | |
1369 | for (var j = forward ? 0 : text.length - 1, te = forward ? text.length : -1; j != te; j += d, pos+=d) { |
|
1841 | for (var j = forward ? 0 : text.length - 1, te = forward ? text.length : -1; j != te; j += d, pos+=d) { | |
1370 | if (pos >= from && pos < to && re.test(cur = text.charAt(j))) { |
|
1842 | if (pos >= from && pos < to && re.test(cur = text.charAt(j))) { | |
1371 | var match = matching[cur]; |
|
1843 | var match = matching[cur]; | |
@@ -1376,8 +1848,8 b' var CodeMirror = (function() {' | |||||
1376 | } |
|
1848 | } | |
1377 | } |
|
1849 | } | |
1378 | } |
|
1850 | } | |
1379 |
for (var i = head.line, e = forward ? Math.min(i + 100, |
|
1851 | for (var i = head.line, e = forward ? Math.min(i + 100, doc.size) : Math.max(-1, i - 100); i != e; i+=d) { | |
1380 |
var line = |
|
1852 | var line = getLine(i), first = i == head.line; | |
1381 | var found = scan(line, first && forward ? pos + 1 : 0, first && !forward ? pos : line.text.length); |
|
1853 | var found = scan(line, first && forward ? pos + 1 : 0, first && !forward ? pos : line.text.length); | |
1382 | if (found) break; |
|
1854 | if (found) break; | |
1383 | } |
|
1855 | } | |
@@ -1399,9 +1871,9 b' var CodeMirror = (function() {' | |||||
1399 | var minindent, minline; |
|
1871 | var minindent, minline; | |
1400 | for (var search = n, lim = n - 40; search > lim; --search) { |
|
1872 | for (var search = n, lim = n - 40; search > lim; --search) { | |
1401 | if (search == 0) return 0; |
|
1873 | if (search == 0) return 0; | |
1402 |
var line = |
|
1874 | var line = getLine(search-1); | |
1403 | if (line.stateAfter) return search; |
|
1875 | if (line.stateAfter) return search; | |
1404 | var indented = line.indentation(); |
|
1876 | var indented = line.indentation(options.tabSize); | |
1405 | if (minline == null || minindent > indented) { |
|
1877 | if (minline == null || minindent > indented) { | |
1406 | minline = search - 1; |
|
1878 | minline = search - 1; | |
1407 | minindent = indented; |
|
1879 | minindent = indented; | |
@@ -1410,56 +1882,62 b' var CodeMirror = (function() {' | |||||
1410 | return minline; |
|
1882 | return minline; | |
1411 | } |
|
1883 | } | |
1412 | function getStateBefore(n) { |
|
1884 | function getStateBefore(n) { | |
1413 |
var start = findStartLine(n), state = start && |
|
1885 | var start = findStartLine(n), state = start && getLine(start-1).stateAfter; | |
1414 | if (!state) state = startState(mode); |
|
1886 | if (!state) state = startState(mode); | |
1415 | else state = copyState(mode, state); |
|
1887 | else state = copyState(mode, state); | |
1416 | for (var i = start; i < n; ++i) { |
|
1888 | doc.iter(start, n, function(line) { | |
1417 | var line = lines[i]; |
|
1889 | line.highlight(mode, state, options.tabSize); | |
1418 | line.highlight(mode, state); |
|
|||
1419 | line.stateAfter = copyState(mode, state); |
|
1890 | line.stateAfter = copyState(mode, state); | |
1420 | } |
|
1891 | }); | |
1421 | changes.push({from: start, to: n}); |
|
1892 | if (start < n) changes.push({from: start, to: n}); | |
1422 |
if (n < |
|
1893 | if (n < doc.size && !getLine(n).stateAfter) work.push(n); | |
1423 | return state; |
|
1894 | return state; | |
1424 | } |
|
1895 | } | |
1425 | function highlightLines(start, end) { |
|
1896 | function highlightLines(start, end) { | |
1426 | var state = getStateBefore(start); |
|
1897 | var state = getStateBefore(start); | |
1427 | for (var i = start; i < end; ++i) { |
|
1898 | doc.iter(start, end, function(line) { | |
1428 | var line = lines[i]; |
|
1899 | line.highlight(mode, state, options.tabSize); | |
1429 | line.highlight(mode, state); |
|
|||
1430 | line.stateAfter = copyState(mode, state); |
|
1900 | line.stateAfter = copyState(mode, state); | |
1431 | } |
|
1901 | }); | |
1432 | } |
|
1902 | } | |
1433 | function highlightWorker() { |
|
1903 | function highlightWorker() { | |
1434 | var end = +new Date + options.workTime; |
|
1904 | var end = +new Date + options.workTime; | |
1435 | var foundWork = work.length; |
|
1905 | var foundWork = work.length; | |
1436 | while (work.length) { |
|
1906 | while (work.length) { | |
1437 |
if (! |
|
1907 | if (!getLine(showingFrom).stateAfter) var task = showingFrom; | |
1438 | else var task = work.pop(); |
|
1908 | else var task = work.pop(); | |
1439 |
if (task >= |
|
1909 | if (task >= doc.size) continue; | |
1440 |
var start = findStartLine(task), state = start && |
|
1910 | var start = findStartLine(task), state = start && getLine(start-1).stateAfter; | |
1441 | if (state) state = copyState(mode, state); |
|
1911 | if (state) state = copyState(mode, state); | |
1442 | else state = startState(mode); |
|
1912 | else state = startState(mode); | |
1443 |
|
1913 | |||
1444 |
var unchanged = 0, compare = mode.compareStates, realChange = false |
|
1914 | var unchanged = 0, compare = mode.compareStates, realChange = false, | |
1445 | for (var i = start, l = lines.length; i < l; ++i) { |
|
1915 | i = start, bail = false; | |
1446 | var line = lines[i], hadState = line.stateAfter; |
|
1916 | doc.iter(i, doc.size, function(line) { | |
|
1917 | var hadState = line.stateAfter; | |||
1447 | if (+new Date > end) { |
|
1918 | if (+new Date > end) { | |
1448 | work.push(i); |
|
1919 | work.push(i); | |
1449 | startWorker(options.workDelay); |
|
1920 | startWorker(options.workDelay); | |
1450 | if (realChange) changes.push({from: task, to: i + 1}); |
|
1921 | if (realChange) changes.push({from: task, to: i + 1}); | |
1451 | return; |
|
1922 | return (bail = true); | |
1452 | } |
|
1923 | } | |
1453 | var changed = line.highlight(mode, state); |
|
1924 | var changed = line.highlight(mode, state, options.tabSize); | |
1454 | if (changed) realChange = true; |
|
1925 | if (changed) realChange = true; | |
1455 | line.stateAfter = copyState(mode, state); |
|
1926 | line.stateAfter = copyState(mode, state); | |
|
1927 | var done = null; | |||
1456 | if (compare) { |
|
1928 | if (compare) { | |
1457 |
|
|
1929 | var same = hadState && compare(hadState, state); | |
1458 | } else { |
|
1930 | if (same != Pass) done = !!same; | |
|
1931 | } | |||
|
1932 | if (done == null) { | |||
1459 | if (changed !== false || !hadState) unchanged = 0; |
|
1933 | if (changed !== false || !hadState) unchanged = 0; | |
1460 | else if (++unchanged > 3) break; |
|
1934 | else if (++unchanged > 3 && (!mode.indent || mode.indent(hadState, "") == mode.indent(state, ""))) | |
|
1935 | done = true; | |||
1461 | } |
|
1936 | } | |
1462 | } |
|
1937 | if (done) return true; | |
|
1938 | ++i; | |||
|
1939 | }); | |||
|
1940 | if (bail) return; | |||
1463 | if (realChange) changes.push({from: task, to: i + 1}); |
|
1941 | if (realChange) changes.push({from: task, to: i + 1}); | |
1464 | } |
|
1942 | } | |
1465 | if (foundWork && options.onHighlightComplete) |
|
1943 | if (foundWork && options.onHighlightComplete) | |
@@ -1475,35 +1953,44 b' var CodeMirror = (function() {' | |||||
1475 | // be awkward, slow, and error-prone), but instead updates are |
|
1953 | // be awkward, slow, and error-prone), but instead updates are | |
1476 | // batched and then all combined and executed at once. |
|
1954 | // batched and then all combined and executed at once. | |
1477 | function startOperation() { |
|
1955 | function startOperation() { | |
1478 |
updateInput = |
|
1956 | updateInput = userSelChange = textChanged = null; | |
|
1957 | changes = []; selectionChanged = false; callbacks = []; | |||
1479 | } |
|
1958 | } | |
1480 | function endOperation() { |
|
1959 | function endOperation() { | |
1481 | var reScroll = false; |
|
1960 | if (updateMaxLine) computeMaxLength(); | |
1482 | if (selectionChanged) reScroll = !scrollCursorIntoView(); |
|
1961 | if (maxLineChanged && !options.lineWrapping) { | |
1483 | if (changes.length) updateDisplay(changes); |
|
1962 | widthForcer.style.left = stringWidth(maxLine) + "px"; | |
|
1963 | maxLineChanged = false; | |||
|
1964 | } | |||
|
1965 | var newScrollPos, updated; | |||
|
1966 | if (selectionChanged) { | |||
|
1967 | var coords = calculateCursorCoords(); | |||
|
1968 | newScrollPos = calculateScrollPos(coords.x, coords.y, coords.x, coords.yBot); | |||
|
1969 | } | |||
|
1970 | if (changes.length) updated = updateDisplay(changes, true, (newScrollPos ? newScrollPos.scrollTop : null)); | |||
1484 | else { |
|
1971 | else { | |
1485 |
if (selectionChanged) update |
|
1972 | if (selectionChanged) updateSelection(); | |
1486 | if (gutterDirty) updateGutter(); |
|
1973 | if (gutterDirty) updateGutter(); | |
1487 | } |
|
1974 | } | |
1488 |
if ( |
|
1975 | if (newScrollPos) scrollCursorIntoView(); | |
1489 | if (selectionChanged) {scrollEditorIntoView(); restartBlink();} |
|
1976 | if (selectionChanged) {scrollEditorIntoView(); restartBlink();} | |
1490 |
|
1977 | |||
1491 | // updateInput can be set to a boolean value to force/prevent an |
|
|||
1492 | // update. |
|
|||
1493 | if (focused && !leaveInputAlone && |
|
1978 | if (focused && !leaveInputAlone && | |
1494 | (updateInput === true || (updateInput !== false && selectionChanged))) |
|
1979 | (updateInput === true || (updateInput !== false && selectionChanged))) | |
1495 |
|
|
1980 | resetInput(userSelChange); | |
1496 |
|
1981 | |||
1497 | if (selectionChanged && options.matchBrackets) |
|
1982 | if (selectionChanged && options.matchBrackets) | |
1498 | setTimeout(operation(function() { |
|
1983 | setTimeout(operation(function() { | |
1499 | if (bracketHighlighted) {bracketHighlighted(); bracketHighlighted = null;} |
|
1984 | if (bracketHighlighted) {bracketHighlighted(); bracketHighlighted = null;} | |
1500 | matchBrackets(false); |
|
1985 | if (posEq(sel.from, sel.to)) matchBrackets(false); | |
1501 | }), 20); |
|
1986 | }), 20); | |
1502 |
var |
|
1987 | var sc = selectionChanged, cbs = callbacks; // these can be reset by callbacks | |
1503 |
if ( |
|
1988 | if (textChanged && options.onChange && instance) | |
|
1989 | options.onChange(instance, textChanged); | |||
|
1990 | if (sc && options.onCursorActivity) | |||
1504 | options.onCursorActivity(instance); |
|
1991 | options.onCursorActivity(instance); | |
1505 | if (tc && options.onChange && instance) |
|
1992 | for (var i = 0; i < cbs.length; ++i) cbs[i](instance); | |
1506 |
options.on |
|
1993 | if (updated && options.onUpdate) options.onUpdate(instance); | |
1507 | } |
|
1994 | } | |
1508 | var nestedOperation = 0; |
|
1995 | var nestedOperation = 0; | |
1509 | function operation(f) { |
|
1996 | function operation(f) { | |
@@ -1515,120 +2002,11 b' var CodeMirror = (function() {' | |||||
1515 | }; |
|
2002 | }; | |
1516 | } |
|
2003 | } | |
1517 |
|
2004 | |||
1518 | function SearchCursor(query, pos, caseFold) { |
|
2005 | function compoundChange(f) { | |
1519 | this.atOccurrence = false; |
|
2006 | history.startCompound(); | |
1520 | if (caseFold == null) caseFold = typeof query == "string" && query == query.toLowerCase(); |
|
2007 | try { return f(); } finally { history.endCompound(); } | |
1521 |
|
||||
1522 | if (pos && typeof pos == "object") pos = clipPos(pos); |
|
|||
1523 | else pos = {line: 0, ch: 0}; |
|
|||
1524 | this.pos = {from: pos, to: pos}; |
|
|||
1525 |
|
||||
1526 | // The matches method is filled in based on the type of query. |
|
|||
1527 | // It takes a position and a direction, and returns an object |
|
|||
1528 | // describing the next occurrence of the query, or null if no |
|
|||
1529 | // more matches were found. |
|
|||
1530 | if (typeof query != "string") // Regexp match |
|
|||
1531 | this.matches = function(reverse, pos) { |
|
|||
1532 | if (reverse) { |
|
|||
1533 | var line = lines[pos.line].text.slice(0, pos.ch), match = line.match(query), start = 0; |
|
|||
1534 | while (match) { |
|
|||
1535 | var ind = line.indexOf(match[0]); |
|
|||
1536 | start += ind; |
|
|||
1537 | line = line.slice(ind + 1); |
|
|||
1538 | var newmatch = line.match(query); |
|
|||
1539 | if (newmatch) match = newmatch; |
|
|||
1540 | else break; |
|
|||
1541 | start++; |
|
|||
1542 | } |
|
|||
1543 | } |
|
|||
1544 | else { |
|
|||
1545 | var line = lines[pos.line].text.slice(pos.ch), match = line.match(query), |
|
|||
1546 | start = match && pos.ch + line.indexOf(match[0]); |
|
|||
1547 | } |
|
|||
1548 | if (match) |
|
|||
1549 | return {from: {line: pos.line, ch: start}, |
|
|||
1550 | to: {line: pos.line, ch: start + match[0].length}, |
|
|||
1551 | match: match}; |
|
|||
1552 | }; |
|
|||
1553 | else { // String query |
|
|||
1554 | if (caseFold) query = query.toLowerCase(); |
|
|||
1555 | var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;}; |
|
|||
1556 | var target = query.split("\n"); |
|
|||
1557 | // Different methods for single-line and multi-line queries |
|
|||
1558 | if (target.length == 1) |
|
|||
1559 | this.matches = function(reverse, pos) { |
|
|||
1560 | var line = fold(lines[pos.line].text), len = query.length, match; |
|
|||
1561 | if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1) |
|
|||
1562 | : (match = line.indexOf(query, pos.ch)) != -1) |
|
|||
1563 | return {from: {line: pos.line, ch: match}, |
|
|||
1564 | to: {line: pos.line, ch: match + len}}; |
|
|||
1565 | }; |
|
|||
1566 | else |
|
|||
1567 | this.matches = function(reverse, pos) { |
|
|||
1568 | var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(lines[ln].text); |
|
|||
1569 | var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match)); |
|
|||
1570 | if (reverse ? offsetA >= pos.ch || offsetA != match.length |
|
|||
1571 | : offsetA <= pos.ch || offsetA != line.length - match.length) |
|
|||
1572 | return; |
|
|||
1573 | for (;;) { |
|
|||
1574 | if (reverse ? !ln : ln == lines.length - 1) return; |
|
|||
1575 | line = fold(lines[ln += reverse ? -1 : 1].text); |
|
|||
1576 | match = target[reverse ? --idx : ++idx]; |
|
|||
1577 | if (idx > 0 && idx < target.length - 1) { |
|
|||
1578 | if (line != match) return; |
|
|||
1579 | else continue; |
|
|||
1580 | } |
|
|||
1581 | var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length); |
|
|||
1582 | if (reverse ? offsetB != line.length - match.length : offsetB != match.length) |
|
|||
1583 | return; |
|
|||
1584 | var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB}; |
|
|||
1585 | return {from: reverse ? end : start, to: reverse ? start : end}; |
|
|||
1586 | } |
|
|||
1587 | }; |
|
|||
1588 | } |
|
|||
1589 | } |
|
2008 | } | |
1590 |
|
2009 | |||
1591 | SearchCursor.prototype = { |
|
|||
1592 | findNext: function() {return this.find(false);}, |
|
|||
1593 | findPrevious: function() {return this.find(true);}, |
|
|||
1594 |
|
||||
1595 | find: function(reverse) { |
|
|||
1596 | var self = this, pos = clipPos(reverse ? this.pos.from : this.pos.to); |
|
|||
1597 | function savePosAndFail(line) { |
|
|||
1598 | var pos = {line: line, ch: 0}; |
|
|||
1599 | self.pos = {from: pos, to: pos}; |
|
|||
1600 | self.atOccurrence = false; |
|
|||
1601 | return false; |
|
|||
1602 | } |
|
|||
1603 |
|
||||
1604 | for (;;) { |
|
|||
1605 | if (this.pos = this.matches(reverse, pos)) { |
|
|||
1606 | this.atOccurrence = true; |
|
|||
1607 | return this.pos.match || true; |
|
|||
1608 | } |
|
|||
1609 | if (reverse) { |
|
|||
1610 | if (!pos.line) return savePosAndFail(0); |
|
|||
1611 | pos = {line: pos.line-1, ch: lines[pos.line-1].text.length}; |
|
|||
1612 | } |
|
|||
1613 | else { |
|
|||
1614 | if (pos.line == lines.length - 1) return savePosAndFail(lines.length); |
|
|||
1615 | pos = {line: pos.line+1, ch: 0}; |
|
|||
1616 | } |
|
|||
1617 | } |
|
|||
1618 | }, |
|
|||
1619 |
|
||||
1620 | from: function() {if (this.atOccurrence) return copyPos(this.pos.from);}, |
|
|||
1621 | to: function() {if (this.atOccurrence) return copyPos(this.pos.to);}, |
|
|||
1622 |
|
||||
1623 | replace: function(newText) { |
|
|||
1624 | var self = this; |
|
|||
1625 | if (this.atOccurrence) |
|
|||
1626 | operation(function() { |
|
|||
1627 | self.pos.to = replaceRange(newText, self.pos.from, self.pos.to); |
|
|||
1628 | })(); |
|
|||
1629 | } |
|
|||
1630 | }; |
|
|||
1631 |
|
||||
1632 | for (var ext in extensions) |
|
2010 | for (var ext in extensions) | |
1633 | if (extensions.propertyIsEnumerable(ext) && |
|
2011 | if (extensions.propertyIsEnumerable(ext) && | |
1634 | !instance.propertyIsEnumerable(ext)) |
|
2012 | !instance.propertyIsEnumerable(ext)) | |
@@ -1643,51 +2021,66 b' var CodeMirror = (function() {' | |||||
1643 | theme: "default", |
|
2021 | theme: "default", | |
1644 | indentUnit: 2, |
|
2022 | indentUnit: 2, | |
1645 | indentWithTabs: false, |
|
2023 | indentWithTabs: false, | |
1646 | tabMode: "classic", |
|
2024 | smartIndent: true, | |
1647 | enterMode: "indent", |
|
2025 | tabSize: 4, | |
|
2026 | keyMap: "default", | |||
|
2027 | extraKeys: null, | |||
1648 | electricChars: true, |
|
2028 | electricChars: true, | |
|
2029 | autoClearEmptyLines: false, | |||
1649 | onKeyEvent: null, |
|
2030 | onKeyEvent: null, | |
|
2031 | onDragEvent: null, | |||
|
2032 | lineWrapping: false, | |||
1650 | lineNumbers: false, |
|
2033 | lineNumbers: false, | |
1651 | gutter: false, |
|
2034 | gutter: false, | |
1652 | fixedGutter: false, |
|
2035 | fixedGutter: false, | |
1653 | firstLineNumber: 1, |
|
2036 | firstLineNumber: 1, | |
1654 | readOnly: false, |
|
2037 | readOnly: false, | |
1655 |
|
|
2038 | dragDrop: true, | |
1656 | onChange: null, |
|
2039 | onChange: null, | |
1657 | onCursorActivity: null, |
|
2040 | onCursorActivity: null, | |
1658 | onGutterClick: null, |
|
2041 | onGutterClick: null, | |
1659 | onHighlightComplete: null, |
|
2042 | onHighlightComplete: null, | |
|
2043 | onUpdate: null, | |||
1660 | onFocus: null, onBlur: null, onScroll: null, |
|
2044 | onFocus: null, onBlur: null, onScroll: null, | |
1661 | matchBrackets: false, |
|
2045 | matchBrackets: false, | |
1662 | workTime: 100, |
|
2046 | workTime: 100, | |
1663 | workDelay: 200, |
|
2047 | workDelay: 200, | |
|
2048 | pollInterval: 100, | |||
1664 | undoDepth: 40, |
|
2049 | undoDepth: 40, | |
1665 | tabindex: null, |
|
2050 | tabindex: null, | |
1666 | document: window.document |
|
2051 | autofocus: null | |
1667 | }; |
|
2052 | }; | |
1668 |
|
2053 | |||
|
2054 | var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent); | |||
|
2055 | var mac = ios || /Mac/.test(navigator.platform); | |||
|
2056 | var win = /Win/.test(navigator.platform); | |||
|
2057 | ||||
1669 | // Known modes, by name and by MIME |
|
2058 | // Known modes, by name and by MIME | |
1670 | var modes = {}, mimeModes = {}; |
|
2059 | var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {}; | |
1671 | CodeMirror.defineMode = function(name, mode) { |
|
2060 | CodeMirror.defineMode = function(name, mode) { | |
1672 | if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name; |
|
2061 | if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name; | |
|
2062 | if (arguments.length > 2) { | |||
|
2063 | mode.dependencies = []; | |||
|
2064 | for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]); | |||
|
2065 | } | |||
1673 | modes[name] = mode; |
|
2066 | modes[name] = mode; | |
1674 | }; |
|
2067 | }; | |
1675 | CodeMirror.defineMIME = function(mime, spec) { |
|
2068 | CodeMirror.defineMIME = function(mime, spec) { | |
1676 | mimeModes[mime] = spec; |
|
2069 | mimeModes[mime] = spec; | |
1677 | }; |
|
2070 | }; | |
1678 |
CodeMirror. |
|
2071 | CodeMirror.resolveMode = function(spec) { | |
1679 | if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) |
|
2072 | if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) | |
1680 | spec = mimeModes[spec]; |
|
2073 | spec = mimeModes[spec]; | |
1681 | if (typeof spec == "string") |
|
2074 | else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) | |
1682 | var mname = spec, config = {}; |
|
2075 | return CodeMirror.resolveMode("application/xml"); | |
1683 | else if (spec != null) |
|
2076 | if (typeof spec == "string") return {name: spec}; | |
1684 | var mname = spec.name, config = spec; |
|
2077 | else return spec || {name: "null"}; | |
1685 | var mfactory = modes[mname]; |
|
2078 | }; | |
1686 | if (!mfactory) { |
|
2079 | CodeMirror.getMode = function(options, spec) { | |
1687 | if (window.console) console.warn("No mode " + mname + " found, falling back to plain text."); |
|
2080 | var spec = CodeMirror.resolveMode(spec); | |
1688 | return CodeMirror.getMode(options, "text/plain"); |
|
2081 | var mfactory = modes[spec.name]; | |
1689 | } |
|
2082 | if (!mfactory) return CodeMirror.getMode(options, "text/plain"); | |
1690 |
return mfactory(options, c |
|
2083 | return mfactory(options, spec); | |
1691 | }; |
|
2084 | }; | |
1692 | CodeMirror.listModes = function() { |
|
2085 | CodeMirror.listModes = function() { | |
1693 | var list = []; |
|
2086 | var list = []; | |
@@ -1702,16 +2095,137 b' var CodeMirror = (function() {' | |||||
1702 | return list; |
|
2095 | return list; | |
1703 | }; |
|
2096 | }; | |
1704 |
|
2097 | |||
1705 | var extensions = {}; |
|
2098 | var extensions = CodeMirror.extensions = {}; | |
1706 | CodeMirror.defineExtension = function(name, func) { |
|
2099 | CodeMirror.defineExtension = function(name, func) { | |
1707 | extensions[name] = func; |
|
2100 | extensions[name] = func; | |
1708 | }; |
|
2101 | }; | |
1709 |
|
2102 | |||
|
2103 | var commands = CodeMirror.commands = { | |||
|
2104 | selectAll: function(cm) {cm.setSelection({line: 0, ch: 0}, {line: cm.lineCount() - 1});}, | |||
|
2105 | killLine: function(cm) { | |||
|
2106 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); | |||
|
2107 | if (!sel && cm.getLine(from.line).length == from.ch) cm.replaceRange("", from, {line: from.line + 1, ch: 0}); | |||
|
2108 | else cm.replaceRange("", from, sel ? to : {line: from.line}); | |||
|
2109 | }, | |||
|
2110 | deleteLine: function(cm) {var l = cm.getCursor().line; cm.replaceRange("", {line: l, ch: 0}, {line: l});}, | |||
|
2111 | undo: function(cm) {cm.undo();}, | |||
|
2112 | redo: function(cm) {cm.redo();}, | |||
|
2113 | goDocStart: function(cm) {cm.setCursor(0, 0, true);}, | |||
|
2114 | goDocEnd: function(cm) {cm.setSelection({line: cm.lineCount() - 1}, null, true);}, | |||
|
2115 | goLineStart: function(cm) {cm.setCursor(cm.getCursor().line, 0, true);}, | |||
|
2116 | goLineStartSmart: function(cm) { | |||
|
2117 | var cur = cm.getCursor(); | |||
|
2118 | var text = cm.getLine(cur.line), firstNonWS = Math.max(0, text.search(/\S/)); | |||
|
2119 | cm.setCursor(cur.line, cur.ch <= firstNonWS && cur.ch ? 0 : firstNonWS, true); | |||
|
2120 | }, | |||
|
2121 | goLineEnd: function(cm) {cm.setSelection({line: cm.getCursor().line}, null, true);}, | |||
|
2122 | goLineUp: function(cm) {cm.moveV(-1, "line");}, | |||
|
2123 | goLineDown: function(cm) {cm.moveV(1, "line");}, | |||
|
2124 | goPageUp: function(cm) {cm.moveV(-1, "page");}, | |||
|
2125 | goPageDown: function(cm) {cm.moveV(1, "page");}, | |||
|
2126 | goCharLeft: function(cm) {cm.moveH(-1, "char");}, | |||
|
2127 | goCharRight: function(cm) {cm.moveH(1, "char");}, | |||
|
2128 | goColumnLeft: function(cm) {cm.moveH(-1, "column");}, | |||
|
2129 | goColumnRight: function(cm) {cm.moveH(1, "column");}, | |||
|
2130 | goWordLeft: function(cm) {cm.moveH(-1, "word");}, | |||
|
2131 | goWordRight: function(cm) {cm.moveH(1, "word");}, | |||
|
2132 | delCharLeft: function(cm) {cm.deleteH(-1, "char");}, | |||
|
2133 | delCharRight: function(cm) {cm.deleteH(1, "char");}, | |||
|
2134 | delWordLeft: function(cm) {cm.deleteH(-1, "word");}, | |||
|
2135 | delWordRight: function(cm) {cm.deleteH(1, "word");}, | |||
|
2136 | indentAuto: function(cm) {cm.indentSelection("smart");}, | |||
|
2137 | indentMore: function(cm) {cm.indentSelection("add");}, | |||
|
2138 | indentLess: function(cm) {cm.indentSelection("subtract");}, | |||
|
2139 | insertTab: function(cm) {cm.replaceSelection("\t", "end");}, | |||
|
2140 | defaultTab: function(cm) { | |||
|
2141 | if (cm.somethingSelected()) cm.indentSelection("add"); | |||
|
2142 | else cm.replaceSelection("\t", "end"); | |||
|
2143 | }, | |||
|
2144 | transposeChars: function(cm) { | |||
|
2145 | var cur = cm.getCursor(), line = cm.getLine(cur.line); | |||
|
2146 | if (cur.ch > 0 && cur.ch < line.length - 1) | |||
|
2147 | cm.replaceRange(line.charAt(cur.ch) + line.charAt(cur.ch - 1), | |||
|
2148 | {line: cur.line, ch: cur.ch - 1}, {line: cur.line, ch: cur.ch + 1}); | |||
|
2149 | }, | |||
|
2150 | newlineAndIndent: function(cm) { | |||
|
2151 | cm.replaceSelection("\n", "end"); | |||
|
2152 | cm.indentLine(cm.getCursor().line); | |||
|
2153 | }, | |||
|
2154 | toggleOverwrite: function(cm) {cm.toggleOverwrite();} | |||
|
2155 | }; | |||
|
2156 | ||||
|
2157 | var keyMap = CodeMirror.keyMap = {}; | |||
|
2158 | keyMap.basic = { | |||
|
2159 | "Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown", | |||
|
2160 | "End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown", | |||
|
2161 | "Delete": "delCharRight", "Backspace": "delCharLeft", "Tab": "defaultTab", "Shift-Tab": "indentAuto", | |||
|
2162 | "Enter": "newlineAndIndent", "Insert": "toggleOverwrite" | |||
|
2163 | }; | |||
|
2164 | // Note that the save and find-related commands aren't defined by | |||
|
2165 | // default. Unknown commands are simply ignored. | |||
|
2166 | keyMap.pcDefault = { | |||
|
2167 | "Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo", | |||
|
2168 | "Ctrl-Home": "goDocStart", "Alt-Up": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Down": "goDocEnd", | |||
|
2169 | "Ctrl-Left": "goWordLeft", "Ctrl-Right": "goWordRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd", | |||
|
2170 | "Ctrl-Backspace": "delWordLeft", "Ctrl-Delete": "delWordRight", "Ctrl-S": "save", "Ctrl-F": "find", | |||
|
2171 | "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll", | |||
|
2172 | "Ctrl-[": "indentLess", "Ctrl-]": "indentMore", | |||
|
2173 | fallthrough: "basic" | |||
|
2174 | }; | |||
|
2175 | keyMap.macDefault = { | |||
|
2176 | "Cmd-A": "selectAll", "Cmd-D": "deleteLine", "Cmd-Z": "undo", "Shift-Cmd-Z": "redo", "Cmd-Y": "redo", | |||
|
2177 | "Cmd-Up": "goDocStart", "Cmd-End": "goDocEnd", "Cmd-Down": "goDocEnd", "Alt-Left": "goWordLeft", | |||
|
2178 | "Alt-Right": "goWordRight", "Cmd-Left": "goLineStart", "Cmd-Right": "goLineEnd", "Alt-Backspace": "delWordLeft", | |||
|
2179 | "Ctrl-Alt-Backspace": "delWordRight", "Alt-Delete": "delWordRight", "Cmd-S": "save", "Cmd-F": "find", | |||
|
2180 | "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll", | |||
|
2181 | "Cmd-[": "indentLess", "Cmd-]": "indentMore", | |||
|
2182 | fallthrough: ["basic", "emacsy"] | |||
|
2183 | }; | |||
|
2184 | keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault; | |||
|
2185 | keyMap.emacsy = { | |||
|
2186 | "Ctrl-F": "goCharRight", "Ctrl-B": "goCharLeft", "Ctrl-P": "goLineUp", "Ctrl-N": "goLineDown", | |||
|
2187 | "Alt-F": "goWordRight", "Alt-B": "goWordLeft", "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd", | |||
|
2188 | "Ctrl-V": "goPageUp", "Shift-Ctrl-V": "goPageDown", "Ctrl-D": "delCharRight", "Ctrl-H": "delCharLeft", | |||
|
2189 | "Alt-D": "delWordRight", "Alt-Backspace": "delWordLeft", "Ctrl-K": "killLine", "Ctrl-T": "transposeChars" | |||
|
2190 | }; | |||
|
2191 | ||||
|
2192 | function getKeyMap(val) { | |||
|
2193 | if (typeof val == "string") return keyMap[val]; | |||
|
2194 | else return val; | |||
|
2195 | } | |||
|
2196 | function lookupKey(name, extraMap, map, handle, stop) { | |||
|
2197 | function lookup(map) { | |||
|
2198 | map = getKeyMap(map); | |||
|
2199 | var found = map[name]; | |||
|
2200 | if (found != null && handle(found)) return true; | |||
|
2201 | if (map.nofallthrough) { | |||
|
2202 | if (stop) stop(); | |||
|
2203 | return true; | |||
|
2204 | } | |||
|
2205 | var fallthrough = map.fallthrough; | |||
|
2206 | if (fallthrough == null) return false; | |||
|
2207 | if (Object.prototype.toString.call(fallthrough) != "[object Array]") | |||
|
2208 | return lookup(fallthrough); | |||
|
2209 | for (var i = 0, e = fallthrough.length; i < e; ++i) { | |||
|
2210 | if (lookup(fallthrough[i])) return true; | |||
|
2211 | } | |||
|
2212 | return false; | |||
|
2213 | } | |||
|
2214 | if (extraMap && lookup(extraMap)) return true; | |||
|
2215 | return lookup(map); | |||
|
2216 | } | |||
|
2217 | function isModifierKey(event) { | |||
|
2218 | var name = keyNames[e_prop(event, "keyCode")]; | |||
|
2219 | return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod"; | |||
|
2220 | } | |||
|
2221 | ||||
1710 | CodeMirror.fromTextArea = function(textarea, options) { |
|
2222 | CodeMirror.fromTextArea = function(textarea, options) { | |
1711 | if (!options) options = {}; |
|
2223 | if (!options) options = {}; | |
1712 | options.value = textarea.value; |
|
2224 | options.value = textarea.value; | |
1713 | if (!options.tabindex && textarea.tabindex) |
|
2225 | if (!options.tabindex && textarea.tabindex) | |
1714 | options.tabindex = textarea.tabindex; |
|
2226 | options.tabindex = textarea.tabindex; | |
|
2227 | if (options.autofocus == null && textarea.getAttribute("autofocus") != null) | |||
|
2228 | options.autofocus = true; | |||
1715 |
|
2229 | |||
1716 | function save() {textarea.value = instance.getValue();} |
|
2230 | function save() {textarea.value = instance.getValue();} | |
1717 | if (textarea.form) { |
|
2231 | if (textarea.form) { | |
@@ -1734,6 +2248,7 b' var CodeMirror = (function() {' | |||||
1734 | textarea.parentNode.insertBefore(node, textarea.nextSibling); |
|
2248 | textarea.parentNode.insertBefore(node, textarea.nextSibling); | |
1735 | }, options); |
|
2249 | }, options); | |
1736 | instance.save = save; |
|
2250 | instance.save = save; | |
|
2251 | instance.getTextArea = function() { return textarea; }; | |||
1737 | instance.toTextArea = function() { |
|
2252 | instance.toTextArea = function() { | |
1738 | save(); |
|
2253 | save(); | |
1739 | textarea.parentNode.removeChild(instance.getWrapperElement()); |
|
2254 | textarea.parentNode.removeChild(instance.getWrapperElement()); | |
@@ -1760,16 +2275,17 b' var CodeMirror = (function() {' | |||||
1760 | } |
|
2275 | } | |
1761 | return nstate; |
|
2276 | return nstate; | |
1762 | } |
|
2277 | } | |
1763 |
CodeMirror. |
|
2278 | CodeMirror.copyState = copyState; | |
1764 | function startState(mode, a1, a2) { |
|
2279 | function startState(mode, a1, a2) { | |
1765 | return mode.startState ? mode.startState(a1, a2) : true; |
|
2280 | return mode.startState ? mode.startState(a1, a2) : true; | |
1766 | } |
|
2281 | } | |
1767 |
CodeMirror. |
|
2282 | CodeMirror.startState = startState; | |
1768 |
|
2283 | |||
1769 | // The character stream used by a mode's parser. |
|
2284 | // The character stream used by a mode's parser. | |
1770 | function StringStream(string) { |
|
2285 | function StringStream(string, tabSize) { | |
1771 | this.pos = this.start = 0; |
|
2286 | this.pos = this.start = 0; | |
1772 | this.string = string; |
|
2287 | this.string = string; | |
|
2288 | this.tabSize = tabSize || 8; | |||
1773 | } |
|
2289 | } | |
1774 | StringStream.prototype = { |
|
2290 | StringStream.prototype = { | |
1775 | eol: function() {return this.pos >= this.string.length;}, |
|
2291 | eol: function() {return this.pos >= this.string.length;}, | |
@@ -1801,8 +2317,8 b' var CodeMirror = (function() {' | |||||
1801 | if (found > -1) {this.pos = found; return true;} |
|
2317 | if (found > -1) {this.pos = found; return true;} | |
1802 | }, |
|
2318 | }, | |
1803 | backUp: function(n) {this.pos -= n;}, |
|
2319 | backUp: function(n) {this.pos -= n;}, | |
1804 | column: function() {return countColumn(this.string, this.start);}, |
|
2320 | column: function() {return countColumn(this.string, this.start, this.tabSize);}, | |
1805 | indentation: function() {return countColumn(this.string);}, |
|
2321 | indentation: function() {return countColumn(this.string, null, this.tabSize);}, | |
1806 | match: function(pattern, consume, caseInsensitive) { |
|
2322 | match: function(pattern, consume, caseInsensitive) { | |
1807 | if (typeof pattern == "string") { |
|
2323 | if (typeof pattern == "string") { | |
1808 | function cased(str) {return caseInsensitive ? str.toLowerCase() : str;} |
|
2324 | function cased(str) {return caseInsensitive ? str.toLowerCase() : str;} | |
@@ -1810,8 +2326,7 b' var CodeMirror = (function() {' | |||||
1810 | if (consume !== false) this.pos += pattern.length; |
|
2326 | if (consume !== false) this.pos += pattern.length; | |
1811 | return true; |
|
2327 | return true; | |
1812 | } |
|
2328 | } | |
1813 | } |
|
2329 | } else { | |
1814 | else { |
|
|||
1815 | var match = this.string.slice(this.pos).match(pattern); |
|
2330 | var match = this.string.slice(this.pos).match(pattern); | |
1816 | if (match && consume !== false) this.pos += match[0].length; |
|
2331 | if (match && consume !== false) this.pos += match[0].length; | |
1817 | return match; |
|
2332 | return match; | |
@@ -1821,22 +2336,86 b' var CodeMirror = (function() {' | |||||
1821 | }; |
|
2336 | }; | |
1822 | CodeMirror.StringStream = StringStream; |
|
2337 | CodeMirror.StringStream = StringStream; | |
1823 |
|
2338 | |||
|
2339 | function MarkedText(from, to, className, marker) { | |||
|
2340 | this.from = from; this.to = to; this.style = className; this.marker = marker; | |||
|
2341 | } | |||
|
2342 | MarkedText.prototype = { | |||
|
2343 | attach: function(line) { this.marker.set.push(line); }, | |||
|
2344 | detach: function(line) { | |||
|
2345 | var ix = indexOf(this.marker.set, line); | |||
|
2346 | if (ix > -1) this.marker.set.splice(ix, 1); | |||
|
2347 | }, | |||
|
2348 | split: function(pos, lenBefore) { | |||
|
2349 | if (this.to <= pos && this.to != null) return null; | |||
|
2350 | var from = this.from < pos || this.from == null ? null : this.from - pos + lenBefore; | |||
|
2351 | var to = this.to == null ? null : this.to - pos + lenBefore; | |||
|
2352 | return new MarkedText(from, to, this.style, this.marker); | |||
|
2353 | }, | |||
|
2354 | dup: function() { return new MarkedText(null, null, this.style, this.marker); }, | |||
|
2355 | clipTo: function(fromOpen, from, toOpen, to, diff) { | |||
|
2356 | if (fromOpen && to > this.from && (to < this.to || this.to == null)) | |||
|
2357 | this.from = null; | |||
|
2358 | else if (this.from != null && this.from >= from) | |||
|
2359 | this.from = Math.max(to, this.from) + diff; | |||
|
2360 | if (toOpen && (from < this.to || this.to == null) && (from > this.from || this.from == null)) | |||
|
2361 | this.to = null; | |||
|
2362 | else if (this.to != null && this.to > from) | |||
|
2363 | this.to = to < this.to ? this.to + diff : from; | |||
|
2364 | }, | |||
|
2365 | isDead: function() { return this.from != null && this.to != null && this.from >= this.to; }, | |||
|
2366 | sameSet: function(x) { return this.marker == x.marker; } | |||
|
2367 | }; | |||
|
2368 | ||||
|
2369 | function Bookmark(pos) { | |||
|
2370 | this.from = pos; this.to = pos; this.line = null; | |||
|
2371 | } | |||
|
2372 | Bookmark.prototype = { | |||
|
2373 | attach: function(line) { this.line = line; }, | |||
|
2374 | detach: function(line) { if (this.line == line) this.line = null; }, | |||
|
2375 | split: function(pos, lenBefore) { | |||
|
2376 | if (pos < this.from) { | |||
|
2377 | this.from = this.to = (this.from - pos) + lenBefore; | |||
|
2378 | return this; | |||
|
2379 | } | |||
|
2380 | }, | |||
|
2381 | isDead: function() { return this.from > this.to; }, | |||
|
2382 | clipTo: function(fromOpen, from, toOpen, to, diff) { | |||
|
2383 | if ((fromOpen || from < this.from) && (toOpen || to > this.to)) { | |||
|
2384 | this.from = 0; this.to = -1; | |||
|
2385 | } else if (this.from > from) { | |||
|
2386 | this.from = this.to = Math.max(to, this.from) + diff; | |||
|
2387 | } | |||
|
2388 | }, | |||
|
2389 | sameSet: function(x) { return false; }, | |||
|
2390 | find: function() { | |||
|
2391 | if (!this.line || !this.line.parent) return null; | |||
|
2392 | return {line: lineNo(this.line), ch: this.from}; | |||
|
2393 | }, | |||
|
2394 | clear: function() { | |||
|
2395 | if (this.line) { | |||
|
2396 | var found = indexOf(this.line.marked, this); | |||
|
2397 | if (found != -1) this.line.marked.splice(found, 1); | |||
|
2398 | this.line = null; | |||
|
2399 | } | |||
|
2400 | } | |||
|
2401 | }; | |||
|
2402 | ||||
1824 | // Line objects. These hold state related to a line, including |
|
2403 | // Line objects. These hold state related to a line, including | |
1825 | // highlighting info (the styles array). |
|
2404 | // highlighting info (the styles array). | |
1826 | function Line(text, styles) { |
|
2405 | function Line(text, styles) { | |
1827 | this.styles = styles || [text, null]; |
|
2406 | this.styles = styles || [text, null]; | |
1828 | this.stateAfter = null; |
|
|||
1829 | this.text = text; |
|
2407 | this.text = text; | |
1830 | this.marked = this.gutterMarker = this.className = null; |
|
2408 | this.height = 1; | |
|
2409 | this.marked = this.gutterMarker = this.className = this.bgClassName = this.handlers = null; | |||
|
2410 | this.stateAfter = this.parent = this.hidden = null; | |||
1831 | } |
|
2411 | } | |
1832 | Line.inheritMarks = function(text, orig) { |
|
2412 | Line.inheritMarks = function(text, orig) { | |
1833 | var ln = new Line(text), mk = orig.marked; |
|
2413 | var ln = new Line(text), mk = orig && orig.marked; | |
1834 | if (mk) { |
|
2414 | if (mk) { | |
1835 | for (var i = 0; i < mk.length; ++i) { |
|
2415 | for (var i = 0; i < mk.length; ++i) { | |
1836 | if (mk[i].to == null) { |
|
2416 | if (mk[i].to == null && mk[i].style) { | |
1837 | var newmk = ln.marked || (ln.marked = []), mark = mk[i]; |
|
2417 | var newmk = ln.marked || (ln.marked = []), mark = mk[i]; | |
1838 | newmk.push({from: null, to: null, style: mark.style, set: mark.set}); |
|
2418 | var nmark = mark.dup(); newmk.push(nmark); nmark.attach(ln); | |
1839 | mark.set.push(ln); |
|
|||
1840 | } |
|
2419 | } | |
1841 | } |
|
2420 | } | |
1842 | } |
|
2421 | } | |
@@ -1853,23 +2432,11 b' var CodeMirror = (function() {' | |||||
1853 | this.text = this.text.slice(0, from) + text + this.text.slice(to); |
|
2432 | this.text = this.text.slice(0, from) + text + this.text.slice(to); | |
1854 | this.stateAfter = null; |
|
2433 | this.stateAfter = null; | |
1855 | if (mk) { |
|
2434 | if (mk) { | |
1856 |
var diff = text.length - (to - from) |
|
2435 | var diff = text.length - (to - from); | |
1857 | var changeStart = Math.min(from, from + diff); |
|
|||
1858 | for (var i = 0; i < mk.length; ++i) { |
|
2436 | for (var i = 0; i < mk.length; ++i) { | |
1859 |
var mark = mk[i] |
|
2437 | var mark = mk[i]; | |
1860 | if (mark.from != null && mark.from >= end) del = true; |
|
2438 | mark.clipTo(from == null, from || 0, to_ == null, to, diff); | |
1861 | else { |
|
2439 | if (mark.isDead()) {mark.detach(this); mk.splice(i--, 1);} | |
1862 | if (mark.from != null && mark.from >= from) { |
|
|||
1863 | mark.from += diff; |
|
|||
1864 | if (mark.from <= 0) mark.from = from == null ? null : 0; |
|
|||
1865 | } |
|
|||
1866 | else if (to_ == null) mark.to = null; |
|
|||
1867 | if (mark.to != null && mark.to > from) { |
|
|||
1868 | mark.to += diff; |
|
|||
1869 | if (mark.to < 0) del = true; |
|
|||
1870 | } |
|
|||
1871 | } |
|
|||
1872 | if (del || (mark.from != null && mark.to != null && mark.from >= mark.to)) mk.splice(i--, 1); |
|
|||
1873 | } |
|
2440 | } | |
1874 | } |
|
2441 | } | |
1875 | }, |
|
2442 | }, | |
@@ -1881,57 +2448,77 b' var CodeMirror = (function() {' | |||||
1881 | if (mk) { |
|
2448 | if (mk) { | |
1882 | for (var i = 0; i < mk.length; ++i) { |
|
2449 | for (var i = 0; i < mk.length; ++i) { | |
1883 | var mark = mk[i]; |
|
2450 | var mark = mk[i]; | |
1884 | if (mark.to > pos || mark.to == null) { |
|
2451 | var newmark = mark.split(pos, textBefore.length); | |
|
2452 | if (newmark) { | |||
1885 | if (!taken.marked) taken.marked = []; |
|
2453 | if (!taken.marked) taken.marked = []; | |
1886 |
taken.marked.push( |
|
2454 | taken.marked.push(newmark); newmark.attach(taken); | |
1887 | from: mark.from < pos || mark.from == null ? null : mark.from - pos + textBefore.length, |
|
2455 | if (newmark == mark) mk.splice(i--, 1); | |
1888 | to: mark.to == null ? null : mark.to - pos + textBefore.length, |
|
|||
1889 | style: mark.style, set: mark.set |
|
|||
1890 | }); |
|
|||
1891 | mark.set.push(taken); |
|
|||
1892 | } |
|
2456 | } | |
1893 | } |
|
2457 | } | |
1894 | } |
|
2458 | } | |
1895 | return taken; |
|
2459 | return taken; | |
1896 | }, |
|
2460 | }, | |
1897 | append: function(line) { |
|
2461 | append: function(line) { | |
1898 | if (!line.text.length) return; |
|
2462 | var mylen = this.text.length, mk = line.marked, mymk = this.marked; | |
1899 | var mylen = this.text.length, mk = line.marked; |
|
|||
1900 | this.text += line.text; |
|
2463 | this.text += line.text; | |
1901 | copyStyles(0, line.text.length, line.styles, this.styles); |
|
2464 | copyStyles(0, line.text.length, line.styles, this.styles); | |
1902 |
if (m |
|
2465 | if (mymk) { | |
1903 | var mymk = this.marked || (this.marked = []); |
|
|||
1904 | for (var i = 0; i < mymk.length; ++i) |
|
2466 | for (var i = 0; i < mymk.length; ++i) | |
1905 | if (mymk[i].to == null) mymk[i].to = mylen; |
|
2467 | if (mymk[i].to == null) mymk[i].to = mylen; | |
|
2468 | } | |||
|
2469 | if (mk && mk.length) { | |||
|
2470 | if (!mymk) this.marked = mymk = []; | |||
1906 | outer: for (var i = 0; i < mk.length; ++i) { |
|
2471 | outer: for (var i = 0; i < mk.length; ++i) { | |
1907 | var mark = mk[i]; |
|
2472 | var mark = mk[i]; | |
1908 | if (!mark.from) { |
|
2473 | if (!mark.from) { | |
1909 | for (var j = 0; j < mymk.length; ++j) { |
|
2474 | for (var j = 0; j < mymk.length; ++j) { | |
1910 | var mymark = mymk[j]; |
|
2475 | var mymark = mymk[j]; | |
1911 |
if (mymark.to == mylen && mymark.s |
|
2476 | if (mymark.to == mylen && mymark.sameSet(mark)) { | |
1912 | mymark.to = mark.to == null ? null : mark.to + mylen; |
|
2477 | mymark.to = mark.to == null ? null : mark.to + mylen; | |
|
2478 | if (mymark.isDead()) { | |||
|
2479 | mymark.detach(this); | |||
|
2480 | mk.splice(i--, 1); | |||
|
2481 | } | |||
1913 | continue outer; |
|
2482 | continue outer; | |
1914 | } |
|
2483 | } | |
1915 | } |
|
2484 | } | |
1916 | } |
|
2485 | } | |
1917 | mymk.push(mark); |
|
2486 | mymk.push(mark); | |
1918 |
mark. |
|
2487 | mark.attach(this); | |
1919 | mark.from += mylen; |
|
2488 | mark.from += mylen; | |
1920 | if (mark.to != null) mark.to += mylen; |
|
2489 | if (mark.to != null) mark.to += mylen; | |
1921 | } |
|
2490 | } | |
1922 | } |
|
2491 | } | |
1923 | }, |
|
2492 | }, | |
1924 | addMark: function(from, to, style, set) { |
|
2493 | fixMarkEnds: function(other) { | |
1925 | set.push(this); |
|
2494 | var mk = this.marked, omk = other.marked; | |
|
2495 | if (!mk) return; | |||
|
2496 | for (var i = 0; i < mk.length; ++i) { | |||
|
2497 | var mark = mk[i], close = mark.to == null; | |||
|
2498 | if (close && omk) { | |||
|
2499 | for (var j = 0; j < omk.length; ++j) | |||
|
2500 | if (omk[j].sameSet(mark)) {close = false; break;} | |||
|
2501 | } | |||
|
2502 | if (close) mark.to = this.text.length; | |||
|
2503 | } | |||
|
2504 | }, | |||
|
2505 | fixMarkStarts: function() { | |||
|
2506 | var mk = this.marked; | |||
|
2507 | if (!mk) return; | |||
|
2508 | for (var i = 0; i < mk.length; ++i) | |||
|
2509 | if (mk[i].from == null) mk[i].from = 0; | |||
|
2510 | }, | |||
|
2511 | addMark: function(mark) { | |||
|
2512 | mark.attach(this); | |||
1926 | if (this.marked == null) this.marked = []; |
|
2513 | if (this.marked == null) this.marked = []; | |
1927 | this.marked.push({from: from, to: to, style: style, set: set}); |
|
2514 | this.marked.push(mark); | |
1928 | this.marked.sort(function(a, b){return (a.from || 0) - (b.from || 0);}); |
|
2515 | this.marked.sort(function(a, b){return (a.from || 0) - (b.from || 0);}); | |
1929 | }, |
|
2516 | }, | |
1930 | // Run the given mode's parser over a line, update the styles |
|
2517 | // Run the given mode's parser over a line, update the styles | |
1931 | // array, which contains alternating fragments of text and CSS |
|
2518 | // array, which contains alternating fragments of text and CSS | |
1932 | // classes. |
|
2519 | // classes. | |
1933 | highlight: function(mode, state) { |
|
2520 | highlight: function(mode, state, tabSize) { | |
1934 | var stream = new StringStream(this.text), st = this.styles, pos = 0; |
|
2521 | var stream = new StringStream(this.text, tabSize), st = this.styles, pos = 0; | |
1935 | var changed = false, curWord = st[0], prevWord; |
|
2522 | var changed = false, curWord = st[0], prevWord; | |
1936 | if (this.text == "" && mode.blankLine) mode.blankLine(state); |
|
2523 | if (this.text == "" && mode.blankLine) mode.blankLine(state); | |
1937 | while (!stream.eol()) { |
|
2524 | while (!stream.eol()) { | |
@@ -1972,74 +2559,125 b' var CodeMirror = (function() {' | |||||
1972 | className: style || null, |
|
2559 | className: style || null, | |
1973 | state: state}; |
|
2560 | state: state}; | |
1974 | }, |
|
2561 | }, | |
1975 | indentation: function() {return countColumn(this.text);}, |
|
2562 | indentation: function(tabSize) {return countColumn(this.text, null, tabSize);}, | |
1976 | // Produces an HTML fragment for the line, taking selection, |
|
2563 | // Produces an HTML fragment for the line, taking selection, | |
1977 | // marking, and highlighting into account. |
|
2564 | // marking, and highlighting into account. | |
1978 |
getHTML: function( |
|
2565 | getHTML: function(makeTab, wrapAt, wrapId, wrapWBR) { | |
1979 | var html = []; |
|
2566 | var html = [], first = true, col = 0; | |
1980 | if (includePre) |
|
2567 | function span_(text, style) { | |
1981 | html.push(this.className ? '<pre class="' + this.className + '">': "<pre>"); |
|
|||
1982 | function span(text, style) { |
|
|||
1983 | if (!text) return; |
|
2568 | if (!text) return; | |
1984 | if (style) html.push('<span class="', style, '">', htmlEscape(text), "</span>"); |
|
2569 | // Work around a bug where, in some compat modes, IE ignores leading spaces | |
1985 | else html.push(htmlEscape(text)); |
|
2570 | if (first && ie && text.charAt(0) == " ") text = "\u00a0" + text.slice(1); | |
|
2571 | first = false; | |||
|
2572 | if (text.indexOf("\t") == -1) { | |||
|
2573 | col += text.length; | |||
|
2574 | var escaped = htmlEscape(text); | |||
|
2575 | } else { | |||
|
2576 | var escaped = ""; | |||
|
2577 | for (var pos = 0;;) { | |||
|
2578 | var idx = text.indexOf("\t", pos); | |||
|
2579 | if (idx == -1) { | |||
|
2580 | escaped += htmlEscape(text.slice(pos)); | |||
|
2581 | col += text.length - pos; | |||
|
2582 | break; | |||
|
2583 | } else { | |||
|
2584 | col += idx - pos; | |||
|
2585 | var tab = makeTab(col); | |||
|
2586 | escaped += htmlEscape(text.slice(pos, idx)) + tab.html; | |||
|
2587 | col += tab.width; | |||
|
2588 | pos = idx + 1; | |||
|
2589 | } | |||
|
2590 | } | |||
|
2591 | } | |||
|
2592 | if (style) html.push('<span class="', style, '">', escaped, "</span>"); | |||
|
2593 | else html.push(escaped); | |||
1986 | } |
|
2594 | } | |
|
2595 | var span = span_; | |||
|
2596 | if (wrapAt != null) { | |||
|
2597 | var outPos = 0, open = "<span id=\"" + wrapId + "\">"; | |||
|
2598 | span = function(text, style) { | |||
|
2599 | var l = text.length; | |||
|
2600 | if (wrapAt >= outPos && wrapAt < outPos + l) { | |||
|
2601 | if (wrapAt > outPos) { | |||
|
2602 | span_(text.slice(0, wrapAt - outPos), style); | |||
|
2603 | // See comment at the definition of spanAffectsWrapping | |||
|
2604 | if (wrapWBR) html.push("<wbr>"); | |||
|
2605 | } | |||
|
2606 | html.push(open); | |||
|
2607 | var cut = wrapAt - outPos; | |||
|
2608 | span_(opera ? text.slice(cut, cut + 1) : text.slice(cut), style); | |||
|
2609 | html.push("</span>"); | |||
|
2610 | if (opera) span_(text.slice(cut + 1), style); | |||
|
2611 | wrapAt--; | |||
|
2612 | outPos += l; | |||
|
2613 | } else { | |||
|
2614 | outPos += l; | |||
|
2615 | span_(text, style); | |||
|
2616 | // Output empty wrapper when at end of line | |||
|
2617 | if (outPos == wrapAt && outPos == len) html.push(open + " </span>"); | |||
|
2618 | // Stop outputting HTML when gone sufficiently far beyond measure | |||
|
2619 | else if (outPos > wrapAt + 10 && /\s/.test(text)) span = function(){}; | |||
|
2620 | } | |||
|
2621 | } | |||
|
2622 | } | |||
|
2623 | ||||
1987 | var st = this.styles, allText = this.text, marked = this.marked; |
|
2624 | var st = this.styles, allText = this.text, marked = this.marked; | |
1988 | if (sfrom == sto) sfrom = null; |
|
|||
1989 | var len = allText.length; |
|
2625 | var len = allText.length; | |
1990 | if (endAt != null) len = Math.min(endAt, len); |
|
2626 | function styleToClass(style) { | |
|
2627 | if (!style) return null; | |||
|
2628 | return "cm-" + style.replace(/ +/g, " cm-"); | |||
|
2629 | } | |||
1991 |
|
2630 | |||
1992 |
if (!allText && |
|
2631 | if (!allText && wrapAt == null) { | |
1993 | span(" ", sfrom != null && sto == null ? "CodeMirror-selected" : null); |
|
2632 | span(" "); | |
1994 |
else if (!marked |
|
2633 | } else if (!marked || !marked.length) { | |
1995 | for (var i = 0, ch = 0; ch < len; i+=2) { |
|
2634 | for (var i = 0, ch = 0; ch < len; i+=2) { | |
1996 | var str = st[i], style = st[i+1], l = str.length; |
|
2635 | var str = st[i], style = st[i+1], l = str.length; | |
1997 | if (ch + l > len) str = str.slice(0, len - ch); |
|
2636 | if (ch + l > len) str = str.slice(0, len - ch); | |
1998 | ch += l; |
|
2637 | ch += l; | |
1999 |
span(str, style |
|
2638 | span(str, styleToClass(style)); | |
2000 | } |
|
2639 | } | |
2001 | else { |
|
2640 | } else { | |
2002 | var pos = 0, i = 0, text = "", style, sg = 0; |
|
2641 | var pos = 0, i = 0, text = "", style, sg = 0; | |
2003 | var markpos = -1, mark = null; |
|
2642 | var nextChange = marked[0].from || 0, marks = [], markpos = 0; | |
2004 |
function ne |
|
2643 | function advanceMarks() { | |
2005 |
|
|
2644 | var m; | |
2006 | markpos += 1; |
|
2645 | while (markpos < marked.length && | |
2007 | mark = (markpos < marked.length) ? marked[markpos] : null; |
|
2646 | ((m = marked[markpos]).from == pos || m.from == null)) { | |
|
2647 | if (m.style != null) marks.push(m); | |||
|
2648 | ++markpos; | |||
|
2649 | } | |||
|
2650 | nextChange = markpos < marked.length ? marked[markpos].from : Infinity; | |||
|
2651 | for (var i = 0; i < marks.length; ++i) { | |||
|
2652 | var to = marks[i].to || Infinity; | |||
|
2653 | if (to == pos) marks.splice(i--, 1); | |||
|
2654 | else nextChange = Math.min(to, nextChange); | |||
2008 | } |
|
2655 | } | |
2009 | } |
|
2656 | } | |
2010 | nextMark(); |
|
2657 | var m = 0; | |
2011 | while (pos < len) { |
|
2658 | while (pos < len) { | |
2012 | var upto = len; |
|
2659 | if (nextChange == pos) advanceMarks(); | |
2013 | var extraStyle = ""; |
|
2660 | var upto = Math.min(len, nextChange); | |
2014 |
|
|
2661 | while (true) { | |
2015 | if (sfrom > pos) upto = sfrom; |
|
2662 | if (text) { | |
2016 | else if (sto == null || sto > pos) { |
|
2663 | var end = pos + text.length; | |
2017 |
|
|
2664 | var appliedStyle = style; | |
2018 | if (sto != null) upto = Math.min(upto, sto); |
|
2665 | for (var j = 0; j < marks.length; ++j) | |
|
2666 | appliedStyle = (appliedStyle ? appliedStyle + " " : "") + marks[j].style; | |||
|
2667 | span(end > upto ? text.slice(0, upto - pos) : text, appliedStyle); | |||
|
2668 | if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;} | |||
|
2669 | pos = end; | |||
2019 | } |
|
2670 | } | |
2020 | } |
|
2671 | text = st[i++]; style = styleToClass(st[i++]); | |
2021 | while (mark && mark.to != null && mark.to <= pos) nextMark(); |
|
|||
2022 | if (mark) { |
|
|||
2023 | if (mark.from > pos) upto = Math.min(upto, mark.from); |
|
|||
2024 | else { |
|
|||
2025 | extraStyle += " " + mark.style; |
|
|||
2026 | if (mark.to != null) upto = Math.min(upto, mark.to); |
|
|||
2027 | } |
|
|||
2028 | } |
|
|||
2029 | for (;;) { |
|
|||
2030 | var end = pos + text.length; |
|
|||
2031 | var appliedStyle = style; |
|
|||
2032 | if (extraStyle) appliedStyle = style ? style + extraStyle : extraStyle; |
|
|||
2033 | span(end > upto ? text.slice(0, upto - pos) : text, appliedStyle); |
|
|||
2034 | if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;} |
|
|||
2035 | pos = end; |
|
|||
2036 | text = st[i++]; style = "cm-" + st[i++]; |
|
|||
2037 | } |
|
2672 | } | |
2038 | } |
|
2673 | } | |
2039 | if (sfrom != null && sto == null) span(" ", "CodeMirror-selected"); |
|
|||
2040 | } |
|
2674 | } | |
2041 | if (includePre) html.push("</pre>"); |
|
|||
2042 | return html.join(""); |
|
2675 | return html.join(""); | |
|
2676 | }, | |||
|
2677 | cleanUp: function() { | |||
|
2678 | this.parent = null; | |||
|
2679 | if (this.marked) | |||
|
2680 | for (var i = 0, e = this.marked.length; i < e; ++i) this.marked[i].detach(this); | |||
2043 | } |
|
2681 | } | |
2044 | }; |
|
2682 | }; | |
2045 | // Utility used by replace and split above |
|
2683 | // Utility used by replace and split above | |
@@ -2049,8 +2687,7 b' var CodeMirror = (function() {' | |||||
2049 | if (state == 0) { |
|
2687 | if (state == 0) { | |
2050 | if (end > from) dest.push(part.slice(from - pos, Math.min(part.length, to - pos)), source[i+1]); |
|
2688 | if (end > from) dest.push(part.slice(from - pos, Math.min(part.length, to - pos)), source[i+1]); | |
2051 | if (end >= from) state = 1; |
|
2689 | if (end >= from) state = 1; | |
2052 | } |
|
2690 | } else if (state == 1) { | |
2053 | else if (state == 1) { |
|
|||
2054 | if (end > to) dest.push(part.slice(0, to - pos), source[i+1]); |
|
2691 | if (end > to) dest.push(part.slice(0, to - pos), source[i+1]); | |
2055 | else dest.push(part, source[i+1]); |
|
2692 | else dest.push(part, source[i+1]); | |
2056 | } |
|
2693 | } | |
@@ -2058,36 +2695,229 b' var CodeMirror = (function() {' | |||||
2058 | } |
|
2695 | } | |
2059 | } |
|
2696 | } | |
2060 |
|
2697 | |||
|
2698 | // Data structure that holds the sequence of lines. | |||
|
2699 | function LeafChunk(lines) { | |||
|
2700 | this.lines = lines; | |||
|
2701 | this.parent = null; | |||
|
2702 | for (var i = 0, e = lines.length, height = 0; i < e; ++i) { | |||
|
2703 | lines[i].parent = this; | |||
|
2704 | height += lines[i].height; | |||
|
2705 | } | |||
|
2706 | this.height = height; | |||
|
2707 | } | |||
|
2708 | LeafChunk.prototype = { | |||
|
2709 | chunkSize: function() { return this.lines.length; }, | |||
|
2710 | remove: function(at, n, callbacks) { | |||
|
2711 | for (var i = at, e = at + n; i < e; ++i) { | |||
|
2712 | var line = this.lines[i]; | |||
|
2713 | this.height -= line.height; | |||
|
2714 | line.cleanUp(); | |||
|
2715 | if (line.handlers) | |||
|
2716 | for (var j = 0; j < line.handlers.length; ++j) callbacks.push(line.handlers[j]); | |||
|
2717 | } | |||
|
2718 | this.lines.splice(at, n); | |||
|
2719 | }, | |||
|
2720 | collapse: function(lines) { | |||
|
2721 | lines.splice.apply(lines, [lines.length, 0].concat(this.lines)); | |||
|
2722 | }, | |||
|
2723 | insertHeight: function(at, lines, height) { | |||
|
2724 | this.height += height; | |||
|
2725 | this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at)); | |||
|
2726 | for (var i = 0, e = lines.length; i < e; ++i) lines[i].parent = this; | |||
|
2727 | }, | |||
|
2728 | iterN: function(at, n, op) { | |||
|
2729 | for (var e = at + n; at < e; ++at) | |||
|
2730 | if (op(this.lines[at])) return true; | |||
|
2731 | } | |||
|
2732 | }; | |||
|
2733 | function BranchChunk(children) { | |||
|
2734 | this.children = children; | |||
|
2735 | var size = 0, height = 0; | |||
|
2736 | for (var i = 0, e = children.length; i < e; ++i) { | |||
|
2737 | var ch = children[i]; | |||
|
2738 | size += ch.chunkSize(); height += ch.height; | |||
|
2739 | ch.parent = this; | |||
|
2740 | } | |||
|
2741 | this.size = size; | |||
|
2742 | this.height = height; | |||
|
2743 | this.parent = null; | |||
|
2744 | } | |||
|
2745 | BranchChunk.prototype = { | |||
|
2746 | chunkSize: function() { return this.size; }, | |||
|
2747 | remove: function(at, n, callbacks) { | |||
|
2748 | this.size -= n; | |||
|
2749 | for (var i = 0; i < this.children.length; ++i) { | |||
|
2750 | var child = this.children[i], sz = child.chunkSize(); | |||
|
2751 | if (at < sz) { | |||
|
2752 | var rm = Math.min(n, sz - at), oldHeight = child.height; | |||
|
2753 | child.remove(at, rm, callbacks); | |||
|
2754 | this.height -= oldHeight - child.height; | |||
|
2755 | if (sz == rm) { this.children.splice(i--, 1); child.parent = null; } | |||
|
2756 | if ((n -= rm) == 0) break; | |||
|
2757 | at = 0; | |||
|
2758 | } else at -= sz; | |||
|
2759 | } | |||
|
2760 | if (this.size - n < 25) { | |||
|
2761 | var lines = []; | |||
|
2762 | this.collapse(lines); | |||
|
2763 | this.children = [new LeafChunk(lines)]; | |||
|
2764 | this.children[0].parent = this; | |||
|
2765 | } | |||
|
2766 | }, | |||
|
2767 | collapse: function(lines) { | |||
|
2768 | for (var i = 0, e = this.children.length; i < e; ++i) this.children[i].collapse(lines); | |||
|
2769 | }, | |||
|
2770 | insert: function(at, lines) { | |||
|
2771 | var height = 0; | |||
|
2772 | for (var i = 0, e = lines.length; i < e; ++i) height += lines[i].height; | |||
|
2773 | this.insertHeight(at, lines, height); | |||
|
2774 | }, | |||
|
2775 | insertHeight: function(at, lines, height) { | |||
|
2776 | this.size += lines.length; | |||
|
2777 | this.height += height; | |||
|
2778 | for (var i = 0, e = this.children.length; i < e; ++i) { | |||
|
2779 | var child = this.children[i], sz = child.chunkSize(); | |||
|
2780 | if (at <= sz) { | |||
|
2781 | child.insertHeight(at, lines, height); | |||
|
2782 | if (child.lines && child.lines.length > 50) { | |||
|
2783 | while (child.lines.length > 50) { | |||
|
2784 | var spilled = child.lines.splice(child.lines.length - 25, 25); | |||
|
2785 | var newleaf = new LeafChunk(spilled); | |||
|
2786 | child.height -= newleaf.height; | |||
|
2787 | this.children.splice(i + 1, 0, newleaf); | |||
|
2788 | newleaf.parent = this; | |||
|
2789 | } | |||
|
2790 | this.maybeSpill(); | |||
|
2791 | } | |||
|
2792 | break; | |||
|
2793 | } | |||
|
2794 | at -= sz; | |||
|
2795 | } | |||
|
2796 | }, | |||
|
2797 | maybeSpill: function() { | |||
|
2798 | if (this.children.length <= 10) return; | |||
|
2799 | var me = this; | |||
|
2800 | do { | |||
|
2801 | var spilled = me.children.splice(me.children.length - 5, 5); | |||
|
2802 | var sibling = new BranchChunk(spilled); | |||
|
2803 | if (!me.parent) { // Become the parent node | |||
|
2804 | var copy = new BranchChunk(me.children); | |||
|
2805 | copy.parent = me; | |||
|
2806 | me.children = [copy, sibling]; | |||
|
2807 | me = copy; | |||
|
2808 | } else { | |||
|
2809 | me.size -= sibling.size; | |||
|
2810 | me.height -= sibling.height; | |||
|
2811 | var myIndex = indexOf(me.parent.children, me); | |||
|
2812 | me.parent.children.splice(myIndex + 1, 0, sibling); | |||
|
2813 | } | |||
|
2814 | sibling.parent = me.parent; | |||
|
2815 | } while (me.children.length > 10); | |||
|
2816 | me.parent.maybeSpill(); | |||
|
2817 | }, | |||
|
2818 | iter: function(from, to, op) { this.iterN(from, to - from, op); }, | |||
|
2819 | iterN: function(at, n, op) { | |||
|
2820 | for (var i = 0, e = this.children.length; i < e; ++i) { | |||
|
2821 | var child = this.children[i], sz = child.chunkSize(); | |||
|
2822 | if (at < sz) { | |||
|
2823 | var used = Math.min(n, sz - at); | |||
|
2824 | if (child.iterN(at, used, op)) return true; | |||
|
2825 | if ((n -= used) == 0) break; | |||
|
2826 | at = 0; | |||
|
2827 | } else at -= sz; | |||
|
2828 | } | |||
|
2829 | } | |||
|
2830 | }; | |||
|
2831 | ||||
|
2832 | function getLineAt(chunk, n) { | |||
|
2833 | while (!chunk.lines) { | |||
|
2834 | for (var i = 0;; ++i) { | |||
|
2835 | var child = chunk.children[i], sz = child.chunkSize(); | |||
|
2836 | if (n < sz) { chunk = child; break; } | |||
|
2837 | n -= sz; | |||
|
2838 | } | |||
|
2839 | } | |||
|
2840 | return chunk.lines[n]; | |||
|
2841 | } | |||
|
2842 | function lineNo(line) { | |||
|
2843 | if (line.parent == null) return null; | |||
|
2844 | var cur = line.parent, no = indexOf(cur.lines, line); | |||
|
2845 | for (var chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) { | |||
|
2846 | for (var i = 0, e = chunk.children.length; ; ++i) { | |||
|
2847 | if (chunk.children[i] == cur) break; | |||
|
2848 | no += chunk.children[i].chunkSize(); | |||
|
2849 | } | |||
|
2850 | } | |||
|
2851 | return no; | |||
|
2852 | } | |||
|
2853 | function lineAtHeight(chunk, h) { | |||
|
2854 | var n = 0; | |||
|
2855 | outer: do { | |||
|
2856 | for (var i = 0, e = chunk.children.length; i < e; ++i) { | |||
|
2857 | var child = chunk.children[i], ch = child.height; | |||
|
2858 | if (h < ch) { chunk = child; continue outer; } | |||
|
2859 | h -= ch; | |||
|
2860 | n += child.chunkSize(); | |||
|
2861 | } | |||
|
2862 | return n; | |||
|
2863 | } while (!chunk.lines); | |||
|
2864 | for (var i = 0, e = chunk.lines.length; i < e; ++i) { | |||
|
2865 | var line = chunk.lines[i], lh = line.height; | |||
|
2866 | if (h < lh) break; | |||
|
2867 | h -= lh; | |||
|
2868 | } | |||
|
2869 | return n + i; | |||
|
2870 | } | |||
|
2871 | function heightAtLine(chunk, n) { | |||
|
2872 | var h = 0; | |||
|
2873 | outer: do { | |||
|
2874 | for (var i = 0, e = chunk.children.length; i < e; ++i) { | |||
|
2875 | var child = chunk.children[i], sz = child.chunkSize(); | |||
|
2876 | if (n < sz) { chunk = child; continue outer; } | |||
|
2877 | n -= sz; | |||
|
2878 | h += child.height; | |||
|
2879 | } | |||
|
2880 | return h; | |||
|
2881 | } while (!chunk.lines); | |||
|
2882 | for (var i = 0; i < n; ++i) h += chunk.lines[i].height; | |||
|
2883 | return h; | |||
|
2884 | } | |||
|
2885 | ||||
2061 | // The history object 'chunks' changes that are made close together |
|
2886 | // The history object 'chunks' changes that are made close together | |
2062 | // and at almost the same time into bigger undoable units. |
|
2887 | // and at almost the same time into bigger undoable units. | |
2063 | function History() { |
|
2888 | function History() { | |
2064 | this.time = 0; |
|
2889 | this.time = 0; | |
2065 | this.done = []; this.undone = []; |
|
2890 | this.done = []; this.undone = []; | |
|
2891 | this.compound = 0; | |||
|
2892 | this.closed = false; | |||
2066 | } |
|
2893 | } | |
2067 | History.prototype = { |
|
2894 | History.prototype = { | |
2068 | addChange: function(start, added, old) { |
|
2895 | addChange: function(start, added, old) { | |
2069 | this.undone.length = 0; |
|
2896 | this.undone.length = 0; | |
2070 |
var time = +new Date, |
|
2897 | var time = +new Date, cur = this.done[this.done.length - 1], last = cur && cur[cur.length - 1]; | |
2071 |
|
|
2898 | var dtime = time - this.time; | |
2072 | last.start > start + added || last.start + last.added < start - last.added + last.old.length) |
|
2899 | ||
2073 | this.done.push({start: start, added: added, old: old}); |
|
2900 | if (this.compound && cur && !this.closed) { | |
2074 | else { |
|
2901 | cur.push({start: start, added: added, old: old}); | |
2075 | var oldoff = 0; |
|
2902 | } else if (dtime > 400 || !last || this.closed || | |
2076 | if (start < last.start) { |
|
2903 | last.start > start + old.length || last.start + last.added < start) { | |
2077 | for (var i = last.start - start - 1; i >= 0; --i) |
|
2904 | this.done.push([{start: start, added: added, old: old}]); | |
2078 | last.old.unshift(old[i]); |
|
2905 | this.closed = false; | |
2079 | last.added += last.start - start; |
|
2906 | } else { | |
2080 |
last.start |
|
2907 | var startBefore = Math.max(0, last.start - start), | |
2081 | } |
|
2908 | endAfter = Math.max(0, (start + old.length) - (last.start + last.added)); | |
2082 | else if (last.start < start) { |
|
2909 | for (var i = startBefore; i > 0; --i) last.old.unshift(old[i - 1]); | |
2083 | oldoff = start - last.start; |
|
2910 | for (var i = endAfter; i > 0; --i) last.old.push(old[old.length - i]); | |
2084 | added += oldoff; |
|
2911 | if (startBefore) last.start = start; | |
2085 | } |
|
2912 | last.added += added - (old.length - startBefore - endAfter); | |
2086 | for (var i = last.added - oldoff, e = old.length; i < e; ++i) |
|
|||
2087 | last.old.push(old[i]); |
|
|||
2088 | if (last.added < added) last.added = added; |
|
|||
2089 | } |
|
2913 | } | |
2090 | this.time = time; |
|
2914 | this.time = time; | |
|
2915 | }, | |||
|
2916 | startCompound: function() { | |||
|
2917 | if (!this.compound++) this.closed = true; | |||
|
2918 | }, | |||
|
2919 | endCompound: function() { | |||
|
2920 | if (!--this.compound) this.closed = true; | |||
2091 | } |
|
2921 | } | |
2092 | }; |
|
2922 | }; | |
2093 |
|
2923 | |||
@@ -2107,6 +2937,10 b' var CodeMirror = (function() {' | |||||
2107 | else e.cancelBubble = true; |
|
2937 | else e.cancelBubble = true; | |
2108 | } |
|
2938 | } | |
2109 | function e_stop(e) {e_preventDefault(e); e_stopPropagation(e);} |
|
2939 | function e_stop(e) {e_preventDefault(e); e_stopPropagation(e);} | |
|
2940 | CodeMirror.e_stop = e_stop; | |||
|
2941 | CodeMirror.e_preventDefault = e_preventDefault; | |||
|
2942 | CodeMirror.e_stopPropagation = e_stopPropagation; | |||
|
2943 | ||||
2110 | function e_target(e) {return e.target || e.srcElement;} |
|
2944 | function e_target(e) {return e.target || e.srcElement;} | |
2111 | function e_button(e) { |
|
2945 | function e_button(e) { | |
2112 | if (e.which) return e.which; |
|
2946 | if (e.which) return e.which; | |
@@ -2115,60 +2949,76 b' var CodeMirror = (function() {' | |||||
2115 | else if (e.button & 4) return 2; |
|
2949 | else if (e.button & 4) return 2; | |
2116 | } |
|
2950 | } | |
2117 |
|
2951 | |||
|
2952 | // Allow 3rd-party code to override event properties by adding an override | |||
|
2953 | // object to an event object. | |||
|
2954 | function e_prop(e, prop) { | |||
|
2955 | var overridden = e.override && e.override.hasOwnProperty(prop); | |||
|
2956 | return overridden ? e.override[prop] : e[prop]; | |||
|
2957 | } | |||
|
2958 | ||||
2118 | // Event handler registration. If disconnect is true, it'll return a |
|
2959 | // Event handler registration. If disconnect is true, it'll return a | |
2119 | // function that unregisters the handler. |
|
2960 | // function that unregisters the handler. | |
2120 | function connect(node, type, handler, disconnect) { |
|
2961 | function connect(node, type, handler, disconnect) { | |
2121 | function wrapHandler(event) {handler(event || window.event);} |
|
|||
2122 | if (typeof node.addEventListener == "function") { |
|
2962 | if (typeof node.addEventListener == "function") { | |
2123 |
node.addEventListener(type, |
|
2963 | node.addEventListener(type, handler, false); | |
2124 |
if (disconnect) return function() {node.removeEventListener(type, |
|
2964 | if (disconnect) return function() {node.removeEventListener(type, handler, false);}; | |
2125 | } |
|
2965 | } else { | |
2126 | else { |
|
2966 | var wrapHandler = function(event) {handler(event || window.event);}; | |
2127 | node.attachEvent("on" + type, wrapHandler); |
|
2967 | node.attachEvent("on" + type, wrapHandler); | |
2128 | if (disconnect) return function() {node.detachEvent("on" + type, wrapHandler);}; |
|
2968 | if (disconnect) return function() {node.detachEvent("on" + type, wrapHandler);}; | |
2129 | } |
|
2969 | } | |
2130 | } |
|
2970 | } | |
|
2971 | CodeMirror.connect = connect; | |||
2131 |
|
2972 | |||
2132 | function Delayed() {this.id = null;} |
|
2973 | function Delayed() {this.id = null;} | |
2133 | Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}}; |
|
2974 | Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}}; | |
2134 |
|
2975 | |||
2135 | // Some IE versions don't preserve whitespace when setting the |
|
2976 | var Pass = CodeMirror.Pass = {toString: function(){return "CodeMirror.Pass";}}; | |
2136 | // innerHTML of a PRE tag. |
|
|||
2137 | var badInnerHTML = (function() { |
|
|||
2138 | var pre = document.createElement("pre"); |
|
|||
2139 | pre.innerHTML = " "; return !pre.innerHTML; |
|
|||
2140 | })(); |
|
|||
2141 |
|
||||
2142 | // Detect drag-and-drop |
|
|||
2143 | var dragAndDrop = (function() { |
|
|||
2144 | // IE8 has ondragstart and ondrop properties, but doesn't seem to |
|
|||
2145 | // actually support ondragstart the way it's supposed to work. |
|
|||
2146 | if (/MSIE [1-8]\b/.test(navigator.userAgent)) return false; |
|
|||
2147 | var div = document.createElement('div'); |
|
|||
2148 | return "ondragstart" in div && "ondrop" in div; |
|
|||
2149 | })(); |
|
|||
2150 |
|
2977 | |||
2151 | var gecko = /gecko\/\d{7}/i.test(navigator.userAgent); |
|
2978 | var gecko = /gecko\/\d{7}/i.test(navigator.userAgent); | |
2152 | var ie = /MSIE \d/.test(navigator.userAgent); |
|
2979 | var ie = /MSIE \d/.test(navigator.userAgent); | |
|
2980 | var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent); | |||
|
2981 | var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent); | |||
|
2982 | var quirksMode = ie && document.documentMode == 5; | |||
|
2983 | var webkit = /WebKit\//.test(navigator.userAgent); | |||
|
2984 | var chrome = /Chrome\//.test(navigator.userAgent); | |||
|
2985 | var opera = /Opera\//.test(navigator.userAgent); | |||
2153 | var safari = /Apple Computer/.test(navigator.vendor); |
|
2986 | var safari = /Apple Computer/.test(navigator.vendor); | |
|
2987 | var khtml = /KHTML\//.test(navigator.userAgent); | |||
|
2988 | var mac_geLion = /Mac OS X 10\D([7-9]|\d\d)\D/.test(navigator.userAgent); | |||
2154 |
|
2989 | |||
2155 | var lineSep = "\n"; |
|
2990 | // Detect drag-and-drop | |
|
2991 | var dragAndDrop = function() { | |||
|
2992 | // There is *some* kind of drag-and-drop support in IE6-8, but I | |||
|
2993 | // couldn't get it to work yet. | |||
|
2994 | if (ie_lt9) return false; | |||
|
2995 | var div = document.createElement('div'); | |||
|
2996 | return "draggable" in div || "dragDrop" in div; | |||
|
2997 | }(); | |||
|
2998 | ||||
2156 | // Feature-detect whether newlines in textareas are converted to \r\n |
|
2999 | // Feature-detect whether newlines in textareas are converted to \r\n | |
2157 |
|
|
3000 | var lineSep = function () { | |
2158 | var te = document.createElement("textarea"); |
|
3001 | var te = document.createElement("textarea"); | |
2159 | te.value = "foo\nbar"; |
|
3002 | te.value = "foo\nbar"; | |
2160 |
if (te.value.indexOf("\r") > -1) |
|
3003 | if (te.value.indexOf("\r") > -1) return "\r\n"; | |
2161 | }()); |
|
3004 | return "\n"; | |
|
3005 | }(); | |||
2162 |
|
3006 | |||
2163 | var tabSize = 8; |
|
3007 | // For a reason I have yet to figure out, some browsers disallow | |
2164 | var mac = /Mac/.test(navigator.platform); |
|
3008 | // word wrapping between certain characters *only* if a new inline | |
2165 | var movementKeys = {}; |
|
3009 | // element is started between them. This makes it hard to reliably | |
2166 | for (var i = 35; i <= 40; ++i) |
|
3010 | // measure the position of things, since that requires inserting an | |
2167 | movementKeys[i] = movementKeys["c" + i] = true; |
|
3011 | // extra span. This terribly fragile set of regexps matches the | |
|
3012 | // character combinations that suffer from this phenomenon on the | |||
|
3013 | // various browsers. | |||
|
3014 | var spanAffectsWrapping = /^$/; // Won't match any two-character string | |||
|
3015 | if (gecko) spanAffectsWrapping = /$'/; | |||
|
3016 | else if (safari) spanAffectsWrapping = /\-[^ \-?]|\?[^ !'\"\),.\-\/:;\?\]\}]/; | |||
|
3017 | else if (chrome) spanAffectsWrapping = /\-[^ \-\.?]|\?[^ \-\.?\]\}:;!'\"\),\/]|[\.!\"#&%\)*+,:;=>\]|\}~][\(\{\[<]|\$'/; | |||
2168 |
|
3018 | |||
2169 | // Counts the column offset in a string, taking tabs into account. |
|
3019 | // Counts the column offset in a string, taking tabs into account. | |
2170 | // Used mostly to find indentation. |
|
3020 | // Used mostly to find indentation. | |
2171 | function countColumn(string, end) { |
|
3021 | function countColumn(string, end, tabSize) { | |
2172 | if (end == null) { |
|
3022 | if (end == null) { | |
2173 | end = string.search(/[^\s\u00a0]/); |
|
3023 | end = string.search(/[^\s\u00a0]/); | |
2174 | if (end == -1) end = string.length; |
|
3024 | if (end == -1) end = string.length; | |
@@ -2184,25 +3034,54 b' var CodeMirror = (function() {' | |||||
2184 | if (elt.currentStyle) return elt.currentStyle; |
|
3034 | if (elt.currentStyle) return elt.currentStyle; | |
2185 | return window.getComputedStyle(elt, null); |
|
3035 | return window.getComputedStyle(elt, null); | |
2186 | } |
|
3036 | } | |
|
3037 | ||||
2187 | // Find the position of an element by following the offsetParent chain. |
|
3038 | // Find the position of an element by following the offsetParent chain. | |
2188 | // If screen==true, it returns screen (rather than page) coordinates. |
|
3039 | // If screen==true, it returns screen (rather than page) coordinates. | |
2189 | function eltOffset(node, screen) { |
|
3040 | function eltOffset(node, screen) { | |
2190 |
var |
|
3041 | var bod = node.ownerDocument.body; | |
2191 |
var x = 0, y = 0, skip |
|
3042 | var x = 0, y = 0, skipBody = false; | |
2192 | for (var n = node; n; n = n.offsetParent) { |
|
3043 | for (var n = node; n; n = n.offsetParent) { | |
2193 |
|
|
3044 | var ol = n.offsetLeft, ot = n.offsetTop; | |
|
3045 | // Firefox reports weird inverted offsets when the body has a border. | |||
|
3046 | if (n == bod) { x += Math.abs(ol); y += Math.abs(ot); } | |||
|
3047 | else { x += ol, y += ot; } | |||
2194 | if (screen && computedStyle(n).position == "fixed") |
|
3048 | if (screen && computedStyle(n).position == "fixed") | |
2195 |
skip |
|
3049 | skipBody = true; | |
2196 | } |
|
3050 | } | |
2197 |
var e = screen && !skip |
|
3051 | var e = screen && !skipBody ? null : bod; | |
2198 | for (var n = node.parentNode; n != e; n = n.parentNode) |
|
3052 | for (var n = node.parentNode; n != e; n = n.parentNode) | |
2199 | if (n.scrollLeft != null) { x -= n.scrollLeft; y -= n.scrollTop;} |
|
3053 | if (n.scrollLeft != null) { x -= n.scrollLeft; y -= n.scrollTop;} | |
2200 | return {left: x, top: y}; |
|
3054 | return {left: x, top: y}; | |
2201 | } |
|
3055 | } | |
|
3056 | // Use the faster and saner getBoundingClientRect method when possible. | |||
|
3057 | if (document.documentElement.getBoundingClientRect != null) eltOffset = function(node, screen) { | |||
|
3058 | // Take the parts of bounding client rect that we are interested in so we are able to edit if need be, | |||
|
3059 | // since the returned value cannot be changed externally (they are kept in sync as the element moves within the page) | |||
|
3060 | try { var box = node.getBoundingClientRect(); box = { top: box.top, left: box.left }; } | |||
|
3061 | catch(e) { box = {top: 0, left: 0}; } | |||
|
3062 | if (!screen) { | |||
|
3063 | // Get the toplevel scroll, working around browser differences. | |||
|
3064 | if (window.pageYOffset == null) { | |||
|
3065 | var t = document.documentElement || document.body.parentNode; | |||
|
3066 | if (t.scrollTop == null) t = document.body; | |||
|
3067 | box.top += t.scrollTop; box.left += t.scrollLeft; | |||
|
3068 | } else { | |||
|
3069 | box.top += window.pageYOffset; box.left += window.pageXOffset; | |||
|
3070 | } | |||
|
3071 | } | |||
|
3072 | return box; | |||
|
3073 | }; | |||
|
3074 | ||||
2202 | // Get a node's text content. |
|
3075 | // Get a node's text content. | |
2203 | function eltText(node) { |
|
3076 | function eltText(node) { | |
2204 | return node.textContent || node.innerText || node.nodeValue || ""; |
|
3077 | return node.textContent || node.innerText || node.nodeValue || ""; | |
2205 | } |
|
3078 | } | |
|
3079 | function selectInput(node) { | |||
|
3080 | if (ios) { // Mobile Safari apparently has a bug where select() is broken. | |||
|
3081 | node.selectionStart = 0; | |||
|
3082 | node.selectionEnd = node.value.length; | |||
|
3083 | } else node.select(); | |||
|
3084 | } | |||
2206 |
|
3085 | |||
2207 | // Operations on {line, ch} objects. |
|
3086 | // Operations on {line, ch} objects. | |
2208 | function posEq(a, b) {return a.line == b.line && a.ch == b.ch;} |
|
3087 | function posEq(a, b) {return a.line == b.line && a.ch == b.ch;} | |
@@ -2211,21 +3090,30 b' var CodeMirror = (function() {' | |||||
2211 |
|
3090 | |||
2212 | var escapeElement = document.createElement("pre"); |
|
3091 | var escapeElement = document.createElement("pre"); | |
2213 | function htmlEscape(str) { |
|
3092 | function htmlEscape(str) { | |
2214 | if (badTextContent) { |
|
3093 | escapeElement.textContent = str; | |
|
3094 | return escapeElement.innerHTML; | |||
|
3095 | } | |||
|
3096 | // Recent (late 2011) Opera betas insert bogus newlines at the start | |||
|
3097 | // of the textContent, so we strip those. | |||
|
3098 | if (htmlEscape("a") == "\na") { | |||
|
3099 | htmlEscape = function(str) { | |||
|
3100 | escapeElement.textContent = str; | |||
|
3101 | return escapeElement.innerHTML.slice(1); | |||
|
3102 | }; | |||
|
3103 | // Some IEs don't preserve tabs through innerHTML | |||
|
3104 | } else if (htmlEscape("\t") != "\t") { | |||
|
3105 | htmlEscape = function(str) { | |||
2215 | escapeElement.innerHTML = ""; |
|
3106 | escapeElement.innerHTML = ""; | |
2216 | escapeElement.appendChild(document.createTextNode(str)); |
|
3107 | escapeElement.appendChild(document.createTextNode(str)); | |
2217 | } else { |
|
3108 | return escapeElement.innerHTML; | |
2218 | escapeElement.textContent = str; |
|
3109 | }; | |
2219 | } |
|
|||
2220 | return escapeElement.innerHTML; |
|
|||
2221 | } |
|
3110 | } | |
2222 | var badTextContent = htmlEscape("\t") != "\t"; |
|
|||
2223 | CodeMirror.htmlEscape = htmlEscape; |
|
3111 | CodeMirror.htmlEscape = htmlEscape; | |
2224 |
|
3112 | |||
2225 | // Used to position the cursor after an undo/redo by finding the |
|
3113 | // Used to position the cursor after an undo/redo by finding the | |
2226 | // last edited character. |
|
3114 | // last edited character. | |
2227 | function editEnd(from, to) { |
|
3115 | function editEnd(from, to) { | |
2228 |
if (!to) return |
|
3116 | if (!to) return 0; | |
2229 | if (!from) return to.length; |
|
3117 | if (!from) return to.length; | |
2230 | for (var i = from.length, j = to.length; i >= 0 && j >= 0; --i, --j) |
|
3118 | for (var i = from.length, j = to.length; i >= 0 && j >= 0; --i, --j) | |
2231 | if (from.charAt(i) != to.charAt(j)) break; |
|
3119 | if (from.charAt(i) != to.charAt(j)) break; | |
@@ -2238,95 +3126,54 b' var CodeMirror = (function() {' | |||||
2238 | if (collection[i] == elt) return i; |
|
3126 | if (collection[i] == elt) return i; | |
2239 | return -1; |
|
3127 | return -1; | |
2240 | } |
|
3128 | } | |
|
3129 | function isWordChar(ch) { | |||
|
3130 | return /\w/.test(ch) || ch.toUpperCase() != ch.toLowerCase(); | |||
|
3131 | } | |||
2241 |
|
3132 | |||
2242 | // See if "".split is the broken IE version, if so, provide an |
|
3133 | // See if "".split is the broken IE version, if so, provide an | |
2243 | // alternative way to split lines. |
|
3134 | // alternative way to split lines. | |
2244 | var splitLines, selRange, setSelRange; |
|
3135 | var splitLines = "\n\nb".split(/\n/).length != 3 ? function(string) { | |
2245 | if ("\n\nb".split(/\n/).length != 3) |
|
3136 | var pos = 0, nl, result = []; | |
2246 | splitLines = function(string) { |
|
3137 | while ((nl = string.indexOf("\n", pos)) > -1) { | |
2247 | var pos = 0, nl, result = []; |
|
3138 | result.push(string.slice(pos, string.charAt(nl-1) == "\r" ? nl - 1 : nl)); | |
2248 | while ((nl = string.indexOf("\n", pos)) > -1) { |
|
3139 | pos = nl + 1; | |
2249 | result.push(string.slice(pos, string.charAt(nl-1) == "\r" ? nl - 1 : nl)); |
|
3140 | } | |
2250 | pos = nl + 1; |
|
3141 | result.push(string.slice(pos)); | |
2251 | } |
|
3142 | return result; | |
2252 | result.push(string.slice(pos)); |
|
3143 | } : function(string){return string.split(/\r?\n/);}; | |
2253 | return result; |
|
|||
2254 | }; |
|
|||
2255 | else |
|
|||
2256 | splitLines = function(string){return string.split(/\r?\n/);}; |
|
|||
2257 | CodeMirror.splitLines = splitLines; |
|
3144 | CodeMirror.splitLines = splitLines; | |
2258 |
|
3145 | |||
2259 | // Sane model of finding and setting the selection in a textarea |
|
3146 | var hasSelection = window.getSelection ? function(te) { | |
2260 | if (window.getSelection) { |
|
3147 | try { return te.selectionStart != te.selectionEnd; } | |
2261 | selRange = function(te) { |
|
3148 | catch(e) { return false; } | |
2262 | try {return {start: te.selectionStart, end: te.selectionEnd};} |
|
3149 | } : function(te) { | |
2263 | catch(e) {return null;} |
|
3150 | try {var range = te.ownerDocument.selection.createRange();} | |
2264 | }; |
|
3151 | catch(e) {} | |
2265 | if (safari) |
|
3152 | if (!range || range.parentElement() != te) return false; | |
2266 | // On Safari, selection set with setSelectionRange are in a sort |
|
3153 | return range.compareEndPoints("StartToEnd", range) != 0; | |
2267 | // of limbo wrt their anchor. If you press shift-left in them, |
|
3154 | }; | |
2268 | // the anchor is put at the end, and the selection expanded to |
|
|||
2269 | // the left. If you press shift-right, the anchor ends up at the |
|
|||
2270 | // front. This is not what CodeMirror wants, so it does a |
|
|||
2271 | // spurious modify() call to get out of limbo. |
|
|||
2272 | setSelRange = function(te, start, end) { |
|
|||
2273 | if (start == end) |
|
|||
2274 | te.setSelectionRange(start, end); |
|
|||
2275 | else { |
|
|||
2276 | te.setSelectionRange(start, end - 1); |
|
|||
2277 | window.getSelection().modify("extend", "forward", "character"); |
|
|||
2278 | } |
|
|||
2279 | }; |
|
|||
2280 | else |
|
|||
2281 | setSelRange = function(te, start, end) { |
|
|||
2282 | try {te.setSelectionRange(start, end);} |
|
|||
2283 | catch(e) {} // Fails on Firefox when textarea isn't part of the document |
|
|||
2284 | }; |
|
|||
2285 | } |
|
|||
2286 | // IE model. Don't ask. |
|
|||
2287 | else { |
|
|||
2288 | selRange = function(te) { |
|
|||
2289 | try {var range = te.ownerDocument.selection.createRange();} |
|
|||
2290 | catch(e) {return null;} |
|
|||
2291 | if (!range || range.parentElement() != te) return null; |
|
|||
2292 | var val = te.value, len = val.length, localRange = te.createTextRange(); |
|
|||
2293 | localRange.moveToBookmark(range.getBookmark()); |
|
|||
2294 | var endRange = te.createTextRange(); |
|
|||
2295 | endRange.collapse(false); |
|
|||
2296 |
|
||||
2297 | if (localRange.compareEndPoints("StartToEnd", endRange) > -1) |
|
|||
2298 | return {start: len, end: len}; |
|
|||
2299 |
|
||||
2300 | var start = -localRange.moveStart("character", -len); |
|
|||
2301 | for (var i = val.indexOf("\r"); i > -1 && i < start; i = val.indexOf("\r", i+1), start++) {} |
|
|||
2302 |
|
||||
2303 | if (localRange.compareEndPoints("EndToEnd", endRange) > -1) |
|
|||
2304 | return {start: start, end: len}; |
|
|||
2305 |
|
||||
2306 | var end = -localRange.moveEnd("character", -len); |
|
|||
2307 | for (var i = val.indexOf("\r"); i > -1 && i < end; i = val.indexOf("\r", i+1), end++) {} |
|
|||
2308 | return {start: start, end: end}; |
|
|||
2309 | }; |
|
|||
2310 | setSelRange = function(te, start, end) { |
|
|||
2311 | var range = te.createTextRange(); |
|
|||
2312 | range.collapse(true); |
|
|||
2313 | var endrange = range.duplicate(); |
|
|||
2314 | var newlines = 0, txt = te.value; |
|
|||
2315 | for (var pos = txt.indexOf("\n"); pos > -1 && pos < start; pos = txt.indexOf("\n", pos + 1)) |
|
|||
2316 | ++newlines; |
|
|||
2317 | range.move("character", start - newlines); |
|
|||
2318 | for (; pos > -1 && pos < end; pos = txt.indexOf("\n", pos + 1)) |
|
|||
2319 | ++newlines; |
|
|||
2320 | endrange.move("character", end - newlines); |
|
|||
2321 | range.setEndPoint("EndToEnd", endrange); |
|
|||
2322 | range.select(); |
|
|||
2323 | }; |
|
|||
2324 | } |
|
|||
2325 |
|
3155 | |||
2326 | CodeMirror.defineMode("null", function() { |
|
3156 | CodeMirror.defineMode("null", function() { | |
2327 | return {token: function(stream) {stream.skipToEnd();}}; |
|
3157 | return {token: function(stream) {stream.skipToEnd();}}; | |
2328 | }); |
|
3158 | }); | |
2329 | CodeMirror.defineMIME("text/plain", "null"); |
|
3159 | CodeMirror.defineMIME("text/plain", "null"); | |
2330 |
|
3160 | |||
|
3161 | var keyNames = {3: "Enter", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt", | |||
|
3162 | 19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", | |||
|
3163 | 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert", | |||
|
3164 | 46: "Delete", 59: ";", 91: "Mod", 92: "Mod", 93: "Mod", 109: "-", 107: "=", 127: "Delete", | |||
|
3165 | 186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\", | |||
|
3166 | 221: "]", 222: "'", 63276: "PageUp", 63277: "PageDown", 63275: "End", 63273: "Home", | |||
|
3167 | 63234: "Left", 63232: "Up", 63235: "Right", 63233: "Down", 63302: "Insert", 63272: "Delete"}; | |||
|
3168 | CodeMirror.keyNames = keyNames; | |||
|
3169 | (function() { | |||
|
3170 | // Number keys | |||
|
3171 | for (var i = 0; i < 10; i++) keyNames[i + 48] = String(i); | |||
|
3172 | // Alphabetic keys | |||
|
3173 | for (var i = 65; i <= 90; i++) keyNames[i] = String.fromCharCode(i); | |||
|
3174 | // Function keys | |||
|
3175 | for (var i = 1; i <= 12; i++) keyNames[i + 111] = keyNames[i + 63235] = "F" + i; | |||
|
3176 | })(); | |||
|
3177 | ||||
2331 | return CodeMirror; |
|
3178 | return CodeMirror; | |
2332 | })(); |
|
3179 | })(); |
General Comments 0
You need to be logged in to leave comments.
Login now