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