##// END OF EJS Templates
Code comments and minor fixes.
Brian E. Granger -
Show More
@@ -1,491 +1,494 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Cell
9 // Cell
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * An extendable module that provide base functionnality to create cell for notebook.
12 * An extendable module that provide base functionnality to create cell for notebook.
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule Cell
15 * @submodule Cell
16 */
16 */
17
17
18 var IPython = (function (IPython) {
18 var IPython = (function (IPython) {
19 "use strict";
19 "use strict";
20
20
21 var utils = IPython.utils;
21 var utils = IPython.utils;
22
22
23 /**
23 /**
24 * The Base `Cell` class from which to inherit
24 * The Base `Cell` class from which to inherit
25 * @class Cell
25 * @class Cell
26 **/
26 **/
27
27
28 /*
28 /*
29 * @constructor
29 * @constructor
30 *
30 *
31 * @param {object|undefined} [options]
31 * @param {object|undefined} [options]
32 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
32 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
33 */
33 */
34 var Cell = function (options) {
34 var Cell = function (options) {
35
35
36 options = this.mergeopt(Cell, options);
36 options = this.mergeopt(Cell, options);
37 // superclass default overwrite our default
37 // superclass default overwrite our default
38
38
39 this.placeholder = options.placeholder || '';
39 this.placeholder = options.placeholder || '';
40 this.read_only = options.cm_config.readOnly;
40 this.read_only = options.cm_config.readOnly;
41 this.selected = false;
41 this.selected = false;
42 this.rendered = false;
42 this.rendered = false;
43 this.mode = 'command';
43 this.mode = 'command';
44 this.metadata = {};
44 this.metadata = {};
45 // load this from metadata later ?
45 // load this from metadata later ?
46 this.user_highlight = 'auto';
46 this.user_highlight = 'auto';
47 this.cm_config = options.cm_config;
47 this.cm_config = options.cm_config;
48 this.cell_id = utils.uuid();
48 this.cell_id = utils.uuid();
49 this._options = options;
49 this._options = options;
50
50
51 // For JS VM engines optimisation, attributes should be all set (even
51 // For JS VM engines optimisation, attributes should be all set (even
52 // to null) in the constructor, and if possible, if different subclass
52 // to null) in the constructor, and if possible, if different subclass
53 // have new attributes with same name, they should be created in the
53 // have new attributes with same name, they should be created in the
54 // same order. Easiest is to create and set to null in parent class.
54 // same order. Easiest is to create and set to null in parent class.
55
55
56 this.element = null;
56 this.element = null;
57 this.cell_type = this.cell_type || null;
57 this.cell_type = this.cell_type || null;
58 this.code_mirror = null;
58 this.code_mirror = null;
59
59
60
60
61 this.create_element();
61 this.create_element();
62 if (this.element !== null) {
62 if (this.element !== null) {
63 this.element.data("cell", this);
63 this.element.data("cell", this);
64 this.bind_events();
64 this.bind_events();
65 }
65 }
66 };
66 };
67
67
68 Cell.options_default = {
68 Cell.options_default = {
69 cm_config : {
69 cm_config : {
70 indentUnit : 4,
70 indentUnit : 4,
71 readOnly: false,
71 readOnly: false,
72 theme: "default"
72 theme: "default"
73 }
73 }
74 };
74 };
75
75
76 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
76 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
77 // by disabling drag/drop altogether on Safari
77 // by disabling drag/drop altogether on Safari
78 // https://github.com/marijnh/CodeMirror/issues/332
78 // https://github.com/marijnh/CodeMirror/issues/332
79
79
80 if (utils.browser[0] == "Safari") {
80 if (utils.browser[0] == "Safari") {
81 Cell.options_default.cm_config.dragDrop = false;
81 Cell.options_default.cm_config.dragDrop = false;
82 }
82 }
83
83
84 Cell.prototype.mergeopt = function(_class, options, overwrite){
84 Cell.prototype.mergeopt = function(_class, options, overwrite){
85 options = options || {};
85 options = options || {};
86 overwrite = overwrite || {};
86 overwrite = overwrite || {};
87 return $.extend(true, {}, _class.options_default, options, overwrite)
87 return $.extend(true, {}, _class.options_default, options, overwrite)
88
88
89 }
89 }
90
90
91
91
92
92
93 /**
93 /**
94 * Empty. Subclasses must implement create_element.
94 * Empty. Subclasses must implement create_element.
95 * This should contain all the code to create the DOM element in notebook
95 * This should contain all the code to create the DOM element in notebook
96 * and will be called by Base Class constructor.
96 * and will be called by Base Class constructor.
97 * @method create_element
97 * @method create_element
98 */
98 */
99 Cell.prototype.create_element = function () {
99 Cell.prototype.create_element = function () {
100 };
100 };
101
101
102
102
103 /**
103 /**
104 * Subclasses can implement override bind_events.
104 * Subclasses can implement override bind_events.
105 * Be carefull to call the parent method when overwriting as it fires event.
105 * Be carefull to call the parent method when overwriting as it fires event.
106 * this will be triggerd after create_element in constructor.
106 * this will be triggerd after create_element in constructor.
107 * @method bind_events
107 * @method bind_events
108 */
108 */
109 Cell.prototype.bind_events = function () {
109 Cell.prototype.bind_events = function () {
110 var that = this;
110 var that = this;
111 // We trigger events so that Cell doesn't have to depend on Notebook.
111 // We trigger events so that Cell doesn't have to depend on Notebook.
112 that.element.click(function (event) {
112 that.element.click(function (event) {
113 if (that.selected === false) {
113 if (that.selected === false) {
114 $([IPython.events]).trigger('select.Cell', {'cell':that});
114 $([IPython.events]).trigger('select.Cell', {'cell':that});
115 };
115 };
116 });
116 });
117 that.element.focusin(function (event) {
117 that.element.focusin(function (event) {
118 if (that.selected === false) {
118 if (that.selected === false) {
119 $([IPython.events]).trigger('select.Cell', {'cell':that});
119 $([IPython.events]).trigger('select.Cell', {'cell':that});
120 };
120 };
121 });
121 });
122 that.element.focusout(function (event) {
122 that.element.focusout(function (event) {
123 var is_or_has = function (a, b) {
123 var is_or_has = function (a, b) {
124 // Is b a child of a or a itself?
124 // Is b a child of a or a itself?
125 return a.has(b).length !==0 || a.is(b);
125 return a.has(b).length !==0 || a.is(b);
126 }
126 }
127 if (that.mode === 'edit') {
127 if (that.mode === 'edit') {
128 // Most of the time, when a cell is in edit mode and focusout
129 // fires, it means we should enter command mode. But there are cases
130 // when we should not enter command mode.
128 setTimeout(function () {
131 setTimeout(function () {
129 var trigger = true;
132 var trigger = true;
130 var target = $(document.activeElement);
133 var target = $(document.activeElement);
131 var completer = that.element.find($('div.completions'));
134 var completer = $('div.completions');
132 var tooltip = $('div#tooltip')
135 var tooltip = $('div#tooltip');
133 if (target.length > 0) {
136 if (target.length > 0) {
134 // If the focused element (target) is inside the cell
137 // If the focused element (target) is inside the cell
135 // (that.element) don't enter command mode.
138 // (that.element) don't enter command mode.
136 if (is_or_has(that.element, target)) {
139 if (is_or_has(that.element, target)) {
137 trigger = false;
140 trigger = false;
138 // The focused element is outside the cell
141 // The focused element is outside the cell
139 } else {
142 } else {
140 // If the focused element is the tooltip or completer
143 // If the focused element is the tooltip or completer
141 // don't enter command mode, otherwise do.
144 // don't enter command mode, otherwise do.
142 trigger = true;
145 trigger = true;
143 if (tooltip.length > 0 && is_or_has(tooltip, target)) {
146 if (tooltip.length > 0 && is_or_has(tooltip, target)) {
144 trigger = false;
147 trigger = false;
145 } else if (completer.length > 0 && is_or_has(completer, target)) {
148 } else if (completer.length > 0 && is_or_has(completer, target)) {
146 trigger = false;
149 trigger = false;
147 }
150 }
148 }
151 }
149 }
152 }
150 if (trigger) {
153 if (trigger) {
151 $([IPython.events]).trigger('command_mode.Cell', {'cell':that});
154 $([IPython.events]).trigger('command_mode.Cell', {'cell':that});
152 }
155 }
153 }, 1);
156 }, 1);
154 };
157 };
155 });
158 });
156 if (this.code_mirror) {
159 if (this.code_mirror) {
157 this.code_mirror.on("change", function(cm, change) {
160 this.code_mirror.on("change", function(cm, change) {
158 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
161 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
159 });
162 });
160 };
163 };
161 if (this.code_mirror) {
164 if (this.code_mirror) {
162 this.code_mirror.on('focus', function(cm, change) {
165 this.code_mirror.on('focus', function(cm, change) {
163 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
166 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
164 });
167 });
165 };
168 };
166 };
169 };
167
170
168 /**
171 /**
169 * Triger typsetting of math by mathjax on current cell element
172 * Triger typsetting of math by mathjax on current cell element
170 * @method typeset
173 * @method typeset
171 */
174 */
172 Cell.prototype.typeset = function () {
175 Cell.prototype.typeset = function () {
173 if (window.MathJax) {
176 if (window.MathJax) {
174 var cell_math = this.element.get(0);
177 var cell_math = this.element.get(0);
175 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
178 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
176 };
179 };
177 };
180 };
178
181
179 /**
182 /**
180 * handle cell level logic when a cell is selected
183 * handle cell level logic when a cell is selected
181 * @method select
184 * @method select
182 * @return is the action being taken
185 * @return is the action being taken
183 */
186 */
184 Cell.prototype.select = function () {
187 Cell.prototype.select = function () {
185 if (!this.selected) {
188 if (!this.selected) {
186 this.element.addClass('selected');
189 this.element.addClass('selected');
187 this.element.removeClass('unselected');
190 this.element.removeClass('unselected');
188 this.selected = true;
191 this.selected = true;
189 return true;
192 return true;
190 } else {
193 } else {
191 return false;
194 return false;
192 };
195 };
193 };
196 };
194
197
195 /**
198 /**
196 * handle cell level logic when a cell is unselected
199 * handle cell level logic when a cell is unselected
197 * @method unselect
200 * @method unselect
198 * @return is the action being taken
201 * @return is the action being taken
199 */
202 */
200 Cell.prototype.unselect = function () {
203 Cell.prototype.unselect = function () {
201 if (this.selected) {
204 if (this.selected) {
202 this.element.addClass('unselected');
205 this.element.addClass('unselected');
203 this.element.removeClass('selected');
206 this.element.removeClass('selected');
204 this.selected = false;
207 this.selected = false;
205 return true;
208 return true;
206 } else {
209 } else {
207 return false;
210 return false;
208 };
211 };
209 };
212 };
210
213
211 /**
214 /**
212 * handle cell level logic when a cell is rendered
215 * handle cell level logic when a cell is rendered
213 * @method render
216 * @method render
214 * @return is the action being taken
217 * @return is the action being taken
215 */
218 */
216 Cell.prototype.render = function () {
219 Cell.prototype.render = function () {
217 if (!this.rendered) {
220 if (!this.rendered) {
218 this.element.addClass('rendered');
221 this.element.addClass('rendered');
219 this.element.removeClass('unrendered');
222 this.element.removeClass('unrendered');
220 this.rendered = true;
223 this.rendered = true;
221 return true;
224 return true;
222 } else {
225 } else {
223 return false;
226 return false;
224 };
227 };
225 };
228 };
226
229
227 /**
230 /**
228 * handle cell level logic when a cell is unrendered
231 * handle cell level logic when a cell is unrendered
229 * @method unrender
232 * @method unrender
230 * @return is the action being taken
233 * @return is the action being taken
231 */
234 */
232 Cell.prototype.unrender = function () {
235 Cell.prototype.unrender = function () {
233 if (this.rendered) {
236 if (this.rendered) {
234 this.element.addClass('unrendered');
237 this.element.addClass('unrendered');
235 this.element.removeClass('rendered');
238 this.element.removeClass('rendered');
236 this.rendered = false;
239 this.rendered = false;
237 return true;
240 return true;
238 } else {
241 } else {
239 return false;
242 return false;
240 };
243 };
241 };
244 };
242
245
243 /**
246 /**
244 * enter the command mode for the cell
247 * enter the command mode for the cell
245 * @method command_mode
248 * @method command_mode
246 * @return is the action being taken
249 * @return is the action being taken
247 */
250 */
248 Cell.prototype.command_mode = function () {
251 Cell.prototype.command_mode = function () {
249 if (this.mode !== 'command') {
252 if (this.mode !== 'command') {
250 this.element.addClass('command_mode');
253 this.element.addClass('command_mode');
251 this.element.removeClass('edit_mode');
254 this.element.removeClass('edit_mode');
252 this.mode = 'command';
255 this.mode = 'command';
253 return true;
256 return true;
254 } else {
257 } else {
255 return false;
258 return false;
256 };
259 };
257 };
260 };
258
261
259 /**
262 /**
260 * enter the edit mode for the cell
263 * enter the edit mode for the cell
261 * @method command_mode
264 * @method command_mode
262 * @return is the action being taken
265 * @return is the action being taken
263 */
266 */
264 Cell.prototype.edit_mode = function () {
267 Cell.prototype.edit_mode = function () {
265 if (this.mode !== 'edit') {
268 if (this.mode !== 'edit') {
266 this.element.addClass('edit_mode');
269 this.element.addClass('edit_mode');
267 this.element.removeClass('command_mode');
270 this.element.removeClass('command_mode');
268 this.mode = 'edit';
271 this.mode = 'edit';
269 return true;
272 return true;
270 } else {
273 } else {
271 return false;
274 return false;
272 };
275 };
273 }
276 }
274
277
275 /**
278 /**
276 * Focus the cell in the DOM sense
279 * Focus the cell in the DOM sense
277 * @method focus_cell
280 * @method focus_cell
278 */
281 */
279 Cell.prototype.focus_cell = function () {
282 Cell.prototype.focus_cell = function () {
280 this.element.focus();
283 this.element.focus();
281 }
284 }
282
285
283 /**
286 /**
284 * Focus the editor area so a user can type
287 * Focus the editor area so a user can type
285 * @method focus_editor
288 * @method focus_editor
286 */
289 */
287 Cell.prototype.focus_editor = function () {
290 Cell.prototype.focus_editor = function () {
288 this.refresh();
291 this.refresh();
289 this.code_mirror.focus();
292 this.code_mirror.focus();
290 }
293 }
291
294
292 /**
295 /**
293 * Refresh codemirror instance
296 * Refresh codemirror instance
294 * @method refresh
297 * @method refresh
295 */
298 */
296 Cell.prototype.refresh = function () {
299 Cell.prototype.refresh = function () {
297 this.code_mirror.refresh();
300 this.code_mirror.refresh();
298 };
301 };
299
302
300 /**
303 /**
301 * should be overritten by subclass
304 * should be overritten by subclass
302 * @method get_text
305 * @method get_text
303 */
306 */
304 Cell.prototype.get_text = function () {
307 Cell.prototype.get_text = function () {
305 };
308 };
306
309
307 /**
310 /**
308 * should be overritten by subclass
311 * should be overritten by subclass
309 * @method set_text
312 * @method set_text
310 * @param {string} text
313 * @param {string} text
311 */
314 */
312 Cell.prototype.set_text = function (text) {
315 Cell.prototype.set_text = function (text) {
313 };
316 };
314
317
315 /**
318 /**
316 * should be overritten by subclass
319 * should be overritten by subclass
317 * serialise cell to json.
320 * serialise cell to json.
318 * @method toJSON
321 * @method toJSON
319 **/
322 **/
320 Cell.prototype.toJSON = function () {
323 Cell.prototype.toJSON = function () {
321 var data = {};
324 var data = {};
322 data.metadata = this.metadata;
325 data.metadata = this.metadata;
323 data.cell_type = this.cell_type;
326 data.cell_type = this.cell_type;
324 return data;
327 return data;
325 };
328 };
326
329
327
330
328 /**
331 /**
329 * should be overritten by subclass
332 * should be overritten by subclass
330 * @method fromJSON
333 * @method fromJSON
331 **/
334 **/
332 Cell.prototype.fromJSON = function (data) {
335 Cell.prototype.fromJSON = function (data) {
333 if (data.metadata !== undefined) {
336 if (data.metadata !== undefined) {
334 this.metadata = data.metadata;
337 this.metadata = data.metadata;
335 }
338 }
336 this.celltoolbar.rebuild();
339 this.celltoolbar.rebuild();
337 };
340 };
338
341
339
342
340 /**
343 /**
341 * can the cell be split into two cells
344 * can the cell be split into two cells
342 * @method is_splittable
345 * @method is_splittable
343 **/
346 **/
344 Cell.prototype.is_splittable = function () {
347 Cell.prototype.is_splittable = function () {
345 return true;
348 return true;
346 };
349 };
347
350
348
351
349 /**
352 /**
350 * can the cell be merged with other cells
353 * can the cell be merged with other cells
351 * @method is_mergeable
354 * @method is_mergeable
352 **/
355 **/
353 Cell.prototype.is_mergeable = function () {
356 Cell.prototype.is_mergeable = function () {
354 return true;
357 return true;
355 };
358 };
356
359
357
360
358 /**
361 /**
359 * @return {String} - the text before the cursor
362 * @return {String} - the text before the cursor
360 * @method get_pre_cursor
363 * @method get_pre_cursor
361 **/
364 **/
362 Cell.prototype.get_pre_cursor = function () {
365 Cell.prototype.get_pre_cursor = function () {
363 var cursor = this.code_mirror.getCursor();
366 var cursor = this.code_mirror.getCursor();
364 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
367 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
365 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
368 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
366 return text;
369 return text;
367 }
370 }
368
371
369
372
370 /**
373 /**
371 * @return {String} - the text after the cursor
374 * @return {String} - the text after the cursor
372 * @method get_post_cursor
375 * @method get_post_cursor
373 **/
376 **/
374 Cell.prototype.get_post_cursor = function () {
377 Cell.prototype.get_post_cursor = function () {
375 var cursor = this.code_mirror.getCursor();
378 var cursor = this.code_mirror.getCursor();
376 var last_line_num = this.code_mirror.lineCount()-1;
379 var last_line_num = this.code_mirror.lineCount()-1;
377 var last_line_len = this.code_mirror.getLine(last_line_num).length;
380 var last_line_len = this.code_mirror.getLine(last_line_num).length;
378 var end = {line:last_line_num, ch:last_line_len}
381 var end = {line:last_line_num, ch:last_line_len}
379 var text = this.code_mirror.getRange(cursor, end);
382 var text = this.code_mirror.getRange(cursor, end);
380 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
383 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
381 return text;
384 return text;
382 };
385 };
383
386
384 /**
387 /**
385 * Show/Hide CodeMirror LineNumber
388 * Show/Hide CodeMirror LineNumber
386 * @method show_line_numbers
389 * @method show_line_numbers
387 *
390 *
388 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
391 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
389 **/
392 **/
390 Cell.prototype.show_line_numbers = function (value) {
393 Cell.prototype.show_line_numbers = function (value) {
391 this.code_mirror.setOption('lineNumbers', value);
394 this.code_mirror.setOption('lineNumbers', value);
392 this.code_mirror.refresh();
395 this.code_mirror.refresh();
393 };
396 };
394
397
395 /**
398 /**
396 * Toggle CodeMirror LineNumber
399 * Toggle CodeMirror LineNumber
397 * @method toggle_line_numbers
400 * @method toggle_line_numbers
398 **/
401 **/
399 Cell.prototype.toggle_line_numbers = function () {
402 Cell.prototype.toggle_line_numbers = function () {
400 var val = this.code_mirror.getOption('lineNumbers');
403 var val = this.code_mirror.getOption('lineNumbers');
401 this.show_line_numbers(!val);
404 this.show_line_numbers(!val);
402 };
405 };
403
406
404 /**
407 /**
405 * Force codemirror highlight mode
408 * Force codemirror highlight mode
406 * @method force_highlight
409 * @method force_highlight
407 * @param {object} - CodeMirror mode
410 * @param {object} - CodeMirror mode
408 **/
411 **/
409 Cell.prototype.force_highlight = function(mode) {
412 Cell.prototype.force_highlight = function(mode) {
410 this.user_highlight = mode;
413 this.user_highlight = mode;
411 this.auto_highlight();
414 this.auto_highlight();
412 };
415 };
413
416
414 /**
417 /**
415 * Try to autodetect cell highlight mode, or use selected mode
418 * Try to autodetect cell highlight mode, or use selected mode
416 * @methods _auto_highlight
419 * @methods _auto_highlight
417 * @private
420 * @private
418 * @param {String|object|undefined} - CodeMirror mode | 'auto'
421 * @param {String|object|undefined} - CodeMirror mode | 'auto'
419 **/
422 **/
420 Cell.prototype._auto_highlight = function (modes) {
423 Cell.prototype._auto_highlight = function (modes) {
421 //Here we handle manually selected modes
424 //Here we handle manually selected modes
422 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
425 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
423 {
426 {
424 var mode = this.user_highlight;
427 var mode = this.user_highlight;
425 CodeMirror.autoLoadMode(this.code_mirror, mode);
428 CodeMirror.autoLoadMode(this.code_mirror, mode);
426 this.code_mirror.setOption('mode', mode);
429 this.code_mirror.setOption('mode', mode);
427 return;
430 return;
428 }
431 }
429 var current_mode = this.code_mirror.getOption('mode', mode);
432 var current_mode = this.code_mirror.getOption('mode', mode);
430 var first_line = this.code_mirror.getLine(0);
433 var first_line = this.code_mirror.getLine(0);
431 // loop on every pairs
434 // loop on every pairs
432 for( var mode in modes) {
435 for( var mode in modes) {
433 var regs = modes[mode]['reg'];
436 var regs = modes[mode]['reg'];
434 // only one key every time but regexp can't be keys...
437 // only one key every time but regexp can't be keys...
435 for(var reg in regs ) {
438 for(var reg in regs ) {
436 // here we handle non magic_modes
439 // here we handle non magic_modes
437 if(first_line.match(regs[reg]) != null) {
440 if(first_line.match(regs[reg]) != null) {
438 if(current_mode == mode){
441 if(current_mode == mode){
439 return;
442 return;
440 }
443 }
441 if (mode.search('magic_') != 0) {
444 if (mode.search('magic_') != 0) {
442 this.code_mirror.setOption('mode', mode);
445 this.code_mirror.setOption('mode', mode);
443 CodeMirror.autoLoadMode(this.code_mirror, mode);
446 CodeMirror.autoLoadMode(this.code_mirror, mode);
444 return;
447 return;
445 }
448 }
446 var open = modes[mode]['open']|| "%%";
449 var open = modes[mode]['open']|| "%%";
447 var close = modes[mode]['close']|| "%%end";
450 var close = modes[mode]['close']|| "%%end";
448 var mmode = mode;
451 var mmode = mode;
449 mode = mmode.substr(6);
452 mode = mmode.substr(6);
450 if(current_mode == mode){
453 if(current_mode == mode){
451 return;
454 return;
452 }
455 }
453 CodeMirror.autoLoadMode(this.code_mirror, mode);
456 CodeMirror.autoLoadMode(this.code_mirror, mode);
454 // create on the fly a mode that swhitch between
457 // create on the fly a mode that swhitch between
455 // plain/text and smth else otherwise `%%` is
458 // plain/text and smth else otherwise `%%` is
456 // source of some highlight issues.
459 // source of some highlight issues.
457 // we use patchedGetMode to circumvent a bug in CM
460 // we use patchedGetMode to circumvent a bug in CM
458 CodeMirror.defineMode(mmode , function(config) {
461 CodeMirror.defineMode(mmode , function(config) {
459 return CodeMirror.multiplexingMode(
462 return CodeMirror.multiplexingMode(
460 CodeMirror.patchedGetMode(config, 'text/plain'),
463 CodeMirror.patchedGetMode(config, 'text/plain'),
461 // always set someting on close
464 // always set someting on close
462 {open: open, close: close,
465 {open: open, close: close,
463 mode: CodeMirror.patchedGetMode(config, mode),
466 mode: CodeMirror.patchedGetMode(config, mode),
464 delimStyle: "delimit"
467 delimStyle: "delimit"
465 }
468 }
466 );
469 );
467 });
470 });
468 this.code_mirror.setOption('mode', mmode);
471 this.code_mirror.setOption('mode', mmode);
469 return;
472 return;
470 }
473 }
471 }
474 }
472 }
475 }
473 // fallback on default
476 // fallback on default
474 var default_mode
477 var default_mode
475 try {
478 try {
476 default_mode = this._options.cm_config.mode;
479 default_mode = this._options.cm_config.mode;
477 } catch(e) {
480 } catch(e) {
478 default_mode = 'text/plain';
481 default_mode = 'text/plain';
479 }
482 }
480 if( current_mode === default_mode){
483 if( current_mode === default_mode){
481 return
484 return
482 }
485 }
483 this.code_mirror.setOption('mode', default_mode);
486 this.code_mirror.setOption('mode', default_mode);
484 };
487 };
485
488
486 IPython.Cell = Cell;
489 IPython.Cell = Cell;
487
490
488 return IPython;
491 return IPython;
489
492
490 }(IPython));
493 }(IPython));
491
494
@@ -1,357 +1,357 b''
1 // function completer.
1 // function completer.
2 //
2 //
3 // completer should be a class that takes an cell instance
3 // completer should be a class that takes an cell instance
4 var IPython = (function (IPython) {
4 var IPython = (function (IPython) {
5 // that will prevent us from misspelling
5 // that will prevent us from misspelling
6 "use strict";
6 "use strict";
7
7
8 // easier key mapping
8 // easier key mapping
9 var key = IPython.utils.keycodes;
9 var key = IPython.utils.keycodes;
10
10
11 function prepend_n_prc(str, n) {
11 function prepend_n_prc(str, n) {
12 for( var i =0 ; i< n ; i++){
12 for( var i =0 ; i< n ; i++){
13 str = '%'+str ;
13 str = '%'+str ;
14 }
14 }
15 return str;
15 return str;
16 }
16 }
17
17
18 function _existing_completion(item, completion_array){
18 function _existing_completion(item, completion_array){
19 for( var c in completion_array ) {
19 for( var c in completion_array ) {
20 if(completion_array[c].trim().substr(-item.length) == item)
20 if(completion_array[c].trim().substr(-item.length) == item)
21 { return true; }
21 { return true; }
22 }
22 }
23 return false;
23 return false;
24 }
24 }
25
25
26 // what is the common start of all completions
26 // what is the common start of all completions
27 function shared_start(B, drop_prct) {
27 function shared_start(B, drop_prct) {
28 if (B.length == 1) {
28 if (B.length == 1) {
29 return B[0];
29 return B[0];
30 }
30 }
31 var A = new Array();
31 var A = new Array();
32 var common;
32 var common;
33 var min_lead_prct = 10;
33 var min_lead_prct = 10;
34 for (var i = 0; i < B.length; i++) {
34 for (var i = 0; i < B.length; i++) {
35 var str = B[i].str;
35 var str = B[i].str;
36 var localmin = 0;
36 var localmin = 0;
37 if(drop_prct === true){
37 if(drop_prct === true){
38 while ( str.substr(0, 1) == '%') {
38 while ( str.substr(0, 1) == '%') {
39 localmin = localmin+1;
39 localmin = localmin+1;
40 str = str.substring(1);
40 str = str.substring(1);
41 }
41 }
42 }
42 }
43 min_lead_prct = Math.min(min_lead_prct, localmin);
43 min_lead_prct = Math.min(min_lead_prct, localmin);
44 A.push(str);
44 A.push(str);
45 }
45 }
46
46
47 if (A.length > 1) {
47 if (A.length > 1) {
48 var tem1, tem2, s;
48 var tem1, tem2, s;
49 A = A.slice(0).sort();
49 A = A.slice(0).sort();
50 tem1 = A[0];
50 tem1 = A[0];
51 s = tem1.length;
51 s = tem1.length;
52 tem2 = A.pop();
52 tem2 = A.pop();
53 while (s && tem2.indexOf(tem1) == -1) {
53 while (s && tem2.indexOf(tem1) == -1) {
54 tem1 = tem1.substring(0, --s);
54 tem1 = tem1.substring(0, --s);
55 }
55 }
56 if (tem1 === "" || tem2.indexOf(tem1) !== 0) {
56 if (tem1 === "" || tem2.indexOf(tem1) !== 0) {
57 return {
57 return {
58 str:prepend_n_prc('', min_lead_prct),
58 str:prepend_n_prc('', min_lead_prct),
59 type: "computed",
59 type: "computed",
60 from: B[0].from,
60 from: B[0].from,
61 to: B[0].to
61 to: B[0].to
62 };
62 };
63 }
63 }
64 return {
64 return {
65 str: prepend_n_prc(tem1, min_lead_prct),
65 str: prepend_n_prc(tem1, min_lead_prct),
66 type: "computed",
66 type: "computed",
67 from: B[0].from,
67 from: B[0].from,
68 to: B[0].to
68 to: B[0].to
69 };
69 };
70 }
70 }
71 return null;
71 return null;
72 }
72 }
73
73
74
74
75 var Completer = function (cell) {
75 var Completer = function (cell) {
76 this.cell = cell;
76 this.cell = cell;
77 this.editor = cell.code_mirror;
77 this.editor = cell.code_mirror;
78 var that = this;
78 var that = this;
79 $([IPython.events]).on('status_busy.Kernel', function () {
79 $([IPython.events]).on('status_busy.Kernel', function () {
80 that.skip_kernel_completion = true;
80 that.skip_kernel_completion = true;
81 });
81 });
82 $([IPython.events]).on('status_idle.Kernel', function () {
82 $([IPython.events]).on('status_idle.Kernel', function () {
83 that.skip_kernel_completion = false;
83 that.skip_kernel_completion = false;
84 });
84 });
85 };
85 };
86
86
87
87
88 Completer.prototype.startCompletion = function () {
88 Completer.prototype.startCompletion = function () {
89 // call for a 'first' completion, that will set the editor and do some
89 // call for a 'first' completion, that will set the editor and do some
90 // special behaviour like autopicking if only one completion availlable
90 // special behaviour like autopicking if only one completion availlable
91 //
91 //
92 if (this.editor.somethingSelected()) return;
92 if (this.editor.somethingSelected()) return;
93 this.done = false;
93 this.done = false;
94 // use to get focus back on opera
94 // use to get focus back on opera
95 this.carry_on_completion(true);
95 this.carry_on_completion(true);
96 };
96 };
97
97
98
98
99 // easy access for julia to monkeypatch
99 // easy access for julia to monkeypatch
100 //
100 //
101 Completer.reinvoke_re = /[%0-9a-z._/\\:~-]/i;
101 Completer.reinvoke_re = /[%0-9a-z._/\\:~-]/i;
102
102
103 Completer.prototype.reinvoke= function(pre_cursor, block, cursor){
103 Completer.prototype.reinvoke= function(pre_cursor, block, cursor){
104 return Completer.reinvoke_re.test(pre_cursor);
104 return Completer.reinvoke_re.test(pre_cursor);
105 }
105 }
106
106
107 /**
107 /**
108 *
108 *
109 * pass true as parameter if this is the first invocation of the completer
109 * pass true as parameter if this is the first invocation of the completer
110 * this will prevent the completer to dissmiss itself if it is not on a
110 * this will prevent the completer to dissmiss itself if it is not on a
111 * word boundary like pressing tab after a space, and make it autopick the
111 * word boundary like pressing tab after a space, and make it autopick the
112 * only choice if there is only one which prevent from popping the UI. as
112 * only choice if there is only one which prevent from popping the UI. as
113 * well as fast-forwarding the typing if all completion have a common
113 * well as fast-forwarding the typing if all completion have a common
114 * shared start
114 * shared start
115 **/
115 **/
116 Completer.prototype.carry_on_completion = function (first_invocation) {
116 Completer.prototype.carry_on_completion = function (first_invocation) {
117 // Pass true as parameter if you want the completer to autopick when
117 // Pass true as parameter if you want the completer to autopick when
118 // only one completion. This function is automatically reinvoked at
118 // only one completion. This function is automatically reinvoked at
119 // each keystroke with first_invocation = false
119 // each keystroke with first_invocation = false
120 var cur = this.editor.getCursor();
120 var cur = this.editor.getCursor();
121 var line = this.editor.getLine(cur.line);
121 var line = this.editor.getLine(cur.line);
122 var pre_cursor = this.editor.getRange({
122 var pre_cursor = this.editor.getRange({
123 line: cur.line,
123 line: cur.line,
124 ch: cur.ch - 1
124 ch: cur.ch - 1
125 }, cur);
125 }, cur);
126
126
127 // we need to check that we are still on a word boundary
127 // we need to check that we are still on a word boundary
128 // because while typing the completer is still reinvoking itself
128 // because while typing the completer is still reinvoking itself
129 // so dismiss if we are on a "bad" caracter
129 // so dismiss if we are on a "bad" caracter
130 if (!this.reinvoke(pre_cursor) && !first_invocation) {
130 if (!this.reinvoke(pre_cursor) && !first_invocation) {
131 this.close();
131 this.close();
132 return;
132 return;
133 }
133 }
134
134
135 this.autopick = false;
135 this.autopick = false;
136 if (first_invocation) {
136 if (first_invocation) {
137 this.autopick = true;
137 this.autopick = true;
138 }
138 }
139
139
140 // We want a single cursor position.
140 // We want a single cursor position.
141 if (this.editor.somethingSelected()) {
141 if (this.editor.somethingSelected()) {
142 return;
142 return;
143 };
143 };
144
144
145 // one kernel completion came back, finish_completing will be called with the results
145 // one kernel completion came back, finish_completing will be called with the results
146 // we fork here and directly call finish completing if kernel is busy
146 // we fork here and directly call finish completing if kernel is busy
147 if (this.skip_kernel_completion == true) {
147 if (this.skip_kernel_completion == true) {
148 this.finish_completing({
148 this.finish_completing({
149 'matches': [],
149 'matches': [],
150 matched_text: ""
150 matched_text: ""
151 })
151 })
152 } else {
152 } else {
153 this.cell.kernel.complete(line, cur.ch, $.proxy(this.finish_completing, this));
153 this.cell.kernel.complete(line, cur.ch, $.proxy(this.finish_completing, this));
154 }
154 }
155 };
155 };
156
156
157 Completer.prototype.finish_completing = function (msg) {
157 Completer.prototype.finish_completing = function (msg) {
158 // let's build a function that wrap all that stuff into what is needed
158 // let's build a function that wrap all that stuff into what is needed
159 // for the new completer:
159 // for the new completer:
160 var content = msg.content;
160 var content = msg.content;
161 var matched_text = content.matched_text;
161 var matched_text = content.matched_text;
162 var matches = content.matches;
162 var matches = content.matches;
163
163
164 var cur = this.editor.getCursor();
164 var cur = this.editor.getCursor();
165 var results = CodeMirror.contextHint(this.editor);
165 var results = CodeMirror.contextHint(this.editor);
166 var filterd_results = Array();
166 var filterd_results = Array();
167 //remove results from context completion
167 //remove results from context completion
168 //that are already in kernel completion
168 //that are already in kernel completion
169 for(var elm in results) {
169 for(var elm in results) {
170 if(_existing_completion(results[elm]['str'], matches) == false)
170 if(_existing_completion(results[elm]['str'], matches) == false)
171 { filterd_results.push(results[elm]); }
171 { filterd_results.push(results[elm]); }
172 }
172 }
173
173
174 // append the introspection result, in order, at at the beginning of
174 // append the introspection result, in order, at at the beginning of
175 // the table and compute the replacement range from current cursor
175 // the table and compute the replacement range from current cursor
176 // positon and matched_text length.
176 // positon and matched_text length.
177 for (var i = matches.length - 1; i >= 0; --i) {
177 for (var i = matches.length - 1; i >= 0; --i) {
178 filterd_results.unshift({
178 filterd_results.unshift({
179 str: matches[i],
179 str: matches[i],
180 type: "introspection",
180 type: "introspection",
181 from: {
181 from: {
182 line: cur.line,
182 line: cur.line,
183 ch: cur.ch - matched_text.length
183 ch: cur.ch - matched_text.length
184 },
184 },
185 to: {
185 to: {
186 line: cur.line,
186 line: cur.line,
187 ch: cur.ch
187 ch: cur.ch
188 }
188 }
189 });
189 });
190 }
190 }
191
191
192 // one the 2 sources results have been merge, deal with it
192 // one the 2 sources results have been merge, deal with it
193 this.raw_result = filterd_results;
193 this.raw_result = filterd_results;
194
194
195 // if empty result return
195 // if empty result return
196 if (!this.raw_result || !this.raw_result.length) return;
196 if (!this.raw_result || !this.raw_result.length) return;
197
197
198 // When there is only one completion, use it directly.
198 // When there is only one completion, use it directly.
199 if (this.autopick == true && this.raw_result.length == 1) {
199 if (this.autopick == true && this.raw_result.length == 1) {
200 this.insert(this.raw_result[0]);
200 this.insert(this.raw_result[0]);
201 return;
201 return;
202 }
202 }
203
203
204 if (this.raw_result.length == 1) {
204 if (this.raw_result.length == 1) {
205 // test if first and only completion totally matches
205 // test if first and only completion totally matches
206 // what is typed, in this case dismiss
206 // what is typed, in this case dismiss
207 var str = this.raw_result[0].str;
207 var str = this.raw_result[0].str;
208 var pre_cursor = this.editor.getRange({
208 var pre_cursor = this.editor.getRange({
209 line: cur.line,
209 line: cur.line,
210 ch: cur.ch - str.length
210 ch: cur.ch - str.length
211 }, cur);
211 }, cur);
212 if (pre_cursor == str) {
212 if (pre_cursor == str) {
213 this.close();
213 this.close();
214 return;
214 return;
215 }
215 }
216 }
216 }
217
217
218 this.complete = $('<div/>').addClass('completions');
218 this.complete = $('<div/>').addClass('completions');
219 this.complete.attr('id', 'complete');
219 this.complete.attr('id', 'complete');
220
220
221 // Currently webkit doesn't use the size attr correctly. See:
222 // https://code.google.com/p/chromium/issues/detail?id=4579
221 this.sel = $('<select style="width: auto"/>')
223 this.sel = $('<select style="width: auto"/>')
222 .attr('multiple', 'true')
224 .attr('multiple', 'true')
223 .attr('size', Math.min(10, this.raw_result.length));
225 .attr('size', Math.min(10, this.raw_result.length));
224 this.complete.append(this.sel);
226 this.complete.append(this.sel);
225 $('body').append(this.complete);
227 $('body').append(this.complete);
226
228
227 // After everything is on the page, compute the postion.
229 // After everything is on the page, compute the postion.
228 // We put it above the code if it is too close to the bottom of the page.
230 // We put it above the code if it is too close to the bottom of the page.
229 var cur = this.editor.getCursor();
231 var cur = this.editor.getCursor();
230 cur.ch = cur.ch-matched_text.length;
232 cur.ch = cur.ch-matched_text.length;
231 var pos = this.editor.cursorCoords(cur);
233 var pos = this.editor.cursorCoords(cur);
232 var left = pos.left-3;
234 var left = pos.left-3;
233 var top;
235 var top;
234 var cheight = this.complete.height();
236 var cheight = this.complete.height();
235 var wheight = $(window).height();
237 var wheight = $(window).height();
236 if (pos.bottom+cheight+5 > wheight) {
238 if (pos.bottom+cheight+5 > wheight) {
237 top = pos.top-cheight-4;
239 top = pos.top-cheight-4;
238 } else {
240 } else {
239 top = pos.bottom+1;
241 top = pos.bottom+1;
240 }
242 }
241 this.complete.css('left', left + 'px');
243 this.complete.css('left', left + 'px');
242 this.complete.css('top', top + 'px');
244 this.complete.css('top', top + 'px');
243
245
244
246
245 //build the container
247 //build the container
246 var that = this;
248 var that = this;
247 this.sel.dblclick(function () {
249 this.sel.dblclick(function () {
248 that.pick();
250 that.pick();
249 });
251 });
250 this.sel.blur(this.close);
252 this.sel.blur(this.close);
251 this.sel.keydown(function (event) {
253 this.sel.keydown(function (event) {
252 that.keydown(event);
254 that.keydown(event);
253 });
255 });
254
256
255 this.build_gui_list(this.raw_result);
257 this.build_gui_list(this.raw_result);
256
258
257 this.sel.focus();
259 this.sel.focus();
258 // This needs to be after the focus() call because that puts the notebook into
259 // command mode.
260 IPython.keyboard_manager.null_mode();
260 IPython.keyboard_manager.null_mode();
261 // Opera sometimes ignores focusing a freshly created node
261 // Opera sometimes ignores focusing a freshly created node
262 if (window.opera) setTimeout(function () {
262 if (window.opera) setTimeout(function () {
263 if (!this.done) this.sel.focus();
263 if (!this.done) this.sel.focus();
264 }, 100);
264 }, 100);
265 return true;
265 return true;
266 }
266 }
267
267
268 Completer.prototype.insert = function (completion) {
268 Completer.prototype.insert = function (completion) {
269 this.editor.replaceRange(completion.str, completion.from, completion.to);
269 this.editor.replaceRange(completion.str, completion.from, completion.to);
270 }
270 }
271
271
272 Completer.prototype.build_gui_list = function (completions) {
272 Completer.prototype.build_gui_list = function (completions) {
273 for (var i = 0; i < completions.length; ++i) {
273 for (var i = 0; i < completions.length; ++i) {
274 var opt = $('<option/>').text(completions[i].str).addClass(completions[i].type);
274 var opt = $('<option/>').text(completions[i].str).addClass(completions[i].type);
275 this.sel.append(opt);
275 this.sel.append(opt);
276 }
276 }
277 this.sel.children().first().attr('selected', 'true');
277 this.sel.children().first().attr('selected', 'true');
278 this.sel.scrollTop(0);
278 this.sel.scrollTop(0);
279 }
279 }
280
280
281 Completer.prototype.close = function () {
281 Completer.prototype.close = function () {
282 if (this.done) return;
282 if (this.done) return;
283 this.done = true;
283 this.done = true;
284 $('.completions').remove();
284 $('.completions').remove();
285 IPython.keyboard_manager.edit_mode();
285 IPython.keyboard_manager.edit_mode();
286 }
286 }
287
287
288 Completer.prototype.pick = function () {
288 Completer.prototype.pick = function () {
289 this.insert(this.raw_result[this.sel[0].selectedIndex]);
289 this.insert(this.raw_result[this.sel[0].selectedIndex]);
290 this.close();
290 this.close();
291 var that = this;
291 var that = this;
292 setTimeout(function () {
292 setTimeout(function () {
293 that.editor.focus();
293 that.editor.focus();
294 }, 50);
294 }, 50);
295 }
295 }
296
296
297
297
298 Completer.prototype.keydown = function (event) {
298 Completer.prototype.keydown = function (event) {
299 var code = event.keyCode;
299 var code = event.keyCode;
300 var that = this;
300 var that = this;
301 var special_key = false;
301 var special_key = false;
302
302
303 // detect special keys like SHIFT,PGUP,...
303 // detect special keys like SHIFT,PGUP,...
304 for( var _key in key ) {
304 for( var _key in key ) {
305 if (code == key[_key] ) {
305 if (code == key[_key] ) {
306 special_key = true;
306 special_key = true;
307 }
307 }
308 };
308 };
309
309
310 // Enter
310 // Enter
311 if (code == key.ENTER) {
311 if (code == key.ENTER) {
312 CodeMirror.e_stop(event);
312 CodeMirror.e_stop(event);
313 this.pick();
313 this.pick();
314 }
314 }
315 // Escape or backspace
315 // Escape or backspace
316 else if (code == key.ESC) {
316 else if (code == key.ESC) {
317 CodeMirror.e_stop(event);
317 CodeMirror.e_stop(event);
318 this.close();
318 this.close();
319 this.editor.focus();
319 this.editor.focus();
320 } else if (code == key.SPACE || code == key.BACKSPACE) {
320 } else if (code == key.SPACE || code == key.BACKSPACE) {
321 this.close();
321 this.close();
322 this.editor.focus();
322 this.editor.focus();
323 } else if (code == key.TAB) {
323 } else if (code == key.TAB) {
324 //all the fastforwarding operation,
324 //all the fastforwarding operation,
325 //Check that shared start is not null which can append with prefixed completion
325 //Check that shared start is not null which can append with prefixed completion
326 // like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
326 // like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
327 // to erase py
327 // to erase py
328 var sh = shared_start(this.raw_result, true);
328 var sh = shared_start(this.raw_result, true);
329 if (sh) {
329 if (sh) {
330 this.insert(sh);
330 this.insert(sh);
331 }
331 }
332 this.close();
332 this.close();
333 CodeMirror.e_stop(event);
333 CodeMirror.e_stop(event);
334 this.editor.focus();
334 this.editor.focus();
335 //reinvoke self
335 //reinvoke self
336 setTimeout(function () {
336 setTimeout(function () {
337 that.carry_on_completion();
337 that.carry_on_completion();
338 }, 50);
338 }, 50);
339 } else if (code == key.UPARROW || code == key.DOWNARROW) {
339 } else if (code == key.UPARROW || code == key.DOWNARROW) {
340 // need to do that to be able to move the arrow
340 // need to do that to be able to move the arrow
341 // when on the first or last line ofo a code cell
341 // when on the first or last line ofo a code cell
342 event.stopPropagation();
342 event.stopPropagation();
343 } else if (special_key != true) {
343 } else if (special_key != true) {
344 this.close();
344 this.close();
345 this.editor.focus();
345 this.editor.focus();
346 //we give focus to the editor immediately and call sell in 50 ms
346 //we give focus to the editor immediately and call sell in 50 ms
347 setTimeout(function () {
347 setTimeout(function () {
348 that.carry_on_completion();
348 that.carry_on_completion();
349 }, 50);
349 }, 50);
350 }
350 }
351 }
351 }
352
352
353
353
354 IPython.Completer = Completer;
354 IPython.Completer = Completer;
355
355
356 return IPython;
356 return IPython;
357 }(IPython));
357 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now