Ryan
Ryan

Reputation: 918

Open file in existing emacs frame (Windows)

I'm using Emacs 24.3 on Windows 8. I want to be able to right-click a file and select "Edit with Emacs" and have the file open in an existing emacs frame. All steps I have done so far are listed below. Most of it was taken direction from the Emacs documentation page for Windows.

The following are the registry keys I used to add "Edit with Emacs" to my context menu:

[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\openwemacs]
@="&Edit with Emacs"
[HKEY_CLASSES_ROOT\*\shell\openwemacs\command]
@="C:\\Portable Software\\emacs-24.3\\bin\\emacsclientw.exe -n \"%1\""
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs]
@="Edit &with Emacs"
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs\command]
@="C:\\Portable Software\\emacs-24.3\\bin\\emacsclientw.exe --alternate-editor=\"C:\\Portable Software\\emacs-24.3\\bin\\runemacs.exe\" -n \"%1\""

I also set the ALTERNATE_EDITOR environment variable to C:\\path\\to\\runemacs.exe

At the beginning of my .emacs I have added the following code per this answer.

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

Adding that got rid of the "server already running" error when opening a second file, but it still opens in a new frame.

So what am I missing to get emacs to open new files in the existing frame?

Upvotes: 8

Views: 3872

Answers (3)

fiskhandlarn
fiskhandlarn

Reputation: 306

This worked for me.

Create C:\Program Files\runemacs.bat with the following contents:

@echo off

:: Set the path to where the Emacs binaries are
set binpath=C:\Program Files\emacs-26.1-x86_64\bin

:: If no arg is given edit this file
if "%~1"=="" (
  set filename="C:\Program Files\runemacs.bat"
) else (
  set filename="%~1"
)

:: Run Emacsclient
"%binpath%\emacsclientw.exe" --no-wait --alternate-editor="%binpath%\runemacs.exe" %filename%

And open all files via C:\Program Files\runemacs.bat instead of C:\Program Files\emacs-26.1-x86_64\bin\runemacs.exe.

Upvotes: 0

Ryan
Ryan

Reputation: 918

I accidentally figured this out while trying to fix synctex with SumatraPDF. It would appear that in addition to the ALTERNATE_EDITOR environment variable pointing to runemacs.exe, you must also create an EMACS_SERVER_FILE environment variable that points to the server file (mine was stored in the .emacs.d\server directory). Once I did that, files that I tell to Open with Emacs opened in the existing frame rather than creating their own.

Upvotes: 5

JSON
JSON

Reputation: 4606

It seems that emacsclient is failing to connect with the server and starting a new instance of emacs each time. You may need to unblock something in any software firewall you have installed.

Upvotes: -1

Related Questions