Adding actions to a keymap?


#1

In Hydrogen, I’ve got ctrlenter mapped to run the current line:

'atom-workspace atom-text-editor:not([mini]), .platform-linux atom-text-editor':
  'ctrl-enter': 'hydrogen:run'

That’s fabulous and works great. But what I’d really like to do is run the current line and then move the cursor to the next line. I wanted this to work:

'atom-workspace atom-text-editor:not([mini]), .platform-linux atom-text-editor':
  'ctrl-enter': 'hydrogen:run, core:move-down'

but it doesn’t (with that keybinding, nothing happens on ctrlenter.) Is there a way to combine the two actions or does Hydrogen need to define an action that includes moving the cursor before I can use it?


#2

See my answer on Stack Overflow regarding how to compose two or more commands:


#3

Cool. I came up with …

atom.commands.add 'atom-text-editor', 'custom:exec-and-move', ->
  editor = atom.workspace.getActiveTextEditor()
  editor.hydrogen:run()
  editor.core:move-down()

which doesn’t work (nothing happens) – would love help fine tuning that syntax!


#4

Ah, you’re conflating commands with functions on an object (like the editor object in your example). Commands are functions but functions can’t directly call commands. What you’re looking for is atom.commands.dispatch, but invoking it is a little complicated in the way you’re wanting to. If nobody else can get to it soon, I’ll help out after I get home from work tonight.


#5

You should change that to

atom.commands.add 'atom-text-editor', 'custom:exec-and-move', ->
  editor = atom.views.getView(atom.workspace.getActiveTextEditor())
  atom.commands.dispatch(editor, 'hydrogen:run')
  atom.commands.dispatch(editor, 'core:move-down')

The atom.commands.dispatch tells Atom to run that command on the DOM element you provide, in this case that of the active text editor.


#6

Thanks! I’ve got …

In init.coffee:

atom.commands.add 'atom-text-editor', 'custom:exec-and-move', ->
  editor = atom.views.getView(atom.workspace.getActiveTextEditor())
  atom.commands.dispatch(editor, 'hydrogen:run')
  atom.commands.dispatch(editor, 'core:move-down')

And in keymap.cson:

'atom-workspace atom-text-editor:not([mini]), .platform-linux atom-text-editor':
  'ctrl-enter': 'custom:exec-and-move'

Definitely works, thought i seems like Hydrogen is taking a really long time to run. Probably a different problem, tho.