Show More
@@ -1,843 +1,843 | |||
|
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 | |
|
14 | 14 | var utils = IPython.utils; |
|
15 | 15 | |
|
16 | 16 | var CodeCell = function (notebook) { |
|
17 | 17 | this.code_mirror = null; |
|
18 |
this.input_prompt_number = |
|
|
18 | this.input_prompt_number = null; | |
|
19 | 19 | this.is_completing = false; |
|
20 | 20 | this.completion_cursor = null; |
|
21 | 21 | this.outputs = []; |
|
22 | 22 | this.collapsed = false; |
|
23 | 23 | this.tooltip_timeout = null; |
|
24 | 24 | IPython.Cell.apply(this, arguments); |
|
25 | 25 | }; |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | CodeCell.prototype = new IPython.Cell(); |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | CodeCell.prototype.create_element = function () { |
|
32 | 32 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox'); |
|
33 | 33 | cell.attr('tabindex','2'); |
|
34 | 34 | var input = $('<div></div>').addClass('input hbox'); |
|
35 | 35 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
36 | 36 | var input_area = $('<div/>').addClass('input_area box-flex1'); |
|
37 | 37 | this.code_mirror = CodeMirror(input_area.get(0), { |
|
38 | 38 | indentUnit : 4, |
|
39 | 39 | mode: 'python', |
|
40 | 40 | theme: 'ipython', |
|
41 | 41 | readOnly: this.read_only, |
|
42 | 42 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
43 | 43 | }); |
|
44 | 44 | input.append(input_area); |
|
45 | 45 | var output = $('<div></div>').addClass('output vbox'); |
|
46 | 46 | cell.append(input).append(output); |
|
47 | 47 | this.element = cell; |
|
48 | 48 | this.collapse(); |
|
49 | 49 | }; |
|
50 | 50 | |
|
51 | 51 | //TODO, try to diminish the number of parameters. |
|
52 | 52 | CodeCell.prototype.request_tooltip_after_time = function (pre_cursor,time){ |
|
53 | 53 | var that = this; |
|
54 | 54 | if (pre_cursor === "" || pre_cursor === "(" ) { |
|
55 | 55 | // don't do anything if line beggin with '(' or is empty |
|
56 | 56 | } else { |
|
57 | 57 | // Will set a timer to request tooltip in `time` |
|
58 | 58 | that.tooltip_timeout = setTimeout(function(){ |
|
59 | 59 | IPython.notebook.request_tool_tip(that, pre_cursor) |
|
60 | 60 | },time); |
|
61 | 61 | } |
|
62 | 62 | }; |
|
63 | 63 | |
|
64 | 64 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
65 | 65 | // This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
66 | 66 | // handlers and is used to provide custom key handling. Its return |
|
67 | 67 | // value is used to determine if CodeMirror should ignore the event: |
|
68 | 68 | // true = ignore, false = don't ignore. |
|
69 | 69 | |
|
70 | 70 | if (this.read_only){ |
|
71 | 71 | return false; |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | // note that we are comparing and setting the time to wait at each key press. |
|
75 | 75 | // a better wqy might be to generate a new function on each time change and |
|
76 | 76 | // assign it to CodeCell.prototype.request_tooltip_after_time |
|
77 | 77 | tooltip_wait_time = this.notebook.time_before_tooltip; |
|
78 | 78 | tooltip_on_tab = this.notebook.tooltip_on_tab; |
|
79 | 79 | var that = this; |
|
80 | 80 | // whatever key is pressed, first, cancel the tooltip request before |
|
81 | 81 | // they are sent, and remove tooltip if any |
|
82 | 82 | if(event.type === 'keydown' ){ |
|
83 | 83 | that.remove_and_cancel_tooltip(); |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { |
|
87 | 87 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
88 | 88 | return true; |
|
89 | 89 | }else if (event.which === 40 && event.type === 'keypress' && tooltip_wait_time >= 0) { |
|
90 | 90 | // triger aon keypress (!) otherwise inconsistent event.which depending on plateform |
|
91 | 91 | // browser and keyboard layout ! |
|
92 | 92 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
93 | 93 | var cursor = editor.getCursor(); |
|
94 | 94 | var pre_cursor = editor.getRange({line:cursor.line,ch:0},cursor).trim()+'('; |
|
95 | 95 | that.request_tooltip_after_time(pre_cursor,tooltip_wait_time); |
|
96 | 96 | } else if (event.keyCode === 9 && event.type == 'keydown') { |
|
97 | 97 | // Tab completion. |
|
98 | 98 | var cur = editor.getCursor(); |
|
99 | 99 | //Do not trim here because of tooltip |
|
100 | 100 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
101 | 101 | if (pre_cursor.trim() === "") { |
|
102 | 102 | // Don't autocomplete if the part of the line before the cursor |
|
103 | 103 | // is empty. In this case, let CodeMirror handle indentation. |
|
104 | 104 | return false; |
|
105 | 105 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && tooltip_on_tab ) { |
|
106 | 106 | that.request_tooltip_after_time(pre_cursor,0); |
|
107 | 107 | } else { |
|
108 | 108 | pre_cursor.trim(); |
|
109 | 109 | // Autocomplete the current line. |
|
110 | 110 | event.stop(); |
|
111 | 111 | var line = editor.getLine(cur.line); |
|
112 | 112 | this.is_completing = true; |
|
113 | 113 | this.completion_cursor = cur; |
|
114 | 114 | IPython.notebook.complete_cell(this, line, cur.ch); |
|
115 | 115 | return true; |
|
116 | 116 | } |
|
117 | 117 | } else if (event.keyCode === 8 && event.type == 'keydown') { |
|
118 | 118 | // If backspace and the line ends with 4 spaces, remove them. |
|
119 | 119 | var cur = editor.getCursor(); |
|
120 | 120 | var line = editor.getLine(cur.line); |
|
121 | 121 | var ending = line.slice(-4); |
|
122 | 122 | if (ending === ' ') { |
|
123 | 123 | editor.replaceRange('', |
|
124 | 124 | {line: cur.line, ch: cur.ch-4}, |
|
125 | 125 | {line: cur.line, ch: cur.ch} |
|
126 | 126 | ); |
|
127 | 127 | event.stop(); |
|
128 | 128 | return true; |
|
129 | 129 | } else { |
|
130 | 130 | return false; |
|
131 | 131 | } |
|
132 | 132 | } else if (event.keyCode === 76 && event.ctrlKey && event.shiftKey |
|
133 | 133 | && event.type == 'keydown') { |
|
134 | 134 | // toggle line numbers with Ctrl-Shift-L |
|
135 | 135 | this.toggle_line_numbers(); |
|
136 | 136 | } |
|
137 | 137 | else { |
|
138 | 138 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
139 | 139 | // use those to disable tab completion. |
|
140 | 140 | if (this.is_completing && event.keyCode !== 9) { |
|
141 | 141 | var ed_cur = editor.getCursor(); |
|
142 | 142 | var cc_cur = this.completion_cursor; |
|
143 | 143 | if (ed_cur.line !== cc_cur.line || ed_cur.ch !== cc_cur.ch) { |
|
144 | 144 | this.is_completing = false; |
|
145 | 145 | this.completion_cursor = null; |
|
146 | 146 | } |
|
147 | 147 | } |
|
148 | 148 | return false; |
|
149 | 149 | }; |
|
150 | 150 | return false; |
|
151 | 151 | }; |
|
152 | 152 | |
|
153 | 153 | CodeCell.prototype.remove_and_cancel_tooltip = function() { |
|
154 | 154 | // note that we don't handle closing directly inside the calltip |
|
155 | 155 | // as in the completer, because it is not focusable, so won't |
|
156 | 156 | // get the event. |
|
157 | 157 | if (this.tooltip_timeout != null){ |
|
158 | 158 | clearTimeout(this.tooltip_timeout); |
|
159 | 159 | $('#tooltip').remove(); |
|
160 | 160 | this.tooltip_timeout = null; |
|
161 | 161 | } |
|
162 | 162 | } |
|
163 | 163 | |
|
164 | 164 | CodeCell.prototype.finish_tooltip = function (reply) { |
|
165 | 165 | // Extract call tip data; the priority is call, init, main. |
|
166 | 166 | defstring = reply.call_def; |
|
167 | 167 | if (defstring == null) { defstring = reply.init_definition; } |
|
168 | 168 | if (defstring == null) { defstring = reply.definition; } |
|
169 | 169 | |
|
170 | 170 | docstring = reply.call_docstring; |
|
171 | 171 | if (docstring == null) { docstring = reply.init_docstring; } |
|
172 | 172 | if (docstring == null) { docstring = reply.docstring; } |
|
173 | 173 | if (docstring == null) { docstring = "<empty docstring>"; } |
|
174 | 174 | |
|
175 | 175 | name=reply.name; |
|
176 | 176 | |
|
177 | 177 | var that = this; |
|
178 | 178 | var tooltip = $('<div/>').attr('id', 'tooltip').addClass('tooltip'); |
|
179 | 179 | // remove to have the tooltip not Limited in X and Y |
|
180 | 180 | tooltip.addClass('smalltooltip'); |
|
181 | 181 | var pre=$('<pre/>').html(utils.fixConsole(docstring)); |
|
182 | 182 | var expandlink=$('<a/>').attr('href',"#"); |
|
183 | 183 | expandlink.addClass("ui-corner-all"); //rounded corner |
|
184 | 184 | expandlink.attr('role',"button"); |
|
185 | 185 | //expandlink.addClass('ui-button'); |
|
186 | 186 | //expandlink.addClass('ui-state-default'); |
|
187 | 187 | var expandspan=$('<span/>').text('Expand'); |
|
188 | 188 | expandspan.addClass('ui-icon'); |
|
189 | 189 | expandspan.addClass('ui-icon-plus'); |
|
190 | 190 | expandlink.append(expandspan); |
|
191 | 191 | expandlink.attr('id','expanbutton'); |
|
192 | 192 | expandlink.click(function(){ |
|
193 | 193 | tooltip.removeClass('smalltooltip'); |
|
194 | 194 | tooltip.addClass('bigtooltip'); |
|
195 | 195 | $('#expanbutton').remove(); |
|
196 | 196 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
197 | 197 | }); |
|
198 | 198 | var morelink=$('<a/>').attr('href',"#"); |
|
199 | 199 | morelink.attr('role',"button"); |
|
200 | 200 | morelink.addClass('ui-button'); |
|
201 | 201 | //morelink.addClass("ui-corner-all"); //rounded corner |
|
202 | 202 | //morelink.addClass('ui-state-default'); |
|
203 | 203 | var morespan=$('<span/>').text('Open in Pager'); |
|
204 | 204 | morespan.addClass('ui-icon'); |
|
205 | 205 | morespan.addClass('ui-icon-arrowstop-l-n'); |
|
206 | 206 | morelink.append(morespan); |
|
207 | 207 | morelink.click(function(){ |
|
208 | 208 | var msg_id = IPython.notebook.kernel.execute(name+"?"); |
|
209 | 209 | IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.selected_cell().cell_id; |
|
210 | 210 | that.remove_and_cancel_tooltip(); |
|
211 | 211 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
212 | 212 | }); |
|
213 | 213 | |
|
214 | 214 | var closelink=$('<a/>').attr('href',"#"); |
|
215 | 215 | closelink.attr('role',"button"); |
|
216 | 216 | closelink.addClass('ui-button'); |
|
217 | 217 | //closelink.addClass("ui-corner-all"); //rounded corner |
|
218 | 218 | //closelink.adClass('ui-state-default'); // grey background and blue cross |
|
219 | 219 | var closespan=$('<span/>').text('Close'); |
|
220 | 220 | closespan.addClass('ui-icon'); |
|
221 | 221 | closespan.addClass('ui-icon-close'); |
|
222 | 222 | closelink.append(closespan); |
|
223 | 223 | closelink.click(function(){ |
|
224 | 224 | that.remove_and_cancel_tooltip(); |
|
225 | 225 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
226 | 226 | }); |
|
227 | 227 | //construct the tooltip |
|
228 | 228 | tooltip.append(closelink); |
|
229 | 229 | tooltip.append(expandlink); |
|
230 | 230 | tooltip.append(morelink); |
|
231 | 231 | if(defstring){ |
|
232 | 232 | defstring_html = $('<pre/>').html(utils.fixConsole(defstring)); |
|
233 | 233 | tooltip.append(defstring_html); |
|
234 | 234 | } |
|
235 | 235 | tooltip.append(pre); |
|
236 | 236 | var pos = this.code_mirror.cursorCoords(); |
|
237 | 237 | tooltip.css('left',pos.x+'px'); |
|
238 | 238 | tooltip.css('top',pos.yBot+'px'); |
|
239 | 239 | $('body').append(tooltip); |
|
240 | 240 | |
|
241 | 241 | // issues with cross-closing if multiple tooltip in less than 5sec |
|
242 | 242 | // keep it comented for now |
|
243 | 243 | // setTimeout(that.remove_and_cancel_tooltip, 5000); |
|
244 | 244 | }; |
|
245 | 245 | |
|
246 | 246 | // As you type completer |
|
247 | 247 | CodeCell.prototype.finish_completing = function (matched_text, matches) { |
|
248 | 248 | //return if not completing or nothing to complete |
|
249 | 249 | if (!this.is_completing || matches.length === 0) {return;} |
|
250 | 250 | |
|
251 | 251 | // for later readability |
|
252 | 252 | var key = { tab:9, |
|
253 | 253 | esc:27, |
|
254 | 254 | backspace:8, |
|
255 | 255 | space:32, |
|
256 | 256 | shift:16, |
|
257 | 257 | enter:13, |
|
258 | 258 | // _ is 95 |
|
259 | 259 | isCompSymbol : function (code) |
|
260 | 260 | { |
|
261 | 261 | return (code > 64 && code <= 90) |
|
262 | 262 | || (code >= 97 && code <= 122) |
|
263 | 263 | || (code == 95) |
|
264 | 264 | }, |
|
265 | 265 | dismissAndAppend : function (code) |
|
266 | 266 | { |
|
267 | 267 | chararr = '()[]+-/\\. ,=*'.split(""); |
|
268 | 268 | codearr = chararr.map(function(x){return x.charCodeAt(0)}); |
|
269 | 269 | return jQuery.inArray(code, codearr) != -1; |
|
270 | 270 | } |
|
271 | 271 | |
|
272 | 272 | } |
|
273 | 273 | |
|
274 | 274 | // smart completion, sort kwarg ending with '=' |
|
275 | 275 | var newm = new Array(); |
|
276 | 276 | if(this.notebook.smart_completer) |
|
277 | 277 | { |
|
278 | 278 | kwargs = new Array(); |
|
279 | 279 | other = new Array(); |
|
280 | 280 | for(var i = 0 ; i<matches.length ; ++i){ |
|
281 | 281 | if(matches[i].substr(-1) === '='){ |
|
282 | 282 | kwargs.push(matches[i]); |
|
283 | 283 | }else{other.push(matches[i]);} |
|
284 | 284 | } |
|
285 | 285 | newm = kwargs.concat(other); |
|
286 | 286 | matches = newm; |
|
287 | 287 | } |
|
288 | 288 | // end sort kwargs |
|
289 | 289 | |
|
290 | 290 | // give common prefix of a array of string |
|
291 | 291 | function sharedStart(A){ |
|
292 | 292 | if(A.length == 1){return A[0]} |
|
293 | 293 | if(A.length > 1 ){ |
|
294 | 294 | var tem1, tem2, s, A = A.slice(0).sort(); |
|
295 | 295 | tem1 = A[0]; |
|
296 | 296 | s = tem1.length; |
|
297 | 297 | tem2 = A.pop(); |
|
298 | 298 | while(s && tem2.indexOf(tem1) == -1){ |
|
299 | 299 | tem1 = tem1.substring(0, --s); |
|
300 | 300 | } |
|
301 | 301 | return tem1; |
|
302 | 302 | } |
|
303 | 303 | return ""; |
|
304 | 304 | } |
|
305 | 305 | |
|
306 | 306 | |
|
307 | 307 | //try to check if the user is typing tab at least twice after a word |
|
308 | 308 | // and completion is "done" |
|
309 | 309 | fallback_on_tooltip_after = 2 |
|
310 | 310 | if(matches.length == 1 && matched_text === matches[0]) |
|
311 | 311 | { |
|
312 | 312 | if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text) |
|
313 | 313 | { |
|
314 | 314 | console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !'); |
|
315 | 315 | console.log('You should understand that there is no (more) completion for that !'); |
|
316 | 316 | console.log("I'll show you the tooltip, will you stop bothering me ?"); |
|
317 | 317 | this.request_tooltip_after_time(matched_text+'(',0); |
|
318 | 318 | return; |
|
319 | 319 | } |
|
320 | 320 | this.prevmatch = matched_text |
|
321 | 321 | this.npressed = this.npressed+1; |
|
322 | 322 | } |
|
323 | 323 | else |
|
324 | 324 | { |
|
325 | 325 | this.prevmatch = ""; |
|
326 | 326 | this.npressed = 0; |
|
327 | 327 | } |
|
328 | 328 | // end fallback on tooltip |
|
329 | 329 | //================================== |
|
330 | 330 | // Real completion logic start here |
|
331 | 331 | var that = this; |
|
332 | 332 | var cur = this.completion_cursor; |
|
333 | 333 | var done = false; |
|
334 | 334 | |
|
335 | 335 | // call to dismmiss the completer |
|
336 | 336 | var close = function () { |
|
337 | 337 | if (done) return; |
|
338 | 338 | done = true; |
|
339 | 339 | if (complete != undefined) |
|
340 | 340 | {complete.remove();} |
|
341 | 341 | that.is_completing = false; |
|
342 | 342 | that.completion_cursor = null; |
|
343 | 343 | }; |
|
344 | 344 | |
|
345 | 345 | // update codemirror with the typed text |
|
346 | 346 | prev = matched_text |
|
347 | 347 | var update = function (inserted_text, event) { |
|
348 | 348 | that.code_mirror.replaceRange( |
|
349 | 349 | inserted_text, |
|
350 | 350 | {line: cur.line, ch: (cur.ch-matched_text.length)}, |
|
351 | 351 | {line: cur.line, ch: (cur.ch+prev.length-matched_text.length)} |
|
352 | 352 | ); |
|
353 | 353 | prev = inserted_text |
|
354 | 354 | if(event != null){ |
|
355 | 355 | event.stopPropagation(); |
|
356 | 356 | event.preventDefault(); |
|
357 | 357 | } |
|
358 | 358 | }; |
|
359 | 359 | // insert the given text and exit the completer |
|
360 | 360 | var insert = function (selected_text, event) { |
|
361 | 361 | update(selected_text) |
|
362 | 362 | close(); |
|
363 | 363 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
364 | 364 | }; |
|
365 | 365 | |
|
366 | 366 | // insert the curent highlited selection and exit |
|
367 | 367 | var pick = function () { |
|
368 | 368 | insert(select.val()[0],null); |
|
369 | 369 | }; |
|
370 | 370 | |
|
371 | 371 | |
|
372 | 372 | // Define function to clear the completer, refill it with the new |
|
373 | 373 | // matches, update the pseuso typing field. autopick insert match if |
|
374 | 374 | // only one left, in no matches (anymore) dismiss itself by pasting |
|
375 | 375 | // what the user have typed until then |
|
376 | 376 | var complete_with = function(matches,typed_text,autopick,event) |
|
377 | 377 | { |
|
378 | 378 | // If autopick an only one match, past. |
|
379 | 379 | // Used to 'pick' when pressing tab |
|
380 | 380 | if (matches.length < 1) { |
|
381 | 381 | insert(typed_text,event); |
|
382 | 382 | if(event != null){ |
|
383 | 383 | event.stopPropagation(); |
|
384 | 384 | event.preventDefault(); |
|
385 | 385 | } |
|
386 | 386 | } else if (autopick && matches.length == 1) { |
|
387 | 387 | insert(matches[0],event); |
|
388 | 388 | if(event != null){ |
|
389 | 389 | event.stopPropagation(); |
|
390 | 390 | event.preventDefault(); |
|
391 | 391 | } |
|
392 | 392 | } |
|
393 | 393 | //clear the previous completion if any |
|
394 | 394 | update(typed_text,event); |
|
395 | 395 | complete.children().children().remove(); |
|
396 | 396 | $('#asyoutype').html("<b>"+matched_text+"</b>"+typed_text.substr(matched_text.length)); |
|
397 | 397 | select = $('#asyoutypeselect'); |
|
398 | 398 | for (var i = 0; i<matches.length; ++i) { |
|
399 | 399 | select.append($('<option/>').html(matches[i])); |
|
400 | 400 | } |
|
401 | 401 | select.children().first().attr('selected','true'); |
|
402 | 402 | } |
|
403 | 403 | |
|
404 | 404 | // create html for completer |
|
405 | 405 | var complete = $('<div/>').addClass('completions'); |
|
406 | 406 | complete.attr('id','complete'); |
|
407 | 407 | complete.append($('<p/>').attr('id', 'asyoutype').html('<b>fixed part</b>user part'));//pseudo input field |
|
408 | 408 | |
|
409 | 409 | var select = $('<select/>').attr('multiple','true'); |
|
410 | 410 | select.attr('id', 'asyoutypeselect') |
|
411 | 411 | select.attr('size',Math.min(10,matches.length)); |
|
412 | 412 | var pos = this.code_mirror.cursorCoords(); |
|
413 | 413 | |
|
414 | 414 | // TODO: I propose to remove enough horizontal pixel |
|
415 | 415 | // to align the text later |
|
416 | 416 | complete.css('left',pos.x+'px'); |
|
417 | 417 | complete.css('top',pos.yBot+'px'); |
|
418 | 418 | complete.append(select); |
|
419 | 419 | |
|
420 | 420 | $('body').append(complete); |
|
421 | 421 | |
|
422 | 422 | // So a first actual completion. see if all the completion start wit |
|
423 | 423 | // the same letter and complete if necessary |
|
424 | 424 | fastForward = sharedStart(matches) |
|
425 | 425 | typed_characters = fastForward.substr(matched_text.length); |
|
426 | 426 | complete_with(matches,matched_text+typed_characters,true,null); |
|
427 | 427 | filterd = matches; |
|
428 | 428 | // Give focus to select, and make it filter the match as the user type |
|
429 | 429 | // by filtering the previous matches. Called by .keypress and .keydown |
|
430 | 430 | var downandpress = function (event,press_or_down) { |
|
431 | 431 | var code = event.which; |
|
432 | 432 | var autopick = false; // auto 'pick' if only one match |
|
433 | 433 | if (press_or_down === 0){ |
|
434 | 434 | press = true; down = false; //Are we called from keypress or keydown |
|
435 | 435 | } else if (press_or_down == 1){ |
|
436 | 436 | press = false; down = true; |
|
437 | 437 | } |
|
438 | 438 | if (code === key.shift) { |
|
439 | 439 | // nothing on Shift |
|
440 | 440 | return; |
|
441 | 441 | } |
|
442 | 442 | if (key.dismissAndAppend(code) && press) { |
|
443 | 443 | var newchar = String.fromCharCode(code); |
|
444 | 444 | typed_characters = typed_characters+newchar; |
|
445 | 445 | insert(matched_text+typed_characters,event); |
|
446 | 446 | return |
|
447 | 447 | } |
|
448 | 448 | if (code === key.enter) { |
|
449 | 449 | // Pressing ENTER will cause a pick |
|
450 | 450 | event.stopPropagation(); |
|
451 | 451 | event.preventDefault(); |
|
452 | 452 | pick(); |
|
453 | 453 | } else if (code === 38 || code === 40) { |
|
454 | 454 | // We don't want the document keydown handler to handle UP/DOWN, |
|
455 | 455 | // but we want the default action. |
|
456 | 456 | event.stopPropagation(); |
|
457 | 457 | } else if ( (code == key.backspace)||(code == key.tab && down) || press || key.isCompSymbol(code)){ |
|
458 | 458 | if( key.isCompSymbol(code) && press) |
|
459 | 459 | { |
|
460 | 460 | var newchar = String.fromCharCode(code); |
|
461 | 461 | typed_characters = typed_characters+newchar; |
|
462 | 462 | } else if (code == key.tab) { |
|
463 | 463 | fastForward = sharedStart(filterd) |
|
464 | 464 | ffsub = fastForward.substr(matched_text.length+typed_characters.length); |
|
465 | 465 | typed_characters = typed_characters+ffsub; |
|
466 | 466 | autopick = true; |
|
467 | 467 | } else if (code == key.backspace && down) { |
|
468 | 468 | // cancel if user have erase everything, otherwise decrease |
|
469 | 469 | // what we filter with |
|
470 | 470 | event.preventDefault(); |
|
471 | 471 | if (typed_characters.length <= 0) |
|
472 | 472 | { |
|
473 | 473 | insert(matched_text,event) |
|
474 | 474 | return |
|
475 | 475 | } |
|
476 | 476 | typed_characters = typed_characters.substr(0,typed_characters.length-1); |
|
477 | 477 | } else if (press && code != key.backspace && code != key.tab && code != 0){ |
|
478 | 478 | insert(matched_text+typed_characters,event); |
|
479 | 479 | return |
|
480 | 480 | } else { |
|
481 | 481 | return |
|
482 | 482 | } |
|
483 | 483 | re = new RegExp("^"+"\%?"+matched_text+typed_characters,""); |
|
484 | 484 | filterd = matches.filter(function(x){return re.test(x)}); |
|
485 | 485 | complete_with(filterd,matched_text+typed_characters,autopick,event); |
|
486 | 486 | } else if( code == key.esc) { |
|
487 | 487 | // dismiss the completer and go back to before invoking it |
|
488 | 488 | insert(matched_text,event); |
|
489 | 489 | } else if( press ){ // abort only on .keypress or esc |
|
490 | 490 | // abort with what the user have pressed until now |
|
491 | 491 | console.log('aborting with keycode : '+code+' is down :'+down); |
|
492 | 492 | } |
|
493 | 493 | } |
|
494 | 494 | select.keydown(function (event) { |
|
495 | 495 | downandpress(event,1) |
|
496 | 496 | }); |
|
497 | 497 | select.keypress(function (event) { |
|
498 | 498 | downandpress(event,0) |
|
499 | 499 | }); |
|
500 | 500 | // Double click also causes a pick. |
|
501 | 501 | // and bind the last actions. |
|
502 | 502 | select.dblclick(pick); |
|
503 | 503 | select.blur(close); |
|
504 | 504 | select.focus(); |
|
505 | 505 | }; |
|
506 | 506 | |
|
507 | 507 | CodeCell.prototype.toggle_line_numbers = function () { |
|
508 | 508 | if (this.code_mirror.getOption('lineNumbers') == false) { |
|
509 | 509 | this.code_mirror.setOption('lineNumbers', true); |
|
510 | 510 | } else { |
|
511 | 511 | this.code_mirror.setOption('lineNumbers', false); |
|
512 | 512 | } |
|
513 | 513 | this.code_mirror.refresh(); |
|
514 | 514 | }; |
|
515 | 515 | |
|
516 | 516 | CodeCell.prototype.select = function () { |
|
517 | 517 | IPython.Cell.prototype.select.apply(this); |
|
518 | 518 | // Todo: this dance is needed because as of CodeMirror 2.12, focus is |
|
519 | 519 | // not causing the cursor to blink if the editor is empty initially. |
|
520 | 520 | // While this seems to fix the issue, this should be fixed |
|
521 | 521 | // in CodeMirror proper. |
|
522 | 522 | var s = this.code_mirror.getValue(); |
|
523 | 523 | this.code_mirror.focus(); |
|
524 | 524 | if (s === '') this.code_mirror.setValue(''); |
|
525 | 525 | }; |
|
526 | 526 | |
|
527 | 527 | |
|
528 | 528 | CodeCell.prototype.select_all = function () { |
|
529 | 529 | var start = {line: 0, ch: 0}; |
|
530 | 530 | var nlines = this.code_mirror.lineCount(); |
|
531 | 531 | var last_line = this.code_mirror.getLine(nlines-1); |
|
532 | 532 | var end = {line: nlines-1, ch: last_line.length}; |
|
533 | 533 | this.code_mirror.setSelection(start, end); |
|
534 | 534 | }; |
|
535 | 535 | |
|
536 | 536 | |
|
537 | 537 | CodeCell.prototype.append_output = function (json) { |
|
538 | 538 | this.expand(); |
|
539 | 539 | if (json.output_type === 'pyout') { |
|
540 | 540 | this.append_pyout(json); |
|
541 | 541 | } else if (json.output_type === 'pyerr') { |
|
542 | 542 | this.append_pyerr(json); |
|
543 | 543 | } else if (json.output_type === 'display_data') { |
|
544 | 544 | this.append_display_data(json); |
|
545 | 545 | } else if (json.output_type === 'stream') { |
|
546 | 546 | this.append_stream(json); |
|
547 | 547 | }; |
|
548 | 548 | this.outputs.push(json); |
|
549 | 549 | }; |
|
550 | 550 | |
|
551 | 551 | |
|
552 | 552 | CodeCell.prototype.create_output_area = function () { |
|
553 | 553 | var oa = $("<div/>").addClass("hbox output_area"); |
|
554 | 554 | oa.append($('<div/>').addClass('prompt')); |
|
555 | 555 | return oa; |
|
556 | 556 | }; |
|
557 | 557 | |
|
558 | 558 | |
|
559 | 559 | CodeCell.prototype.append_pyout = function (json) { |
|
560 | 560 | n = json.prompt_number || ' '; |
|
561 | 561 | var toinsert = this.create_output_area(); |
|
562 | 562 | toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); |
|
563 | 563 | this.append_mime_type(json, toinsert); |
|
564 | 564 | this.element.find('div.output').append(toinsert); |
|
565 | 565 | // If we just output latex, typeset it. |
|
566 | 566 | if ((json.latex !== undefined) || (json.html !== undefined)) { |
|
567 | 567 | this.typeset(); |
|
568 | 568 | }; |
|
569 | 569 | }; |
|
570 | 570 | |
|
571 | 571 | |
|
572 | 572 | CodeCell.prototype.append_pyerr = function (json) { |
|
573 | 573 | var tb = json.traceback; |
|
574 | 574 | if (tb !== undefined && tb.length > 0) { |
|
575 | 575 | var s = ''; |
|
576 | 576 | var len = tb.length; |
|
577 | 577 | for (var i=0; i<len; i++) { |
|
578 | 578 | s = s + tb[i] + '\n'; |
|
579 | 579 | } |
|
580 | 580 | s = s + '\n'; |
|
581 | 581 | var toinsert = this.create_output_area(); |
|
582 | 582 | this.append_text(s, toinsert); |
|
583 | 583 | this.element.find('div.output').append(toinsert); |
|
584 | 584 | }; |
|
585 | 585 | }; |
|
586 | 586 | |
|
587 | 587 | |
|
588 | 588 | CodeCell.prototype.append_stream = function (json) { |
|
589 | 589 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
590 | 590 | // default to most likely stdout: |
|
591 | 591 | if (json.stream == undefined){ |
|
592 | 592 | json.stream = 'stdout'; |
|
593 | 593 | } |
|
594 | 594 | var subclass = "output_"+json.stream; |
|
595 | 595 | if (this.outputs.length > 0){ |
|
596 | 596 | // have at least one output to consider |
|
597 | 597 | var last = this.outputs[this.outputs.length-1]; |
|
598 | 598 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
599 | 599 | // latest output was in the same stream, |
|
600 | 600 | // so append directly into its pre tag |
|
601 | 601 | // escape ANSI & HTML specials: |
|
602 | 602 | var text = utils.fixConsole(json.text); |
|
603 | 603 | this.element.find('div.'+subclass).last().find('pre').append(text); |
|
604 | 604 | return; |
|
605 | 605 | } |
|
606 | 606 | } |
|
607 | 607 | |
|
608 | 608 | // If we got here, attach a new div |
|
609 | 609 | var toinsert = this.create_output_area(); |
|
610 | 610 | this.append_text(json.text, toinsert, "output_stream "+subclass); |
|
611 | 611 | this.element.find('div.output').append(toinsert); |
|
612 | 612 | }; |
|
613 | 613 | |
|
614 | 614 | |
|
615 | 615 | CodeCell.prototype.append_display_data = function (json) { |
|
616 | 616 | var toinsert = this.create_output_area(); |
|
617 | 617 | this.append_mime_type(json, toinsert); |
|
618 | 618 | this.element.find('div.output').append(toinsert); |
|
619 | 619 | // If we just output latex, typeset it. |
|
620 | 620 | if ( (json.latex !== undefined) || (json.html !== undefined) ) { |
|
621 | 621 | this.typeset(); |
|
622 | 622 | }; |
|
623 | 623 | }; |
|
624 | 624 | |
|
625 | 625 | |
|
626 | 626 | CodeCell.prototype.append_mime_type = function (json, element) { |
|
627 | 627 | if (json.html !== undefined) { |
|
628 | 628 | this.append_html(json.html, element); |
|
629 | 629 | } else if (json.latex !== undefined) { |
|
630 | 630 | this.append_latex(json.latex, element); |
|
631 | 631 | } else if (json.svg !== undefined) { |
|
632 | 632 | this.append_svg(json.svg, element); |
|
633 | 633 | } else if (json.png !== undefined) { |
|
634 | 634 | this.append_png(json.png, element); |
|
635 | 635 | } else if (json.jpeg !== undefined) { |
|
636 | 636 | this.append_jpeg(json.jpeg, element); |
|
637 | 637 | } else if (json.text !== undefined) { |
|
638 | 638 | this.append_text(json.text, element); |
|
639 | 639 | }; |
|
640 | 640 | }; |
|
641 | 641 | |
|
642 | 642 | |
|
643 | 643 | CodeCell.prototype.append_html = function (html, element) { |
|
644 | 644 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html"); |
|
645 | 645 | toinsert.append(html); |
|
646 | 646 | element.append(toinsert); |
|
647 | 647 | }; |
|
648 | 648 | |
|
649 | 649 | |
|
650 | 650 | CodeCell.prototype.append_text = function (data, element, extra_class) { |
|
651 | 651 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text"); |
|
652 | 652 | // escape ANSI & HTML specials in plaintext: |
|
653 | 653 | data = utils.fixConsole(data); |
|
654 | 654 | if (extra_class){ |
|
655 | 655 | toinsert.addClass(extra_class); |
|
656 | 656 | } |
|
657 | 657 | toinsert.append($("<pre/>").html(data)); |
|
658 | 658 | element.append(toinsert); |
|
659 | 659 | }; |
|
660 | 660 | |
|
661 | 661 | |
|
662 | 662 | CodeCell.prototype.append_svg = function (svg, element) { |
|
663 | 663 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg"); |
|
664 | 664 | toinsert.append(svg); |
|
665 | 665 | element.append(toinsert); |
|
666 | 666 | }; |
|
667 | 667 | |
|
668 | 668 | |
|
669 | 669 | CodeCell.prototype.append_png = function (png, element) { |
|
670 | 670 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png"); |
|
671 | 671 | toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png)); |
|
672 | 672 | element.append(toinsert); |
|
673 | 673 | }; |
|
674 | 674 | |
|
675 | 675 | |
|
676 | 676 | CodeCell.prototype.append_jpeg = function (jpeg, element) { |
|
677 | 677 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg"); |
|
678 | 678 | toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg)); |
|
679 | 679 | element.append(toinsert); |
|
680 | 680 | }; |
|
681 | 681 | |
|
682 | 682 | |
|
683 | 683 | CodeCell.prototype.append_latex = function (latex, element) { |
|
684 | 684 | // This method cannot do the typesetting because the latex first has to |
|
685 | 685 | // be on the page. |
|
686 | 686 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex"); |
|
687 | 687 | toinsert.append(latex); |
|
688 | 688 | element.append(toinsert); |
|
689 | 689 | }; |
|
690 | 690 | |
|
691 | 691 | |
|
692 | 692 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
693 | 693 | var output_div = this.element.find("div.output"); |
|
694 | 694 | if (stdout && stderr && other){ |
|
695 | 695 | // clear all, no need for logic |
|
696 | 696 | output_div.html(""); |
|
697 | 697 | this.outputs = []; |
|
698 | 698 | return; |
|
699 | 699 | } |
|
700 | 700 | // remove html output |
|
701 | 701 | // each output_subarea that has an identifying class is in an output_area |
|
702 | 702 | // which is the element to be removed. |
|
703 | 703 | if (stdout){ |
|
704 | 704 | output_div.find("div.output_stdout").parent().remove(); |
|
705 | 705 | } |
|
706 | 706 | if (stderr){ |
|
707 | 707 | output_div.find("div.output_stderr").parent().remove(); |
|
708 | 708 | } |
|
709 | 709 | if (other){ |
|
710 | 710 | output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove(); |
|
711 | 711 | } |
|
712 | 712 | |
|
713 | 713 | // remove cleared outputs from JSON list: |
|
714 | 714 | for (var i = this.outputs.length - 1; i >= 0; i--){ |
|
715 | 715 | var out = this.outputs[i]; |
|
716 | 716 | var output_type = out.output_type; |
|
717 | 717 | if (output_type == "display_data" && other){ |
|
718 | 718 | this.outputs.splice(i,1); |
|
719 | 719 | }else if (output_type == "stream"){ |
|
720 | 720 | if (stdout && out.stream == "stdout"){ |
|
721 | 721 | this.outputs.splice(i,1); |
|
722 | 722 | }else if (stderr && out.stream == "stderr"){ |
|
723 | 723 | this.outputs.splice(i,1); |
|
724 | 724 | } |
|
725 | 725 | } |
|
726 | 726 | } |
|
727 | 727 | }; |
|
728 | 728 | |
|
729 | 729 | |
|
730 | 730 | CodeCell.prototype.clear_input = function () { |
|
731 | 731 | this.code_mirror.setValue(''); |
|
732 | 732 | }; |
|
733 | 733 | |
|
734 | 734 | |
|
735 | 735 | CodeCell.prototype.collapse = function () { |
|
736 | 736 | if (!this.collapsed) { |
|
737 | 737 | this.element.find('div.output').hide(); |
|
738 | 738 | this.collapsed = true; |
|
739 | 739 | }; |
|
740 | 740 | }; |
|
741 | 741 | |
|
742 | 742 | |
|
743 | 743 | CodeCell.prototype.expand = function () { |
|
744 | 744 | if (this.collapsed) { |
|
745 | 745 | this.element.find('div.output').show(); |
|
746 | 746 | this.collapsed = false; |
|
747 | 747 | }; |
|
748 | 748 | }; |
|
749 | 749 | |
|
750 | 750 | |
|
751 | 751 | CodeCell.prototype.toggle_output = function () { |
|
752 | 752 | if (this.collapsed) { |
|
753 | 753 | this.expand(); |
|
754 | 754 | } else { |
|
755 | 755 | this.collapse(); |
|
756 | 756 | }; |
|
757 | 757 | }; |
|
758 | 758 | |
|
759 | 759 | CodeCell.prototype.set_input_prompt = function (number) { |
|
760 | var n = number || ' '; | |
|
761 | this.input_prompt_number = n; | |
|
762 | this.element.find('div.input_prompt').html('In [' + n + ']:'); | |
|
760 | this.input_prompt_number = number; | |
|
761 | var ns = number || " "; | |
|
762 | this.element.find('div.input_prompt').html('In [' + ns + ']:'); | |
|
763 | 763 | }; |
|
764 | 764 | |
|
765 | 765 | |
|
766 | 766 | CodeCell.prototype.get_code = function () { |
|
767 | 767 | return this.code_mirror.getValue(); |
|
768 | 768 | }; |
|
769 | 769 | |
|
770 | 770 | |
|
771 | 771 | CodeCell.prototype.set_code = function (code) { |
|
772 | 772 | return this.code_mirror.setValue(code); |
|
773 | 773 | }; |
|
774 | 774 | |
|
775 | 775 | |
|
776 | 776 | CodeCell.prototype.at_top = function () { |
|
777 | 777 | var cursor = this.code_mirror.getCursor(); |
|
778 | 778 | if (cursor.line === 0) { |
|
779 | 779 | return true; |
|
780 | 780 | } else { |
|
781 | 781 | return false; |
|
782 | 782 | } |
|
783 | 783 | }; |
|
784 | 784 | |
|
785 | 785 | |
|
786 | 786 | CodeCell.prototype.at_bottom = function () { |
|
787 | 787 | var cursor = this.code_mirror.getCursor(); |
|
788 | 788 | if (cursor.line === (this.code_mirror.lineCount()-1)) { |
|
789 | 789 | return true; |
|
790 | 790 | } else { |
|
791 | 791 | return false; |
|
792 | 792 | } |
|
793 | 793 | }; |
|
794 | 794 | |
|
795 | 795 | |
|
796 | 796 | CodeCell.prototype.fromJSON = function (data) { |
|
797 | 797 | console.log('Import from JSON:', data); |
|
798 | 798 | if (data.cell_type === 'code') { |
|
799 | 799 | if (data.input !== undefined) { |
|
800 | 800 | this.set_code(data.input); |
|
801 | 801 | } |
|
802 | 802 | if (data.prompt_number !== undefined) { |
|
803 | 803 | this.set_input_prompt(data.prompt_number); |
|
804 | 804 | } else { |
|
805 | 805 | this.set_input_prompt(); |
|
806 | 806 | }; |
|
807 | 807 | var len = data.outputs.length; |
|
808 | 808 | for (var i=0; i<len; i++) { |
|
809 | 809 | this.append_output(data.outputs[i]); |
|
810 | 810 | }; |
|
811 | 811 | if (data.collapsed !== undefined) { |
|
812 | 812 | if (data.collapsed) { |
|
813 | 813 | this.collapse(); |
|
814 | 814 | }; |
|
815 | 815 | }; |
|
816 | 816 | }; |
|
817 | 817 | }; |
|
818 | 818 | |
|
819 | 819 | |
|
820 | 820 | CodeCell.prototype.toJSON = function () { |
|
821 | 821 | var data = {}; |
|
822 | 822 | data.input = this.get_code(); |
|
823 | 823 | data.cell_type = 'code'; |
|
824 |
if (this.input_prompt_number |
|
|
824 | if (this.input_prompt_number) { | |
|
825 | 825 | data.prompt_number = this.input_prompt_number; |
|
826 | 826 | }; |
|
827 | 827 | var outputs = []; |
|
828 | 828 | var len = this.outputs.length; |
|
829 | 829 | for (var i=0; i<len; i++) { |
|
830 | 830 | outputs[i] = this.outputs[i]; |
|
831 | 831 | }; |
|
832 | 832 | data.outputs = outputs; |
|
833 | 833 | data.language = 'python'; |
|
834 | 834 | data.collapsed = this.collapsed; |
|
835 | 835 | // console.log('Export to JSON:',data); |
|
836 | 836 | return data; |
|
837 | 837 | }; |
|
838 | 838 | |
|
839 | 839 | |
|
840 | 840 | IPython.CodeCell = CodeCell; |
|
841 | 841 | |
|
842 | 842 | return IPython; |
|
843 | 843 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now