// Copyright (c) IPython Development Team. // Distributed under the terms of the Modified BSD License. /** * * * @module codecell * @namespace codecell * @class CodeCell */ define([ 'base/js/namespace', 'jquery', 'base/js/utils', 'base/js/keyboard', 'services/config', 'notebook/js/cell', 'notebook/js/outputarea', 'notebook/js/completer', 'notebook/js/celltoolbar', 'codemirror/lib/codemirror', 'codemirror/mode/python/python', 'notebook/js/codemirror-ipython' ], function(IPython, $, utils, keyboard, configmod, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip ) { "use strict"; var Cell = cell.Cell; /* 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 keycodes = keyboard.keycodes; var CodeCell = function (kernel, options) { /** * Constructor * * A Cell conceived to write code. * * Parameters: * kernel: Kernel instance * 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. * options: dictionary * Dictionary of keyword arguments. * events: $(Events) instance * config: dictionary * keyboard_manager: KeyboardManager instance * notebook: Notebook instance * tooltip: Tooltip instance */ this.kernel = kernel || null; this.notebook = options.notebook; this.collapsed = false; this.events = options.events; this.tooltip = options.tooltip; this.config = options.config; this.class_config = new configmod.ConfigWithDefaults(this.config, CodeCell.config_defaults, 'CodeCell'); // 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; // Keep a stack of the 'active' output areas (where active means the // output area that recieves output). When a user activates an output // area, it gets pushed to the stack. Then, when the output area is // deactivated, it's popped from the stack. When the stack is empty, // the cell's output area is used. this.active_output_areas = []; var that = this; Object.defineProperty(this, 'active_output_area', { get: function() { if (that.active_output_areas && that.active_output_areas.length > 0) { return that.active_output_areas[that.active_output_areas.length-1]; } else { return that.output_area; } }, }); this.last_msg_id = null; this.completer = null; this.widget_views = []; this._widgets_live = true; Cell.apply(this,[{ config: $.extend({}, CodeCell.options_default), keyboard_manager: options.keyboard_manager, events: this.events}]); // Attributes we want to override in this subclass. this.cell_type = "code"; 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.config_defaults = { cell_magic_highlight : { 'magic_javascript' :{'reg':[/^%%javascript/]}, 'magic_perl' :{'reg':[/^%%perl/]}, 'magic_ruby' :{'reg':[/^%%ruby/]}, 'magic_python' :{'reg':[/^%%python3?/]}, 'magic_shell' :{'reg':[/^%%bash/]}, 'magic_r' :{'reg':[/^%%R/]}, 'magic_text/x-cython' :{'reg':[/^%%cython/]}, }, }; CodeCell.msg_cells = {}; CodeCell.prototype = Object.create(Cell.prototype); /** * @method push_output_area */ CodeCell.prototype.push_output_area = function (output_area) { this.active_output_areas.push(output_area); }; /** * @method pop_output_area */ CodeCell.prototype.pop_output_area = function (output_area) { var index = this.active_output_areas.lastIndexOf(output_area); if (index > -1) { this.active_output_areas.splice(index, 1); } }; /** * @method auto_highlight */ CodeCell.prototype.auto_highlight = function () { this._auto_highlight(this.class_config.get_sync('cell_magic_highlight')); }; /** @method create_element */ CodeCell.prototype.create_element = function () { Cell.prototype.create_element.apply(this, arguments); var cell = $('
').addClass('cell 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 celltoolbar.CellToolbar({ cell: this, notebook: this.notebook}); inner_cell.append(this.celltoolbar.element); var input_area = $('
').addClass('input_area'); this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config); this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this)) $(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 that = this; var widget_clear_buton = $('