Permalink
Browse files

All motions return a Range.

This dramatically simplifies operators: The logic for inclusivity/exclusivity
and the behaviour of removing or changing it is now encapsulated in the range.
  • Loading branch information...
1 parent 71258ba commit eac9df7b08d28c8363bbd27ded5d408bfca4ced0 @georgebrock georgebrock committed Dec 10, 2012
Showing with 103 additions and 141 deletions.
  1. +1 −0 index.html
  2. +7 −0 js/base.js
  3. +18 −19 js/normal_mode/line_search.js
  4. +37 −35 js/normal_mode/motions.js
  5. +10 −87 js/normal_mode/operators.js
  6. +30 −0 js/range.js
View
@@ -17,6 +17,7 @@
<script src='js/base.js'></script>
<script src='js/utils.js'></script>
+ <script src='js/range.js'></script>
<script src='js/operation.js'></script>
<script src='js/command.js'></script>
<script src='js/command_list.js'></script>
View
@@ -291,4 +291,11 @@
return {row: row, col: col};
};
+
+ Vimulator.Base.prototype.cursorCopy = function () {
+ return {
+ row: this.cursor.row,
+ col: this.cursor.col
+ };
+ };
}());
@@ -1,8 +1,11 @@
(function () {
- var C = Vimulator.Command,
- U = Vimulator.Utils;
+ var C, U, CR, findForwards, untilForwards, findBackwards, untilBackwards;
- function findForwards(vim, count, chr) {
+ C = Vimulator.Command;
+ U = Vimulator.Utils;
+ CR = Vimulator.CharacterRange;
+
+ findForwards = CR.captureInclusive(function (vim, count, chr) {
var found, position;
found = -1;
@@ -12,11 +15,9 @@
vim.moveCursor(position.row, position.col);
position = vim.findNext(chr);
}
+ });
- return found > 0;
- }
-
- function untilForwards(vim, count, chr, repeat) {
+ untilForwards = CR.captureExclusive(function (vim, count, chr, repeat) {
if (vim.currentLine().charAt(vim.cursor.col + 1) === chr) {
if (repeat && count === 1) {
count += 1;
@@ -27,9 +28,9 @@
if (findForwards(vim, count, chr)) {
vim.moveCursorRelative(0, -1);
}
- }
+ });
- function findBackwards(vim, count, chr) {
+ findBackwards = CR.captureInclusive(function (vim, count, chr) {
var found, position;
found = -1;
@@ -39,11 +40,9 @@
vim.moveCursor(position.row, position.col);
position = vim.findLast(chr);
}
+ });
- return found > 0;
- }
-
- function untilBackwards(vim, count, chr, repeat) {
+ untilBackwards = CR.captureExclusive(function (vim, count, chr, repeat) {
if (vim.currentLine().charAt(vim.cursor.col - 1) === chr) {
if (repeat && count === 1) {
count += 1;
@@ -54,14 +53,14 @@
if (findBackwards(vim, count, chr)) {
vim.moveCursorRelative(0, 1);
}
- }
+ });
Vimulator.NormalMode.LineSearch = {
'f': new C({
argument: "literal",
callback: function (vim, count, chr) {
vim.lastSearch = {op: 'f', chr: chr};
- findForwards(vim, count, chr);
+ return findForwards(vim, count, chr);
},
description: function (count, chr) {
return "Find the " + U.ordinalize(count) + " occurence of " +
@@ -73,7 +72,7 @@
argument: "literal",
callback: function (vim, count, chr) {
vim.lastSearch = {op: 'F', chr: chr};
- findBackwards(vim, count, chr);
+ return findBackwards(vim, count, chr);
},
description: function (count, chr) {
return "Find the " + U.ordinalize(count) + " occurence of " +
@@ -85,7 +84,7 @@
argument: "literal",
callback: function (vim, count, chr) {
vim.lastSearch = {op: 't', chr: chr};
- untilForwards(vim, count, chr);
+ return untilForwards(vim, count, chr);
},
description: function (count, chr) {
return "Move to the " + U.ordinalize(count) + " occurence of " +
@@ -97,7 +96,7 @@
argument: "literal",
callback: function (vim, count, chr) {
vim.lastSearch = {op: 'T', chr: chr};
- untilBackwards(vim, count, chr);
+ return untilBackwards(vim, count, chr);
},
description: function (count, chr) {
return "Move to the " + U.ordinalize(count) + " occurence of " +
@@ -120,7 +119,7 @@
'T': untilBackwards
};
- findFuncs[vim.lastSearch.op](vim, count, vim.lastSearch.chr, true);
+ return findFuncs[vim.lastSearch.op](vim, count, vim.lastSearch.chr, true);
},
description: function (count) {
desc = "Repeat the last search ";
View
@@ -1,41 +1,43 @@
(function () {
var C = Vimulator.Command,
- U = Vimulator.Utils;
+ U = Vimulator.Utils,
+ CR = Vimulator.CharacterRange,
+ LR = Vimulator.LineRange;
Vimulator.NormalMode.Motions = {
// Character motions
'h': new C({
- callback: function (vim, count) {
+ callback: CR.captureExclusive(function (vim, count) {
vim.moveCursorRelative(0, -count);
- },
+ }),
description: function (count) {
return "Move left " + U.pluralize(count, "character");
}
}),
'j': new C({
- callback: function (vim, count) {
+ callback: LR.capture(function (vim, count) {
vim.moveCursorRelative(count, 0);
- },
+ }),
description: function (count) {
return "Move down " + U.pluralize(count, "line");
}
}),
'k': new C({
- callback: function (vim, count) {
+ callback: LR.capture(function (vim, count) {
vim.moveCursorRelative(-count, 0);
- },
+ }),
description: function (count) {
return "Move up " + U.pluralize(count, "line");
}
}),
'l': new C({
- callback: function (vim, count) {
+ callback: CR.captureExclusive(function (vim, count) {
vim.moveCursorRelative(0, count);
- },
+ }),
description: function (count) {
return "Move right " + U.pluralize(count, "character");
}
@@ -45,16 +47,16 @@
// Line motions
'0': new C({
- callback: function (vim) {
+ callback: CR.captureExclusive(function (vim) {
vim.moveCursorCol(0);
- },
+ }),
description: "Move to the start of the line"
}),
'$': new C({
- callback: function (vim, count) {
+ callback: CR.captureInclusive(function (vim, count) {
vim.moveCursorRelative(count - 1, '$');
- },
+ }),
description: function (count) {
if (count === 1) {
return "Move to the end of the line";
@@ -66,9 +68,9 @@
}),
'^': new C({
- callback: function (vim) {
+ callback: CR.captureExclusive(function (vim) {
vim.moveCursorCol('^');
- },
+ }),
description: "Move to the first non-space on the line",
}),
@@ -77,7 +79,7 @@
//TODO DRY this code
'w': new C({
- callback: function (vim, count) {
+ callback: CR.captureExclusive(function (vim, count) {
var words, result;
while (true) {
@@ -95,14 +97,14 @@
return;
}
}
- },
+ }),
description: function (count) {
return "Move forward " + U.pluralize(count, "word");
}
}),
'W': new C({
- callback: function (vim, count) {
+ callback: CR.captureExclusive(function (vim, count) {
var words, result;
while (true) {
@@ -120,15 +122,15 @@
return;
}
}
- },
+ }),
description: function (count) {
return "Move forward " + U.pluralize(count, "word") +
" (including punctuation)";
}
}),
'e': new C({
- callback: function (vim, count) {
+ callback: CR.captureInclusive(function (vim, count) {
var words, result;
while (true) {
@@ -146,7 +148,7 @@
return;
}
}
- },
+ }),
description: function (count) {
if (count === 1) {
return "Move to the next word end";
@@ -158,7 +160,7 @@
}),
'E': new C({
- callback: function (vim, count) {
+ callback: CR.captureInclusive(function (vim, count) {
var words, result;
while (true) {
@@ -176,7 +178,7 @@
return;
}
}
- },
+ }),
description: function (count) {
if (count === 1) {
return "Move to the next word end (including punctuation)";
@@ -188,7 +190,7 @@
}),
'b': new C({
- callback: function (vim, count) {
+ callback: CR.captureExclusive(function (vim, count) {
var words, result;
while (true) {
@@ -205,14 +207,14 @@
return;
}
}
- },
+ }),
description: function (count) {
return "Move back " + U.pluralize(count, "word");
}
}),
'B': new C({
- callback: function (vim, count) {
+ callback: CR.captureExclusive(function (vim, count) {
var words, result;
while (true) {
@@ -229,7 +231,7 @@
return;
}
}
- },
+ }),
description: function (count) {
return "Move back " + U.pluralize(count, "word") +
" (including punctuation)";
@@ -241,10 +243,10 @@
'gg': new C({
defaultCount: null,
- callback: function (vim, count) {
+ callback: LR.capture(function (vim, count) {
var row = count ? count - 1 : 0;
vim.moveCursor(row, '^');
- },
+ }),
description: function (count) {
if (count) {
return "Jump to line " + count;
@@ -256,10 +258,10 @@
'G': new C({
defaultCount: null,
- callback: function (vim, count) {
+ callback: LR.capture(function (vim, count) {
var row = count ? count - 1 : '$';
vim.moveCursor(row, '^');
- },
+ }),
description: function (count) {
if (count) {
return "Jump to line " + count;
@@ -270,9 +272,9 @@
}),
'+': new C({
- callback: function (vim, count) {
+ callback: LR.capture(function (vim, count) {
vim.moveCursorRelative(count, '^');
- },
+ }),
description: function (count) {
if (count === 1) {
return "Move to the start of the next line";
@@ -283,9 +285,9 @@
}
}),
'-': new C({
- callback: function (vim, count) {
+ callback: LR.capture(function (vim, count) {
vim.moveCursorRelative(-count, '^');
- },
+ }),
description: function (count) {
if (count === 1) {
return "Move to the start of the previous line";
Oops, something went wrong.

0 comments on commit eac9df7

Please sign in to comment.