Show More
@@ -1,442 +1,446 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
2 | // Copyright (C) 2008-2011 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // CodeCell |
|
9 | // CodeCell | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
12 | * An extendable module that provide base functionnality to create cell for notebook. | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule CodeCell |
|
15 | * @submodule CodeCell | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | /* local util for codemirror */ |
|
19 | /* local util for codemirror */ | |
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} |
|
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} | |
21 |
|
21 | |||
22 | /** |
|
22 | /** | |
23 | * |
|
23 | * | |
24 | * function to delete until previous non blanking space character |
|
24 | * function to delete until previous non blanking space character | |
25 | * or first multiple of 4 tabstop. |
|
25 | * or first multiple of 4 tabstop. | |
26 | * @private |
|
26 | * @private | |
27 | */ |
|
27 | */ | |
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ |
|
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ | |
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); |
|
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); | |
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} |
|
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} | |
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); |
|
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); | |
32 | var tabsize = cm.getOption('tabSize'); |
|
32 | var tabsize = cm.getOption('tabSize'); | |
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; |
|
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; | |
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} |
|
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} | |
35 | var select = cm.getRange(from,cur) |
|
35 | var select = cm.getRange(from,cur) | |
36 | if( select.match(/^\ +$/) != null){ |
|
36 | if( select.match(/^\ +$/) != null){ | |
37 | cm.replaceRange("",from,cur) |
|
37 | cm.replaceRange("",from,cur) | |
38 | } else { |
|
38 | } else { | |
39 | cm.deleteH(-1,"char") |
|
39 | cm.deleteH(-1,"char") | |
40 | } |
|
40 | } | |
41 | }; |
|
41 | }; | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | var IPython = (function (IPython) { |
|
44 | var IPython = (function (IPython) { | |
45 | "use strict"; |
|
45 | "use strict"; | |
46 |
|
46 | |||
47 | var utils = IPython.utils; |
|
47 | var utils = IPython.utils; | |
48 | var key = IPython.utils.keycodes; |
|
48 | var key = IPython.utils.keycodes; | |
49 |
|
49 | |||
50 | /** |
|
50 | /** | |
51 | * A Cell conceived to write code. |
|
51 | * A Cell conceived to write code. | |
52 | * |
|
52 | * | |
53 | * The kernel doesn't have to be set at creation time, in that case |
|
53 | * The kernel doesn't have to be set at creation time, in that case | |
54 | * it will be null and set_kernel has to be called later. |
|
54 | * it will be null and set_kernel has to be called later. | |
55 | * @class CodeCell |
|
55 | * @class CodeCell | |
56 | * @extends IPython.Cell |
|
56 | * @extends IPython.Cell | |
57 | * |
|
57 | * | |
58 | * @constructor |
|
58 | * @constructor | |
59 | * @param {Object|null} kernel |
|
59 | * @param {Object|null} kernel | |
60 | * @param {object|undefined} [options] |
|
60 | * @param {object|undefined} [options] | |
61 | * @param [options.cm_config] {object} config to pass to CodeMirror |
|
61 | * @param [options.cm_config] {object} config to pass to CodeMirror | |
62 | */ |
|
62 | */ | |
63 | var CodeCell = function (kernel, options) { |
|
63 | var CodeCell = function (kernel, options) { | |
64 | this.kernel = kernel || null; |
|
64 | this.kernel = kernel || null; | |
65 | this.code_mirror = null; |
|
65 | this.code_mirror = null; | |
66 | this.input_prompt_number = null; |
|
66 | this.input_prompt_number = null; | |
67 | this.collapsed = false; |
|
67 | this.collapsed = false; | |
68 | this.cell_type = "code"; |
|
68 | this.cell_type = "code"; | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | var cm_overwrite_options = { |
|
71 | var cm_overwrite_options = { | |
72 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
72 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
73 | }; |
|
73 | }; | |
74 |
|
74 | |||
75 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); |
|
75 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); | |
76 |
|
76 | |||
77 | IPython.Cell.apply(this,[options]); |
|
77 | IPython.Cell.apply(this,[options]); | |
78 |
|
78 | |||
79 | var that = this; |
|
79 | var that = this; | |
80 | this.element.focusout( |
|
80 | this.element.focusout( | |
81 | function() { that.auto_highlight(); } |
|
81 | function() { that.auto_highlight(); } | |
82 | ); |
|
82 | ); | |
83 | }; |
|
83 | }; | |
84 |
|
84 | |||
85 | CodeCell.options_default = { |
|
85 | CodeCell.options_default = { | |
86 | cm_config : { |
|
86 | cm_config : { | |
87 | extraKeys: { |
|
87 | extraKeys: { | |
88 | "Tab" : "indentMore", |
|
88 | "Tab" : "indentMore", | |
89 | "Shift-Tab" : "indentLess", |
|
89 | "Shift-Tab" : "indentLess", | |
90 | "Backspace" : "delSpaceToPrevTabStop", |
|
90 | "Backspace" : "delSpaceToPrevTabStop", | |
91 | "Cmd-/" : "toggleComment", |
|
91 | "Cmd-/" : "toggleComment", | |
92 | "Ctrl-/" : "toggleComment" |
|
92 | "Ctrl-/" : "toggleComment" | |
93 | }, |
|
93 | }, | |
94 | mode: 'ipython', |
|
94 | mode: 'ipython', | |
95 | theme: 'ipython', |
|
95 | theme: 'ipython', | |
96 | matchBrackets: true |
|
96 | matchBrackets: true | |
97 | } |
|
97 | } | |
98 | }; |
|
98 | }; | |
99 |
|
99 | |||
100 |
|
100 | |||
101 | CodeCell.prototype = new IPython.Cell(); |
|
101 | CodeCell.prototype = new IPython.Cell(); | |
102 |
|
102 | |||
103 | /** |
|
103 | /** | |
104 | * @method auto_highlight |
|
104 | * @method auto_highlight | |
105 | */ |
|
105 | */ | |
106 | CodeCell.prototype.auto_highlight = function () { |
|
106 | CodeCell.prototype.auto_highlight = function () { | |
107 | this._auto_highlight(IPython.config.cell_magic_highlight) |
|
107 | this._auto_highlight(IPython.config.cell_magic_highlight) | |
108 | }; |
|
108 | }; | |
109 |
|
109 | |||
110 | /** @method create_element */ |
|
110 | /** @method create_element */ | |
111 | CodeCell.prototype.create_element = function () { |
|
111 | CodeCell.prototype.create_element = function () { | |
112 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
112 | IPython.Cell.prototype.create_element.apply(this, arguments); | |
113 |
|
113 | |||
114 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); |
|
114 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); | |
115 | cell.attr('tabindex','2'); |
|
115 | cell.attr('tabindex','2'); | |
116 |
|
116 | |||
117 | this.celltoolbar = new IPython.CellToolbar(this); |
|
117 | this.celltoolbar = new IPython.CellToolbar(this); | |
118 |
|
118 | |||
119 | var input = $('<div></div>').addClass('input'); |
|
119 | var input = $('<div></div>').addClass('input'); | |
120 | var vbox = $('<div/>').addClass('vbox box-flex1') |
|
120 | var vbox = $('<div/>').addClass('vbox box-flex1') | |
121 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
121 | input.append($('<div/>').addClass('prompt input_prompt')); | |
122 | vbox.append(this.celltoolbar.element); |
|
122 | vbox.append(this.celltoolbar.element); | |
123 | var input_area = $('<div/>').addClass('input_area'); |
|
123 | var input_area = $('<div/>').addClass('input_area'); | |
124 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); |
|
124 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); | |
125 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); |
|
125 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); | |
126 | vbox.append(input_area); |
|
126 | vbox.append(input_area); | |
127 | input.append(vbox); |
|
127 | input.append(vbox); | |
128 | var output = $('<div></div>'); |
|
128 | var output = $('<div></div>'); | |
129 | cell.append(input).append(output); |
|
129 | cell.append(input).append(output); | |
130 | this.element = cell; |
|
130 | this.element = cell; | |
131 | this.output_area = new IPython.OutputArea(output, true); |
|
131 | this.output_area = new IPython.OutputArea(output, true); | |
132 |
|
132 | |||
133 | // construct a completer only if class exist |
|
133 | // construct a completer only if class exist | |
134 | // otherwise no print view |
|
134 | // otherwise no print view | |
135 | if (IPython.Completer !== undefined) |
|
135 | if (IPython.Completer !== undefined) | |
136 | { |
|
136 | { | |
137 | this.completer = new IPython.Completer(this); |
|
137 | this.completer = new IPython.Completer(this); | |
138 | } |
|
138 | } | |
139 | }; |
|
139 | }; | |
140 |
|
140 | |||
141 | /** |
|
141 | /** | |
142 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
142 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
143 | * handlers and is used to provide custom key handling. Its return |
|
143 | * handlers and is used to provide custom key handling. Its return | |
144 | * value is used to determine if CodeMirror should ignore the event: |
|
144 | * value is used to determine if CodeMirror should ignore the event: | |
145 | * true = ignore, false = don't ignore. |
|
145 | * true = ignore, false = don't ignore. | |
146 | * @method handle_codemirror_keyevent |
|
146 | * @method handle_codemirror_keyevent | |
147 | */ |
|
147 | */ | |
148 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
148 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
149 |
|
149 | |||
150 | var that = this; |
|
150 | var that = this; | |
151 | // whatever key is pressed, first, cancel the tooltip request before |
|
151 | // whatever key is pressed, first, cancel the tooltip request before | |
152 | // they are sent, and remove tooltip if any, except for tab again |
|
152 | // they are sent, and remove tooltip if any, except for tab again | |
153 | if (event.type === 'keydown' && event.which != key.TAB ) { |
|
153 | if (event.type === 'keydown' && event.which != key.TAB ) { | |
154 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
154 | IPython.tooltip.remove_and_cancel_tooltip(); | |
155 | }; |
|
155 | }; | |
156 |
|
156 | |||
157 | var cur = editor.getCursor(); |
|
157 | var cur = editor.getCursor(); | |
158 | if (event.keyCode === key.ENTER){ |
|
158 | if (event.keyCode === key.ENTER){ | |
159 | this.auto_highlight(); |
|
159 | this.auto_highlight(); | |
160 | } |
|
160 | } | |
161 |
|
161 | |||
162 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { |
|
162 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { | |
163 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
163 | // Always ignore shift-enter in CodeMirror as we handle it. | |
164 | return true; |
|
164 | return true; | |
165 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
|
165 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | |
166 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
166 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform | |
167 | // browser and keyboard layout ! |
|
167 | // browser and keyboard layout ! | |
168 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
168 | // Pressing '(' , request tooltip, don't forget to reappend it | |
169 |
// The second argument says to hide the tooltip if the docstring |
|
169 | // The second argument says to hide the tooltip if the docstring | |
170 | // is actually empty |
|
170 | // is actually empty | |
171 | IPython.tooltip.pending(that, true); |
|
171 | IPython.tooltip.pending(that, true); | |
172 | } else if (event.which === key.UPARROW && event.type === 'keydown') { |
|
172 | } else if (event.which === key.UPARROW && event.type === 'keydown') { | |
173 | // If we are not at the top, let CM handle the up arrow and |
|
173 | // If we are not at the top, let CM handle the up arrow and | |
174 | // prevent the global keydown handler from handling it. |
|
174 | // prevent the global keydown handler from handling it. | |
175 | if (!that.at_top()) { |
|
175 | if (!that.at_top()) { | |
176 | event.stop(); |
|
176 | event.stop(); | |
177 | return false; |
|
177 | return false; | |
178 | } else { |
|
178 | } else { | |
179 | return true; |
|
179 | return true; | |
180 | }; |
|
180 | }; | |
181 | } else if (event.which === key.ESC) { |
|
181 | } else if (event.which === key.ESC) { | |
182 |
IPython.tooltip. |
|
182 | if (!IPython.tooltip._hidden) { | |
183 | return true; |
|
183 | IPython.tooltip.remove_and_cancel_tooltip(true); | |
|
184 | return true; | |||
|
185 | } else { | |||
|
186 | return false; | |||
|
187 | } | |||
184 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
188 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
185 | // If we are not at the bottom, let CM handle the down arrow and |
|
189 | // If we are not at the bottom, let CM handle the down arrow and | |
186 | // prevent the global keydown handler from handling it. |
|
190 | // prevent the global keydown handler from handling it. | |
187 | if (!that.at_bottom()) { |
|
191 | if (!that.at_bottom()) { | |
188 | event.stop(); |
|
192 | event.stop(); | |
189 | return false; |
|
193 | return false; | |
190 | } else { |
|
194 | } else { | |
191 | return true; |
|
195 | return true; | |
192 | }; |
|
196 | }; | |
193 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { |
|
197 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { | |
194 | if (editor.somethingSelected()){ |
|
198 | if (editor.somethingSelected()){ | |
195 | var anchor = editor.getCursor("anchor"); |
|
199 | var anchor = editor.getCursor("anchor"); | |
196 | var head = editor.getCursor("head"); |
|
200 | var head = editor.getCursor("head"); | |
197 | if( anchor.line != head.line){ |
|
201 | if( anchor.line != head.line){ | |
198 | return false; |
|
202 | return false; | |
199 | } |
|
203 | } | |
200 | } |
|
204 | } | |
201 | IPython.tooltip.request(that); |
|
205 | IPython.tooltip.request(that); | |
202 | event.stop(); |
|
206 | event.stop(); | |
203 | return true; |
|
207 | return true; | |
204 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { |
|
208 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { | |
205 | // Tab completion. |
|
209 | // Tab completion. | |
206 | //Do not trim here because of tooltip |
|
210 | //Do not trim here because of tooltip | |
207 | if (editor.somethingSelected()){return false} |
|
211 | if (editor.somethingSelected()){return false} | |
208 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
212 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | |
209 | if (pre_cursor.trim() === "") { |
|
213 | if (pre_cursor.trim() === "") { | |
210 | // Don't autocomplete if the part of the line before the cursor |
|
214 | // Don't autocomplete if the part of the line before the cursor | |
211 | // is empty. In this case, let CodeMirror handle indentation. |
|
215 | // is empty. In this case, let CodeMirror handle indentation. | |
212 | return false; |
|
216 | return false; | |
213 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { |
|
217 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { | |
214 | IPython.tooltip.request(that); |
|
218 | IPython.tooltip.request(that); | |
215 | // Prevent the event from bubbling up. |
|
219 | // Prevent the event from bubbling up. | |
216 | event.stop(); |
|
220 | event.stop(); | |
217 | // Prevent CodeMirror from handling the tab. |
|
221 | // Prevent CodeMirror from handling the tab. | |
218 | return true; |
|
222 | return true; | |
219 | } else { |
|
223 | } else { | |
220 | event.stop(); |
|
224 | event.stop(); | |
221 | this.completer.startCompletion(); |
|
225 | this.completer.startCompletion(); | |
222 | return true; |
|
226 | return true; | |
223 | }; |
|
227 | }; | |
224 | } else { |
|
228 | } else { | |
225 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
229 | // keypress/keyup also trigger on TAB press, and we don't want to | |
226 | // use those to disable tab completion. |
|
230 | // use those to disable tab completion. | |
227 | return false; |
|
231 | return false; | |
228 | }; |
|
232 | }; | |
229 | return false; |
|
233 | return false; | |
230 | }; |
|
234 | }; | |
231 |
|
235 | |||
232 |
|
236 | |||
233 | // Kernel related calls. |
|
237 | // Kernel related calls. | |
234 |
|
238 | |||
235 | CodeCell.prototype.set_kernel = function (kernel) { |
|
239 | CodeCell.prototype.set_kernel = function (kernel) { | |
236 | this.kernel = kernel; |
|
240 | this.kernel = kernel; | |
237 | } |
|
241 | } | |
238 |
|
242 | |||
239 | /** |
|
243 | /** | |
240 | * Execute current code cell to the kernel |
|
244 | * Execute current code cell to the kernel | |
241 | * @method execute |
|
245 | * @method execute | |
242 | */ |
|
246 | */ | |
243 | CodeCell.prototype.execute = function () { |
|
247 | CodeCell.prototype.execute = function () { | |
244 | this.output_area.clear_output(true, true, true); |
|
248 | this.output_area.clear_output(true, true, true); | |
245 | this.set_input_prompt('*'); |
|
249 | this.set_input_prompt('*'); | |
246 | this.element.addClass("running"); |
|
250 | this.element.addClass("running"); | |
247 | var callbacks = { |
|
251 | var callbacks = { | |
248 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
252 | 'execute_reply': $.proxy(this._handle_execute_reply, this), | |
249 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
253 | 'output': $.proxy(this.output_area.handle_output, this.output_area), | |
250 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
254 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), | |
251 | 'set_next_input': $.proxy(this._handle_set_next_input, this), |
|
255 | 'set_next_input': $.proxy(this._handle_set_next_input, this), | |
252 | 'input_request': $.proxy(this._handle_input_request, this) |
|
256 | 'input_request': $.proxy(this._handle_input_request, this) | |
253 | }; |
|
257 | }; | |
254 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); |
|
258 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); | |
255 | }; |
|
259 | }; | |
256 |
|
260 | |||
257 | /** |
|
261 | /** | |
258 | * @method _handle_execute_reply |
|
262 | * @method _handle_execute_reply | |
259 | * @private |
|
263 | * @private | |
260 | */ |
|
264 | */ | |
261 | CodeCell.prototype._handle_execute_reply = function (content) { |
|
265 | CodeCell.prototype._handle_execute_reply = function (content) { | |
262 | this.set_input_prompt(content.execution_count); |
|
266 | this.set_input_prompt(content.execution_count); | |
263 | this.element.removeClass("running"); |
|
267 | this.element.removeClass("running"); | |
264 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); |
|
268 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); | |
265 | } |
|
269 | } | |
266 |
|
270 | |||
267 | /** |
|
271 | /** | |
268 | * @method _handle_set_next_input |
|
272 | * @method _handle_set_next_input | |
269 | * @private |
|
273 | * @private | |
270 | */ |
|
274 | */ | |
271 | CodeCell.prototype._handle_set_next_input = function (text) { |
|
275 | CodeCell.prototype._handle_set_next_input = function (text) { | |
272 | var data = {'cell': this, 'text': text} |
|
276 | var data = {'cell': this, 'text': text} | |
273 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
277 | $([IPython.events]).trigger('set_next_input.Notebook', data); | |
274 | } |
|
278 | } | |
275 |
|
279 | |||
276 | /** |
|
280 | /** | |
277 | * @method _handle_input_request |
|
281 | * @method _handle_input_request | |
278 | * @private |
|
282 | * @private | |
279 | */ |
|
283 | */ | |
280 | CodeCell.prototype._handle_input_request = function (content) { |
|
284 | CodeCell.prototype._handle_input_request = function (content) { | |
281 | this.output_area.append_raw_input(content); |
|
285 | this.output_area.append_raw_input(content); | |
282 | } |
|
286 | } | |
283 |
|
287 | |||
284 |
|
288 | |||
285 | // Basic cell manipulation. |
|
289 | // Basic cell manipulation. | |
286 |
|
290 | |||
287 | CodeCell.prototype.select = function () { |
|
291 | CodeCell.prototype.select = function () { | |
288 | IPython.Cell.prototype.select.apply(this); |
|
292 | IPython.Cell.prototype.select.apply(this); | |
289 | this.code_mirror.refresh(); |
|
293 | this.code_mirror.refresh(); | |
290 | this.code_mirror.focus(); |
|
294 | this.code_mirror.focus(); | |
291 | this.auto_highlight(); |
|
295 | this.auto_highlight(); | |
292 | // We used to need an additional refresh() after the focus, but |
|
296 | // We used to need an additional refresh() after the focus, but | |
293 | // it appears that this has been fixed in CM. This bug would show |
|
297 | // it appears that this has been fixed in CM. This bug would show | |
294 | // up on FF when a newly loaded markdown cell was edited. |
|
298 | // up on FF when a newly loaded markdown cell was edited. | |
295 | }; |
|
299 | }; | |
296 |
|
300 | |||
297 |
|
301 | |||
298 | CodeCell.prototype.select_all = function () { |
|
302 | CodeCell.prototype.select_all = function () { | |
299 | var start = {line: 0, ch: 0}; |
|
303 | var start = {line: 0, ch: 0}; | |
300 | var nlines = this.code_mirror.lineCount(); |
|
304 | var nlines = this.code_mirror.lineCount(); | |
301 | var last_line = this.code_mirror.getLine(nlines-1); |
|
305 | var last_line = this.code_mirror.getLine(nlines-1); | |
302 | var end = {line: nlines-1, ch: last_line.length}; |
|
306 | var end = {line: nlines-1, ch: last_line.length}; | |
303 | this.code_mirror.setSelection(start, end); |
|
307 | this.code_mirror.setSelection(start, end); | |
304 | }; |
|
308 | }; | |
305 |
|
309 | |||
306 |
|
310 | |||
307 | CodeCell.prototype.collapse = function () { |
|
311 | CodeCell.prototype.collapse = function () { | |
308 | this.collapsed = true; |
|
312 | this.collapsed = true; | |
309 | this.output_area.collapse(); |
|
313 | this.output_area.collapse(); | |
310 | }; |
|
314 | }; | |
311 |
|
315 | |||
312 |
|
316 | |||
313 | CodeCell.prototype.expand = function () { |
|
317 | CodeCell.prototype.expand = function () { | |
314 | this.collapsed = false; |
|
318 | this.collapsed = false; | |
315 | this.output_area.expand(); |
|
319 | this.output_area.expand(); | |
316 | }; |
|
320 | }; | |
317 |
|
321 | |||
318 |
|
322 | |||
319 | CodeCell.prototype.toggle_output = function () { |
|
323 | CodeCell.prototype.toggle_output = function () { | |
320 | this.collapsed = Boolean(1 - this.collapsed); |
|
324 | this.collapsed = Boolean(1 - this.collapsed); | |
321 | this.output_area.toggle_output(); |
|
325 | this.output_area.toggle_output(); | |
322 | }; |
|
326 | }; | |
323 |
|
327 | |||
324 |
|
328 | |||
325 | CodeCell.prototype.toggle_output_scroll = function () { |
|
329 | CodeCell.prototype.toggle_output_scroll = function () { | |
326 | this.output_area.toggle_scroll(); |
|
330 | this.output_area.toggle_scroll(); | |
327 | }; |
|
331 | }; | |
328 |
|
332 | |||
329 |
|
333 | |||
330 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
334 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { | |
331 | var ns = prompt_value || " "; |
|
335 | var ns = prompt_value || " "; | |
332 | return 'In [' + ns + ']:' |
|
336 | return 'In [' + ns + ']:' | |
333 | }; |
|
337 | }; | |
334 |
|
338 | |||
335 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
339 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { | |
336 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
340 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; | |
337 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; |
|
341 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; | |
338 | return html.join('</br>') |
|
342 | return html.join('</br>') | |
339 | }; |
|
343 | }; | |
340 |
|
344 | |||
341 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
345 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; | |
342 |
|
346 | |||
343 |
|
347 | |||
344 | CodeCell.prototype.set_input_prompt = function (number) { |
|
348 | CodeCell.prototype.set_input_prompt = function (number) { | |
345 | var nline = 1 |
|
349 | var nline = 1 | |
346 | if( this.code_mirror != undefined) { |
|
350 | if( this.code_mirror != undefined) { | |
347 | nline = this.code_mirror.lineCount(); |
|
351 | nline = this.code_mirror.lineCount(); | |
348 | } |
|
352 | } | |
349 | this.input_prompt_number = number; |
|
353 | this.input_prompt_number = number; | |
350 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
354 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); | |
351 | this.element.find('div.input_prompt').html(prompt_html); |
|
355 | this.element.find('div.input_prompt').html(prompt_html); | |
352 | }; |
|
356 | }; | |
353 |
|
357 | |||
354 |
|
358 | |||
355 | CodeCell.prototype.clear_input = function () { |
|
359 | CodeCell.prototype.clear_input = function () { | |
356 | this.code_mirror.setValue(''); |
|
360 | this.code_mirror.setValue(''); | |
357 | }; |
|
361 | }; | |
358 |
|
362 | |||
359 |
|
363 | |||
360 | CodeCell.prototype.get_text = function () { |
|
364 | CodeCell.prototype.get_text = function () { | |
361 | return this.code_mirror.getValue(); |
|
365 | return this.code_mirror.getValue(); | |
362 | }; |
|
366 | }; | |
363 |
|
367 | |||
364 |
|
368 | |||
365 | CodeCell.prototype.set_text = function (code) { |
|
369 | CodeCell.prototype.set_text = function (code) { | |
366 | return this.code_mirror.setValue(code); |
|
370 | return this.code_mirror.setValue(code); | |
367 | }; |
|
371 | }; | |
368 |
|
372 | |||
369 |
|
373 | |||
370 | CodeCell.prototype.at_top = function () { |
|
374 | CodeCell.prototype.at_top = function () { | |
371 | var cursor = this.code_mirror.getCursor(); |
|
375 | var cursor = this.code_mirror.getCursor(); | |
372 | if (cursor.line === 0 && cursor.ch === 0) { |
|
376 | if (cursor.line === 0 && cursor.ch === 0) { | |
373 | return true; |
|
377 | return true; | |
374 | } else { |
|
378 | } else { | |
375 | return false; |
|
379 | return false; | |
376 | } |
|
380 | } | |
377 | }; |
|
381 | }; | |
378 |
|
382 | |||
379 |
|
383 | |||
380 | CodeCell.prototype.at_bottom = function () { |
|
384 | CodeCell.prototype.at_bottom = function () { | |
381 | var cursor = this.code_mirror.getCursor(); |
|
385 | var cursor = this.code_mirror.getCursor(); | |
382 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
386 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | |
383 | return true; |
|
387 | return true; | |
384 | } else { |
|
388 | } else { | |
385 | return false; |
|
389 | return false; | |
386 | } |
|
390 | } | |
387 | }; |
|
391 | }; | |
388 |
|
392 | |||
389 |
|
393 | |||
390 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
394 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { | |
391 | this.output_area.clear_output(stdout, stderr, other); |
|
395 | this.output_area.clear_output(stdout, stderr, other); | |
392 | }; |
|
396 | }; | |
393 |
|
397 | |||
394 |
|
398 | |||
395 | // JSON serialization |
|
399 | // JSON serialization | |
396 |
|
400 | |||
397 | CodeCell.prototype.fromJSON = function (data) { |
|
401 | CodeCell.prototype.fromJSON = function (data) { | |
398 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
402 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
399 | if (data.cell_type === 'code') { |
|
403 | if (data.cell_type === 'code') { | |
400 | if (data.input !== undefined) { |
|
404 | if (data.input !== undefined) { | |
401 | this.set_text(data.input); |
|
405 | this.set_text(data.input); | |
402 | // make this value the starting point, so that we can only undo |
|
406 | // make this value the starting point, so that we can only undo | |
403 | // to this state, instead of a blank cell |
|
407 | // to this state, instead of a blank cell | |
404 | this.code_mirror.clearHistory(); |
|
408 | this.code_mirror.clearHistory(); | |
405 | this.auto_highlight(); |
|
409 | this.auto_highlight(); | |
406 | } |
|
410 | } | |
407 | if (data.prompt_number !== undefined) { |
|
411 | if (data.prompt_number !== undefined) { | |
408 | this.set_input_prompt(data.prompt_number); |
|
412 | this.set_input_prompt(data.prompt_number); | |
409 | } else { |
|
413 | } else { | |
410 | this.set_input_prompt(); |
|
414 | this.set_input_prompt(); | |
411 | }; |
|
415 | }; | |
412 | this.output_area.fromJSON(data.outputs); |
|
416 | this.output_area.fromJSON(data.outputs); | |
413 | if (data.collapsed !== undefined) { |
|
417 | if (data.collapsed !== undefined) { | |
414 | if (data.collapsed) { |
|
418 | if (data.collapsed) { | |
415 | this.collapse(); |
|
419 | this.collapse(); | |
416 | } else { |
|
420 | } else { | |
417 | this.expand(); |
|
421 | this.expand(); | |
418 | }; |
|
422 | }; | |
419 | }; |
|
423 | }; | |
420 | }; |
|
424 | }; | |
421 | }; |
|
425 | }; | |
422 |
|
426 | |||
423 |
|
427 | |||
424 | CodeCell.prototype.toJSON = function () { |
|
428 | CodeCell.prototype.toJSON = function () { | |
425 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
429 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
426 | data.input = this.get_text(); |
|
430 | data.input = this.get_text(); | |
427 | data.cell_type = 'code'; |
|
431 | data.cell_type = 'code'; | |
428 | if (this.input_prompt_number) { |
|
432 | if (this.input_prompt_number) { | |
429 | data.prompt_number = this.input_prompt_number; |
|
433 | data.prompt_number = this.input_prompt_number; | |
430 | }; |
|
434 | }; | |
431 | var outputs = this.output_area.toJSON(); |
|
435 | var outputs = this.output_area.toJSON(); | |
432 | data.outputs = outputs; |
|
436 | data.outputs = outputs; | |
433 | data.language = 'python'; |
|
437 | data.language = 'python'; | |
434 | data.collapsed = this.collapsed; |
|
438 | data.collapsed = this.collapsed; | |
435 | return data; |
|
439 | return data; | |
436 | }; |
|
440 | }; | |
437 |
|
441 | |||
438 |
|
442 | |||
439 | IPython.CodeCell = CodeCell; |
|
443 | IPython.CodeCell = CodeCell; | |
440 |
|
444 | |||
441 | return IPython; |
|
445 | return IPython; | |
442 | }(IPython)); |
|
446 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now