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