##// END OF EJS Templates
typo, use keycodes object instead of magic numbers
Paul Ivanov -
Show More
@@ -1,543 +1,543
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 // Cell
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 Cell
16 16 */
17 17
18 18 var IPython = (function (IPython) {
19 19 "use strict";
20 20
21 21 var utils = IPython.utils;
22 22
23 23 /**
24 24 * The Base `Cell` class from which to inherit
25 25 * @class Cell
26 26 **/
27 27
28 28 /*
29 29 * @constructor
30 30 *
31 31 * @param {object|undefined} [options]
32 32 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
33 33 */
34 34 var Cell = function (options) {
35 35
36 36 options = this.mergeopt(Cell, options);
37 37 // superclass default overwrite our default
38 38
39 39 this.placeholder = options.placeholder || '';
40 40 this.read_only = options.cm_config.readOnly;
41 41 this.selected = false;
42 42 this.rendered = false;
43 43 this.mode = 'command';
44 44 this.metadata = {};
45 45 // load this from metadata later ?
46 46 this.user_highlight = 'auto';
47 47 this.cm_config = options.cm_config;
48 48 this.cell_id = utils.uuid();
49 49 this._options = options;
50 50
51 51 // For JS VM engines optimization, attributes should be all set (even
52 52 // to null) in the constructor, and if possible, if different subclass
53 53 // have new attributes with same name, they should be created in the
54 54 // same order. Easiest is to create and set to null in parent class.
55 55
56 56 this.element = null;
57 57 this.cell_type = this.cell_type || null;
58 58 this.code_mirror = null;
59 59
60 60 this.create_element();
61 61 if (this.element !== null) {
62 62 this.element.data("cell", this);
63 63 this.bind_events();
64 64 this.init_classes();
65 65 }
66 66 };
67 67
68 68 Cell.options_default = {
69 69 cm_config : {
70 70 indentUnit : 4,
71 71 readOnly: false,
72 72 theme: "default"
73 73 }
74 74 };
75 75
76 76 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
77 77 // by disabling drag/drop altogether on Safari
78 78 // https://github.com/marijnh/CodeMirror/issues/332
79 79 if (utils.browser[0] == "Safari") {
80 80 Cell.options_default.cm_config.dragDrop = false;
81 81 }
82 82
83 83 Cell.prototype.mergeopt = function(_class, options, overwrite){
84 84 options = options || {};
85 85 overwrite = overwrite || {};
86 86 return $.extend(true, {}, _class.options_default, options, overwrite);
87 87 };
88 88
89 89 /**
90 90 * Empty. Subclasses must implement create_element.
91 91 * This should contain all the code to create the DOM element in notebook
92 92 * and will be called by Base Class constructor.
93 93 * @method create_element
94 94 */
95 95 Cell.prototype.create_element = function () {
96 96 };
97 97
98 98 Cell.prototype.init_classes = function () {
99 99 // Call after this.element exists to initialize the css classes
100 100 // related to selected, rendered and mode.
101 101 if (this.selected) {
102 102 this.element.addClass('selected');
103 103 } else {
104 104 this.element.addClass('unselected');
105 105 }
106 106 if (this.rendered) {
107 107 this.element.addClass('rendered');
108 108 } else {
109 109 this.element.addClass('unrendered');
110 110 }
111 111 if (this.mode === 'edit') {
112 112 this.element.addClass('edit_mode');
113 113 } else {
114 114 this.element.addClass('command_mode');
115 115 }
116 116 };
117 117
118 118 /**
119 119 * Subclasses can implement override bind_events.
120 120 * Be carefull to call the parent method when overwriting as it fires event.
121 121 * this will be triggerd after create_element in constructor.
122 122 * @method bind_events
123 123 */
124 124 Cell.prototype.bind_events = function () {
125 125 var that = this;
126 126 // We trigger events so that Cell doesn't have to depend on Notebook.
127 127 that.element.click(function (event) {
128 128 if (!that.selected) {
129 129 $([IPython.events]).trigger('select.Cell', {'cell':that});
130 130 }
131 131 });
132 132 that.element.focusin(function (event) {
133 133 if (!that.selected) {
134 134 $([IPython.events]).trigger('select.Cell', {'cell':that});
135 135 }
136 136 });
137 137 if (this.code_mirror) {
138 138 this.code_mirror.on("change", function(cm, change) {
139 139 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
140 140 });
141 141 }
142 142 if (this.code_mirror) {
143 143 this.code_mirror.on('focus', function(cm, change) {
144 144 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
145 145 });
146 146 }
147 147 if (this.code_mirror) {
148 148 this.code_mirror.on('blur', function(cm, change) {
149 149 // Check if this unfocus event is legit.
150 150 if (!that.should_cancel_blur()) {
151 151 $([IPython.events]).trigger('command_mode.Cell', {cell: that});
152 152 }
153 153 });
154 154 }
155 155 };
156 156
157 157 /**
158 158 * Triger typsetting of math by mathjax on current cell element
159 159 * @method typeset
160 160 */
161 161 Cell.prototype.typeset = function () {
162 162 if (window.MathJax) {
163 163 var cell_math = this.element.get(0);
164 164 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
165 165 }
166 166 };
167 167
168 168 /**
169 169 * handle cell level logic when a cell is selected
170 170 * @method select
171 171 * @return is the action being taken
172 172 */
173 173 Cell.prototype.select = function () {
174 174 if (!this.selected) {
175 175 this.element.addClass('selected');
176 176 this.element.removeClass('unselected');
177 177 this.selected = true;
178 178 return true;
179 179 } else {
180 180 return false;
181 181 }
182 182 };
183 183
184 184 /**
185 185 * handle cell level logic when a cell is unselected
186 186 * @method unselect
187 187 * @return is the action being taken
188 188 */
189 189 Cell.prototype.unselect = function () {
190 190 if (this.selected) {
191 191 this.element.addClass('unselected');
192 192 this.element.removeClass('selected');
193 193 this.selected = false;
194 194 return true;
195 195 } else {
196 196 return false;
197 197 }
198 198 };
199 199
200 200 /**
201 201 * handle cell level logic when a cell is rendered
202 202 * @method render
203 203 * @return is the action being taken
204 204 */
205 205 Cell.prototype.render = function () {
206 206 if (!this.rendered) {
207 207 this.element.addClass('rendered');
208 208 this.element.removeClass('unrendered');
209 209 this.rendered = true;
210 210 return true;
211 211 } else {
212 212 return false;
213 213 }
214 214 };
215 215
216 216 /**
217 217 * handle cell level logic when a cell is unrendered
218 218 * @method unrender
219 219 * @return is the action being taken
220 220 */
221 221 Cell.prototype.unrender = function () {
222 222 if (this.rendered) {
223 223 this.element.addClass('unrendered');
224 224 this.element.removeClass('rendered');
225 225 this.rendered = false;
226 226 return true;
227 227 } else {
228 228 return false;
229 229 }
230 230 };
231 231
232 232 /**
233 233 * Either delegates keyboard shortcut handling to either IPython keyboard
234 234 * manager when in command mode, or CodeMirror when in edit mode
235 235 *
236 236 * @method handle_keyevent
237 237 * @param {CodeMirror} editor - The codemirror instance bound to the cell
238 238 * @param {event} event -
239 239 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
240 240 */
241 CodeCell.prototype.handle_keyevent = function (editor, event) {
241 Cell.prototype.handle_keyevent = function (editor, event) {
242 242
243 243 // console.log('CM', this.mode, event.which, event.type)
244 244
245 245 if (this.mode === 'command') {
246 246 return true;
247 247 } else if (this.mode === 'edit') {
248 248 return this.handle_codemirror_keyevent(editor, event);
249 249 }
250 250 };
251 251
252 252 /**
253 253 * @method at_top
254 254 * @return {Boolean}
255 255 */
256 256 Cell.prototype.at_top = function () {
257 257 var cm = this.code_mirror
258 258 var cursor = cm.getCursor();
259 259 if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) {
260 260 console.log('at top');
261 261 return true;
262 262 } else {
263 263 return false;
264 264 }
265 265 };
266 266
267 267 /**
268 268 * @method at_bottom
269 269 * @return {Boolean}
270 270 * */
271 271 Cell.prototype.at_bottom = function () {
272 272 var cm = this.code_mirror
273 273 var cursor = cm.getCursor();
274 274 if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) {
275 275 return true;
276 276 } else {
277 277 return false;
278 278 }
279 279 };
280 280 /**
281 281 * enter the command mode for the cell
282 282 * @method command_mode
283 283 * @return is the action being taken
284 284 */
285 285 Cell.prototype.command_mode = function () {
286 286 if (this.mode !== 'command') {
287 287 this.element.addClass('command_mode');
288 288 this.element.removeClass('edit_mode');
289 289 this.mode = 'command';
290 290 return true;
291 291 } else {
292 292 return false;
293 293 }
294 294 };
295 295
296 296 /**
297 297 * enter the edit mode for the cell
298 298 * @method command_mode
299 299 * @return is the action being taken
300 300 */
301 301 Cell.prototype.edit_mode = function () {
302 302 if (this.mode !== 'edit') {
303 303 this.element.addClass('edit_mode');
304 304 this.element.removeClass('command_mode');
305 305 this.mode = 'edit';
306 306 return true;
307 307 } else {
308 308 return false;
309 309 }
310 310 };
311 311
312 312 /**
313 313 * Determine whether or not the unfocus event should be aknowledged.
314 314 *
315 315 * @method should_cancel_blur
316 316 *
317 317 * @return results {bool} Whether or not to ignore the cell's blur event.
318 318 **/
319 319 Cell.prototype.should_cancel_blur = function () {
320 320 return false;
321 321 };
322 322
323 323 /**
324 324 * Focus the cell in the DOM sense
325 325 * @method focus_cell
326 326 */
327 327 Cell.prototype.focus_cell = function () {
328 328 this.element.focus();
329 329 };
330 330
331 331 /**
332 332 * Focus the editor area so a user can type
333 333 *
334 334 * NOTE: If codemirror is focused via a mouse click event, you don't want to
335 335 * call this because it will cause a page jump.
336 336 * @method focus_editor
337 337 */
338 338 Cell.prototype.focus_editor = function () {
339 339 this.refresh();
340 340 this.code_mirror.focus();
341 341 };
342 342
343 343 /**
344 344 * Refresh codemirror instance
345 345 * @method refresh
346 346 */
347 347 Cell.prototype.refresh = function () {
348 348 this.code_mirror.refresh();
349 349 };
350 350
351 351 /**
352 352 * should be overritten by subclass
353 353 * @method get_text
354 354 */
355 355 Cell.prototype.get_text = function () {
356 356 };
357 357
358 358 /**
359 359 * should be overritten by subclass
360 360 * @method set_text
361 361 * @param {string} text
362 362 */
363 363 Cell.prototype.set_text = function (text) {
364 364 };
365 365
366 366 /**
367 367 * should be overritten by subclass
368 368 * serialise cell to json.
369 369 * @method toJSON
370 370 **/
371 371 Cell.prototype.toJSON = function () {
372 372 var data = {};
373 373 data.metadata = this.metadata;
374 374 data.cell_type = this.cell_type;
375 375 return data;
376 376 };
377 377
378 378
379 379 /**
380 380 * should be overritten by subclass
381 381 * @method fromJSON
382 382 **/
383 383 Cell.prototype.fromJSON = function (data) {
384 384 if (data.metadata !== undefined) {
385 385 this.metadata = data.metadata;
386 386 }
387 387 this.celltoolbar.rebuild();
388 388 };
389 389
390 390
391 391 /**
392 392 * can the cell be split into two cells
393 393 * @method is_splittable
394 394 **/
395 395 Cell.prototype.is_splittable = function () {
396 396 return true;
397 397 };
398 398
399 399
400 400 /**
401 401 * can the cell be merged with other cells
402 402 * @method is_mergeable
403 403 **/
404 404 Cell.prototype.is_mergeable = function () {
405 405 return true;
406 406 };
407 407
408 408
409 409 /**
410 410 * @return {String} - the text before the cursor
411 411 * @method get_pre_cursor
412 412 **/
413 413 Cell.prototype.get_pre_cursor = function () {
414 414 var cursor = this.code_mirror.getCursor();
415 415 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
416 416 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
417 417 return text;
418 418 };
419 419
420 420
421 421 /**
422 422 * @return {String} - the text after the cursor
423 423 * @method get_post_cursor
424 424 **/
425 425 Cell.prototype.get_post_cursor = function () {
426 426 var cursor = this.code_mirror.getCursor();
427 427 var last_line_num = this.code_mirror.lineCount()-1;
428 428 var last_line_len = this.code_mirror.getLine(last_line_num).length;
429 429 var end = {line:last_line_num, ch:last_line_len};
430 430 var text = this.code_mirror.getRange(cursor, end);
431 431 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
432 432 return text;
433 433 };
434 434
435 435 /**
436 436 * Show/Hide CodeMirror LineNumber
437 437 * @method show_line_numbers
438 438 *
439 439 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
440 440 **/
441 441 Cell.prototype.show_line_numbers = function (value) {
442 442 this.code_mirror.setOption('lineNumbers', value);
443 443 this.code_mirror.refresh();
444 444 };
445 445
446 446 /**
447 447 * Toggle CodeMirror LineNumber
448 448 * @method toggle_line_numbers
449 449 **/
450 450 Cell.prototype.toggle_line_numbers = function () {
451 451 var val = this.code_mirror.getOption('lineNumbers');
452 452 this.show_line_numbers(!val);
453 453 };
454 454
455 455 /**
456 456 * Force codemirror highlight mode
457 457 * @method force_highlight
458 458 * @param {object} - CodeMirror mode
459 459 **/
460 460 Cell.prototype.force_highlight = function(mode) {
461 461 this.user_highlight = mode;
462 462 this.auto_highlight();
463 463 };
464 464
465 465 /**
466 466 * Try to autodetect cell highlight mode, or use selected mode
467 467 * @methods _auto_highlight
468 468 * @private
469 469 * @param {String|object|undefined} - CodeMirror mode | 'auto'
470 470 **/
471 471 Cell.prototype._auto_highlight = function (modes) {
472 472 //Here we handle manually selected modes
473 473 var mode;
474 474 if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
475 475 {
476 476 mode = this.user_highlight;
477 477 CodeMirror.autoLoadMode(this.code_mirror, mode);
478 478 this.code_mirror.setOption('mode', mode);
479 479 return;
480 480 }
481 481 var current_mode = this.code_mirror.getOption('mode', mode);
482 482 var first_line = this.code_mirror.getLine(0);
483 483 // loop on every pairs
484 484 for(mode in modes) {
485 485 var regs = modes[mode].reg;
486 486 // only one key every time but regexp can't be keys...
487 487 for(var i=0; i<regs.length; i++) {
488 488 // here we handle non magic_modes
489 489 if(first_line.match(regs[i]) !== null) {
490 490 if(current_mode == mode){
491 491 return;
492 492 }
493 493 if (mode.search('magic_') !== 0) {
494 494 this.code_mirror.setOption('mode', mode);
495 495 CodeMirror.autoLoadMode(this.code_mirror, mode);
496 496 return;
497 497 }
498 498 var open = modes[mode].open || "%%";
499 499 var close = modes[mode].close || "%%end";
500 500 var mmode = mode;
501 501 mode = mmode.substr(6);
502 502 if(current_mode == mode){
503 503 return;
504 504 }
505 505 CodeMirror.autoLoadMode(this.code_mirror, mode);
506 506 // create on the fly a mode that swhitch between
507 507 // plain/text and smth else otherwise `%%` is
508 508 // source of some highlight issues.
509 509 // we use patchedGetMode to circumvent a bug in CM
510 510 CodeMirror.defineMode(mmode , function(config) {
511 511 return CodeMirror.multiplexingMode(
512 512 CodeMirror.patchedGetMode(config, 'text/plain'),
513 513 // always set someting on close
514 514 {open: open, close: close,
515 515 mode: CodeMirror.patchedGetMode(config, mode),
516 516 delimStyle: "delimit"
517 517 }
518 518 );
519 519 });
520 520 this.code_mirror.setOption('mode', mmode);
521 521 return;
522 522 }
523 523 }
524 524 }
525 525 // fallback on default
526 526 var default_mode;
527 527 try {
528 528 default_mode = this._options.cm_config.mode;
529 529 } catch(e) {
530 530 default_mode = 'text/plain';
531 531 }
532 532 if( current_mode === default_mode){
533 533 return;
534 534 }
535 535 this.code_mirror.setOption('mode', default_mode);
536 536 };
537 537
538 538 IPython.Cell = Cell;
539 539
540 540 return IPython;
541 541
542 542 }(IPython));
543 543
@@ -1,549 +1,549
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 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 keycodes = IPython.keyboard.keycodes;
49 49
50 50 /**
51 51 * A Cell conceived to write code.
52 52 *
53 53 * The kernel doesn't have to be set at creation time, in that case
54 54 * it will be null and set_kernel has to be called later.
55 55 * @class CodeCell
56 56 * @extends IPython.Cell
57 57 *
58 58 * @constructor
59 59 * @param {Object|null} kernel
60 60 * @param {object|undefined} [options]
61 61 * @param [options.cm_config] {object} config to pass to CodeMirror
62 62 */
63 63 var CodeCell = function (kernel, options) {
64 64 this.kernel = kernel || null;
65 65 this.collapsed = false;
66 66
67 67 // create all attributed in constructor function
68 68 // even if null for V8 VM optimisation
69 69 this.input_prompt_number = null;
70 70 this.celltoolbar = null;
71 71 this.output_area = null;
72 72 this.last_msg_id = null;
73 73 this.completer = null;
74 74
75 75
76 76 var cm_overwrite_options = {
77 77 onKeyEvent: $.proxy(this.handle_keyevent,this)
78 78 };
79 79
80 80 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
81 81
82 82 IPython.Cell.apply(this,[options]);
83 83
84 84 // Attributes we want to override in this subclass.
85 85 this.cell_type = "code";
86 86
87 87 var that = this;
88 88 this.element.focusout(
89 89 function() { that.auto_highlight(); }
90 90 );
91 91 };
92 92
93 93 CodeCell.options_default = {
94 94 cm_config : {
95 95 extraKeys: {
96 96 "Tab" : "indentMore",
97 97 "Shift-Tab" : "indentLess",
98 98 "Backspace" : "delSpaceToPrevTabStop",
99 99 "Cmd-/" : "toggleComment",
100 100 "Ctrl-/" : "toggleComment"
101 101 },
102 102 mode: 'ipython',
103 103 theme: 'ipython',
104 104 matchBrackets: true,
105 105 autoCloseBrackets: true
106 106 }
107 107 };
108 108
109 109 CodeCell.msg_cells = {};
110 110
111 111 CodeCell.prototype = new IPython.Cell();
112 112
113 113 /**
114 114 * @method auto_highlight
115 115 */
116 116 CodeCell.prototype.auto_highlight = function () {
117 117 this._auto_highlight(IPython.config.cell_magic_highlight);
118 118 };
119 119
120 120 /** @method create_element */
121 121 CodeCell.prototype.create_element = function () {
122 122 IPython.Cell.prototype.create_element.apply(this, arguments);
123 123
124 124 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
125 125 cell.attr('tabindex','2');
126 126
127 127 var input = $('<div></div>').addClass('input');
128 128 var prompt = $('<div/>').addClass('prompt input_prompt');
129 129 var inner_cell = $('<div/>').addClass('inner_cell');
130 130 this.celltoolbar = new IPython.CellToolbar(this);
131 131 inner_cell.append(this.celltoolbar.element);
132 132 var input_area = $('<div/>').addClass('input_area');
133 133 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
134 134 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
135 135 inner_cell.append(input_area);
136 136 input.append(prompt).append(inner_cell);
137 137
138 138 var widget_area = $('<div/>')
139 139 .addClass('widget-area')
140 140 .hide();
141 141 this.widget_area = widget_area;
142 142 var widget_prompt = $('<div/>')
143 143 .addClass('prompt')
144 144 .appendTo(widget_area);
145 145 var widget_subarea = $('<div/>')
146 146 .addClass('widget-subarea')
147 147 .appendTo(widget_area);
148 148 this.widget_subarea = widget_subarea;
149 149 var widget_clear_buton = $('<button />')
150 150 .addClass('close')
151 151 .html('&times;')
152 152 .click(function() {
153 153 widget_area.slideUp('', function(){ widget_subarea.html(''); });
154 154 })
155 155 .appendTo(widget_prompt);
156 156
157 157 var output = $('<div></div>');
158 158 cell.append(input).append(widget_area).append(output);
159 159 this.element = cell;
160 160 this.output_area = new IPython.OutputArea(output, true);
161 161 this.completer = new IPython.Completer(this);
162 162 };
163 163
164 164 /** @method bind_events */
165 165 CodeCell.prototype.bind_events = function () {
166 166 IPython.Cell.prototype.bind_events.apply(this);
167 167 var that = this;
168 168
169 169 this.element.focusout(
170 170 function() { that.auto_highlight(); }
171 171 );
172 172 };
173 173
174 174
175 175 /**
176 176 * This method gets called in CodeMirror's onKeyDown/onKeyPress
177 177 * handlers and is used to provide custom key handling. Its return
178 178 * value is used to determine if CodeMirror should ignore the event:
179 179 * true = ignore, false = don't ignore.
180 180 * @method handle_codemirror_keyevent
181 181 */
182 182 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
183 183
184 184 var that = this;
185 185 // whatever key is pressed, first, cancel the tooltip request before
186 186 // they are sent, and remove tooltip if any, except for tab again
187 187 var tooltip_closed = null;
188 188 if (event.type === 'keydown' && event.which != keycodes.tab ) {
189 189 tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip();
190 190 }
191 191
192 192 var cur = editor.getCursor();
193 193 if (event.keyCode === keycodes.enter){
194 194 this.auto_highlight();
195 195 }
196 196
197 197 if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) {
198 198 // Always ignore shift-enter in CodeMirror as we handle it.
199 199 return true;
200 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
200 } else if (event.which === keycodes.down && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
201 201 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
202 202 // browser and keyboard layout !
203 203 // Pressing '(' , request tooltip, don't forget to reappend it
204 204 // The second argument says to hide the tooltip if the docstring
205 205 // is actually empty
206 206 IPython.tooltip.pending(that, true);
207 207 } else if (event.which === keycodes.up && event.type === 'keydown') {
208 208 // If we are not at the top, let CM handle the up arrow and
209 209 // prevent the global keydown handler from handling it.
210 210 if (!that.at_top()) {
211 211 event.stop();
212 212 return false;
213 213 } else {
214 214 return true;
215 215 }
216 216 } else if (event.which === keycodes.esc && event.type === 'keydown') {
217 217 // First see if the tooltip is active and if so cancel it.
218 218 if (tooltip_closed) {
219 219 // The call to remove_and_cancel_tooltip above in L177 doesn't pass
220 220 // force=true. Because of this it won't actually close the tooltip
221 221 // if it is in sticky mode. Thus, we have to check again if it is open
222 222 // and close it with force=true.
223 223 if (!IPython.tooltip._hidden) {
224 224 IPython.tooltip.remove_and_cancel_tooltip(true);
225 225 }
226 226 // If we closed the tooltip, don't let CM or the global handlers
227 227 // handle this event.
228 228 event.stop();
229 229 return true;
230 230 }
231 231 if (that.code_mirror.options.keyMap === "vim-insert") {
232 232 // vim keyMap is active and in insert mode. In this case we leave vim
233 233 // insert mode, but remain in notebook edit mode.
234 234 // Let' CM handle this event and prevent global handling.
235 235 event.stop();
236 236 return false;
237 237 } else {
238 238 // vim keyMap is not active. Leave notebook edit mode.
239 239 // Don't let CM handle the event, defer to global handling.
240 240 return true;
241 241 }
242 242 } else if (event.which === keycodes.down && event.type === 'keydown') {
243 243 // If we are not at the bottom, let CM handle the down arrow and
244 244 // prevent the global keydown handler from handling it.
245 245 if (!that.at_bottom()) {
246 246 event.stop();
247 247 return false;
248 248 } else {
249 249 return true;
250 250 }
251 251 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
252 252 if (editor.somethingSelected()){
253 253 var anchor = editor.getCursor("anchor");
254 254 var head = editor.getCursor("head");
255 255 if( anchor.line != head.line){
256 256 return false;
257 257 }
258 258 }
259 259 IPython.tooltip.request(that);
260 260 event.stop();
261 261 return true;
262 262 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
263 263 // Tab completion.
264 264 IPython.tooltip.remove_and_cancel_tooltip();
265 265 if (editor.somethingSelected()) {
266 266 return false;
267 267 }
268 268 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
269 269 if (pre_cursor.trim() === "") {
270 270 // Don't autocomplete if the part of the line before the cursor
271 271 // is empty. In this case, let CodeMirror handle indentation.
272 272 return false;
273 273 } else {
274 274 event.stop();
275 275 this.completer.startCompletion();
276 276 return true;
277 277 }
278 278 } else {
279 279 // keypress/keyup also trigger on TAB press, and we don't want to
280 280 // use those to disable tab completion.
281 281 return false;
282 282 }
283 283 return false;
284 284 };
285 285
286 286 // Kernel related calls.
287 287
288 288 CodeCell.prototype.set_kernel = function (kernel) {
289 289 this.kernel = kernel;
290 290 };
291 291
292 292 /**
293 293 * Execute current code cell to the kernel
294 294 * @method execute
295 295 */
296 296 CodeCell.prototype.execute = function () {
297 297 this.output_area.clear_output();
298 298
299 299 // Clear widget area
300 300 this.widget_subarea.html('');
301 301 this.widget_subarea.height('');
302 302 this.widget_area.height('');
303 303 this.widget_area.hide();
304 304
305 305 this.set_input_prompt('*');
306 306 this.element.addClass("running");
307 307 if (this.last_msg_id) {
308 308 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
309 309 }
310 310 var callbacks = this.get_callbacks();
311 311
312 312 var old_msg_id = this.last_msg_id;
313 313 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
314 314 if (old_msg_id) {
315 315 delete CodeCell.msg_cells[old_msg_id];
316 316 }
317 317 CodeCell.msg_cells[this.last_msg_id] = this;
318 318 };
319 319
320 320 /**
321 321 * Construct the default callbacks for
322 322 * @method get_callbacks
323 323 */
324 324 CodeCell.prototype.get_callbacks = function () {
325 325 return {
326 326 shell : {
327 327 reply : $.proxy(this._handle_execute_reply, this),
328 328 payload : {
329 329 set_next_input : $.proxy(this._handle_set_next_input, this),
330 330 page : $.proxy(this._open_with_pager, this)
331 331 }
332 332 },
333 333 iopub : {
334 334 output : $.proxy(this.output_area.handle_output, this.output_area),
335 335 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
336 336 },
337 337 input : $.proxy(this._handle_input_request, this)
338 338 };
339 339 };
340 340
341 341 CodeCell.prototype._open_with_pager = function (payload) {
342 342 $([IPython.events]).trigger('open_with_text.Pager', payload);
343 343 };
344 344
345 345 /**
346 346 * @method _handle_execute_reply
347 347 * @private
348 348 */
349 349 CodeCell.prototype._handle_execute_reply = function (msg) {
350 350 this.set_input_prompt(msg.content.execution_count);
351 351 this.element.removeClass("running");
352 352 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
353 353 };
354 354
355 355 /**
356 356 * @method _handle_set_next_input
357 357 * @private
358 358 */
359 359 CodeCell.prototype._handle_set_next_input = function (payload) {
360 360 var data = {'cell': this, 'text': payload.text};
361 361 $([IPython.events]).trigger('set_next_input.Notebook', data);
362 362 };
363 363
364 364 /**
365 365 * @method _handle_input_request
366 366 * @private
367 367 */
368 368 CodeCell.prototype._handle_input_request = function (msg) {
369 369 this.output_area.append_raw_input(msg);
370 370 };
371 371
372 372
373 373 // Basic cell manipulation.
374 374
375 375 CodeCell.prototype.select = function () {
376 376 var cont = IPython.Cell.prototype.select.apply(this);
377 377 if (cont) {
378 378 this.code_mirror.refresh();
379 379 this.auto_highlight();
380 380 }
381 381 return cont;
382 382 };
383 383
384 384 CodeCell.prototype.render = function () {
385 385 var cont = IPython.Cell.prototype.render.apply(this);
386 386 // Always execute, even if we are already in the rendered state
387 387 return cont;
388 388 };
389 389
390 390 CodeCell.prototype.unrender = function () {
391 391 // CodeCell is always rendered
392 392 return false;
393 393 };
394 394
395 395 /**
396 396 * Determine whether or not the unfocus event should be aknowledged.
397 397 *
398 398 * @method should_cancel_blur
399 399 *
400 400 * @return results {bool} Whether or not to ignore the cell's blur event.
401 401 **/
402 402 CodeCell.prototype.should_cancel_blur = function () {
403 403 // Cancel this unfocus event if the base wants to cancel or the cell
404 404 // completer is open or the tooltip is open.
405 405 return IPython.Cell.prototype.should_cancel_blur.apply(this) ||
406 406 (this.completer && this.completer.is_visible()) ||
407 407 (IPython.tooltip && IPython.tooltip.is_visible());
408 408 };
409 409
410 410 CodeCell.prototype.select_all = function () {
411 411 var start = {line: 0, ch: 0};
412 412 var nlines = this.code_mirror.lineCount();
413 413 var last_line = this.code_mirror.getLine(nlines-1);
414 414 var end = {line: nlines-1, ch: last_line.length};
415 415 this.code_mirror.setSelection(start, end);
416 416 };
417 417
418 418
419 419 CodeCell.prototype.collapse_output = function () {
420 420 this.collapsed = true;
421 421 this.output_area.collapse();
422 422 };
423 423
424 424
425 425 CodeCell.prototype.expand_output = function () {
426 426 this.collapsed = false;
427 427 this.output_area.expand();
428 428 this.output_area.unscroll_area();
429 429 };
430 430
431 431 CodeCell.prototype.scroll_output = function () {
432 432 this.output_area.expand();
433 433 this.output_area.scroll_if_long();
434 434 };
435 435
436 436 CodeCell.prototype.toggle_output = function () {
437 437 this.collapsed = Boolean(1 - this.collapsed);
438 438 this.output_area.toggle_output();
439 439 };
440 440
441 441 CodeCell.prototype.toggle_output_scroll = function () {
442 442 this.output_area.toggle_scroll();
443 443 };
444 444
445 445
446 446 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
447 447 var ns;
448 448 if (prompt_value == undefined) {
449 449 ns = "&nbsp;";
450 450 } else {
451 451 ns = encodeURIComponent(prompt_value);
452 452 }
453 453 return 'In&nbsp;[' + ns + ']:';
454 454 };
455 455
456 456 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
457 457 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
458 458 for(var i=1; i < lines_number; i++) {
459 459 html.push(['...:']);
460 460 }
461 461 return html.join('<br/>');
462 462 };
463 463
464 464 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
465 465
466 466
467 467 CodeCell.prototype.set_input_prompt = function (number) {
468 468 var nline = 1;
469 469 if (this.code_mirror !== undefined) {
470 470 nline = this.code_mirror.lineCount();
471 471 }
472 472 this.input_prompt_number = number;
473 473 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
474 474 // This HTML call is okay because the user contents are escaped.
475 475 this.element.find('div.input_prompt').html(prompt_html);
476 476 };
477 477
478 478
479 479 CodeCell.prototype.clear_input = function () {
480 480 this.code_mirror.setValue('');
481 481 };
482 482
483 483
484 484 CodeCell.prototype.get_text = function () {
485 485 return this.code_mirror.getValue();
486 486 };
487 487
488 488
489 489 CodeCell.prototype.set_text = function (code) {
490 490 return this.code_mirror.setValue(code);
491 491 };
492 492
493 493
494 494 CodeCell.prototype.clear_output = function (wait) {
495 495 this.output_area.clear_output(wait);
496 496 this.set_input_prompt();
497 497 };
498 498
499 499
500 500 // JSON serialization
501 501
502 502 CodeCell.prototype.fromJSON = function (data) {
503 503 IPython.Cell.prototype.fromJSON.apply(this, arguments);
504 504 if (data.cell_type === 'code') {
505 505 if (data.input !== undefined) {
506 506 this.set_text(data.input);
507 507 // make this value the starting point, so that we can only undo
508 508 // to this state, instead of a blank cell
509 509 this.code_mirror.clearHistory();
510 510 this.auto_highlight();
511 511 }
512 512 if (data.prompt_number !== undefined) {
513 513 this.set_input_prompt(data.prompt_number);
514 514 } else {
515 515 this.set_input_prompt();
516 516 }
517 517 this.output_area.trusted = data.trusted || false;
518 518 this.output_area.fromJSON(data.outputs);
519 519 if (data.collapsed !== undefined) {
520 520 if (data.collapsed) {
521 521 this.collapse_output();
522 522 } else {
523 523 this.expand_output();
524 524 }
525 525 }
526 526 }
527 527 };
528 528
529 529
530 530 CodeCell.prototype.toJSON = function () {
531 531 var data = IPython.Cell.prototype.toJSON.apply(this);
532 532 data.input = this.get_text();
533 533 // is finite protect against undefined and '*' value
534 534 if (isFinite(this.input_prompt_number)) {
535 535 data.prompt_number = this.input_prompt_number;
536 536 }
537 537 var outputs = this.output_area.toJSON();
538 538 data.outputs = outputs;
539 539 data.language = 'python';
540 540 data.trusted = this.output_area.trusted;
541 541 data.collapsed = this.collapsed;
542 542 return data;
543 543 };
544 544
545 545
546 546 IPython.CodeCell = CodeCell;
547 547
548 548 return IPython;
549 549 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now