NB: The Emacs world uses the word frame to refer to what in most other contexts is called window, and uses the word window to describe something else. In this post, however, the word window always refers to an X11 window.

I routinely run subordinate shells under Emacs. In these shells I often need to run programs that pop up a separate graphics window and then proceed to display sequences of images (charts of scientific data) in this window. After displaying each image in the sequence, the program prints a prompt such as

Hit <Return> to see next plot:

...and pauses for user input1.

Unfortunately, at this point the graphic window has the focus, and does not respond to any keyboard input.

Therefore, in order to see every next image, one must first switch the focus to the Emacs window before one can press Return.

This is no big deal when the sequence consists of 2-3 pictures, especially since I can switch the focus from the keyboard (with Alt+Tab but the utility of this sort of program is to be able to quickly inspect tens (sometimes even hundreds) of images. It is in this use-case that the extra step of returning the focus to the Emacs window starts getting seriously in the way.

Is there a way to configure Emacs so that, whenever a subordinate program pops up a new window

  1. the Emacs window retains the focus; and
  2. the new window (even without the focus) remains always on top of the Emacs window2


(Of these two items, the first one is far more important than the second one.)

1A quick example of the problem, for those who have R installed, is the following: (1) start a sub-shell (M-x shell); (2) in this shell start an interactive R session; (3) in the R session run the command example(plot). This will present a sequence of examples from the plot command's documentation page. (If you have the ESS (aka Emacs Speaks Statistics) package installed, steps (1) and (2) can be replaced with the command M-x R.) This example is a somewhat different use-case from the one I'm dealing with, since it's about displaying documentation rather than data, but it is one that others can more readily try out than my actual use-case, and conveys all the essential information.

2 When working with a small screen (e.g. a laptop) the focus-switching business entails the additional (though more tractable) inconvenience that the window with the focus, by default, obscures all other windows. This means that, when the Emacs window is maximized (as is normally the case when I work on my laptop, for example), switching focus entails obscuring the plot temporarily. This greatly hampers the ability to make visual comparisons between a plot and its successor. As already said, this problem is at least solvable: un-maximize the Emacs window, and give it a size consistent with having both it and the graphic window simultaneously visible.

  • (I assume that that prompt is from Emacs and it expects minibuffer input.) Some window managers give the focus to a new frame when it is created. I don't have time now to look into all of your post, but it sounds like what you need to do is redirect the frame focus back to the frame that has the minibuffer, which is where you are expecting the input: (redirect-frame-focus (selected-frame) FRAME), where FRAME is the previously selected frame, with the active minibuffer. You can also use select-frame-set-input-focus to switch the focus. – Drew May 15 '17 at 14:17
  • > (I assume that that prompt is from Emacs and it expects minibuffer input.) No, actually, the prompt does not appear in the minibuffer, but rather in the sub-shell's main buffer. – kjo May 15 '17 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.