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