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