//---------------------------------------------------------------------------- // 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. //---------------------------------------------------------------------------- //============================================================================ // CodeCell //============================================================================ /** * An extendable module that provide base functionnality to create cell for notebook. * @module IPython * @namespace IPython * @submodule CodeCell */ /* local util for codemirror */ var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}; /** * * 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); if (!posEq(from, to)) { cm.replaceRange("", from, to); return; } 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; from = {ch:cur.ch-chToPrevTabStop,line:cur.line}; var select = cm.getRange(from,cur); if( select.match(/^\ +$/) !== null){ cm.replaceRange("",from,cur); } else { cm.deleteH(-1,"char"); } }; var IPython = (function (IPython) { "use strict"; var utils = IPython.utils; var key = IPython.utils.keycodes; /** * 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 * @param {object|undefined} [options] * @param [options.cm_config] {object} config to pass to CodeMirror */ var CodeCell = function (kernel, options) { this.kernel = kernel || null; this.collapsed = false; // 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; var cm_overwrite_options = { onKeyEvent: $.proxy(this.handle_keyevent,this) }; options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); IPython.Cell.apply(this,[options]); // Attributes we want to override in this subclass. this.cell_type = "code"; var that = this; this.element.focusout( function() { that.auto_highlight(); } ); }; CodeCell.options_default = { cm_config : { extraKeys: { "Tab" : "indentMore", "Shift-Tab" : "indentLess", "Backspace" : "delSpaceToPrevTabStop", "Cmd-/" : "toggleComment", "Ctrl-/" : "toggleComment" }, mode: 'ipython', theme: 'ipython', matchBrackets: true } }; CodeCell.prototype = new IPython.Cell(); /** * @method auto_highlight */ CodeCell.prototype.auto_highlight = function () { this._auto_highlight(IPython.config.cell_magic_highlight); }; /** @method create_element */ CodeCell.prototype.create_element = function () { IPython.Cell.prototype.create_element.apply(this, arguments); var cell = $('
').addClass('cell border-box-sizing code_cell'); cell.attr('tabindex','2'); var input = $('
').addClass('input'); var prompt = $('
').addClass('prompt input_prompt'); var inner_cell = $('
').addClass('inner_cell'); this.celltoolbar = new IPython.CellToolbar(this); inner_cell.append(this.celltoolbar.element); var input_area = $('
').addClass('input_area'); this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); $(this.code_mirror.getInputField()).attr("spellcheck", "false"); inner_cell.append(input_area); input.append(prompt).append(inner_cell); var widget_area = $('
') .addClass('widget-area') .hide(); this.widget_area = widget_area; var widget_prompt = $('
') .addClass('prompt') .appendTo(widget_area); var widget_subarea = $('
') .addClass('widget-subarea') .appendTo(widget_area); this.widget_subarea = widget_subarea; var widget_clear_buton = $('