Emacs

This is intended mostly for my own use. I need to keep track of how I build my Emacs, and how I configure it. However I'm always willing to share, so if this can help you in any way, feel free to use it!

Links

Gnu Emacs homepage
Gnu NT-Emacs homepage (the native Win32 port)
Emacs manual
Emacs Wiki
Emacs history time line
A font suggested as good programming font on the emacs nt maillist.
A win32 installation guide. Probably more up to date than mine.

Getting Emacs

The information here is a bit old. I've moved back to using MSVC to build my Emacs. That build well everytime. Someday I might update the build instructions for cygwin.
This is geared towards Win32. Most decent operating systems include an Emacs in the distribution, but for Win32 you need to get your own. Two choices, build your own or download a prebuilt binary:

Build your own

Get sources from Gnu (or mirrors, ftp.sunet.se will be used here). Official source packages are available here. Or, download these CVS snapshots Emacs 2003-03-13 and Gnulib 2003-03-13. I will be using the CVS snapshots in my example. NT-Emacs builds rather easily with Microsoft Visual C++, but I will be describing how it can be done with Cygwin and a mingw gnumake. How to build is also described on the Emacs Wiki.

1. Unpack and patch the CVS snapshots.

In a cygwin shell window, do this:

cd /cygdrive/c/tmp //Or another directory where you can build.
tar xvfj emacs_cvs_2004_03_13.tar.bz2
tar xvfj gnulib_cvs_2004_03_13.tar.bz2
cd emacs
cp ../gnulib/regex.* src/.
patch -p0 < ../patch.txt//Get patch.txt here. Path depends on where you saved it.

2. Configure

In a Win32 command window (cmd.exe), do this:

c:
cd \tmp\emacs\nt
.\configure

3. Build

In a Win32 command window (cmd.exe), do this:

c:\mingw\bin\mingw32-make bootstrap
c:\mingw\bin\mingw32-make //I don't think this is needed, but just in case...

4. Install

In a Win32 command window (cmd.exe), do this:

c:\mingw\bin\mingw32-make install INSTALL_DIR="c:/gnu/emacs_cvs"

Done! I didn't show how to compile Emacs with support for different picture formats. Normally you don't need picture support in Emacs.

Download a prebuilt binary

On your favorite Gnu mirror you should be able to find this.

Start using Emacs

There are a number of things that needs to be configured, and additional installs to be made, but for now, let's start the newly installed Emacs just to get have a look.

If you're really new to Emacs, the built in tutorial is the right thing to start now. You start the built in tutorial by pressing the "Control" key at the same time as the "h" key (press Control first, then tap h). Then release all keys and press the "t" key. This is also known as "C-h t".

Basic configuration

1. Remove toolbar

Type "C-x C-f", then "C-a C-k" and "~/.emacs < ret >
" This will open up your Emacs configuration file. Now add these lines:

;; Turn off tool bar (tool-bar-mode -1)
Type "C-x s" to save the configuration file. Then restart you Emacs ("C-x C-c" to exit).Starting up you should see this:

Much more area available for real work! A good start!

2. Windows cut and paste

Since I spent a lot of my time on Windows, and since some of the programs I must use doesn't have that much configuration possibilities, I tend to configure Emacs to behave like other Windows programs. Not necessarily a good idea, but practical. Let's add Windows style cut and paste (C-c C-v C-x). The package to do this is now in the standard Emacs, but for reference look at http://www.cua.dk. Add this to your .emacs:

;; Turn on CUA mode. Windows style cut and paste. (cua-mode t)
Now place the point just after the ")" parenthesis and type "C-x C-e". This will evaluate just that configuration so that you don't have to restart Emacs. Now, try it out. Hold "Shift" down while moving the cursor with the arrow keys. When you have something marked, type "C-c" to copy and try "C-v" to paste. Since "C-x" is now configured to cut the marked text (region), you will sometimes do a cut that was meant to be the start of another command. In those cases, remember that "C-z" or "C-_" will undo the change.

