Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-click open .txt files in the same emacs frame (if one is currently open)

Tags:

emacs

windows

I would like to set emacs as the default editor for text files when I double-click open them in Windows. However I would like to open them asa buffer in the same running instance of emacs (i.e. frame) , if any. Right now the behaviour will open another instance of emacs (i.e. another emacs frame).

Does anyone know which specification in init.el would allow this behaviour?

like image 580
octi Avatar asked Oct 25 '11 16:10

octi


2 Answers

I've been using this guy's approach, which is an alternative to adding entries to the registry. I've reproduced his steps here:

  • Create the following batch file 'runemacsclientw.bat'.
::::::::::::::::::::::::::::::::::::::::::::::::::
:::
::: Emacsclient startup script runemacsclientw.bat
::: Robert Adesam, [email protected]
::: http://www.adesam.se/robert/
:::
::: N.B. Alot of this is reused from other Emacs
::: users that have published stuff on the
::: Internet. Thank you! :)
::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
:: Emacs binaries...
set binpath=c:\Program Files\emacs\bin
:: If no arg is given set filename to c:\
if "%~1"=="" (
  set filename=c:\
) else (
  set filename=%~1
)
:: Run Emacsclient
"%binpath%\emacsclientw.exe" --no-wait --alternate-editor="%binpath%\runemacs.exe" "%filename%"
  • Convert the bat file to exe with a bat to exe converter
    • Choose batch file.
    • Check 'invisible application'.
    • Under 'version information' select an icon (if you want to make it purdy)
    • Compile and exit.
  • Copy 'runemacsclientw.bat' to .../emacs/bin.
  • Associate file types with runemacsclientw.bat.
  • Add (server-start) to init file.
  • Change owner of ~/.emacs.d/server (in %appdata%) to the current user (it will default to local administrator). This removes the "unsafe directory ~/.emacs.d/server" warning.
like image 116
Ben Avatar answered Oct 30 '22 01:10

Ben


First have a quick look at emacsclient documentation.

Then in your init.el file start the emacsclient server by running:

(server-start)

Next we'll add some keys to the registry which gives an "Edit with Emacs" option in the context menu for all files.

Add the following keys:

[HKEY_CLASSES_ROOT*\shell\Emacs] @="Edit with Emacs"

[HKEY_CLASSES_ROOT*\shell\Emacs\command] @="c:\Program Files (x86)\emacs-23.2\bin\emacsclientw.exe" --no-wait --alternate-editor="c:/Program Files (x86)/emacs-23.2/bin/runemacs.exe" "%1"

The quotes are literal and should be included. You may have to play with the paths a bit so that they fit your environment.

As for the double-click behavior it's a matter of knowing which registry keys to add. If you know that they you should be able to generalize this answer to the behavior you want.

The program emacsclient will blast the file into a running copy of emacs (provided you ran 'server-start') or if there is not an existing copy it will run the command supplied with the "alternate-editor" option.

like image 32
Craig Wright Avatar answered Oct 29 '22 23:10

Craig Wright