Permalink
Browse files

Adding more commands and improving existing ones.

  • Loading branch information...
1 parent b290151 commit aac24ee573f42d9e191662e7ae365b10b229190b @Gozala committed Sep 8, 2011
Showing with 209 additions and 25 deletions.
  1. +35 −2 commands.js
  2. +51 −11 keyboard.js
  3. +11 −1 params.js
  4. +112 −11 utils.js
View
@@ -94,7 +94,6 @@ var commands = exports.commands = {
man: 'Stop Insert mode as soon as possible. Works like' +
'typing <Esc> in **insert** mode.',
exec: function stop(env, params, request) {
- moveBack(env, 1)
normalMode(env)
}
},
@@ -183,8 +182,42 @@ var commands = exports.commands = {
},
deleteCharBack: function(env, params, request) {
removePreviosChars(env, params.count)
+ },
+ moveForwardTo: {
+ params: [ types.count, types.char ],
+ exec: function(env, params, request) {
+ utils.moveForwardTo(env, types.char.valueOf(params.char), params.count)
+ }
+ },
+ moveForwardAt: {
+ params: [ types.count, types.char ],
+ exec: function(env, params, request) {
+ utils.moveForwardAt(env, types.char.valueOf(params.char), params.count)
+ }
+ },
+ moveBackwardTo: {
+ params: [ types.count, types.char ],
+ exec: function(env, params, request) {
+ utils.moveBackwardTo(env, types.char.valueOf(params.char), params.count)
+ }
+ },
+ moveBackwardAt: {
+ params: [ types.count, types.char ],
+ exec: function(env, params, request) {
+ utils.moveBackwardAt(env, types.char.valueOf(params.char), params.count)
+ }
+ },
+ moveToFirstChar: function(env, params, request) {
+ utils.moveToFirstChar(env)
+ },
+ searchForward: {
+ exec: function(env, params, request) {
+ }
+ },
+ searchBackword: {
+ exec: function(env, params, request) {
+ }
}
-
}
exports.plug = function plug(data, reason) {
View
@@ -14,33 +14,41 @@ var StateHandler = require("ace/keyboard/state_handler").StateHandler
var keyUtil = require("pilot/keys");
var matchCharacterOnly = require("ace/keyboard/state_handler").matchCharacterOnly
-
var states = exports.states = {
start: [ // normal mode
{
- key: ':',
+ key: "esc",
+ exec: 'stop',
+ then: "start"
+ },
+ {
+ regex: '^:$',
exec: 'commandLine'
},
{
- key: 'i',
+ regex: '^u$',
+ exec: 'undo'
+ },
+ {
+ regex: '^i$',
params: [ types.count ],
exec: 'start',
then: 'insertMode'
},
{
- key: 'shift-i',
+ regex: '^shift-i$',
params: [ types.bang ],
exec: 'start',
then: 'insertMode'
},
{
- key: 'a',
+ regex: '^a$',
params: [ types.count ],
exec: 'append',
then: 'insertMode'
},
{
- key: 'shift-a',
+ regex: '^shift-a$',
params: [ types.count, types.bang ],
exec: 'append',
then: 'insertMode'
@@ -90,6 +98,30 @@ var states = exports.states = {
params: [ types.count ]
},
{
+ key: null,
+ regex: [ types.count.regex, 't', types.char.regex ],
+ exec: "moveForwardTo",
+ params: [ types.count, types.char ]
+ },
+ {
+ key: null,
+ regex: [ types.count.regex, 'shift-t', types.char.regex ],
+ exec: "moveBackwardTo",
+ params: [ types.count, types.char ]
+ },
+ {
+ key: null,
+ regex: [ types.count.regex, 'f', types.char.regex ],
+ exec: "moveForwardAt",
+ params: [ types.count, types.char ]
+ },
+ {
+ key: null,
+ regex: [ types.count.regex, 'shift-f', types.char.regex ],
+ exec: "moveBackwardAt",
+ params: [ types.count, types.char ]
+ },
+ {
regex: [ types.count.regex, 'd', 'd' ],
exec: 'deleteLines',
params: [ types.count ]
@@ -99,14 +131,18 @@ var states = exports.states = {
params: [ types.line ]
},
{
- key: 'shift-4',
+ regex: '^shift-4$',
exec: 'gotolineend'
},
{
- key: 'shift-6',
+ regex: '^shift-6$',
exec: 'gotolinestart'
},
{
+ regex: '^0$',
+ exec: 'moveToFirstChar'
+ },
+ {
regex: [ types.count.regex, '(e|shift-e)' ],
exec: 'goToEndWord',
params: [ types.count ]
@@ -117,11 +153,11 @@ var states = exports.states = {
params: [ types.count ]
},
{
- key: 'shift-g',
+ regex: '^shift-g$',
exec: 'gotoend'
},
{
- regex: [ 'g', 'g' ],
+ regex: '^gg$',
exec: 'gotostart'
},
{
@@ -135,9 +171,13 @@ var states = exports.states = {
params: [ types.count ]
},
{
+ regex: 'command-s',
+ exec: 'write'
+ },
+ {
comment: 'Catch some keyboard input to stop it here',
match: matchCharacterOnly
- }
+ },
],
insertMode: [
{
View
@@ -20,7 +20,7 @@ exports.bang = {
exports.count = {
name: 'count',
- regex: '([0-9]*)',
+ regex: '^([0-9]*)',
match: 1,
type: 'number',
defaultValue: 1
@@ -33,4 +33,14 @@ exports.line = {
type: 'number'
}
+exports.char = {
+ name: 'char',
+ regex: '((?:shift-){0,1}[\\s\\S])',
+ match: 2,
+ type: 'text',
+ valueOf: function(input) {
+ return input.length > 1 ? input.replace('shift-', '').toUpperCase() : input
+ }
+}
+
});
View
123 utils.js
@@ -2,6 +2,7 @@
/*jshint asi: true undef: true es5: true node: true devel: true browser: true
forin: true latedef: false */
/*global define: true */
+
!define(function(require, exports, module) {
'use strict';
@@ -61,59 +62,159 @@ exports.removeLines = removeLines
function moveToEndOfNextWord(env) {
env.editor.navigateWordRight()
}
-// Move curret to the end of the n'th word on the right.
+
function moveToEndOfFollowingWord(env, count) {
+ doc: "Move caret to the end of the n'th word on the right."
+
repeat(moveToEndOfNextWord, count, [env])
}
exports.moveToEndOfFollowingWord = moveToEndOfFollowingWord
-// Move curret to the enf of the word on the left.
function moveToEndOfPreviosWord(env) {
+ doc: "Move caret to the enf of the word on the left."
+
env.editor.navigateWordLeft()
}
-// Move curret to the end of the n'th word on the left.
function moveToEndOfPassedWord(env, count) {
+ doc: "Move caret to the end of the n'th word on the left."
+
repeat(moveToEndOfPreviosWord, count, [env])
}
exports.moveToEndOfPassedWord = moveToEndOfPassedWord
-// Remove char on the left side of the curret.
function removePreviosChar(env) {
+ doc: "Remove char on the left side of the caret."
+
env.editor.removeLeft()
}
-// Remove `count` chars on the left side of the curret.
function removePreviosChars(env, count) {
+ doc: "Remove `count` chars on the left side of the caret."
+
repeat(removePreviosChar, count, [env])
}
exports.removePreviosChars = removePreviosChars
-// Remove one chare on the right side of the curret.
function removeFollowingChar(env) {
+ doc: "Remove one chare on the right side of the caret."
+
env.editor.removeRight()
}
-// Remove `count` chars on the right side of the curret.
function removeFollowingChars(env, count) {
+ doc: "Remove `count` chars on the right side of the caret."
+
repeat(removeFollowingChar, count, [env])
}
exports.removeFollowingChars = removeFollowingChars
-// Switch editor to normal mode
function normalMode(env) {
- //env.editor.focus()
+ doc: "Switch editor to normal mode"
+
env.editor.setStyle('normal-mode')
+ env.editor.clearSelection()
+ if (!env.editor.getOverwrite()) moveBack(env, 1)
env.editor.setOverwrite(true)
}
exports.normalMode = normalMode
-// Switch editor to insert mode
function insertMode(env) {
- //env.editor.focus()
+ doc: "Switch editor to insert mode"
+
env.editor.unsetStyle('normal-mode')
env.editor.setOverwrite(false)
}
exports.insertMode = insertMode
+function cursor(env) {
+ doc: "Gets current cursor position: { row: 5, column: 2 }"
+
+ return env.editor.getSelection().getSelectionLead()
+}
+
+function row(env) {
+ doc: "Returns row cursor is on."
+
+ return cursor(env).row
+}
+
+function column(env) {
+ doc: "Returns column cursor is at."
+
+ return cursor(env).column
+}
+
+function line(env, number) {
+ doc: "Returns content under the given line number."
+
+ return env.editor.getSession().getLine(number)
+}
+
+function navigateTo(env, row, column) {
+ doc: "Moves cursor to the given row and column."
+
+ env.editor.moveCursorToPosition({ row: row, column: column })
+}
+exports.navigateTo = navigateTo
+
+function moveToFirstChar(env) {
+ doc: "Move to the first character of the line."
+
+ navigateTo(env, row(env), 0)
+}
+exports.moveToFirstChar = moveToFirstChar
+
+function followingCharColumn(env, row, column, char, n) {
+ doc: "Returns position of the `n`-th `char` on the give `row` from the given"
+ | "column`. If not found `0` is returned."
+
+ var matches = line(env, row).substr(column).split(char)
+ return n < matches.length ? matches.slice(0, n).join(char).length : 0
+}
+
+function previousCharColumn(env, row, column, char, n) {
+ doc: "Returns position of the `n`-th `char` on the give `row` from the given"
+ | "column` to the left. If not found `0` is returned."
+
+ var matches = line(env, row).substr(0, column).split(char)
+ return n < matches.length ? matches.slice(-1 * n).join(char).length + 1: 0
+}
+
+function moveForwardTo(env, char, n) {
+ doc: "Moves caret in front of next `n`-th `char` on the line."
+
+ var axis = cursor(env)
+ var column = followingCharColumn(env, axis.row, axis.column + 1, char, n)
+ if (column) navigateTo(env, axis.row, axis.column + column)
+}
+exports.moveForwardTo = moveForwardTo
+
+function moveForwardAt(env, char, n) {
+ doc: "Moves caret to the next `n`-th (right) `char` on the line."
+
+ var axis = cursor(env)
+ var column = followingCharColumn(env, axis.row, axis.column + 1, char, n)
+ if (column) navigateTo(env, axis.row, axis.column + column + 1)
+}
+exports.moveForwardAt = moveForwardAt
+
+function moveBackwardTo(env, char, n) {
+ doc: "Moves caret just after the previous `n`-th (left) `char` on the line."
+
+ var axis = cursor(env)
+ var column = previousCharColumn(env, axis.row, axis.column - 1, char, n)
+ if (column) navigateTo(env, axis.row, axis.column - column)
+}
+exports.moveBackwardTo = moveBackwardTo
+
+function moveBackwardAt(env, char, n) {
+ doc: "Moves caret to the prvious `n`-th (left) `char` on the line."
+
+ var axis = cursor(env)
+ var column = previousCharColumn(env, axis.row, axis.column, char, n)
+ if (column) navigateTo(env, axis.row, axis.column - column)
+}
+exports.moveBackwardAt = moveBackwardAt
+
});

0 comments on commit aac24ee

Please sign in to comment.