keymap.coffee | |
---|---|
This is a pretty standard key-to-command keymap except for a few details:
| class Keymap |
Building a Keymap | |
Build an instance of | @getDefault: ->
keymap = new Keymap
keymap.mapCommand keys, commandClass for own keys, commandClass of require('./commands').defaultMappings
keymap.mapOperator keys, operationClass for own keys, operationClass of require('./operators').defaultMappings
keymap.mapMotion keys, motionClass for own keys, motionClass of require('./motions').defaultMappings
keymap
constructor: ->
@commands = {}
@motions = {}
@visualCommands = {} |
Use some objects to de-duplicate repeated partial commands. | @partialCommands = {}
@partialMotions = {}
@partialVisualCommands = {} |
Mapping commands | |
Map the | mapCommand: (keys, commandClass) ->
if commandClass::exec
@commands[keys] = commandClass
if keys.length is 2
@partialCommands[keys[0]] = true
if commandClass::visualExec
@visualCommands[keys] = commandClass
if keys.length is 2
@partialVisualCommands[keys[0]] = true |
Map | mapMotion: (keys, motionClass) ->
@commands[keys] = motionClass
@motions[keys] = motionClass
@visualCommands[keys] = motionClass
if keys.length is 2
@partialMotions[keys[0]] = true
@partialCommands[keys[0]] = true
@partialVisualCommands[keys[0]] = true |
Map | mapOperator: (keys, operatorClass) ->
@commands[keys] = operatorClass
@visualCommands[keys] = operatorClass
if keys.length is 2
@partialCommands[keys[0]] = true
@partialVisualCommands[keys[0]] = true |
Finding commands in the Keymap
| |
Build a regex that will help us split up the
| buildPartialCommandRegex = (partialCommands) ->
///
^
([1-9]\d*)?
(
[#{(char for own char, nothing of partialCommands).join ''}]?
([\s\S]*)
)?
$
/// |
Find a normal mode command, which could be a motion, an operator, or a "regular" normal mode command. | commandFor: (commandPart) ->
@partialCommandRegex or= buildPartialCommandRegex @partialCommands
[commandPart, count, command, beyondPartial] = commandPart.match @partialCommandRegex
if beyondPartial
if commandClass = @commands[command]
new commandClass(parseInt(count) or null)
else
false
else
true |
Find a motion. | motionFor: (commandPart, operatorPending) ->
@partialMotionRegex or= buildPartialCommandRegex @partialMotions
[commandPart, count, motion, beyondPartial] = commandPart.match @partialCommandRegex
if beyondPartial
if motion is operatorPending |
If we're finding | {LinewiseCommandMotion} = require './motions'
new LinewiseCommandMotion(parseInt(count) or null)
else if motionClass = @motions[motion]
new motionClass(parseInt(count) or null)
else
false
else
true |
Find a visual mode command, which could be a motion, an operator, or a "regular" visual mode command. | visualCommandFor: (commandPart) ->
@partialVisualCommandRegex or= buildPartialCommandRegex @partialVisualCommands
[commandPart, count, command, beyondPartial] = commandPart.match @partialVisualCommandRegex
if beyondPartial
if commandClass = @visualCommands[command]
new commandClass(parseInt(count) or null)
else
false
else
true |
Exports | module.exports = Keymap
|