##// END OF EJS Templates
remove alt-- shortcut...
MinRK -
Show More
@@ -1,608 +1,608 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 browser = IPython.utils.browser[0];
15 var browser = IPython.utils.browser[0];
16 var platform = IPython.utils.platform;
16 var platform = IPython.utils.platform;
17
17
18 // Default keyboard shortcuts
18 // Default keyboard shortcuts
19
19
20 var default_common_shortcuts = {
20 var default_common_shortcuts = {
21 'shift' : {
21 'shift' : {
22 help : '',
22 help : '',
23 help_index : '',
23 help_index : '',
24 handler : function (event) {
24 handler : function (event) {
25 // ignore shift keydown
25 // ignore shift keydown
26 return true;
26 return true;
27 }
27 }
28 },
28 },
29 'shift-enter' : {
29 'shift-enter' : {
30 help : 'run cell, select below',
30 help : 'run cell, select below',
31 help_index : 'ba',
31 help_index : 'ba',
32 handler : function (event) {
32 handler : function (event) {
33 IPython.notebook.execute_cell_and_select_below();
33 IPython.notebook.execute_cell_and_select_below();
34 return false;
34 return false;
35 }
35 }
36 },
36 },
37 'ctrl-enter' : {
37 'ctrl-enter' : {
38 help : 'run cell',
38 help : 'run cell',
39 help_index : 'bb',
39 help_index : 'bb',
40 handler : function (event) {
40 handler : function (event) {
41 IPython.notebook.execute_cell();
41 IPython.notebook.execute_cell();
42 return false;
42 return false;
43 }
43 }
44 },
44 },
45 'alt-enter' : {
45 'alt-enter' : {
46 help : 'run cell, insert below',
46 help : 'run cell, insert below',
47 help_index : 'bc',
47 help_index : 'bc',
48 handler : function (event) {
48 handler : function (event) {
49 IPython.notebook.execute_cell_and_insert_below();
49 IPython.notebook.execute_cell_and_insert_below();
50 return false;
50 return false;
51 }
51 }
52 }
52 }
53 };
53 };
54
54
55 if (platform === 'MacOS') {
55 if (platform === 'MacOS') {
56 default_common_shortcuts['cmd-s'] =
56 default_common_shortcuts['cmd-s'] =
57 {
57 {
58 help : 'save notebook',
58 help : 'save notebook',
59 help_index : 'fb',
59 help_index : 'fb',
60 handler : function (event) {
60 handler : function (event) {
61 IPython.notebook.save_checkpoint();
61 IPython.notebook.save_checkpoint();
62 event.preventDefault();
62 event.preventDefault();
63 return false;
63 return false;
64 }
64 }
65 };
65 };
66 } else {
66 } else {
67 default_common_shortcuts['ctrl-s'] =
67 default_common_shortcuts['ctrl-s'] =
68 {
68 {
69 help : 'save notebook',
69 help : 'save notebook',
70 help_index : 'fb',
70 help_index : 'fb',
71 handler : function (event) {
71 handler : function (event) {
72 IPython.notebook.save_checkpoint();
72 IPython.notebook.save_checkpoint();
73 event.preventDefault();
73 event.preventDefault();
74 return false;
74 return false;
75 }
75 }
76 };
76 };
77 }
77 }
78
78
79 // Edit mode defaults
79 // Edit mode defaults
80
80
81 var default_edit_shortcuts = {
81 var default_edit_shortcuts = {
82 'esc' : {
82 'esc' : {
83 help : 'command mode',
83 help : 'command mode',
84 help_index : 'aa',
84 help_index : 'aa',
85 handler : function (event) {
85 handler : function (event) {
86 IPython.notebook.command_mode();
86 IPython.notebook.command_mode();
87 return false;
87 return false;
88 }
88 }
89 },
89 },
90 'ctrl-m' : {
90 'ctrl-m' : {
91 help : 'command mode',
91 help : 'command mode',
92 help_index : 'ab',
92 help_index : 'ab',
93 handler : function (event) {
93 handler : function (event) {
94 IPython.notebook.command_mode();
94 IPython.notebook.command_mode();
95 return false;
95 return false;
96 }
96 }
97 },
97 },
98 'up' : {
98 'up' : {
99 help : '',
99 help : '',
100 help_index : '',
100 help_index : '',
101 handler : function (event) {
101 handler : function (event) {
102 var index = IPython.notebook.get_selected_index();
102 var index = IPython.notebook.get_selected_index();
103 var cell = IPython.notebook.get_cell(index);
103 var cell = IPython.notebook.get_cell(index);
104 if (cell && cell.at_top() && index !== 0) {
104 if (cell && cell.at_top() && index !== 0) {
105 event.preventDefault();
105 event.preventDefault();
106 IPython.notebook.command_mode();
106 IPython.notebook.command_mode();
107 IPython.notebook.select_prev();
107 IPython.notebook.select_prev();
108 IPython.notebook.edit_mode();
108 IPython.notebook.edit_mode();
109 var cm = IPython.notebook.get_selected_cell().code_mirror;
109 var cm = IPython.notebook.get_selected_cell().code_mirror;
110 cm.setCursor(cm.lastLine(), 0);
110 cm.setCursor(cm.lastLine(), 0);
111 return false;
111 return false;
112 } else if (cell) {
112 } else if (cell) {
113 var cm = cell.code_mirror;
113 var cm = cell.code_mirror;
114 var cursor = cm.getCursor();
114 var cursor = cm.getCursor();
115 cursor.line -= 1;
115 cursor.line -= 1;
116 cm.setCursor(cursor);
116 cm.setCursor(cursor);
117 return false;
117 return false;
118 }
118 }
119 }
119 }
120 },
120 },
121 'down' : {
121 'down' : {
122 help : '',
122 help : '',
123 help_index : '',
123 help_index : '',
124 handler : function (event) {
124 handler : function (event) {
125 var index = IPython.notebook.get_selected_index();
125 var index = IPython.notebook.get_selected_index();
126 var cell = IPython.notebook.get_cell(index);
126 var cell = IPython.notebook.get_cell(index);
127 if (cell.at_bottom() && index !== (IPython.notebook.ncells()-1)) {
127 if (cell.at_bottom() && index !== (IPython.notebook.ncells()-1)) {
128 event.preventDefault();
128 event.preventDefault();
129 IPython.notebook.command_mode();
129 IPython.notebook.command_mode();
130 IPython.notebook.select_next();
130 IPython.notebook.select_next();
131 IPython.notebook.edit_mode();
131 IPython.notebook.edit_mode();
132 var cm = IPython.notebook.get_selected_cell().code_mirror;
132 var cm = IPython.notebook.get_selected_cell().code_mirror;
133 cm.setCursor(0, 0);
133 cm.setCursor(0, 0);
134 return false;
134 return false;
135 } else {
135 } else {
136 var cm = cell.code_mirror;
136 var cm = cell.code_mirror;
137 var cursor = cm.getCursor();
137 var cursor = cm.getCursor();
138 cursor.line += 1;
138 cursor.line += 1;
139 cm.setCursor(cursor);
139 cm.setCursor(cursor);
140 return false;
140 return false;
141 }
141 }
142 }
142 }
143 },
143 },
144 'alt--' : {
144 'ctrl-shift--' : {
145 help : 'split cell',
145 help : 'split cell',
146 help_index : 'ea',
146 help_index : 'ea',
147 handler : function (event) {
147 handler : function (event) {
148 IPython.notebook.split_cell();
148 IPython.notebook.split_cell();
149 return false;
149 return false;
150 }
150 }
151 },
151 },
152 'alt-subtract' : {
152 'ctrl-shift-subtract' : {
153 help : '',
153 help : '',
154 help_index : 'eb',
154 help_index : 'eb',
155 handler : function (event) {
155 handler : function (event) {
156 IPython.notebook.split_cell();
156 IPython.notebook.split_cell();
157 return false;
157 return false;
158 }
158 }
159 },
159 },
160 'tab' : {
160 'tab' : {
161 help : 'indent or complete',
161 help : 'indent or complete',
162 help_index : 'ec',
162 help_index : 'ec',
163 },
163 },
164 'shift-tab' : {
164 'shift-tab' : {
165 help : 'tooltip',
165 help : 'tooltip',
166 help_index : 'ed',
166 help_index : 'ed',
167 },
167 },
168 };
168 };
169
169
170 if (platform === 'MacOS') {
170 if (platform === 'MacOS') {
171 default_edit_shortcuts['cmd-/'] =
171 default_edit_shortcuts['cmd-/'] =
172 {
172 {
173 help : 'toggle comment',
173 help : 'toggle comment',
174 help_index : 'ee'
174 help_index : 'ee'
175 };
175 };
176 default_edit_shortcuts['cmd-]'] =
176 default_edit_shortcuts['cmd-]'] =
177 {
177 {
178 help : 'indent',
178 help : 'indent',
179 help_index : 'ef'
179 help_index : 'ef'
180 };
180 };
181 default_edit_shortcuts['cmd-['] =
181 default_edit_shortcuts['cmd-['] =
182 {
182 {
183 help : 'dedent',
183 help : 'dedent',
184 help_index : 'eg'
184 help_index : 'eg'
185 };
185 };
186 } else {
186 } else {
187 default_edit_shortcuts['ctrl-/'] =
187 default_edit_shortcuts['ctrl-/'] =
188 {
188 {
189 help : 'toggle comment',
189 help : 'toggle comment',
190 help_index : 'ee'
190 help_index : 'ee'
191 };
191 };
192 default_edit_shortcuts['ctrl-]'] =
192 default_edit_shortcuts['ctrl-]'] =
193 {
193 {
194 help : 'indent',
194 help : 'indent',
195 help_index : 'ef'
195 help_index : 'ef'
196 };
196 };
197 default_edit_shortcuts['ctrl-['] =
197 default_edit_shortcuts['ctrl-['] =
198 {
198 {
199 help : 'dedent',
199 help : 'dedent',
200 help_index : 'eg'
200 help_index : 'eg'
201 };
201 };
202 }
202 }
203
203
204 // Command mode defaults
204 // Command mode defaults
205
205
206 var default_command_shortcuts = {
206 var default_command_shortcuts = {
207 'enter' : {
207 'enter' : {
208 help : 'edit mode',
208 help : 'edit mode',
209 help_index : 'aa',
209 help_index : 'aa',
210 handler : function (event) {
210 handler : function (event) {
211 IPython.notebook.edit_mode();
211 IPython.notebook.edit_mode();
212 return false;
212 return false;
213 }
213 }
214 },
214 },
215 'up' : {
215 'up' : {
216 help : 'select previous cell',
216 help : 'select previous cell',
217 help_index : 'da',
217 help_index : 'da',
218 handler : function (event) {
218 handler : function (event) {
219 var index = IPython.notebook.get_selected_index();
219 var index = IPython.notebook.get_selected_index();
220 if (index !== 0 && index !== null) {
220 if (index !== 0 && index !== null) {
221 IPython.notebook.select_prev();
221 IPython.notebook.select_prev();
222 IPython.notebook.focus_cell();
222 IPython.notebook.focus_cell();
223 }
223 }
224 return false;
224 return false;
225 }
225 }
226 },
226 },
227 'down' : {
227 'down' : {
228 help : 'select next cell',
228 help : 'select next cell',
229 help_index : 'db',
229 help_index : 'db',
230 handler : function (event) {
230 handler : function (event) {
231 var index = IPython.notebook.get_selected_index();
231 var index = IPython.notebook.get_selected_index();
232 if (index !== (IPython.notebook.ncells()-1) && index !== null) {
232 if (index !== (IPython.notebook.ncells()-1) && index !== null) {
233 IPython.notebook.select_next();
233 IPython.notebook.select_next();
234 IPython.notebook.focus_cell();
234 IPython.notebook.focus_cell();
235 }
235 }
236 return false;
236 return false;
237 }
237 }
238 },
238 },
239 'k' : {
239 'k' : {
240 help : 'select previous cell',
240 help : 'select previous cell',
241 help_index : 'dc',
241 help_index : 'dc',
242 handler : function (event) {
242 handler : function (event) {
243 var index = IPython.notebook.get_selected_index();
243 var index = IPython.notebook.get_selected_index();
244 if (index !== 0 && index !== null) {
244 if (index !== 0 && index !== null) {
245 IPython.notebook.select_prev();
245 IPython.notebook.select_prev();
246 IPython.notebook.focus_cell();
246 IPython.notebook.focus_cell();
247 }
247 }
248 return false;
248 return false;
249 }
249 }
250 },
250 },
251 'j' : {
251 'j' : {
252 help : 'select next cell',
252 help : 'select next cell',
253 help_index : 'dd',
253 help_index : 'dd',
254 handler : function (event) {
254 handler : function (event) {
255 var index = IPython.notebook.get_selected_index();
255 var index = IPython.notebook.get_selected_index();
256 if (index !== (IPython.notebook.ncells()-1) && index !== null) {
256 if (index !== (IPython.notebook.ncells()-1) && index !== null) {
257 IPython.notebook.select_next();
257 IPython.notebook.select_next();
258 IPython.notebook.focus_cell();
258 IPython.notebook.focus_cell();
259 }
259 }
260 return false;
260 return false;
261 }
261 }
262 },
262 },
263 'x' : {
263 'x' : {
264 help : 'cut cell',
264 help : 'cut cell',
265 help_index : 'ee',
265 help_index : 'ee',
266 handler : function (event) {
266 handler : function (event) {
267 IPython.notebook.cut_cell();
267 IPython.notebook.cut_cell();
268 return false;
268 return false;
269 }
269 }
270 },
270 },
271 'c' : {
271 'c' : {
272 help : 'copy cell',
272 help : 'copy cell',
273 help_index : 'ef',
273 help_index : 'ef',
274 handler : function (event) {
274 handler : function (event) {
275 IPython.notebook.copy_cell();
275 IPython.notebook.copy_cell();
276 return false;
276 return false;
277 }
277 }
278 },
278 },
279 'shift-v' : {
279 'shift-v' : {
280 help : 'paste cell above',
280 help : 'paste cell above',
281 help_index : 'eg',
281 help_index : 'eg',
282 handler : function (event) {
282 handler : function (event) {
283 IPython.notebook.paste_cell_above();
283 IPython.notebook.paste_cell_above();
284 return false;
284 return false;
285 }
285 }
286 },
286 },
287 'v' : {
287 'v' : {
288 help : 'paste cell below',
288 help : 'paste cell below',
289 help_index : 'eh',
289 help_index : 'eh',
290 handler : function (event) {
290 handler : function (event) {
291 IPython.notebook.paste_cell_below();
291 IPython.notebook.paste_cell_below();
292 return false;
292 return false;
293 }
293 }
294 },
294 },
295 'd' : {
295 'd' : {
296 help : 'delete cell (press twice)',
296 help : 'delete cell (press twice)',
297 help_index : 'ej',
297 help_index : 'ej',
298 count: 2,
298 count: 2,
299 handler : function (event) {
299 handler : function (event) {
300 IPython.notebook.delete_cell();
300 IPython.notebook.delete_cell();
301 return false;
301 return false;
302 }
302 }
303 },
303 },
304 'a' : {
304 'a' : {
305 help : 'insert cell above',
305 help : 'insert cell above',
306 help_index : 'ec',
306 help_index : 'ec',
307 handler : function (event) {
307 handler : function (event) {
308 IPython.notebook.insert_cell_above('code');
308 IPython.notebook.insert_cell_above('code');
309 IPython.notebook.select_prev();
309 IPython.notebook.select_prev();
310 IPython.notebook.focus_cell();
310 IPython.notebook.focus_cell();
311 return false;
311 return false;
312 }
312 }
313 },
313 },
314 'b' : {
314 'b' : {
315 help : 'insert cell below',
315 help : 'insert cell below',
316 help_index : 'ed',
316 help_index : 'ed',
317 handler : function (event) {
317 handler : function (event) {
318 IPython.notebook.insert_cell_below('code');
318 IPython.notebook.insert_cell_below('code');
319 IPython.notebook.select_next();
319 IPython.notebook.select_next();
320 IPython.notebook.focus_cell();
320 IPython.notebook.focus_cell();
321 return false;
321 return false;
322 }
322 }
323 },
323 },
324 'y' : {
324 'y' : {
325 help : 'to code',
325 help : 'to code',
326 help_index : 'ca',
326 help_index : 'ca',
327 handler : function (event) {
327 handler : function (event) {
328 IPython.notebook.to_code();
328 IPython.notebook.to_code();
329 return false;
329 return false;
330 }
330 }
331 },
331 },
332 'm' : {
332 'm' : {
333 help : 'to markdown',
333 help : 'to markdown',
334 help_index : 'cb',
334 help_index : 'cb',
335 handler : function (event) {
335 handler : function (event) {
336 IPython.notebook.to_markdown();
336 IPython.notebook.to_markdown();
337 return false;
337 return false;
338 }
338 }
339 },
339 },
340 'r' : {
340 'r' : {
341 help : 'to raw',
341 help : 'to raw',
342 help_index : 'cc',
342 help_index : 'cc',
343 handler : function (event) {
343 handler : function (event) {
344 IPython.notebook.to_raw();
344 IPython.notebook.to_raw();
345 return false;
345 return false;
346 }
346 }
347 },
347 },
348 '1' : {
348 '1' : {
349 help : 'to heading 1',
349 help : 'to heading 1',
350 help_index : 'cd',
350 help_index : 'cd',
351 handler : function (event) {
351 handler : function (event) {
352 IPython.notebook.to_heading(undefined, 1);
352 IPython.notebook.to_heading(undefined, 1);
353 return false;
353 return false;
354 }
354 }
355 },
355 },
356 '2' : {
356 '2' : {
357 help : 'to heading 2',
357 help : 'to heading 2',
358 help_index : 'ce',
358 help_index : 'ce',
359 handler : function (event) {
359 handler : function (event) {
360 IPython.notebook.to_heading(undefined, 2);
360 IPython.notebook.to_heading(undefined, 2);
361 return false;
361 return false;
362 }
362 }
363 },
363 },
364 '3' : {
364 '3' : {
365 help : 'to heading 3',
365 help : 'to heading 3',
366 help_index : 'cf',
366 help_index : 'cf',
367 handler : function (event) {
367 handler : function (event) {
368 IPython.notebook.to_heading(undefined, 3);
368 IPython.notebook.to_heading(undefined, 3);
369 return false;
369 return false;
370 }
370 }
371 },
371 },
372 '4' : {
372 '4' : {
373 help : 'to heading 4',
373 help : 'to heading 4',
374 help_index : 'cg',
374 help_index : 'cg',
375 handler : function (event) {
375 handler : function (event) {
376 IPython.notebook.to_heading(undefined, 4);
376 IPython.notebook.to_heading(undefined, 4);
377 return false;
377 return false;
378 }
378 }
379 },
379 },
380 '5' : {
380 '5' : {
381 help : 'to heading 5',
381 help : 'to heading 5',
382 help_index : 'ch',
382 help_index : 'ch',
383 handler : function (event) {
383 handler : function (event) {
384 IPython.notebook.to_heading(undefined, 5);
384 IPython.notebook.to_heading(undefined, 5);
385 return false;
385 return false;
386 }
386 }
387 },
387 },
388 '6' : {
388 '6' : {
389 help : 'to heading 6',
389 help : 'to heading 6',
390 help_index : 'ci',
390 help_index : 'ci',
391 handler : function (event) {
391 handler : function (event) {
392 IPython.notebook.to_heading(undefined, 6);
392 IPython.notebook.to_heading(undefined, 6);
393 return false;
393 return false;
394 }
394 }
395 },
395 },
396 'o' : {
396 'o' : {
397 help : 'toggle output',
397 help : 'toggle output',
398 help_index : 'gb',
398 help_index : 'gb',
399 handler : function (event) {
399 handler : function (event) {
400 IPython.notebook.toggle_output();
400 IPython.notebook.toggle_output();
401 return false;
401 return false;
402 }
402 }
403 },
403 },
404 'shift-o' : {
404 'shift-o' : {
405 help : 'toggle output scrolling',
405 help : 'toggle output scrolling',
406 help_index : 'gc',
406 help_index : 'gc',
407 handler : function (event) {
407 handler : function (event) {
408 IPython.notebook.toggle_output_scroll();
408 IPython.notebook.toggle_output_scroll();
409 return false;
409 return false;
410 }
410 }
411 },
411 },
412 's' : {
412 's' : {
413 help : 'save notebook',
413 help : 'save notebook',
414 help_index : 'fa',
414 help_index : 'fa',
415 handler : function (event) {
415 handler : function (event) {
416 IPython.notebook.save_checkpoint();
416 IPython.notebook.save_checkpoint();
417 return false;
417 return false;
418 }
418 }
419 },
419 },
420 'ctrl-j' : {
420 'ctrl-j' : {
421 help : 'move cell down',
421 help : 'move cell down',
422 help_index : 'eb',
422 help_index : 'eb',
423 handler : function (event) {
423 handler : function (event) {
424 IPython.notebook.move_cell_down();
424 IPython.notebook.move_cell_down();
425 return false;
425 return false;
426 }
426 }
427 },
427 },
428 'ctrl-k' : {
428 'ctrl-k' : {
429 help : 'move cell up',
429 help : 'move cell up',
430 help_index : 'ea',
430 help_index : 'ea',
431 handler : function (event) {
431 handler : function (event) {
432 IPython.notebook.move_cell_up();
432 IPython.notebook.move_cell_up();
433 return false;
433 return false;
434 }
434 }
435 },
435 },
436 'l' : {
436 'l' : {
437 help : 'toggle line numbers',
437 help : 'toggle line numbers',
438 help_index : 'ga',
438 help_index : 'ga',
439 handler : function (event) {
439 handler : function (event) {
440 IPython.notebook.cell_toggle_line_numbers();
440 IPython.notebook.cell_toggle_line_numbers();
441 return false;
441 return false;
442 }
442 }
443 },
443 },
444 'i' : {
444 'i' : {
445 help : 'interrupt kernel (press twice)',
445 help : 'interrupt kernel (press twice)',
446 help_index : 'ha',
446 help_index : 'ha',
447 count: 2,
447 count: 2,
448 handler : function (event) {
448 handler : function (event) {
449 IPython.notebook.kernel.interrupt();
449 IPython.notebook.kernel.interrupt();
450 return false;
450 return false;
451 }
451 }
452 },
452 },
453 '0' : {
453 '0' : {
454 help : 'restart kernel (press twice)',
454 help : 'restart kernel (press twice)',
455 help_index : 'hb',
455 help_index : 'hb',
456 count: 2,
456 count: 2,
457 handler : function (event) {
457 handler : function (event) {
458 IPython.notebook.restart_kernel();
458 IPython.notebook.restart_kernel();
459 return false;
459 return false;
460 }
460 }
461 },
461 },
462 'h' : {
462 'h' : {
463 help : 'keyboard shortcuts',
463 help : 'keyboard shortcuts',
464 help_index : 'ge',
464 help_index : 'ge',
465 handler : function (event) {
465 handler : function (event) {
466 IPython.quick_help.show_keyboard_shortcuts();
466 IPython.quick_help.show_keyboard_shortcuts();
467 return false;
467 return false;
468 }
468 }
469 },
469 },
470 'z' : {
470 'z' : {
471 help : 'undo last delete',
471 help : 'undo last delete',
472 help_index : 'ei',
472 help_index : 'ei',
473 handler : function (event) {
473 handler : function (event) {
474 IPython.notebook.undelete_cell();
474 IPython.notebook.undelete_cell();
475 return false;
475 return false;
476 }
476 }
477 },
477 },
478 'shift-m' : {
478 'shift-m' : {
479 help : 'merge cell below',
479 help : 'merge cell below',
480 help_index : 'ek',
480 help_index : 'ek',
481 handler : function (event) {
481 handler : function (event) {
482 IPython.notebook.merge_cell_below();
482 IPython.notebook.merge_cell_below();
483 return false;
483 return false;
484 }
484 }
485 },
485 },
486 'q' : {
486 'q' : {
487 help : 'close pager',
487 help : 'close pager',
488 help_index : 'gd',
488 help_index : 'gd',
489 handler : function (event) {
489 handler : function (event) {
490 IPython.pager.collapse();
490 IPython.pager.collapse();
491 return false;
491 return false;
492 }
492 }
493 },
493 },
494 };
494 };
495
495
496
496
497 // Main keyboard manager for the notebook
497 // Main keyboard manager for the notebook
498
498
499 var ShortcutManager = IPython.keyboard.ShortcutManager;
499 var ShortcutManager = IPython.keyboard.ShortcutManager;
500 var keycodes = IPython.keyboard.keycodes;
500 var keycodes = IPython.keyboard.keycodes;
501
501
502 var KeyboardManager = function () {
502 var KeyboardManager = function () {
503 this.mode = 'command';
503 this.mode = 'command';
504 this.enabled = true;
504 this.enabled = true;
505 this.bind_events();
505 this.bind_events();
506 this.command_shortcuts = new ShortcutManager();
506 this.command_shortcuts = new ShortcutManager();
507 this.command_shortcuts.add_shortcuts(default_common_shortcuts);
507 this.command_shortcuts.add_shortcuts(default_common_shortcuts);
508 this.command_shortcuts.add_shortcuts(default_command_shortcuts);
508 this.command_shortcuts.add_shortcuts(default_command_shortcuts);
509 this.edit_shortcuts = new ShortcutManager();
509 this.edit_shortcuts = new ShortcutManager();
510 this.edit_shortcuts.add_shortcuts(default_common_shortcuts);
510 this.edit_shortcuts.add_shortcuts(default_common_shortcuts);
511 this.edit_shortcuts.add_shortcuts(default_edit_shortcuts);
511 this.edit_shortcuts.add_shortcuts(default_edit_shortcuts);
512 };
512 };
513
513
514 KeyboardManager.prototype.bind_events = function () {
514 KeyboardManager.prototype.bind_events = function () {
515 var that = this;
515 var that = this;
516 $(document).keydown(function (event) {
516 $(document).keydown(function (event) {
517 return that.handle_keydown(event);
517 return that.handle_keydown(event);
518 });
518 });
519 };
519 };
520
520
521 KeyboardManager.prototype.handle_keydown = function (event) {
521 KeyboardManager.prototype.handle_keydown = function (event) {
522 var notebook = IPython.notebook;
522 var notebook = IPython.notebook;
523
523
524 if (event.which === keycodes.esc) {
524 if (event.which === keycodes.esc) {
525 // Intercept escape at highest level to avoid closing
525 // Intercept escape at highest level to avoid closing
526 // websocket connection with firefox
526 // websocket connection with firefox
527 event.preventDefault();
527 event.preventDefault();
528 }
528 }
529
529
530 if (!this.enabled) {
530 if (!this.enabled) {
531 if (event.which === keycodes.esc) {
531 if (event.which === keycodes.esc) {
532 // ESC
532 // ESC
533 notebook.command_mode();
533 notebook.command_mode();
534 return false;
534 return false;
535 }
535 }
536 return true;
536 return true;
537 }
537 }
538
538
539 if (this.mode === 'edit') {
539 if (this.mode === 'edit') {
540 return this.edit_shortcuts.call_handler(event);
540 return this.edit_shortcuts.call_handler(event);
541 } else if (this.mode === 'command') {
541 } else if (this.mode === 'command') {
542 return this.command_shortcuts.call_handler(event);
542 return this.command_shortcuts.call_handler(event);
543 }
543 }
544 return true;
544 return true;
545 };
545 };
546
546
547 KeyboardManager.prototype.edit_mode = function () {
547 KeyboardManager.prototype.edit_mode = function () {
548 this.last_mode = this.mode;
548 this.last_mode = this.mode;
549 this.mode = 'edit';
549 this.mode = 'edit';
550 };
550 };
551
551
552 KeyboardManager.prototype.command_mode = function () {
552 KeyboardManager.prototype.command_mode = function () {
553 this.last_mode = this.mode;
553 this.last_mode = this.mode;
554 this.mode = 'command';
554 this.mode = 'command';
555 };
555 };
556
556
557 KeyboardManager.prototype.enable = function () {
557 KeyboardManager.prototype.enable = function () {
558 this.enabled = true;
558 this.enabled = true;
559 };
559 };
560
560
561 KeyboardManager.prototype.disable = function () {
561 KeyboardManager.prototype.disable = function () {
562 this.enabled = false;
562 this.enabled = false;
563 };
563 };
564
564
565 KeyboardManager.prototype.register_events = function (e) {
565 KeyboardManager.prototype.register_events = function (e) {
566 var that = this;
566 var that = this;
567 var handle_focus = function () {
567 var handle_focus = function () {
568 that.disable();
568 that.disable();
569 };
569 };
570 var handle_blur = function () {
570 var handle_blur = function () {
571 that.enable();
571 that.enable();
572 };
572 };
573 e.on('focusin', handle_focus);
573 e.on('focusin', handle_focus);
574 e.on('focusout', handle_blur);
574 e.on('focusout', handle_blur);
575 // TODO: Very strange. The focusout event does not seem fire for the
575 // TODO: Very strange. The focusout event does not seem fire for the
576 // bootstrap textboxes on FF25&26... This works around that by
576 // bootstrap textboxes on FF25&26... This works around that by
577 // registering focus and blur events recursively on all inputs within
577 // registering focus and blur events recursively on all inputs within
578 // registered element.
578 // registered element.
579 e.find('input').blur(handle_blur);
579 e.find('input').blur(handle_blur);
580 e.on('DOMNodeInserted', function (event) {
580 e.on('DOMNodeInserted', function (event) {
581 var target = $(event.target);
581 var target = $(event.target);
582 if (target.is('input')) {
582 if (target.is('input')) {
583 target.blur(handle_blur);
583 target.blur(handle_blur);
584 } else {
584 } else {
585 target.find('input').blur(handle_blur);
585 target.find('input').blur(handle_blur);
586 }
586 }
587 });
587 });
588 // There are times (raw_input) where we remove the element from the DOM before
588 // There are times (raw_input) where we remove the element from the DOM before
589 // focusout is called. In this case we bind to the remove event of jQueryUI,
589 // focusout is called. In this case we bind to the remove event of jQueryUI,
590 // which gets triggered upon removal, iff it is focused at the time.
590 // which gets triggered upon removal, iff it is focused at the time.
591 // is_focused must be used to check for the case where an element within
591 // is_focused must be used to check for the case where an element within
592 // the element being removed is focused.
592 // the element being removed is focused.
593 e.on('remove', function () {
593 e.on('remove', function () {
594 if (IPython.utils.is_focused(e[0])) {
594 if (IPython.utils.is_focused(e[0])) {
595 that.enable();
595 that.enable();
596 }
596 }
597 });
597 });
598 };
598 };
599
599
600
600
601 IPython.default_common_shortcuts = default_common_shortcuts;
601 IPython.default_common_shortcuts = default_common_shortcuts;
602 IPython.default_edit_shortcuts = default_edit_shortcuts;
602 IPython.default_edit_shortcuts = default_edit_shortcuts;
603 IPython.default_command_shortcuts = default_command_shortcuts;
603 IPython.default_command_shortcuts = default_command_shortcuts;
604 IPython.KeyboardManager = KeyboardManager;
604 IPython.KeyboardManager = KeyboardManager;
605
605
606 return IPython;
606 return IPython;
607
607
608 }(IPython));
608 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now