Show More
@@ -1,336 +1,341 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // Cell |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule Cell |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | var IPython = (function (IPython) { |
|
19 | 19 | "use strict"; |
|
20 | 20 | |
|
21 | 21 | var utils = IPython.utils; |
|
22 | 22 | |
|
23 | 23 | /** |
|
24 | 24 | * The Base `Cell` class from which to inherit |
|
25 | 25 | * @class Cell |
|
26 | 26 | **/ |
|
27 | 27 | |
|
28 | 28 | /* |
|
29 | 29 | * @constructor |
|
30 | 30 | * |
|
31 | 31 | * @param {object|undefined} [options] |
|
32 | 32 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters |
|
33 | 33 | */ |
|
34 | 34 | var Cell = function (options) { |
|
35 | 35 | |
|
36 | 36 | options = this.mergeopt(Cell, options) |
|
37 | 37 | // superclass default overwrite our default |
|
38 | 38 | |
|
39 | 39 | this.placeholder = options.placeholder || ''; |
|
40 | 40 | this.read_only = options.cm_config.readOnly; |
|
41 | 41 | this.selected = false; |
|
42 | 42 | this.element = null; |
|
43 | 43 | this.metadata = {}; |
|
44 | 44 | // load this from metadata later ? |
|
45 | 45 | this.user_highlight = 'auto'; |
|
46 | 46 | this.cm_config = options.cm_config; |
|
47 | 47 | this.create_element(); |
|
48 | 48 | if (this.element !== null) { |
|
49 | 49 | this.element.data("cell", this); |
|
50 | 50 | this.bind_events(); |
|
51 | 51 | } |
|
52 | 52 | this.cell_id = utils.uuid(); |
|
53 | 53 | this._options = options; |
|
54 | 54 | }; |
|
55 | 55 | |
|
56 | 56 | Cell.options_default = { |
|
57 | 57 | cm_config : { |
|
58 | 58 | indentUnit : 4, |
|
59 | 59 | readOnly: false, |
|
60 | 60 | theme: "default" |
|
61 | 61 | } |
|
62 | 62 | }; |
|
63 | 63 | |
|
64 | 64 | // FIXME: Workaround CM Bug #332 (Safari segfault on drag) |
|
65 | 65 | // by disabling drag/drop altogether on Safari |
|
66 | 66 | // https://github.com/marijnh/CodeMirror/issues/332 |
|
67 | 67 | |
|
68 | 68 | if (utils.browser[0] == "Safari") { |
|
69 | 69 | Cell.options_default.cm_config.dragDrop = false; |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | Cell.prototype.mergeopt = function(_class, options, overwrite){ |
|
73 | 73 | overwrite = overwrite || {}; |
|
74 | 74 | return $.extend(true, {}, _class.options_default, options, overwrite) |
|
75 | 75 | |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | /** |
|
81 | 81 | * Empty. Subclasses must implement create_element. |
|
82 | 82 | * This should contain all the code to create the DOM element in notebook |
|
83 | 83 | * and will be called by Base Class constructor. |
|
84 | 84 | * @method create_element |
|
85 | 85 | */ |
|
86 | 86 | Cell.prototype.create_element = function () { |
|
87 | 87 | }; |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | /** |
|
91 | 91 | * Subclasses can implement override bind_events. |
|
92 | 92 | * Be carefull to call the parent method when overwriting as it fires event. |
|
93 | 93 | * this will be triggerd after create_element in constructor. |
|
94 | 94 | * @method bind_events |
|
95 | 95 | */ |
|
96 | 96 | Cell.prototype.bind_events = function () { |
|
97 | 97 | var that = this; |
|
98 | 98 | // We trigger events so that Cell doesn't have to depend on Notebook. |
|
99 | 99 | that.element.click(function (event) { |
|
100 | 100 | if (that.selected === false) { |
|
101 | 101 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
102 | 102 | } |
|
103 | 103 | }); |
|
104 | 104 | that.element.focusin(function (event) { |
|
105 | 105 | if (that.selected === false) { |
|
106 | 106 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
107 | 107 | } |
|
108 | 108 | }); |
|
109 | 109 | if (this.code_mirror) { |
|
110 | 110 | this.code_mirror.on("change", function(cm, change) { |
|
111 | 111 | $([IPython.events]).trigger("set_dirty.Notebook", {value: true}); |
|
112 | 112 | }); |
|
113 | 113 | } |
|
114 | 114 | }; |
|
115 | 115 | |
|
116 | 116 | /** |
|
117 | 117 | * Triger typsetting of math by mathjax on current cell element |
|
118 | 118 | * @method typeset |
|
119 | 119 | */ |
|
120 | 120 | Cell.prototype.typeset = function () { |
|
121 | 121 | if (window.MathJax){ |
|
122 | 122 | var cell_math = this.element.get(0); |
|
123 | 123 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); |
|
124 | 124 | } |
|
125 | 125 | }; |
|
126 | 126 | |
|
127 | 127 | /** |
|
128 | 128 | * should be triggerd when cell is selected |
|
129 | 129 | * @method select |
|
130 | 130 | */ |
|
131 | 131 | Cell.prototype.select = function () { |
|
132 | 132 | this.element.addClass('selected'); |
|
133 | 133 | this.selected = true; |
|
134 | 134 | }; |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | /** |
|
138 | 138 | * should be triggerd when cell is unselected |
|
139 | 139 | * @method unselect |
|
140 | 140 | */ |
|
141 | 141 | Cell.prototype.unselect = function () { |
|
142 | 142 | this.element.removeClass('selected'); |
|
143 | 143 | this.selected = false; |
|
144 | 144 | }; |
|
145 | 145 | |
|
146 | 146 | /** |
|
147 | 147 | * should be overritten by subclass |
|
148 | 148 | * @method get_text |
|
149 | 149 | */ |
|
150 | 150 | Cell.prototype.get_text = function () { |
|
151 | 151 | }; |
|
152 | 152 | |
|
153 | 153 | /** |
|
154 | 154 | * should be overritten by subclass |
|
155 | 155 | * @method set_text |
|
156 | 156 | * @param {string} text |
|
157 | 157 | */ |
|
158 | 158 | Cell.prototype.set_text = function (text) { |
|
159 | 159 | }; |
|
160 | 160 | |
|
161 | 161 | /** |
|
162 | 162 | * Refresh codemirror instance |
|
163 | 163 | * @method refresh |
|
164 | 164 | */ |
|
165 | 165 | Cell.prototype.refresh = function () { |
|
166 | 166 | this.code_mirror.refresh(); |
|
167 | 167 | }; |
|
168 | 168 | |
|
169 | 169 | |
|
170 | 170 | /** |
|
171 | 171 | * should be overritten by subclass |
|
172 | 172 | * @method edit |
|
173 | 173 | **/ |
|
174 | 174 | Cell.prototype.edit = function () { |
|
175 | 175 | }; |
|
176 | 176 | |
|
177 | 177 | |
|
178 | 178 | /** |
|
179 | 179 | * should be overritten by subclass |
|
180 | 180 | * @method render |
|
181 | 181 | **/ |
|
182 | 182 | Cell.prototype.render = function () { |
|
183 | 183 | }; |
|
184 | 184 | |
|
185 | 185 | /** |
|
186 | 186 | * should be overritten by subclass |
|
187 | 187 | * serialise cell to json. |
|
188 | 188 | * @method toJSON |
|
189 | 189 | **/ |
|
190 | 190 | Cell.prototype.toJSON = function () { |
|
191 | 191 | var data = {}; |
|
192 | 192 | data.metadata = this.metadata; |
|
193 | 193 | return data; |
|
194 | 194 | }; |
|
195 | 195 | |
|
196 | 196 | |
|
197 | 197 | /** |
|
198 | 198 | * should be overritten by subclass |
|
199 | 199 | * @method fromJSON |
|
200 | 200 | **/ |
|
201 | 201 | Cell.prototype.fromJSON = function (data) { |
|
202 | 202 | if (data.metadata !== undefined) { |
|
203 | 203 | this.metadata = data.metadata; |
|
204 | 204 | } |
|
205 | 205 | this.celltoolbar.rebuild(); |
|
206 | 206 | }; |
|
207 | 207 | |
|
208 | 208 | |
|
209 | 209 | /** |
|
210 | 210 | * can the cell be splitted in 2 cells. |
|
211 | 211 | * @method is_splittable |
|
212 | 212 | **/ |
|
213 | 213 | Cell.prototype.is_splittable = function () { |
|
214 | 214 | return true; |
|
215 | 215 | }; |
|
216 | 216 | |
|
217 | 217 | |
|
218 | 218 | /** |
|
219 | 219 | * @return {String} - the text before the cursor |
|
220 | 220 | * @method get_pre_cursor |
|
221 | 221 | **/ |
|
222 | 222 | Cell.prototype.get_pre_cursor = function () { |
|
223 | 223 | var cursor = this.code_mirror.getCursor(); |
|
224 | 224 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); |
|
225 | 225 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
226 | 226 | return text; |
|
227 | 227 | } |
|
228 | 228 | |
|
229 | 229 | |
|
230 | 230 | /** |
|
231 | 231 | * @return {String} - the text after the cursor |
|
232 | 232 | * @method get_post_cursor |
|
233 | 233 | **/ |
|
234 | 234 | Cell.prototype.get_post_cursor = function () { |
|
235 | 235 | var cursor = this.code_mirror.getCursor(); |
|
236 | 236 | var last_line_num = this.code_mirror.lineCount()-1; |
|
237 | 237 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
238 | 238 | var end = {line:last_line_num, ch:last_line_len} |
|
239 | 239 | var text = this.code_mirror.getRange(cursor, end); |
|
240 | 240 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
241 | 241 | return text; |
|
242 | 242 | }; |
|
243 | 243 | |
|
244 | 244 | /** |
|
245 | 245 | * Show/Hide CodeMirror LineNumber |
|
246 | 246 | * @method show_line_numbers |
|
247 | 247 | * |
|
248 | 248 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror |
|
249 | 249 | **/ |
|
250 | 250 | Cell.prototype.show_line_numbers = function (value) { |
|
251 | 251 | this.code_mirror.setOption('lineNumbers', value); |
|
252 | 252 | this.code_mirror.refresh(); |
|
253 | 253 | }; |
|
254 | 254 | |
|
255 | 255 | /** |
|
256 | 256 | * Toggle CodeMirror LineNumber |
|
257 | 257 | * @method toggle_line_numbers |
|
258 | 258 | **/ |
|
259 | 259 | Cell.prototype.toggle_line_numbers = function () { |
|
260 | 260 | var val = this.code_mirror.getOption('lineNumbers'); |
|
261 | 261 | this.show_line_numbers(!val); |
|
262 | 262 | }; |
|
263 | 263 | |
|
264 | 264 | /** |
|
265 | 265 | * Force codemirror highlight mode |
|
266 | 266 | * @method force_highlight |
|
267 | 267 | * @param {object} - CodeMirror mode |
|
268 | 268 | **/ |
|
269 | 269 | Cell.prototype.force_highlight = function(mode) { |
|
270 | 270 | this.user_highlight = mode; |
|
271 | 271 | this.auto_highlight(); |
|
272 | 272 | }; |
|
273 | 273 | |
|
274 | 274 | /** |
|
275 | 275 | * Try to autodetect cell highlight mode, or use selected mode |
|
276 | 276 | * @methods _auto_highlight |
|
277 | 277 | * @private |
|
278 | 278 | * @param {String|object|undefined} - CodeMirror mode | 'auto' |
|
279 | 279 | **/ |
|
280 | 280 | Cell.prototype._auto_highlight = function (modes) { |
|
281 | 281 | //Here we handle manually selected modes |
|
282 | 282 | if( this.user_highlight != undefined && this.user_highlight != 'auto' ) |
|
283 | 283 | { |
|
284 | 284 | var mode = this.user_highlight; |
|
285 | 285 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
286 | 286 | this.code_mirror.setOption('mode', mode); |
|
287 | 287 | return; |
|
288 | 288 | } |
|
289 | 289 | var first_line = this.code_mirror.getLine(0); |
|
290 | 290 | // loop on every pairs |
|
291 | 291 | for( var mode in modes) { |
|
292 | 292 | var regs = modes[mode]['reg']; |
|
293 | 293 | // only one key every time but regexp can't be keys... |
|
294 | 294 | for(var reg in regs ) { |
|
295 | 295 | // here we handle non magic_modes |
|
296 | 296 | if(first_line.match(regs[reg]) != null) { |
|
297 | 297 | if (mode.search('magic_') != 0) { |
|
298 | 298 | this.code_mirror.setOption('mode', mode); |
|
299 | 299 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
300 | 300 | return; |
|
301 | 301 | } |
|
302 | 302 | var open = modes[mode]['open']|| "%%"; |
|
303 | 303 | var close = modes[mode]['close']|| "%%end"; |
|
304 | 304 | var mmode = mode; |
|
305 | 305 | mode = mmode.substr(6); |
|
306 | 306 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
307 | 307 | // create on the fly a mode that swhitch between |
|
308 | 308 | // plain/text and smth else otherwise `%%` is |
|
309 | 309 | // source of some highlight issues. |
|
310 | 310 | // we use patchedGetMode to circumvent a bug in CM |
|
311 | 311 | CodeMirror.defineMode(mmode , function(config) { |
|
312 | 312 | return CodeMirror.multiplexingMode( |
|
313 | 313 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
314 | 314 | // always set someting on close |
|
315 | 315 | {open: open, close: close, |
|
316 | 316 | mode: CodeMirror.patchedGetMode(config, mode), |
|
317 | 317 | delimStyle: "delimit" |
|
318 | 318 | } |
|
319 | 319 | ); |
|
320 | 320 | }); |
|
321 | 321 | this.code_mirror.setOption('mode', mmode); |
|
322 | 322 | return; |
|
323 | 323 | } |
|
324 | 324 | } |
|
325 | 325 | } |
|
326 |
// fallback on default |
|
|
327 | var default_mode = this.default_mode || 'text/plain'; | |
|
326 | // fallback on default | |
|
327 | var default_mode | |
|
328 | try { | |
|
329 | default_mode = this._options.cm_config.mode; | |
|
330 | } catch(e) { | |
|
331 | default_mode = 'text/plain'; | |
|
332 | } | |
|
328 | 333 | this.code_mirror.setOption('mode', default_mode); |
|
329 | 334 | }; |
|
330 | 335 | |
|
331 | 336 | IPython.Cell = Cell; |
|
332 | 337 | |
|
333 | 338 | return IPython; |
|
334 | 339 | |
|
335 | 340 | }(IPython)); |
|
336 | 341 |
@@ -1,443 +1,442 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // CodeCell |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule CodeCell |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | /* local util for codemirror */ |
|
20 | 20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} |
|
21 | 21 | |
|
22 | 22 | /** |
|
23 | 23 | * |
|
24 | 24 | * function to delete until previous non blanking space character |
|
25 | 25 | * or first multiple of 4 tabstop. |
|
26 | 26 | * @private |
|
27 | 27 | */ |
|
28 | 28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ |
|
29 | 29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); |
|
30 | 30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} |
|
31 | 31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); |
|
32 | 32 | var tabsize = cm.getOption('tabSize'); |
|
33 | 33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; |
|
34 | 34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} |
|
35 | 35 | var select = cm.getRange(from,cur) |
|
36 | 36 | if( select.match(/^\ +$/) != null){ |
|
37 | 37 | cm.replaceRange("",from,cur) |
|
38 | 38 | } else { |
|
39 | 39 | cm.deleteH(-1,"char") |
|
40 | 40 | } |
|
41 | 41 | }; |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | var IPython = (function (IPython) { |
|
45 | 45 | "use strict"; |
|
46 | 46 | |
|
47 | 47 | var utils = IPython.utils; |
|
48 | 48 | var key = IPython.utils.keycodes; |
|
49 | 49 | |
|
50 | 50 | /** |
|
51 | 51 | * A Cell conceived to write code. |
|
52 | 52 | * |
|
53 | 53 | * The kernel doesn't have to be set at creation time, in that case |
|
54 | 54 | * it will be null and set_kernel has to be called later. |
|
55 | 55 | * @class CodeCell |
|
56 | 56 | * @extends IPython.Cell |
|
57 | 57 | * |
|
58 | 58 | * @constructor |
|
59 | 59 | * @param {Object|null} kernel |
|
60 | 60 | * @param {object|undefined} [options] |
|
61 | 61 | * @param [options.cm_config] {object} config to pass to CodeMirror |
|
62 | 62 | */ |
|
63 | 63 | var CodeCell = function (kernel, options) { |
|
64 | 64 | this.kernel = kernel || null; |
|
65 | 65 | this.code_mirror = null; |
|
66 | 66 | this.input_prompt_number = null; |
|
67 | 67 | this.collapsed = false; |
|
68 | this.default_mode = 'ipython'; | |
|
69 | 68 | this.cell_type = "code"; |
|
70 | 69 | |
|
71 | 70 | |
|
72 | 71 | var cm_overwrite_options = { |
|
73 | 72 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
74 | 73 | }; |
|
75 | 74 | |
|
76 | 75 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); |
|
77 | 76 | |
|
78 | 77 | IPython.Cell.apply(this,[options]); |
|
79 | 78 | |
|
80 | 79 | var that = this; |
|
81 | 80 | this.element.focusout( |
|
82 | 81 | function() { that.auto_highlight(); } |
|
83 | 82 | ); |
|
84 | 83 | }; |
|
85 | 84 | |
|
86 | 85 | CodeCell.options_default = { |
|
87 | 86 | cm_config : { |
|
88 | 87 | extraKeys: { |
|
89 | 88 | "Tab" : "indentMore", |
|
90 | 89 | "Shift-Tab" : "indentLess", |
|
91 | 90 | "Backspace" : "delSpaceToPrevTabStop", |
|
92 | 91 | "Cmd-/" : "toggleComment", |
|
93 | 92 | "Ctrl-/" : "toggleComment" |
|
94 | 93 | }, |
|
95 | 94 | mode: 'ipython', |
|
96 | 95 | theme: 'ipython', |
|
97 | 96 | matchBrackets: true |
|
98 | 97 | } |
|
99 | 98 | }; |
|
100 | 99 | |
|
101 | 100 | |
|
102 | 101 | CodeCell.prototype = new IPython.Cell(); |
|
103 | 102 | |
|
104 | 103 | /** |
|
105 | 104 | * @method auto_highlight |
|
106 | 105 | */ |
|
107 | 106 | CodeCell.prototype.auto_highlight = function () { |
|
108 | 107 | this._auto_highlight(IPython.config.cell_magic_highlight) |
|
109 | 108 | }; |
|
110 | 109 | |
|
111 | 110 | /** @method create_element */ |
|
112 | 111 | CodeCell.prototype.create_element = function () { |
|
113 | 112 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
114 | 113 | |
|
115 | 114 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); |
|
116 | 115 | cell.attr('tabindex','2'); |
|
117 | 116 | |
|
118 | 117 | this.celltoolbar = new IPython.CellToolbar(this); |
|
119 | 118 | |
|
120 | 119 | var input = $('<div></div>').addClass('input'); |
|
121 | 120 | var vbox = $('<div/>').addClass('vbox box-flex1') |
|
122 | 121 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
123 | 122 | vbox.append(this.celltoolbar.element); |
|
124 | 123 | var input_area = $('<div/>').addClass('input_area'); |
|
125 | 124 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); |
|
126 | 125 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); |
|
127 | 126 | vbox.append(input_area); |
|
128 | 127 | input.append(vbox); |
|
129 | 128 | var output = $('<div></div>'); |
|
130 | 129 | cell.append(input).append(output); |
|
131 | 130 | this.element = cell; |
|
132 | 131 | this.output_area = new IPython.OutputArea(output, true); |
|
133 | 132 | |
|
134 | 133 | // construct a completer only if class exist |
|
135 | 134 | // otherwise no print view |
|
136 | 135 | if (IPython.Completer !== undefined) |
|
137 | 136 | { |
|
138 | 137 | this.completer = new IPython.Completer(this); |
|
139 | 138 | } |
|
140 | 139 | }; |
|
141 | 140 | |
|
142 | 141 | /** |
|
143 | 142 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
144 | 143 | * handlers and is used to provide custom key handling. Its return |
|
145 | 144 | * value is used to determine if CodeMirror should ignore the event: |
|
146 | 145 | * true = ignore, false = don't ignore. |
|
147 | 146 | * @method handle_codemirror_keyevent |
|
148 | 147 | */ |
|
149 | 148 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
150 | 149 | |
|
151 | 150 | var that = this; |
|
152 | 151 | // whatever key is pressed, first, cancel the tooltip request before |
|
153 | 152 | // they are sent, and remove tooltip if any, except for tab again |
|
154 | 153 | if (event.type === 'keydown' && event.which != key.TAB ) { |
|
155 | 154 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
156 | 155 | }; |
|
157 | 156 | |
|
158 | 157 | var cur = editor.getCursor(); |
|
159 | 158 | if (event.keyCode === key.ENTER){ |
|
160 | 159 | this.auto_highlight(); |
|
161 | 160 | } |
|
162 | 161 | |
|
163 | 162 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { |
|
164 | 163 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
165 | 164 | return true; |
|
166 | 165 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
|
167 | 166 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
168 | 167 | // browser and keyboard layout ! |
|
169 | 168 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
170 | 169 | // The second argument says to hide the tooltip if the docstring |
|
171 | 170 | // is actually empty |
|
172 | 171 | IPython.tooltip.pending(that, true); |
|
173 | 172 | } else if (event.which === key.UPARROW && event.type === 'keydown') { |
|
174 | 173 | // If we are not at the top, let CM handle the up arrow and |
|
175 | 174 | // prevent the global keydown handler from handling it. |
|
176 | 175 | if (!that.at_top()) { |
|
177 | 176 | event.stop(); |
|
178 | 177 | return false; |
|
179 | 178 | } else { |
|
180 | 179 | return true; |
|
181 | 180 | }; |
|
182 | 181 | } else if (event.which === key.ESC) { |
|
183 | 182 | IPython.tooltip.remove_and_cancel_tooltip(true); |
|
184 | 183 | return true; |
|
185 | 184 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
186 | 185 | // If we are not at the bottom, let CM handle the down arrow and |
|
187 | 186 | // prevent the global keydown handler from handling it. |
|
188 | 187 | if (!that.at_bottom()) { |
|
189 | 188 | event.stop(); |
|
190 | 189 | return false; |
|
191 | 190 | } else { |
|
192 | 191 | return true; |
|
193 | 192 | }; |
|
194 | 193 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { |
|
195 | 194 | if (editor.somethingSelected()){ |
|
196 | 195 | var anchor = editor.getCursor("anchor"); |
|
197 | 196 | var head = editor.getCursor("head"); |
|
198 | 197 | if( anchor.line != head.line){ |
|
199 | 198 | return false; |
|
200 | 199 | } |
|
201 | 200 | } |
|
202 | 201 | IPython.tooltip.request(that); |
|
203 | 202 | event.stop(); |
|
204 | 203 | return true; |
|
205 | 204 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { |
|
206 | 205 | // Tab completion. |
|
207 | 206 | //Do not trim here because of tooltip |
|
208 | 207 | if (editor.somethingSelected()){return false} |
|
209 | 208 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
210 | 209 | if (pre_cursor.trim() === "") { |
|
211 | 210 | // Don't autocomplete if the part of the line before the cursor |
|
212 | 211 | // is empty. In this case, let CodeMirror handle indentation. |
|
213 | 212 | return false; |
|
214 | 213 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { |
|
215 | 214 | IPython.tooltip.request(that); |
|
216 | 215 | // Prevent the event from bubbling up. |
|
217 | 216 | event.stop(); |
|
218 | 217 | // Prevent CodeMirror from handling the tab. |
|
219 | 218 | return true; |
|
220 | 219 | } else { |
|
221 | 220 | event.stop(); |
|
222 | 221 | this.completer.startCompletion(); |
|
223 | 222 | return true; |
|
224 | 223 | }; |
|
225 | 224 | } else { |
|
226 | 225 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
227 | 226 | // use those to disable tab completion. |
|
228 | 227 | return false; |
|
229 | 228 | }; |
|
230 | 229 | return false; |
|
231 | 230 | }; |
|
232 | 231 | |
|
233 | 232 | |
|
234 | 233 | // Kernel related calls. |
|
235 | 234 | |
|
236 | 235 | CodeCell.prototype.set_kernel = function (kernel) { |
|
237 | 236 | this.kernel = kernel; |
|
238 | 237 | } |
|
239 | 238 | |
|
240 | 239 | /** |
|
241 | 240 | * Execute current code cell to the kernel |
|
242 | 241 | * @method execute |
|
243 | 242 | */ |
|
244 | 243 | CodeCell.prototype.execute = function () { |
|
245 | 244 | this.output_area.clear_output(true, true, true); |
|
246 | 245 | this.set_input_prompt('*'); |
|
247 | 246 | this.element.addClass("running"); |
|
248 | 247 | var callbacks = { |
|
249 | 248 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
250 | 249 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
251 | 250 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
252 | 251 | 'set_next_input': $.proxy(this._handle_set_next_input, this), |
|
253 | 252 | 'input_request': $.proxy(this._handle_input_request, this) |
|
254 | 253 | }; |
|
255 | 254 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); |
|
256 | 255 | }; |
|
257 | 256 | |
|
258 | 257 | /** |
|
259 | 258 | * @method _handle_execute_reply |
|
260 | 259 | * @private |
|
261 | 260 | */ |
|
262 | 261 | CodeCell.prototype._handle_execute_reply = function (content) { |
|
263 | 262 | this.set_input_prompt(content.execution_count); |
|
264 | 263 | this.element.removeClass("running"); |
|
265 | 264 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); |
|
266 | 265 | } |
|
267 | 266 | |
|
268 | 267 | /** |
|
269 | 268 | * @method _handle_set_next_input |
|
270 | 269 | * @private |
|
271 | 270 | */ |
|
272 | 271 | CodeCell.prototype._handle_set_next_input = function (text) { |
|
273 | 272 | var data = {'cell': this, 'text': text} |
|
274 | 273 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
275 | 274 | } |
|
276 | 275 | |
|
277 | 276 | /** |
|
278 | 277 | * @method _handle_input_request |
|
279 | 278 | * @private |
|
280 | 279 | */ |
|
281 | 280 | CodeCell.prototype._handle_input_request = function (content) { |
|
282 | 281 | this.output_area.append_raw_input(content); |
|
283 | 282 | } |
|
284 | 283 | |
|
285 | 284 | |
|
286 | 285 | // Basic cell manipulation. |
|
287 | 286 | |
|
288 | 287 | CodeCell.prototype.select = function () { |
|
289 | 288 | IPython.Cell.prototype.select.apply(this); |
|
290 | 289 | this.code_mirror.refresh(); |
|
291 | 290 | this.code_mirror.focus(); |
|
292 | 291 | this.auto_highlight(); |
|
293 | 292 | // We used to need an additional refresh() after the focus, but |
|
294 | 293 | // it appears that this has been fixed in CM. This bug would show |
|
295 | 294 | // up on FF when a newly loaded markdown cell was edited. |
|
296 | 295 | }; |
|
297 | 296 | |
|
298 | 297 | |
|
299 | 298 | CodeCell.prototype.select_all = function () { |
|
300 | 299 | var start = {line: 0, ch: 0}; |
|
301 | 300 | var nlines = this.code_mirror.lineCount(); |
|
302 | 301 | var last_line = this.code_mirror.getLine(nlines-1); |
|
303 | 302 | var end = {line: nlines-1, ch: last_line.length}; |
|
304 | 303 | this.code_mirror.setSelection(start, end); |
|
305 | 304 | }; |
|
306 | 305 | |
|
307 | 306 | |
|
308 | 307 | CodeCell.prototype.collapse = function () { |
|
309 | 308 | this.collapsed = true; |
|
310 | 309 | this.output_area.collapse(); |
|
311 | 310 | }; |
|
312 | 311 | |
|
313 | 312 | |
|
314 | 313 | CodeCell.prototype.expand = function () { |
|
315 | 314 | this.collapsed = false; |
|
316 | 315 | this.output_area.expand(); |
|
317 | 316 | }; |
|
318 | 317 | |
|
319 | 318 | |
|
320 | 319 | CodeCell.prototype.toggle_output = function () { |
|
321 | 320 | this.collapsed = Boolean(1 - this.collapsed); |
|
322 | 321 | this.output_area.toggle_output(); |
|
323 | 322 | }; |
|
324 | 323 | |
|
325 | 324 | |
|
326 | 325 | CodeCell.prototype.toggle_output_scroll = function () { |
|
327 | 326 | this.output_area.toggle_scroll(); |
|
328 | 327 | }; |
|
329 | 328 | |
|
330 | 329 | |
|
331 | 330 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
332 | 331 | var ns = prompt_value || " "; |
|
333 | 332 | return 'In [' + ns + ']:' |
|
334 | 333 | }; |
|
335 | 334 | |
|
336 | 335 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
337 | 336 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
338 | 337 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; |
|
339 | 338 | return html.join('</br>') |
|
340 | 339 | }; |
|
341 | 340 | |
|
342 | 341 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
343 | 342 | |
|
344 | 343 | |
|
345 | 344 | CodeCell.prototype.set_input_prompt = function (number) { |
|
346 | 345 | var nline = 1 |
|
347 | 346 | if( this.code_mirror != undefined) { |
|
348 | 347 | nline = this.code_mirror.lineCount(); |
|
349 | 348 | } |
|
350 | 349 | this.input_prompt_number = number; |
|
351 | 350 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
352 | 351 | this.element.find('div.input_prompt').html(prompt_html); |
|
353 | 352 | }; |
|
354 | 353 | |
|
355 | 354 | |
|
356 | 355 | CodeCell.prototype.clear_input = function () { |
|
357 | 356 | this.code_mirror.setValue(''); |
|
358 | 357 | }; |
|
359 | 358 | |
|
360 | 359 | |
|
361 | 360 | CodeCell.prototype.get_text = function () { |
|
362 | 361 | return this.code_mirror.getValue(); |
|
363 | 362 | }; |
|
364 | 363 | |
|
365 | 364 | |
|
366 | 365 | CodeCell.prototype.set_text = function (code) { |
|
367 | 366 | return this.code_mirror.setValue(code); |
|
368 | 367 | }; |
|
369 | 368 | |
|
370 | 369 | |
|
371 | 370 | CodeCell.prototype.at_top = function () { |
|
372 | 371 | var cursor = this.code_mirror.getCursor(); |
|
373 | 372 | if (cursor.line === 0 && cursor.ch === 0) { |
|
374 | 373 | return true; |
|
375 | 374 | } else { |
|
376 | 375 | return false; |
|
377 | 376 | } |
|
378 | 377 | }; |
|
379 | 378 | |
|
380 | 379 | |
|
381 | 380 | CodeCell.prototype.at_bottom = function () { |
|
382 | 381 | var cursor = this.code_mirror.getCursor(); |
|
383 | 382 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
384 | 383 | return true; |
|
385 | 384 | } else { |
|
386 | 385 | return false; |
|
387 | 386 | } |
|
388 | 387 | }; |
|
389 | 388 | |
|
390 | 389 | |
|
391 | 390 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
392 | 391 | this.output_area.clear_output(stdout, stderr, other); |
|
393 | 392 | }; |
|
394 | 393 | |
|
395 | 394 | |
|
396 | 395 | // JSON serialization |
|
397 | 396 | |
|
398 | 397 | CodeCell.prototype.fromJSON = function (data) { |
|
399 | 398 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
400 | 399 | if (data.cell_type === 'code') { |
|
401 | 400 | if (data.input !== undefined) { |
|
402 | 401 | this.set_text(data.input); |
|
403 | 402 | // make this value the starting point, so that we can only undo |
|
404 | 403 | // to this state, instead of a blank cell |
|
405 | 404 | this.code_mirror.clearHistory(); |
|
406 | 405 | this.auto_highlight(); |
|
407 | 406 | } |
|
408 | 407 | if (data.prompt_number !== undefined) { |
|
409 | 408 | this.set_input_prompt(data.prompt_number); |
|
410 | 409 | } else { |
|
411 | 410 | this.set_input_prompt(); |
|
412 | 411 | }; |
|
413 | 412 | this.output_area.fromJSON(data.outputs); |
|
414 | 413 | if (data.collapsed !== undefined) { |
|
415 | 414 | if (data.collapsed) { |
|
416 | 415 | this.collapse(); |
|
417 | 416 | } else { |
|
418 | 417 | this.expand(); |
|
419 | 418 | }; |
|
420 | 419 | }; |
|
421 | 420 | }; |
|
422 | 421 | }; |
|
423 | 422 | |
|
424 | 423 | |
|
425 | 424 | CodeCell.prototype.toJSON = function () { |
|
426 | 425 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
427 | 426 | data.input = this.get_text(); |
|
428 | 427 | data.cell_type = 'code'; |
|
429 | 428 | if (this.input_prompt_number) { |
|
430 | 429 | data.prompt_number = this.input_prompt_number; |
|
431 | 430 | }; |
|
432 | 431 | var outputs = this.output_area.toJSON(); |
|
433 | 432 | data.outputs = outputs; |
|
434 | 433 | data.language = 'python'; |
|
435 | 434 | data.collapsed = this.collapsed; |
|
436 | 435 | return data; |
|
437 | 436 | }; |
|
438 | 437 | |
|
439 | 438 | |
|
440 | 439 | IPython.CodeCell = CodeCell; |
|
441 | 440 | |
|
442 | 441 | return IPython; |
|
443 | 442 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now