Emacs/Visual Studio installation notes

John Smathers 6/2/2000


Last spring I started a new job and switched over from the glory of UNIX to the dark side of the Microsoft Development Studio / Visual C++ programming environment. Lots of UNIX tools have been ported to the Microsoft platform, and installing them provides a pretty decent compromise. These notes describe how I installed Emacs and the drivers required to make it work with Visual C++.




1) Install Cygwin tools to get ediff (for merges). Also provides many other UNIX commands such as grep and tar. Run setup.exe to install the package; I installed it in C:\cygwin.

2) Install Emacs:
Untar the Emacs binary distribution into C:\emacs. You can grab the latest version from gnu.org (see the URL in the References section). The XEmacs Windows port is not as far along and therefore doesn't work so great in Win-NT, it's probably not worth bothering with.

3) Install the gnuserv package (including gnuclientw.exe) so that emacs can talk to Visual Studio. (You can grab a version that should work from the Gnu Emacs-NT page in the References section).

4) Add emacs, the Cygwin tools and gnuclientw.exe to the path: Select Control-Panel->System->Environment. Define the a User Variable for Path, it will be appended to the system-wide Path variable. For example:

Variable: Path
Value:    C:\cygwin\bin;C:\gnu\gnuserv-2.1p1\gnuserv;C:\emacs\bin

5) Add the gnuserv package to the load-path in the .emacs (in Win-NT this is C:\_emacs) file:

(setq load-path (cons (expand-file-name "c:/gnu/gnuserv-2.1p1/gnuserv/") load-path))
(require 'gnuserv)

6) Copy the VisEmacs.dll file to the C:\emacs\Add-In directory.

7) Add the VisEmacs.dll file to the system's registry:

regsvr32 VisEmacs.dll
If you have problems getting the VisEmacs.dll file to register you may need to build the .dll file from source. Download the source, open it as a project in Visual Studio and compile.

8) Open Visual Studio. Select "Tools->Customize...->Add-Ins and Macro Files". Browse over to the c:\emacs\Add-In directory. Select VisEmacs.dll. (VisEmacs.dll is supposed to appear automatically; you may have to type it in yourself).


.emacs is not a valid file name in Windows, so your customizations go in the _emacs file. Typically this goes at the top of your C: directory.

I like emacs because of the colors. Add this to your _emacs file to turn them on:

(global-font-lock-mode 1)

;; The default max file size is 256000 bytes, 
;; but some of our source files are larger
(setq font-lock-maximum-size 512000) 

You can have emacs start in your own directory by default (this isn't really helpful for VisEmacs, but it's something I like anyhow).

(cd "C:/mydocuments/")

If you edit files in Visual C++, then return to your emacs window, this will cause the file to be automatically reloaded in emacs. (Alternatively, you can type "M-x revert-buffer" each time you return to the emacs window).

;; Automatically reload files after they've been modified 
;; (typically in Visual C++)
(global-auto-revert-mode 1)
It's nice to have Visual C++ behave the same way, so you don't get the annoying "This file has been modified outside of the source editor. Do you want to reload it?" message. Choose Tools->Options->Editor and select "Automatic reload of externally modified files".

Goto-line is a keyboard shortcut I use a lot.

;; Map ESC-g for goto-line (like with XEmacs)
(global-set-key "\M-g" 'goto-line)    

Electric Buffers makes it a loss less painful to change buffers: your cursor immediately goes to the correct window when you go C-x C-b.

;; Use the "electric-buffer-list" instead of "buffer-list"
;; so that the cursor automatically switches to the other window
(global-set-key "\C-x\C-b" 'electric-buffer-list)

Highlighting regions is turned off by default. Very lame. Here's how you turn it back on:

;; Highlight regions for cutting or copying
(setq transient-mark-mode t)

Highlighting matching parens is very useful when digging through someone else's spaghetti code:

(setq show-paren-mode t)

I made a bunch of changes to my c-style so that the C++ editing style looks pretty much like the style that everyone else using the Visual C++ editor is producing. (4 space indents is the main thing). My code for this looks pretty heinous, so I won't include it here. Basically I started with the .emacs example listed when you go "M-x info", then select "cc-mode".

Source Control

You can have Emacs talk to Microsoft Source Safe if that is what you are using for source control. I use the source-safe.el package, available from the GNU web site. They hide it pretty well, but the last time I checked you could get it at: http://www.gnu.org/software/emacs/windows/ntemacs/contrib/source-safe.el.

In case they've taken it down, you also might find it at http://xemacs.seanm.ca/lisp/source-safe.el.

Put the source-safe.el file in your site-lisp directory. You'll need to define the SSDIR environmental variable to specify where the main source-safe repository is located. "\\Servername\Project", for example. Then you can add the source-safe hooks to your _emacs file:

;; Specify where your source-safe executable is located
(setq ss-program 
      "C:\\Program Files\\Microsoft Visual Studio\\Common\\VSS\\win32\\SS.exe")

;; Specify where your working directories are.
;; The slashes need to be escaped a couple of times; the drive name
;; needs to start with a '^' so that the REGEXP stuff works right
;; (the comments in source-safe.el explain this).
(setq ss-project-dirs 
      '(("^C:\\\\mw\\\\src\\\\" . "$/src/")
      ("^C:\\\\mw\\\\docs\\\\" . "$/docs/")
There are a whole bunch of source-safe commands that you'll probably want to load. See the comments in source-safe.el for a complete list; here are a couple of examples:
(autoload 'ss-get "source-safe"
  "Get the latest version of the file currently being visited." t)

(autoload 'ss-checkout "source-safe"
  "Check out the currently visited file so you can edit it." t)

If you work with databases, another handy module is the sql.el package. You can use this to connect directly to a database, like running sqlplus with Oracle, but without opening a shell first. To connect to a PostgreSQL database, go "M-x sql-postgres", for Oracle go "M-x sql-oracle" and for a MySQL go "M-x sql-mysql". For each of these modes there is an options variable. For example:

(setq sql-postgres-options '("-Uacs" "-P" "pager=off"))

For indenting sql files, I use the sql-indent.el package, available at http://www.geocities.com/kensanata/elisp/sql-indent.el.txt. Copy it to your site-lisp directory and add the following to the _emacs file:

;; load the indent styles for editing sql scripts
(eval-after-load "sql"
  '(load-library "sql-indent"))

;; Set the indent size to something more reasonable than the default
(setq sql-indent-offset 4)

Other packages

There are lots of other Emacs goodies at http://www.gnusoftware.com/Emacs/Lisp/. (The toggle-source.el package is pretty handy, for example).

Exercises for the reader

There are ways you can customize emacs so you can do your compiling within emacs. I haven't tried it out yet, let me know if you get it working.