##// END OF EJS Templates
autochange highlight with cell magics...
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,33 b''
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2012 The IPython Development Team
3 //
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
7
8 //============================================================================
9 // Notebook
10 //============================================================================
11
12 var IPython = (function (IPython) {
13
14 var config = {
15 cell_magic_highlight : {
16 'magic_javascript':{'reg':[/^%%javascript/]}
17 ,'magic_perl' :{'reg':[/^%%perl/]}
18 ,'magic_ruby' :{'reg':[/^%%ruby/]}
19 ,'magic_python' :{'reg':[/^%%python3?/]}
20 ,'magic_shell' :{'reg':[/^%%bash/]}
21 ,'magic_r' :{'reg':[/^%%R/]}
22 },
23 raw_cell_highlight : {
24 'diff' :{'reg':[/^diff/]}
25 }
26 };
27
28 IPython.config = config;
29
30 return IPython;
31
32 }(IPython));
33
@@ -20,19 +20,21 b' var IPython = (function (IPython) {'
20 this.selected = false;
20 this.selected = false;
21 this.element = null;
21 this.element = null;
22 this.metadata = {};
22 this.metadata = {};
23 // load this from metadata later ?
24 this.user_highlight == 'auto';
23 this.create_element();
25 this.create_element();
24 if (this.element !== null) {
26 if (this.element !== null) {
25 this.element.data("cell", this);
27 this.element.data("cell", this);
26 this.bind_events();
28 this.bind_events();
27 }
29 }
28 this.cell_id = utils.uuid();
30 this.cell_id = utils.uuid();
31
29 };
32 };
30
33
31
34
32 // Subclasses must implement create_element.
35 // Subclasses must implement create_element.
33 Cell.prototype.create_element = function () {};
36 Cell.prototype.create_element = function () {};
34
37
35
36 Cell.prototype.bind_events = function () {
38 Cell.prototype.bind_events = function () {
37 var that = this;
39 var that = this;
38 // We trigger events so that Cell doesn't have to depend on Notebook.
40 // We trigger events so that Cell doesn't have to depend on Notebook.
@@ -154,6 +156,61 b' var IPython = (function (IPython) {'
154 this.code_mirror.refresh();
156 this.code_mirror.refresh();
155 };
157 };
156
158
159 Cell.prototype.force_highlight = function(mode) {
160 this.user_highlight = mode;
161 this.auto_highlight();
162 };
163
164 Cell.prototype._auto_highlight = function (modes) {
165 //Here we handle manually selected modes
166 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
167 {
168 var mode = this.user_highlight;
169 CodeMirror.autoLoadMode(this.code_mirror, mode);
170 this.code_mirror.setOption('mode', mode);
171 return;
172 }
173 var first_line = this.code_mirror.getLine(0);
174 // loop on every pairs
175 for( var mode in modes) {
176 var regs = modes[mode]['reg'];
177 // only one key every time but regexp can't be keys...
178 for(var reg in regs ) {
179 // here we handle non magic_modes
180 if(first_line.match(regs[reg]) != null) {
181 if (mode.search('magic_') != 0) {
182 this.code_mirror.setOption('mode',mode);
183 CodeMirror.autoLoadMode(this.code_mirror, mode);
184 return;
185 }
186 var open = modes[mode]['open']|| "%%";
187 var close = modes[mode]['close']|| "%%end";
188 var mmode = mode;
189 mode = mmode.substr(6);
190 CodeMirror.autoLoadMode(this.code_mirror, mode);
191 // create on the fly a mode that swhitch between
192 // plain/text and smth else otherwise `%%` is
193 // source of some highlight issues.
194 // we use patchedGetMode to circumvent a bug in CM
195 CodeMirror.defineMode(mmode , function(config) {
196 return CodeMirror.multiplexingMode(
197 CodeMirror.patchedGetMode(config, 'text/plain'),
198 // always set someting on close
199 {open: open, close: close,
200 mode: CodeMirror.patchedGetMode(config, mode),
201 delimStyle: "delimit"
202 }
203 );
204 });
205 this.code_mirror.setOption('mode', mmode);
206 return;
207 }
208 }
209 }
210 // fallback on default (python)
211 var default_mode = this.default_mode || 'text/plain';
212 this.code_mirror.setOption('mode', default_mode);
213 };
157
214
158 IPython.Cell = Cell;
215 IPython.Cell = Cell;
159
216
@@ -14,6 +14,7 b' var IPython = (function (IPython) {'
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16 var key = IPython.utils.keycodes;
16 var key = IPython.utils.keycodes;
17 CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js";
17
18
18 var CodeCell = function (kernel) {
19 var CodeCell = function (kernel) {
19 // The kernel doesn't have to be set at creation time, in that case
20 // The kernel doesn't have to be set at creation time, in that case
@@ -23,13 +24,23 b' var IPython = (function (IPython) {'
23 this.input_prompt_number = null;
24 this.input_prompt_number = null;
24 this.tooltip_on_tab = true;
25 this.tooltip_on_tab = true;
25 this.collapsed = false;
26 this.collapsed = false;
27 this.default_mode = 'python';
26 IPython.Cell.apply(this, arguments);
28 IPython.Cell.apply(this, arguments);
29
30 var that = this;
31 this.element.focusout(
32 function() { that.auto_highlight(); }
33 );
27 };
34 };
28
35
29
36
30 CodeCell.prototype = new IPython.Cell();
37 CodeCell.prototype = new IPython.Cell();
31
38
32
39
40 CodeCell.prototype.auto_highlight = function () {
41 this._auto_highlight(IPython.config.cell_magic_highlight)
42 };
43
33 CodeCell.prototype.create_element = function () {
44 CodeCell.prototype.create_element = function () {
34 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
45 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
35 cell.attr('tabindex','2');
46 cell.attr('tabindex','2');
@@ -76,6 +87,9 b' var IPython = (function (IPython) {'
76 };
87 };
77
88
78 var cur = editor.getCursor();
89 var cur = editor.getCursor();
90 if (event.keyCode === key.ENTER){
91 this.auto_highlight();
92 }
79
93
80 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
94 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
81 // Always ignore shift-enter in CodeMirror as we handle it.
95 // Always ignore shift-enter in CodeMirror as we handle it.
@@ -172,6 +186,7 b' var IPython = (function (IPython) {'
172 IPython.Cell.prototype.select.apply(this);
186 IPython.Cell.prototype.select.apply(this);
173 this.code_mirror.refresh();
187 this.code_mirror.refresh();
174 this.code_mirror.focus();
188 this.code_mirror.focus();
189 this.auto_highlight();
175 // We used to need an additional refresh() after the focus, but
190 // We used to need an additional refresh() after the focus, but
176 // it appears that this has been fixed in CM. This bug would show
191 // it appears that this has been fixed in CM. This bug would show
177 // up on FF when a newly loaded markdown cell was edited.
192 // up on FF when a newly loaded markdown cell was edited.
@@ -267,6 +282,7 b' var IPython = (function (IPython) {'
267 // make this value the starting point, so that we can only undo
282 // make this value the starting point, so that we can only undo
268 // to this state, instead of a blank cell
283 // to this state, instead of a blank cell
269 this.code_mirror.clearHistory();
284 this.code_mirror.clearHistory();
285 this.auto_highlight();
270 }
286 }
271 if (data.prompt_number !== undefined) {
287 if (data.prompt_number !== undefined) {
272 this.set_input_prompt(data.prompt_number);
288 this.set_input_prompt(data.prompt_number);
@@ -1,5 +1,5 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 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.
@@ -12,6 +12,25 b''
12
12
13 $(document).ready(function () {
13 $(document).ready(function () {
14
14
15 // monkey patch CM to be able to syntax highlight cell magics
16 // bug reported upstream,
17 // see https://github.com/marijnh/CodeMirror2/issues/670
18 if(CodeMirror.getMode(1,'text/plain').indent == undefined ){
19 console.log('patching CM for undefined indent');
20 CodeMirror.modes.null = function() { return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0}}}
21 }
22
23 CodeMirror.patchedGetMode = function(config, mode){
24 var cmmode = CodeMirror.getMode(config, mode);
25 if(cmmode.indent == null)
26 {
27 console.log('patch mode "' , mode, '" on the fly');
28 cmmode.indent = function(){return 0};
29 }
30 return cmmode;
31 }
32 // end monkey patching CodeMirror
33
15 IPython.init_mathjax();
34 IPython.init_mathjax();
16
35
17 IPython.read_only = $('body').data('readOnly') === 'True';
36 IPython.read_only = $('body').data('readOnly') === 'True';
@@ -257,11 +257,19 b' var IPython = (function (IPython) {'
257 this.code_mirror_mode = 'rst';
257 this.code_mirror_mode = 'rst';
258 IPython.TextCell.apply(this, arguments);
258 IPython.TextCell.apply(this, arguments);
259 this.cell_type = 'raw';
259 this.cell_type = 'raw';
260 var that = this
261
262 this.element.focusout(
263 function() { that.auto_highlight(); }
264 );
260 };
265 };
261
266
262
267
263 RawCell.prototype = new TextCell();
268 RawCell.prototype = new TextCell();
264
269
270 RawCell.prototype.auto_highlight = function () {
271 this._auto_highlight(IPython.config.raw_cell_highlight);
272 };
265
273
266 RawCell.prototype.render = function () {
274 RawCell.prototype.render = function () {
267 this.rendered = true;
275 this.rendered = true;
@@ -212,6 +212,8 b' data-notebook-id={{notebook_id}}'
212 {% block script %}
212 {% block script %}
213
213
214 <script src="{{ static_url("codemirror/lib/codemirror.js") }}" charset="utf-8"></script>
214 <script src="{{ static_url("codemirror/lib/codemirror.js") }}" charset="utf-8"></script>
215 <script src="{{ static_url("codemirror/lib/util/loadmode.js") }}" charset="utf-8"></script>
216 <script src="{{ static_url("codemirror/lib/util/multiplex.js") }}" charset="utf-8"></script>
215 <script src="{{ static_url("codemirror/mode/python/python.js") }}" charset="utf-8"></script>
217 <script src="{{ static_url("codemirror/mode/python/python.js") }}" charset="utf-8"></script>
216 <script src="{{ static_url("codemirror/mode/htmlmixed/htmlmixed.js") }}" charset="utf-8"></script>
218 <script src="{{ static_url("codemirror/mode/htmlmixed/htmlmixed.js") }}" charset="utf-8"></script>
217 <script src="{{ static_url("codemirror/mode/xml/xml.js") }}" charset="utf-8"></script>
219 <script src="{{ static_url("codemirror/mode/xml/xml.js") }}" charset="utf-8"></script>
@@ -243,6 +245,7 b' data-notebook-id={{notebook_id}}'
243 <script src="{{ static_url("js/notebook.js") }}" type="text/javascript" charset="utf-8"></script>
245 <script src="{{ static_url("js/notebook.js") }}" type="text/javascript" charset="utf-8"></script>
244 <script src="{{ static_url("js/notificationwidget.js") }}" type="text/javascript" charset="utf-8"></script>
246 <script src="{{ static_url("js/notificationwidget.js") }}" type="text/javascript" charset="utf-8"></script>
245 <script src="{{ static_url("js/tooltip.js") }}" type="text/javascript" charset="utf-8"></script>
247 <script src="{{ static_url("js/tooltip.js") }}" type="text/javascript" charset="utf-8"></script>
248 <script src="{{ static_url("js/config.js") }}" type="text/javascript" charset="utf-8"></script>
246 <script src="{{ static_url("js/notebookmain.js") }}" type="text/javascript" charset="utf-8"></script>
249 <script src="{{ static_url("js/notebookmain.js") }}" type="text/javascript" charset="utf-8"></script>
247
250
248 <script src="{{ static_url("js/contexthint.js") }}" charset="utf-8"></script>
251 <script src="{{ static_url("js/contexthint.js") }}" charset="utf-8"></script>
General Comments 0
You need to be logged in to leave comments. Login now