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