##// END OF EJS Templates
Display prompt in the notebook correctly
Jessica B. Hamrick -
Show More
@@ -1,532 +1,532 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3 /**
3 /**
4 *
4 *
5 *
5 *
6 * @module codecell
6 * @module codecell
7 * @namespace codecell
7 * @namespace codecell
8 * @class CodeCell
8 * @class CodeCell
9 */
9 */
10
10
11
11
12 define([
12 define([
13 'base/js/namespace',
13 'base/js/namespace',
14 'jquery',
14 'jquery',
15 'base/js/utils',
15 'base/js/utils',
16 'base/js/keyboard',
16 'base/js/keyboard',
17 'notebook/js/cell',
17 'notebook/js/cell',
18 'notebook/js/outputarea',
18 'notebook/js/outputarea',
19 'notebook/js/completer',
19 'notebook/js/completer',
20 'notebook/js/celltoolbar',
20 'notebook/js/celltoolbar',
21 'codemirror/lib/codemirror',
21 'codemirror/lib/codemirror',
22 'codemirror/mode/python/python',
22 'codemirror/mode/python/python',
23 'notebook/js/codemirror-ipython'
23 'notebook/js/codemirror-ipython'
24 ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip) {
24 ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip) {
25 "use strict";
25 "use strict";
26 var Cell = cell.Cell;
26 var Cell = cell.Cell;
27
27
28 /* local util for codemirror */
28 /* local util for codemirror */
29 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
29 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
30
30
31 /**
31 /**
32 *
32 *
33 * function to delete until previous non blanking space character
33 * function to delete until previous non blanking space character
34 * or first multiple of 4 tabstop.
34 * or first multiple of 4 tabstop.
35 * @private
35 * @private
36 */
36 */
37 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
37 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
38 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
38 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
39 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
39 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
40 var cur = cm.getCursor(), line = cm.getLine(cur.line);
40 var cur = cm.getCursor(), line = cm.getLine(cur.line);
41 var tabsize = cm.getOption('tabSize');
41 var tabsize = cm.getOption('tabSize');
42 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
42 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
43 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
43 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
44 var select = cm.getRange(from,cur);
44 var select = cm.getRange(from,cur);
45 if( select.match(/^\ +$/) !== null){
45 if( select.match(/^\ +$/) !== null){
46 cm.replaceRange("",from,cur);
46 cm.replaceRange("",from,cur);
47 } else {
47 } else {
48 cm.deleteH(-1,"char");
48 cm.deleteH(-1,"char");
49 }
49 }
50 };
50 };
51
51
52 var keycodes = keyboard.keycodes;
52 var keycodes = keyboard.keycodes;
53
53
54 var CodeCell = function (kernel, options) {
54 var CodeCell = function (kernel, options) {
55 // Constructor
55 // Constructor
56 //
56 //
57 // A Cell conceived to write code.
57 // A Cell conceived to write code.
58 //
58 //
59 // Parameters:
59 // Parameters:
60 // kernel: Kernel instance
60 // kernel: Kernel instance
61 // The kernel doesn't have to be set at creation time, in that case
61 // The kernel doesn't have to be set at creation time, in that case
62 // it will be null and set_kernel has to be called later.
62 // it will be null and set_kernel has to be called later.
63 // options: dictionary
63 // options: dictionary
64 // Dictionary of keyword arguments.
64 // Dictionary of keyword arguments.
65 // events: $(Events) instance
65 // events: $(Events) instance
66 // config: dictionary
66 // config: dictionary
67 // keyboard_manager: KeyboardManager instance
67 // keyboard_manager: KeyboardManager instance
68 // notebook: Notebook instance
68 // notebook: Notebook instance
69 // tooltip: Tooltip instance
69 // tooltip: Tooltip instance
70 this.kernel = kernel || null;
70 this.kernel = kernel || null;
71 this.notebook = options.notebook;
71 this.notebook = options.notebook;
72 this.collapsed = false;
72 this.collapsed = false;
73 this.events = options.events;
73 this.events = options.events;
74 this.tooltip = options.tooltip;
74 this.tooltip = options.tooltip;
75 this.config = options.config;
75 this.config = options.config;
76
76
77 // create all attributed in constructor function
77 // create all attributed in constructor function
78 // even if null for V8 VM optimisation
78 // even if null for V8 VM optimisation
79 this.input_prompt_number = null;
79 this.input_prompt_number = null;
80 this.celltoolbar = null;
80 this.celltoolbar = null;
81 this.output_area = null;
81 this.output_area = null;
82 this.last_msg_id = null;
82 this.last_msg_id = null;
83 this.completer = null;
83 this.completer = null;
84
84
85
85
86 var config = utils.mergeopt(CodeCell, this.config);
86 var config = utils.mergeopt(CodeCell, this.config);
87 Cell.apply(this,[{
87 Cell.apply(this,[{
88 config: config,
88 config: config,
89 keyboard_manager: options.keyboard_manager,
89 keyboard_manager: options.keyboard_manager,
90 events: this.events}]);
90 events: this.events}]);
91
91
92 // Attributes we want to override in this subclass.
92 // Attributes we want to override in this subclass.
93 this.cell_type = "code";
93 this.cell_type = "code";
94
94
95 var that = this;
95 var that = this;
96 this.element.focusout(
96 this.element.focusout(
97 function() { that.auto_highlight(); }
97 function() { that.auto_highlight(); }
98 );
98 );
99 };
99 };
100
100
101 CodeCell.options_default = {
101 CodeCell.options_default = {
102 cm_config : {
102 cm_config : {
103 extraKeys: {
103 extraKeys: {
104 "Tab" : "indentMore",
104 "Tab" : "indentMore",
105 "Shift-Tab" : "indentLess",
105 "Shift-Tab" : "indentLess",
106 "Backspace" : "delSpaceToPrevTabStop",
106 "Backspace" : "delSpaceToPrevTabStop",
107 "Cmd-/" : "toggleComment",
107 "Cmd-/" : "toggleComment",
108 "Ctrl-/" : "toggleComment"
108 "Ctrl-/" : "toggleComment"
109 },
109 },
110 mode: 'ipython',
110 mode: 'ipython',
111 theme: 'ipython',
111 theme: 'ipython',
112 matchBrackets: true
112 matchBrackets: true
113 }
113 }
114 };
114 };
115
115
116 CodeCell.msg_cells = {};
116 CodeCell.msg_cells = {};
117
117
118 CodeCell.prototype = new Cell();
118 CodeCell.prototype = new Cell();
119
119
120 /**
120 /**
121 * @method auto_highlight
121 * @method auto_highlight
122 */
122 */
123 CodeCell.prototype.auto_highlight = function () {
123 CodeCell.prototype.auto_highlight = function () {
124 this._auto_highlight(this.config.cell_magic_highlight);
124 this._auto_highlight(this.config.cell_magic_highlight);
125 };
125 };
126
126
127 /** @method create_element */
127 /** @method create_element */
128 CodeCell.prototype.create_element = function () {
128 CodeCell.prototype.create_element = function () {
129 Cell.prototype.create_element.apply(this, arguments);
129 Cell.prototype.create_element.apply(this, arguments);
130
130
131 var cell = $('<div></div>').addClass('cell code_cell');
131 var cell = $('<div></div>').addClass('cell code_cell');
132 cell.attr('tabindex','2');
132 cell.attr('tabindex','2');
133
133
134 var input = $('<div></div>').addClass('input');
134 var input = $('<div></div>').addClass('input');
135 var prompt = $('<div/>').addClass('prompt input_prompt');
135 var prompt = $('<div/>').addClass('prompt input_prompt');
136 var inner_cell = $('<div/>').addClass('inner_cell');
136 var inner_cell = $('<div/>').addClass('inner_cell');
137 this.celltoolbar = new celltoolbar.CellToolbar({
137 this.celltoolbar = new celltoolbar.CellToolbar({
138 cell: this,
138 cell: this,
139 notebook: this.notebook});
139 notebook: this.notebook});
140 inner_cell.append(this.celltoolbar.element);
140 inner_cell.append(this.celltoolbar.element);
141 var input_area = $('<div/>').addClass('input_area');
141 var input_area = $('<div/>').addClass('input_area');
142 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
142 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
143 this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this))
143 this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this))
144 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
144 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
145 inner_cell.append(input_area);
145 inner_cell.append(input_area);
146 input.append(prompt).append(inner_cell);
146 input.append(prompt).append(inner_cell);
147
147
148 var widget_area = $('<div/>')
148 var widget_area = $('<div/>')
149 .addClass('widget-area')
149 .addClass('widget-area')
150 .hide();
150 .hide();
151 this.widget_area = widget_area;
151 this.widget_area = widget_area;
152 var widget_prompt = $('<div/>')
152 var widget_prompt = $('<div/>')
153 .addClass('prompt')
153 .addClass('prompt')
154 .appendTo(widget_area);
154 .appendTo(widget_area);
155 var widget_subarea = $('<div/>')
155 var widget_subarea = $('<div/>')
156 .addClass('widget-subarea')
156 .addClass('widget-subarea')
157 .appendTo(widget_area);
157 .appendTo(widget_area);
158 this.widget_subarea = widget_subarea;
158 this.widget_subarea = widget_subarea;
159 var widget_clear_buton = $('<button />')
159 var widget_clear_buton = $('<button />')
160 .addClass('close')
160 .addClass('close')
161 .html('&times;')
161 .html('&times;')
162 .click(function() {
162 .click(function() {
163 widget_area.slideUp('', function(){ widget_subarea.html(''); });
163 widget_area.slideUp('', function(){ widget_subarea.html(''); });
164 })
164 })
165 .appendTo(widget_prompt);
165 .appendTo(widget_prompt);
166
166
167 var output = $('<div></div>');
167 var output = $('<div></div>');
168 cell.append(input).append(widget_area).append(output);
168 cell.append(input).append(widget_area).append(output);
169 this.element = cell;
169 this.element = cell;
170 this.output_area = new outputarea.OutputArea({
170 this.output_area = new outputarea.OutputArea({
171 selector: output,
171 selector: output,
172 prompt_area: true,
172 prompt_area: true,
173 events: this.events,
173 events: this.events,
174 keyboard_manager: this.keyboard_manager});
174 keyboard_manager: this.keyboard_manager});
175 this.completer = new completer.Completer(this, this.events);
175 this.completer = new completer.Completer(this, this.events);
176 };
176 };
177
177
178 /** @method bind_events */
178 /** @method bind_events */
179 CodeCell.prototype.bind_events = function () {
179 CodeCell.prototype.bind_events = function () {
180 Cell.prototype.bind_events.apply(this);
180 Cell.prototype.bind_events.apply(this);
181 var that = this;
181 var that = this;
182
182
183 this.element.focusout(
183 this.element.focusout(
184 function() { that.auto_highlight(); }
184 function() { that.auto_highlight(); }
185 );
185 );
186 };
186 };
187
187
188
188
189 /**
189 /**
190 * This method gets called in CodeMirror's onKeyDown/onKeyPress
190 * This method gets called in CodeMirror's onKeyDown/onKeyPress
191 * handlers and is used to provide custom key handling. Its return
191 * handlers and is used to provide custom key handling. Its return
192 * value is used to determine if CodeMirror should ignore the event:
192 * value is used to determine if CodeMirror should ignore the event:
193 * true = ignore, false = don't ignore.
193 * true = ignore, false = don't ignore.
194 * @method handle_codemirror_keyevent
194 * @method handle_codemirror_keyevent
195 */
195 */
196 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
196 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
197
197
198 var that = this;
198 var that = this;
199 // whatever key is pressed, first, cancel the tooltip request before
199 // whatever key is pressed, first, cancel the tooltip request before
200 // they are sent, and remove tooltip if any, except for tab again
200 // they are sent, and remove tooltip if any, except for tab again
201 var tooltip_closed = null;
201 var tooltip_closed = null;
202 if (event.type === 'keydown' && event.which != keycodes.tab ) {
202 if (event.type === 'keydown' && event.which != keycodes.tab ) {
203 tooltip_closed = this.tooltip.remove_and_cancel_tooltip();
203 tooltip_closed = this.tooltip.remove_and_cancel_tooltip();
204 }
204 }
205
205
206 var cur = editor.getCursor();
206 var cur = editor.getCursor();
207 if (event.keyCode === keycodes.enter){
207 if (event.keyCode === keycodes.enter){
208 this.auto_highlight();
208 this.auto_highlight();
209 }
209 }
210
210
211 if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) {
211 if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) {
212 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
212 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
213 // browser and keyboard layout !
213 // browser and keyboard layout !
214 // Pressing '(' , request tooltip, don't forget to reappend it
214 // Pressing '(' , request tooltip, don't forget to reappend it
215 // The second argument says to hide the tooltip if the docstring
215 // The second argument says to hide the tooltip if the docstring
216 // is actually empty
216 // is actually empty
217 this.tooltip.pending(that, true);
217 this.tooltip.pending(that, true);
218 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
218 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
219 // If tooltip is active, cancel it. The call to
219 // If tooltip is active, cancel it. The call to
220 // remove_and_cancel_tooltip above doesn't pass, force=true.
220 // remove_and_cancel_tooltip above doesn't pass, force=true.
221 // Because of this it won't actually close the tooltip
221 // Because of this it won't actually close the tooltip
222 // if it is in sticky mode. Thus, we have to check again if it is open
222 // if it is in sticky mode. Thus, we have to check again if it is open
223 // and close it with force=true.
223 // and close it with force=true.
224 if (!this.tooltip._hidden) {
224 if (!this.tooltip._hidden) {
225 this.tooltip.remove_and_cancel_tooltip(true);
225 this.tooltip.remove_and_cancel_tooltip(true);
226 }
226 }
227 // If we closed the tooltip, don't let CM or the global handlers
227 // If we closed the tooltip, don't let CM or the global handlers
228 // handle this event.
228 // handle this event.
229 event.codemirrorIgnore = true;
229 event.codemirrorIgnore = true;
230 event.preventDefault();
230 event.preventDefault();
231 return true;
231 return true;
232 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
232 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
233 if (editor.somethingSelected() || editor.getSelections().length !== 1){
233 if (editor.somethingSelected() || editor.getSelections().length !== 1){
234 var anchor = editor.getCursor("anchor");
234 var anchor = editor.getCursor("anchor");
235 var head = editor.getCursor("head");
235 var head = editor.getCursor("head");
236 if( anchor.line != head.line){
236 if( anchor.line != head.line){
237 return false;
237 return false;
238 }
238 }
239 }
239 }
240 this.tooltip.request(that);
240 this.tooltip.request(that);
241 event.codemirrorIgnore = true;
241 event.codemirrorIgnore = true;
242 event.preventDefault();
242 event.preventDefault();
243 return true;
243 return true;
244 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
244 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
245 // Tab completion.
245 // Tab completion.
246 this.tooltip.remove_and_cancel_tooltip();
246 this.tooltip.remove_and_cancel_tooltip();
247
247
248 // completion does not work on multicursor, it might be possible though in some cases
248 // completion does not work on multicursor, it might be possible though in some cases
249 if (editor.somethingSelected() || editor.getSelections().length > 1) {
249 if (editor.somethingSelected() || editor.getSelections().length > 1) {
250 return false;
250 return false;
251 }
251 }
252 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
252 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
253 if (pre_cursor.trim() === "") {
253 if (pre_cursor.trim() === "") {
254 // Don't autocomplete if the part of the line before the cursor
254 // Don't autocomplete if the part of the line before the cursor
255 // is empty. In this case, let CodeMirror handle indentation.
255 // is empty. In this case, let CodeMirror handle indentation.
256 return false;
256 return false;
257 } else {
257 } else {
258 event.codemirrorIgnore = true;
258 event.codemirrorIgnore = true;
259 event.preventDefault();
259 event.preventDefault();
260 this.completer.startCompletion();
260 this.completer.startCompletion();
261 return true;
261 return true;
262 }
262 }
263 }
263 }
264
264
265 // keyboard event wasn't one of those unique to code cells, let's see
265 // keyboard event wasn't one of those unique to code cells, let's see
266 // if it's one of the generic ones (i.e. check edit mode shortcuts)
266 // if it's one of the generic ones (i.e. check edit mode shortcuts)
267 return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]);
267 return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]);
268 };
268 };
269
269
270 // Kernel related calls.
270 // Kernel related calls.
271
271
272 CodeCell.prototype.set_kernel = function (kernel) {
272 CodeCell.prototype.set_kernel = function (kernel) {
273 this.kernel = kernel;
273 this.kernel = kernel;
274 };
274 };
275
275
276 /**
276 /**
277 * Execute current code cell to the kernel
277 * Execute current code cell to the kernel
278 * @method execute
278 * @method execute
279 */
279 */
280 CodeCell.prototype.execute = function () {
280 CodeCell.prototype.execute = function () {
281 this.output_area.clear_output();
281 this.output_area.clear_output();
282
282
283 // Clear widget area
283 // Clear widget area
284 this.widget_subarea.html('');
284 this.widget_subarea.html('');
285 this.widget_subarea.height('');
285 this.widget_subarea.height('');
286 this.widget_area.height('');
286 this.widget_area.height('');
287 this.widget_area.hide();
287 this.widget_area.hide();
288
288
289 this.set_input_prompt('*');
289 this.set_input_prompt('*');
290 this.element.addClass("running");
290 this.element.addClass("running");
291 if (this.last_msg_id) {
291 if (this.last_msg_id) {
292 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
292 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
293 }
293 }
294 var callbacks = this.get_callbacks();
294 var callbacks = this.get_callbacks();
295
295
296 var old_msg_id = this.last_msg_id;
296 var old_msg_id = this.last_msg_id;
297 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
297 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
298 if (old_msg_id) {
298 if (old_msg_id) {
299 delete CodeCell.msg_cells[old_msg_id];
299 delete CodeCell.msg_cells[old_msg_id];
300 }
300 }
301 CodeCell.msg_cells[this.last_msg_id] = this;
301 CodeCell.msg_cells[this.last_msg_id] = this;
302 this.render();
302 this.render();
303 };
303 };
304
304
305 /**
305 /**
306 * Construct the default callbacks for
306 * Construct the default callbacks for
307 * @method get_callbacks
307 * @method get_callbacks
308 */
308 */
309 CodeCell.prototype.get_callbacks = function () {
309 CodeCell.prototype.get_callbacks = function () {
310 return {
310 return {
311 shell : {
311 shell : {
312 reply : $.proxy(this._handle_execute_reply, this),
312 reply : $.proxy(this._handle_execute_reply, this),
313 payload : {
313 payload : {
314 set_next_input : $.proxy(this._handle_set_next_input, this),
314 set_next_input : $.proxy(this._handle_set_next_input, this),
315 page : $.proxy(this._open_with_pager, this)
315 page : $.proxy(this._open_with_pager, this)
316 }
316 }
317 },
317 },
318 iopub : {
318 iopub : {
319 output : $.proxy(this.output_area.handle_output, this.output_area),
319 output : $.proxy(this.output_area.handle_output, this.output_area),
320 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
320 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
321 },
321 },
322 input : $.proxy(this._handle_input_request, this)
322 input : $.proxy(this._handle_input_request, this)
323 };
323 };
324 };
324 };
325
325
326 CodeCell.prototype._open_with_pager = function (payload) {
326 CodeCell.prototype._open_with_pager = function (payload) {
327 this.events.trigger('open_with_text.Pager', payload);
327 this.events.trigger('open_with_text.Pager', payload);
328 };
328 };
329
329
330 /**
330 /**
331 * @method _handle_execute_reply
331 * @method _handle_execute_reply
332 * @private
332 * @private
333 */
333 */
334 CodeCell.prototype._handle_execute_reply = function (msg) {
334 CodeCell.prototype._handle_execute_reply = function (msg) {
335 this.set_input_prompt(msg.content.execution_count);
335 this.set_input_prompt(msg.content.execution_count);
336 this.element.removeClass("running");
336 this.element.removeClass("running");
337 this.events.trigger('set_dirty.Notebook', {value: true});
337 this.events.trigger('set_dirty.Notebook', {value: true});
338 };
338 };
339
339
340 /**
340 /**
341 * @method _handle_set_next_input
341 * @method _handle_set_next_input
342 * @private
342 * @private
343 */
343 */
344 CodeCell.prototype._handle_set_next_input = function (payload) {
344 CodeCell.prototype._handle_set_next_input = function (payload) {
345 var data = {'cell': this, 'text': payload.text};
345 var data = {'cell': this, 'text': payload.text};
346 this.events.trigger('set_next_input.Notebook', data);
346 this.events.trigger('set_next_input.Notebook', data);
347 };
347 };
348
348
349 /**
349 /**
350 * @method _handle_input_request
350 * @method _handle_input_request
351 * @private
351 * @private
352 */
352 */
353 CodeCell.prototype._handle_input_request = function (msg) {
353 CodeCell.prototype._handle_input_request = function (msg) {
354 this.output_area.append_raw_input(msg);
354 this.output_area.append_raw_input(msg);
355 };
355 };
356
356
357
357
358 // Basic cell manipulation.
358 // Basic cell manipulation.
359
359
360 CodeCell.prototype.select = function () {
360 CodeCell.prototype.select = function () {
361 var cont = Cell.prototype.select.apply(this);
361 var cont = Cell.prototype.select.apply(this);
362 if (cont) {
362 if (cont) {
363 this.code_mirror.refresh();
363 this.code_mirror.refresh();
364 this.auto_highlight();
364 this.auto_highlight();
365 }
365 }
366 return cont;
366 return cont;
367 };
367 };
368
368
369 CodeCell.prototype.render = function () {
369 CodeCell.prototype.render = function () {
370 var cont = Cell.prototype.render.apply(this);
370 var cont = Cell.prototype.render.apply(this);
371 // Always execute, even if we are already in the rendered state
371 // Always execute, even if we are already in the rendered state
372 return cont;
372 return cont;
373 };
373 };
374
374
375 CodeCell.prototype.select_all = function () {
375 CodeCell.prototype.select_all = function () {
376 var start = {line: 0, ch: 0};
376 var start = {line: 0, ch: 0};
377 var nlines = this.code_mirror.lineCount();
377 var nlines = this.code_mirror.lineCount();
378 var last_line = this.code_mirror.getLine(nlines-1);
378 var last_line = this.code_mirror.getLine(nlines-1);
379 var end = {line: nlines-1, ch: last_line.length};
379 var end = {line: nlines-1, ch: last_line.length};
380 this.code_mirror.setSelection(start, end);
380 this.code_mirror.setSelection(start, end);
381 };
381 };
382
382
383
383
384 CodeCell.prototype.collapse_output = function () {
384 CodeCell.prototype.collapse_output = function () {
385 this.collapsed = true;
385 this.collapsed = true;
386 this.output_area.collapse();
386 this.output_area.collapse();
387 };
387 };
388
388
389
389
390 CodeCell.prototype.expand_output = function () {
390 CodeCell.prototype.expand_output = function () {
391 this.collapsed = false;
391 this.collapsed = false;
392 this.output_area.expand();
392 this.output_area.expand();
393 this.output_area.unscroll_area();
393 this.output_area.unscroll_area();
394 };
394 };
395
395
396 CodeCell.prototype.scroll_output = function () {
396 CodeCell.prototype.scroll_output = function () {
397 this.output_area.expand();
397 this.output_area.expand();
398 this.output_area.scroll_if_long();
398 this.output_area.scroll_if_long();
399 };
399 };
400
400
401 CodeCell.prototype.toggle_output = function () {
401 CodeCell.prototype.toggle_output = function () {
402 this.collapsed = Boolean(1 - this.collapsed);
402 this.collapsed = Boolean(1 - this.collapsed);
403 this.output_area.toggle_output();
403 this.output_area.toggle_output();
404 };
404 };
405
405
406 CodeCell.prototype.toggle_output_scroll = function () {
406 CodeCell.prototype.toggle_output_scroll = function () {
407 this.output_area.toggle_scroll();
407 this.output_area.toggle_scroll();
408 };
408 };
409
409
410
410
411 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
411 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
412 var ns;
412 var ns;
413 if (prompt_value === undefined) {
413 if (prompt_value === undefined || prompt_value === null) {
414 ns = "&nbsp;";
414 ns = "&nbsp;";
415 } else {
415 } else {
416 ns = encodeURIComponent(prompt_value);
416 ns = encodeURIComponent(prompt_value);
417 }
417 }
418 return 'In&nbsp;[' + ns + ']:';
418 return 'In&nbsp;[' + ns + ']:';
419 };
419 };
420
420
421 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
421 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
422 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
422 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
423 for(var i=1; i < lines_number; i++) {
423 for(var i=1; i < lines_number; i++) {
424 html.push(['...:']);
424 html.push(['...:']);
425 }
425 }
426 return html.join('<br/>');
426 return html.join('<br/>');
427 };
427 };
428
428
429 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
429 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
430
430
431
431
432 CodeCell.prototype.set_input_prompt = function (number) {
432 CodeCell.prototype.set_input_prompt = function (number) {
433 var nline = 1;
433 var nline = 1;
434 if (this.code_mirror !== undefined) {
434 if (this.code_mirror !== undefined) {
435 nline = this.code_mirror.lineCount();
435 nline = this.code_mirror.lineCount();
436 }
436 }
437 this.input_prompt_number = number;
437 this.input_prompt_number = number;
438 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
438 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
439 // This HTML call is okay because the user contents are escaped.
439 // This HTML call is okay because the user contents are escaped.
440 this.element.find('div.input_prompt').html(prompt_html);
440 this.element.find('div.input_prompt').html(prompt_html);
441 };
441 };
442
442
443
443
444 CodeCell.prototype.clear_input = function () {
444 CodeCell.prototype.clear_input = function () {
445 this.code_mirror.setValue('');
445 this.code_mirror.setValue('');
446 };
446 };
447
447
448
448
449 CodeCell.prototype.get_text = function () {
449 CodeCell.prototype.get_text = function () {
450 return this.code_mirror.getValue();
450 return this.code_mirror.getValue();
451 };
451 };
452
452
453
453
454 CodeCell.prototype.set_text = function (code) {
454 CodeCell.prototype.set_text = function (code) {
455 return this.code_mirror.setValue(code);
455 return this.code_mirror.setValue(code);
456 };
456 };
457
457
458
458
459 CodeCell.prototype.clear_output = function (wait) {
459 CodeCell.prototype.clear_output = function (wait) {
460 this.output_area.clear_output(wait);
460 this.output_area.clear_output(wait);
461 this.set_input_prompt();
461 this.set_input_prompt();
462 };
462 };
463
463
464
464
465 // JSON serialization
465 // JSON serialization
466
466
467 CodeCell.prototype.fromJSON = function (data) {
467 CodeCell.prototype.fromJSON = function (data) {
468 Cell.prototype.fromJSON.apply(this, arguments);
468 Cell.prototype.fromJSON.apply(this, arguments);
469 if (data.cell_type === 'code') {
469 if (data.cell_type === 'code') {
470 if (data.input !== undefined) {
470 if (data.input !== undefined) {
471 this.set_text(data.input);
471 this.set_text(data.input);
472 // make this value the starting point, so that we can only undo
472 // make this value the starting point, so that we can only undo
473 // to this state, instead of a blank cell
473 // to this state, instead of a blank cell
474 this.code_mirror.clearHistory();
474 this.code_mirror.clearHistory();
475 this.auto_highlight();
475 this.auto_highlight();
476 }
476 }
477 if (data.prompt_number !== undefined) {
477 if (data.prompt_number !== undefined) {
478 this.set_input_prompt(data.prompt_number);
478 this.set_input_prompt(data.prompt_number);
479 } else {
479 } else {
480 this.set_input_prompt();
480 this.set_input_prompt();
481 }
481 }
482 this.output_area.trusted = data.metadata.trusted || false;
482 this.output_area.trusted = data.metadata.trusted || false;
483 this.output_area.fromJSON(data.outputs);
483 this.output_area.fromJSON(data.outputs);
484 if (data.collapsed !== undefined) {
484 if (data.collapsed !== undefined) {
485 if (data.collapsed) {
485 if (data.collapsed) {
486 this.collapse_output();
486 this.collapse_output();
487 } else {
487 } else {
488 this.expand_output();
488 this.expand_output();
489 }
489 }
490 }
490 }
491 }
491 }
492 };
492 };
493
493
494
494
495 CodeCell.prototype.toJSON = function () {
495 CodeCell.prototype.toJSON = function () {
496 var data = Cell.prototype.toJSON.apply(this);
496 var data = Cell.prototype.toJSON.apply(this);
497 data.input = this.get_text();
497 data.input = this.get_text();
498 // is finite protect against undefined and '*' value
498 // is finite protect against undefined and '*' value
499 if (isFinite(this.input_prompt_number)) {
499 if (isFinite(this.input_prompt_number)) {
500 data.prompt_number = this.input_prompt_number;
500 data.prompt_number = this.input_prompt_number;
501 }
501 }
502 var outputs = this.output_area.toJSON();
502 var outputs = this.output_area.toJSON();
503 data.outputs = outputs;
503 data.outputs = outputs;
504 data.language = 'python';
504 data.language = 'python';
505 data.metadata.trusted = this.output_area.trusted;
505 data.metadata.trusted = this.output_area.trusted;
506 data.collapsed = this.output_area.collapsed;
506 data.collapsed = this.output_area.collapsed;
507 return data;
507 return data;
508 };
508 };
509
509
510 /**
510 /**
511 * handle cell level logic when a cell is unselected
511 * handle cell level logic when a cell is unselected
512 * @method unselect
512 * @method unselect
513 * @return is the action being taken
513 * @return is the action being taken
514 */
514 */
515 CodeCell.prototype.unselect = function () {
515 CodeCell.prototype.unselect = function () {
516 var cont = Cell.prototype.unselect.apply(this);
516 var cont = Cell.prototype.unselect.apply(this);
517 if (cont) {
517 if (cont) {
518 // When a code cell is usnelected, make sure that the corresponding
518 // When a code cell is usnelected, make sure that the corresponding
519 // tooltip and completer to that cell is closed.
519 // tooltip and completer to that cell is closed.
520 this.tooltip.remove_and_cancel_tooltip(true);
520 this.tooltip.remove_and_cancel_tooltip(true);
521 if (this.completer !== null) {
521 if (this.completer !== null) {
522 this.completer.close();
522 this.completer.close();
523 }
523 }
524 }
524 }
525 return cont;
525 return cont;
526 };
526 };
527
527
528 // Backwards compatability.
528 // Backwards compatability.
529 IPython.CodeCell = CodeCell;
529 IPython.CodeCell = CodeCell;
530
530
531 return {'CodeCell': CodeCell};
531 return {'CodeCell': CodeCell};
532 });
532 });
General Comments 0
You need to be logged in to leave comments. Login now