##// END OF EJS Templates
Fixing this->that.
Brian E. Granger -
Show More
@@ -1,502 +1,502 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 this.init_classes();
65 this.init_classes();
66 }
66 }
67 };
67 };
68
68
69 Cell.options_default = {
69 Cell.options_default = {
70 cm_config : {
70 cm_config : {
71 indentUnit : 4,
71 indentUnit : 4,
72 readOnly: false,
72 readOnly: false,
73 theme: "default"
73 theme: "default"
74 }
74 }
75 };
75 };
76
76
77 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
77 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
78 // by disabling drag/drop altogether on Safari
78 // by disabling drag/drop altogether on Safari
79 // https://github.com/marijnh/CodeMirror/issues/332
79 // https://github.com/marijnh/CodeMirror/issues/332
80
80
81 if (utils.browser[0] == "Safari") {
81 if (utils.browser[0] == "Safari") {
82 Cell.options_default.cm_config.dragDrop = false;
82 Cell.options_default.cm_config.dragDrop = false;
83 }
83 }
84
84
85 Cell.prototype.mergeopt = function(_class, options, overwrite){
85 Cell.prototype.mergeopt = function(_class, options, overwrite){
86 options = options || {};
86 options = options || {};
87 overwrite = overwrite || {};
87 overwrite = overwrite || {};
88 return $.extend(true, {}, _class.options_default, options, overwrite)
88 return $.extend(true, {}, _class.options_default, options, overwrite)
89
89
90 }
90 }
91
91
92
92
93
93
94 /**
94 /**
95 * Empty. Subclasses must implement create_element.
95 * Empty. Subclasses must implement create_element.
96 * This should contain all the code to create the DOM element in notebook
96 * This should contain all the code to create the DOM element in notebook
97 * and will be called by Base Class constructor.
97 * and will be called by Base Class constructor.
98 * @method create_element
98 * @method create_element
99 */
99 */
100 Cell.prototype.create_element = function () {
100 Cell.prototype.create_element = function () {
101 };
101 };
102
102
103 Cell.prototype.init_classes = function () {
103 Cell.prototype.init_classes = function () {
104 // Call after this.element exists to initialize the css classes
104 // Call after this.element exists to initialize the css classes
105 // related to selected, rendered and mode.
105 // related to selected, rendered and mode.
106 if (this.selected) {
106 if (this.selected) {
107 this.element.addClass('selected');
107 this.element.addClass('selected');
108 } else {
108 } else {
109 this.element.addClass('unselected');
109 this.element.addClass('unselected');
110 }
110 }
111 if (this.rendered) {
111 if (this.rendered) {
112 this.element.addClass('rendered');
112 this.element.addClass('rendered');
113 } else {
113 } else {
114 this.element.addClass('unrendered');
114 this.element.addClass('unrendered');
115 }
115 }
116 if (this.mode === 'edit') {
116 if (this.mode === 'edit') {
117 this.element.addClass('edit_mode');
117 this.element.addClass('edit_mode');
118 } else {
118 } else {
119 this.element.addClass('command_mode');
119 this.element.addClass('command_mode');
120 }
120 }
121 }
121 }
122
122
123
123
124 /**
124 /**
125 * Subclasses can implement override bind_events.
125 * Subclasses can implement override bind_events.
126 * Be carefull to call the parent method when overwriting as it fires event.
126 * Be carefull to call the parent method when overwriting as it fires event.
127 * this will be triggerd after create_element in constructor.
127 * this will be triggerd after create_element in constructor.
128 * @method bind_events
128 * @method bind_events
129 */
129 */
130 Cell.prototype.bind_events = function () {
130 Cell.prototype.bind_events = function () {
131 var that = this;
131 var that = this;
132 // We trigger events so that Cell doesn't have to depend on Notebook.
132 // We trigger events so that Cell doesn't have to depend on Notebook.
133 that.element.click(function (event) {
133 that.element.click(function (event) {
134 if (!that.selected) {
134 if (!that.selected) {
135 $([IPython.events]).trigger('select.Cell', {'cell':that});
135 $([IPython.events]).trigger('select.Cell', {'cell':that});
136 };
136 };
137 });
137 });
138 that.element.focusin(function (event) {
138 that.element.focusin(function (event) {
139 if (!that.selected) {
139 if (!that.selected) {
140 $([IPython.events]).trigger('select.Cell', {'cell':that});
140 $([IPython.events]).trigger('select.Cell', {'cell':that});
141 };
141 };
142 });
142 });
143 if (this.code_mirror) {
143 if (this.code_mirror) {
144 this.code_mirror.on("change", function(cm, change) {
144 this.code_mirror.on("change", function(cm, change) {
145 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
145 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
146 });
146 });
147 }
147 }
148 if (this.code_mirror) {
148 if (this.code_mirror) {
149 this.code_mirror.on('focus', function(cm, change) {
149 this.code_mirror.on('focus', function(cm, change) {
150 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
150 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
151 });
151 });
152 }
152 }
153 if (this.code_mirror) {
153 if (this.code_mirror) {
154 this.code_mirror.on('blur', function(cm, change) {
154 this.code_mirror.on('blur', function(cm, change) {
155 if (that.mode === 'edit') {
155 if (that.mode === 'edit') {
156 setTimeout(function () {
156 setTimeout(function () {
157 var isf = IPython.utils.is_focused;
157 var isf = IPython.utils.is_focused;
158 var trigger = true;
158 var trigger = true;
159 if (isf('div#tooltip') || isf('div.completions')) {
159 if (isf('div#tooltip') || isf('div.completions')) {
160 trigger = false;
160 trigger = false;
161 }
161 }
162 if (trigger) {
162 if (trigger) {
163 $([IPython.events]).trigger('command_mode.Cell', {cell: that});
163 $([IPython.events]).trigger('command_mode.Cell', {cell: that});
164 }
164 }
165 }, 1);
165 }, 1);
166 }
166 }
167 });
167 });
168 }
168 }
169 };
169 };
170
170
171 /**
171 /**
172 * Triger typsetting of math by mathjax on current cell element
172 * Triger typsetting of math by mathjax on current cell element
173 * @method typeset
173 * @method typeset
174 */
174 */
175 Cell.prototype.typeset = function () {
175 Cell.prototype.typeset = function () {
176 if (window.MathJax) {
176 if (window.MathJax) {
177 var cell_math = this.element.get(0);
177 var cell_math = this.element.get(0);
178 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
178 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
179 }
179 }
180 };
180 };
181
181
182 /**
182 /**
183 * handle cell level logic when a cell is selected
183 * handle cell level logic when a cell is selected
184 * @method select
184 * @method select
185 * @return is the action being taken
185 * @return is the action being taken
186 */
186 */
187 Cell.prototype.select = function () {
187 Cell.prototype.select = function () {
188 if (!this.selected) {
188 if (!this.selected) {
189 this.element.addClass('selected');
189 this.element.addClass('selected');
190 this.element.removeClass('unselected');
190 this.element.removeClass('unselected');
191 this.selected = true;
191 this.selected = true;
192 return true;
192 return true;
193 } else {
193 } else {
194 return false;
194 return false;
195 }
195 }
196 };
196 };
197
197
198 /**
198 /**
199 * handle cell level logic when a cell is unselected
199 * handle cell level logic when a cell is unselected
200 * @method unselect
200 * @method unselect
201 * @return is the action being taken
201 * @return is the action being taken
202 */
202 */
203 Cell.prototype.unselect = function () {
203 Cell.prototype.unselect = function () {
204 if (this.selected) {
204 if (this.selected) {
205 this.element.addClass('unselected');
205 this.element.addClass('unselected');
206 this.element.removeClass('selected');
206 this.element.removeClass('selected');
207 this.selected = false;
207 this.selected = false;
208 return true;
208 return true;
209 } else {
209 } else {
210 return false;
210 return false;
211 }
211 }
212 };
212 };
213
213
214 /**
214 /**
215 * handle cell level logic when a cell is rendered
215 * handle cell level logic when a cell is rendered
216 * @method render
216 * @method render
217 * @return is the action being taken
217 * @return is the action being taken
218 */
218 */
219 Cell.prototype.render = function () {
219 Cell.prototype.render = function () {
220 if (!this.rendered) {
220 if (!this.rendered) {
221 this.element.addClass('rendered');
221 this.element.addClass('rendered');
222 this.element.removeClass('unrendered');
222 this.element.removeClass('unrendered');
223 this.rendered = true;
223 this.rendered = true;
224 return true;
224 return true;
225 } else {
225 } else {
226 return false;
226 return false;
227 }
227 }
228 };
228 };
229
229
230 /**
230 /**
231 * handle cell level logic when a cell is unrendered
231 * handle cell level logic when a cell is unrendered
232 * @method unrender
232 * @method unrender
233 * @return is the action being taken
233 * @return is the action being taken
234 */
234 */
235 Cell.prototype.unrender = function () {
235 Cell.prototype.unrender = function () {
236 if (this.rendered) {
236 if (this.rendered) {
237 this.element.addClass('unrendered');
237 this.element.addClass('unrendered');
238 this.element.removeClass('rendered');
238 this.element.removeClass('rendered');
239 this.rendered = false;
239 this.rendered = false;
240 return true;
240 return true;
241 } else {
241 } else {
242 return false;
242 return false;
243 }
243 }
244 };
244 };
245
245
246 /**
246 /**
247 * enter the command mode for the cell
247 * enter the command mode for the cell
248 * @method command_mode
248 * @method command_mode
249 * @return is the action being taken
249 * @return is the action being taken
250 */
250 */
251 Cell.prototype.command_mode = function () {
251 Cell.prototype.command_mode = function () {
252 if (this.mode !== 'command') {
252 if (this.mode !== 'command') {
253 this.element.addClass('command_mode');
253 this.element.addClass('command_mode');
254 this.element.removeClass('edit_mode');
254 this.element.removeClass('edit_mode');
255 this.mode = 'command';
255 this.mode = 'command';
256 return true;
256 return true;
257 } else {
257 } else {
258 return false;
258 return false;
259 }
259 }
260 };
260 };
261
261
262 /**
262 /**
263 * enter the edit mode for the cell
263 * enter the edit mode for the cell
264 * @method command_mode
264 * @method command_mode
265 * @return is the action being taken
265 * @return is the action being taken
266 */
266 */
267 Cell.prototype.edit_mode = function () {
267 Cell.prototype.edit_mode = function () {
268 if (this.mode !== 'edit') {
268 if (this.mode !== 'edit') {
269 this.element.addClass('edit_mode');
269 this.element.addClass('edit_mode');
270 this.element.removeClass('command_mode');
270 this.element.removeClass('command_mode');
271 this.mode = 'edit';
271 this.mode = 'edit';
272 return true;
272 return true;
273 } else {
273 } else {
274 return false;
274 return false;
275 }
275 }
276 }
276 }
277
277
278 /**
278 /**
279 * Focus the cell in the DOM sense
279 * Focus the cell in the DOM sense
280 * @method focus_cell
280 * @method focus_cell
281 */
281 */
282 Cell.prototype.focus_cell = function () {
282 Cell.prototype.focus_cell = function () {
283 this.element.focus();
283 this.element.focus();
284 }
284 }
285
285
286 /**
286 /**
287 * Focus the editor area so a user can type
287 * Focus the editor area so a user can type
288 * @method focus_editor
288 * @method focus_editor
289 */
289 */
290 Cell.prototype.focus_editor = function () {
290 Cell.prototype.focus_editor = function () {
291 var that = this;
291 var that = this;
292 this.refresh();
292 this.refresh();
293 // Only focus the CM editor if it is not focused already. This prevents jumps
293 // Only focus the CM editor if it is not focused already. This prevents jumps
294 // related to the previous prompt position.
294 // related to the previous prompt position.
295 setTimeout(function () {
295 setTimeout(function () {
296 var isf = IPython.utils.is_focused;
296 var isf = IPython.utils.is_focused;
297 if (!isf(that.element.find('div.CodeMirror'))) {
297 if (!isf(that.element.find('div.CodeMirror'))) {
298 this.code_mirror.focus();
298 that.code_mirror.focus();
299 }
299 }
300 }, 1);
300 }, 1);
301 }
301 }
302
302
303 /**
303 /**
304 * Refresh codemirror instance
304 * Refresh codemirror instance
305 * @method refresh
305 * @method refresh
306 */
306 */
307 Cell.prototype.refresh = function () {
307 Cell.prototype.refresh = function () {
308 this.code_mirror.refresh();
308 this.code_mirror.refresh();
309 };
309 };
310
310
311 /**
311 /**
312 * should be overritten by subclass
312 * should be overritten by subclass
313 * @method get_text
313 * @method get_text
314 */
314 */
315 Cell.prototype.get_text = function () {
315 Cell.prototype.get_text = function () {
316 };
316 };
317
317
318 /**
318 /**
319 * should be overritten by subclass
319 * should be overritten by subclass
320 * @method set_text
320 * @method set_text
321 * @param {string} text
321 * @param {string} text
322 */
322 */
323 Cell.prototype.set_text = function (text) {
323 Cell.prototype.set_text = function (text) {
324 };
324 };
325
325
326 /**
326 /**
327 * should be overritten by subclass
327 * should be overritten by subclass
328 * serialise cell to json.
328 * serialise cell to json.
329 * @method toJSON
329 * @method toJSON
330 **/
330 **/
331 Cell.prototype.toJSON = function () {
331 Cell.prototype.toJSON = function () {
332 var data = {};
332 var data = {};
333 data.metadata = this.metadata;
333 data.metadata = this.metadata;
334 data.cell_type = this.cell_type;
334 data.cell_type = this.cell_type;
335 return data;
335 return data;
336 };
336 };
337
337
338
338
339 /**
339 /**
340 * should be overritten by subclass
340 * should be overritten by subclass
341 * @method fromJSON
341 * @method fromJSON
342 **/
342 **/
343 Cell.prototype.fromJSON = function (data) {
343 Cell.prototype.fromJSON = function (data) {
344 if (data.metadata !== undefined) {
344 if (data.metadata !== undefined) {
345 this.metadata = data.metadata;
345 this.metadata = data.metadata;
346 }
346 }
347 this.celltoolbar.rebuild();
347 this.celltoolbar.rebuild();
348 };
348 };
349
349
350
350
351 /**
351 /**
352 * can the cell be split into two cells
352 * can the cell be split into two cells
353 * @method is_splittable
353 * @method is_splittable
354 **/
354 **/
355 Cell.prototype.is_splittable = function () {
355 Cell.prototype.is_splittable = function () {
356 return true;
356 return true;
357 };
357 };
358
358
359
359
360 /**
360 /**
361 * can the cell be merged with other cells
361 * can the cell be merged with other cells
362 * @method is_mergeable
362 * @method is_mergeable
363 **/
363 **/
364 Cell.prototype.is_mergeable = function () {
364 Cell.prototype.is_mergeable = function () {
365 return true;
365 return true;
366 };
366 };
367
367
368
368
369 /**
369 /**
370 * @return {String} - the text before the cursor
370 * @return {String} - the text before the cursor
371 * @method get_pre_cursor
371 * @method get_pre_cursor
372 **/
372 **/
373 Cell.prototype.get_pre_cursor = function () {
373 Cell.prototype.get_pre_cursor = function () {
374 var cursor = this.code_mirror.getCursor();
374 var cursor = this.code_mirror.getCursor();
375 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
375 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
376 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
376 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
377 return text;
377 return text;
378 }
378 }
379
379
380
380
381 /**
381 /**
382 * @return {String} - the text after the cursor
382 * @return {String} - the text after the cursor
383 * @method get_post_cursor
383 * @method get_post_cursor
384 **/
384 **/
385 Cell.prototype.get_post_cursor = function () {
385 Cell.prototype.get_post_cursor = function () {
386 var cursor = this.code_mirror.getCursor();
386 var cursor = this.code_mirror.getCursor();
387 var last_line_num = this.code_mirror.lineCount()-1;
387 var last_line_num = this.code_mirror.lineCount()-1;
388 var last_line_len = this.code_mirror.getLine(last_line_num).length;
388 var last_line_len = this.code_mirror.getLine(last_line_num).length;
389 var end = {line:last_line_num, ch:last_line_len}
389 var end = {line:last_line_num, ch:last_line_len}
390 var text = this.code_mirror.getRange(cursor, end);
390 var text = this.code_mirror.getRange(cursor, end);
391 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
391 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
392 return text;
392 return text;
393 };
393 };
394
394
395 /**
395 /**
396 * Show/Hide CodeMirror LineNumber
396 * Show/Hide CodeMirror LineNumber
397 * @method show_line_numbers
397 * @method show_line_numbers
398 *
398 *
399 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
399 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
400 **/
400 **/
401 Cell.prototype.show_line_numbers = function (value) {
401 Cell.prototype.show_line_numbers = function (value) {
402 this.code_mirror.setOption('lineNumbers', value);
402 this.code_mirror.setOption('lineNumbers', value);
403 this.code_mirror.refresh();
403 this.code_mirror.refresh();
404 };
404 };
405
405
406 /**
406 /**
407 * Toggle CodeMirror LineNumber
407 * Toggle CodeMirror LineNumber
408 * @method toggle_line_numbers
408 * @method toggle_line_numbers
409 **/
409 **/
410 Cell.prototype.toggle_line_numbers = function () {
410 Cell.prototype.toggle_line_numbers = function () {
411 var val = this.code_mirror.getOption('lineNumbers');
411 var val = this.code_mirror.getOption('lineNumbers');
412 this.show_line_numbers(!val);
412 this.show_line_numbers(!val);
413 };
413 };
414
414
415 /**
415 /**
416 * Force codemirror highlight mode
416 * Force codemirror highlight mode
417 * @method force_highlight
417 * @method force_highlight
418 * @param {object} - CodeMirror mode
418 * @param {object} - CodeMirror mode
419 **/
419 **/
420 Cell.prototype.force_highlight = function(mode) {
420 Cell.prototype.force_highlight = function(mode) {
421 this.user_highlight = mode;
421 this.user_highlight = mode;
422 this.auto_highlight();
422 this.auto_highlight();
423 };
423 };
424
424
425 /**
425 /**
426 * Try to autodetect cell highlight mode, or use selected mode
426 * Try to autodetect cell highlight mode, or use selected mode
427 * @methods _auto_highlight
427 * @methods _auto_highlight
428 * @private
428 * @private
429 * @param {String|object|undefined} - CodeMirror mode | 'auto'
429 * @param {String|object|undefined} - CodeMirror mode | 'auto'
430 **/
430 **/
431 Cell.prototype._auto_highlight = function (modes) {
431 Cell.prototype._auto_highlight = function (modes) {
432 //Here we handle manually selected modes
432 //Here we handle manually selected modes
433 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
433 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
434 {
434 {
435 var mode = this.user_highlight;
435 var mode = this.user_highlight;
436 CodeMirror.autoLoadMode(this.code_mirror, mode);
436 CodeMirror.autoLoadMode(this.code_mirror, mode);
437 this.code_mirror.setOption('mode', mode);
437 this.code_mirror.setOption('mode', mode);
438 return;
438 return;
439 }
439 }
440 var current_mode = this.code_mirror.getOption('mode', mode);
440 var current_mode = this.code_mirror.getOption('mode', mode);
441 var first_line = this.code_mirror.getLine(0);
441 var first_line = this.code_mirror.getLine(0);
442 // loop on every pairs
442 // loop on every pairs
443 for( var mode in modes) {
443 for( var mode in modes) {
444 var regs = modes[mode]['reg'];
444 var regs = modes[mode]['reg'];
445 // only one key every time but regexp can't be keys...
445 // only one key every time but regexp can't be keys...
446 for(var i=0; i<regs.length; i++) {
446 for(var i=0; i<regs.length; i++) {
447 // here we handle non magic_modes
447 // here we handle non magic_modes
448 if(first_line.match(regs[i]) != null) {
448 if(first_line.match(regs[i]) != null) {
449 if(current_mode == mode){
449 if(current_mode == mode){
450 return;
450 return;
451 }
451 }
452 if (mode.search('magic_') != 0) {
452 if (mode.search('magic_') != 0) {
453 this.code_mirror.setOption('mode', mode);
453 this.code_mirror.setOption('mode', mode);
454 CodeMirror.autoLoadMode(this.code_mirror, mode);
454 CodeMirror.autoLoadMode(this.code_mirror, mode);
455 return;
455 return;
456 }
456 }
457 var open = modes[mode]['open']|| "%%";
457 var open = modes[mode]['open']|| "%%";
458 var close = modes[mode]['close']|| "%%end";
458 var close = modes[mode]['close']|| "%%end";
459 var mmode = mode;
459 var mmode = mode;
460 mode = mmode.substr(6);
460 mode = mmode.substr(6);
461 if(current_mode == mode){
461 if(current_mode == mode){
462 return;
462 return;
463 }
463 }
464 CodeMirror.autoLoadMode(this.code_mirror, mode);
464 CodeMirror.autoLoadMode(this.code_mirror, mode);
465 // create on the fly a mode that swhitch between
465 // create on the fly a mode that swhitch between
466 // plain/text and smth else otherwise `%%` is
466 // plain/text and smth else otherwise `%%` is
467 // source of some highlight issues.
467 // source of some highlight issues.
468 // we use patchedGetMode to circumvent a bug in CM
468 // we use patchedGetMode to circumvent a bug in CM
469 CodeMirror.defineMode(mmode , function(config) {
469 CodeMirror.defineMode(mmode , function(config) {
470 return CodeMirror.multiplexingMode(
470 return CodeMirror.multiplexingMode(
471 CodeMirror.patchedGetMode(config, 'text/plain'),
471 CodeMirror.patchedGetMode(config, 'text/plain'),
472 // always set someting on close
472 // always set someting on close
473 {open: open, close: close,
473 {open: open, close: close,
474 mode: CodeMirror.patchedGetMode(config, mode),
474 mode: CodeMirror.patchedGetMode(config, mode),
475 delimStyle: "delimit"
475 delimStyle: "delimit"
476 }
476 }
477 );
477 );
478 });
478 });
479 this.code_mirror.setOption('mode', mmode);
479 this.code_mirror.setOption('mode', mmode);
480 return;
480 return;
481 }
481 }
482 }
482 }
483 }
483 }
484 // fallback on default
484 // fallback on default
485 var default_mode
485 var default_mode
486 try {
486 try {
487 default_mode = this._options.cm_config.mode;
487 default_mode = this._options.cm_config.mode;
488 } catch(e) {
488 } catch(e) {
489 default_mode = 'text/plain';
489 default_mode = 'text/plain';
490 }
490 }
491 if( current_mode === default_mode){
491 if( current_mode === default_mode){
492 return
492 return
493 }
493 }
494 this.code_mirror.setOption('mode', default_mode);
494 this.code_mirror.setOption('mode', default_mode);
495 };
495 };
496
496
497 IPython.Cell = Cell;
497 IPython.Cell = Cell;
498
498
499 return IPython;
499 return IPython;
500
500
501 }(IPython));
501 }(IPython));
502
502
General Comments 0
You need to be logged in to leave comments. Login now