| 2495 | |
| 2496 | // Register Vim with CodeMirror |
| 2497 | function buildVimKeyMap() { |
| 2498 | /** |
| 2499 | * Handle the raw key event from CodeMirror. Translate the |
| 2500 | * Shift + key modifier to the resulting letter, while preserving other |
| 2501 | * modifers. |
| 2502 | */ |
| 2503 | // TODO: Figure out a way to catch capslock. |
| 2504 | function handleKeyEvent_(cm, key, modifier) { |
| 2505 | if (isUpperCase(key)) { |
| 2506 | // Convert to lower case if shift is not the modifier since the key |
| 2507 | // we get from CodeMirror is always upper case. |
| 2508 | if (modifier == 'Shift') { |
| 2509 | modifier = null; |
| 2510 | } |
| 2511 | else { |
| 2512 | key = key.toLowerCase(); |
| 2513 | } |
| 2514 | } |
| 2515 | if (modifier) { |
| 2516 | // Vim will parse modifier+key combination as a single key. |
| 2517 | key = modifier + '-' + key; |
| 2518 | } |
| 2519 | vim.handleKey(cm, key); |
| 2520 | } |
| 2521 | |
| 2522 | // Closure to bind CodeMirror, key, modifier. |
| 2523 | function keyMapper(key, modifier) { |
| 2524 | return function(cm) { |
| 2525 | handleKeyEvent_(cm, key, modifier); |
| 2526 | }; |
| 2527 | } |
| 2528 | |
| 2529 | var modifiers = ['Shift', 'Ctrl']; |
| 2530 | var keyMap = { |
| 2531 | 'nofallthrough': true, |
| 2532 | 'style': 'fat-cursor' |
| 2533 | }; |
| 2534 | function bindKeys(keys, modifier) { |
| 2535 | for (var i = 0; i < keys.length; i++) { |
| 2536 | var key = keys[i]; |
| 2537 | if (!modifier && inArray(key, specialSymbols)) { |
| 2538 | // Wrap special symbols with '' because that's how CodeMirror binds |
| 2539 | // them. |
| 2540 | key = "'" + key + "'"; |
| 2541 | } |
| 2542 | if (modifier) { |
| 2543 | keyMap[modifier + '-' + key] = keyMapper(keys[i], modifier); |
| 2544 | } else { |
| 2545 | keyMap[key] = keyMapper(keys[i]); |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | bindKeys(upperCaseAlphabet); |
| 2550 | bindKeys(upperCaseAlphabet, 'Shift'); |
| 2551 | bindKeys(upperCaseAlphabet, 'Ctrl'); |
| 2552 | bindKeys(specialSymbols); |
| 2553 | bindKeys(specialSymbols, 'Ctrl'); |
| 2554 | bindKeys(numbers); |