annotate rhodecode/public/js/codemirror.js @ 1606:a30689fc4f61 beta

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