##// END OF EJS Templates
tweak history prefix search (up/^p) in qtconsole...
tweak history prefix search (up/^p) in qtconsole moving the cursor around the line could result in weird inconsistencies in the prefix. This simplifies the logic by always using the cursor position to set the history prefix, and better determine when the history prefix has actually changed.

File last commit:

r9142:38560081
r9205:ef8c14cd
Show More
codecell.js
384 lines | 13.1 KiB | application/javascript | JavascriptLexer
Brian E. Granger
More review changes....
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.
//----------------------------------------------------------------------------
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
//============================================================================
// CodeCell
//============================================================================
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* An extendable module that provide base functionnality to create cell for notebook.
* @module IPython
* @namespace IPython
* @submodule CodeCell
*/
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var IPython = (function (IPython) {
Matthias BUSSONNIER
use strict and clean a little....
r7132 "use strict";
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var utils = IPython.utils;
Matthias BUSSONNIER
add a keycodes structure to utils...
r7136 var key = IPython.utils.keycodes;
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js";
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
start docummenting kernel
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
*/
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 var CodeCell = function (kernel) {
Brian Granger
Fixed order of notebook loading and kernel starting....
r7197 this.kernel = kernel || null;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.code_mirror = null;
MinRK
store nonexistent prompt number as null
r5814 this.input_prompt_number = null;
MinRK
restore collapsed state for cells...
r7524 this.collapsed = false;
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 this.default_mode = 'python';
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 IPython.Cell.apply(this, arguments);
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202
var that = this;
this.element.focusout(
Matthias BUSSONNIER
fix some whitespace
r8281 function() { that.auto_highlight(); }
);
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
CodeCell.prototype = new IPython.Cell();
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* @method auto_highlight
*/
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 CodeCell.prototype.auto_highlight = function () {
this._auto_highlight(IPython.config.cell_magic_highlight)
};
Matthias BUSSONNIER
start docummenting kernel
r8768 /** @method create_element */
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.create_element = function () {
Matthias BUSSONNIER
create celltoolbar in cell.js and inherit
r9073 IPython.Cell.prototype.create_element.apply(this, arguments);
Matthias BUSSONNIER
Add a per cell toolbar....
r9055
Brian E. Granger
Pager is working again.
r4361 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
Brian E. Granger
Better tabindex support.
r4629 cell.attr('tabindex','2');
Brian E. Granger
Fixing styling issues with CellToolbar....
r9142
this.celltoolbar = new IPython.CellToolbar(this);
cell.append(this.celltoolbar.element);
Brian E. Granger
More work updating the notebook to use dynamics resizing.
r4360 var input = $('<div></div>').addClass('input hbox');
Brian E. Granger
Updating font-sizing to use the YUI protocol.
r4379 input.append($('<div/>').addClass('prompt input_prompt'));
Brian E. Granger
More work updating the notebook to use dynamics resizing.
r4360 var input_area = $('<div/>').addClass('input_area box-flex1');
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.code_mirror = CodeMirror(input_area.get(0), {
indentUnit : 4,
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 mode: 'python',
theme: 'ipython',
MinRK
add read-only view for notebooks...
r5200 readOnly: this.read_only,
Matthias BUSSONNIER
select correct behavior for backspace in codecell
r8060 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
Adam Davis
Added bracket matching for code cells
r8231 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this),
matchBrackets: true
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 });
input.append(input_area);
Brian Granger
Refactored CodeCell to use new OutputArea object for output....
r7177 var output = $('<div></div>');
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 cell.append(input).append(output);
this.element = cell;
Matthias BUSSONNIER
retab tab to space
r7205 this.output_area = new IPython.OutputArea(output, true);
Matthias BUSSONNIER
use strict and clean a little....
r7132
Matthias BUSSONNIER
fix print view...
r7166 // construct a completer only if class exist
// otherwise no print view
Brian Granger
Fixed order of notebook loading and kernel starting....
r7197 if (IPython.Completer !== undefined)
Matthias BUSSONNIER
fix print view...
r7166 {
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 this.completer = new IPython.Completer(this);
Matthias BUSSONNIER
fix print view...
r7166 }
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Matthias BUSSONNIER
start docummenting kernel
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
*/
Brian E. Granger
Fixing execution related things....
r4378 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
Michael Droettboom
Handle carriage return characters ("\r") in HTML notebook output....
r7339
MinRK
skip codemirror key-event handling when read-only
r5656 if (this.read_only){
return false;
}
Michael Droettboom
Handle carriage return characters ("\r") in HTML notebook output....
r7339
Matthias BUSSONNIER
tooltip on <tab>
r5398 var that = this;
Matthias BUSSONNIER
Add Tootip to notebook....
r5397 // whatever key is pressed, first, cancel the tooltip request before
Matthias BUSSONNIER
multiple tooltip action...
r7160 // they are sent, and remove tooltip if any, except for tab again
Matthias BUSSONNIER
Uppercase constant keycode in utils.js
r7193 if (event.type === 'keydown' && event.which != key.TAB ) {
Matthias Bussonnier
fix scrolltop
r7151 IPython.tooltip.remove_and_cancel_tooltip();
Brian Granger
Making keyboard shortcut for showing line numbers consistent.
r6059 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 var cur = editor.getCursor();
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 if (event.keyCode === key.ENTER){
this.auto_highlight();
}
Brian Granger
Making keyboard shortcut for showing line numbers consistent.
r6059
Matthias BUSSONNIER
Uppercase constant keycode in utils.js
r7193 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
Brian E. Granger
Fixing execution related things....
r4378 // Always ignore shift-enter in CodeMirror as we handle it.
return true;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
Matthias BUSSONNIER
multiple tooltip action...
r7160 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
Matthias BUSSONNIER
Replace trigering tooltip for cross platform indep...
r5403 // browser and keyboard layout !
Matthias BUSSONNIER
Improve tooltip tringgering,make it configurable...
r5399 // Pressing '(' , request tooltip, don't forget to reappend it
Matthias BUSSONNIER
multiple tooltip action...
r7160 IPython.tooltip.pending(that);
Brian Granger
Misc fixes to the code cell and output area.
r7199 } else if (event.which === key.UPARROW && event.type === 'keydown') {
Brian Granger
Updating cell logic....
r5944 // 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 {
Michael Droettboom
Handle carriage return characters ("\r") in HTML notebook output....
r7339 return true;
Brian Granger
Updating cell logic....
r5944 };
Matthias BUSSONNIER
click on close cancell stick
r7206 } else if (event.which === key.ESC) {
IPython.tooltip.remove_and_cancel_tooltip(true);
return true;
Brian Granger
Misc fixes to the code cell and output area.
r7199 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
Brian Granger
Updating cell logic....
r5944 // 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 {
Michael Droettboom
Handle carriage return characters ("\r") in HTML notebook output....
r7339 return true;
Brian Granger
Updating cell logic....
r5944 };
Bussonnier Matthias
shift tqb for tooltip
r8949 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
if (editor.somethingSelected()){
var anchor = editor.getCursor("anchor");
var head = editor.getCursor("head");
if( anchor.line != head.line){
return false;
}
}
IPython.tooltip.request(that);
event.stop();
return true;
Matthias BUSSONNIER
Uppercase constant keycode in utils.js
r7193 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
Brian E. Granger
Fixing tab completion edge cases.
r4555 // Tab completion.
Matthias BUSSONNIER
tooltip on <tab>
r5398 //Do not trim here because of tooltip
Matthias BUSSONNIER
Don't catch tab press when something selected...
r8587 if (editor.somethingSelected()){return false}
Matthias BUSSONNIER
tooltip on <tab>
r5398 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
if (pre_cursor.trim() === "") {
Fernando Perez
Add Ctrl-L as a way to toggle line-numbers for any individual code cell
r5016 // Don't autocomplete if the part of the line before the cursor
// is empty. In this case, let CodeMirror handle indentation.
Brian E. Granger
Notebook now uses tab for autocompletion.
r4393 return false;
Bussonnier Matthias
shift tqb for tooltip
r8949 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
Matthias BUSSONNIER
multiple tooltip action...
r7160 IPython.tooltip.request(that);
Brian Granger
Fixing #1337. Tooltip stops TAB from being handled by others....
r6052 // Prevent the event from bubbling up.
event.stop();
// Prevent CodeMirror from handling the tab.
return true;
Brian E. Granger
Notebook now uses tab for autocompletion.
r4393 } else {
event.stop();
Matthias BUSSONNIER
use strict and clean a little....
r7132 this.completer.startCompletion();
Brian E. Granger
Notebook now uses tab for autocompletion.
r4393 return true;
Brian Granger
Making keyboard shortcut for showing line numbers consistent.
r6059 };
} else {
Fernando Perez
Add Ctrl-L as a way to toggle line-numbers for any individual code cell
r5016 // keypress/keyup also trigger on TAB press, and we don't want to
// use those to disable tab completion.
Brian E. Granger
Fixing execution related things....
r4378 return false;
};
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 return false;
Brian E. Granger
Fixing execution related things....
r4378 };
Brian Granger
Fixed order of notebook loading and kernel starting....
r7197
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 // Kernel related calls.
Brian Granger
Fixed order of notebook loading and kernel starting....
r7197 CodeCell.prototype.set_kernel = function (kernel) {
this.kernel = kernel;
}
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* Execute current code cell to the kernel
* @method execute
*/
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 CodeCell.prototype.execute = function () {
Brian Granger
Refactored CodeCell to use new OutputArea object for output....
r7177 this.output_area.clear_output(true, true, true);
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 this.set_input_prompt('*');
this.element.addClass("running");
var callbacks = {
'execute_reply': $.proxy(this._handle_execute_reply, this),
Brian Granger
Refactored CodeCell to use new OutputArea object for output....
r7177 'output': $.proxy(this.output_area.handle_output, this.output_area),
'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
Brian Granger
Removing cell from execute callbacks in kernel.js.
r7223 'set_next_input': $.proxy(this._handle_set_next_input, this)
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
Brian Granger
Adding options to Kernel.execute with a default of silent=true.
r7176 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* @method _handle_execute_reply
* @private
*/
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 CodeCell.prototype._handle_execute_reply = function (content) {
this.set_input_prompt(content.execution_count);
this.element.removeClass("running");
Brian Granger
Making Notebook.set_dirty an event so CodeCell can set it....
r7228 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
Matthias BUSSONNIER
move more code into the completer itself
r7141 }
Brian E. Granger
Autocompletion working with CTRL-SPACE.
r4389
Brian Granger
Removing cell from execute callbacks in kernel.js.
r7223 CodeCell.prototype._handle_set_next_input = function (text) {
var data = {'cell': this, 'text': text}
$([IPython.events]).trigger('set_next_input.Notebook', data);
}
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 // Basic cell manipulation.
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
Brian Granger
Fixing bugs that have shown up since updating CM to 2.2.
r5942 this.code_mirror.refresh();
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.code_mirror.focus();
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 this.auto_highlight();
Brian Granger
Removing extra refresh that is no longer needed because of CM fix.
r5971 // We used to need an additional refresh() after the focus, but
// it appears that this has been fixed in CM. This bug would show
// up on FF when a newly loaded markdown cell was edited.
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Ctrl-Enter now does not delete input, but selects it.
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);
};
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 CodeCell.prototype.collapse = function () {
MinRK
restore collapsed state for cells...
r7524 this.collapsed = true;
this.output_area.collapse();
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
CodeCell.prototype.expand = function () {
MinRK
restore collapsed state for cells...
r7524 this.collapsed = false;
this.output_area.expand();
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
CodeCell.prototype.toggle_output = function () {
MinRK
restore collapsed state for cells...
r7524 this.collapsed = Boolean(1 - this.collapsed);
this.output_area.toggle_output();
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
MinRK
add ^M-O for toggling output scroll
r7429 CodeCell.prototype.toggle_output_scroll = function () {
this.output_area.toggle_scroll();
};
Bussonnier Matthias
add ability to create continuation prompt
r8467 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
var ns = prompt_value || "&nbsp;";
return 'In&nbsp;[' + ns + ']:'
};
Matthias BUSSONNIER
start docummenting kernel
r8768
Bussonnier Matthias
add ability to create continuation prompt
r8467 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
for(var i=1; i < lines_number; i++){html.push(['...:'])};
return html.join('</br>')
};
Matthias BUSSONNIER
start docummenting kernel
r8768
Bussonnier Matthias
add ability to create continuation prompt
r8467 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 CodeCell.prototype.set_input_prompt = function (number) {
Bussonnier Matthias
add ability to create continuation prompt
r8467 var nline = 1
if( this.code_mirror != undefined) {
nline = this.code_mirror.lineCount();
}
Paul Ivanov
clear In[ ] prompt numbers again
r8603 this.input_prompt_number = number;
Bussonnier Matthias
add ability to create continuation prompt
r8467 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
this.element.find('div.input_prompt').html(prompt_html);
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
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();
Bradley M. Froehle
notebook: up/down arrow keys move to begin/end of line at top/bottom of cell...
r8168 if (cursor.line === 0 && cursor.ch === 0) {
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 return true;
} else {
return false;
}
};
CodeCell.prototype.at_bottom = function () {
var cursor = this.code_mirror.getCursor();
Bradley M. Froehle
notebook: up/down arrow keys move to begin/end of line at top/bottom of cell...
r8168 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 return true;
} else {
return false;
}
};
MinRK
add channel-selection to clear_output...
r5085 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
Brian Granger
Refactored CodeCell to use new OutputArea object for output....
r7177 this.output_area.clear_output(stdout, stderr, other);
MinRK
[notebook] clear_output is handled after a delay...
r6423 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
// JSON serialization
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.fromJSON = function (data) {
MinRK
add empty metadata field on cells/worksheets...
r7523 IPython.Cell.prototype.fromJSON.apply(this, arguments);
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 if (data.cell_type === 'code') {
Brian E. Granger
Massive work on the notebook document format....
r4484 if (data.input !== undefined) {
Brian Granger
Work on the base Cell API....
r5943 this.set_text(data.input);
Paul Ivanov
fix for #1678, undo no longer clears cells...
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();
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 this.auto_highlight();
Brian E. Granger
Massive work on the notebook document format....
r4484 }
if (data.prompt_number !== undefined) {
this.set_input_prompt(data.prompt_number);
} else {
this.set_input_prompt();
};
Brian Granger
Refactored CodeCell to use new OutputArea object for output....
r7177 this.output_area.fromJSON(data.outputs);
Brian E. Granger
Added collapsed field to the code cell.
r4533 if (data.collapsed !== undefined) {
if (data.collapsed) {
this.collapse();
Brian Granger
Fixing minor bugs in nbformat and saving....
r6032 } else {
this.expand();
Brian E. Granger
Added collapsed field to the code cell.
r4533 };
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
};
CodeCell.prototype.toJSON = function () {
MinRK
add empty metadata field on cells/worksheets...
r7523 var data = IPython.Cell.prototype.toJSON.apply(this);
Brian Granger
Work on the base Cell API....
r5943 data.input = this.get_text();
Brian E. Granger
Massive work on the notebook document format....
r4484 data.cell_type = 'code';
MinRK
store nonexistent prompt number as null
r5814 if (this.input_prompt_number) {
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 data.prompt_number = this.input_prompt_number;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian Granger
Refactored CodeCell to use new OutputArea object for output....
r7177 var outputs = this.output_area.toJSON();
Brian E. Granger
Added saving and loading of output of all types.
r4497 data.outputs = outputs;
Brian E. Granger
Massive work on the notebook document format....
r4484 data.language = 'python';
Brian E. Granger
Added collapsed field to the code cell.
r4533 data.collapsed = this.collapsed;
Brian E. Granger
Massive work on the notebook document format....
r4484 return data;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Added saving and loading of output of all types.
r4497
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 IPython.CodeCell = CodeCell;
return IPython;
}(IPython));