##// END OF EJS Templates
prompt '*' strore fix + tab remove tooltip...
Matthias BUSSONNIER -
Show More
@@ -1,460 +1,463 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 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
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.code_mirror = null;
66 66 this.input_prompt_number = null;
67 67 this.collapsed = false;
68 68 this.cell_type = "code";
69 69
70 70
71 71 var cm_overwrite_options = {
72 72 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
73 73 };
74 74
75 75 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
76 76
77 77 IPython.Cell.apply(this,[options]);
78 78
79 79 var that = this;
80 80 this.element.focusout(
81 81 function() { that.auto_highlight(); }
82 82 );
83 83 };
84 84
85 85 CodeCell.options_default = {
86 86 cm_config : {
87 87 extraKeys: {
88 88 "Tab" : "indentMore",
89 89 "Shift-Tab" : "indentLess",
90 90 "Backspace" : "delSpaceToPrevTabStop",
91 91 "Cmd-/" : "toggleComment",
92 92 "Ctrl-/" : "toggleComment"
93 93 },
94 94 mode: 'ipython',
95 95 theme: 'ipython',
96 96 matchBrackets: true
97 97 }
98 98 };
99 99
100 100
101 101 CodeCell.prototype = new IPython.Cell();
102 102
103 103 /**
104 104 * @method auto_highlight
105 105 */
106 106 CodeCell.prototype.auto_highlight = function () {
107 107 this._auto_highlight(IPython.config.cell_magic_highlight);
108 108 };
109 109
110 110 /** @method create_element */
111 111 CodeCell.prototype.create_element = function () {
112 112 IPython.Cell.prototype.create_element.apply(this, arguments);
113 113
114 114 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
115 115 cell.attr('tabindex','2');
116 116
117 117 this.celltoolbar = new IPython.CellToolbar(this);
118 118
119 119 var input = $('<div></div>').addClass('input');
120 120 var vbox = $('<div/>').addClass('vbox box-flex1');
121 121 input.append($('<div/>').addClass('prompt input_prompt'));
122 122 vbox.append(this.celltoolbar.element);
123 123 var input_area = $('<div/>').addClass('input_area');
124 124 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
125 125 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
126 126 vbox.append(input_area);
127 127 input.append(vbox);
128 128 var output = $('<div></div>');
129 129 cell.append(input).append(output);
130 130 this.element = cell;
131 131 this.output_area = new IPython.OutputArea(output, true);
132 132
133 133 // construct a completer only if class exist
134 134 // otherwise no print view
135 135 if (IPython.Completer !== undefined)
136 136 {
137 137 this.completer = new IPython.Completer(this);
138 138 }
139 139 };
140 140
141 141 /**
142 142 * This method gets called in CodeMirror's onKeyDown/onKeyPress
143 143 * handlers and is used to provide custom key handling. Its return
144 144 * value is used to determine if CodeMirror should ignore the event:
145 145 * true = ignore, false = don't ignore.
146 146 * @method handle_codemirror_keyevent
147 147 */
148 148 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
149 149
150 150 var that = this;
151 151 // whatever key is pressed, first, cancel the tooltip request before
152 152 // they are sent, and remove tooltip if any, except for tab again
153 153 if (event.type === 'keydown' && event.which != key.TAB ) {
154 154 IPython.tooltip.remove_and_cancel_tooltip();
155 155 }
156 156
157 157 var cur = editor.getCursor();
158 158 if (event.keyCode === key.ENTER){
159 159 this.auto_highlight();
160 160 }
161 161
162 162 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
163 163 // Always ignore shift-enter in CodeMirror as we handle it.
164 164 return true;
165 165 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
166 166 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
167 167 // browser and keyboard layout !
168 168 // Pressing '(' , request tooltip, don't forget to reappend it
169 169 // The second argument says to hide the tooltip if the docstring
170 170 // is actually empty
171 171 IPython.tooltip.pending(that, true);
172 172 } else if (event.which === key.UPARROW && event.type === 'keydown') {
173 173 // If we are not at the top, let CM handle the up arrow and
174 174 // prevent the global keydown handler from handling it.
175 175 if (!that.at_top()) {
176 176 event.stop();
177 177 return false;
178 178 } else {
179 179 return true;
180 180 }
181 181 } else if (event.which === key.ESC) {
182 182 return IPython.tooltip.remove_and_cancel_tooltip(true);
183 183 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
184 184 // If we are not at the bottom, let CM handle the down arrow and
185 185 // prevent the global keydown handler from handling it.
186 186 if (!that.at_bottom()) {
187 187 event.stop();
188 188 return false;
189 189 } else {
190 190 return true;
191 191 }
192 192 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
193 193 if (editor.somethingSelected()){
194 194 var anchor = editor.getCursor("anchor");
195 195 var head = editor.getCursor("head");
196 196 if( anchor.line != head.line){
197 197 return false;
198 198 }
199 199 }
200 200 IPython.tooltip.request(that);
201 201 event.stop();
202 202 return true;
203 203 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
204 204 // Tab completion.
205 //Do not trim here because of tooltip
206 if (editor.somethingSelected()) { return false; }
205 IPython.tooltip.remove_and_cancel_tooltip();
206 if (editor.somethingSelected()) {
207 return false;
208 }
207 209 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
208 210 if (pre_cursor.trim() === "") {
209 211 // Don't autocomplete if the part of the line before the cursor
210 212 // is empty. In this case, let CodeMirror handle indentation.
211 213 return false;
212 214 } else {
213 215 event.stop();
214 216 this.completer.startCompletion();
215 217 return true;
216 218 }
217 219 } else {
218 220 // keypress/keyup also trigger on TAB press, and we don't want to
219 221 // use those to disable tab completion.
220 222 return false;
221 223 }
222 224 return false;
223 225 };
224 226
225 227
226 228 // Kernel related calls.
227 229
228 230 CodeCell.prototype.set_kernel = function (kernel) {
229 231 this.kernel = kernel;
230 232 };
231 233
232 234 /**
233 235 * Execute current code cell to the kernel
234 236 * @method execute
235 237 */
236 238 CodeCell.prototype.execute = function () {
237 239 this.output_area.clear_output();
238 240 this.set_input_prompt('*');
239 241 this.element.addClass("running");
240 242 if (this.last_msg_id) {
241 243 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
242 244 }
243 245 var callbacks = this.get_callbacks();
244 246
245 247 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
246 248 };
247 249
248 250 /**
249 251 * Construct the default callbacks for
250 252 * @method get_callbacks
251 253 */
252 254 CodeCell.prototype.get_callbacks = function () {
253 255 return {
254 256 shell : {
255 257 reply : $.proxy(this._handle_execute_reply, this),
256 258 payload : {
257 259 set_next_input : $.proxy(this._handle_set_next_input, this),
258 260 page : $.proxy(this._open_with_pager, this)
259 261 }
260 262 },
261 263 iopub : {
262 264 output : $.proxy(this.output_area.handle_output, this.output_area),
263 265 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
264 266 },
265 267 input : $.proxy(this._handle_input_request, this)
266 268 };
267 269 };
268 270
269 271 CodeCell.prototype._open_with_pager = function (payload) {
270 272 $([IPython.events]).trigger('open_with_text.Pager', payload);
271 273 };
272 274
273 275 /**
274 276 * @method _handle_execute_reply
275 277 * @private
276 278 */
277 279 CodeCell.prototype._handle_execute_reply = function (msg) {
278 280 this.set_input_prompt(msg.content.execution_count);
279 281 this.element.removeClass("running");
280 282 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
281 283 };
282 284
283 285 /**
284 286 * @method _handle_set_next_input
285 287 * @private
286 288 */
287 289 CodeCell.prototype._handle_set_next_input = function (payload) {
288 290 var data = {'cell': this, 'text': payload.text};
289 291 $([IPython.events]).trigger('set_next_input.Notebook', data);
290 292 };
291 293
292 294 /**
293 295 * @method _handle_input_request
294 296 * @private
295 297 */
296 298 CodeCell.prototype._handle_input_request = function (msg) {
297 299 this.output_area.append_raw_input(msg);
298 300 };
299 301
300 302
301 303 // Basic cell manipulation.
302 304
303 305 CodeCell.prototype.select = function () {
304 306 IPython.Cell.prototype.select.apply(this);
305 307 this.code_mirror.refresh();
306 308 this.code_mirror.focus();
307 309 this.auto_highlight();
308 310 // We used to need an additional refresh() after the focus, but
309 311 // it appears that this has been fixed in CM. This bug would show
310 312 // up on FF when a newly loaded markdown cell was edited.
311 313 };
312 314
313 315
314 316 CodeCell.prototype.select_all = function () {
315 317 var start = {line: 0, ch: 0};
316 318 var nlines = this.code_mirror.lineCount();
317 319 var last_line = this.code_mirror.getLine(nlines-1);
318 320 var end = {line: nlines-1, ch: last_line.length};
319 321 this.code_mirror.setSelection(start, end);
320 322 };
321 323
322 324
323 325 CodeCell.prototype.collapse = function () {
324 326 this.collapsed = true;
325 327 this.output_area.collapse();
326 328 };
327 329
328 330
329 331 CodeCell.prototype.expand = function () {
330 332 this.collapsed = false;
331 333 this.output_area.expand();
332 334 };
333 335
334 336
335 337 CodeCell.prototype.toggle_output = function () {
336 338 this.collapsed = Boolean(1 - this.collapsed);
337 339 this.output_area.toggle_output();
338 340 };
339 341
340 342
341 343 CodeCell.prototype.toggle_output_scroll = function () {
342 344 this.output_area.toggle_scroll();
343 345 };
344 346
345 347
346 348 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
347 349 var ns = prompt_value || "&nbsp;";
348 350 return 'In&nbsp;[' + ns + ']:';
349 351 };
350 352
351 353 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
352 354 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
353 355 for(var i=1; i < lines_number; i++) {
354 356 html.push(['...:']);
355 357 }
356 358 return html.join('<br/>');
357 359 };
358 360
359 361 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
360 362
361 363
362 364 CodeCell.prototype.set_input_prompt = function (number) {
363 365 var nline = 1;
364 366 if (this.code_mirror !== undefined) {
365 367 nline = this.code_mirror.lineCount();
366 368 }
367 369 this.input_prompt_number = number;
368 370 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
369 371 this.element.find('div.input_prompt').html(prompt_html);
370 372 };
371 373
372 374
373 375 CodeCell.prototype.clear_input = function () {
374 376 this.code_mirror.setValue('');
375 377 };
376 378
377 379
378 380 CodeCell.prototype.get_text = function () {
379 381 return this.code_mirror.getValue();
380 382 };
381 383
382 384
383 385 CodeCell.prototype.set_text = function (code) {
384 386 return this.code_mirror.setValue(code);
385 387 };
386 388
387 389
388 390 CodeCell.prototype.at_top = function () {
389 391 var cursor = this.code_mirror.getCursor();
390 392 if (cursor.line === 0 && cursor.ch === 0) {
391 393 return true;
392 394 } else {
393 395 return false;
394 396 }
395 397 };
396 398
397 399
398 400 CodeCell.prototype.at_bottom = function () {
399 401 var cursor = this.code_mirror.getCursor();
400 402 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
401 403 return true;
402 404 } else {
403 405 return false;
404 406 }
405 407 };
406 408
407 409
408 410 CodeCell.prototype.clear_output = function (wait) {
409 411 this.output_area.clear_output(wait);
410 412 };
411 413
412 414
413 415 // JSON serialization
414 416
415 417 CodeCell.prototype.fromJSON = function (data) {
416 418 IPython.Cell.prototype.fromJSON.apply(this, arguments);
417 419 if (data.cell_type === 'code') {
418 420 if (data.input !== undefined) {
419 421 this.set_text(data.input);
420 422 // make this value the starting point, so that we can only undo
421 423 // to this state, instead of a blank cell
422 424 this.code_mirror.clearHistory();
423 425 this.auto_highlight();
424 426 }
425 427 if (data.prompt_number !== undefined) {
426 428 this.set_input_prompt(data.prompt_number);
427 429 } else {
428 430 this.set_input_prompt();
429 431 }
430 432 this.output_area.fromJSON(data.outputs);
431 433 if (data.collapsed !== undefined) {
432 434 if (data.collapsed) {
433 435 this.collapse();
434 436 } else {
435 437 this.expand();
436 438 }
437 439 }
438 440 }
439 441 };
440 442
441 443
442 444 CodeCell.prototype.toJSON = function () {
443 445 var data = IPython.Cell.prototype.toJSON.apply(this);
444 446 data.input = this.get_text();
445 447 data.cell_type = 'code';
446 if (this.input_prompt_number) {
448 // is finite protect against undefined and '*' value
449 if (isFinite(this.input_prompt_number)) {
447 450 data.prompt_number = this.input_prompt_number;
448 451 }
449 452 var outputs = this.output_area.toJSON();
450 453 data.outputs = outputs;
451 454 data.language = 'python';
452 455 data.collapsed = this.collapsed;
453 456 return data;
454 457 };
455 458
456 459
457 460 IPython.CodeCell = CodeCell;
458 461
459 462 return IPython;
460 463 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now