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