Show More
This diff has been collapsed as it changes many lines, (634 lines changed) Show them Hide them | |||||
@@ -1,293 +1,579 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2011 The IPython Development Team |
|
2 | // Copyright (C) 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 | // Keyboard management |
|
9 | // Keyboard management | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | var IPython = (function (IPython) { |
|
12 | var IPython = (function (IPython) { | |
13 | "use strict"; |
|
13 | "use strict"; | |
14 |
|
14 | |||
15 | var key = IPython.utils.keycodes; |
|
15 | // Setup global keycodes and inverse keycodes. | |
16 |
|
16 | |||
17 | var KeyboardManager = function () { |
|
17 | // See http://unixpapa.com/js/key.html for a complete description. The short of | |
18 | this.mode = 'command'; |
|
18 | // it is that there are different keycode sets. Firefox uses the "Mozilla keycodes" | |
19 | this.enabled = true; |
|
19 | // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same | |
20 | this.delete_count = 0; |
|
20 | // but have minor differences. | |
21 | this.bind_events(); |
|
|||
22 | }; |
|
|||
23 |
|
21 | |||
24 | KeyboardManager.prototype.bind_events = function () { |
|
22 | // These apply to Firefox, (Webkit and IE) | |
25 | var that = this; |
|
23 | var _keycodes = { | |
26 | $(document).keydown(function (event) { |
|
24 | 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, | |
27 | return that.handle_keydown(event); |
|
25 | 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, | |
28 | }); |
|
26 | 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, | |
|
27 | '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54, | |||
|
28 | '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48, | |||
|
29 | '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191, | |||
|
30 | '\\ |': 220, '- _': 109, '\' "': 222, | |||
|
31 | 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100, | |||
|
32 | 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105, | |||
|
33 | 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111, | |||
|
34 | 'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118, | |||
|
35 | 'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126, | |||
|
36 | 'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, | |||
|
37 | 'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34, | |||
|
38 | 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40, | |||
|
39 | 'insert': 45, 'delete': 46, 'numlock': 144, | |||
29 | }; |
|
40 | }; | |
30 |
|
41 | |||
31 | KeyboardManager.prototype.handle_keydown = function (event) { |
|
42 | // These apply to Firefox and Opera | |
32 | var notebook = IPython.notebook; |
|
43 | var _mozilla_keycodes = { | |
|
44 | '; :': 59, '= +': 61 | |||
|
45 | } | |||
33 |
|
|
46 | ||
34 | console.log('keyboard_manager', this.mode, event.keyCode); |
|
47 | // This apply to Webkit and IE | |
|
48 | var _ie_keycodes = { | |||
|
49 | '; :': 186, '= +': 187, | |||
|
50 | } | |||
35 |
|
|
51 | ||
36 | if (event.which === key.ESC) { |
|
52 | var browser = IPython.utils.browser[0]; | |
37 | // Intercept escape at highest level to avoid closing |
|
53 | ||
38 | // websocket connection with firefox |
|
54 | if (browser === 'Firefox' || browser === 'Opera') { | |
39 | event.preventDefault(); |
|
55 | $.extend(_keycodes, _mozilla_keycodes); | |
|
56 | } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') { | |||
|
57 | $.extend(_keycodes, _ie_keycodes); | |||
40 |
|
|
58 | } | |
41 |
|
59 | |||
42 | if (!this.enabled) { |
|
60 | var keycodes = {}; | |
43 | return true; |
|
61 | var inv_keycodes = {}; | |
|
62 | for (var name in _keycodes) { | |||
|
63 | console.log(name); | |||
|
64 | var names = name.split(' '); | |||
|
65 | if (names.length === 1) { | |||
|
66 | var n = names[0] | |||
|
67 | keycodes[n] = _keycodes[n] | |||
|
68 | inv_keycodes[_keycodes[n]] = n | |||
|
69 | // console.log(keycodes[n], inv_keycodes[_keycodes[n]]); | |||
|
70 | } else { | |||
|
71 | var primary = names[0]; | |||
|
72 | var secondary = names[1]; | |||
|
73 | keycodes[primary] = _keycodes[name] | |||
|
74 | keycodes[secondary] = _keycodes[name] | |||
|
75 | inv_keycodes[_keycodes[name]] = primary | |||
|
76 | // console.log(keycodes[primary], keycodes[secondary], inv_keycodes[_keycodes[name]]) | |||
|
77 | } | |||
44 |
|
|
78 | } | |
45 |
|
79 | |||
46 | // Event handlers for both command and edit mode |
|
80 | ||
47 | if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { |
|
81 | // Default keyboard shortcuts | |
48 | // Save (CTRL+S) or (Command+S on Mac) |
|
82 | ||
49 | notebook.save_checkpoint(); |
|
83 | var default_common_shortcuts = { | |
|
84 | 'meta+s' : { | |||
|
85 | help : 'save notebook', | |||
|
86 | handler : function (event) { | |||
|
87 | IPython.notebook.save_checkpoint(); | |||
50 | event.preventDefault(); |
|
88 | event.preventDefault(); | |
51 | return false; |
|
89 | return false; | |
52 | } else if (event.which === key.ESC) { |
|
90 | } | |
53 | // Intercept escape at highest level to avoid closing |
|
91 | }, | |
54 | // websocket connection with firefox |
|
92 | 'ctrl+s' : { | |
|
93 | help : 'save notebook', | |||
|
94 | handler : function (event) { | |||
|
95 | IPython.notebook.save_checkpoint(); | |||
55 | event.preventDefault(); |
|
96 | event.preventDefault(); | |
56 | // Don't return yet to allow edit/command modes to handle |
|
97 | return false; | |
57 | } else if (event.which === key.SHIFT) { |
|
98 | } | |
|
99 | }, | |||
|
100 | 'shift' : { | |||
|
101 | help : '', | |||
|
102 | handler : function (event) { | |||
58 | // ignore shift keydown |
|
103 | // ignore shift keydown | |
59 | return true; |
|
104 | return true; | |
60 | } else if (event.which === key.ENTER && event.shiftKey) { |
|
105 | } | |
61 | notebook.execute_selected_cell('shift'); |
|
106 | }, | |
|
107 | 'shift+enter' : { | |||
|
108 | help : 'run cell', | |||
|
109 | handler : function (event) { | |||
|
110 | IPython.notebook.execute_selected_cell('shift'); | |||
62 | return false; |
|
111 | return false; | |
63 | } else if (event.which === key.ENTER && event.altKey) { |
|
112 | } | |
64 | // Execute code cell, and insert new in place |
|
113 | }, | |
65 | notebook.execute_selected_cell('alt'); |
|
114 | 'alt+enter' : { | |
|
115 | help : 'run cell', | |||
|
116 | handler : function (event) { | |||
|
117 | IPython.notebook.execute_selected_cell('alt'); | |||
66 | return false; |
|
118 | return false; | |
67 | } else if (event.which === key.ENTER && event.ctrlKey) { |
|
119 | } | |
68 | notebook.execute_selected_cell('ctrl'); |
|
120 | }, | |
|
121 | 'ctrl+enter' : { | |||
|
122 | help : 'run cell', | |||
|
123 | handler : function (event) { | |||
|
124 | IPython.notebook.execute_selected_cell('ctrl'); | |||
69 | return false; |
|
125 | return false; | |
70 | } |
|
126 | } | |
71 |
|
||||
72 | if (this.mode === 'edit') { |
|
|||
73 | return this.handle_edit_mode(event); |
|
|||
74 | } else if (this.mode === 'command' && !(event.ctrlKey || event.altKey || event.metaKey)) { |
|
|||
75 | return this.handle_command_mode(event); |
|
|||
76 | } |
|
127 | } | |
77 | } |
|
128 | } | |
78 |
|
129 | |||
79 | KeyboardManager.prototype.handle_edit_mode = function (event) { |
|
130 | // Edit mode defaults | |
80 | var notebook = IPython.notebook; |
|
|||
81 |
|
131 | |||
82 | if (event.which === key.ESC) { |
|
132 | var default_edit_shortcuts = { | |
83 | // ESC |
|
133 | 'esc' : { | |
84 |
|
|
134 | help : 'command mode', | |
|
135 | handler : function (event) { | |||
|
136 | IPython.notebook.command_mode(); | |||
85 | return false; |
|
137 | return false; | |
86 | } else if (event.which === 77 && event.ctrlKey) { |
|
138 | } | |
87 | // Ctrl-m |
|
139 | }, | |
88 | notebook.command_mode(); |
|
140 | 'ctrl+m' : { | |
|
141 | help : 'command mode', | |||
|
142 | handler : function (event) { | |||
|
143 | IPython.notebook.command_mode(); | |||
89 | return false; |
|
144 | return false; | |
90 | } else if (event.which === key.UPARROW && !event.shiftKey) { |
|
145 | } | |
91 | var cell = notebook.get_selected_cell(); |
|
146 | }, | |
|
147 | 'up' : { | |||
|
148 | help : 'select previous cell', | |||
|
149 | handler : function (event) { | |||
|
150 | var cell = IPython.notebook.get_selected_cell(); | |||
92 | if (cell && cell.at_top()) { |
|
151 | if (cell && cell.at_top()) { | |
93 | event.preventDefault(); |
|
152 | event.preventDefault(); | |
94 | notebook.command_mode() |
|
153 | IPython.notebook.command_mode() | |
95 | notebook.select_prev(); |
|
154 | IPython.notebook.select_prev(); | |
96 | notebook.edit_mode(); |
|
155 | IPython.notebook.edit_mode(); | |
97 | return false; |
|
156 | return false; | |
98 | }; |
|
157 | }; | |
99 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { |
|
158 | } | |
100 | var cell = notebook.get_selected_cell(); |
|
159 | }, | |
|
160 | 'down' : { | |||
|
161 | help : 'select next cell', | |||
|
162 | handler : function (event) { | |||
|
163 | var cell = IPython.notebook.get_selected_cell(); | |||
101 | if (cell && cell.at_bottom()) { |
|
164 | if (cell && cell.at_bottom()) { | |
102 | event.preventDefault(); |
|
165 | event.preventDefault(); | |
103 | notebook.command_mode() |
|
166 | IPython.notebook.command_mode() | |
104 | notebook.select_next(); |
|
167 | IPython.notebook.select_next(); | |
105 | notebook.edit_mode(); |
|
168 | IPython.notebook.edit_mode(); | |
106 | return false; |
|
169 | return false; | |
107 | }; |
|
170 | }; | |
108 | }; |
|
|||
109 | return true; |
|
|||
110 | } |
|
171 | } | |
|
172 | }, | |||
111 |
|
173 | |||
112 | KeyboardManager.prototype.handle_command_mode = function (event) { |
|
174 | } | |
113 | var that = this; |
|
|||
114 | var notebook = IPython.notebook; |
|
|||
115 |
|
175 | |||
116 | if (event.which === key.ENTER && !(event.ctrlKey || event.altKey || event.shiftKey)) { |
|
176 | // Command mode defaults | |
117 | // Enter edit mode = ENTER alone |
|
177 | ||
118 | notebook.edit_mode(); |
|
178 | var default_command_shortcuts = { | |
|
179 | 'enter' : { | |||
|
180 | help : 'edit mode', | |||
|
181 | handler : function (event) { | |||
|
182 | IPython.notebook.edit_mode(); | |||
119 | return false; |
|
183 | return false; | |
120 | } else if (event.which === key.UPARROW && !event.shiftKey) { |
|
184 | } | |
121 | var index = notebook.get_selected_index(); |
|
185 | }, | |
|
186 | 'up' : { | |||
|
187 | help : 'select previous cell', | |||
|
188 | handler : function (event) { | |||
|
189 | var index = IPython.notebook.get_selected_index(); | |||
122 | if (index !== 0 && index !== null) { |
|
190 | if (index !== 0 && index !== null) { | |
123 | notebook.select_prev(); |
|
191 | IPython.notebook.select_prev(); | |
124 | var cell = notebook.get_selected_cell(); |
|
192 | var cell = IPython.notebook.get_selected_cell(); | |
125 | cell.focus_cell(); |
|
193 | cell.focus_cell(); | |
126 | }; |
|
194 | }; | |
127 | return false; |
|
195 | return false; | |
128 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { |
|
196 | } | |
129 | var index = notebook.get_selected_index(); |
|
197 | }, | |
130 | if (index !== (notebook.ncells()-1) && index !== null) { |
|
198 | 'down' : { | |
131 |
|
|
199 | help : 'select next cell', | |
132 | var cell = notebook.get_selected_cell(); |
|
200 | handler : function (event) { | |
|
201 | var index = IPython.notebook.get_selected_index(); | |||
|
202 | if (index !== (IPython.notebook.ncells()-1) && index !== null) { | |||
|
203 | IPython.notebook.select_next(); | |||
|
204 | var cell = IPython.notebook.get_selected_cell(); | |||
133 | cell.focus_cell(); |
|
205 | cell.focus_cell(); | |
134 | }; |
|
206 | }; | |
135 | return false; |
|
207 | return false; | |
136 | } else if (event.which === 88) { |
|
208 | } | |
137 | // Cut selected cell = x |
|
209 | }, | |
138 | notebook.cut_cell(); |
|
210 | 'x' : { | |
|
211 | help : 'cut cell', | |||
|
212 | handler : function (event) { | |||
|
213 | IPython.IPython.notebook.cut_cell(); | |||
139 | return false; |
|
214 | return false; | |
140 | } else if (event.which === 67) { |
|
215 | } | |
141 | // Copy selected cell = c |
|
216 | }, | |
142 | notebook.copy_cell(); |
|
217 | 'c' : { | |
|
218 | help : 'copy cell', | |||
|
219 | handler : function (event) { | |||
|
220 | IPython.IPython.notebook.copy_cell(); | |||
143 | return false; |
|
221 | return false; | |
144 | } else if (event.which === 86) { |
|
222 | } | |
145 | // Paste below selected cell = v |
|
223 | }, | |
146 | notebook.paste_cell_below(); |
|
224 | 'v' : { | |
|
225 | help : 'paste cell below', | |||
|
226 | handler : function (event) { | |||
|
227 | IPython.IPython.notebook.paste_cell_below(); | |||
147 | return false; |
|
228 | return false; | |
148 | } else if (event.which === 68) { |
|
229 | } | |
149 | // Delete selected cell = d |
|
230 | }, | |
150 | var dc = this.delete_count; |
|
231 | 'd' : { | |
|
232 | help : 'delete cell (press twice)', | |||
|
233 | handler : function (event) { | |||
|
234 | var dc = IPython.delete_count; | |||
151 | console.log('delete_count', dc); |
|
235 | console.log('delete_count', dc); | |
152 |
if (dc === |
|
236 | if (dc === undefined) { | |
153 |
|
|
237 | IPython.delete_count = 0; | |
|
238 | } else if (dc === 0) { | |||
|
239 | IPython.delete_count = 1; | |||
154 | setTimeout(function () { |
|
240 | setTimeout(function () { | |
155 |
|
|
241 | IPython.delete_count = 0; | |
156 |
}, |
|
242 | }, 800); | |
157 | } else if (dc === 1) { |
|
243 | } else if (dc === 1) { | |
158 | notebook.delete_cell(); |
|
244 | IPython.notebook.delete_cell(); | |
|
245 | IPython.delete_count = 0; | |||
159 | } |
|
246 | } | |
160 | return false; |
|
247 | return false; | |
161 | } else if (event.which === 65) { |
|
248 | } | |
162 | // Insert code cell above selected = a |
|
249 | }, | |
163 | notebook.insert_cell_above('code'); |
|
250 | 'a' : { | |
164 | notebook.select_prev(); |
|
251 | help : 'insert cell above', | |
165 | return false; |
|
252 | handler : function (event) { | |
166 | } else if (event.which === 66) { |
|
253 | IPython.notebook.insert_cell_above('code'); | |
167 | // Insert code cell below selected = b |
|
254 | IPython.notebook.select_prev(); | |
168 | notebook.insert_cell_below('code'); |
|
|||
169 | notebook.select_next(); |
|
|||
170 | return false; |
|
255 | return false; | |
171 | } else if (event.which === 89) { |
|
256 | } | |
172 | // To code = y |
|
257 | }, | |
173 | notebook.to_code(); |
|
258 | 'b' : { | |
|
259 | help : 'insert cell below', | |||
|
260 | handler : function (event) { | |||
|
261 | IPython.notebook.insert_cell_below('code'); | |||
|
262 | IPython.notebook.select_next(); | |||
174 | return false; |
|
263 | return false; | |
175 | } else if (event.which === 77) { |
|
264 | } | |
176 | // To markdown = m |
|
265 | }, | |
177 | notebook.to_markdown(); |
|
266 | 'y' : { | |
|
267 | help : 'to code', | |||
|
268 | handler : function (event) { | |||
|
269 | IPython.notebook.to_code(); | |||
178 | return false; |
|
270 | return false; | |
179 | } else if (event.which === 84) { |
|
271 | } | |
180 | // To Raw = t |
|
272 | }, | |
181 | notebook.to_raw(); |
|
273 | 'm' : { | |
|
274 | help : 'to markdown', | |||
|
275 | handler : function (event) { | |||
|
276 | IPython.notebook.to_markdown(); | |||
182 | return false; |
|
277 | return false; | |
183 | } else if (event.which === 49) { |
|
278 | } | |
184 | // To Heading 1 = 1 |
|
279 | }, | |
185 | notebook.to_heading(undefined, 1); |
|
280 | 't' : { | |
|
281 | help : 'to raw', | |||
|
282 | handler : function (event) { | |||
|
283 | IPython.notebook.to_raw(); | |||
186 | return false; |
|
284 | return false; | |
187 | } else if (event.which === 50) { |
|
285 | } | |
188 | // To Heading 2 = 2 |
|
286 | }, | |
189 | notebook.to_heading(undefined, 2); |
|
287 | '1' : { | |
|
288 | help : 'to heading 1', | |||
|
289 | handler : function (event) { | |||
|
290 | IPython.notebook.to_heading(undefined, 1); | |||
190 | return false; |
|
291 | return false; | |
191 | } else if (event.which === 51) { |
|
292 | } | |
192 | // To Heading 3 = 3 |
|
293 | }, | |
193 | notebook.to_heading(undefined, 3); |
|
294 | '2' : { | |
|
295 | help : 'to heading 2', | |||
|
296 | handler : function (event) { | |||
|
297 | IPython.notebook.to_heading(undefined, 2); | |||
194 | return false; |
|
298 | return false; | |
195 | } else if (event.which === 52) { |
|
299 | } | |
196 | // To Heading 4 = 4 |
|
300 | }, | |
197 | notebook.to_heading(undefined, 4); |
|
301 | '3' : { | |
|
302 | help : 'to heading 3', | |||
|
303 | handler : function (event) { | |||
|
304 | IPython.notebook.to_heading(undefined, 3); | |||
198 | return false; |
|
305 | return false; | |
199 | } else if (event.which === 53) { |
|
306 | } | |
200 | // To Heading 5 = 5 |
|
307 | }, | |
201 | notebook.to_heading(undefined, 5); |
|
308 | '4' : { | |
|
309 | help : 'to heading 4', | |||
|
310 | handler : function (event) { | |||
|
311 | IPython.notebook.to_heading(undefined, 4); | |||
202 | return false; |
|
312 | return false; | |
203 | } else if (event.which === 54) { |
|
313 | } | |
204 | // To Heading 6 = 6 |
|
314 | }, | |
205 | notebook.to_heading(undefined, 6); |
|
315 | '5' : { | |
|
316 | help : 'to heading 5', | |||
|
317 | handler : function (event) { | |||
|
318 | IPython.notebook.to_heading(undefined, 5); | |||
206 | return false; |
|
319 | return false; | |
207 | } else if (event.which === 79) { |
|
320 | } | |
208 | // Toggle output = o |
|
321 | }, | |
209 | if (event.shiftKey) { |
|
322 | '6' : { | |
210 | notebook.toggle_output_scroll(); |
|
323 | help : 'to heading 6', | |
211 | } else { |
|
324 | handler : function (event) { | |
212 |
notebook.to |
|
325 | IPython.notebook.to_heading(undefined, 6); | |
213 | }; |
|
|||
214 | return false; |
|
326 | return false; | |
215 | } else if (event.which === 83) { |
|
327 | } | |
216 | // Save notebook = s |
|
328 | }, | |
217 | notebook.save_checkpoint(); |
|
329 | 'o' : { | |
|
330 | help : 'toggle output', | |||
|
331 | handler : function (event) { | |||
|
332 | IPython.notebook.toggle_output(); | |||
218 | return false; |
|
333 | return false; | |
219 | } else if (event.which === 74) { |
|
334 | } | |
220 | // Move cell down = j |
|
335 | }, | |
221 | notebook.move_cell_down(); |
|
336 | 'shift+o' : { | |
|
337 | help : 'toggle output', | |||
|
338 | handler : function (event) { | |||
|
339 | IPython.notebook.toggle_output_scroll(); | |||
222 | return false; |
|
340 | return false; | |
223 | } else if (event.which === 75) { |
|
341 | } | |
224 | // Move cell up = k |
|
342 | }, | |
225 | notebook.move_cell_up(); |
|
343 | 's' : { | |
|
344 | help : 'save notebook', | |||
|
345 | handler : function (event) { | |||
|
346 | IPython.notebook.save_checkpoint(); | |||
226 | return false; |
|
347 | return false; | |
227 | } else if (event.which === 80) { |
|
348 | } | |
228 | // Select previous = p |
|
349 | }, | |
229 | notebook.select_prev(); |
|
350 | 'ctrl+j' : { | |
|
351 | help : 'move cell down', | |||
|
352 | handler : function (event) { | |||
|
353 | IPython.notebook.move_cell_down(); | |||
230 | return false; |
|
354 | return false; | |
231 | } else if (event.which === 78) { |
|
355 | } | |
232 | // Select next = n |
|
356 | }, | |
233 | notebook.select_next(); |
|
357 | 'ctrl+k' : { | |
|
358 | help : 'move cell up', | |||
|
359 | handler : function (event) { | |||
|
360 | IPython.notebook.move_cell_up(); | |||
234 | return false; |
|
361 | return false; | |
235 | } else if (event.which === 76) { |
|
362 | } | |
236 | // Toggle line numbers = l |
|
363 | }, | |
237 | notebook.cell_toggle_line_numbers(); |
|
364 | 'l' : { | |
|
365 | help : 'toggle line numbers', | |||
|
366 | handler : function (event) { | |||
|
367 | IPython.notebook.cell_toggle_line_numbers(); | |||
238 | return false; |
|
368 | return false; | |
239 | } else if (event.which === 73) { |
|
369 | } | |
240 | // Interrupt kernel = i |
|
370 | }, | |
241 | notebook.kernel.interrupt(); |
|
371 | 'i' : { | |
|
372 | help : 'interrupt kernel', | |||
|
373 | handler : function (event) { | |||
|
374 | IPython.notebook.kernel.interrupt(); | |||
242 | return false; |
|
375 | return false; | |
243 | } else if (event.which === 190) { |
|
376 | } | |
244 | // Restart kernel = . # matches qt console |
|
377 | }, | |
245 | notebook.restart_kernel(); |
|
378 | '.' : { | |
|
379 | help : 'restart kernel', | |||
|
380 | handler : function (event) { | |||
|
381 | IPython.notebook.restart_kernel(); | |||
246 | return false; |
|
382 | return false; | |
247 | } else if (event.which === 72) { |
|
383 | } | |
248 | // Show keyboard shortcuts = h |
|
384 | }, | |
|
385 | 'h' : { | |||
|
386 | help : 'keyboard shortcuts', | |||
|
387 | handler : function (event) { | |||
249 | IPython.quick_help.show_keyboard_shortcuts(); |
|
388 | IPython.quick_help.show_keyboard_shortcuts(); | |
250 | return false; |
|
389 | return false; | |
251 | } else if (event.which === 90) { |
|
390 | } | |
252 | // Undo last cell delete = z |
|
391 | }, | |
253 | notebook.undelete_cell(); |
|
392 | 'z' : { | |
|
393 | help : 'undo last delete', | |||
|
394 | handler : function (event) { | |||
|
395 | IPython.notebook.undelete_cell(); | |||
254 | return false; |
|
396 | return false; | |
255 | } else if (event.which === 189 || event.which === 173) { |
|
397 | } | |
256 | // how fun! '-' is 189 in Chrome, but 173 in FF and Opera |
|
398 | }, | |
257 | // Split cell = - |
|
399 | '-' : { | |
258 |
|
|
400 | help : 'split cell', | |
|
401 | handler : function (event) { | |||
|
402 | IPython.notebook.split_cell(); | |||
259 | return false; |
|
403 | return false; | |
260 | } else if ((event.which === 61 || event.which === 187) && event.shiftKey) { |
|
404 | } | |
261 | notebook.merge_cell_below(); |
|
405 | }, | |
|
406 | 'shift+=' : { | |||
|
407 | help : 'merge cell below', | |||
|
408 | handler : function (event) { | |||
|
409 | IPython.notebook.merge_cell_below(); | |||
262 | return false; |
|
410 | return false; | |
263 |
} |
|
411 | } | |
264 | // If we havn't handled it, let someone else. |
|
412 | }, | |
|
413 | } | |||
|
414 | ||||
|
415 | ||||
|
416 | // Shortcut manager class | |||
|
417 | ||||
|
418 | var ShortcutManager = function () { | |||
|
419 | this._shortcuts = {} | |||
|
420 | } | |||
|
421 | ||||
|
422 | ShortcutManager.prototype.canonicalize_key = function (key) { | |||
|
423 | return inv_keycodes[keycodes[key]]; | |||
|
424 | } | |||
|
425 | ||||
|
426 | ShortcutManager.prototype.canonicalize_shortcut = function (shortcut) { | |||
|
427 | // Sort a sequence of + separated modifiers into the order alt+ctrl+meta+shift | |||
|
428 | var values = shortcut.split("+"); | |||
|
429 | if (values.length === 1) { | |||
|
430 | return this.canonicalize_key(values[0]) | |||
|
431 | } else { | |||
|
432 | var modifiers = values.slice(0,-1); | |||
|
433 | var key = this.canonicalize_key(values[-1]); | |||
|
434 | modifiers.sort(); | |||
|
435 | return modifiers.join('+') + '+' + key; | |||
|
436 | } | |||
|
437 | } | |||
|
438 | ||||
|
439 | ShortcutManager.prototype.event_to_shortcut = function (event) { | |||
|
440 | // Convert a jQuery keyboard event to a strong based keyboard shortcut | |||
|
441 | var shortcut = ''; | |||
|
442 | var key = inv_keycodes[event.which] | |||
|
443 | if (event.altKey && key !== 'alt') {shortcut += 'alt+';} | |||
|
444 | if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';} | |||
|
445 | if (event.metaKey && key !== 'meta') {shortcut += 'meta+';} | |||
|
446 | if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';} | |||
|
447 | shortcut += key; | |||
|
448 | return shortcut | |||
|
449 | } | |||
|
450 | ||||
|
451 | ShortcutManager.prototype.clear_shortcuts = function () { | |||
|
452 | this._shortcuts = {}; | |||
|
453 | } | |||
|
454 | ||||
|
455 | ShortcutManager.prototype.add_shortcut = function (shortcut, data) { | |||
|
456 | shortcut = this.canonicalize_shortcut(shortcut); | |||
|
457 | this._shortcuts[shortcut] = data; | |||
|
458 | } | |||
|
459 | ||||
|
460 | ShortcutManager.prototype.add_shortcuts = function (data) { | |||
|
461 | for (var shortcut in data) { | |||
|
462 | this.add_shortcut(shortcut, data[shortcut]); | |||
|
463 | } | |||
|
464 | } | |||
|
465 | ||||
|
466 | ShortcutManager.prototype.remove_shortcut = function (shortcut) { | |||
|
467 | shortcut = this.canonicalize_shortcut(shortcut); | |||
|
468 | delete this._shortcuts[shortcut]; | |||
|
469 | } | |||
|
470 | ||||
|
471 | ShortcutManager.prototype.call_handler = function (event) { | |||
|
472 | var shortcut = this.event_to_shortcut(event); | |||
|
473 | var data = this._shortcuts[shortcut]; | |||
|
474 | if (data !== undefined) { | |||
|
475 | console.log('call_handler', shortcut, data['help']); | |||
|
476 | var handler = data['handler']; | |||
|
477 | if (handler !== undefined) { | |||
|
478 | return handler(event); | |||
|
479 | } | |||
|
480 | } | |||
265 | return true; |
|
481 | return true; | |
|
482 | } | |||
|
483 | ||||
|
484 | ||||
|
485 | ||||
|
486 | // Main keyboard manager for the notebook | |||
|
487 | ||||
|
488 | var KeyboardManager = function () { | |||
|
489 | this.mode = 'command'; | |||
|
490 | this.enabled = true; | |||
|
491 | this.delete_count = 0; | |||
|
492 | this.bind_events(); | |||
|
493 | this.command_shortcuts = new ShortcutManager(); | |||
|
494 | this.command_shortcuts.add_shortcuts(default_common_shortcuts); | |||
|
495 | this.command_shortcuts.add_shortcuts(default_command_shortcuts); | |||
|
496 | this.edit_shortcuts = new ShortcutManager(); | |||
|
497 | this.edit_shortcuts.add_shortcuts(default_common_shortcuts); | |||
|
498 | this.edit_shortcuts.add_shortcuts(default_edit_shortcuts); | |||
266 | }; |
|
499 | }; | |
267 |
|
500 | |||
|
501 | KeyboardManager.prototype.bind_events = function () { | |||
|
502 | var that = this; | |||
|
503 | $(document).keydown(function (event) { | |||
|
504 | return that.handle_keydown(event); | |||
|
505 | }); | |||
|
506 | }; | |||
|
507 | ||||
|
508 | KeyboardManager.prototype.handle_keydown = function (event) { | |||
|
509 | var notebook = IPython.notebook; | |||
|
510 | ||||
|
511 | console.log('keyboard_manager', this.mode, event.keyCode); | |||
|
512 | ||||
|
513 | if (event.which === keycodes['esc']) { | |||
|
514 | // Intercept escape at highest level to avoid closing | |||
|
515 | // websocket connection with firefox | |||
|
516 | event.preventDefault(); | |||
|
517 | } | |||
|
518 | ||||
|
519 | if (!this.enabled) { | |||
|
520 | if (event.which === keycodes['esc']) { | |||
|
521 | // ESC | |||
|
522 | notebook.command_mode(); | |||
|
523 | return false; | |||
|
524 | } | |||
|
525 | return true; | |||
|
526 | } | |||
|
527 | ||||
|
528 | if (this.mode === 'edit') { | |||
|
529 | return this.edit_shortcuts.call_handler(event); | |||
|
530 | } else if (this.mode === 'command') { | |||
|
531 | return this.command_shortcuts.call_handler(event); | |||
|
532 | } | |||
|
533 | return true; | |||
|
534 | } | |||
|
535 | ||||
268 | KeyboardManager.prototype.edit_mode = function () { |
|
536 | KeyboardManager.prototype.edit_mode = function () { | |
269 | console.log('KeyboardManager', 'changing to edit mode'); |
|
537 | console.log('KeyboardManager', 'changing to edit mode'); | |
270 | this.last_mode = this.mode; |
|
538 | this.last_mode = this.mode; | |
271 | this.mode = 'edit'; |
|
539 | this.mode = 'edit'; | |
272 | } |
|
540 | } | |
273 |
|
541 | |||
274 | KeyboardManager.prototype.command_mode = function () { |
|
542 | KeyboardManager.prototype.command_mode = function () { | |
275 | console.log('KeyboardManager', 'changing to command mode'); |
|
543 | console.log('KeyboardManager', 'changing to command mode'); | |
276 | this.last_mode = this.mode; |
|
544 | this.last_mode = this.mode; | |
277 | this.mode = 'command'; |
|
545 | this.mode = 'command'; | |
278 | } |
|
546 | } | |
279 |
|
547 | |||
280 | KeyboardManager.prototype.enable = function () { |
|
548 | KeyboardManager.prototype.enable = function () { | |
281 | this.enabled = true; |
|
549 | this.enabled = true; | |
282 | } |
|
550 | } | |
283 |
|
551 | |||
284 | KeyboardManager.prototype.disable = function () { |
|
552 | KeyboardManager.prototype.disable = function () { | |
285 | this.enabled = false; |
|
553 | this.enabled = false; | |
286 | } |
|
554 | } | |
287 |
|
555 | |||
|
556 | KeyboardManager.prototype.register_events = function (e) { | |||
|
557 | var that = this; | |||
|
558 | e.on('focusin', function () { | |||
|
559 | that.command_mode(); | |||
|
560 | that.disable(); | |||
|
561 | }); | |||
|
562 | e.on('focusout', function () { | |||
|
563 | that.command_mode(); | |||
|
564 | that.enable(); | |||
|
565 | }) | |||
|
566 | } | |||
|
567 | ||||
288 |
|
568 | |||
|
569 | IPython.keycodes = keycodes; | |||
|
570 | IPython.inv_keycodes = inv_keycodes; | |||
|
571 | IPython.default_common_shortcuts = default_common_shortcuts; | |||
|
572 | IPython.default_edit_shortcuts = default_edit_shortcuts; | |||
|
573 | IPython.default_command_shortcuts = default_command_shortcuts; | |||
|
574 | IPython.ShortcutManager = ShortcutManager; | |||
289 | IPython.KeyboardManager = KeyboardManager; |
|
575 | IPython.KeyboardManager = KeyboardManager; | |
290 |
|
576 | |||
291 | return IPython; |
|
577 | return IPython; | |
292 |
|
578 | |||
293 | }(IPython)); |
|
579 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now