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