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