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