Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created July 9, 2015 04:48
Show Gist options
  • Save dtinth/2e81cbae13b9bab73542 to your computer and use it in GitHub Desktop.
Save dtinth/2e81cbae13b9bab73542 to your computer and use it in GitHub Desktop.
Atom chords add-on (to be made as plugin soon)
do -> # Chords add-on
# Uppercase letter only, must be sorted alphabetically
chords = {
'AKW': '川',
'ERQ': 'require',
'IMP': 'import'
'FNU': 'function',
}
matchChords = (history, chords) ->
maxChordLength = Math.max((key.length for key of chords)...)
for i in [2..maxChordLength]
chars = (char for { char } in history.slice(-i))
chars.sort()
result = chords[chars.join('')]
if result
return {
length: i
expansion: result
}
return null
atom.workspace.observeTextEditors (editor) ->
view = atom.views.getView(editor)
history = []
view.addEventListener 'keydown', (event) ->
return if event.ctrlKey or event.shiftKey or event.metaKey
while history.length and history[0].timeStamp < event.timeStamp - 64
history.shift()
if 65 <= event.keyCode <= 90
history.push
char: String.fromCharCode(event.keyCode)
timeStamp: event.timeStamp
match = matchChords(history, chords)
if match
setTimeout ->
editor.selectLeft(match.length)
editor.insertText(match.expansion)
else
history.length = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment