##// END OF EJS Templates
Merge pull request #2312 from adamgd/master...
Brian E. Granger -
r8828:c8270060 merge
parent child Browse files
Show More
@@ -1,368 +1,369
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 var IPython = (function (IPython) {
19 19 "use strict";
20 20
21 21 var utils = IPython.utils;
22 22 var key = IPython.utils.keycodes;
23 23 CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js";
24 24
25 25 /**
26 26 * A Cell conceived to write code.
27 27 *
28 28 * The kernel doesn't have to be set at creation time, in that case
29 29 * it will be null and set_kernel has to be called later.
30 30 * @class CodeCell
31 31 * @extends IPython.Cell
32 32 *
33 33 * @constructor
34 34 * @param {Object|null} kernel
35 35 */
36 36 var CodeCell = function (kernel) {
37 37 this.kernel = kernel || null;
38 38 this.code_mirror = null;
39 39 this.input_prompt_number = null;
40 40 this.tooltip_on_tab = true;
41 41 this.collapsed = false;
42 42 this.default_mode = 'python';
43 43 IPython.Cell.apply(this, arguments);
44 44
45 45 var that = this;
46 46 this.element.focusout(
47 47 function() { that.auto_highlight(); }
48 48 );
49 49 };
50 50
51 51
52 52 CodeCell.prototype = new IPython.Cell();
53 53
54 54 /**
55 55 * @method auto_highlight
56 56 */
57 57 CodeCell.prototype.auto_highlight = function () {
58 58 this._auto_highlight(IPython.config.cell_magic_highlight)
59 59 };
60 60
61 61 /** @method create_element */
62 62 CodeCell.prototype.create_element = function () {
63 63 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
64 64 cell.attr('tabindex','2');
65 65 var input = $('<div></div>').addClass('input hbox');
66 66 input.append($('<div/>').addClass('prompt input_prompt'));
67 67 var input_area = $('<div/>').addClass('input_area box-flex1');
68 68 this.code_mirror = CodeMirror(input_area.get(0), {
69 69 indentUnit : 4,
70 70 mode: 'python',
71 71 theme: 'ipython',
72 72 readOnly: this.read_only,
73 73 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
74 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
74 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this),
75 matchBrackets: true
75 76 });
76 77 input.append(input_area);
77 78 var output = $('<div></div>');
78 79 cell.append(input).append(output);
79 80 this.element = cell;
80 81 this.output_area = new IPython.OutputArea(output, true);
81 82
82 83 // construct a completer only if class exist
83 84 // otherwise no print view
84 85 if (IPython.Completer !== undefined)
85 86 {
86 87 this.completer = new IPython.Completer(this);
87 88 }
88 89 };
89 90
90 91 /**
91 92 * This method gets called in CodeMirror's onKeyDown/onKeyPress
92 93 * handlers and is used to provide custom key handling. Its return
93 94 * value is used to determine if CodeMirror should ignore the event:
94 95 * true = ignore, false = don't ignore.
95 96 * @method handle_codemirror_keyevent
96 97 */
97 98 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
98 99
99 100 if (this.read_only){
100 101 return false;
101 102 }
102 103
103 104 var that = this;
104 105 // whatever key is pressed, first, cancel the tooltip request before
105 106 // they are sent, and remove tooltip if any, except for tab again
106 107 if (event.type === 'keydown' && event.which != key.TAB ) {
107 108 IPython.tooltip.remove_and_cancel_tooltip();
108 109 };
109 110
110 111 var cur = editor.getCursor();
111 112 if (event.keyCode === key.ENTER){
112 113 this.auto_highlight();
113 114 }
114 115
115 116 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
116 117 // Always ignore shift-enter in CodeMirror as we handle it.
117 118 return true;
118 119 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
119 120 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
120 121 // browser and keyboard layout !
121 122 // Pressing '(' , request tooltip, don't forget to reappend it
122 123 IPython.tooltip.pending(that);
123 124 } else if (event.which === key.UPARROW && event.type === 'keydown') {
124 125 // If we are not at the top, let CM handle the up arrow and
125 126 // prevent the global keydown handler from handling it.
126 127 if (!that.at_top()) {
127 128 event.stop();
128 129 return false;
129 130 } else {
130 131 return true;
131 132 };
132 133 } else if (event.which === key.ESC) {
133 134 IPython.tooltip.remove_and_cancel_tooltip(true);
134 135 return true;
135 136 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
136 137 // If we are not at the bottom, let CM handle the down arrow and
137 138 // prevent the global keydown handler from handling it.
138 139 if (!that.at_bottom()) {
139 140 event.stop();
140 141 return false;
141 142 } else {
142 143 return true;
143 144 };
144 145 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
145 146 // Tab completion.
146 147 //Do not trim here because of tooltip
147 148 if (editor.somethingSelected()){return false}
148 149 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
149 150 if (pre_cursor.trim() === "") {
150 151 // Don't autocomplete if the part of the line before the cursor
151 152 // is empty. In this case, let CodeMirror handle indentation.
152 153 return false;
153 154 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
154 155 IPython.tooltip.request(that);
155 156 // Prevent the event from bubbling up.
156 157 event.stop();
157 158 // Prevent CodeMirror from handling the tab.
158 159 return true;
159 160 } else {
160 161 event.stop();
161 162 this.completer.startCompletion();
162 163 return true;
163 164 };
164 165 } else {
165 166 // keypress/keyup also trigger on TAB press, and we don't want to
166 167 // use those to disable tab completion.
167 168 return false;
168 169 };
169 170 return false;
170 171 };
171 172
172 173
173 174 // Kernel related calls.
174 175
175 176 CodeCell.prototype.set_kernel = function (kernel) {
176 177 this.kernel = kernel;
177 178 }
178 179
179 180 /**
180 181 * Execute current code cell to the kernel
181 182 * @method execute
182 183 */
183 184 CodeCell.prototype.execute = function () {
184 185 this.output_area.clear_output(true, true, true);
185 186 this.set_input_prompt('*');
186 187 this.element.addClass("running");
187 188 var callbacks = {
188 189 'execute_reply': $.proxy(this._handle_execute_reply, this),
189 190 'output': $.proxy(this.output_area.handle_output, this.output_area),
190 191 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
191 192 'set_next_input': $.proxy(this._handle_set_next_input, this)
192 193 };
193 194 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
194 195 };
195 196
196 197 /**
197 198 * @method _handle_execute_reply
198 199 * @private
199 200 */
200 201 CodeCell.prototype._handle_execute_reply = function (content) {
201 202 this.set_input_prompt(content.execution_count);
202 203 this.element.removeClass("running");
203 204 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
204 205 }
205 206
206 207 CodeCell.prototype._handle_set_next_input = function (text) {
207 208 var data = {'cell': this, 'text': text}
208 209 $([IPython.events]).trigger('set_next_input.Notebook', data);
209 210 }
210 211
211 212 // Basic cell manipulation.
212 213
213 214 CodeCell.prototype.select = function () {
214 215 IPython.Cell.prototype.select.apply(this);
215 216 this.code_mirror.refresh();
216 217 this.code_mirror.focus();
217 218 this.auto_highlight();
218 219 // We used to need an additional refresh() after the focus, but
219 220 // it appears that this has been fixed in CM. This bug would show
220 221 // up on FF when a newly loaded markdown cell was edited.
221 222 };
222 223
223 224
224 225 CodeCell.prototype.select_all = function () {
225 226 var start = {line: 0, ch: 0};
226 227 var nlines = this.code_mirror.lineCount();
227 228 var last_line = this.code_mirror.getLine(nlines-1);
228 229 var end = {line: nlines-1, ch: last_line.length};
229 230 this.code_mirror.setSelection(start, end);
230 231 };
231 232
232 233
233 234 CodeCell.prototype.collapse = function () {
234 235 this.collapsed = true;
235 236 this.output_area.collapse();
236 237 };
237 238
238 239
239 240 CodeCell.prototype.expand = function () {
240 241 this.collapsed = false;
241 242 this.output_area.expand();
242 243 };
243 244
244 245
245 246 CodeCell.prototype.toggle_output = function () {
246 247 this.collapsed = Boolean(1 - this.collapsed);
247 248 this.output_area.toggle_output();
248 249 };
249 250
250 251
251 252 CodeCell.prototype.toggle_output_scroll = function () {
252 253 this.output_area.toggle_scroll();
253 254 };
254 255
255 256
256 257 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
257 258 var ns = prompt_value || "&nbsp;";
258 259 return 'In&nbsp;[' + ns + ']:'
259 260 };
260 261
261 262 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
262 263 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
263 264 for(var i=1; i < lines_number; i++){html.push(['...:'])};
264 265 return html.join('</br>')
265 266 };
266 267
267 268 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
268 269
269 270
270 271 CodeCell.prototype.set_input_prompt = function (number) {
271 272 var nline = 1
272 273 if( this.code_mirror != undefined) {
273 274 nline = this.code_mirror.lineCount();
274 275 }
275 276 this.input_prompt_number = number;
276 277 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
277 278 this.element.find('div.input_prompt').html(prompt_html);
278 279 };
279 280
280 281
281 282 CodeCell.prototype.clear_input = function () {
282 283 this.code_mirror.setValue('');
283 284 };
284 285
285 286
286 287 CodeCell.prototype.get_text = function () {
287 288 return this.code_mirror.getValue();
288 289 };
289 290
290 291
291 292 CodeCell.prototype.set_text = function (code) {
292 293 return this.code_mirror.setValue(code);
293 294 };
294 295
295 296
296 297 CodeCell.prototype.at_top = function () {
297 298 var cursor = this.code_mirror.getCursor();
298 299 if (cursor.line === 0 && cursor.ch === 0) {
299 300 return true;
300 301 } else {
301 302 return false;
302 303 }
303 304 };
304 305
305 306
306 307 CodeCell.prototype.at_bottom = function () {
307 308 var cursor = this.code_mirror.getCursor();
308 309 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
309 310 return true;
310 311 } else {
311 312 return false;
312 313 }
313 314 };
314 315
315 316
316 317 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
317 318 this.output_area.clear_output(stdout, stderr, other);
318 319 };
319 320
320 321
321 322 // JSON serialization
322 323
323 324 CodeCell.prototype.fromJSON = function (data) {
324 325 IPython.Cell.prototype.fromJSON.apply(this, arguments);
325 326 if (data.cell_type === 'code') {
326 327 if (data.input !== undefined) {
327 328 this.set_text(data.input);
328 329 // make this value the starting point, so that we can only undo
329 330 // to this state, instead of a blank cell
330 331 this.code_mirror.clearHistory();
331 332 this.auto_highlight();
332 333 }
333 334 if (data.prompt_number !== undefined) {
334 335 this.set_input_prompt(data.prompt_number);
335 336 } else {
336 337 this.set_input_prompt();
337 338 };
338 339 this.output_area.fromJSON(data.outputs);
339 340 if (data.collapsed !== undefined) {
340 341 if (data.collapsed) {
341 342 this.collapse();
342 343 } else {
343 344 this.expand();
344 345 };
345 346 };
346 347 };
347 348 };
348 349
349 350
350 351 CodeCell.prototype.toJSON = function () {
351 352 var data = IPython.Cell.prototype.toJSON.apply(this);
352 353 data.input = this.get_text();
353 354 data.cell_type = 'code';
354 355 if (this.input_prompt_number) {
355 356 data.prompt_number = this.input_prompt_number;
356 357 };
357 358 var outputs = this.output_area.toJSON();
358 359 data.outputs = outputs;
359 360 data.language = 'python';
360 361 data.collapsed = this.collapsed;
361 362 return data;
362 363 };
363 364
364 365
365 366 IPython.CodeCell = CodeCell;
366 367
367 368 return IPython;
368 369 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now