19

I am particularly interested in the full Windows experience including:

  • Automatically starting a server if one is not already started
  • How to call emacs so that it utilizes the server (e.g. from command line or third party tools)
  • Context menu support (e.g. right-click on a file and allow it to be sent to emacs for viewing and editing)

Is such support available out of the box anywhere?

cristobalito
  • 315
  • 4
  • 9

3 Answers3

22

Automatically starting a server if one is not already started

This should be the same for any Emacs build. Add this snippet to your .emacs or .emacs.d/init.el.

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

And then just start emacs by running the runemacs.exe executable that comes with the build.

How to call emacs so that it utilizes the server (e.g. from command line or third party tools)

Emacs' Windows build comes with an emacsclientw.exe executable in the bin/ directory. Use the full path to that executable as your editor of choice on any third party tools you need to configure.

Context menu support (e.g. right-click on a file and allow it to be sent to emacs for viewing and editing)

  1. Right click on a file, select Open With -> Select Default Program.
  2. In the window that pops up, navigate to your emacsclientw.exe executable mentioned above and choose it.
  3. If you don't want Emacs to be the default, do steps 1 and 2 again, choosing a different program as the default.

emacsclientw.exe will now always be an option in the context menu.

cristobalito
  • 315
  • 4
  • 9
Malabarba
  • 22,878
  • 6
  • 78
  • 163
  • Thanks - great point about the server, I wasn't thinking. – cristobalito Sep 24 '14 at 19:12
  • Regarding the emacsclient.exe, shouldn't that be emacsclientw.exe? Also, I thought you had to run this with the -a flag to specify an alternative editor if the server wasn't running. – cristobalito Sep 24 '14 at 19:17
  • In order to get the server to work properly with my configuration, I also had to add an environment variable `EMACS_SERVER_FILE` that pointed to my `.emacs.d\server` directory. – Ryan Sep 30 '14 at 00:10
  • @Ryan I was able to get this working without the need for the environment variable - just needed to ensure that the server was running. – cristobalito Dec 30 '14 at 15:21
  • Hmm, for me evaluating `(server-running-p)` returns `:other` on Windows (even after trying setting that `EMACS_SERVER_FILE` env var. – Kaushal Modi Nov 05 '15 at 04:06
  • **Update:** Got it working after `(setq server-use-tcp t) (defun server-ensure-safe-dir (dir) "Noop" t)` and setting `EMACS_SERVER_FILE` to point NOT to the directory but to the actual `server` file (For me that was `PATH\TO\.emacs.d\server\server`). – Kaushal Modi Nov 05 '15 at 04:27
6

Automatically starting a server if one is not already started

I highly recommend setting an environment variable for ALTERNATE_EDITOR to runemacs.exe. This way you can always call emacsclient even if there is no server. Just add (server-start) to your init file. When a server does not exist emacsclient calls the ALTERNATE_EDITOR which is emacs in our case. Since emace starts up a server in the init file, any new calls to emacsclient will be instantaneous.

How to call emacs so that it utilizes the server (e.g. from command line or third party tools)

Use emacsclientw.exe -c -n "your_file" to open your_file . The -c argument creates a new graphical frame and -n allows you to close the frame without losing server state. You can even call it without a filename to open the *Scratch* buffer. Works from a desktop shortcut also. Using emacsclient.exe (note the missing w) will pop up a command prompt for a few seconds everytime you call it.

Adding to context menu

Emacswiki has a nice page on adding Emacs to the context menu here. It involves editing the registry node HKEY_CLASSES_ROOT\* to add an action to open with emacs.

Vamsi
  • 3,916
  • 22
  • 35
  • The option `-c` tells Emacs explicitly to create a new instance/window, which is not what the OP wanted. Using `emacsclientw.exe` without options works better. – Christian Davén Apr 15 '20 at 05:26
5

Others have answered the server part. Here is what I do, to get UNIX / GNU/Linux-like utilities such as grep and diff:

  1. Install Cygwin (one-time operation).
  2. Load cygwin-mount.el.
  3. Load setup-cygwin.el.

However, be aware that there are different versions of Cygwin. I use an older version (dunno which one - hard to tell, AFAICT). Dunno whether you will have problems with recent versions.

See also:

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Note: Do not use the cygwin EMACS. Install the GNU build from http://ftp.gnu.org/gnu/emacs/windows/ – M Smith Sep 24 '14 at 18:30
  • I do not recommend to install Cygwin, as there can be undesirable side-effects (like breaking other gnu windows ports like emacs, gcc, ...), for just having some gnu tools. Instead install [GnuWin32](http://gnuwin32.sourceforge.net) ports. You can install the whole package or selected tools of your choice. – Seki Sep 24 '14 at 23:07
  • @Seki - Yes, Eli Zaretskii says the same thing as you. All I can say is that I have had no problem (and I have serious doubts wrt your claim of it **breaking Emacs**). But (a) I am using an old Cygwin release and (b) I don't use most of the UNIX / GNU Linux utilities provided by Cygwin. – Drew Sep 25 '14 at 01:27
  • @drew: perhaps excessive for talking about "breaking" emacs, "causing weird behavior" would have been better. For example Cygwin Make will brake the MinGW toolchain if you do not carefully check your path for Cygwin tools not being taken into account or taken by default if no other exist. It is not limited to Make, many other tools cannot mix and sometimes it is subtle to point the problem. So in short, either you have a native port of Emacs and it is better to have native GNU tools, or you can use the whole Cygwin distribution including its emacs. – Seki Sep 25 '14 at 08:36
  • @Seki: So it seems that your main point is that Cygwin and MingGW do not play well together. That's not surprising to me. (FWIW, I have nothing against using MingGW instead of Cygwin. For me it was simple to install Cygwin, and I don't use enough of it to have encountered problems.) – Drew Sep 25 '14 at 15:11
  • @Drew: No, actually my point was that installing Cygwin for just having some Gnu command line tools is overkill when simpler and smaller solutions exist. GnuWin32 binaries are more "isolated" (you can just install one tool) when you just need, say, `grep` or `wget` while Cygwin must install a package of tools for at first initialize the Cygwin ecosystem. But Cygwin is useful, I do use it for specific tasks. – Seki Sep 26 '14 at 07:44
  • This answers the question in the original, overly-general, title, but does not help with the actual question. – Gilles 'SO- stop being evil' Sep 26 '14 at 23:46