Show More
@@ -1,439 +1,440 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
2 | // Copyright (C) 2008-2011 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // CodeCell |
|
9 | // CodeCell | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
12 | * An extendable module that provide base functionnality to create cell for notebook. | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule CodeCell |
|
15 | * @submodule CodeCell | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | /* local util for codemirror */ |
|
19 | /* local util for codemirror */ | |
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} |
|
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} | |
21 |
|
21 | |||
22 | /** |
|
22 | /** | |
23 | * |
|
23 | * | |
24 | * function to delete until previous non blanking space character |
|
24 | * function to delete until previous non blanking space character | |
25 | * or first multiple of 4 tabstop. |
|
25 | * or first multiple of 4 tabstop. | |
26 | * @private |
|
26 | * @private | |
27 | */ |
|
27 | */ | |
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ |
|
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ | |
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); |
|
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); | |
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} |
|
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} | |
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); |
|
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); | |
32 | var tabsize = cm.getOption('tabSize'); |
|
32 | var tabsize = cm.getOption('tabSize'); | |
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; |
|
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; | |
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} |
|
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} | |
35 | var select = cm.getRange(from,cur) |
|
35 | var select = cm.getRange(from,cur) | |
36 | if( select.match(/^\ +$/) != null){ |
|
36 | if( select.match(/^\ +$/) != null){ | |
37 | cm.replaceRange("",from,cur) |
|
37 | cm.replaceRange("",from,cur) | |
38 | } else { |
|
38 | } else { | |
39 | cm.deleteH(-1,"char") |
|
39 | cm.deleteH(-1,"char") | |
40 | } |
|
40 | } | |
41 | }; |
|
41 | }; | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | var IPython = (function (IPython) { |
|
44 | var IPython = (function (IPython) { | |
45 | "use strict"; |
|
45 | "use strict"; | |
46 |
|
46 | |||
47 | var utils = IPython.utils; |
|
47 | var utils = IPython.utils; | |
48 | var key = IPython.utils.keycodes; |
|
48 | var key = IPython.utils.keycodes; | |
49 | CodeMirror.modeURL = "/static/components/codemirror/mode/%N/%N.js"; |
|
49 | CodeMirror.modeURL = "/static/components/codemirror/mode/%N/%N.js"; | |
50 |
|
50 | |||
51 | /** |
|
51 | /** | |
52 | * A Cell conceived to write code. |
|
52 | * A Cell conceived to write code. | |
53 | * |
|
53 | * | |
54 | * The kernel doesn't have to be set at creation time, in that case |
|
54 | * The kernel doesn't have to be set at creation time, in that case | |
55 | * it will be null and set_kernel has to be called later. |
|
55 | * it will be null and set_kernel has to be called later. | |
56 | * @class CodeCell |
|
56 | * @class CodeCell | |
57 | * @extends IPython.Cell |
|
57 | * @extends IPython.Cell | |
58 | * |
|
58 | * | |
59 | * @constructor |
|
59 | * @constructor | |
60 | * @param {Object|null} kernel |
|
60 | * @param {Object|null} kernel | |
61 | * @param {object|undefined} [options] |
|
61 | * @param {object|undefined} [options] | |
62 | * @param [options.cm_config] {object} config to pass to CodeMirror |
|
62 | * @param [options.cm_config] {object} config to pass to CodeMirror | |
63 | */ |
|
63 | */ | |
64 | var CodeCell = function (kernel, options) { |
|
64 | var CodeCell = function (kernel, options) { | |
65 | this.kernel = kernel || null; |
|
65 | this.kernel = kernel || null; | |
66 | this.code_mirror = null; |
|
66 | this.code_mirror = null; | |
67 | this.input_prompt_number = null; |
|
67 | this.input_prompt_number = null; | |
68 | this.collapsed = false; |
|
68 | this.collapsed = false; | |
69 | this.default_mode = 'ipython'; |
|
69 | this.default_mode = 'ipython'; | |
|
70 | this.cell_type = "code"; | |||
70 |
|
71 | |||
71 |
|
72 | |||
72 | var cm_overwrite_options = { |
|
73 | var cm_overwrite_options = { | |
73 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
74 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
74 | }; |
|
75 | }; | |
75 |
|
76 | |||
76 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); |
|
77 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); | |
77 |
|
78 | |||
78 | IPython.Cell.apply(this,[options]); |
|
79 | IPython.Cell.apply(this,[options]); | |
79 |
|
80 | |||
80 | var that = this; |
|
81 | var that = this; | |
81 | this.element.focusout( |
|
82 | this.element.focusout( | |
82 | function() { that.auto_highlight(); } |
|
83 | function() { that.auto_highlight(); } | |
83 | ); |
|
84 | ); | |
84 | }; |
|
85 | }; | |
85 |
|
86 | |||
86 | CodeCell.options_default = { |
|
87 | CodeCell.options_default = { | |
87 | cm_config : { |
|
88 | cm_config : { | |
88 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"}, |
|
89 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"}, | |
89 | mode: 'ipython', |
|
90 | mode: 'ipython', | |
90 | theme: 'ipython', |
|
91 | theme: 'ipython', | |
91 | matchBrackets: true |
|
92 | matchBrackets: true | |
92 | } |
|
93 | } | |
93 | }; |
|
94 | }; | |
94 |
|
95 | |||
95 |
|
96 | |||
96 | CodeCell.prototype = new IPython.Cell(); |
|
97 | CodeCell.prototype = new IPython.Cell(); | |
97 |
|
98 | |||
98 | /** |
|
99 | /** | |
99 | * @method auto_highlight |
|
100 | * @method auto_highlight | |
100 | */ |
|
101 | */ | |
101 | CodeCell.prototype.auto_highlight = function () { |
|
102 | CodeCell.prototype.auto_highlight = function () { | |
102 | this._auto_highlight(IPython.config.cell_magic_highlight) |
|
103 | this._auto_highlight(IPython.config.cell_magic_highlight) | |
103 | }; |
|
104 | }; | |
104 |
|
105 | |||
105 | /** @method create_element */ |
|
106 | /** @method create_element */ | |
106 | CodeCell.prototype.create_element = function () { |
|
107 | CodeCell.prototype.create_element = function () { | |
107 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
108 | IPython.Cell.prototype.create_element.apply(this, arguments); | |
108 |
|
109 | |||
109 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); |
|
110 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); | |
110 | cell.attr('tabindex','2'); |
|
111 | cell.attr('tabindex','2'); | |
111 |
|
112 | |||
112 | this.celltoolbar = new IPython.CellToolbar(this); |
|
113 | this.celltoolbar = new IPython.CellToolbar(this); | |
113 |
|
114 | |||
114 | var input = $('<div></div>').addClass('input'); |
|
115 | var input = $('<div></div>').addClass('input'); | |
115 | var vbox = $('<div/>').addClass('vbox box-flex1') |
|
116 | var vbox = $('<div/>').addClass('vbox box-flex1') | |
116 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
117 | input.append($('<div/>').addClass('prompt input_prompt')); | |
117 | vbox.append(this.celltoolbar.element); |
|
118 | vbox.append(this.celltoolbar.element); | |
118 | var input_area = $('<div/>').addClass('input_area'); |
|
119 | var input_area = $('<div/>').addClass('input_area'); | |
119 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); |
|
120 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); | |
120 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); |
|
121 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); | |
121 | vbox.append(input_area); |
|
122 | vbox.append(input_area); | |
122 | input.append(vbox); |
|
123 | input.append(vbox); | |
123 | var output = $('<div></div>'); |
|
124 | var output = $('<div></div>'); | |
124 | cell.append(input).append(output); |
|
125 | cell.append(input).append(output); | |
125 | this.element = cell; |
|
126 | this.element = cell; | |
126 | this.output_area = new IPython.OutputArea(output, true); |
|
127 | this.output_area = new IPython.OutputArea(output, true); | |
127 |
|
128 | |||
128 | // construct a completer only if class exist |
|
129 | // construct a completer only if class exist | |
129 | // otherwise no print view |
|
130 | // otherwise no print view | |
130 | if (IPython.Completer !== undefined) |
|
131 | if (IPython.Completer !== undefined) | |
131 | { |
|
132 | { | |
132 | this.completer = new IPython.Completer(this); |
|
133 | this.completer = new IPython.Completer(this); | |
133 | } |
|
134 | } | |
134 | }; |
|
135 | }; | |
135 |
|
136 | |||
136 | /** |
|
137 | /** | |
137 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
138 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
138 | * handlers and is used to provide custom key handling. Its return |
|
139 | * handlers and is used to provide custom key handling. Its return | |
139 | * value is used to determine if CodeMirror should ignore the event: |
|
140 | * value is used to determine if CodeMirror should ignore the event: | |
140 | * true = ignore, false = don't ignore. |
|
141 | * true = ignore, false = don't ignore. | |
141 | * @method handle_codemirror_keyevent |
|
142 | * @method handle_codemirror_keyevent | |
142 | */ |
|
143 | */ | |
143 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
144 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
144 |
|
145 | |||
145 | if (this.read_only){ |
|
146 | if (this.read_only){ | |
146 | return false; |
|
147 | return false; | |
147 | } |
|
148 | } | |
148 |
|
149 | |||
149 | var that = this; |
|
150 | var that = this; | |
150 | // whatever key is pressed, first, cancel the tooltip request before |
|
151 | // whatever key is pressed, first, cancel the tooltip request before | |
151 | // they are sent, and remove tooltip if any, except for tab again |
|
152 | // they are sent, and remove tooltip if any, except for tab again | |
152 | if (event.type === 'keydown' && event.which != key.TAB ) { |
|
153 | if (event.type === 'keydown' && event.which != key.TAB ) { | |
153 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
154 | IPython.tooltip.remove_and_cancel_tooltip(); | |
154 | }; |
|
155 | }; | |
155 |
|
156 | |||
156 | var cur = editor.getCursor(); |
|
157 | var cur = editor.getCursor(); | |
157 | if (event.keyCode === key.ENTER){ |
|
158 | if (event.keyCode === key.ENTER){ | |
158 | this.auto_highlight(); |
|
159 | this.auto_highlight(); | |
159 | } |
|
160 | } | |
160 |
|
161 | |||
161 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { |
|
162 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { | |
162 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
163 | // Always ignore shift-enter in CodeMirror as we handle it. | |
163 | return true; |
|
164 | return true; | |
164 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
|
165 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | |
165 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
166 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform | |
166 | // browser and keyboard layout ! |
|
167 | // browser and keyboard layout ! | |
167 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
168 | // Pressing '(' , request tooltip, don't forget to reappend it | |
168 | IPython.tooltip.pending(that); |
|
169 | IPython.tooltip.pending(that); | |
169 | } else if (event.which === key.UPARROW && event.type === 'keydown') { |
|
170 | } else if (event.which === key.UPARROW && event.type === 'keydown') { | |
170 | // If we are not at the top, let CM handle the up arrow and |
|
171 | // If we are not at the top, let CM handle the up arrow and | |
171 | // prevent the global keydown handler from handling it. |
|
172 | // prevent the global keydown handler from handling it. | |
172 | if (!that.at_top()) { |
|
173 | if (!that.at_top()) { | |
173 | event.stop(); |
|
174 | event.stop(); | |
174 | return false; |
|
175 | return false; | |
175 | } else { |
|
176 | } else { | |
176 | return true; |
|
177 | return true; | |
177 | }; |
|
178 | }; | |
178 | } else if (event.which === key.ESC) { |
|
179 | } else if (event.which === key.ESC) { | |
179 | IPython.tooltip.remove_and_cancel_tooltip(true); |
|
180 | IPython.tooltip.remove_and_cancel_tooltip(true); | |
180 | return true; |
|
181 | return true; | |
181 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
182 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
182 | // If we are not at the bottom, let CM handle the down arrow and |
|
183 | // If we are not at the bottom, let CM handle the down arrow and | |
183 | // prevent the global keydown handler from handling it. |
|
184 | // prevent the global keydown handler from handling it. | |
184 | if (!that.at_bottom()) { |
|
185 | if (!that.at_bottom()) { | |
185 | event.stop(); |
|
186 | event.stop(); | |
186 | return false; |
|
187 | return false; | |
187 | } else { |
|
188 | } else { | |
188 | return true; |
|
189 | return true; | |
189 | }; |
|
190 | }; | |
190 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { |
|
191 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { | |
191 | if (editor.somethingSelected()){ |
|
192 | if (editor.somethingSelected()){ | |
192 | var anchor = editor.getCursor("anchor"); |
|
193 | var anchor = editor.getCursor("anchor"); | |
193 | var head = editor.getCursor("head"); |
|
194 | var head = editor.getCursor("head"); | |
194 | if( anchor.line != head.line){ |
|
195 | if( anchor.line != head.line){ | |
195 | return false; |
|
196 | return false; | |
196 | } |
|
197 | } | |
197 | } |
|
198 | } | |
198 | IPython.tooltip.request(that); |
|
199 | IPython.tooltip.request(that); | |
199 | event.stop(); |
|
200 | event.stop(); | |
200 | return true; |
|
201 | return true; | |
201 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { |
|
202 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { | |
202 | // Tab completion. |
|
203 | // Tab completion. | |
203 | //Do not trim here because of tooltip |
|
204 | //Do not trim here because of tooltip | |
204 | if (editor.somethingSelected()){return false} |
|
205 | if (editor.somethingSelected()){return false} | |
205 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
206 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | |
206 | if (pre_cursor.trim() === "") { |
|
207 | if (pre_cursor.trim() === "") { | |
207 | // Don't autocomplete if the part of the line before the cursor |
|
208 | // Don't autocomplete if the part of the line before the cursor | |
208 | // is empty. In this case, let CodeMirror handle indentation. |
|
209 | // is empty. In this case, let CodeMirror handle indentation. | |
209 | return false; |
|
210 | return false; | |
210 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { |
|
211 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { | |
211 | IPython.tooltip.request(that); |
|
212 | IPython.tooltip.request(that); | |
212 | // Prevent the event from bubbling up. |
|
213 | // Prevent the event from bubbling up. | |
213 | event.stop(); |
|
214 | event.stop(); | |
214 | // Prevent CodeMirror from handling the tab. |
|
215 | // Prevent CodeMirror from handling the tab. | |
215 | return true; |
|
216 | return true; | |
216 | } else { |
|
217 | } else { | |
217 | event.stop(); |
|
218 | event.stop(); | |
218 | this.completer.startCompletion(); |
|
219 | this.completer.startCompletion(); | |
219 | return true; |
|
220 | return true; | |
220 | }; |
|
221 | }; | |
221 | } else { |
|
222 | } else { | |
222 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
223 | // keypress/keyup also trigger on TAB press, and we don't want to | |
223 | // use those to disable tab completion. |
|
224 | // use those to disable tab completion. | |
224 | return false; |
|
225 | return false; | |
225 | }; |
|
226 | }; | |
226 | return false; |
|
227 | return false; | |
227 | }; |
|
228 | }; | |
228 |
|
229 | |||
229 |
|
230 | |||
230 | // Kernel related calls. |
|
231 | // Kernel related calls. | |
231 |
|
232 | |||
232 | CodeCell.prototype.set_kernel = function (kernel) { |
|
233 | CodeCell.prototype.set_kernel = function (kernel) { | |
233 | this.kernel = kernel; |
|
234 | this.kernel = kernel; | |
234 | } |
|
235 | } | |
235 |
|
236 | |||
236 | /** |
|
237 | /** | |
237 | * Execute current code cell to the kernel |
|
238 | * Execute current code cell to the kernel | |
238 | * @method execute |
|
239 | * @method execute | |
239 | */ |
|
240 | */ | |
240 | CodeCell.prototype.execute = function () { |
|
241 | CodeCell.prototype.execute = function () { | |
241 | this.output_area.clear_output(true, true, true); |
|
242 | this.output_area.clear_output(true, true, true); | |
242 | this.set_input_prompt('*'); |
|
243 | this.set_input_prompt('*'); | |
243 | this.element.addClass("running"); |
|
244 | this.element.addClass("running"); | |
244 | var callbacks = { |
|
245 | var callbacks = { | |
245 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
246 | 'execute_reply': $.proxy(this._handle_execute_reply, this), | |
246 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
247 | 'output': $.proxy(this.output_area.handle_output, this.output_area), | |
247 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
248 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), | |
248 | 'set_next_input': $.proxy(this._handle_set_next_input, this), |
|
249 | 'set_next_input': $.proxy(this._handle_set_next_input, this), | |
249 | 'input_request': $.proxy(this._handle_input_request, this) |
|
250 | 'input_request': $.proxy(this._handle_input_request, this) | |
250 | }; |
|
251 | }; | |
251 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); |
|
252 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); | |
252 | }; |
|
253 | }; | |
253 |
|
254 | |||
254 | /** |
|
255 | /** | |
255 | * @method _handle_execute_reply |
|
256 | * @method _handle_execute_reply | |
256 | * @private |
|
257 | * @private | |
257 | */ |
|
258 | */ | |
258 | CodeCell.prototype._handle_execute_reply = function (content) { |
|
259 | CodeCell.prototype._handle_execute_reply = function (content) { | |
259 | this.set_input_prompt(content.execution_count); |
|
260 | this.set_input_prompt(content.execution_count); | |
260 | this.element.removeClass("running"); |
|
261 | this.element.removeClass("running"); | |
261 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); |
|
262 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); | |
262 | } |
|
263 | } | |
263 |
|
264 | |||
264 | /** |
|
265 | /** | |
265 | * @method _handle_set_next_input |
|
266 | * @method _handle_set_next_input | |
266 | * @private |
|
267 | * @private | |
267 | */ |
|
268 | */ | |
268 | CodeCell.prototype._handle_set_next_input = function (text) { |
|
269 | CodeCell.prototype._handle_set_next_input = function (text) { | |
269 | var data = {'cell': this, 'text': text} |
|
270 | var data = {'cell': this, 'text': text} | |
270 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
271 | $([IPython.events]).trigger('set_next_input.Notebook', data); | |
271 | } |
|
272 | } | |
272 |
|
273 | |||
273 | /** |
|
274 | /** | |
274 | * @method _handle_input_request |
|
275 | * @method _handle_input_request | |
275 | * @private |
|
276 | * @private | |
276 | */ |
|
277 | */ | |
277 | CodeCell.prototype._handle_input_request = function (content) { |
|
278 | CodeCell.prototype._handle_input_request = function (content) { | |
278 | this.output_area.append_raw_input(content); |
|
279 | this.output_area.append_raw_input(content); | |
279 | } |
|
280 | } | |
280 |
|
281 | |||
281 |
|
282 | |||
282 | // Basic cell manipulation. |
|
283 | // Basic cell manipulation. | |
283 |
|
284 | |||
284 | CodeCell.prototype.select = function () { |
|
285 | CodeCell.prototype.select = function () { | |
285 | IPython.Cell.prototype.select.apply(this); |
|
286 | IPython.Cell.prototype.select.apply(this); | |
286 | this.code_mirror.refresh(); |
|
287 | this.code_mirror.refresh(); | |
287 | this.code_mirror.focus(); |
|
288 | this.code_mirror.focus(); | |
288 | this.auto_highlight(); |
|
289 | this.auto_highlight(); | |
289 | // We used to need an additional refresh() after the focus, but |
|
290 | // We used to need an additional refresh() after the focus, but | |
290 | // it appears that this has been fixed in CM. This bug would show |
|
291 | // it appears that this has been fixed in CM. This bug would show | |
291 | // up on FF when a newly loaded markdown cell was edited. |
|
292 | // up on FF when a newly loaded markdown cell was edited. | |
292 | }; |
|
293 | }; | |
293 |
|
294 | |||
294 |
|
295 | |||
295 | CodeCell.prototype.select_all = function () { |
|
296 | CodeCell.prototype.select_all = function () { | |
296 | var start = {line: 0, ch: 0}; |
|
297 | var start = {line: 0, ch: 0}; | |
297 | var nlines = this.code_mirror.lineCount(); |
|
298 | var nlines = this.code_mirror.lineCount(); | |
298 | var last_line = this.code_mirror.getLine(nlines-1); |
|
299 | var last_line = this.code_mirror.getLine(nlines-1); | |
299 | var end = {line: nlines-1, ch: last_line.length}; |
|
300 | var end = {line: nlines-1, ch: last_line.length}; | |
300 | this.code_mirror.setSelection(start, end); |
|
301 | this.code_mirror.setSelection(start, end); | |
301 | }; |
|
302 | }; | |
302 |
|
303 | |||
303 |
|
304 | |||
304 | CodeCell.prototype.collapse = function () { |
|
305 | CodeCell.prototype.collapse = function () { | |
305 | this.collapsed = true; |
|
306 | this.collapsed = true; | |
306 | this.output_area.collapse(); |
|
307 | this.output_area.collapse(); | |
307 | }; |
|
308 | }; | |
308 |
|
309 | |||
309 |
|
310 | |||
310 | CodeCell.prototype.expand = function () { |
|
311 | CodeCell.prototype.expand = function () { | |
311 | this.collapsed = false; |
|
312 | this.collapsed = false; | |
312 | this.output_area.expand(); |
|
313 | this.output_area.expand(); | |
313 | }; |
|
314 | }; | |
314 |
|
315 | |||
315 |
|
316 | |||
316 | CodeCell.prototype.toggle_output = function () { |
|
317 | CodeCell.prototype.toggle_output = function () { | |
317 | this.collapsed = Boolean(1 - this.collapsed); |
|
318 | this.collapsed = Boolean(1 - this.collapsed); | |
318 | this.output_area.toggle_output(); |
|
319 | this.output_area.toggle_output(); | |
319 | }; |
|
320 | }; | |
320 |
|
321 | |||
321 |
|
322 | |||
322 | CodeCell.prototype.toggle_output_scroll = function () { |
|
323 | CodeCell.prototype.toggle_output_scroll = function () { | |
323 | this.output_area.toggle_scroll(); |
|
324 | this.output_area.toggle_scroll(); | |
324 | }; |
|
325 | }; | |
325 |
|
326 | |||
326 |
|
327 | |||
327 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
328 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { | |
328 | var ns = prompt_value || " "; |
|
329 | var ns = prompt_value || " "; | |
329 | return 'In [' + ns + ']:' |
|
330 | return 'In [' + ns + ']:' | |
330 | }; |
|
331 | }; | |
331 |
|
332 | |||
332 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
333 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { | |
333 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
334 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; | |
334 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; |
|
335 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; | |
335 | return html.join('</br>') |
|
336 | return html.join('</br>') | |
336 | }; |
|
337 | }; | |
337 |
|
338 | |||
338 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
339 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; | |
339 |
|
340 | |||
340 |
|
341 | |||
341 | CodeCell.prototype.set_input_prompt = function (number) { |
|
342 | CodeCell.prototype.set_input_prompt = function (number) { | |
342 | var nline = 1 |
|
343 | var nline = 1 | |
343 | if( this.code_mirror != undefined) { |
|
344 | if( this.code_mirror != undefined) { | |
344 | nline = this.code_mirror.lineCount(); |
|
345 | nline = this.code_mirror.lineCount(); | |
345 | } |
|
346 | } | |
346 | this.input_prompt_number = number; |
|
347 | this.input_prompt_number = number; | |
347 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
348 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); | |
348 | this.element.find('div.input_prompt').html(prompt_html); |
|
349 | this.element.find('div.input_prompt').html(prompt_html); | |
349 | }; |
|
350 | }; | |
350 |
|
351 | |||
351 |
|
352 | |||
352 | CodeCell.prototype.clear_input = function () { |
|
353 | CodeCell.prototype.clear_input = function () { | |
353 | this.code_mirror.setValue(''); |
|
354 | this.code_mirror.setValue(''); | |
354 | }; |
|
355 | }; | |
355 |
|
356 | |||
356 |
|
357 | |||
357 | CodeCell.prototype.get_text = function () { |
|
358 | CodeCell.prototype.get_text = function () { | |
358 | return this.code_mirror.getValue(); |
|
359 | return this.code_mirror.getValue(); | |
359 | }; |
|
360 | }; | |
360 |
|
361 | |||
361 |
|
362 | |||
362 | CodeCell.prototype.set_text = function (code) { |
|
363 | CodeCell.prototype.set_text = function (code) { | |
363 | return this.code_mirror.setValue(code); |
|
364 | return this.code_mirror.setValue(code); | |
364 | }; |
|
365 | }; | |
365 |
|
366 | |||
366 |
|
367 | |||
367 | CodeCell.prototype.at_top = function () { |
|
368 | CodeCell.prototype.at_top = function () { | |
368 | var cursor = this.code_mirror.getCursor(); |
|
369 | var cursor = this.code_mirror.getCursor(); | |
369 | if (cursor.line === 0 && cursor.ch === 0) { |
|
370 | if (cursor.line === 0 && cursor.ch === 0) { | |
370 | return true; |
|
371 | return true; | |
371 | } else { |
|
372 | } else { | |
372 | return false; |
|
373 | return false; | |
373 | } |
|
374 | } | |
374 | }; |
|
375 | }; | |
375 |
|
376 | |||
376 |
|
377 | |||
377 | CodeCell.prototype.at_bottom = function () { |
|
378 | CodeCell.prototype.at_bottom = function () { | |
378 | var cursor = this.code_mirror.getCursor(); |
|
379 | var cursor = this.code_mirror.getCursor(); | |
379 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
380 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | |
380 | return true; |
|
381 | return true; | |
381 | } else { |
|
382 | } else { | |
382 | return false; |
|
383 | return false; | |
383 | } |
|
384 | } | |
384 | }; |
|
385 | }; | |
385 |
|
386 | |||
386 |
|
387 | |||
387 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
388 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { | |
388 | this.output_area.clear_output(stdout, stderr, other); |
|
389 | this.output_area.clear_output(stdout, stderr, other); | |
389 | }; |
|
390 | }; | |
390 |
|
391 | |||
391 |
|
392 | |||
392 | // JSON serialization |
|
393 | // JSON serialization | |
393 |
|
394 | |||
394 | CodeCell.prototype.fromJSON = function (data) { |
|
395 | CodeCell.prototype.fromJSON = function (data) { | |
395 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
396 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
396 | if (data.cell_type === 'code') { |
|
397 | if (data.cell_type === 'code') { | |
397 | if (data.input !== undefined) { |
|
398 | if (data.input !== undefined) { | |
398 | this.set_text(data.input); |
|
399 | this.set_text(data.input); | |
399 | // make this value the starting point, so that we can only undo |
|
400 | // make this value the starting point, so that we can only undo | |
400 | // to this state, instead of a blank cell |
|
401 | // to this state, instead of a blank cell | |
401 | this.code_mirror.clearHistory(); |
|
402 | this.code_mirror.clearHistory(); | |
402 | this.auto_highlight(); |
|
403 | this.auto_highlight(); | |
403 | } |
|
404 | } | |
404 | if (data.prompt_number !== undefined) { |
|
405 | if (data.prompt_number !== undefined) { | |
405 | this.set_input_prompt(data.prompt_number); |
|
406 | this.set_input_prompt(data.prompt_number); | |
406 | } else { |
|
407 | } else { | |
407 | this.set_input_prompt(); |
|
408 | this.set_input_prompt(); | |
408 | }; |
|
409 | }; | |
409 | this.output_area.fromJSON(data.outputs); |
|
410 | this.output_area.fromJSON(data.outputs); | |
410 | if (data.collapsed !== undefined) { |
|
411 | if (data.collapsed !== undefined) { | |
411 | if (data.collapsed) { |
|
412 | if (data.collapsed) { | |
412 | this.collapse(); |
|
413 | this.collapse(); | |
413 | } else { |
|
414 | } else { | |
414 | this.expand(); |
|
415 | this.expand(); | |
415 | }; |
|
416 | }; | |
416 | }; |
|
417 | }; | |
417 | }; |
|
418 | }; | |
418 | }; |
|
419 | }; | |
419 |
|
420 | |||
420 |
|
421 | |||
421 | CodeCell.prototype.toJSON = function () { |
|
422 | CodeCell.prototype.toJSON = function () { | |
422 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
423 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
423 | data.input = this.get_text(); |
|
424 | data.input = this.get_text(); | |
424 | data.cell_type = 'code'; |
|
425 | data.cell_type = 'code'; | |
425 | if (this.input_prompt_number) { |
|
426 | if (this.input_prompt_number) { | |
426 | data.prompt_number = this.input_prompt_number; |
|
427 | data.prompt_number = this.input_prompt_number; | |
427 | }; |
|
428 | }; | |
428 | var outputs = this.output_area.toJSON(); |
|
429 | var outputs = this.output_area.toJSON(); | |
429 | data.outputs = outputs; |
|
430 | data.outputs = outputs; | |
430 | data.language = 'python'; |
|
431 | data.language = 'python'; | |
431 | data.collapsed = this.collapsed; |
|
432 | data.collapsed = this.collapsed; | |
432 | return data; |
|
433 | return data; | |
433 | }; |
|
434 | }; | |
434 |
|
435 | |||
435 |
|
436 | |||
436 | IPython.CodeCell = CodeCell; |
|
437 | IPython.CodeCell = CodeCell; | |
437 |
|
438 | |||
438 | return IPython; |
|
439 | return IPython; | |
439 | }(IPython)); |
|
440 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now