Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Support backspace in insert mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgebrock committed Dec 7, 2012
1 parent eb8beb6 commit 540e3ab
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
4 changes: 4 additions & 0 deletions css/base.css
Expand Up @@ -3,6 +3,10 @@
margin: 20px auto;
font-family: 'Arial', sans-serif;
}
.vimulator input {
position: absolute;
left: -9999px;
}
.vimulator pre {
position: relative;
z-index: 2;
Expand Down
44 changes: 36 additions & 8 deletions js/base.js
Expand Up @@ -36,9 +36,15 @@
Vimulator.Base.prototype.bindKeyListeners = function () {
var vim = this;

this.input = $('<input type="text">').appendTo(this.container)
.focus()
.blur(function () {
$(this).focus();
});

// Use keyup for special characters like escape
$(window).keyup(function (e) {
if (e.keyCode === 27) {
if (e.keyCode < 32) {
vim.keyPress(e.keyCode);
return false;
}
Expand Down Expand Up @@ -136,13 +142,35 @@
return this.lines[this.cursor.row];
};

Vimulator.Base.prototype.appendText = function (text) {
line = this.currentLine();
this.lines[this.cursor.row] =
line.substr(0, this.cursor.col) +
text +
line.substr(this.cursor.col);
this.cursor.col += text.length;
Vimulator.Base.prototype.appendChr = function (chr) {
var line;

if (chr === Vimulator.Utils.Keys.BACKSPACE) {
this.removeChr();
} else {
line = this.currentLine();
this.lines[this.cursor.row] =
line.substr(0, this.cursor.col) +
chr +
line.substr(this.cursor.col);
this.cursor.col += 1;
}
};

Vimulator.Base.prototype.removeChr = function () {
var line = this.currentLine();

if (this.cursor.col === 0 && this.cursor.row > 0) {
this.moveCursorRelative(-1, '$');
this.cursor.col += 1; //FIXME
this.lines[this.cursor.row] += line;
this.removeRows(this.cursor.row + 1, this.cursor.row + 2);
} else if (this.cursor.col > 0) {
this.lines[this.cursor.row] =
line.substr(0, this.cursor.col - 1) +
line.substr(this.cursor.col);
this.cursor.col -= 1;
}
};

Vimulator.Base.prototype.insertRowBelow = function (text, index) {
Expand Down
2 changes: 1 addition & 1 deletion js/insert_mode.js
Expand Up @@ -28,7 +28,7 @@
return op;
} else {
this.vim.registers["."] += key;
this.vim.appendText(key);
this.vim.appendChr(key);
}
};
}());
5 changes: 3 additions & 2 deletions js/utils.js
Expand Up @@ -4,8 +4,9 @@
Vimulator.Utils = {};

Vimulator.Utils.Keys = K = {
ESC: '\u001B',
RETURN: '\u000D'
BACKSPACE: '\u0008',
RETURN: '\u000D',
ESC: '\u001B'
};

Vimulator.Utils.pluralize = function (count, word) {
Expand Down
23 changes: 23 additions & 0 deletions tests/acceptance/insertion.js
Expand Up @@ -95,3 +95,26 @@ describe("Substituting lines with S", function () {
expect(currentText()).toBe("1 and 2\nThird\nFourth");
});
});

describe("Deleting characters with backspace", function () {
it("deletes a character before the cursor", function () {
reset("Hello world!!1");
pressKeys("A" + BACKSPACE + BACKSPACE + ESC);
expect(currentText()).toBe("Hello world!");
expect(cursorPosition()).toEqual({row: 0, col: 11});
});

it("deletes line breaks", function () {
reset("First line\nSecond line\nThird line");
pressKeys("ja" + BACKSPACE + BACKSPACE + ESC);
expect(currentText()).toBe("First lineecond line\nThird line");
expect(cursorPosition()).toEqual({row: 0, col: 9});
});

it("does not delete beyond the end of the document", function () {
reset("Hello world");
pressKeys("gI" + BACKSPACE + BACKSPACE + ESC);
expect(currentText()).toBe("Hello world");
expect(cursorPosition()).toEqual({row: 0, col: 0});
});
});
5 changes: 5 additions & 0 deletions tests/acceptance/repeat.js
Expand Up @@ -23,6 +23,11 @@ describe("Repeating the last command with .", function () {
expect(currentText()).toBe("Here ore some words, they ore all here");
});

it("repeats backspaces", function () {
pressKeys("ea" + BACKSPACE + ESC + "e.e.");
expect(currentText()).toBe("Her ar som words, they are all here");
});

it("does not repeat motions", function () {
pressKeys("3l");
expect(cursorPosition()).toEqual({row: 0, col: 3});
Expand Down
1 change: 1 addition & 0 deletions tests/helpers.js
@@ -1,5 +1,6 @@
ESC = '\u001B';
RETURN = '\u000D';
BACKSPACE = '\u0008';

function pressKeys(keys) {
jQuery.each(keys.split(''), function (i, key) {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/insert_mode.js
Expand Up @@ -13,7 +13,7 @@ describe("InsertMode", function () {
vim = jasmine.createSpyObj("vim", [
"moveCursorRelative",
"setMode",
"appendText"
"appendChr"
]);
vim.registers = {};
im = new Vimulator.InsertMode(vim);
Expand All @@ -33,7 +33,7 @@ describe("InsertMode", function () {
for (i = 0; i < chars.length; i +=1) {
chr = chars[i];
im.keyPress(chr);
expect(vim.appendText).toHaveBeenCalledWith(chr);
expect(vim.appendChr).toHaveBeenCalledWith(chr);
}
});

Expand Down

0 comments on commit 540e3ab

Please sign in to comment.