3

In case it's relevant, I'm on a Mac, running OSX 10.12 and Carbon Emacs 24.3.1.

If I open a file using /Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n filepath the Emacs window comes to the top and gets focus unless opening the file triggers a minibuffer prompt (such as Symbolic link to Hg-controlled source file; follow link? (yes or no).

Is there a way to configure Emacs to grab focus and raise its window in these cases as well?

1 Answers1

3

Assuming that are you using the GUI frame, basically you just need pass -e '(x-focus-frame nil)' as argument of emacsclient.

In that case: emacsclient -n -e '(x-focus-frame nil)' filepath

EDIT by OP: (You may need to use: emacsclient -n -e "(progn (x-focus-frame nil) (find-file \"filepath\"))" —— See documentation for emacsclient's -e parameter.)

I wrote a similar script to accomplish that here and another stuff for personal reasons. Mainly to avoid pain about opening multiple frames running on daemon mode like:

  • handling daemon crashing (and restarting for my init system [OpenRC])
  • be sure that when a frame exists, opening the file on it instead creating a new one
  • if a current frame was used, ensure that raises that frame and focus it.

The script is in some way portable and you can try to use it in the last case.

Manoel Vilela
  • 675
  • 3
  • 20
  • This got me there with a couple of changes: `(raise-frame)` did not work, but `(x-focus-frame nil)` did; and (empirically and from `emacsclient --help`) I can't use plain filepath to open a file if I also use `-e`. What worked for me: `emacsclient -n -e "(progn (x-focus-frame nil) (find-file \"$1\"))"` – Joshua Goldberg Nov 03 '17 at 14:36
  • See: https://emacs.stackexchange.com/questions/29512/emacsclient-opens-a-file-and-does-eval-simultaneously and https://emacs.stackexchange.com/questions/9826/can-you-activate-bring-to-front-emacs-using-native-elisp-e-g-when-working-in – Joshua Goldberg Nov 03 '17 at 14:38
  • Probably is different because you are running on Mac Os X? Works fine for me `(raise-frame)`, I'm editing the answer. – Manoel Vilela Nov 03 '17 at 21:21
  • About the filepath stuff, try executing in the way that I edited the answer, works fine here: keep the filepath on the last argument. – Manoel Vilela Nov 03 '17 at 21:32
  • Re: filepath, if I do that in my setup, the window raises, the file does not open, and the console shows "*ERROR*: Symbol's value as variable is void: filepath". Maybe an emacs version difference? – Joshua Goldberg Nov 04 '17 at 01:30
  • Well... are you sure that you are keeping the rounded quotes for the argument after `-e`? Like `-e '(x-focus-frame nil)' filepath`. Maybe can be a version problem, but I used the 25.4.1, 26.0 and 27.0 and don't have any problem about that. – Manoel Vilela Nov 04 '17 at 04:26
  • 1
    Wr.t. raise-frame -vs- focus-frame: they're *both* needed in general (the effect of either of them is dependent on the window system/manager in use). – Stefan Nov 08 '17 at 18:13