`window:focus-previous-pane` doesn't work from init.coffee


#1

I define a custom command in init.coffee which opens a new pane and does some output there, but I want to keep the focus (cursor) on the text editor. I tried two commands to get this behavior, but both don’t work. In init.coffee I have:

atom.commands.add 'atom-text-editor', 'custom:command', ->
  editor = atom.workspace.getActivePaneItem()
  ...
  atom.commands.dispatch(atom.views.getView(editor), 'command:newPane')
  # try to get focus back
  # this doesn't work:
  atom.workspace.activatePreviousPane()
  # this doesn't work either:
  atom.commands.dispatch(atom.views.getView(atom.workspace), 'window:focus-previous-pane')

If I run
window:focus-previous-pane
from the command palette it works, though.


#2

Commands are typically designed with a specific use case in mind. If your use doesn’t match how they are expecting to be used, they will behave unpredictably or simply not work. It is almost always better to simply use the Atom API than try to force commands to do something that they weren’t designed to do.

Also, when you dispatch a command, to my understanding it happens asynchronously. So you’re probably trying to activate the previous pane before the new pane is even open yet when you run it as code. When you run it manually, of course you wait for the new pane to open before executing the command.


#3

Ok, but if the command:newPane is a complex command from a package (I’ve not written), and I just want to alter the behavior slightly (like keeping the focus), what would be my options then? If you mean with “…simply use the Atom API…” that I should fork that other package and adapt it to my needs, that sounds like a lot of work. For such a tiny tweak I have in mind – where is the hack? Isn’t this “A hackable text editor for the 21st Century”? :wink:


#4

Community packages are guaranteed to have wildly varying quality and extensibility. The amount of work needed to make the package do what you want it to is similarly variable.

Which package has behavior that you’re trying to modify?


#5

No, I mean that you should use the Pane.splitRight() function (or whichever of the directions suits your fancy). Commands aren’t intended to be a way for code to interact with other code, which is why you’re running into the problems you are :grinning:


#6

The actually command (which I simply called command:newPane before) is the one from this package:


I’m not sure how Pane.splitRight() helps me here? because the package command will again open a new pane and start a process etc…
Sorry for my inexperience… :thinking:


#7

Pane.splitRight() is probably what command-executor is doing under the covers of the command you’re calling. It creates a new pane to the right of the currently focused one. So I’m suggesting that you directly call the code function instead of going through a mechanism that isn’t designed for what you’re doing.


#8

@leedohm Got the idea! Thanks.
Sadly, command-executor is written in .js, so it will be tiresome to extract the necessary information. I guess I’ll write my own package in the end…