Show More
@@ -1,220 +1,220 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 | var IPython = (function (IPython) { |
|
12 | var IPython = (function (IPython) { | |
13 |
|
13 | |||
14 | var utils = IPython.utils; |
|
14 | var utils = IPython.utils; | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | var Cell = function () { |
|
17 | var Cell = function () { | |
18 | this.placeholder = this.placeholder || ''; |
|
18 | this.placeholder = this.placeholder || ''; | |
19 | this.read_only = false; |
|
19 | this.read_only = false; | |
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 ? |
|
23 | // load this from metadata later ? | |
24 | this.user_highlight == 'auto'; |
|
24 | this.user_highlight == 'auto'; | |
25 | this.create_element(); |
|
25 | this.create_element(); | |
26 | if (this.element !== null) { |
|
26 | if (this.element !== null) { | |
27 | this.element.data("cell", this); |
|
27 | this.element.data("cell", this); | |
28 | this.bind_events(); |
|
28 | this.bind_events(); | |
29 | } |
|
29 | } | |
30 | this.cell_id = utils.uuid(); |
|
30 | this.cell_id = utils.uuid(); | |
31 |
|
||||
32 | }; |
|
31 | }; | |
33 |
|
32 | |||
34 |
|
33 | |||
35 | // Subclasses must implement create_element. |
|
34 | // Subclasses must implement create_element. | |
36 | Cell.prototype.create_element = function () {}; |
|
35 | Cell.prototype.create_element = function () {}; | |
37 |
|
36 | |||
|
37 | ||||
38 | Cell.prototype.bind_events = function () { |
|
38 | Cell.prototype.bind_events = function () { | |
39 | var that = this; |
|
39 | var that = this; | |
40 | // 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. | |
41 | that.element.click(function (event) { |
|
41 | that.element.click(function (event) { | |
42 | if (that.selected === false) { |
|
42 | if (that.selected === false) { | |
43 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
43 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
44 | } |
|
44 | } | |
45 | }); |
|
45 | }); | |
46 | that.element.focusin(function (event) { |
|
46 | that.element.focusin(function (event) { | |
47 | if (that.selected === false) { |
|
47 | if (that.selected === false) { | |
48 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
48 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
49 | } |
|
49 | } | |
50 | }); |
|
50 | }); | |
51 | }; |
|
51 | }; | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | // typeset with MathJax if MathJax is available |
|
54 | // typeset with MathJax if MathJax is available | |
55 | Cell.prototype.typeset = function () { |
|
55 | Cell.prototype.typeset = function () { | |
56 | if (window.MathJax){ |
|
56 | if (window.MathJax){ | |
57 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
57 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
58 | } |
|
58 | } | |
59 | }; |
|
59 | }; | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | Cell.prototype.select = function () { |
|
62 | Cell.prototype.select = function () { | |
63 | this.element.addClass('ui-widget-content ui-corner-all'); |
|
63 | this.element.addClass('ui-widget-content ui-corner-all'); | |
64 | this.selected = true; |
|
64 | this.selected = true; | |
65 | }; |
|
65 | }; | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | Cell.prototype.unselect = function () { |
|
68 | Cell.prototype.unselect = function () { | |
69 | this.element.removeClass('ui-widget-content ui-corner-all'); |
|
69 | this.element.removeClass('ui-widget-content ui-corner-all'); | |
70 | this.selected = false; |
|
70 | this.selected = false; | |
71 | }; |
|
71 | }; | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | Cell.prototype.get_text = function () { |
|
74 | Cell.prototype.get_text = function () { | |
75 | }; |
|
75 | }; | |
76 |
|
76 | |||
77 |
|
77 | |||
78 | Cell.prototype.set_text = function (text) { |
|
78 | Cell.prototype.set_text = function (text) { | |
79 | }; |
|
79 | }; | |
80 |
|
80 | |||
81 |
|
81 | |||
82 | Cell.prototype.refresh = function () { |
|
82 | Cell.prototype.refresh = function () { | |
83 | this.code_mirror.refresh(); |
|
83 | this.code_mirror.refresh(); | |
84 | }; |
|
84 | }; | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | Cell.prototype.edit = function () { |
|
87 | Cell.prototype.edit = function () { | |
88 | }; |
|
88 | }; | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | Cell.prototype.render = function () { |
|
91 | Cell.prototype.render = function () { | |
92 | }; |
|
92 | }; | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | Cell.prototype.toJSON = function () { |
|
95 | Cell.prototype.toJSON = function () { | |
96 | var data = {}; |
|
96 | var data = {}; | |
97 | data.metadata = this.metadata; |
|
97 | data.metadata = this.metadata; | |
98 | return data; |
|
98 | return data; | |
99 | }; |
|
99 | }; | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | Cell.prototype.fromJSON = function (data) { |
|
102 | Cell.prototype.fromJSON = function (data) { | |
103 | if (data.metadata !== undefined) { |
|
103 | if (data.metadata !== undefined) { | |
104 | this.metadata = data.metadata; |
|
104 | this.metadata = data.metadata; | |
105 | } |
|
105 | } | |
106 | }; |
|
106 | }; | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | Cell.prototype.is_splittable = function () { |
|
109 | Cell.prototype.is_splittable = function () { | |
110 | return true; |
|
110 | return true; | |
111 | }; |
|
111 | }; | |
112 |
|
112 | |||
113 |
|
113 | |||
114 | Cell.prototype.get_pre_cursor = function () { |
|
114 | Cell.prototype.get_pre_cursor = function () { | |
115 | var cursor = this.code_mirror.getCursor(); |
|
115 | var cursor = this.code_mirror.getCursor(); | |
116 | var text = this.code_mirror.getRange({line:0,ch:0}, cursor); |
|
116 | var text = this.code_mirror.getRange({line:0,ch:0}, cursor); | |
117 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
117 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
118 | return text; |
|
118 | return text; | |
119 | } |
|
119 | } | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | Cell.prototype.get_post_cursor = function () { |
|
122 | Cell.prototype.get_post_cursor = function () { | |
123 | var cursor = this.code_mirror.getCursor(); |
|
123 | var cursor = this.code_mirror.getCursor(); | |
124 | var last_line_num = this.code_mirror.lineCount()-1; |
|
124 | var last_line_num = this.code_mirror.lineCount()-1; | |
125 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
125 | var last_line_len = this.code_mirror.getLine(last_line_num).length; | |
126 | var end = {line:last_line_num, ch:last_line_len} |
|
126 | var end = {line:last_line_num, ch:last_line_len} | |
127 | var text = this.code_mirror.getRange(cursor, end); |
|
127 | var text = this.code_mirror.getRange(cursor, end); | |
128 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
128 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
129 | return text; |
|
129 | return text; | |
130 | }; |
|
130 | }; | |
131 |
|
131 | |||
132 |
|
132 | |||
133 | Cell.prototype.grow = function(element) { |
|
133 | Cell.prototype.grow = function(element) { | |
134 | // Grow the cell by hand. This is used upon reloading from JSON, when the |
|
134 | // Grow the cell by hand. This is used upon reloading from JSON, when the | |
135 | // autogrow handler is not called. |
|
135 | // autogrow handler is not called. | |
136 | var dom = element.get(0); |
|
136 | var dom = element.get(0); | |
137 | var lines_count = 0; |
|
137 | var lines_count = 0; | |
138 | // modified split rule from |
|
138 | // modified split rule from | |
139 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 |
|
139 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 | |
140 | var lines = dom.value.split(/\r|\r\n|\n/); |
|
140 | var lines = dom.value.split(/\r|\r\n|\n/); | |
141 | lines_count = lines.length; |
|
141 | lines_count = lines.length; | |
142 | if (lines_count >= 1) { |
|
142 | if (lines_count >= 1) { | |
143 | dom.rows = lines_count; |
|
143 | dom.rows = lines_count; | |
144 | } else { |
|
144 | } else { | |
145 | dom.rows = 1; |
|
145 | dom.rows = 1; | |
146 | } |
|
146 | } | |
147 | }; |
|
147 | }; | |
148 |
|
148 | |||
149 |
|
149 | |||
150 | Cell.prototype.toggle_line_numbers = function () { |
|
150 | Cell.prototype.toggle_line_numbers = function () { | |
151 | if (this.code_mirror.getOption('lineNumbers') == false) { |
|
151 | if (this.code_mirror.getOption('lineNumbers') == false) { | |
152 | this.code_mirror.setOption('lineNumbers', true); |
|
152 | this.code_mirror.setOption('lineNumbers', true); | |
153 | } else { |
|
153 | } else { | |
154 | this.code_mirror.setOption('lineNumbers', false); |
|
154 | this.code_mirror.setOption('lineNumbers', false); | |
155 | } |
|
155 | } | |
156 | this.code_mirror.refresh(); |
|
156 | this.code_mirror.refresh(); | |
157 | }; |
|
157 | }; | |
158 |
|
158 | |||
159 | Cell.prototype.force_highlight = function(mode) { |
|
159 | Cell.prototype.force_highlight = function(mode) { | |
160 | this.user_highlight = mode; |
|
160 | this.user_highlight = mode; | |
161 | this.auto_highlight(); |
|
161 | this.auto_highlight(); | |
162 | }; |
|
162 | }; | |
163 |
|
163 | |||
164 | Cell.prototype._auto_highlight = function (modes) { |
|
164 | Cell.prototype._auto_highlight = function (modes) { | |
165 | //Here we handle manually selected modes |
|
165 | //Here we handle manually selected modes | |
166 | if( this.user_highlight != undefined && this.user_highlight != 'auto' ) |
|
166 | if( this.user_highlight != undefined && this.user_highlight != 'auto' ) | |
167 | { |
|
167 | { | |
168 | var mode = this.user_highlight; |
|
168 | var mode = this.user_highlight; | |
169 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
169 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
170 | this.code_mirror.setOption('mode', mode); |
|
170 | this.code_mirror.setOption('mode', mode); | |
171 | return; |
|
171 | return; | |
172 | } |
|
172 | } | |
173 | var first_line = this.code_mirror.getLine(0); |
|
173 | var first_line = this.code_mirror.getLine(0); | |
174 | // loop on every pairs |
|
174 | // loop on every pairs | |
175 | for( var mode in modes) { |
|
175 | for( var mode in modes) { | |
176 | var regs = modes[mode]['reg']; |
|
176 | var regs = modes[mode]['reg']; | |
177 | // only one key every time but regexp can't be keys... |
|
177 | // only one key every time but regexp can't be keys... | |
178 | for(var reg in regs ) { |
|
178 | for(var reg in regs ) { | |
179 | // here we handle non magic_modes |
|
179 | // here we handle non magic_modes | |
180 | if(first_line.match(regs[reg]) != null) { |
|
180 | if(first_line.match(regs[reg]) != null) { | |
181 | if (mode.search('magic_') != 0) { |
|
181 | if (mode.search('magic_') != 0) { | |
182 | this.code_mirror.setOption('mode',mode); |
|
182 | this.code_mirror.setOption('mode',mode); | |
183 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
183 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
184 | return; |
|
184 | return; | |
185 | } |
|
185 | } | |
186 | var open = modes[mode]['open']|| "%%"; |
|
186 | var open = modes[mode]['open']|| "%%"; | |
187 | var close = modes[mode]['close']|| "%%end"; |
|
187 | var close = modes[mode]['close']|| "%%end"; | |
188 | var mmode = mode; |
|
188 | var mmode = mode; | |
189 | mode = mmode.substr(6); |
|
189 | mode = mmode.substr(6); | |
190 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
190 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
191 | // create on the fly a mode that swhitch between |
|
191 | // create on the fly a mode that swhitch between | |
192 | // plain/text and smth else otherwise `%%` is |
|
192 | // plain/text and smth else otherwise `%%` is | |
193 | // source of some highlight issues. |
|
193 | // source of some highlight issues. | |
194 | // we use patchedGetMode to circumvent a bug in CM |
|
194 | // we use patchedGetMode to circumvent a bug in CM | |
195 | CodeMirror.defineMode(mmode , function(config) { |
|
195 | CodeMirror.defineMode(mmode , function(config) { | |
196 | return CodeMirror.multiplexingMode( |
|
196 | return CodeMirror.multiplexingMode( | |
197 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
197 | CodeMirror.patchedGetMode(config, 'text/plain'), | |
198 | // always set someting on close |
|
198 | // always set someting on close | |
199 | {open: open, close: close, |
|
199 | {open: open, close: close, | |
200 | mode: CodeMirror.patchedGetMode(config, mode), |
|
200 | mode: CodeMirror.patchedGetMode(config, mode), | |
201 | delimStyle: "delimit" |
|
201 | delimStyle: "delimit" | |
202 | } |
|
202 | } | |
203 | ); |
|
203 | ); | |
204 | }); |
|
204 | }); | |
205 | this.code_mirror.setOption('mode', mmode); |
|
205 | this.code_mirror.setOption('mode', mmode); | |
206 | return; |
|
206 | return; | |
207 | } |
|
207 | } | |
208 | } |
|
208 | } | |
209 | } |
|
209 | } | |
210 | // fallback on default (python) |
|
210 | // fallback on default (python) | |
211 | var default_mode = this.default_mode || 'text/plain'; |
|
211 | var default_mode = this.default_mode || 'text/plain'; | |
212 | this.code_mirror.setOption('mode', default_mode); |
|
212 | this.code_mirror.setOption('mode', default_mode); | |
213 | }; |
|
213 | }; | |
214 |
|
214 | |||
215 | IPython.Cell = Cell; |
|
215 | IPython.Cell = Cell; | |
216 |
|
216 | |||
217 | return IPython; |
|
217 | return IPython; | |
218 |
|
218 | |||
219 | }(IPython)); |
|
219 | }(IPython)); | |
220 |
|
220 |
@@ -1,322 +1,322 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 | // CodeCell |
|
9 | // CodeCell | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | var IPython = (function (IPython) { |
|
12 | var IPython = (function (IPython) { | |
13 | "use strict"; |
|
13 | "use strict"; | |
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 | CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js"; | |
18 |
|
18 | |||
19 | var CodeCell = function (kernel) { |
|
19 | var CodeCell = function (kernel) { | |
20 | // 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 | |
21 | // it will be null and set_kernel has to be called later. |
|
21 | // it will be null and set_kernel has to be called later. | |
22 | this.kernel = kernel || null; |
|
22 | this.kernel = kernel || null; | |
23 | this.code_mirror = null; |
|
23 | this.code_mirror = null; | |
24 | this.input_prompt_number = null; |
|
24 | this.input_prompt_number = null; | |
25 | this.tooltip_on_tab = true; |
|
25 | this.tooltip_on_tab = true; | |
26 | this.collapsed = false; |
|
26 | this.collapsed = false; | |
27 | this.default_mode = 'python'; |
|
27 | this.default_mode = 'python'; | |
28 | IPython.Cell.apply(this, arguments); |
|
28 | IPython.Cell.apply(this, arguments); | |
29 |
|
29 | |||
30 | var that = this; |
|
30 | var that = this; | |
31 | this.element.focusout( |
|
31 | this.element.focusout( | |
32 |
|
|
32 | function() { that.auto_highlight(); } | |
33 |
|
|
33 | ); | |
34 | }; |
|
34 | }; | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | CodeCell.prototype = new IPython.Cell(); |
|
37 | CodeCell.prototype = new IPython.Cell(); | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | CodeCell.prototype.auto_highlight = function () { |
|
40 | CodeCell.prototype.auto_highlight = function () { | |
41 | this._auto_highlight(IPython.config.cell_magic_highlight) |
|
41 | this._auto_highlight(IPython.config.cell_magic_highlight) | |
42 | }; |
|
42 | }; | |
43 |
|
43 | |||
44 | CodeCell.prototype.create_element = function () { |
|
44 | CodeCell.prototype.create_element = function () { | |
45 | 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'); | |
46 | cell.attr('tabindex','2'); |
|
46 | cell.attr('tabindex','2'); | |
47 | var input = $('<div></div>').addClass('input hbox'); |
|
47 | var input = $('<div></div>').addClass('input hbox'); | |
48 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
48 | input.append($('<div/>').addClass('prompt input_prompt')); | |
49 | var input_area = $('<div/>').addClass('input_area box-flex1'); |
|
49 | var input_area = $('<div/>').addClass('input_area box-flex1'); | |
50 | this.code_mirror = CodeMirror(input_area.get(0), { |
|
50 | this.code_mirror = CodeMirror(input_area.get(0), { | |
51 | indentUnit : 4, |
|
51 | indentUnit : 4, | |
52 | mode: 'python', |
|
52 | mode: 'python', | |
53 | theme: 'ipython', |
|
53 | theme: 'ipython', | |
54 | readOnly: this.read_only, |
|
54 | readOnly: this.read_only, | |
55 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"}, |
|
55 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"}, | |
56 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
56 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
57 | }); |
|
57 | }); | |
58 | input.append(input_area); |
|
58 | input.append(input_area); | |
59 | var output = $('<div></div>'); |
|
59 | var output = $('<div></div>'); | |
60 | cell.append(input).append(output); |
|
60 | cell.append(input).append(output); | |
61 | this.element = cell; |
|
61 | this.element = cell; | |
62 | this.output_area = new IPython.OutputArea(output, true); |
|
62 | this.output_area = new IPython.OutputArea(output, true); | |
63 |
|
63 | |||
64 | // construct a completer only if class exist |
|
64 | // construct a completer only if class exist | |
65 | // otherwise no print view |
|
65 | // otherwise no print view | |
66 | if (IPython.Completer !== undefined) |
|
66 | if (IPython.Completer !== undefined) | |
67 | { |
|
67 | { | |
68 | this.completer = new IPython.Completer(this); |
|
68 | this.completer = new IPython.Completer(this); | |
69 | } |
|
69 | } | |
70 | }; |
|
70 | }; | |
71 |
|
71 | |||
72 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
72 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
73 | // This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
73 | // This method gets called in CodeMirror's onKeyDown/onKeyPress | |
74 | // handlers and is used to provide custom key handling. Its return |
|
74 | // handlers and is used to provide custom key handling. Its return | |
75 | // value is used to determine if CodeMirror should ignore the event: |
|
75 | // value is used to determine if CodeMirror should ignore the event: | |
76 | // true = ignore, false = don't ignore. |
|
76 | // true = ignore, false = don't ignore. | |
77 |
|
77 | |||
78 | if (this.read_only){ |
|
78 | if (this.read_only){ | |
79 | return false; |
|
79 | return false; | |
80 | } |
|
80 | } | |
81 |
|
81 | |||
82 | var that = this; |
|
82 | var that = this; | |
83 | // whatever key is pressed, first, cancel the tooltip request before |
|
83 | // whatever key is pressed, first, cancel the tooltip request before | |
84 | // they are sent, and remove tooltip if any, except for tab again |
|
84 | // they are sent, and remove tooltip if any, except for tab again | |
85 | if (event.type === 'keydown' && event.which != key.TAB ) { |
|
85 | if (event.type === 'keydown' && event.which != key.TAB ) { | |
86 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
86 | IPython.tooltip.remove_and_cancel_tooltip(); | |
87 | }; |
|
87 | }; | |
88 |
|
88 | |||
89 | var cur = editor.getCursor(); |
|
89 | var cur = editor.getCursor(); | |
90 | if (event.keyCode === key.ENTER){ |
|
90 | if (event.keyCode === key.ENTER){ | |
91 | this.auto_highlight(); |
|
91 | this.auto_highlight(); | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { |
|
94 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { | |
95 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
95 | // Always ignore shift-enter in CodeMirror as we handle it. | |
96 | return true; |
|
96 | return true; | |
97 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
|
97 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | |
98 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
98 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform | |
99 | // browser and keyboard layout ! |
|
99 | // browser and keyboard layout ! | |
100 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
100 | // Pressing '(' , request tooltip, don't forget to reappend it | |
101 | IPython.tooltip.pending(that); |
|
101 | IPython.tooltip.pending(that); | |
102 | } else if (event.which === key.UPARROW && event.type === 'keydown') { |
|
102 | } else if (event.which === key.UPARROW && event.type === 'keydown') { | |
103 | // If we are not at the top, let CM handle the up arrow and |
|
103 | // If we are not at the top, let CM handle the up arrow and | |
104 | // prevent the global keydown handler from handling it. |
|
104 | // prevent the global keydown handler from handling it. | |
105 | if (!that.at_top()) { |
|
105 | if (!that.at_top()) { | |
106 | event.stop(); |
|
106 | event.stop(); | |
107 | return false; |
|
107 | return false; | |
108 | } else { |
|
108 | } else { | |
109 | return true; |
|
109 | return true; | |
110 | }; |
|
110 | }; | |
111 | } else if (event.which === key.ESC) { |
|
111 | } else if (event.which === key.ESC) { | |
112 | IPython.tooltip.remove_and_cancel_tooltip(true); |
|
112 | IPython.tooltip.remove_and_cancel_tooltip(true); | |
113 | return true; |
|
113 | return true; | |
114 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
114 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
115 | // If we are not at the bottom, let CM handle the down arrow and |
|
115 | // If we are not at the bottom, let CM handle the down arrow and | |
116 | // prevent the global keydown handler from handling it. |
|
116 | // prevent the global keydown handler from handling it. | |
117 | if (!that.at_bottom()) { |
|
117 | if (!that.at_bottom()) { | |
118 | event.stop(); |
|
118 | event.stop(); | |
119 | return false; |
|
119 | return false; | |
120 | } else { |
|
120 | } else { | |
121 | return true; |
|
121 | return true; | |
122 | }; |
|
122 | }; | |
123 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { |
|
123 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { | |
124 | // Tab completion. |
|
124 | // Tab completion. | |
125 | //Do not trim here because of tooltip |
|
125 | //Do not trim here because of tooltip | |
126 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
126 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | |
127 | if (pre_cursor.trim() === "") { |
|
127 | if (pre_cursor.trim() === "") { | |
128 | // Don't autocomplete if the part of the line before the cursor |
|
128 | // Don't autocomplete if the part of the line before the cursor | |
129 | // is empty. In this case, let CodeMirror handle indentation. |
|
129 | // is empty. In this case, let CodeMirror handle indentation. | |
130 | return false; |
|
130 | return false; | |
131 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) { |
|
131 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) { | |
132 | IPython.tooltip.request(that); |
|
132 | IPython.tooltip.request(that); | |
133 | // Prevent the event from bubbling up. |
|
133 | // Prevent the event from bubbling up. | |
134 | event.stop(); |
|
134 | event.stop(); | |
135 | // Prevent CodeMirror from handling the tab. |
|
135 | // Prevent CodeMirror from handling the tab. | |
136 | return true; |
|
136 | return true; | |
137 | } else { |
|
137 | } else { | |
138 | event.stop(); |
|
138 | event.stop(); | |
139 | this.completer.startCompletion(); |
|
139 | this.completer.startCompletion(); | |
140 | return true; |
|
140 | return true; | |
141 | }; |
|
141 | }; | |
142 | } else { |
|
142 | } else { | |
143 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
143 | // keypress/keyup also trigger on TAB press, and we don't want to | |
144 | // use those to disable tab completion. |
|
144 | // use those to disable tab completion. | |
145 | return false; |
|
145 | return false; | |
146 | }; |
|
146 | }; | |
147 | return false; |
|
147 | return false; | |
148 | }; |
|
148 | }; | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | // Kernel related calls. |
|
151 | // Kernel related calls. | |
152 |
|
152 | |||
153 | CodeCell.prototype.set_kernel = function (kernel) { |
|
153 | CodeCell.prototype.set_kernel = function (kernel) { | |
154 | this.kernel = kernel; |
|
154 | this.kernel = kernel; | |
155 | } |
|
155 | } | |
156 |
|
156 | |||
157 |
|
157 | |||
158 | CodeCell.prototype.execute = function () { |
|
158 | CodeCell.prototype.execute = function () { | |
159 | this.output_area.clear_output(true, true, true); |
|
159 | this.output_area.clear_output(true, true, true); | |
160 | this.set_input_prompt('*'); |
|
160 | this.set_input_prompt('*'); | |
161 | this.element.addClass("running"); |
|
161 | this.element.addClass("running"); | |
162 | var callbacks = { |
|
162 | var callbacks = { | |
163 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
163 | 'execute_reply': $.proxy(this._handle_execute_reply, this), | |
164 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
164 | 'output': $.proxy(this.output_area.handle_output, this.output_area), | |
165 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
165 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), | |
166 | 'set_next_input': $.proxy(this._handle_set_next_input, this) |
|
166 | 'set_next_input': $.proxy(this._handle_set_next_input, this) | |
167 | }; |
|
167 | }; | |
168 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); |
|
168 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); | |
169 | }; |
|
169 | }; | |
170 |
|
170 | |||
171 |
|
171 | |||
172 | CodeCell.prototype._handle_execute_reply = function (content) { |
|
172 | CodeCell.prototype._handle_execute_reply = function (content) { | |
173 | this.set_input_prompt(content.execution_count); |
|
173 | this.set_input_prompt(content.execution_count); | |
174 | this.element.removeClass("running"); |
|
174 | this.element.removeClass("running"); | |
175 | $([IPython.events]).trigger('set_dirty.Notebook', {'value': true}); |
|
175 | $([IPython.events]).trigger('set_dirty.Notebook', {'value': true}); | |
176 | } |
|
176 | } | |
177 |
|
177 | |||
178 | CodeCell.prototype._handle_set_next_input = function (text) { |
|
178 | CodeCell.prototype._handle_set_next_input = function (text) { | |
179 | var data = {'cell': this, 'text': text} |
|
179 | var data = {'cell': this, 'text': text} | |
180 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
180 | $([IPython.events]).trigger('set_next_input.Notebook', data); | |
181 | } |
|
181 | } | |
182 |
|
182 | |||
183 | // Basic cell manipulation. |
|
183 | // Basic cell manipulation. | |
184 |
|
184 | |||
185 | CodeCell.prototype.select = function () { |
|
185 | CodeCell.prototype.select = function () { | |
186 | IPython.Cell.prototype.select.apply(this); |
|
186 | IPython.Cell.prototype.select.apply(this); | |
187 | this.code_mirror.refresh(); |
|
187 | this.code_mirror.refresh(); | |
188 | this.code_mirror.focus(); |
|
188 | this.code_mirror.focus(); | |
189 | this.auto_highlight(); |
|
189 | this.auto_highlight(); | |
190 | // We used to need an additional refresh() after the focus, but |
|
190 | // We used to need an additional refresh() after the focus, but | |
191 | // 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 | |
192 | // up on FF when a newly loaded markdown cell was edited. |
|
192 | // up on FF when a newly loaded markdown cell was edited. | |
193 | }; |
|
193 | }; | |
194 |
|
194 | |||
195 |
|
195 | |||
196 | CodeCell.prototype.select_all = function () { |
|
196 | CodeCell.prototype.select_all = function () { | |
197 | var start = {line: 0, ch: 0}; |
|
197 | var start = {line: 0, ch: 0}; | |
198 | var nlines = this.code_mirror.lineCount(); |
|
198 | var nlines = this.code_mirror.lineCount(); | |
199 | var last_line = this.code_mirror.getLine(nlines-1); |
|
199 | var last_line = this.code_mirror.getLine(nlines-1); | |
200 | var end = {line: nlines-1, ch: last_line.length}; |
|
200 | var end = {line: nlines-1, ch: last_line.length}; | |
201 | this.code_mirror.setSelection(start, end); |
|
201 | this.code_mirror.setSelection(start, end); | |
202 | }; |
|
202 | }; | |
203 |
|
203 | |||
204 |
|
204 | |||
205 | CodeCell.prototype.collapse = function () { |
|
205 | CodeCell.prototype.collapse = function () { | |
206 | this.collapsed = true; |
|
206 | this.collapsed = true; | |
207 | this.output_area.collapse(); |
|
207 | this.output_area.collapse(); | |
208 | }; |
|
208 | }; | |
209 |
|
209 | |||
210 |
|
210 | |||
211 | CodeCell.prototype.expand = function () { |
|
211 | CodeCell.prototype.expand = function () { | |
212 | this.collapsed = false; |
|
212 | this.collapsed = false; | |
213 | this.output_area.expand(); |
|
213 | this.output_area.expand(); | |
214 | }; |
|
214 | }; | |
215 |
|
215 | |||
216 |
|
216 | |||
217 | CodeCell.prototype.toggle_output = function () { |
|
217 | CodeCell.prototype.toggle_output = function () { | |
218 | this.collapsed = Boolean(1 - this.collapsed); |
|
218 | this.collapsed = Boolean(1 - this.collapsed); | |
219 | this.output_area.toggle_output(); |
|
219 | this.output_area.toggle_output(); | |
220 | }; |
|
220 | }; | |
221 |
|
221 | |||
222 |
|
222 | |||
223 | CodeCell.prototype.toggle_output_scroll = function () { |
|
223 | CodeCell.prototype.toggle_output_scroll = function () { | |
224 | this.output_area.toggle_scroll(); |
|
224 | this.output_area.toggle_scroll(); | |
225 | }; |
|
225 | }; | |
226 |
|
226 | |||
227 |
|
227 | |||
228 | CodeCell.prototype.set_input_prompt = function (number) { |
|
228 | CodeCell.prototype.set_input_prompt = function (number) { | |
229 | this.input_prompt_number = number; |
|
229 | this.input_prompt_number = number; | |
230 | var ns = number || " "; |
|
230 | var ns = number || " "; | |
231 | this.element.find('div.input_prompt').html('In [' + ns + ']:'); |
|
231 | this.element.find('div.input_prompt').html('In [' + ns + ']:'); | |
232 | }; |
|
232 | }; | |
233 |
|
233 | |||
234 |
|
234 | |||
235 | CodeCell.prototype.clear_input = function () { |
|
235 | CodeCell.prototype.clear_input = function () { | |
236 | this.code_mirror.setValue(''); |
|
236 | this.code_mirror.setValue(''); | |
237 | }; |
|
237 | }; | |
238 |
|
238 | |||
239 |
|
239 | |||
240 | CodeCell.prototype.get_text = function () { |
|
240 | CodeCell.prototype.get_text = function () { | |
241 | return this.code_mirror.getValue(); |
|
241 | return this.code_mirror.getValue(); | |
242 | }; |
|
242 | }; | |
243 |
|
243 | |||
244 |
|
244 | |||
245 | CodeCell.prototype.set_text = function (code) { |
|
245 | CodeCell.prototype.set_text = function (code) { | |
246 | return this.code_mirror.setValue(code); |
|
246 | return this.code_mirror.setValue(code); | |
247 | }; |
|
247 | }; | |
248 |
|
248 | |||
249 |
|
249 | |||
250 | CodeCell.prototype.at_top = function () { |
|
250 | CodeCell.prototype.at_top = function () { | |
251 | var cursor = this.code_mirror.getCursor(); |
|
251 | var cursor = this.code_mirror.getCursor(); | |
252 | if (cursor.line === 0) { |
|
252 | if (cursor.line === 0) { | |
253 | return true; |
|
253 | return true; | |
254 | } else { |
|
254 | } else { | |
255 | return false; |
|
255 | return false; | |
256 | } |
|
256 | } | |
257 | }; |
|
257 | }; | |
258 |
|
258 | |||
259 |
|
259 | |||
260 | CodeCell.prototype.at_bottom = function () { |
|
260 | CodeCell.prototype.at_bottom = function () { | |
261 | var cursor = this.code_mirror.getCursor(); |
|
261 | var cursor = this.code_mirror.getCursor(); | |
262 | if (cursor.line === (this.code_mirror.lineCount()-1)) { |
|
262 | if (cursor.line === (this.code_mirror.lineCount()-1)) { | |
263 | return true; |
|
263 | return true; | |
264 | } else { |
|
264 | } else { | |
265 | return false; |
|
265 | return false; | |
266 | } |
|
266 | } | |
267 | }; |
|
267 | }; | |
268 |
|
268 | |||
269 |
|
269 | |||
270 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
270 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { | |
271 | this.output_area.clear_output(stdout, stderr, other); |
|
271 | this.output_area.clear_output(stdout, stderr, other); | |
272 | }; |
|
272 | }; | |
273 |
|
273 | |||
274 |
|
274 | |||
275 | // JSON serialization |
|
275 | // JSON serialization | |
276 |
|
276 | |||
277 | CodeCell.prototype.fromJSON = function (data) { |
|
277 | CodeCell.prototype.fromJSON = function (data) { | |
278 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
278 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
279 | if (data.cell_type === 'code') { |
|
279 | if (data.cell_type === 'code') { | |
280 | if (data.input !== undefined) { |
|
280 | if (data.input !== undefined) { | |
281 | this.set_text(data.input); |
|
281 | this.set_text(data.input); | |
282 | // 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 | |
283 | // to this state, instead of a blank cell |
|
283 | // to this state, instead of a blank cell | |
284 | this.code_mirror.clearHistory(); |
|
284 | this.code_mirror.clearHistory(); | |
285 | this.auto_highlight(); |
|
285 | this.auto_highlight(); | |
286 | } |
|
286 | } | |
287 | if (data.prompt_number !== undefined) { |
|
287 | if (data.prompt_number !== undefined) { | |
288 | this.set_input_prompt(data.prompt_number); |
|
288 | this.set_input_prompt(data.prompt_number); | |
289 | } else { |
|
289 | } else { | |
290 | this.set_input_prompt(); |
|
290 | this.set_input_prompt(); | |
291 | }; |
|
291 | }; | |
292 | this.output_area.fromJSON(data.outputs); |
|
292 | this.output_area.fromJSON(data.outputs); | |
293 | if (data.collapsed !== undefined) { |
|
293 | if (data.collapsed !== undefined) { | |
294 | if (data.collapsed) { |
|
294 | if (data.collapsed) { | |
295 | this.collapse(); |
|
295 | this.collapse(); | |
296 | } else { |
|
296 | } else { | |
297 | this.expand(); |
|
297 | this.expand(); | |
298 | }; |
|
298 | }; | |
299 | }; |
|
299 | }; | |
300 | }; |
|
300 | }; | |
301 | }; |
|
301 | }; | |
302 |
|
302 | |||
303 |
|
303 | |||
304 | CodeCell.prototype.toJSON = function () { |
|
304 | CodeCell.prototype.toJSON = function () { | |
305 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
305 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
306 | data.input = this.get_text(); |
|
306 | data.input = this.get_text(); | |
307 | data.cell_type = 'code'; |
|
307 | data.cell_type = 'code'; | |
308 | if (this.input_prompt_number) { |
|
308 | if (this.input_prompt_number) { | |
309 | data.prompt_number = this.input_prompt_number; |
|
309 | data.prompt_number = this.input_prompt_number; | |
310 | }; |
|
310 | }; | |
311 | var outputs = this.output_area.toJSON(); |
|
311 | var outputs = this.output_area.toJSON(); | |
312 | data.outputs = outputs; |
|
312 | data.outputs = outputs; | |
313 | data.language = 'python'; |
|
313 | data.language = 'python'; | |
314 | data.collapsed = this.collapsed; |
|
314 | data.collapsed = this.collapsed; | |
315 | return data; |
|
315 | return data; | |
316 | }; |
|
316 | }; | |
317 |
|
317 | |||
318 |
|
318 | |||
319 | IPython.CodeCell = CodeCell; |
|
319 | IPython.CodeCell = CodeCell; | |
320 |
|
320 | |||
321 | return IPython; |
|
321 | return IPython; | |
322 | }(IPython)); |
|
322 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now