##// END OF EJS Templates
don't execute if kernel is not connected
Min RK -
Show More
@@ -1,526 +1,531 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3 /**
4 4 *
5 5 *
6 6 * @module codecell
7 7 * @namespace codecell
8 8 * @class CodeCell
9 9 */
10 10
11 11
12 12 define([
13 13 'base/js/namespace',
14 14 'jquery',
15 15 'base/js/utils',
16 16 'base/js/keyboard',
17 17 'notebook/js/cell',
18 18 'notebook/js/outputarea',
19 19 'notebook/js/completer',
20 20 'notebook/js/celltoolbar',
21 21 'codemirror/lib/codemirror',
22 22 'codemirror/mode/python/python',
23 23 'notebook/js/codemirror-ipython'
24 24 ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip) {
25 25 "use strict";
26 26 var Cell = cell.Cell;
27 27
28 28 /* local util for codemirror */
29 29 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
30 30
31 31 /**
32 32 *
33 33 * function to delete until previous non blanking space character
34 34 * or first multiple of 4 tabstop.
35 35 * @private
36 36 */
37 37 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
38 38 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
39 39 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
40 40 var cur = cm.getCursor(), line = cm.getLine(cur.line);
41 41 var tabsize = cm.getOption('tabSize');
42 42 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
43 43 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
44 44 var select = cm.getRange(from,cur);
45 45 if( select.match(/^\ +$/) !== null){
46 46 cm.replaceRange("",from,cur);
47 47 } else {
48 48 cm.deleteH(-1,"char");
49 49 }
50 50 };
51 51
52 52 var keycodes = keyboard.keycodes;
53 53
54 54 var CodeCell = function (kernel, options) {
55 55 // Constructor
56 56 //
57 57 // A Cell conceived to write code.
58 58 //
59 59 // Parameters:
60 60 // kernel: Kernel instance
61 61 // The kernel doesn't have to be set at creation time, in that case
62 62 // it will be null and set_kernel has to be called later.
63 63 // options: dictionary
64 64 // Dictionary of keyword arguments.
65 65 // events: $(Events) instance
66 66 // config: dictionary
67 67 // keyboard_manager: KeyboardManager instance
68 68 // notebook: Notebook instance
69 69 // tooltip: Tooltip instance
70 70 this.kernel = kernel || null;
71 71 this.notebook = options.notebook;
72 72 this.collapsed = false;
73 73 this.events = options.events;
74 74 this.tooltip = options.tooltip;
75 75 this.config = options.config;
76 76
77 77 // create all attributed in constructor function
78 78 // even if null for V8 VM optimisation
79 79 this.input_prompt_number = null;
80 80 this.celltoolbar = null;
81 81 this.output_area = null;
82 82 this.last_msg_id = null;
83 83 this.completer = null;
84 84
85 85
86 86 var config = utils.mergeopt(CodeCell, this.config);
87 87 Cell.apply(this,[{
88 88 config: config,
89 89 keyboard_manager: options.keyboard_manager,
90 90 events: this.events}]);
91 91
92 92 // Attributes we want to override in this subclass.
93 93 this.cell_type = "code";
94 94
95 95 var that = this;
96 96 this.element.focusout(
97 97 function() { that.auto_highlight(); }
98 98 );
99 99 };
100 100
101 101 CodeCell.options_default = {
102 102 cm_config : {
103 103 extraKeys: {
104 104 "Tab" : "indentMore",
105 105 "Shift-Tab" : "indentLess",
106 106 "Backspace" : "delSpaceToPrevTabStop",
107 107 "Cmd-/" : "toggleComment",
108 108 "Ctrl-/" : "toggleComment"
109 109 },
110 110 mode: 'ipython',
111 111 theme: 'ipython',
112 112 matchBrackets: true
113 113 }
114 114 };
115 115
116 116 CodeCell.msg_cells = {};
117 117
118 118 CodeCell.prototype = Object.create(Cell.prototype);
119 119
120 120 /**
121 121 * @method auto_highlight
122 122 */
123 123 CodeCell.prototype.auto_highlight = function () {
124 124 this._auto_highlight(this.config.cell_magic_highlight);
125 125 };
126 126
127 127 /** @method create_element */
128 128 CodeCell.prototype.create_element = function () {
129 129 Cell.prototype.create_element.apply(this, arguments);
130 130
131 131 var cell = $('<div></div>').addClass('cell code_cell');
132 132 cell.attr('tabindex','2');
133 133
134 134 var input = $('<div></div>').addClass('input');
135 135 var prompt = $('<div/>').addClass('prompt input_prompt');
136 136 var inner_cell = $('<div/>').addClass('inner_cell');
137 137 this.celltoolbar = new celltoolbar.CellToolbar({
138 138 cell: this,
139 139 notebook: this.notebook});
140 140 inner_cell.append(this.celltoolbar.element);
141 141 var input_area = $('<div/>').addClass('input_area');
142 142 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
143 143 this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this))
144 144 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
145 145 inner_cell.append(input_area);
146 146 input.append(prompt).append(inner_cell);
147 147
148 148 var widget_area = $('<div/>')
149 149 .addClass('widget-area')
150 150 .hide();
151 151 this.widget_area = widget_area;
152 152 var widget_prompt = $('<div/>')
153 153 .addClass('prompt')
154 154 .appendTo(widget_area);
155 155 var widget_subarea = $('<div/>')
156 156 .addClass('widget-subarea')
157 157 .appendTo(widget_area);
158 158 this.widget_subarea = widget_subarea;
159 159 var widget_clear_buton = $('<button />')
160 160 .addClass('close')
161 161 .html('&times;')
162 162 .click(function() {
163 163 widget_area.slideUp('', function(){ widget_subarea.html(''); });
164 164 })
165 165 .appendTo(widget_prompt);
166 166
167 167 var output = $('<div></div>');
168 168 cell.append(input).append(widget_area).append(output);
169 169 this.element = cell;
170 170 this.output_area = new outputarea.OutputArea({
171 171 selector: output,
172 172 prompt_area: true,
173 173 events: this.events,
174 174 keyboard_manager: this.keyboard_manager});
175 175 this.completer = new completer.Completer(this, this.events);
176 176 };
177 177
178 178 /** @method bind_events */
179 179 CodeCell.prototype.bind_events = function () {
180 180 Cell.prototype.bind_events.apply(this);
181 181 var that = this;
182 182
183 183 this.element.focusout(
184 184 function() { that.auto_highlight(); }
185 185 );
186 186 };
187 187
188 188
189 189 /**
190 190 * This method gets called in CodeMirror's onKeyDown/onKeyPress
191 191 * handlers and is used to provide custom key handling. Its return
192 192 * value is used to determine if CodeMirror should ignore the event:
193 193 * true = ignore, false = don't ignore.
194 194 * @method handle_codemirror_keyevent
195 195 */
196 196 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
197 197
198 198 var that = this;
199 199 // whatever key is pressed, first, cancel the tooltip request before
200 200 // they are sent, and remove tooltip if any, except for tab again
201 201 var tooltip_closed = null;
202 202 if (event.type === 'keydown' && event.which != keycodes.tab ) {
203 203 tooltip_closed = this.tooltip.remove_and_cancel_tooltip();
204 204 }
205 205
206 206 var cur = editor.getCursor();
207 207 if (event.keyCode === keycodes.enter){
208 208 this.auto_highlight();
209 209 }
210 210
211 211 if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) {
212 212 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
213 213 // browser and keyboard layout !
214 214 // Pressing '(' , request tooltip, don't forget to reappend it
215 215 // The second argument says to hide the tooltip if the docstring
216 216 // is actually empty
217 217 this.tooltip.pending(that, true);
218 218 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
219 219 // If tooltip is active, cancel it. The call to
220 220 // remove_and_cancel_tooltip above doesn't pass, force=true.
221 221 // Because of this it won't actually close the tooltip
222 222 // if it is in sticky mode. Thus, we have to check again if it is open
223 223 // and close it with force=true.
224 224 if (!this.tooltip._hidden) {
225 225 this.tooltip.remove_and_cancel_tooltip(true);
226 226 }
227 227 // If we closed the tooltip, don't let CM or the global handlers
228 228 // handle this event.
229 229 event.codemirrorIgnore = true;
230 230 event.preventDefault();
231 231 return true;
232 232 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
233 233 if (editor.somethingSelected() || editor.getSelections().length !== 1){
234 234 var anchor = editor.getCursor("anchor");
235 235 var head = editor.getCursor("head");
236 236 if( anchor.line != head.line){
237 237 return false;
238 238 }
239 239 }
240 240 this.tooltip.request(that);
241 241 event.codemirrorIgnore = true;
242 242 event.preventDefault();
243 243 return true;
244 244 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
245 245 // Tab completion.
246 246 this.tooltip.remove_and_cancel_tooltip();
247 247
248 248 // completion does not work on multicursor, it might be possible though in some cases
249 249 if (editor.somethingSelected() || editor.getSelections().length > 1) {
250 250 return false;
251 251 }
252 252 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
253 253 if (pre_cursor.trim() === "") {
254 254 // Don't autocomplete if the part of the line before the cursor
255 255 // is empty. In this case, let CodeMirror handle indentation.
256 256 return false;
257 257 } else {
258 258 event.codemirrorIgnore = true;
259 259 event.preventDefault();
260 260 this.completer.startCompletion();
261 261 return true;
262 262 }
263 263 }
264 264
265 265 // keyboard event wasn't one of those unique to code cells, let's see
266 266 // if it's one of the generic ones (i.e. check edit mode shortcuts)
267 267 return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]);
268 268 };
269 269
270 270 // Kernel related calls.
271 271
272 272 CodeCell.prototype.set_kernel = function (kernel) {
273 273 this.kernel = kernel;
274 274 };
275 275
276 276 /**
277 277 * Execute current code cell to the kernel
278 278 * @method execute
279 279 */
280 280 CodeCell.prototype.execute = function () {
281 if (!this.kernel || !this.kernel.is_connected()) {
282 console.log("Can't execute, kernel is not connected.");
283 return;
284 }
285
281 286 this.output_area.clear_output();
282 287
283 288 // Clear widget area
284 289 this.widget_subarea.html('');
285 290 this.widget_subarea.height('');
286 291 this.widget_area.height('');
287 292 this.widget_area.hide();
288 293
289 294 this.set_input_prompt('*');
290 295 this.element.addClass("running");
291 296 if (this.last_msg_id) {
292 297 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
293 298 }
294 299 var callbacks = this.get_callbacks();
295 300
296 301 var old_msg_id = this.last_msg_id;
297 302 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
298 303 if (old_msg_id) {
299 304 delete CodeCell.msg_cells[old_msg_id];
300 305 }
301 306 CodeCell.msg_cells[this.last_msg_id] = this;
302 307 this.render();
303 308 };
304 309
305 310 /**
306 311 * Construct the default callbacks for
307 312 * @method get_callbacks
308 313 */
309 314 CodeCell.prototype.get_callbacks = function () {
310 315 return {
311 316 shell : {
312 317 reply : $.proxy(this._handle_execute_reply, this),
313 318 payload : {
314 319 set_next_input : $.proxy(this._handle_set_next_input, this),
315 320 page : $.proxy(this._open_with_pager, this)
316 321 }
317 322 },
318 323 iopub : {
319 324 output : $.proxy(this.output_area.handle_output, this.output_area),
320 325 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
321 326 },
322 327 input : $.proxy(this._handle_input_request, this)
323 328 };
324 329 };
325 330
326 331 CodeCell.prototype._open_with_pager = function (payload) {
327 332 this.events.trigger('open_with_text.Pager', payload);
328 333 };
329 334
330 335 /**
331 336 * @method _handle_execute_reply
332 337 * @private
333 338 */
334 339 CodeCell.prototype._handle_execute_reply = function (msg) {
335 340 this.set_input_prompt(msg.content.execution_count);
336 341 this.element.removeClass("running");
337 342 this.events.trigger('set_dirty.Notebook', {value: true});
338 343 };
339 344
340 345 /**
341 346 * @method _handle_set_next_input
342 347 * @private
343 348 */
344 349 CodeCell.prototype._handle_set_next_input = function (payload) {
345 350 var data = {'cell': this, 'text': payload.text};
346 351 this.events.trigger('set_next_input.Notebook', data);
347 352 };
348 353
349 354 /**
350 355 * @method _handle_input_request
351 356 * @private
352 357 */
353 358 CodeCell.prototype._handle_input_request = function (msg) {
354 359 this.output_area.append_raw_input(msg);
355 360 };
356 361
357 362
358 363 // Basic cell manipulation.
359 364
360 365 CodeCell.prototype.select = function () {
361 366 var cont = Cell.prototype.select.apply(this);
362 367 if (cont) {
363 368 this.code_mirror.refresh();
364 369 this.auto_highlight();
365 370 }
366 371 return cont;
367 372 };
368 373
369 374 CodeCell.prototype.render = function () {
370 375 var cont = Cell.prototype.render.apply(this);
371 376 // Always execute, even if we are already in the rendered state
372 377 return cont;
373 378 };
374 379
375 380 CodeCell.prototype.select_all = function () {
376 381 var start = {line: 0, ch: 0};
377 382 var nlines = this.code_mirror.lineCount();
378 383 var last_line = this.code_mirror.getLine(nlines-1);
379 384 var end = {line: nlines-1, ch: last_line.length};
380 385 this.code_mirror.setSelection(start, end);
381 386 };
382 387
383 388
384 389 CodeCell.prototype.collapse_output = function () {
385 390 this.output_area.collapse();
386 391 };
387 392
388 393
389 394 CodeCell.prototype.expand_output = function () {
390 395 this.output_area.expand();
391 396 this.output_area.unscroll_area();
392 397 };
393 398
394 399 CodeCell.prototype.scroll_output = function () {
395 400 this.output_area.expand();
396 401 this.output_area.scroll_if_long();
397 402 };
398 403
399 404 CodeCell.prototype.toggle_output = function () {
400 405 this.output_area.toggle_output();
401 406 };
402 407
403 408 CodeCell.prototype.toggle_output_scroll = function () {
404 409 this.output_area.toggle_scroll();
405 410 };
406 411
407 412
408 413 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
409 414 var ns;
410 415 if (prompt_value === undefined || prompt_value === null) {
411 416 ns = "&nbsp;";
412 417 } else {
413 418 ns = encodeURIComponent(prompt_value);
414 419 }
415 420 return 'In&nbsp;[' + ns + ']:';
416 421 };
417 422
418 423 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
419 424 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
420 425 for(var i=1; i < lines_number; i++) {
421 426 html.push(['...:']);
422 427 }
423 428 return html.join('<br/>');
424 429 };
425 430
426 431 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
427 432
428 433
429 434 CodeCell.prototype.set_input_prompt = function (number) {
430 435 var nline = 1;
431 436 if (this.code_mirror !== undefined) {
432 437 nline = this.code_mirror.lineCount();
433 438 }
434 439 this.input_prompt_number = number;
435 440 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
436 441 // This HTML call is okay because the user contents are escaped.
437 442 this.element.find('div.input_prompt').html(prompt_html);
438 443 };
439 444
440 445
441 446 CodeCell.prototype.clear_input = function () {
442 447 this.code_mirror.setValue('');
443 448 };
444 449
445 450
446 451 CodeCell.prototype.get_text = function () {
447 452 return this.code_mirror.getValue();
448 453 };
449 454
450 455
451 456 CodeCell.prototype.set_text = function (code) {
452 457 return this.code_mirror.setValue(code);
453 458 };
454 459
455 460
456 461 CodeCell.prototype.clear_output = function (wait) {
457 462 this.output_area.clear_output(wait);
458 463 this.set_input_prompt();
459 464 };
460 465
461 466
462 467 // JSON serialization
463 468
464 469 CodeCell.prototype.fromJSON = function (data) {
465 470 Cell.prototype.fromJSON.apply(this, arguments);
466 471 if (data.cell_type === 'code') {
467 472 if (data.source !== undefined) {
468 473 this.set_text(data.source);
469 474 // make this value the starting point, so that we can only undo
470 475 // to this state, instead of a blank cell
471 476 this.code_mirror.clearHistory();
472 477 this.auto_highlight();
473 478 }
474 479 this.set_input_prompt(data.execution_count);
475 480 this.output_area.trusted = data.metadata.trusted || false;
476 481 this.output_area.fromJSON(data.outputs);
477 482 if (data.metadata.collapsed !== undefined) {
478 483 if (data.metadata.collapsed) {
479 484 this.collapse_output();
480 485 } else {
481 486 this.expand_output();
482 487 }
483 488 }
484 489 }
485 490 };
486 491
487 492
488 493 CodeCell.prototype.toJSON = function () {
489 494 var data = Cell.prototype.toJSON.apply(this);
490 495 data.source = this.get_text();
491 496 // is finite protect against undefined and '*' value
492 497 if (isFinite(this.input_prompt_number)) {
493 498 data.execution_count = this.input_prompt_number;
494 499 } else {
495 500 data.execution_count = null;
496 501 }
497 502 var outputs = this.output_area.toJSON();
498 503 data.outputs = outputs;
499 504 data.metadata.trusted = this.output_area.trusted;
500 505 data.metadata.collapsed = this.output_area.collapsed;
501 506 return data;
502 507 };
503 508
504 509 /**
505 510 * handle cell level logic when a cell is unselected
506 511 * @method unselect
507 512 * @return is the action being taken
508 513 */
509 514 CodeCell.prototype.unselect = function () {
510 515 var cont = Cell.prototype.unselect.apply(this);
511 516 if (cont) {
512 517 // When a code cell is usnelected, make sure that the corresponding
513 518 // tooltip and completer to that cell is closed.
514 519 this.tooltip.remove_and_cancel_tooltip(true);
515 520 if (this.completer !== null) {
516 521 this.completer.close();
517 522 }
518 523 }
519 524 return cont;
520 525 };
521 526
522 527 // Backwards compatability.
523 528 IPython.CodeCell = CodeCell;
524 529
525 530 return {'CodeCell': CodeCell};
526 531 });
General Comments 0
You need to be logged in to leave comments. Login now