Merge pull request #1244 from ajaxorg/cmdLine

VIM mode. Fix count handling for dd yy p P
This commit is contained in:
Ruben Daniels 2013-03-15 10:17:01 -07:00
commit b5a5d327de
8 changed files with 119 additions and 50 deletions

View file

@ -92,6 +92,12 @@ var cmdLine = new layout.singleLineEditor(consoleEl);
cmdLine.editor = env.editor; cmdLine.editor = env.editor;
env.editor.cmdLine = cmdLine; env.editor.cmdLine = cmdLine;
env.editor.showCommandLine = function(val) {
this.cmdLine.focus();
if (typeof val == "string")
this.cmdLine.setValue(val, 1);
};
/** /**
* This demonstrates how you can define commands and bind shortcuts to them. * This demonstrates how you can define commands and bind shortcuts to them.
*/ */

View file

@ -247,7 +247,8 @@ var Document = function(text) {
if (position.row >= length) { if (position.row >= length) {
position.row = Math.max(0, length - 1); position.row = Math.max(0, length - 1);
position.column = this.getLine(length-1).length; position.column = this.getLine(length-1).length;
} } else if (position.row < 0)
position.row = 0;
return position; return position;
}; };

View file

@ -103,7 +103,9 @@ var Editor = function(renderer, session) {
* *
**/ **/
this.setKeyboardHandler = function(keyboardHandler) { this.setKeyboardHandler = function(keyboardHandler) {
if (typeof keyboardHandler == "string" && keyboardHandler) { if (!keyboardHandler) {
this.keyBinding.setKeyboardHandler(null);
} else if (typeof keyboardHandler == "string") {
this.$keybindingId = keyboardHandler; this.$keybindingId = keyboardHandler;
var _self = this; var _self = this;
config.loadModule(["keybinding", keyboardHandler], function(module) { config.loadModule(["keybinding", keyboardHandler], function(module) {
@ -1279,16 +1281,15 @@ var Editor = function(renderer, session) {
_numberRx.lastIndex = 0 _numberRx.lastIndex = 0
var s = this.session.getLine(row) var s = this.session.getLine(row)
while(_numberRx.lastIndex < column - 1 ){ while (_numberRx.lastIndex < column) {
var m = _numberRx.exec(s) var m = _numberRx.exec(s)
if(m.index <= column && m.index+m[0].length >= column){ if(m.index <= column && m.index+m[0].length >= column){
var number = { var number = {
value: m[0], value: m[0],
start: m.index, start: m.index,
end: m.index+m[0].length end: m.index+m[0].length
} }
return number return number;
} }
} }
return null; return null;

View file

@ -91,28 +91,29 @@ exports.handler.attach = function(editor) {
editor.session.$emacsMark = null; editor.session.$emacsMark = null;
exports.markMode = function() { editor.emacsMarkMode = function() {
return editor.session.$emacsMark; return this.session.$emacsMark;
} }
exports.setMarkMode = function(p) { editor.setEmacsMarkMode = function(p) {
editor.session.$emacsMark = p; this.session.$emacsMark = p;
} }
editor.on("click",$resetMarkMode); editor.on("click", $resetMarkMode);
editor.on("changeSession",$kbSessionChange); editor.on("changeSession",$kbSessionChange);
editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates; editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates;
editor.setStyle("emacs-mode"); editor.setStyle("emacs-mode");
editor.commands.addCommands(commands); editor.commands.addCommands(commands);
exports.handler.platform = editor.commands.platform; exports.handler.platform = editor.commands.platform;
editor.$emacsModeHandler = this;
}; };
exports.handler.detach = function(editor) { exports.handler.detach = function(editor) {
delete editor.renderer.screenToTextCoordinates; delete editor.renderer.screenToTextCoordinates;
editor.session.$selectLongWords = $formerLongWords; editor.session.$selectLongWords = $formerLongWords;
editor.session.$useEmacsStyleLineStart = $formerLineStart; editor.session.$useEmacsStyleLineStart = $formerLineStart;
editor.removeEventListener("click",$resetMarkMode); editor.removeEventListener("click", $resetMarkMode);
editor.removeEventListener("changeSession",$kbSessionChange); editor.removeEventListener("changeSession", $kbSessionChange);
editor.unsetStyle("emacs-mode"); editor.unsetStyle("emacs-mode");
editor.commands.removeCommands(commands); editor.commands.removeCommands(commands);
}; };
@ -166,9 +167,10 @@ exports.handler.bindKey = function(key, command) {
exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
var editor = data.editor;
// insertstring data.count times // insertstring data.count times
if (hashId == -1) { if (hashId == -1) {
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
if (data.count) { if (data.count) {
var str = Array(data.count + 1).join(key); var str = Array(data.count + 1).join(key);
data.count = null; data.count = null;
@ -223,7 +225,7 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
args = command.args; args = command.args;
if (command.command) command = command.command; if (command.command) command = command.command;
if (command === "goorselect") { if (command === "goorselect") {
command = exports.markMode() ? args[1] : args[0]; command = editor.emacsMarkMode() ? args[1] : args[0];
args = null; args = null;
} }
} }
@ -232,9 +234,9 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
if (command === "insertstring" || if (command === "insertstring" ||
command === "splitline" || command === "splitline" ||
command === "togglecomment") { command === "togglecomment") {
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
} }
command = this.commands[command] || data.editor.commands.commands[command]; command = this.commands[command] || editor.commands.commands[command];
} }
if (!command.readonly && !command.isYank) if (!command.readonly && !command.isYank)
@ -330,10 +332,10 @@ exports.emacsKeys = {
"C-/|C-x u|S-C--|C-z": "undo", "C-/|C-x u|S-C--|C-z": "undo",
"S-C-/|S-C-x u|C--|S-C-z": "redo", //infinite undo? "S-C-/|S-C-x u|C--|S-C-z": "redo", //infinite undo?
// vertical editing // vertical editing
"C-x r": "selectRectangularRegion" "C-x r": "selectRectangularRegion",
"M-x": {command: "focusCommandLine", args: "M-x "}
// todo // todo
// "M-x" "C-x C-t" "M-t" "M-c" "F11" "C-M- "M-q" // "C-x C-t" "M-t" "M-c" "F11" "C-M- "M-q"
}; };
@ -365,21 +367,20 @@ exports.handler.addCommands({
// "goto" commands become "select" commands. // "goto" commands become "select" commands.
// Any insertion or mouse click resets mark-mode. // Any insertion or mouse click resets mark-mode.
// setMark twice in a row at the same place resets markmode // setMark twice in a row at the same place resets markmode
var markMode = exports.markMode(); var markMode = editor.emacsMarkMode();
if (markMode) { if (markMode) {
var cp = editor.getCursorPosition(); var cp = editor.getCursorPosition();
if (editor.selection.isEmpty() && if (editor.selection.isEmpty() &&
markMode.row == cp.row && markMode.column == cp.column) { markMode.row == cp.row && markMode.column == cp.column) {
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
// console.log("Mark mode off"); // console.log("Mark mode off");
return; return;
} }
} }
// turn on mark mode // turn on mark mode
markMode = editor.getCursorPosition(); markMode = editor.getCursorPosition();
exports.setMarkMode(markMode); editor.setEmacsMarkMode(markMode);
editor.selection.setSelectionAnchor(markMode.row, markMode.column); editor.selection.setSelectionAnchor(markMode.row, markMode.column);
}, },
exchangePointAndMark: { exchangePointAndMark: {
exec: function(editor) { exec: function(editor) {
@ -407,7 +408,7 @@ exports.handler.addCommands({
multiselectAction: "forEach" multiselectAction: "forEach"
}, },
killLine: function(editor) { killLine: function(editor) {
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
var pos = editor.getCursorPosition(); var pos = editor.getCursorPosition();
if (pos.column == 0 && if (pos.column == 0 &&
editor.session.doc.getLine(pos.row).length == 0) { editor.session.doc.getLine(pos.row).length == 0) {
@ -448,7 +449,11 @@ exports.handler.addCommands({
}, },
keyboardQuit: function(editor) { keyboardQuit: function(editor) {
editor.selection.clearSelection(); editor.selection.clearSelection();
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
},
focusCommandLine: function(editor, arg) {
if (editor.showCommandLine)
editor.showCommandLine(arg);
} }
}); });

View file

@ -50,11 +50,12 @@ var KeyBinding = function(editor) {
}; };
this.setKeyboardHandler = function(kb) { this.setKeyboardHandler = function(kb) {
if (this.$handlers[this.$handlers.length - 1] == kb) var h = this.$handlers;
if (h[h.length - 1] == kb)
return; return;
while (this.$handlers[1]) while (h[h.length - 1] && h[h.length - 1] != this.defaultHandler)
this.removeKeyboardHandler(this.$handlers[1]); this.removeKeyboardHandler(h[h.length - 1]);
this.addKeyboardHandler(kb, 1); this.addKeyboardHandler(kb, 1);
}; };

View file

@ -32,6 +32,7 @@ define(function(require, exports, module) {
"never use strict"; "never use strict";
var lang = require("../../lib/lang");
var util = require("./maps/util"); var util = require("./maps/util");
var motions = require("./maps/motions"); var motions = require("./maps/motions");
var operators = require("./maps/operators"); var operators = require("./maps/operators");
@ -89,6 +90,8 @@ var actions = exports.actions = {
param: true, param: true,
fn: function(editor, range, count, param) { fn: function(editor, range, count, param) {
if (param && param.length) { if (param && param.length) {
if (param.length > 1)
param = param == "return" ? "\n" : param == "tab" ? "\t" : param;
repeat(function() { editor.insert(param); }, count || 1); repeat(function() { editor.insert(param); }, count || 1);
editor.navigateLeft(); editor.navigateLeft();
} }
@ -207,13 +210,14 @@ var actions = exports.actions = {
editor.setOverwrite(false); editor.setOverwrite(false);
if (defaultReg.isLine) { if (defaultReg.isLine) {
var pos = editor.getCursorPosition(); var pos = editor.getCursorPosition();
var lines = defaultReg.text.split("\n"); pos.column = editor.session.getLine(pos.row).length;
editor.session.getDocument().insertLines(pos.row + 1, lines); var text = lang.stringRepeat("\n" + defaultReg.text, count || 1);
editor.session.insert(pos, text);
editor.moveCursorTo(pos.row + 1, 0); editor.moveCursorTo(pos.row + 1, 0);
} }
else { else {
editor.navigateRight(); editor.navigateRight();
editor.insert(defaultReg.text); editor.insert(lang.stringRepeat(defaultReg.text, count || 1));
editor.navigateLeft(); editor.navigateLeft();
} }
editor.setOverwrite(true); editor.setOverwrite(true);
@ -227,12 +231,13 @@ var actions = exports.actions = {
if (defaultReg.isLine) { if (defaultReg.isLine) {
var pos = editor.getCursorPosition(); var pos = editor.getCursorPosition();
var lines = defaultReg.text.split("\n"); pos.column = 0;
editor.session.getDocument().insertLines(pos.row, lines); var text = lang.stringRepeat(defaultReg.text + "\n", count || 1);
editor.moveCursorTo(pos.row, 0); editor.session.insert(pos, text);
editor.moveCursorToPosition(pos);
} }
else { else {
editor.insert(defaultReg.text); editor.insert(lang.stringRepeat(defaultReg.text, count || 1));
} }
editor.setOverwrite(true); editor.setOverwrite(true);
editor.selection.clearSelection(); editor.selection.clearSelection();
@ -280,17 +285,23 @@ var actions = exports.actions = {
}, },
":": { ":": {
fn: function(editor, range, count, param) { fn: function(editor, range, count, param) {
// not implemented var val = ":";
if (count > 1)
val = ".,.+" + count + val;
if (editor.showCommandLine)
editor.showCommandLine(val);
} }
}, },
"/": { "/": {
fn: function(editor, range, count, param) { fn: function(editor, range, count, param) {
// not implemented if (editor.showCommandLine)
editor.showCommandLine("/");
} }
}, },
"?": { "?": {
fn: function(editor, range, count, param) { fn: function(editor, range, count, param) {
// not implemented if (editor.showCommandLine)
editor.showCommandLine("?");
} }
}, },
".": { ".": {
@ -327,15 +338,19 @@ var inputBuffer = exports.inputBuffer = {
lastInsertCommands: [], lastInsertCommands: [],
push: function(editor, ch, keyId) { push: function(editor, ch, keyId) {
var status = this.status;
var isKeyHandled = true; var isKeyHandled = true;
this.idle = false; this.idle = false;
var wObj = this.waitingForParam; var wObj = this.waitingForParam;
if (/^numpad\d+$/i.test(ch))
ch = ch.substr(6);
if (wObj) { if (wObj) {
this.exec(editor, wObj, ch); this.exec(editor, wObj, ch);
} }
// If input is a number (that doesn't start with 0) // If input is a number (that doesn't start with 0)
else if (!(ch === "0" && !this.currentCount.length) && else if (!(ch === "0" && !this.currentCount.length) &&
(ch.match(/^\d+$/) && this.isAccepting(NUMBER))) { (/^\d+$/.test(ch) && this.isAccepting(NUMBER))) {
// Assuming that ch is always of type String, and not Number // Assuming that ch is always of type String, and not Number
this.currentCount += ch; this.currentCount += ch;
this.currentCmd = NUMBER; this.currentCmd = NUMBER;
@ -389,6 +404,7 @@ var inputBuffer = exports.inputBuffer = {
this.idle = false; this.idle = false;
} }
else if (this.operator) { else if (this.operator) {
this.operator.count = this.getCount();
this.exec(editor, { operator: this.operator }, ch); this.exec(editor, { operator: this.operator }, ch);
} }
else { else {
@ -402,10 +418,9 @@ var inputBuffer = exports.inputBuffer = {
this.status = this.currentCount; this.status = this.currentCount;
} else if (this.status) { } else if (this.status) {
this.status = ""; this.status = "";
} else {
return isKeyHandled;
} }
editor._emit("changeStatus"); if (this.status != status)
editor._emit("changeStatus");
return isKeyHandled; return isKeyHandled;
}, },

View file

@ -138,6 +138,8 @@ function find(editor, needle, dir) {
var Range = require("../../../range").Range; var Range = require("../../../range").Range;
var LAST_SEARCH_MOTION = {};
module.exports = { module.exports = {
"w": new Motion(function(editor) { "w": new Motion(function(editor) {
var str = new StringStream(editor); var str = new StringStream(editor);
@ -381,7 +383,9 @@ module.exports = {
"f": new Motion({ "f": new Motion({
param: true, param: true,
handlesCount: true, handlesCount: true,
getPos: function(editor, range, count, param, isSel) { getPos: function(editor, range, count, param, isSel, isRepeat) {
if (!isRepeat)
LAST_SEARCH_MOTION = {ch: "f", param: param};
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var column = util.getRightNthChar(editor, cursor, param, count || 1); var column = util.getRightNthChar(editor, cursor, param, count || 1);
@ -394,7 +398,9 @@ module.exports = {
"F": new Motion({ "F": new Motion({
param: true, param: true,
handlesCount: true, handlesCount: true,
getPos: function(editor, range, count, param, isSel) { getPos: function(editor, range, count, param, isSel, isRepeat) {
if (!isRepeat)
LAST_SEARCH_MOTION = {ch: "F", param: param};
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var column = util.getLeftNthChar(editor, cursor, param, count || 1); var column = util.getLeftNthChar(editor, cursor, param, count || 1);
@ -407,10 +413,15 @@ module.exports = {
"t": new Motion({ "t": new Motion({
param: true, param: true,
handlesCount: true, handlesCount: true,
getPos: function(editor, range, count, param, isSel) { getPos: function(editor, range, count, param, isSel, isRepeat) {
if (!isRepeat)
LAST_SEARCH_MOTION = {ch: "t", param: param};
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var column = util.getRightNthChar(editor, cursor, param, count || 1); var column = util.getRightNthChar(editor, cursor, param, count || 1);
if (isRepeat && column == 0 && !(count > 1))
var column = util.getRightNthChar(editor, cursor, param, 2);
if (typeof column === "number") { if (typeof column === "number") {
cursor.column += column + (isSel ? 1 : 0); cursor.column += column + (isSel ? 1 : 0);
return cursor; return cursor;
@ -420,16 +431,46 @@ module.exports = {
"T": new Motion({ "T": new Motion({
param: true, param: true,
handlesCount: true, handlesCount: true,
getPos: function(editor, range, count, param, isSel) { getPos: function(editor, range, count, param, isSel, isRepeat) {
if (!isRepeat)
LAST_SEARCH_MOTION = {ch: "T", param: param};
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var column = util.getLeftNthChar(editor, cursor, param, count || 1); var column = util.getLeftNthChar(editor, cursor, param, count || 1);
if (isRepeat && column == 0 && !(count > 1))
var column = util.getLeftNthChar(editor, cursor, param, 2);
if (typeof column === "number") { if (typeof column === "number") {
cursor.column -= column; cursor.column -= column;
return cursor; return cursor;
} }
} }
}), }),
";": new Motion({
handlesCount: true,
getPos: function(editor, range, count, param, isSel) {
var ch = LAST_SEARCH_MOTION.ch;
if (!ch)
return;
return module.exports[ch].getPos(
editor, range, count, LAST_SEARCH_MOTION.param, isSel, true
);
}
}),
",": new Motion({
handlesCount: true,
getPos: function(editor, range, count, param, isSel) {
var ch = LAST_SEARCH_MOTION.ch;
if (!ch)
return;
var up = ch.toUpperCase();
ch = ch === up ? ch.toLowerCase() : up;
return module.exports[ch].getPos(
editor, range, count, LAST_SEARCH_MOTION.param, isSel, true
);
}
}),
"^": { "^": {
nav: function(editor) { nav: function(editor) {
@ -614,7 +655,7 @@ module.exports = {
}; };
module.exports.backspace = module.exports.left = module.exports.h; module.exports.backspace = module.exports.left = module.exports.h;
module.exports.right = module.exports.l; module.exports.space = module.exports.return = module.exports.right = module.exports.l;
module.exports.up = module.exports.k; module.exports.up = module.exports.k;
module.exports.down = module.exports.j; module.exports.down = module.exports.j;
module.exports.pagedown = module.exports["ctrl-d"]; module.exports.pagedown = module.exports["ctrl-d"];

View file

@ -30,7 +30,7 @@
define(function(require, exports, module) { define(function(require, exports, module) {
"never use strict"; "use strict";
var util = require("./util"); var util = require("./util");
var registers = require("../registers"); var registers = require("../registers");
@ -58,7 +58,6 @@ module.exports = {
var selRange = editor.getSelectionRange(); var selRange = editor.getSelectionRange();
// check if end of the document was reached // check if end of the document was reached
if (!selRange.isMultiLine()) { if (!selRange.isMultiLine()) {
lastLineReached = true
var row = selRange.start.row - 1; var row = selRange.start.row - 1;
var col = editor.session.getLine(row).length var col = editor.session.getLine(row).length
selRange.setStart(row, col); selRange.setStart(row, col);