9

When I want to switch between programs in Windows, I ordinarily use the windows key plus a number key to run a program from my taskbar. Most programs will either start running or, if there's already an instance, bring it to the foreground and focus on it. But Emacs (started with runemacs.exe) will create a new entry on the taskbar, and using the shortcut again will just create a new instance. As far as I can tell, I can't even switch between the different frames using Emacs commands (I tried C-x 5 o).

Is there a way to make it so subsequent uses of the windows hotkeys will just use the existing instance of Emacs and, if not, can I at least make it so I can easily navigate between the different frames? I tried this snippet:

(require 'server)
(unless (server-running-p)
  (start-server)) 

from the answer to this question, but it didn't make a difference.

resueman
  • 401
  • 3
  • 10
  • Can you specify which snippet you are referring to? – nispio Oct 15 '14 at 23:48
  • That snippet makes sure that an edit server is created the first time that Emacs is run. Subsequent calls to Emacs which wish to connect to the same edit server must use `emacsclientw.exe` – nispio Oct 15 '14 at 23:55
  • @nispio Ah, that explains why it wasn't working. Is there a way to have everything run on the same edit server without needing to run from different executables? – resueman Oct 15 '14 at 23:56
  • This answer will probably work: http://emacs.stackexchange.com/a/158/93. When you provide `runemacs.exe` as an alternate editor, you can call `emacsclientw.exe` even when there is no edit server running. – nispio Oct 15 '14 at 23:59
  • @resueman Try following the answer @nispio pointed to and edit your taskbar shortcut point to `emacsclientw.exe -c -n` instead of `runemacs.exe`. That should pop up a new frame everytime you use the shortcut. See if this helps. You might have issues with older emacsen (< 24 I guess) though. – Vamsi Oct 16 '14 at 00:21
  • @Vamsi Adding in the `-c` flag seems to work. Thank you. – resueman Oct 16 '14 at 00:30
  • 1
    See my comment to nispio 's answer regarding how to avoid creating a new frame. – Vamsi Oct 16 '14 at 00:36

2 Answers2

12

I stumbled on another answer to the problem on SuperUser today. It seems more hackish, but the result is more consistent with other programs on Windows.

  1. Run runemacs.exe with no pre-existing icon in the taskbar.
  2. Right click on the running Emacs icon in the taskbar, and click on "pin this program to taskbar."
  3. Close Emacs
  4. Shift right-click on the pinned Emacs icon on the taskbar, click on Properties, and change the target from emacs.exe to runemacs.exe.
resueman
  • 401
  • 3
  • 10
  • 1
    FWIW, that's what I do. I didn't understand that that's what you wanted, from your question. I thought you wanted to use emacsclient, among other things. BTW, in `Properties` you can also set the `Start in` directory and open Emacs to a particular file or directory. (I start Emacs with Dired in the `Start in` directory. IOW, I pass the same dir (`"`-quoted) as argument to `runemacs`.) – Drew Oct 18 '14 at 03:10
  • @Drew I probably didn't explain what I wanted very well. I'm still new enough that it's hard to keep everything straight. Thanks for the tips. – resueman Oct 18 '14 at 03:13
  • This is the most straightforward answer, thanks. – gsl Mar 27 '21 at 15:01
8

Add this to your init file to make sure that an edit server is created when emacs starts

(require 'server)
(unless (server-running-p)
  (server-start)) 

Now make your windows shortcut point to emacsclientw.exe as follows:

C:\path\to\emacsclientw.exe -n -e "(raise-frame)" -a "C:\path\to\runemacs.exe"

This should focus an existing Emacs frame (if any), or else call runemacs.exe otherwise. (Thanks to Vamsi for help with tuning the command parameters)

nispio
  • 8,175
  • 2
  • 35
  • 73
  • This works how I want if I pass it a file to run, but if I just use the command as-is then it displays an error saying "file name or argument required." – resueman Oct 16 '14 at 00:10
  • @resueman Does a `-c` flag fix that problem? – nispio Oct 16 '14 at 00:27
  • 3
    @nispio One can use `C:\path\to\emacsclientw.exe -n -e "(raise-frame)" -a "C:\path\to\runemacs.exe"` to avoid creating a new frame and raise the previous frame. – Vamsi Oct 16 '14 at 00:35
  • @Vamsi but without the `-c` will it also expect a file to visit? (If you can't tell I'm not at a Windows machine right now.) – nispio Oct 16 '14 at 00:36
  • @Vamsi That works perfectly. @nispio it seems to work fine without `-c` as long as it has the `-e` expression. – resueman Oct 16 '14 at 00:43
  • @resueman Thanks for confirming. Here I was booting up my windows machine. – Vamsi Oct 16 '14 at 00:50
  • I got error: Symbol's function definition is void: start-server. (start-server) should be (server-start) – chendesheng Jan 17 '17 at 08:41