Show More
@@ -1,345 +1,346 | |||
|
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 | var IPython = (function (IPython) { |
|
13 | 13 | "use strict"; |
|
14 | 14 | |
|
15 | 15 | var utils = IPython.utils; |
|
16 | 16 | var key = IPython.utils.keycodes; |
|
17 | 17 | CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js"; |
|
18 | 18 | |
|
19 | 19 | var CodeCell = function (kernel) { |
|
20 | 20 | // The kernel doesn't have to be set at creation time, in that case |
|
21 | 21 | // it will be null and set_kernel has to be called later. |
|
22 | 22 | this.kernel = kernel || null; |
|
23 | 23 | this.code_mirror = null; |
|
24 | 24 | this.input_prompt_number = null; |
|
25 | 25 | this.tooltip_on_tab = true; |
|
26 | 26 | this.collapsed = false; |
|
27 | 27 | this.default_mode = 'python'; |
|
28 | 28 | IPython.Cell.apply(this, arguments); |
|
29 | 29 | |
|
30 | 30 | var that = this; |
|
31 | 31 | this.element.focusout( |
|
32 | 32 | function() { that.auto_highlight(); } |
|
33 | 33 | ); |
|
34 | 34 | }; |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | CodeCell.prototype = new IPython.Cell(); |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | CodeCell.prototype.auto_highlight = function () { |
|
41 | 41 | this._auto_highlight(IPython.config.cell_magic_highlight) |
|
42 | 42 | }; |
|
43 | 43 | |
|
44 | 44 | CodeCell.prototype.create_element = function () { |
|
45 | 45 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox'); |
|
46 | 46 | cell.attr('tabindex','2'); |
|
47 | 47 | var input = $('<div></div>').addClass('input hbox'); |
|
48 | 48 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
49 | 49 | var input_area = $('<div/>').addClass('input_area box-flex1'); |
|
50 | 50 | this.code_mirror = CodeMirror(input_area.get(0), { |
|
51 | 51 | indentUnit : 4, |
|
52 | 52 | mode: 'python', |
|
53 | 53 | theme: 'ipython', |
|
54 | 54 | readOnly: this.read_only, |
|
55 | 55 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"}, |
|
56 | 56 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
57 | 57 | }); |
|
58 | 58 | input.append(input_area); |
|
59 | 59 | var output = $('<div></div>'); |
|
60 | 60 | cell.append(input).append(output); |
|
61 | 61 | this.element = cell; |
|
62 | 62 | this.output_area = new IPython.OutputArea(output, true); |
|
63 | 63 | |
|
64 | 64 | // construct a completer only if class exist |
|
65 | 65 | // otherwise no print view |
|
66 | 66 | if (IPython.Completer !== undefined) |
|
67 | 67 | { |
|
68 | 68 | this.completer = new IPython.Completer(this); |
|
69 | 69 | } |
|
70 | 70 | }; |
|
71 | 71 | |
|
72 | 72 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
73 | 73 | // This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
74 | 74 | // handlers and is used to provide custom key handling. Its return |
|
75 | 75 | // value is used to determine if CodeMirror should ignore the event: |
|
76 | 76 | // true = ignore, false = don't ignore. |
|
77 | 77 | |
|
78 | 78 | if (this.read_only){ |
|
79 | 79 | return false; |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | var that = this; |
|
83 | 83 | // whatever key is pressed, first, cancel the tooltip request before |
|
84 | 84 | // they are sent, and remove tooltip if any, except for tab again |
|
85 | 85 | if (event.type === 'keydown' && event.which != key.TAB ) { |
|
86 | 86 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
87 | 87 | }; |
|
88 | 88 | |
|
89 | 89 | var cur = editor.getCursor(); |
|
90 | 90 | if (event.keyCode === key.ENTER){ |
|
91 | 91 | this.auto_highlight(); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { |
|
95 | 95 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
96 | 96 | return true; |
|
97 | 97 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
|
98 | 98 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
99 | 99 | // browser and keyboard layout ! |
|
100 | 100 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
101 | 101 | IPython.tooltip.pending(that); |
|
102 | 102 | } else if (event.which === key.UPARROW && event.type === 'keydown') { |
|
103 | 103 | // If we are not at the top, let CM handle the up arrow and |
|
104 | 104 | // prevent the global keydown handler from handling it. |
|
105 | 105 | if (!that.at_top()) { |
|
106 | 106 | event.stop(); |
|
107 | 107 | return false; |
|
108 | 108 | } else { |
|
109 | 109 | return true; |
|
110 | 110 | }; |
|
111 | 111 | } else if (event.which === key.ESC) { |
|
112 | 112 | IPython.tooltip.remove_and_cancel_tooltip(true); |
|
113 | 113 | return true; |
|
114 | 114 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
115 | 115 | // If we are not at the bottom, let CM handle the down arrow and |
|
116 | 116 | // prevent the global keydown handler from handling it. |
|
117 | 117 | if (!that.at_bottom()) { |
|
118 | 118 | event.stop(); |
|
119 | 119 | return false; |
|
120 | 120 | } else { |
|
121 | 121 | return true; |
|
122 | 122 | }; |
|
123 | 123 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { |
|
124 | 124 | // Tab completion. |
|
125 | 125 | //Do not trim here because of tooltip |
|
126 | if (editor.somethingSelected()){return false} | |
|
126 | 127 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
127 | 128 | if (pre_cursor.trim() === "") { |
|
128 | 129 | // Don't autocomplete if the part of the line before the cursor |
|
129 | 130 | // is empty. In this case, let CodeMirror handle indentation. |
|
130 | 131 | return false; |
|
131 | 132 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) { |
|
132 | 133 | IPython.tooltip.request(that); |
|
133 | 134 | // Prevent the event from bubbling up. |
|
134 | 135 | event.stop(); |
|
135 | 136 | // Prevent CodeMirror from handling the tab. |
|
136 | 137 | return true; |
|
137 | 138 | } else { |
|
138 | 139 | event.stop(); |
|
139 | 140 | this.completer.startCompletion(); |
|
140 | 141 | return true; |
|
141 | 142 | }; |
|
142 | 143 | } else { |
|
143 | 144 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
144 | 145 | // use those to disable tab completion. |
|
145 | 146 | return false; |
|
146 | 147 | }; |
|
147 | 148 | return false; |
|
148 | 149 | }; |
|
149 | 150 | |
|
150 | 151 | |
|
151 | 152 | // Kernel related calls. |
|
152 | 153 | |
|
153 | 154 | CodeCell.prototype.set_kernel = function (kernel) { |
|
154 | 155 | this.kernel = kernel; |
|
155 | 156 | } |
|
156 | 157 | |
|
157 | 158 | |
|
158 | 159 | CodeCell.prototype.execute = function () { |
|
159 | 160 | this.output_area.clear_output(true, true, true); |
|
160 | 161 | this.set_input_prompt('*'); |
|
161 | 162 | this.element.addClass("running"); |
|
162 | 163 | var callbacks = { |
|
163 | 164 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
164 | 165 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
165 | 166 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
166 | 167 | 'set_next_input': $.proxy(this._handle_set_next_input, this) |
|
167 | 168 | }; |
|
168 | 169 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); |
|
169 | 170 | }; |
|
170 | 171 | |
|
171 | 172 | |
|
172 | 173 | CodeCell.prototype._handle_execute_reply = function (content) { |
|
173 | 174 | this.set_input_prompt(content.execution_count); |
|
174 | 175 | this.element.removeClass("running"); |
|
175 | 176 | $([IPython.events]).trigger('set_dirty.Notebook', {'value': true}); |
|
176 | 177 | } |
|
177 | 178 | |
|
178 | 179 | CodeCell.prototype._handle_set_next_input = function (text) { |
|
179 | 180 | var data = {'cell': this, 'text': text} |
|
180 | 181 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
181 | 182 | } |
|
182 | 183 | |
|
183 | 184 | // Basic cell manipulation. |
|
184 | 185 | |
|
185 | 186 | CodeCell.prototype.select = function () { |
|
186 | 187 | IPython.Cell.prototype.select.apply(this); |
|
187 | 188 | this.code_mirror.refresh(); |
|
188 | 189 | this.code_mirror.focus(); |
|
189 | 190 | this.auto_highlight(); |
|
190 | 191 | // We used to need an additional refresh() after the focus, but |
|
191 | 192 | // it appears that this has been fixed in CM. This bug would show |
|
192 | 193 | // up on FF when a newly loaded markdown cell was edited. |
|
193 | 194 | }; |
|
194 | 195 | |
|
195 | 196 | |
|
196 | 197 | CodeCell.prototype.select_all = function () { |
|
197 | 198 | var start = {line: 0, ch: 0}; |
|
198 | 199 | var nlines = this.code_mirror.lineCount(); |
|
199 | 200 | var last_line = this.code_mirror.getLine(nlines-1); |
|
200 | 201 | var end = {line: nlines-1, ch: last_line.length}; |
|
201 | 202 | this.code_mirror.setSelection(start, end); |
|
202 | 203 | }; |
|
203 | 204 | |
|
204 | 205 | |
|
205 | 206 | CodeCell.prototype.collapse = function () { |
|
206 | 207 | this.collapsed = true; |
|
207 | 208 | this.output_area.collapse(); |
|
208 | 209 | }; |
|
209 | 210 | |
|
210 | 211 | |
|
211 | 212 | CodeCell.prototype.expand = function () { |
|
212 | 213 | this.collapsed = false; |
|
213 | 214 | this.output_area.expand(); |
|
214 | 215 | }; |
|
215 | 216 | |
|
216 | 217 | |
|
217 | 218 | CodeCell.prototype.toggle_output = function () { |
|
218 | 219 | this.collapsed = Boolean(1 - this.collapsed); |
|
219 | 220 | this.output_area.toggle_output(); |
|
220 | 221 | }; |
|
221 | 222 | |
|
222 | 223 | |
|
223 | 224 | CodeCell.prototype.toggle_output_scroll = function () { |
|
224 | 225 | this.output_area.toggle_scroll(); |
|
225 | 226 | }; |
|
226 | 227 | |
|
227 | 228 | |
|
228 | 229 | |
|
229 | 230 | |
|
230 | 231 | |
|
231 | 232 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
232 | 233 | var ns = prompt_value || " "; |
|
233 | 234 | return 'In [' + ns + ']:' |
|
234 | 235 | }; |
|
235 | 236 | |
|
236 | 237 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
237 | 238 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
238 | 239 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; |
|
239 | 240 | return html.join('</br>') |
|
240 | 241 | }; |
|
241 | 242 | |
|
242 | 243 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
243 | 244 | |
|
244 | 245 | |
|
245 | 246 | CodeCell.prototype.set_input_prompt = function (number) { |
|
246 | 247 | var nline = 1 |
|
247 | 248 | if( this.code_mirror != undefined) { |
|
248 | 249 | nline = this.code_mirror.lineCount(); |
|
249 | 250 | } |
|
250 | 251 | if (number){ |
|
251 | 252 | this.input_prompt_number = number; |
|
252 | 253 | } |
|
253 | 254 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
254 | 255 | this.element.find('div.input_prompt').html(prompt_html); |
|
255 | 256 | }; |
|
256 | 257 | |
|
257 | 258 | |
|
258 | 259 | CodeCell.prototype.clear_input = function () { |
|
259 | 260 | this.code_mirror.setValue(''); |
|
260 | 261 | }; |
|
261 | 262 | |
|
262 | 263 | |
|
263 | 264 | CodeCell.prototype.get_text = function () { |
|
264 | 265 | return this.code_mirror.getValue(); |
|
265 | 266 | }; |
|
266 | 267 | |
|
267 | 268 | |
|
268 | 269 | CodeCell.prototype.set_text = function (code) { |
|
269 | 270 | return this.code_mirror.setValue(code); |
|
270 | 271 | }; |
|
271 | 272 | |
|
272 | 273 | |
|
273 | 274 | CodeCell.prototype.at_top = function () { |
|
274 | 275 | var cursor = this.code_mirror.getCursor(); |
|
275 | 276 | if (cursor.line === 0 && cursor.ch === 0) { |
|
276 | 277 | return true; |
|
277 | 278 | } else { |
|
278 | 279 | return false; |
|
279 | 280 | } |
|
280 | 281 | }; |
|
281 | 282 | |
|
282 | 283 | |
|
283 | 284 | CodeCell.prototype.at_bottom = function () { |
|
284 | 285 | var cursor = this.code_mirror.getCursor(); |
|
285 | 286 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
286 | 287 | return true; |
|
287 | 288 | } else { |
|
288 | 289 | return false; |
|
289 | 290 | } |
|
290 | 291 | }; |
|
291 | 292 | |
|
292 | 293 | |
|
293 | 294 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
294 | 295 | this.output_area.clear_output(stdout, stderr, other); |
|
295 | 296 | }; |
|
296 | 297 | |
|
297 | 298 | |
|
298 | 299 | // JSON serialization |
|
299 | 300 | |
|
300 | 301 | CodeCell.prototype.fromJSON = function (data) { |
|
301 | 302 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
302 | 303 | if (data.cell_type === 'code') { |
|
303 | 304 | if (data.input !== undefined) { |
|
304 | 305 | this.set_text(data.input); |
|
305 | 306 | // make this value the starting point, so that we can only undo |
|
306 | 307 | // to this state, instead of a blank cell |
|
307 | 308 | this.code_mirror.clearHistory(); |
|
308 | 309 | this.auto_highlight(); |
|
309 | 310 | } |
|
310 | 311 | if (data.prompt_number !== undefined) { |
|
311 | 312 | this.set_input_prompt(data.prompt_number); |
|
312 | 313 | } else { |
|
313 | 314 | this.set_input_prompt(); |
|
314 | 315 | }; |
|
315 | 316 | this.output_area.fromJSON(data.outputs); |
|
316 | 317 | if (data.collapsed !== undefined) { |
|
317 | 318 | if (data.collapsed) { |
|
318 | 319 | this.collapse(); |
|
319 | 320 | } else { |
|
320 | 321 | this.expand(); |
|
321 | 322 | }; |
|
322 | 323 | }; |
|
323 | 324 | }; |
|
324 | 325 | }; |
|
325 | 326 | |
|
326 | 327 | |
|
327 | 328 | CodeCell.prototype.toJSON = function () { |
|
328 | 329 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
329 | 330 | data.input = this.get_text(); |
|
330 | 331 | data.cell_type = 'code'; |
|
331 | 332 | if (this.input_prompt_number) { |
|
332 | 333 | data.prompt_number = this.input_prompt_number; |
|
333 | 334 | }; |
|
334 | 335 | var outputs = this.output_area.toJSON(); |
|
335 | 336 | data.outputs = outputs; |
|
336 | 337 | data.language = 'python'; |
|
337 | 338 | data.collapsed = this.collapsed; |
|
338 | 339 | return data; |
|
339 | 340 | }; |
|
340 | 341 | |
|
341 | 342 | |
|
342 | 343 | IPython.CodeCell = CodeCell; |
|
343 | 344 | |
|
344 | 345 | return IPython; |
|
345 | 346 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now