##// END OF EJS Templates
clear In[ ] prompt numbers again
Paul Ivanov -
Show More
@@ -1,345 +1,343 b''
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 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16 var key = IPython.utils.keycodes;
16 var key = IPython.utils.keycodes;
17 CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js";
17 CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js";
18
18
19 var CodeCell = function (kernel) {
19 var CodeCell = function (kernel) {
20 // The kernel doesn't have to be set at creation time, in that case
20 // The kernel doesn't have to be set at creation time, in that case
21 // it will be null and set_kernel has to be called later.
21 // it will be null and set_kernel has to be called later.
22 this.kernel = kernel || null;
22 this.kernel = kernel || null;
23 this.code_mirror = null;
23 this.code_mirror = null;
24 this.input_prompt_number = null;
24 this.input_prompt_number = null;
25 this.tooltip_on_tab = true;
25 this.tooltip_on_tab = true;
26 this.collapsed = false;
26 this.collapsed = false;
27 this.default_mode = 'python';
27 this.default_mode = 'python';
28 IPython.Cell.apply(this, arguments);
28 IPython.Cell.apply(this, arguments);
29
29
30 var that = this;
30 var that = this;
31 this.element.focusout(
31 this.element.focusout(
32 function() { that.auto_highlight(); }
32 function() { that.auto_highlight(); }
33 );
33 );
34 };
34 };
35
35
36
36
37 CodeCell.prototype = new IPython.Cell();
37 CodeCell.prototype = new IPython.Cell();
38
38
39
39
40 CodeCell.prototype.auto_highlight = function () {
40 CodeCell.prototype.auto_highlight = function () {
41 this._auto_highlight(IPython.config.cell_magic_highlight)
41 this._auto_highlight(IPython.config.cell_magic_highlight)
42 };
42 };
43
43
44 CodeCell.prototype.create_element = function () {
44 CodeCell.prototype.create_element = function () {
45 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
45 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
46 cell.attr('tabindex','2');
46 cell.attr('tabindex','2');
47 var input = $('<div></div>').addClass('input hbox');
47 var input = $('<div></div>').addClass('input hbox');
48 input.append($('<div/>').addClass('prompt input_prompt'));
48 input.append($('<div/>').addClass('prompt input_prompt'));
49 var input_area = $('<div/>').addClass('input_area box-flex1');
49 var input_area = $('<div/>').addClass('input_area box-flex1');
50 this.code_mirror = CodeMirror(input_area.get(0), {
50 this.code_mirror = CodeMirror(input_area.get(0), {
51 indentUnit : 4,
51 indentUnit : 4,
52 mode: 'python',
52 mode: 'python',
53 theme: 'ipython',
53 theme: 'ipython',
54 readOnly: this.read_only,
54 readOnly: this.read_only,
55 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
55 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
56 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
56 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
57 });
57 });
58 input.append(input_area);
58 input.append(input_area);
59 var output = $('<div></div>');
59 var output = $('<div></div>');
60 cell.append(input).append(output);
60 cell.append(input).append(output);
61 this.element = cell;
61 this.element = cell;
62 this.output_area = new IPython.OutputArea(output, true);
62 this.output_area = new IPython.OutputArea(output, true);
63
63
64 // construct a completer only if class exist
64 // construct a completer only if class exist
65 // otherwise no print view
65 // otherwise no print view
66 if (IPython.Completer !== undefined)
66 if (IPython.Completer !== undefined)
67 {
67 {
68 this.completer = new IPython.Completer(this);
68 this.completer = new IPython.Completer(this);
69 }
69 }
70 };
70 };
71
71
72 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
72 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
73 // This method gets called in CodeMirror's onKeyDown/onKeyPress
73 // This method gets called in CodeMirror's onKeyDown/onKeyPress
74 // handlers and is used to provide custom key handling. Its return
74 // handlers and is used to provide custom key handling. Its return
75 // value is used to determine if CodeMirror should ignore the event:
75 // value is used to determine if CodeMirror should ignore the event:
76 // true = ignore, false = don't ignore.
76 // true = ignore, false = don't ignore.
77
77
78 if (this.read_only){
78 if (this.read_only){
79 return false;
79 return false;
80 }
80 }
81
81
82 var that = this;
82 var that = this;
83 // whatever key is pressed, first, cancel the tooltip request before
83 // whatever key is pressed, first, cancel the tooltip request before
84 // they are sent, and remove tooltip if any, except for tab again
84 // they are sent, and remove tooltip if any, except for tab again
85 if (event.type === 'keydown' && event.which != key.TAB ) {
85 if (event.type === 'keydown' && event.which != key.TAB ) {
86 IPython.tooltip.remove_and_cancel_tooltip();
86 IPython.tooltip.remove_and_cancel_tooltip();
87 };
87 };
88
88
89 var cur = editor.getCursor();
89 var cur = editor.getCursor();
90 if (event.keyCode === key.ENTER){
90 if (event.keyCode === key.ENTER){
91 this.auto_highlight();
91 this.auto_highlight();
92 }
92 }
93
93
94 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
94 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
95 // Always ignore shift-enter in CodeMirror as we handle it.
95 // Always ignore shift-enter in CodeMirror as we handle it.
96 return true;
96 return true;
97 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
97 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
98 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
98 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
99 // browser and keyboard layout !
99 // browser and keyboard layout !
100 // Pressing '(' , request tooltip, don't forget to reappend it
100 // Pressing '(' , request tooltip, don't forget to reappend it
101 IPython.tooltip.pending(that);
101 IPython.tooltip.pending(that);
102 } else if (event.which === key.UPARROW && event.type === 'keydown') {
102 } else if (event.which === key.UPARROW && event.type === 'keydown') {
103 // If we are not at the top, let CM handle the up arrow and
103 // If we are not at the top, let CM handle the up arrow and
104 // prevent the global keydown handler from handling it.
104 // prevent the global keydown handler from handling it.
105 if (!that.at_top()) {
105 if (!that.at_top()) {
106 event.stop();
106 event.stop();
107 return false;
107 return false;
108 } else {
108 } else {
109 return true;
109 return true;
110 };
110 };
111 } else if (event.which === key.ESC) {
111 } else if (event.which === key.ESC) {
112 IPython.tooltip.remove_and_cancel_tooltip(true);
112 IPython.tooltip.remove_and_cancel_tooltip(true);
113 return true;
113 return true;
114 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
114 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
115 // If we are not at the bottom, let CM handle the down arrow and
115 // If we are not at the bottom, let CM handle the down arrow and
116 // prevent the global keydown handler from handling it.
116 // prevent the global keydown handler from handling it.
117 if (!that.at_bottom()) {
117 if (!that.at_bottom()) {
118 event.stop();
118 event.stop();
119 return false;
119 return false;
120 } else {
120 } else {
121 return true;
121 return true;
122 };
122 };
123 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
123 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
124 // Tab completion.
124 // Tab completion.
125 //Do not trim here because of tooltip
125 //Do not trim here because of tooltip
126 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
126 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
127 if (pre_cursor.trim() === "") {
127 if (pre_cursor.trim() === "") {
128 // Don't autocomplete if the part of the line before the cursor
128 // Don't autocomplete if the part of the line before the cursor
129 // is empty. In this case, let CodeMirror handle indentation.
129 // is empty. In this case, let CodeMirror handle indentation.
130 return false;
130 return false;
131 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
131 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
132 IPython.tooltip.request(that);
132 IPython.tooltip.request(that);
133 // Prevent the event from bubbling up.
133 // Prevent the event from bubbling up.
134 event.stop();
134 event.stop();
135 // Prevent CodeMirror from handling the tab.
135 // Prevent CodeMirror from handling the tab.
136 return true;
136 return true;
137 } else {
137 } else {
138 event.stop();
138 event.stop();
139 this.completer.startCompletion();
139 this.completer.startCompletion();
140 return true;
140 return true;
141 };
141 };
142 } else {
142 } else {
143 // keypress/keyup also trigger on TAB press, and we don't want to
143 // keypress/keyup also trigger on TAB press, and we don't want to
144 // use those to disable tab completion.
144 // use those to disable tab completion.
145 return false;
145 return false;
146 };
146 };
147 return false;
147 return false;
148 };
148 };
149
149
150
150
151 // Kernel related calls.
151 // Kernel related calls.
152
152
153 CodeCell.prototype.set_kernel = function (kernel) {
153 CodeCell.prototype.set_kernel = function (kernel) {
154 this.kernel = kernel;
154 this.kernel = kernel;
155 }
155 }
156
156
157
157
158 CodeCell.prototype.execute = function () {
158 CodeCell.prototype.execute = function () {
159 this.output_area.clear_output(true, true, true);
159 this.output_area.clear_output(true, true, true);
160 this.set_input_prompt('*');
160 this.set_input_prompt('*');
161 this.element.addClass("running");
161 this.element.addClass("running");
162 var callbacks = {
162 var callbacks = {
163 'execute_reply': $.proxy(this._handle_execute_reply, this),
163 'execute_reply': $.proxy(this._handle_execute_reply, this),
164 'output': $.proxy(this.output_area.handle_output, this.output_area),
164 'output': $.proxy(this.output_area.handle_output, this.output_area),
165 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
165 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
166 'set_next_input': $.proxy(this._handle_set_next_input, this)
166 'set_next_input': $.proxy(this._handle_set_next_input, this)
167 };
167 };
168 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
168 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
169 };
169 };
170
170
171
171
172 CodeCell.prototype._handle_execute_reply = function (content) {
172 CodeCell.prototype._handle_execute_reply = function (content) {
173 this.set_input_prompt(content.execution_count);
173 this.set_input_prompt(content.execution_count);
174 this.element.removeClass("running");
174 this.element.removeClass("running");
175 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
175 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
176 }
176 }
177
177
178 CodeCell.prototype._handle_set_next_input = function (text) {
178 CodeCell.prototype._handle_set_next_input = function (text) {
179 var data = {'cell': this, 'text': text}
179 var data = {'cell': this, 'text': text}
180 $([IPython.events]).trigger('set_next_input.Notebook', data);
180 $([IPython.events]).trigger('set_next_input.Notebook', data);
181 }
181 }
182
182
183 // Basic cell manipulation.
183 // Basic cell manipulation.
184
184
185 CodeCell.prototype.select = function () {
185 CodeCell.prototype.select = function () {
186 IPython.Cell.prototype.select.apply(this);
186 IPython.Cell.prototype.select.apply(this);
187 this.code_mirror.refresh();
187 this.code_mirror.refresh();
188 this.code_mirror.focus();
188 this.code_mirror.focus();
189 this.auto_highlight();
189 this.auto_highlight();
190 // We used to need an additional refresh() after the focus, but
190 // We used to need an additional refresh() after the focus, but
191 // it appears that this has been fixed in CM. This bug would show
191 // it appears that this has been fixed in CM. This bug would show
192 // up on FF when a newly loaded markdown cell was edited.
192 // up on FF when a newly loaded markdown cell was edited.
193 };
193 };
194
194
195
195
196 CodeCell.prototype.select_all = function () {
196 CodeCell.prototype.select_all = function () {
197 var start = {line: 0, ch: 0};
197 var start = {line: 0, ch: 0};
198 var nlines = this.code_mirror.lineCount();
198 var nlines = this.code_mirror.lineCount();
199 var last_line = this.code_mirror.getLine(nlines-1);
199 var last_line = this.code_mirror.getLine(nlines-1);
200 var end = {line: nlines-1, ch: last_line.length};
200 var end = {line: nlines-1, ch: last_line.length};
201 this.code_mirror.setSelection(start, end);
201 this.code_mirror.setSelection(start, end);
202 };
202 };
203
203
204
204
205 CodeCell.prototype.collapse = function () {
205 CodeCell.prototype.collapse = function () {
206 this.collapsed = true;
206 this.collapsed = true;
207 this.output_area.collapse();
207 this.output_area.collapse();
208 };
208 };
209
209
210
210
211 CodeCell.prototype.expand = function () {
211 CodeCell.prototype.expand = function () {
212 this.collapsed = false;
212 this.collapsed = false;
213 this.output_area.expand();
213 this.output_area.expand();
214 };
214 };
215
215
216
216
217 CodeCell.prototype.toggle_output = function () {
217 CodeCell.prototype.toggle_output = function () {
218 this.collapsed = Boolean(1 - this.collapsed);
218 this.collapsed = Boolean(1 - this.collapsed);
219 this.output_area.toggle_output();
219 this.output_area.toggle_output();
220 };
220 };
221
221
222
222
223 CodeCell.prototype.toggle_output_scroll = function () {
223 CodeCell.prototype.toggle_output_scroll = function () {
224 this.output_area.toggle_scroll();
224 this.output_area.toggle_scroll();
225 };
225 };
226
226
227
227
228
228
229
229
230
230
231 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
231 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
232 var ns = prompt_value || "&nbsp;";
232 var ns = prompt_value || "&nbsp;";
233 return 'In&nbsp;[' + ns + ']:'
233 return 'In&nbsp;[' + ns + ']:'
234 };
234 };
235
235
236 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
236 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
237 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
237 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
238 for(var i=1; i < lines_number; i++){html.push(['...:'])};
238 for(var i=1; i < lines_number; i++){html.push(['...:'])};
239 return html.join('</br>')
239 return html.join('</br>')
240 };
240 };
241
241
242 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
242 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
243
243
244
244
245 CodeCell.prototype.set_input_prompt = function (number) {
245 CodeCell.prototype.set_input_prompt = function (number) {
246 var nline = 1
246 var nline = 1
247 if( this.code_mirror != undefined) {
247 if( this.code_mirror != undefined) {
248 nline = this.code_mirror.lineCount();
248 nline = this.code_mirror.lineCount();
249 }
249 }
250 if (number){
250 this.input_prompt_number = number;
251 this.input_prompt_number = number;
252 }
253 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
251 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
254 this.element.find('div.input_prompt').html(prompt_html);
252 this.element.find('div.input_prompt').html(prompt_html);
255 };
253 };
256
254
257
255
258 CodeCell.prototype.clear_input = function () {
256 CodeCell.prototype.clear_input = function () {
259 this.code_mirror.setValue('');
257 this.code_mirror.setValue('');
260 };
258 };
261
259
262
260
263 CodeCell.prototype.get_text = function () {
261 CodeCell.prototype.get_text = function () {
264 return this.code_mirror.getValue();
262 return this.code_mirror.getValue();
265 };
263 };
266
264
267
265
268 CodeCell.prototype.set_text = function (code) {
266 CodeCell.prototype.set_text = function (code) {
269 return this.code_mirror.setValue(code);
267 return this.code_mirror.setValue(code);
270 };
268 };
271
269
272
270
273 CodeCell.prototype.at_top = function () {
271 CodeCell.prototype.at_top = function () {
274 var cursor = this.code_mirror.getCursor();
272 var cursor = this.code_mirror.getCursor();
275 if (cursor.line === 0 && cursor.ch === 0) {
273 if (cursor.line === 0 && cursor.ch === 0) {
276 return true;
274 return true;
277 } else {
275 } else {
278 return false;
276 return false;
279 }
277 }
280 };
278 };
281
279
282
280
283 CodeCell.prototype.at_bottom = function () {
281 CodeCell.prototype.at_bottom = function () {
284 var cursor = this.code_mirror.getCursor();
282 var cursor = this.code_mirror.getCursor();
285 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
283 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
286 return true;
284 return true;
287 } else {
285 } else {
288 return false;
286 return false;
289 }
287 }
290 };
288 };
291
289
292
290
293 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
291 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
294 this.output_area.clear_output(stdout, stderr, other);
292 this.output_area.clear_output(stdout, stderr, other);
295 };
293 };
296
294
297
295
298 // JSON serialization
296 // JSON serialization
299
297
300 CodeCell.prototype.fromJSON = function (data) {
298 CodeCell.prototype.fromJSON = function (data) {
301 IPython.Cell.prototype.fromJSON.apply(this, arguments);
299 IPython.Cell.prototype.fromJSON.apply(this, arguments);
302 if (data.cell_type === 'code') {
300 if (data.cell_type === 'code') {
303 if (data.input !== undefined) {
301 if (data.input !== undefined) {
304 this.set_text(data.input);
302 this.set_text(data.input);
305 // make this value the starting point, so that we can only undo
303 // make this value the starting point, so that we can only undo
306 // to this state, instead of a blank cell
304 // to this state, instead of a blank cell
307 this.code_mirror.clearHistory();
305 this.code_mirror.clearHistory();
308 this.auto_highlight();
306 this.auto_highlight();
309 }
307 }
310 if (data.prompt_number !== undefined) {
308 if (data.prompt_number !== undefined) {
311 this.set_input_prompt(data.prompt_number);
309 this.set_input_prompt(data.prompt_number);
312 } else {
310 } else {
313 this.set_input_prompt();
311 this.set_input_prompt();
314 };
312 };
315 this.output_area.fromJSON(data.outputs);
313 this.output_area.fromJSON(data.outputs);
316 if (data.collapsed !== undefined) {
314 if (data.collapsed !== undefined) {
317 if (data.collapsed) {
315 if (data.collapsed) {
318 this.collapse();
316 this.collapse();
319 } else {
317 } else {
320 this.expand();
318 this.expand();
321 };
319 };
322 };
320 };
323 };
321 };
324 };
322 };
325
323
326
324
327 CodeCell.prototype.toJSON = function () {
325 CodeCell.prototype.toJSON = function () {
328 var data = IPython.Cell.prototype.toJSON.apply(this);
326 var data = IPython.Cell.prototype.toJSON.apply(this);
329 data.input = this.get_text();
327 data.input = this.get_text();
330 data.cell_type = 'code';
328 data.cell_type = 'code';
331 if (this.input_prompt_number) {
329 if (this.input_prompt_number) {
332 data.prompt_number = this.input_prompt_number;
330 data.prompt_number = this.input_prompt_number;
333 };
331 };
334 var outputs = this.output_area.toJSON();
332 var outputs = this.output_area.toJSON();
335 data.outputs = outputs;
333 data.outputs = outputs;
336 data.language = 'python';
334 data.language = 'python';
337 data.collapsed = this.collapsed;
335 data.collapsed = this.collapsed;
338 return data;
336 return data;
339 };
337 };
340
338
341
339
342 IPython.CodeCell = CodeCell;
340 IPython.CodeCell = CodeCell;
343
341
344 return IPython;
342 return IPython;
345 }(IPython));
343 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now