Saturday, August 8, 2009

Win32 one instance setup for emacs - simple setup for running one instance of emacs on windows.

Emacs is a powerful text editor. Some might consider Emacs one of the most powerful editors in computing history. It is highly configurable, ported to many different systems and once you get familiar with how to use it, you can be very productive. Emacs is also useful for working with many different programming languages. Emacs is built on top of a Lisp dialect called Emacs Lisp. Other Lisp dialects benefit from the the built-in Lisp syntax highlighting. That is just one example of a non-mainstream language that has support in Emacs.

Emacs is especially popular in an Unix/Linux environment. Because Emacs is GNU and opensource, a free version is available for the Windows platform as well. One issue that new users to emacs may encounter is getting the editor to work like other Windows editors. This small document is how I configure emacs to only allow one instance to run on Windows. This function is not configured by default. This is not about the best setup for Emacs or a robust emacs configuration file, merely a setup for allowing you to have only one instance of emacs running while you open multiple files.

I downloaded the emacs windows binary, version emacs-23.1 and unzipped the archive file to my local drive. I didn't use an installer. I simply unzipped the file and set the emacs bin directory in the system PATH environment variable.

There are two executables that we are most concerned about , runemacs.exe and emacsclientw.exe. The emacsclient is the socket based client application that checks for a running emacsserver instance. If one is available then open the file in a running instance of emacs.

There are two other files that we are going to add to the bin directory. Add the files vb_emacs_start.vbs and run_emacs.bat to the bin directory. Note: the filename and path of these files really aren't important. I used the emacs bin directory for convience. The vb_emacs_start.vbs is the visual basic script for detecting if an instance of emacs is already running. If one is running than run emacsclientw. If one is not running then run the default runemacs.exe.

Cscript.exe is a tool that allows you to run a script file at the command line. You can run VBScript or JScript. The full script to check for emacs or launch a new instance of emacs is shown in the screenshot above.

For convience, I normally include a wrapper batch script to invoke cscript. Here is the content in run_emacs.bat:
@echo off
cscript "F:\downloads\emacs-23.1-bin-i386\emacs-23.1\bin\vb_emacs_start.vbs" %1 %2 %3 %4


Download and review the scripts

run_emacs.bat.txt - simple emacs wrapper script
vb_emacs_start.vbs.txt - vbscript for checking/launching emacs
emacs_conf.emacs.txt - bare bones, emacs configuration. Notice the emacs server line at the bottom

-- Ron Paul 2012

No comments: