##// END OF EJS Templates
ESC should be handled by CM if tooltip is not on
Takeshi Kanmae -
Show More
@@ -1,442 +1,446
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
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 // The second argument says to hide the tooltip if the docstring
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 IPython.tooltip.remove_and_cancel_tooltip(true);
183 return true;
182 if (!IPython.tooltip._hidden) {
183 IPython.tooltip.remove_and_cancel_tooltip(true);
184 return true;
185 } else {
186 return false;
187 }
184 188 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
185 189 // If we are not at the bottom, let CM handle the down arrow and
186 190 // prevent the global keydown handler from handling it.
187 191 if (!that.at_bottom()) {
188 192 event.stop();
189 193 return false;
190 194 } else {
191 195 return true;
192 196 };
193 197 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
194 198 if (editor.somethingSelected()){
195 199 var anchor = editor.getCursor("anchor");
196 200 var head = editor.getCursor("head");
197 201 if( anchor.line != head.line){
198 202 return false;
199 203 }
200 204 }
201 205 IPython.tooltip.request(that);
202 206 event.stop();
203 207 return true;
204 208 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
205 209 // Tab completion.
206 210 //Do not trim here because of tooltip
207 211 if (editor.somethingSelected()){return false}
208 212 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
209 213 if (pre_cursor.trim() === "") {
210 214 // Don't autocomplete if the part of the line before the cursor
211 215 // is empty. In this case, let CodeMirror handle indentation.
212 216 return false;
213 217 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
214 218 IPython.tooltip.request(that);
215 219 // Prevent the event from bubbling up.
216 220 event.stop();
217 221 // Prevent CodeMirror from handling the tab.
218 222 return true;
219 223 } else {
220 224 event.stop();
221 225 this.completer.startCompletion();
222 226 return true;
223 227 };
224 228 } else {
225 229 // keypress/keyup also trigger on TAB press, and we don't want to
226 230 // use those to disable tab completion.
227 231 return false;
228 232 };
229 233 return false;
230 234 };
231 235
232 236
233 237 // Kernel related calls.
234 238
235 239 CodeCell.prototype.set_kernel = function (kernel) {
236 240 this.kernel = kernel;
237 241 }
238 242
239 243 /**
240 244 * Execute current code cell to the kernel
241 245 * @method execute
242 246 */
243 247 CodeCell.prototype.execute = function () {
244 248 this.output_area.clear_output(true, true, true);
245 249 this.set_input_prompt('*');
246 250 this.element.addClass("running");
247 251 var callbacks = {
248 252 'execute_reply': $.proxy(this._handle_execute_reply, this),
249 253 'output': $.proxy(this.output_area.handle_output, this.output_area),
250 254 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
251 255 'set_next_input': $.proxy(this._handle_set_next_input, this),
252 256 'input_request': $.proxy(this._handle_input_request, this)
253 257 };
254 258 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
255 259 };
256 260
257 261 /**
258 262 * @method _handle_execute_reply
259 263 * @private
260 264 */
261 265 CodeCell.prototype._handle_execute_reply = function (content) {
262 266 this.set_input_prompt(content.execution_count);
263 267 this.element.removeClass("running");
264 268 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
265 269 }
266 270
267 271 /**
268 272 * @method _handle_set_next_input
269 273 * @private
270 274 */
271 275 CodeCell.prototype._handle_set_next_input = function (text) {
272 276 var data = {'cell': this, 'text': text}
273 277 $([IPython.events]).trigger('set_next_input.Notebook', data);
274 278 }
275
279
276 280 /**
277 281 * @method _handle_input_request
278 282 * @private
279 283 */
280 284 CodeCell.prototype._handle_input_request = function (content) {
281 285 this.output_area.append_raw_input(content);
282 286 }
283 287
284 288
285 289 // Basic cell manipulation.
286 290
287 291 CodeCell.prototype.select = function () {
288 292 IPython.Cell.prototype.select.apply(this);
289 293 this.code_mirror.refresh();
290 294 this.code_mirror.focus();
291 295 this.auto_highlight();
292 296 // We used to need an additional refresh() after the focus, but
293 297 // it appears that this has been fixed in CM. This bug would show
294 298 // up on FF when a newly loaded markdown cell was edited.
295 299 };
296 300
297 301
298 302 CodeCell.prototype.select_all = function () {
299 303 var start = {line: 0, ch: 0};
300 304 var nlines = this.code_mirror.lineCount();
301 305 var last_line = this.code_mirror.getLine(nlines-1);
302 306 var end = {line: nlines-1, ch: last_line.length};
303 307 this.code_mirror.setSelection(start, end);
304 308 };
305 309
306 310
307 311 CodeCell.prototype.collapse = function () {
308 312 this.collapsed = true;
309 313 this.output_area.collapse();
310 314 };
311 315
312 316
313 317 CodeCell.prototype.expand = function () {
314 318 this.collapsed = false;
315 319 this.output_area.expand();
316 320 };
317 321
318 322
319 323 CodeCell.prototype.toggle_output = function () {
320 324 this.collapsed = Boolean(1 - this.collapsed);
321 325 this.output_area.toggle_output();
322 326 };
323 327
324 328
325 329 CodeCell.prototype.toggle_output_scroll = function () {
326 330 this.output_area.toggle_scroll();
327 331 };
328 332
329 333
330 334 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
331 335 var ns = prompt_value || "&nbsp;";
332 336 return 'In&nbsp;[' + ns + ']:'
333 337 };
334 338
335 339 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
336 340 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
337 341 for(var i=1; i < lines_number; i++){html.push(['...:'])};
338 342 return html.join('</br>')
339 343 };
340 344
341 345 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
342 346
343 347
344 348 CodeCell.prototype.set_input_prompt = function (number) {
345 349 var nline = 1
346 350 if( this.code_mirror != undefined) {
347 351 nline = this.code_mirror.lineCount();
348 352 }
349 353 this.input_prompt_number = number;
350 354 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
351 355 this.element.find('div.input_prompt').html(prompt_html);
352 356 };
353 357
354 358
355 359 CodeCell.prototype.clear_input = function () {
356 360 this.code_mirror.setValue('');
357 361 };
358 362
359 363
360 364 CodeCell.prototype.get_text = function () {
361 365 return this.code_mirror.getValue();
362 366 };
363 367
364 368
365 369 CodeCell.prototype.set_text = function (code) {
366 370 return this.code_mirror.setValue(code);
367 371 };
368 372
369 373
370 374 CodeCell.prototype.at_top = function () {
371 375 var cursor = this.code_mirror.getCursor();
372 376 if (cursor.line === 0 && cursor.ch === 0) {
373 377 return true;
374 378 } else {
375 379 return false;
376 380 }
377 381 };
378 382
379 383
380 384 CodeCell.prototype.at_bottom = function () {
381 385 var cursor = this.code_mirror.getCursor();
382 386 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
383 387 return true;
384 388 } else {
385 389 return false;
386 390 }
387 391 };
388 392
389 393
390 394 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
391 395 this.output_area.clear_output(stdout, stderr, other);
392 396 };
393 397
394 398
395 399 // JSON serialization
396 400
397 401 CodeCell.prototype.fromJSON = function (data) {
398 402 IPython.Cell.prototype.fromJSON.apply(this, arguments);
399 403 if (data.cell_type === 'code') {
400 404 if (data.input !== undefined) {
401 405 this.set_text(data.input);
402 406 // make this value the starting point, so that we can only undo
403 407 // to this state, instead of a blank cell
404 408 this.code_mirror.clearHistory();
405 409 this.auto_highlight();
406 410 }
407 411 if (data.prompt_number !== undefined) {
408 412 this.set_input_prompt(data.prompt_number);
409 413 } else {
410 414 this.set_input_prompt();
411 415 };
412 416 this.output_area.fromJSON(data.outputs);
413 417 if (data.collapsed !== undefined) {
414 418 if (data.collapsed) {
415 419 this.collapse();
416 420 } else {
417 421 this.expand();
418 422 };
419 423 };
420 424 };
421 425 };
422 426
423 427
424 428 CodeCell.prototype.toJSON = function () {
425 429 var data = IPython.Cell.prototype.toJSON.apply(this);
426 430 data.input = this.get_text();
427 431 data.cell_type = 'code';
428 432 if (this.input_prompt_number) {
429 433 data.prompt_number = this.input_prompt_number;
430 434 };
431 435 var outputs = this.output_area.toJSON();
432 436 data.outputs = outputs;
433 437 data.language = 'python';
434 438 data.collapsed = this.collapsed;
435 439 return data;
436 440 };
437 441
438 442
439 443 IPython.CodeCell = CodeCell;
440 444
441 445 return IPython;
442 446 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now