codecell.js
553 lines
| 18.4 KiB
| application/javascript
|
JavascriptLexer
|
r4609 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
|
r4349 | |||
//============================================================================ | ||||
// CodeCell | ||||
//============================================================================ | ||||
|
r8768 | /** | ||
* An extendable module that provide base functionnality to create cell for notebook. | ||||
* @module IPython | ||||
* @namespace IPython | ||||
* @submodule CodeCell | ||||
*/ | ||||
|
r4349 | |||
|
r9515 | |||
/* local util for codemirror */ | ||||
|
r13210 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}; | ||
|
r9515 | |||
/** | ||||
* | ||||
* function to delete until previous non blanking space character | ||||
* or first multiple of 4 tabstop. | ||||
* @private | ||||
*/ | ||||
CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ | ||||
var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); | ||||
|
r13210 | if (!posEq(from, to)) { cm.replaceRange("", from, to); return; } | ||
|
r9515 | var cur = cm.getCursor(), line = cm.getLine(cur.line); | ||
var tabsize = cm.getOption('tabSize'); | ||||
var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; | ||||
|
r13210 | from = {ch:cur.ch-chToPrevTabStop,line:cur.line}; | ||
var select = cm.getRange(from,cur); | ||||
if( select.match(/^\ +$/) !== null){ | ||||
cm.replaceRange("",from,cur); | ||||
|
r9515 | } else { | ||
|
r13210 | cm.deleteH(-1,"char"); | ||
|
r9515 | } | ||
}; | ||||
|
r4352 | var IPython = (function (IPython) { | ||
|
r7132 | "use strict"; | ||
|
r4352 | var utils = IPython.utils; | ||
|
r7136 | var key = IPython.utils.keycodes; | ||
|
r4352 | |||
|
r8768 | /** | ||
* A Cell conceived to write code. | ||||
* | ||||
* The kernel doesn't have to be set at creation time, in that case | ||||
* it will be null and set_kernel has to be called later. | ||||
* @class CodeCell | ||||
* @extends IPython.Cell | ||||
* | ||||
* @constructor | ||||
* @param {Object|null} kernel | ||||
|
r9537 | * @param {object|undefined} [options] | ||
* @param [options.cm_config] {object} config to pass to CodeMirror | ||||
|
r8768 | */ | ||
|
r13133 | var CodeCell = function (kernel, options) { | ||
this.kernel = kernel || null; | ||||
|
r7524 | this.collapsed = false; | ||
|
r9537 | |||
|
r13574 | // create all attributed in constructor function | ||
// even if null for V8 VM optimisation | ||||
this.input_prompt_number = null; | ||||
this.celltoolbar = null; | ||||
this.output_area = null; | ||||
this.last_msg_id = null; | ||||
this.completer = null; | ||||
|
r9537 | |||
var cm_overwrite_options = { | ||||
|
r14021 | onKeyEvent: $.proxy(this.handle_keyevent,this) | ||
|
r9537 | }; | ||
|
r10165 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); | ||
|
r9537 | |||
IPython.Cell.apply(this,[options]); | ||||
|
r8202 | |||
|
r13661 | // Attributes we want to override in this subclass. | ||
this.cell_type = "code"; | ||||
|
r8202 | var that = this; | ||
this.element.focusout( | ||||
|
r8281 | function() { that.auto_highlight(); } | ||
); | ||||
|
r4352 | }; | ||
|
r10165 | CodeCell.options_default = { | ||
cm_config : { | ||||
|
r11488 | extraKeys: { | ||
"Tab" : "indentMore", | ||||
"Shift-Tab" : "indentLess", | ||||
"Backspace" : "delSpaceToPrevTabStop", | ||||
"Cmd-/" : "toggleComment", | ||||
"Ctrl-/" : "toggleComment" | ||||
}, | ||||
|
r10418 | mode: 'ipython', | ||
|
r9537 | theme: 'ipython', | ||
matchBrackets: true | ||||
|
r10165 | } | ||
|
r9537 | }; | ||
|
r4352 | CodeCell.prototype = new IPython.Cell(); | ||
|
r8768 | /** | ||
* @method auto_highlight | ||||
*/ | ||||
|
r8202 | CodeCell.prototype.auto_highlight = function () { | ||
|
r13210 | this._auto_highlight(IPython.config.cell_magic_highlight); | ||
|
r8202 | }; | ||
|
r8768 | /** @method create_element */ | ||
|
r4352 | CodeCell.prototype.create_element = function () { | ||
|
r9073 | IPython.Cell.prototype.create_element.apply(this, arguments); | ||
|
r9055 | |||
|
r10217 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); | ||
|
r4629 | cell.attr('tabindex','2'); | ||
|
r9142 | |||
|
r10215 | var input = $('<div></div>').addClass('input'); | ||
|
r13776 | var prompt = $('<div/>').addClass('prompt input_prompt'); | ||
var inner_cell = $('<div/>').addClass('inner_cell'); | ||||
this.celltoolbar = new IPython.CellToolbar(this); | ||||
inner_cell.append(this.celltoolbar.element); | ||||
|
r9422 | var input_area = $('<div/>').addClass('input_area'); | ||
|
r9537 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); | ||
|
r10065 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); | ||
|
r13776 | inner_cell.append(input_area); | ||
input.append(prompt).append(inner_cell); | ||||
|
r14219 | |||
|
r14234 | var widget_area = $('<div/>') | ||
.addClass('widget_area') | ||||
.hide(); | ||||
this.widget_area = widget_area; | ||||
var widget_prompt = $('<div/>') | ||||
.addClass('prompt') | ||||
.appendTo(widget_area); | ||||
var widget_subarea = $('<div/>') | ||||
.addClass('widget_subarea') | ||||
.appendTo(widget_area); | ||||
this.widget_subarea = widget_subarea; | ||||
var widget_clear_buton = $('<button />') | ||||
.addClass('close') | ||||
.addClass('widget_div_clear') | ||||
.html('×') | ||||
.click(function() { | ||||
widget_area.slideUp('', function(){ widget_subarea.html(''); }); | ||||
}) | ||||
.appendTo(widget_prompt); | ||||
|
r14219 | |||
|
r7177 | var output = $('<div></div>'); | ||
|
r14219 | cell.append(input).append(widget_area).append(output); | ||
|
r4352 | this.element = cell; | ||
|
r7205 | this.output_area = new IPython.OutputArea(output, true); | ||
|
r13574 | this.completer = new IPython.Completer(this); | ||
|
r4352 | }; | ||
|
r14015 | /** @method bind_events */ | ||
CodeCell.prototype.bind_events = function () { | ||||
IPython.Cell.prototype.bind_events.apply(this); | ||||
var that = this; | ||||
this.element.focusout( | ||||
function() { that.auto_highlight(); } | ||||
); | ||||
}; | ||||
|
r14021 | CodeCell.prototype.handle_keyevent = function (editor, event) { | ||
|
r14089 | // console.log('CM', this.mode, event.which, event.type) | ||
|
r14021 | |||
if (this.mode === 'command') { | ||||
return true; | ||||
} else if (this.mode === 'edit') { | ||||
return this.handle_codemirror_keyevent(editor, event); | ||||
} | ||||
}; | ||||
|
r8768 | /** | ||
* This method gets called in CodeMirror's onKeyDown/onKeyPress | ||||
* handlers and is used to provide custom key handling. Its return | ||||
* value is used to determine if CodeMirror should ignore the event: | ||||
* true = ignore, false = don't ignore. | ||||
* @method handle_codemirror_keyevent | ||||
*/ | ||||
|
r4378 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | ||
|
r14021 | |||
|
r5398 | var that = this; | ||
|
r14021 | // whatever key is pressed, first, cancel the tooltip request before | ||
// they are sent, and remove tooltip if any, except for tab again | ||||
|
r14062 | var tooltip_closed = null; | ||
|
r14021 | if (event.type === 'keydown' && event.which != key.TAB ) { | ||
|
r14062 | tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip(); | ||
|
r14021 | } | ||
|
r6059 | |||
|
r14021 | var cur = editor.getCursor(); | ||
if (event.keyCode === key.ENTER){ | ||||
this.auto_highlight(); | ||||
} | ||||
|
r6059 | |||
|
r14077 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey || event.altKey)) { | ||
|
r14021 | // Always ignore shift-enter in CodeMirror as we handle it. | ||
return true; | ||||
} else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | ||||
// triger on keypress (!) otherwise inconsistent event.which depending on plateform | ||||
// browser and keyboard layout ! | ||||
// Pressing '(' , request tooltip, don't forget to reappend it | ||||
// The second argument says to hide the tooltip if the docstring | ||||
// is actually empty | ||||
IPython.tooltip.pending(that, true); | ||||
} else if (event.which === key.UPARROW && event.type === 'keydown') { | ||||
// If we are not at the top, let CM handle the up arrow and | ||||
// prevent the global keydown handler from handling it. | ||||
if (!that.at_top()) { | ||||
event.stop(); | ||||
return false; | ||||
} else { | ||||
|
r7339 | return true; | ||
|
r14021 | } | ||
|
r14062 | } else if (event.which === key.ESC && event.type === 'keydown') { | ||
// First see if the tooltip is active and if so cancel it. | ||||
if (tooltip_closed) { | ||||
// The call to remove_and_cancel_tooltip above in L177 doesn't pass | ||||
// force=true. Because of this it won't actually close the tooltip | ||||
// if it is in sticky mode. Thus, we have to check again if it is open | ||||
// and close it with force=true. | ||||
if (!IPython.tooltip._hidden) { | ||||
IPython.tooltip.remove_and_cancel_tooltip(true); | ||||
} | ||||
// If we closed the tooltip, don't let CM or the global handlers | ||||
// handle this event. | ||||
event.stop(); | ||||
return true; | ||||
} | ||||
if (that.code_mirror.options.keyMap === "vim-insert") { | ||||
// vim keyMap is active and in insert mode. In this case we leave vim | ||||
// insert mode, but remain in notebook edit mode. | ||||
// Let' CM handle this event and prevent global handling. | ||||
event.stop(); | ||||
return false; | ||||
} else { | ||||
// vim keyMap is not active. Leave notebook edit mode. | ||||
// Don't let CM handle the event, defer to global handling. | ||||
return true; | ||||
} | ||||
|
r14021 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | ||
// If we are not at the bottom, let CM handle the down arrow and | ||||
// prevent the global keydown handler from handling it. | ||||
if (!that.at_bottom()) { | ||||
event.stop(); | ||||
return false; | ||||
} else { | ||||
|
r7339 | return true; | ||
|
r14021 | } | ||
|
r14062 | } else if (event.keyCode === key.TAB && event.type === 'keydown' && event.shiftKey) { | ||
|
r14021 | if (editor.somethingSelected()){ | ||
var anchor = editor.getCursor("anchor"); | ||||
var head = editor.getCursor("head"); | ||||
if( anchor.line != head.line){ | ||||
return false; | ||||
|
r8949 | } | ||
} | ||||
IPython.tooltip.request(that); | ||||
event.stop(); | ||||
return true; | ||||
|
r7193 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { | ||
|
r4555 | // Tab completion. | ||
|
r13572 | IPython.tooltip.remove_and_cancel_tooltip(); | ||
if (editor.somethingSelected()) { | ||||
return false; | ||||
} | ||||
|
r5398 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | ||
if (pre_cursor.trim() === "") { | ||||
|
r5016 | // Don't autocomplete if the part of the line before the cursor | ||
// is empty. In this case, let CodeMirror handle indentation. | ||||
|
r4393 | return false; | ||
} else { | ||||
|
r14021 | event.stop(); | ||
this.completer.startCompletion(); | ||||
return true; | ||||
} | ||||
} else { | ||||
// keypress/keyup also trigger on TAB press, and we don't want to | ||||
// use those to disable tab completion. | ||||
|
r4378 | return false; | ||
|
r13210 | } | ||
|
r5479 | return false; | ||
|
r4378 | }; | ||
|
r7168 | // Kernel related calls. | ||
|
r13133 | CodeCell.prototype.set_kernel = function (kernel) { | ||
this.kernel = kernel; | ||||
|
r13210 | }; | ||
|
r7197 | |||
|
r8768 | /** | ||
* Execute current code cell to the kernel | ||||
* @method execute | ||||
*/ | ||||
|
r7168 | CodeCell.prototype.execute = function () { | ||
|
r13102 | this.output_area.clear_output(); | ||
|
r14248 | |||
// Clear widget area | ||||
|
r14235 | this.widget_subarea.html(''); | ||
this.widget_subarea.height(''); | ||||
this.widget_area.height(''); | ||||
|
r14248 | this.widget_area.hide(); | ||
|
r7168 | this.set_input_prompt('*'); | ||
this.element.addClass("running"); | ||||
|
r13225 | if (this.last_msg_id) { | ||
this.kernel.clear_callbacks_for_msg(this.last_msg_id); | ||||
} | ||||
var callbacks = this.get_callbacks(); | ||||
this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); | ||||
}; | ||||
/** | ||||
* Construct the default callbacks for | ||||
* @method get_callbacks | ||||
*/ | ||||
CodeCell.prototype.get_callbacks = function () { | ||||
return { | ||||
|
r13207 | shell : { | ||
reply : $.proxy(this._handle_execute_reply, this), | ||||
payload : { | ||||
set_next_input : $.proxy(this._handle_set_next_input, this), | ||||
|
r13225 | page : $.proxy(this._open_with_pager, this) | ||
|
r13207 | } | ||
}, | ||||
iopub : { | ||||
output : $.proxy(this.output_area.handle_output, this.output_area), | ||||
clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area), | ||||
}, | ||||
input : $.proxy(this._handle_input_request, this) | ||||
|
r13210 | }; | ||
|
r13225 | }; | ||
CodeCell.prototype._open_with_pager = function (payload) { | ||||
$([IPython.events]).trigger('open_with_text.Pager', payload); | ||||
|
r7168 | }; | ||
|
r8768 | /** | ||
* @method _handle_execute_reply | ||||
* @private | ||||
*/ | ||||
|
r13207 | CodeCell.prototype._handle_execute_reply = function (msg) { | ||
this.set_input_prompt(msg.content.execution_count); | ||||
|
r7168 | this.element.removeClass("running"); | ||
|
r10781 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); | ||
|
r13210 | }; | ||
|
r4389 | |||
|
r10368 | /** | ||
* @method _handle_set_next_input | ||||
* @private | ||||
*/ | ||||
|
r13207 | CodeCell.prototype._handle_set_next_input = function (payload) { | ||
|
r13210 | var data = {'cell': this, 'text': payload.text}; | ||
|
r7223 | $([IPython.events]).trigger('set_next_input.Notebook', data); | ||
|
r13210 | }; | ||
|
r12458 | |||
|
r10368 | /** | ||
* @method _handle_input_request | ||||
* @private | ||||
*/ | ||||
|
r13207 | CodeCell.prototype._handle_input_request = function (msg) { | ||
this.output_area.append_raw_input(msg); | ||||
|
r13210 | }; | ||
|
r10368 | |||
|
r7223 | |||
|
r7168 | // Basic cell manipulation. | ||
|
r4352 | CodeCell.prototype.select = function () { | ||
|
r14015 | var cont = IPython.Cell.prototype.select.apply(this); | ||
if (cont) { | ||||
|
r14014 | this.code_mirror.refresh(); | ||
this.auto_highlight(); | ||||
|
r14092 | } | ||
|
r14015 | return cont; | ||
|
r4352 | }; | ||
|
r14014 | CodeCell.prototype.render = function () { | ||
|
r14015 | var cont = IPython.Cell.prototype.render.apply(this); | ||
// Always execute, even if we are already in the rendered state | ||||
return cont; | ||||
|
r14013 | }; | ||
|
r14015 | |||
|
r14014 | CodeCell.prototype.unrender = function () { | ||
|
r14015 | // CodeCell is always rendered | ||
return false; | ||||
|
r14013 | }; | ||
|
r4352 | |||
|
r14014 | CodeCell.prototype.edit_mode = function () { | ||
|
r14015 | var cont = IPython.Cell.prototype.edit_mode.apply(this); | ||
if (cont) { | ||||
|
r14014 | this.focus_editor(); | ||
|
r14092 | } | ||
|
r14015 | return cont; | ||
|
r14014 | } | ||
|
r4675 | CodeCell.prototype.select_all = function () { | ||
var start = {line: 0, ch: 0}; | ||||
var nlines = this.code_mirror.lineCount(); | ||||
var last_line = this.code_mirror.getLine(nlines-1); | ||||
var end = {line: nlines-1, ch: last_line.length}; | ||||
this.code_mirror.setSelection(start, end); | ||||
}; | ||||
|
r7168 | CodeCell.prototype.collapse = function () { | ||
|
r7524 | this.collapsed = true; | ||
this.output_area.collapse(); | ||||
|
r7168 | }; | ||
CodeCell.prototype.expand = function () { | ||||
|
r7524 | this.collapsed = false; | ||
this.output_area.expand(); | ||||
|
r7168 | }; | ||
CodeCell.prototype.toggle_output = function () { | ||||
|
r7524 | this.collapsed = Boolean(1 - this.collapsed); | ||
this.output_area.toggle_output(); | ||||
|
r7168 | }; | ||
|
r7429 | CodeCell.prototype.toggle_output_scroll = function () { | ||
this.output_area.toggle_scroll(); | ||||
}; | ||||
|
r8467 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { | ||
var ns = prompt_value || " "; | ||||
|
r13210 | return 'In [' + ns + ']:'; | ||
|
r8467 | }; | ||
|
r8768 | |||
|
r8467 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { | ||
var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; | ||||
|
r13210 | for(var i=1; i < lines_number; i++) { | ||
html.push(['...:']); | ||||
} | ||||
return html.join('<br/>'); | ||||
|
r8467 | }; | ||
|
r8768 | |||
|
r8467 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; | ||
|
r7168 | CodeCell.prototype.set_input_prompt = function (number) { | ||
|
r13210 | var nline = 1; | ||
if (this.code_mirror !== undefined) { | ||||
|
r8467 | nline = this.code_mirror.lineCount(); | ||
} | ||||
|
r8603 | this.input_prompt_number = number; | ||
|
r8467 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); | ||
this.element.find('div.input_prompt').html(prompt_html); | ||||
|
r7168 | }; | ||
CodeCell.prototype.clear_input = function () { | ||||
this.code_mirror.setValue(''); | ||||
}; | ||||
CodeCell.prototype.get_text = function () { | ||||
return this.code_mirror.getValue(); | ||||
}; | ||||
CodeCell.prototype.set_text = function (code) { | ||||
return this.code_mirror.setValue(code); | ||||
}; | ||||
CodeCell.prototype.at_top = function () { | ||||
var cursor = this.code_mirror.getCursor(); | ||||
|
r8168 | if (cursor.line === 0 && cursor.ch === 0) { | ||
|
r7168 | return true; | ||
} else { | ||||
return false; | ||||
} | ||||
}; | ||||
CodeCell.prototype.at_bottom = function () { | ||||
var cursor = this.code_mirror.getCursor(); | ||||
|
r8168 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | ||
|
r7168 | return true; | ||
} else { | ||||
return false; | ||||
} | ||||
}; | ||||
|
r13102 | CodeCell.prototype.clear_output = function (wait) { | ||
this.output_area.clear_output(wait); | ||||
|
r6423 | }; | ||
|
r7168 | |||
// JSON serialization | ||||
|
r4349 | |||
|
r4352 | CodeCell.prototype.fromJSON = function (data) { | ||
|
r7523 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | ||
|
r4352 | if (data.cell_type === 'code') { | ||
|
r4484 | if (data.input !== undefined) { | ||
|
r5943 | this.set_text(data.input); | ||
|
r7587 | // make this value the starting point, so that we can only undo | ||
// to this state, instead of a blank cell | ||||
this.code_mirror.clearHistory(); | ||||
|
r8202 | this.auto_highlight(); | ||
|
r4484 | } | ||
if (data.prompt_number !== undefined) { | ||||
this.set_input_prompt(data.prompt_number); | ||||
} else { | ||||
this.set_input_prompt(); | ||||
|
r13210 | } | ||
|
r7177 | this.output_area.fromJSON(data.outputs); | ||
|
r4533 | if (data.collapsed !== undefined) { | ||
if (data.collapsed) { | ||||
this.collapse(); | ||||
|
r6032 | } else { | ||
this.expand(); | ||||
|
r13210 | } | ||
} | ||||
} | ||||
|
r4352 | }; | ||
CodeCell.prototype.toJSON = function () { | ||||
|
r7523 | var data = IPython.Cell.prototype.toJSON.apply(this); | ||
|
r5943 | data.input = this.get_text(); | ||
|
r13572 | // is finite protect against undefined and '*' value | ||
if (isFinite(this.input_prompt_number)) { | ||||
|
r5479 | data.prompt_number = this.input_prompt_number; | ||
|
r13210 | } | ||
|
r7177 | var outputs = this.output_area.toJSON(); | ||
|
r4497 | data.outputs = outputs; | ||
|
r4484 | data.language = 'python'; | ||
|
r4533 | data.collapsed = this.collapsed; | ||
|
r4484 | return data; | ||
|
r4352 | }; | ||
|
r4497 | |||
|
r4352 | IPython.CodeCell = CodeCell; | ||
return IPython; | ||||
|
r13207 | }(IPython)); | ||