##// END OF EJS Templates
add is_typing utility...
MinRK -
Show More
@@ -1,291 +1,321 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2012 The IPython Development Team
2 // Copyright (C) 2008-2012 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 // Utilities
9 // Utilities
10 //============================================================================
10 //============================================================================
11
11
12 IPython.namespace('IPython.utils');
12 IPython.namespace('IPython.utils');
13
13
14 IPython.utils = (function (IPython) {
14 IPython.utils = (function (IPython) {
15
15
16 //============================================================================
16 //============================================================================
17 // Cross-browser RegEx Split
17 // Cross-browser RegEx Split
18 //============================================================================
18 //============================================================================
19
19
20 // This code has been MODIFIED from the code licensed below to not replace the
20 // This code has been MODIFIED from the code licensed below to not replace the
21 // default browser split. The license is reproduced here.
21 // default browser split. The license is reproduced here.
22
22
23 // see http://blog.stevenlevithan.com/archives/cross-browser-split for more info:
23 // see http://blog.stevenlevithan.com/archives/cross-browser-split for more info:
24 /*!
24 /*!
25 * Cross-Browser Split 1.1.1
25 * Cross-Browser Split 1.1.1
26 * Copyright 2007-2012 Steven Levithan <stevenlevithan.com>
26 * Copyright 2007-2012 Steven Levithan <stevenlevithan.com>
27 * Available under the MIT License
27 * Available under the MIT License
28 * ECMAScript compliant, uniform cross-browser split method
28 * ECMAScript compliant, uniform cross-browser split method
29 */
29 */
30
30
31 /**
31 /**
32 * Splits a string into an array of strings using a regex or string
32 * Splits a string into an array of strings using a regex or string
33 * separator. Matches of the separator are not included in the result array.
33 * separator. Matches of the separator are not included in the result array.
34 * However, if `separator` is a regex that contains capturing groups,
34 * However, if `separator` is a regex that contains capturing groups,
35 * backreferences are spliced into the result each time `separator` is
35 * backreferences are spliced into the result each time `separator` is
36 * matched. Fixes browser bugs compared to the native
36 * matched. Fixes browser bugs compared to the native
37 * `String.prototype.split` and can be used reliably cross-browser.
37 * `String.prototype.split` and can be used reliably cross-browser.
38 * @param {String} str String to split.
38 * @param {String} str String to split.
39 * @param {RegExp|String} separator Regex or string to use for separating
39 * @param {RegExp|String} separator Regex or string to use for separating
40 * the string.
40 * the string.
41 * @param {Number} [limit] Maximum number of items to include in the result
41 * @param {Number} [limit] Maximum number of items to include in the result
42 * array.
42 * array.
43 * @returns {Array} Array of substrings.
43 * @returns {Array} Array of substrings.
44 * @example
44 * @example
45 *
45 *
46 * // Basic use
46 * // Basic use
47 * regex_split('a b c d', ' ');
47 * regex_split('a b c d', ' ');
48 * // -> ['a', 'b', 'c', 'd']
48 * // -> ['a', 'b', 'c', 'd']
49 *
49 *
50 * // With limit
50 * // With limit
51 * regex_split('a b c d', ' ', 2);
51 * regex_split('a b c d', ' ', 2);
52 * // -> ['a', 'b']
52 * // -> ['a', 'b']
53 *
53 *
54 * // Backreferences in result array
54 * // Backreferences in result array
55 * regex_split('..word1 word2..', /([a-z]+)(\d+)/i);
55 * regex_split('..word1 word2..', /([a-z]+)(\d+)/i);
56 * // -> ['..', 'word', '1', ' ', 'word', '2', '..']
56 * // -> ['..', 'word', '1', ' ', 'word', '2', '..']
57 */
57 */
58 var regex_split = function (str, separator, limit) {
58 var regex_split = function (str, separator, limit) {
59 // If `separator` is not a regex, use `split`
59 // If `separator` is not a regex, use `split`
60 if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
60 if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
61 return split.call(str, separator, limit);
61 return split.call(str, separator, limit);
62 }
62 }
63 var output = [],
63 var output = [],
64 flags = (separator.ignoreCase ? "i" : "") +
64 flags = (separator.ignoreCase ? "i" : "") +
65 (separator.multiline ? "m" : "") +
65 (separator.multiline ? "m" : "") +
66 (separator.extended ? "x" : "") + // Proposed for ES6
66 (separator.extended ? "x" : "") + // Proposed for ES6
67 (separator.sticky ? "y" : ""), // Firefox 3+
67 (separator.sticky ? "y" : ""), // Firefox 3+
68 lastLastIndex = 0,
68 lastLastIndex = 0,
69 // Make `global` and avoid `lastIndex` issues by working with a copy
69 // Make `global` and avoid `lastIndex` issues by working with a copy
70 separator = new RegExp(separator.source, flags + "g"),
70 separator = new RegExp(separator.source, flags + "g"),
71 separator2, match, lastIndex, lastLength;
71 separator2, match, lastIndex, lastLength;
72 str += ""; // Type-convert
72 str += ""; // Type-convert
73
73
74 var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined"
74 var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined"
75 if (!compliantExecNpcg) {
75 if (!compliantExecNpcg) {
76 // Doesn't need flags gy, but they don't hurt
76 // Doesn't need flags gy, but they don't hurt
77 separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
77 separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
78 }
78 }
79 /* Values for `limit`, per the spec:
79 /* Values for `limit`, per the spec:
80 * If undefined: 4294967295 // Math.pow(2, 32) - 1
80 * If undefined: 4294967295 // Math.pow(2, 32) - 1
81 * If 0, Infinity, or NaN: 0
81 * If 0, Infinity, or NaN: 0
82 * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
82 * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
83 * If negative number: 4294967296 - Math.floor(Math.abs(limit))
83 * If negative number: 4294967296 - Math.floor(Math.abs(limit))
84 * If other: Type-convert, then use the above rules
84 * If other: Type-convert, then use the above rules
85 */
85 */
86 limit = typeof(limit) === "undefined" ?
86 limit = typeof(limit) === "undefined" ?
87 -1 >>> 0 : // Math.pow(2, 32) - 1
87 -1 >>> 0 : // Math.pow(2, 32) - 1
88 limit >>> 0; // ToUint32(limit)
88 limit >>> 0; // ToUint32(limit)
89 while (match = separator.exec(str)) {
89 while (match = separator.exec(str)) {
90 // `separator.lastIndex` is not reliable cross-browser
90 // `separator.lastIndex` is not reliable cross-browser
91 lastIndex = match.index + match[0].length;
91 lastIndex = match.index + match[0].length;
92 if (lastIndex > lastLastIndex) {
92 if (lastIndex > lastLastIndex) {
93 output.push(str.slice(lastLastIndex, match.index));
93 output.push(str.slice(lastLastIndex, match.index));
94 // Fix browsers whose `exec` methods don't consistently return `undefined` for
94 // Fix browsers whose `exec` methods don't consistently return `undefined` for
95 // nonparticipating capturing groups
95 // nonparticipating capturing groups
96 if (!compliantExecNpcg && match.length > 1) {
96 if (!compliantExecNpcg && match.length > 1) {
97 match[0].replace(separator2, function () {
97 match[0].replace(separator2, function () {
98 for (var i = 1; i < arguments.length - 2; i++) {
98 for (var i = 1; i < arguments.length - 2; i++) {
99 if (typeof(arguments[i]) === "undefined") {
99 if (typeof(arguments[i]) === "undefined") {
100 match[i] = undefined;
100 match[i] = undefined;
101 }
101 }
102 }
102 }
103 });
103 });
104 }
104 }
105 if (match.length > 1 && match.index < str.length) {
105 if (match.length > 1 && match.index < str.length) {
106 Array.prototype.push.apply(output, match.slice(1));
106 Array.prototype.push.apply(output, match.slice(1));
107 }
107 }
108 lastLength = match[0].length;
108 lastLength = match[0].length;
109 lastLastIndex = lastIndex;
109 lastLastIndex = lastIndex;
110 if (output.length >= limit) {
110 if (output.length >= limit) {
111 break;
111 break;
112 }
112 }
113 }
113 }
114 if (separator.lastIndex === match.index) {
114 if (separator.lastIndex === match.index) {
115 separator.lastIndex++; // Avoid an infinite loop
115 separator.lastIndex++; // Avoid an infinite loop
116 }
116 }
117 }
117 }
118 if (lastLastIndex === str.length) {
118 if (lastLastIndex === str.length) {
119 if (lastLength || !separator.test("")) {
119 if (lastLength || !separator.test("")) {
120 output.push("");
120 output.push("");
121 }
121 }
122 } else {
122 } else {
123 output.push(str.slice(lastLastIndex));
123 output.push(str.slice(lastLastIndex));
124 }
124 }
125 return output.length > limit ? output.slice(0, limit) : output;
125 return output.length > limit ? output.slice(0, limit) : output;
126 };
126 };
127
127
128 //============================================================================
128 //============================================================================
129 // End contributed Cross-browser RegEx Split
129 // End contributed Cross-browser RegEx Split
130 //============================================================================
130 //============================================================================
131
131
132
132
133 var uuid = function () {
133 var uuid = function () {
134 // http://www.ietf.org/rfc/rfc4122.txt
134 // http://www.ietf.org/rfc/rfc4122.txt
135 var s = [];
135 var s = [];
136 var hexDigits = "0123456789ABCDEF";
136 var hexDigits = "0123456789ABCDEF";
137 for (var i = 0; i < 32; i++) {
137 for (var i = 0; i < 32; i++) {
138 s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
138 s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
139 }
139 }
140 s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
140 s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
141 s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
141 s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
142
142
143 var uuid = s.join("");
143 var uuid = s.join("");
144 return uuid;
144 return uuid;
145 };
145 };
146
146
147
147
148 //Fix raw text to parse correctly in crazy XML
148 //Fix raw text to parse correctly in crazy XML
149 function xmlencode(string) {
149 function xmlencode(string) {
150 return string.replace(/\&/g,'&'+'amp;')
150 return string.replace(/\&/g,'&'+'amp;')
151 .replace(/</g,'&'+'lt;')
151 .replace(/</g,'&'+'lt;')
152 .replace(/>/g,'&'+'gt;')
152 .replace(/>/g,'&'+'gt;')
153 .replace(/\'/g,'&'+'apos;')
153 .replace(/\'/g,'&'+'apos;')
154 .replace(/\"/g,'&'+'quot;')
154 .replace(/\"/g,'&'+'quot;')
155 .replace(/`/g,'&'+'#96;');
155 .replace(/`/g,'&'+'#96;');
156 }
156 }
157
157
158
158
159 //Map from terminal commands to CSS classes
159 //Map from terminal commands to CSS classes
160 var ansi_colormap = {
160 var ansi_colormap = {
161 "01":"ansibold",
161 "01":"ansibold",
162 "30":"ansiblack",
162 "30":"ansiblack",
163 "31":"ansired",
163 "31":"ansired",
164 "32":"ansigreen",
164 "32":"ansigreen",
165 "33":"ansiyellow",
165 "33":"ansiyellow",
166 "34":"ansiblue",
166 "34":"ansiblue",
167 "35":"ansipurple",
167 "35":"ansipurple",
168 "36":"ansicyan",
168 "36":"ansicyan",
169 "37":"ansigrey"
169 "37":"ansigrey"
170 };
170 };
171
171
172 function ansispan(str) {
172 function ansispan(str) {
173 // ansispan function adapted from github.com/mmalecki/ansispan (MIT License)
173 // ansispan function adapted from github.com/mmalecki/ansispan (MIT License)
174 Object.keys(ansi_colormap).forEach(function(ansi) {
174 Object.keys(ansi_colormap).forEach(function(ansi) {
175 var span = '<span class="' + ansi_colormap[ansi] + '">';
175 var span = '<span class="' + ansi_colormap[ansi] + '">';
176
176
177 //
177 //
178 // `\033[Xm` == `\033[0;Xm` sets foreground color to `X`.
178 // `\033[Xm` == `\033[0;Xm` sets foreground color to `X`.
179 //
179 //
180 str = str.replace(
180 str = str.replace(
181 new RegExp('\033\\[([01];)?' + ansi + 'm', 'g'), span
181 new RegExp('\033\\[([01];)?' + ansi + 'm', 'g'), span
182 );
182 );
183 });
183 });
184
184
185 str = str.replace(/\033\[([01]|39|22)?m/g, '</span>');
185 str = str.replace(/\033\[([01]|39|22)?m/g, '</span>');
186 return str;
186 return str;
187 };
187 };
188
188
189 // Transform ANSI color escape codes into HTML <span> tags with css
189 // Transform ANSI color escape codes into HTML <span> tags with css
190 // classes listed in the above ansi_colormap object. The actual color used
190 // classes listed in the above ansi_colormap object. The actual color used
191 // are set in the css file.
191 // are set in the css file.
192 function fixConsole(txt) {
192 function fixConsole(txt) {
193 txt = xmlencode(txt);
193 txt = xmlencode(txt);
194 var re = /\033\[([\dA-Fa-f;]*?)m/;
194 var re = /\033\[([\dA-Fa-f;]*?)m/;
195 var opened = false;
195 var opened = false;
196 var cmds = [];
196 var cmds = [];
197 var opener = "";
197 var opener = "";
198 var closer = "";
198 var closer = "";
199
199
200 // Strip all ANSI codes that are not color related. Matches
200 // Strip all ANSI codes that are not color related. Matches
201 // all ANSI codes that do not end with "m".
201 // all ANSI codes that do not end with "m".
202 var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g;
202 var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g;
203 txt = txt.replace(ignored_re, "");
203 txt = txt.replace(ignored_re, "");
204
204
205 // color ansi codes
205 // color ansi codes
206 txt = ansispan(txt);
206 txt = ansispan(txt);
207 return txt;
207 return txt;
208 }
208 }
209
209
210 // Remove chunks that should be overridden by the effect of
210 // Remove chunks that should be overridden by the effect of
211 // carriage return characters
211 // carriage return characters
212 function fixCarriageReturn(txt) {
212 function fixCarriageReturn(txt) {
213 var tmp = txt;
213 var tmp = txt;
214 do {
214 do {
215 txt = tmp;
215 txt = tmp;
216 tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
216 tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
217 tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line
217 tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line
218 } while (tmp.length < txt.length);
218 } while (tmp.length < txt.length);
219 return txt;
219 return txt;
220 }
220 }
221
221
222 // Locate any URLs and convert them to a anchor tag
222 // Locate any URLs and convert them to a anchor tag
223 function autoLinkUrls(txt) {
223 function autoLinkUrls(txt) {
224 return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi,
224 return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi,
225 "$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>");
225 "$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>");
226 }
226 }
227
227
228 // some keycodes that seem to be platform/browser independant
228 // some keycodes that seem to be platform/browser independent
229 var keycodes ={
229 var keycodes = {
230 BACKSPACE: 8,
230 BACKSPACE: 8,
231 TAB : 9,
231 TAB : 9,
232 ENTER : 13,
232 ENTER : 13,
233 SHIFT : 16,
233 SHIFT : 16,
234 CTRL : 17,
234 CTRL : 17,
235 CONTROL : 17,
235 CONTROL : 17,
236 ALT : 18,
236 ALT : 18,
237 CAPS_LOCK: 20,
237 CAPS_LOCK: 20,
238 ESC : 27,
238 ESC : 27,
239 SPACE : 32,
239 SPACE : 32,
240 PGUP : 33,
240 PGUP : 33,
241 PGDOWN : 34,
241 PGDOWN : 34,
242 END : 35,
242 END : 35,
243 HOME : 36,
243 HOME : 36,
244 LEFT_ARROW: 37,
244 LEFT_ARROW: 37,
245 LEFTARROW: 37,
245 LEFTARROW: 37,
246 LEFT : 37,
246 LEFT : 37,
247 UP_ARROW : 38,
247 UP_ARROW : 38,
248 UPARROW : 38,
248 UPARROW : 38,
249 UP : 38,
249 UP : 38,
250 RIGHT_ARROW:39,
250 RIGHT_ARROW:39,
251 RIGHTARROW:39,
251 RIGHTARROW:39,
252 RIGHT : 39,
252 RIGHT : 39,
253 DOWN_ARROW: 40,
253 DOWN_ARROW: 40,
254 DOWNARROW: 40,
254 DOWNARROW: 40,
255 DOWN : 40,
255 DOWN : 40,
256 COMMAND : 91,
256 LEFT_SUPER : 91,
257 RIGHT_SUPER : 92,
258 COMMAND : 93,
259 };
260
261
262 var is_typing = function (event) {
263 // return whether a key event is probably typing (used for setting the dirty flag)
264 var key = event.which;
265 if ( key < 46 ) {
266 if (
267 ( key == keycodes.BACKSPACE ) ||
268 ( key == keycodes.TAB ) ||
269 ( key == keycodes.ENTER )
270 ) {
271 return true;
272 } else {
273 return false;
274 }
275 } else {
276 if (
277 ( key == keycodes.LEFT_SUPER ) ||
278 ( key == keycodes.RIGHT_SUPER ) ||
279 ( key == keycodes.COMMAND )
280 ) {
281 return false;
282 } else {
283 return true;
284 }
285 }
257 };
286 };
258
287
259
288
260 var points_to_pixels = function (points) {
289 var points_to_pixels = function (points) {
261 // A reasonably good way of converting between points and pixels.
290 // A reasonably good way of converting between points and pixels.
262 var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>');
291 var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>');
263 $(body).append(test);
292 $(body).append(test);
264 var pixel_per_point = test.width()/10000;
293 var pixel_per_point = test.width()/10000;
265 test.remove();
294 test.remove();
266 return Math.floor(points*pixel_per_point);
295 return Math.floor(points*pixel_per_point);
267 };
296 };
268
297
269 // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript
298 // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript
270 browser = (function() {
299 browser = (function() {
271 var N= navigator.appName, ua= navigator.userAgent, tem;
300 var N= navigator.appName, ua= navigator.userAgent, tem;
272 var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
301 var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
273 if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
302 if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
274 M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
303 M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
275 return M;
304 return M;
276 })();
305 })();
277
306
278
307
279 return {
308 return {
280 regex_split : regex_split,
309 regex_split : regex_split,
281 uuid : uuid,
310 uuid : uuid,
282 fixConsole : fixConsole,
311 fixConsole : fixConsole,
283 keycodes : keycodes,
312 keycodes : keycodes,
284 fixCarriageReturn : fixCarriageReturn,
313 fixCarriageReturn : fixCarriageReturn,
285 autoLinkUrls : autoLinkUrls,
314 autoLinkUrls : autoLinkUrls,
315 is_typing : is_typing,
286 points_to_pixels : points_to_pixels,
316 points_to_pixels : points_to_pixels,
287 browser : browser
317 browser : browser
288 };
318 };
289
319
290 }(IPython));
320 }(IPython));
291
321
General Comments 0
You need to be logged in to leave comments. Login now