|
399 | 399 | // main num keymap |
400 | 400 | // Add bindings that are influenced by number keys |
401 | 401 | iterObj({ |
402 | | - "Left": "goColumnLeft", "Right": "goColumnRight", |
403 | | - "Down": "goLineDown", "Up": "goLineUp", "Backspace": "goCharLeft", |
404 | | - "Space": "goCharRight", |
405 | 402 | "X": function(cm) {CodeMirror.commands.delCharRight(cm);}, |
406 | 403 | "P": function(cm) { |
407 | 404 | var cur = cm.getCursor().line; |
|
708 | 705 | }; |
709 | 706 | } |
710 | 707 |
|
711 | | - // These are our motion commands to be used for navigation and selection with |
712 | | - // certian other commands. All should return a cursor object. |
713 | | - var motionList = ['B', 'E', 'J', 'K', 'H', 'L', 'W', 'Shift-W', "'^'", "'$'", "'%'", 'Esc']; |
714 | | - |
715 | | - motions = { |
716 | | - 'B': function(cm, times, yank) { return moveToWord(cm, word, -1, times, 'start', yank); }, |
717 | | - 'Shift-B': function(cm, times, yank) { return moveToWord(cm, bigWord, -1, times, 'start', yank); }, |
718 | | - 'E': function(cm, times, yank) { return moveToWord(cm, word, 1, times, 'end', yank); }, |
719 | | - 'Shift-E': function(cm, times, yank) { return moveToWord(cm, bigWord, 1, times, 'end', yank); }, |
720 | | - 'J': function(cm, times) { |
721 | | - var cur = cm.getCursor(); |
722 | | - return {line: cur.line+times, ch : cur.ch}; |
723 | | - }, |
724 | | - |
725 | | - 'K': function(cm, times) { |
726 | | - var cur = cm.getCursor(); |
727 | | - return {line: cur.line-times, ch: cur.ch}; |
728 | | - }, |
729 | | - |
730 | | - 'H': function(cm, times) { |
731 | | - var cur = cm.getCursor(); |
732 | | - return {line: cur.line, ch: cur.ch-times}; |
733 | | - }, |
| 708 | + function offsetCursor(cm, line, ch) { |
| 709 | + var cur = cm.getCursor(); return {line: cur.line + line, ch: cur.ch + ch}; |
| 710 | + } |
734 | 711 |
|
735 | | - 'L': function(cm, times) { |
736 | | - var cur = cm.getCursor(); |
737 | | - return {line: cur.line, ch: cur.ch+times}; |
738 | | - }, |
739 | | - 'W': function(cm, times, yank) { return moveToWord(cm, word, 1, times, 'start', yank); }, |
740 | | - 'Shift-W': function(cm, times, yank) { return moveToWord(cm, bigWord, 1, times, 'start', yank); }, |
| 712 | + // These are the motion commands we use for navigation and selection with |
| 713 | + // certain other commands. All should return a cursor object. |
| 714 | + var motions = { |
| 715 | + "J": function(cm, times) { return offsetCursor(cm, times, 0); }, |
| 716 | + "Down": function(cm, times) { return offsetCursor(cm, times, 0); }, |
| 717 | + "K": function(cm, times) { return offsetCursor(cm, -times, 0); }, |
| 718 | + "Up": function(cm, times) { return offsetCursor(cm, -times, 0); }, |
| 719 | + "L": function(cm, times) { return offsetCursor(cm, 0, times); }, |
| 720 | + "Right": function(cm, times) { return offsetCursor(cm, 0, times); }, |
| 721 | + "Space": function(cm, times) { return offsetCursor(cm, 0, times); }, |
| 722 | + "H": function(cm, times) { return offsetCursor(cm, 0, -times); }, |
| 723 | + "Left": function(cm, times) { return offsetCursor(cm, 0, -times); }, |
| 724 | + "Backspace": function(cm, times) { return offsetCursor(cm, 0, -times); }, |
| 725 | + "B": function(cm, times, yank) { return moveToWord(cm, word, -1, times, 'start', yank); }, |
| 726 | + "Shift-B": function(cm, times, yank) { return moveToWord(cm, bigWord, -1, times, 'start', yank); }, |
| 727 | + "E": function(cm, times, yank) { return moveToWord(cm, word, 1, times, 'end', yank); }, |
| 728 | + "Shift-E": function(cm, times, yank) { return moveToWord(cm, bigWord, 1, times, 'end', yank); }, |
| 729 | + "W": function(cm, times, yank) { return moveToWord(cm, word, 1, times, 'start', yank); }, |
| 730 | + "Shift-W": function(cm, times, yank) { return moveToWord(cm, bigWord, 1, times, 'start', yank); }, |
741 | 731 | "'^'": function(cm, times) { |
742 | | - var cur = cm.getCursor(); |
743 | | - var line = cm.getLine(cur.line).split(''); |
744 | | - |
745 | | - // Empty line :o |
746 | | - if (line.length == 0) return cur; |
747 | | - |
748 | | - for (var index = 0; index < line.length; index++) { |
749 | | - if (line[index].match(/[^\s]/)) return {line: cur.line, ch: index}; |
| 732 | + var cur = cm.getCursor(), line = cm.getLine(cur.line).split(''); |
| 733 | + for (var i = 0; i < line.length; i++) { |
| 734 | + if (line[i].match(/[^\s]/)) return {line: cur.line, ch: index}; |
750 | 735 | } |
| 736 | + return cur; |
751 | 737 | }, |
752 | 738 | "'$'": function(cm) { |
753 | | - var cur = cm.getCursor(); |
754 | | - var line = cm.getLine(cur.line); |
755 | | - return {line: cur.line, ch: line.length}; |
| 739 | + var cur = cm.getCursor(), ch = cm.getLine(cur.line).length; |
| 740 | + return {line: cur.line, ch: ch}; |
756 | 741 | }, |
757 | 742 | "'%'": function(cm) { return findMatchedSymbol(cm, cm.getCursor()); }, |
758 | | - "Esc" : function(cm) { |
759 | | - cm.setOption('vim'); |
760 | | - repeatCount = 0; |
761 | | - |
762 | | - return cm.getCursor(); |
763 | | - } |
| 743 | + "Esc" : function(cm) { cm.setOption("keyMap", "vim"); repeatCount = 0; return cm.getCursor(); } |
764 | 744 | }; |
765 | 745 |
|
766 | 746 | // Map our movement actions each operator and non-operational movement |
767 | | - iterList(motionList, function(key, index, array) { |
| 747 | + iterObj(motions, function(key, motion) { |
768 | 748 | CodeMirror.keyMap['vim-prefix-d'][key] = function(cm) { |
769 | 749 | // Get our selected range |
770 | 750 | var start = cm.getCursor(); |
771 | | - var end = motions[key](cm, repeatCount ? repeatCount : 1, true); |
| 751 | + var end = motion(cm, repeatCount ? repeatCount : 1, true); |
772 | 752 |
|
773 | 753 | // Set swap var if range is of negative length |
774 | 754 | if ((start.line > end.line) || (start.line == end.line && start.ch > end.ch)) var swap = true; |
|
784 | 764 |
|
785 | 765 | CodeMirror.keyMap['vim-prefix-c'][key] = function(cm) { |
786 | 766 | var start = cm.getCursor(); |
787 | | - var end = motions[key](cm, repeatCount ? repeatCount : 1, true); |
| 767 | + var end = motion(cm, repeatCount ? repeatCount : 1, true); |
788 | 768 |
|
789 | 769 | if ((start.line > end.line) || (start.line == end.line && start.ch > end.ch)) var swap = true; |
790 | 770 | pushInBuffer(cm.getRange(swap ? end : start, swap ? start : end)); |
|
796 | 776 |
|
797 | 777 | CodeMirror.keyMap['vim-prefix-y'][key] = function(cm) { |
798 | 778 | var start = cm.getCursor(); |
799 | | - var end = motions[key](cm, repeatCount ? repeatCount : 1, true); |
| 779 | + var end = motion(cm, repeatCount ? repeatCount : 1, true); |
800 | 780 |
|
801 | 781 | if ((start.line > end.line) || (start.line == end.line && start.ch > end.ch)) var swap = true; |
802 | 782 | pushInBuffer(cm.getRange(swap ? end : start, swap ? start : end)); |
|
806 | 786 | }; |
807 | 787 |
|
808 | 788 | CodeMirror.keyMap['vim'][key] = function(cm) { |
809 | | - var cur = motions[key](cm, repeatCount ? repeatCount : 1); |
| 789 | + var cur = motion(cm, repeatCount ? repeatCount : 1); |
810 | 790 | cm.setCursor(cur.line, cur.ch); |
811 | 791 |
|
812 | 792 | repeatCount = 0; |
|
0 commit comments