Skip to content

Commit 46d075d

Browse files
jankeromnesmarijnh
authored andcommitted
[vim] refactor motions
1 parent 505ab98 commit 46d075d

File tree

1 file changed

+34
-54
lines changed

1 file changed

+34
-54
lines changed

keymap/vim.js

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,6 @@
399399
// main num keymap
400400
// Add bindings that are influenced by number keys
401401
iterObj({
402-
"Left": "goColumnLeft", "Right": "goColumnRight",
403-
"Down": "goLineDown", "Up": "goLineUp", "Backspace": "goCharLeft",
404-
"Space": "goCharRight",
405402
"X": function(cm) {CodeMirror.commands.delCharRight(cm);},
406403
"P": function(cm) {
407404
var cur = cm.getCursor().line;
@@ -708,67 +705,50 @@
708705
};
709706
}
710707

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+
}
734711

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); },
741731
"'^'": 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};
750735
}
736+
return cur;
751737
},
752738
"'$'": 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};
756741
},
757742
"'%'": 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(); }
764744
};
765745

766746
// Map our movement actions each operator and non-operational movement
767-
iterList(motionList, function(key, index, array) {
747+
iterObj(motions, function(key, motion) {
768748
CodeMirror.keyMap['vim-prefix-d'][key] = function(cm) {
769749
// Get our selected range
770750
var start = cm.getCursor();
771-
var end = motions[key](cm, repeatCount ? repeatCount : 1, true);
751+
var end = motion(cm, repeatCount ? repeatCount : 1, true);
772752

773753
// Set swap var if range is of negative length
774754
if ((start.line > end.line) || (start.line == end.line && start.ch > end.ch)) var swap = true;
@@ -784,7 +764,7 @@
784764

785765
CodeMirror.keyMap['vim-prefix-c'][key] = function(cm) {
786766
var start = cm.getCursor();
787-
var end = motions[key](cm, repeatCount ? repeatCount : 1, true);
767+
var end = motion(cm, repeatCount ? repeatCount : 1, true);
788768

789769
if ((start.line > end.line) || (start.line == end.line && start.ch > end.ch)) var swap = true;
790770
pushInBuffer(cm.getRange(swap ? end : start, swap ? start : end));
@@ -796,7 +776,7 @@
796776

797777
CodeMirror.keyMap['vim-prefix-y'][key] = function(cm) {
798778
var start = cm.getCursor();
799-
var end = motions[key](cm, repeatCount ? repeatCount : 1, true);
779+
var end = motion(cm, repeatCount ? repeatCount : 1, true);
800780

801781
if ((start.line > end.line) || (start.line == end.line && start.ch > end.ch)) var swap = true;
802782
pushInBuffer(cm.getRange(swap ? end : start, swap ? start : end));
@@ -806,7 +786,7 @@
806786
};
807787

808788
CodeMirror.keyMap['vim'][key] = function(cm) {
809-
var cur = motions[key](cm, repeatCount ? repeatCount : 1);
789+
var cur = motion(cm, repeatCount ? repeatCount : 1);
810790
cm.setCursor(cur.line, cur.ch);
811791

812792
repeatCount = 0;

0 commit comments

Comments
 (0)