Open file in existing emacs frame (Windows)
Asked Answered
H

3

8

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?

Hod answered 25/3, 2013 at 1:12 Comment(0)
H
5

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.

Hod answered 5/5, 2014 at 23:14 Comment(2)
I'm interested in doing this as well. How did you set the environment variables?Godoy
@Calculemus here's a source for how to set environment variables on Windows docs.oracle.com/en/database/oracle/r-enterprise/1.5.1/oread/…Hod
N
0

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.

Nancienancy answered 12/4, 2019 at 6:32 Comment(0)
M
-1

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.

Musjid answered 25/3, 2013 at 3:21 Comment(6)
Any ideas what I would need to unblock?Hod
It depends on the firewall software, but most firewalls will block Emacs from creating a listening socket, so you need to add Emacs to a whitelist (or remove it from the blacklist it got put on when it first got blocked). Some may also block emacsclient from making connections as a client.Musjid
I just created a firewall rule for Windows Firewall to allow connections to runemacs.exe. No change in opening behavior.Hod
emacs.exe is what you need to allow connections to, runemacs.exe exists only to start emacs.exe without a command prompt window.Musjid
I just changed to rule to allow connections to emacs.exe, but it's still opening new files in new windows.Hod
I deleted the .emacs.d\server\server file and restarted Emacs. Then it suddenly worked!Pontifical

© 2022 - 2024 — McMap. All rights reserved.