Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DRY repetitive cursor moving code.

  • Loading branch information...
commit 21e7830de499ebf73f9c2bf160ee9fdc9b2d553d 1 parent 355aebb
@georgebrock georgebrock authored
View
17 js/base.js
@@ -122,7 +122,10 @@
Vimulator.Base.prototype.moveCursorCol = function (col) {
this.cursor.col = col;
if (col === '$' || this.cursor.col >= this.lines[this.cursor.row].length) {
- this.cursor.col = this.lines[this.cursor.row].length - 1;
+ this.cursor.col = this.currentLine().length - 1;
+ }
+ if (col === '^') {
+ this.cursor.col = this.currentLine().search(/[^\s]/);
}
if (this.cursor.col < 0) {
this.cursor.col = 0;
@@ -134,8 +137,16 @@
};
Vimulator.Base.prototype.moveCursorRelative = function(rows, cols) {
var row, col;
- if (rows === '$') { row = '$'; } else { row = this.cursor.row + rows; }
- if (cols === '$') { col = '$'; } else { col = this.cursor.col + cols; }
+ if (typeof rows === 'string') {
+ row = rows;
+ } else {
+ row = this.cursor.row + rows;
+ }
+ if (typeof cols === 'string') {
+ col = cols;
+ } else {
+ col = this.cursor.col + cols;
+ }
return this.moveCursor(row, col);
};
View
3  js/normal_mode/insertion.js
@@ -14,8 +14,7 @@
'I': new C({
repeatable: true,
callback: function (vim) {
- var col = vim.currentLine().search(/[^\s]/);
- vim.moveCursorCol(col);
+ vim.moveCursorCol('^');
vim.setMode("insert");
},
description: "Insert text at the start of the line" +
View
22 js/normal_mode/motions.js
@@ -67,8 +67,7 @@
'^': new C({
callback: function (vim) {
- var col = vim.currentLine().search(/[^\s]/);
- vim.moveCursorCol(col);
+ vim.moveCursorCol('^');
},
description: "Move to the first non-space on the line",
}),
@@ -243,11 +242,8 @@
'gg': new C({
defaultCount: null,
callback: function (vim, count) {
- var col, row;
- row = count ? count - 1 : 0;
- vim.moveCursor(row, 0);
- col = vim.currentLine().search(/[^\s]/);
- vim.moveCursorCol(col);
+ var row = count ? count - 1 : 0;
+ vim.moveCursor(row, '^');
},
description: function (count) {
if (count) {
@@ -261,11 +257,8 @@
'G': new C({
defaultCount: null,
callback: function (vim, count) {
- var row, col;
- row = count ? count - 1 : '$';
- vim.moveCursor(row, 0);
- col = vim.currentLine().search(/[^\s]/);
- vim.moveCursorRelative(0, col);
+ var row = count ? count - 1 : '$';
+ vim.moveCursor(row, '^');
},
description: function (count) {
if (count) {
@@ -279,10 +272,7 @@
//RETURN
'\u000D': new C({
callback: function (vim, count) {
- var col;
- vim.moveCursorRelative(count, 0);
- col = vim.currentLine().search(/[^\s]/);
- vim.moveCursorCol(col);
+ vim.moveCursorRelative(count, '^');
},
description: function (count) {
if (count === 1) {
View
6 js/normal_mode/operators.js
@@ -61,12 +61,10 @@
// d, j and k delete whole rows
if (/[dj]/.test(motion.commandKey)) {
vim.removeRows(before.row, after.row + 1);
- vim.moveCursorRow(before.row);
- vim.moveCursorCol(vim.currentLine().search(/[^\s]/));
+ vim.moveCursor(before.row, '^');
} else if (/[k]/.test(motion.commandKey)) {
vim.removeRows(after.row, before.row + 1);
- vim.moveCursorRow(after.row);
- vim.moveCursorCol(vim.currentLine().search(/[^\s]/));
+ vim.moveCursor(after.row, '^');
} else {
vim.removeRange(before, after);
}
Please sign in to comment.
Something went wrong with that request. Please try again.