// 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', '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, 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; // 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 config = utils.mergeopt(CodeCell, this.config); Cell.apply(this,[{ config: config, keyboard_manager: options.keyboard_manager, events: this.events}]); // 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.msg_cells = {}; CodeCell.prototype = Object.create(Cell.prototype); /** * @method auto_highlight */ CodeCell.prototype.auto_highlight = function () { this._auto_highlight(this.config.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 widget_clear_buton = $('