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