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