3. Interactive do (ido)

A plug in that like CUA was developed by Kim F. Storm is the interactive do package. This is very helpful for switching between different buffers and finding what files to open.

;; Turn on IDO mode. Interactive do. (ido-mode t) (setq ido-max-window-height 1) ; Limit IDO to 1 line of mini buffer. (setq inhibit-startup-message t) ; For some reason, IDO doesn't work until after the splash. Turn splash off.
Try it out by doing "C-x C-f" and write "c:/" (no need to erase the path in the minibuffer). Now type "dow". You get something like:

Find file: c:/dow{Download/ | WINDOWS/}

You will be presented with names that match what you typed. Pressing "Tab" will give you the completion closest to the point. Use the arrow-keys to select among the matches. Also, test to press "C-d". This will take you into dired, where you can do a number of file operations (including opening a file or directory by pressing "Ret").

4. Highlighting

Emacs knows how to highlight a number of different syntaxes (E.g. C, lisp and perl). Let's turn it on:

;; Turn on font locking (global-font-lock-mode t) (setq font-lock-maximum-decoration t)
Evaluate both with "C-x C-e" and you shall get colors on your comments:

5. Colors and fonts

While we're working with colors and fonts, let's add this:

;; Modify the default values for new frames.
(setq default-frame-alist
(append '(
(background-color . "lemon chiffon")
(foreground-color . "dodgerblue4")
(width . 162)
(height . 69)
(font . "6x13")
) default-frame-alist
))

;; Also, let's maximize the frame if we're on Windows.
(if (eq system-type 'windows-nt)
(add-hook 'window-setup-hook 'w32-maximize-frame))

;; Interactive function for maximize.
(defun w32-maximize-frame ()
"Maximize the current frame"
(interactive)
(w32-send-sys-command 61488))

;; Split the window to get two frames.
(split-window-horizontally)

;; No scroll bars
(set-scroll-bar-mode nil)

Now, that did some more than just fonts and colors. It also removed scrollbars, created two frames and maximized on start. You should now get something like this:

6. C-mode

cc-mode can be used to highlight and indent c-style languages. There are lot's of configuration, this is mine:

;; Customizations for C Mode.
(defun my-c-mode-hook ()
;; other customizations
(c-set-offset 'substatement-open 0)
(c-set-offset 'brace-list-open 0)
(c-set-offset 'knr-argdecl-intro 2)

;switch/case: make each case line indent from switch
(c-set-offset 'case-label '+)

;make the ENTER key indent next line properly
(local-set-key "\C-m" 'newline-and-indent)

;syntax-highlight aggressively
(setq font-lock-support-mode 'lazy-lock-mode)
(setq lazy-lock-defer-contextually t)
(setq lazy-lock-defer-time 0)

;make a #define be left-aligned
(setq c-electric-pound-behavior (quote (alignleft)))

;do not impose restriction that all lines not top-level be indented at least
;1 (was imposed by gnu style by default)
(setq c-label-minimum-indentation 0)

(setq tab-width 2
;; this will make sure spaces are used instead of tabs
indent-tabs-mode nil)
;; we like hungry-delete
(c-toggle-hungry-state 1)
;; keybindings for all supported languages. We can put these in
;; c-mode-base-map because c-mode-map, c++-mode-map, objc-mode-map,
;; java-mode-map, idl-mode-map, and pike-mode-map inherit from it.
(define-key c-mode-base-map "\C-m" 'c-context-line-break)
)

(add-hook 'c-mode-hook 'my-c-mode-hook)

;; Have a imenu for C-mode!
(add-hook 'c-mode-hook 'imenu-add-menubar-index)

7. Auto revert

By turning this on, files that change on disk, and have not changed in Emacs are reread automatically.

;; Auto revert. Reread a changed file from disk.
(global-auto-revert-mode)

8. Show columns

Sometimes it's good to find out what column point is at.

;; Show columns.
(custom-set-variables '(column-number-mode t))

9. Done for now

Here's the resulting .emacs. More to come!