##// END OF EJS Templates
Don't catch tab press when something selected...
Matthias BUSSONNIER -
Show More
@@ -1,345 +1,346 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 if (editor.somethingSelected()){return false}
126 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
127 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
127 if (pre_cursor.trim() === "") {
128 if (pre_cursor.trim() === "") {
128 // Don't autocomplete if the part of the line before the cursor
129 // Don't autocomplete if the part of the line before the cursor
129 // is empty. In this case, let CodeMirror handle indentation.
130 // is empty. In this case, let CodeMirror handle indentation.
130 return false;
131 return false;
131 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
132 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
132 IPython.tooltip.request(that);
133 IPython.tooltip.request(that);
133 // Prevent the event from bubbling up.
134 // Prevent the event from bubbling up.
134 event.stop();
135 event.stop();
135 // Prevent CodeMirror from handling the tab.
136 // Prevent CodeMirror from handling the tab.
136 return true;
137 return true;
137 } else {
138 } else {
138 event.stop();
139 event.stop();
139 this.completer.startCompletion();
140 this.completer.startCompletion();
140 return true;
141 return true;
141 };
142 };
142 } else {
143 } else {
143 // keypress/keyup also trigger on TAB press, and we don't want to
144 // keypress/keyup also trigger on TAB press, and we don't want to
144 // use those to disable tab completion.
145 // use those to disable tab completion.
145 return false;
146 return false;
146 };
147 };
147 return false;
148 return false;
148 };
149 };
149
150
150
151
151 // Kernel related calls.
152 // Kernel related calls.
152
153
153 CodeCell.prototype.set_kernel = function (kernel) {
154 CodeCell.prototype.set_kernel = function (kernel) {
154 this.kernel = kernel;
155 this.kernel = kernel;
155 }
156 }
156
157
157
158
158 CodeCell.prototype.execute = function () {
159 CodeCell.prototype.execute = function () {
159 this.output_area.clear_output(true, true, true);
160 this.output_area.clear_output(true, true, true);
160 this.set_input_prompt('*');
161 this.set_input_prompt('*');
161 this.element.addClass("running");
162 this.element.addClass("running");
162 var callbacks = {
163 var callbacks = {
163 'execute_reply': $.proxy(this._handle_execute_reply, this),
164 'execute_reply': $.proxy(this._handle_execute_reply, this),
164 'output': $.proxy(this.output_area.handle_output, this.output_area),
165 'output': $.proxy(this.output_area.handle_output, this.output_area),
165 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
166 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
166 'set_next_input': $.proxy(this._handle_set_next_input, this)
167 'set_next_input': $.proxy(this._handle_set_next_input, this)
167 };
168 };
168 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
169 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
169 };
170 };
170
171
171
172
172 CodeCell.prototype._handle_execute_reply = function (content) {
173 CodeCell.prototype._handle_execute_reply = function (content) {
173 this.set_input_prompt(content.execution_count);
174 this.set_input_prompt(content.execution_count);
174 this.element.removeClass("running");
175 this.element.removeClass("running");
175 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
176 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
176 }
177 }
177
178
178 CodeCell.prototype._handle_set_next_input = function (text) {
179 CodeCell.prototype._handle_set_next_input = function (text) {
179 var data = {'cell': this, 'text': text}
180 var data = {'cell': this, 'text': text}
180 $([IPython.events]).trigger('set_next_input.Notebook', data);
181 $([IPython.events]).trigger('set_next_input.Notebook', data);
181 }
182 }
182
183
183 // Basic cell manipulation.
184 // Basic cell manipulation.
184
185
185 CodeCell.prototype.select = function () {
186 CodeCell.prototype.select = function () {
186 IPython.Cell.prototype.select.apply(this);
187 IPython.Cell.prototype.select.apply(this);
187 this.code_mirror.refresh();
188 this.code_mirror.refresh();
188 this.code_mirror.focus();
189 this.code_mirror.focus();
189 this.auto_highlight();
190 this.auto_highlight();
190 // We used to need an additional refresh() after the focus, but
191 // 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
192 // 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.
193 // up on FF when a newly loaded markdown cell was edited.
193 };
194 };
194
195
195
196
196 CodeCell.prototype.select_all = function () {
197 CodeCell.prototype.select_all = function () {
197 var start = {line: 0, ch: 0};
198 var start = {line: 0, ch: 0};
198 var nlines = this.code_mirror.lineCount();
199 var nlines = this.code_mirror.lineCount();
199 var last_line = this.code_mirror.getLine(nlines-1);
200 var last_line = this.code_mirror.getLine(nlines-1);
200 var end = {line: nlines-1, ch: last_line.length};
201 var end = {line: nlines-1, ch: last_line.length};
201 this.code_mirror.setSelection(start, end);
202 this.code_mirror.setSelection(start, end);
202 };
203 };
203
204
204
205
205 CodeCell.prototype.collapse = function () {
206 CodeCell.prototype.collapse = function () {
206 this.collapsed = true;
207 this.collapsed = true;
207 this.output_area.collapse();
208 this.output_area.collapse();
208 };
209 };
209
210
210
211
211 CodeCell.prototype.expand = function () {
212 CodeCell.prototype.expand = function () {
212 this.collapsed = false;
213 this.collapsed = false;
213 this.output_area.expand();
214 this.output_area.expand();
214 };
215 };
215
216
216
217
217 CodeCell.prototype.toggle_output = function () {
218 CodeCell.prototype.toggle_output = function () {
218 this.collapsed = Boolean(1 - this.collapsed);
219 this.collapsed = Boolean(1 - this.collapsed);
219 this.output_area.toggle_output();
220 this.output_area.toggle_output();
220 };
221 };
221
222
222
223
223 CodeCell.prototype.toggle_output_scroll = function () {
224 CodeCell.prototype.toggle_output_scroll = function () {
224 this.output_area.toggle_scroll();
225 this.output_area.toggle_scroll();
225 };
226 };
226
227
227
228
228
229
229
230
230
231
231 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
232 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
232 var ns = prompt_value || "&nbsp;";
233 var ns = prompt_value || "&nbsp;";
233 return 'In&nbsp;[' + ns + ']:'
234 return 'In&nbsp;[' + ns + ']:'
234 };
235 };
235
236
236 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
237 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
237 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
238 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
238 for(var i=1; i < lines_number; i++){html.push(['...:'])};
239 for(var i=1; i < lines_number; i++){html.push(['...:'])};
239 return html.join('</br>')
240 return html.join('</br>')
240 };
241 };
241
242
242 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
243 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
243
244
244
245
245 CodeCell.prototype.set_input_prompt = function (number) {
246 CodeCell.prototype.set_input_prompt = function (number) {
246 var nline = 1
247 var nline = 1
247 if( this.code_mirror != undefined) {
248 if( this.code_mirror != undefined) {
248 nline = this.code_mirror.lineCount();
249 nline = this.code_mirror.lineCount();
249 }
250 }
250 if (number){
251 if (number){
251 this.input_prompt_number = number;
252 this.input_prompt_number = number;
252 }
253 }
253 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
254 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
254 this.element.find('div.input_prompt').html(prompt_html);
255 this.element.find('div.input_prompt').html(prompt_html);
255 };
256 };
256
257
257
258
258 CodeCell.prototype.clear_input = function () {
259 CodeCell.prototype.clear_input = function () {
259 this.code_mirror.setValue('');
260 this.code_mirror.setValue('');
260 };
261 };
261
262
262
263
263 CodeCell.prototype.get_text = function () {
264 CodeCell.prototype.get_text = function () {
264 return this.code_mirror.getValue();
265 return this.code_mirror.getValue();
265 };
266 };
266
267
267
268
268 CodeCell.prototype.set_text = function (code) {
269 CodeCell.prototype.set_text = function (code) {
269 return this.code_mirror.setValue(code);
270 return this.code_mirror.setValue(code);
270 };
271 };
271
272
272
273
273 CodeCell.prototype.at_top = function () {
274 CodeCell.prototype.at_top = function () {
274 var cursor = this.code_mirror.getCursor();
275 var cursor = this.code_mirror.getCursor();
275 if (cursor.line === 0 && cursor.ch === 0) {
276 if (cursor.line === 0 && cursor.ch === 0) {
276 return true;
277 return true;
277 } else {
278 } else {
278 return false;
279 return false;
279 }
280 }
280 };
281 };
281
282
282
283
283 CodeCell.prototype.at_bottom = function () {
284 CodeCell.prototype.at_bottom = function () {
284 var cursor = this.code_mirror.getCursor();
285 var cursor = this.code_mirror.getCursor();
285 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
286 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
286 return true;
287 return true;
287 } else {
288 } else {
288 return false;
289 return false;
289 }
290 }
290 };
291 };
291
292
292
293
293 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
294 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
294 this.output_area.clear_output(stdout, stderr, other);
295 this.output_area.clear_output(stdout, stderr, other);
295 };
296 };
296
297
297
298
298 // JSON serialization
299 // JSON serialization
299
300
300 CodeCell.prototype.fromJSON = function (data) {
301 CodeCell.prototype.fromJSON = function (data) {
301 IPython.Cell.prototype.fromJSON.apply(this, arguments);
302 IPython.Cell.prototype.fromJSON.apply(this, arguments);
302 if (data.cell_type === 'code') {
303 if (data.cell_type === 'code') {
303 if (data.input !== undefined) {
304 if (data.input !== undefined) {
304 this.set_text(data.input);
305 this.set_text(data.input);
305 // make this value the starting point, so that we can only undo
306 // make this value the starting point, so that we can only undo
306 // to this state, instead of a blank cell
307 // to this state, instead of a blank cell
307 this.code_mirror.clearHistory();
308 this.code_mirror.clearHistory();
308 this.auto_highlight();
309 this.auto_highlight();
309 }
310 }
310 if (data.prompt_number !== undefined) {
311 if (data.prompt_number !== undefined) {
311 this.set_input_prompt(data.prompt_number);
312 this.set_input_prompt(data.prompt_number);
312 } else {
313 } else {
313 this.set_input_prompt();
314 this.set_input_prompt();
314 };
315 };
315 this.output_area.fromJSON(data.outputs);
316 this.output_area.fromJSON(data.outputs);
316 if (data.collapsed !== undefined) {
317 if (data.collapsed !== undefined) {
317 if (data.collapsed) {
318 if (data.collapsed) {
318 this.collapse();
319 this.collapse();
319 } else {
320 } else {
320 this.expand();
321 this.expand();
321 };
322 };
322 };
323 };
323 };
324 };
324 };
325 };
325
326
326
327
327 CodeCell.prototype.toJSON = function () {
328 CodeCell.prototype.toJSON = function () {
328 var data = IPython.Cell.prototype.toJSON.apply(this);
329 var data = IPython.Cell.prototype.toJSON.apply(this);
329 data.input = this.get_text();
330 data.input = this.get_text();
330 data.cell_type = 'code';
331 data.cell_type = 'code';
331 if (this.input_prompt_number) {
332 if (this.input_prompt_number) {
332 data.prompt_number = this.input_prompt_number;
333 data.prompt_number = this.input_prompt_number;
333 };
334 };
334 var outputs = this.output_area.toJSON();
335 var outputs = this.output_area.toJSON();
335 data.outputs = outputs;
336 data.outputs = outputs;
336 data.language = 'python';
337 data.language = 'python';
337 data.collapsed = this.collapsed;
338 data.collapsed = this.collapsed;
338 return data;
339 return data;
339 };
340 };
340
341
341
342
342 IPython.CodeCell = CodeCell;
343 IPython.CodeCell = CodeCell;
343
344
344 return IPython;
345 return IPython;
345 }(IPython));
346 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now