Building & Installing GNU Emacs from Source in Windows Vista

The steps outlined in this entry describe A way that works for me to build and install GNU Emacs on Windows Vista from source code. For alternatives, please review the INSTALL file located in the nt folder of the emacs source tree. The approach I take is to enumerate the steps in a “do this, then this, then this…” fashion. I don’t go into the reasons and technical details of each step here, but I do provide a list of resources at the end where you can find the technical background behind the process I outline below.

I also should mention that the EmacsW32 guys distribute GNU Emacs in a Windows Installer. There you can choose the patched or unpatched versions of the package. The patched version provides better integration with Windows, but I use the unpatched version because I am an emacs purist. I recommend this package if you need to run emacs on Windows and don’t want to build it from the source code. What I really like about the EmacsW32 package is that it's kept at the latest emacs snapshot available.

Now, on with the show… The required prerequisites for this build are the following:

The following optional prerequisites are needed if you want to build emacs with image support:

And finally, the steps:

  1. Download and install a CVS client. I use the one that comes with Cygwin (see link above).
  2. Get the latest version of GNU Emacs from the CVS repository by running the following instruction on the Windows command line:

    cvs -z3 co emacs

    This command will create a working folder called emacs in the current directory.

  3. Download and install the latest version of the MinGW compiler and Make tool. There is a Windows installer provided at the link listed above. At a minimum you will need the core GCC and the Make tool.

  4. Add the bin folder of the MinGW installation to your path. On my machine I did that by running this statement on the command line:

    set PATH=C:\MinGW\bin;%PATH%
  5. Download and install the GNUWin32 core utilities from the link above.

  6. Add the bin folder of the GNUWin32 installation to your path. This can be done by running a command similar to this one:

    set PATH=%PATH%;C:\gnuwin32\bin
  7. The following steps are optional and only needed if you wish to compile emacs with image support:

    1. Download and install all the libraries listed in the optional requirements area above. All of these libraries are provided as a Windows installer. I install them in the same location where I installed the GNUWin32 core utilities.
    2. For the XPM library you will also need to download the source code, which comes in a zip compressed file.
    3. Unzip the XPM library source code.
    4. Find the file named simx.h and copy it to the include folder under your GNUWin32 installation.
  8. From the Windows command line, go into the emacs working folder you got from CVS and then go into the nt folder.

  9. In the nt folder, run the following command:

    cvs update -kb

    This will ensure that the newline characters in the files are correct for Windows.

  10. From the nt folder, issue the following command:

    configure.bat --no-debug --with-gcc

    ... or if you wish to compile emacs with image support issue the following command:

    configure.bat --cflags -IC:\gnuwin32\include --no-debug --with-gcc

    Where the argument after the -I parameter is the path to the header files for the image libraries you just installed. This will effectively prepare the build process for your system.

  11. Initially, we need to do a bootstrap build by running the following command:

    mingw32-make bootstrap

    This is necessary in order to build the emacs lisp interpreter and compile all the .el files. This may take a few minutes, so this is a good point for a break.

  12. Now you should run make alone again to make sure you caught everything:

  13. The last step in this process is to install the build you just finished. To do this run the following command:

    mingw32-make install INSTALL_DIR=C:/emacsfromcvs

    Where the INSTALL_DIR value is the location where you want to install emacs.

I hope you find this entry useful and concise. If you want more information on the topic of building GNU Emacs from source, please refer to these resources:

P.S.: I specifically omitted a step in the process described above. That's the step that calls minwg32-make info which builds the Info manuals. Reason being is that makeinfo.exe is crashing on my system. When I open makeinfo.exe in the VS2008 debugger I get the following error message: Unhandled exception at 0x00425425 in makeinfo.exe: 0xC0000005: Access violation reading location 0x00000000. Looks like a null pointer problem to me.

Ternary Operator in Python

C-like languages such as C# have a ternary operator which uses the symbols: ? and :, while the actual syntax looks something like this:

string s = myBoolValue ? 'True' : 'False';

Python doesn’t feature a ternary operator as in the C-like languages, but the underlying behavior is supported. You can achieve this in one of two ways:

  • s = 'True' if myBoolValue else 'False'
  • s = myBoolValue and 'True' or 'False'

The if form in number 1 above was introduced in Python 2.5 to address feature requests for a ternary conditional form (see PEP 308).

The form in number 2 above is a clever way to achieve the same effect using short circuiting. The conditional form in number 1 is the preferred form as the technique in number 2 is error-prone.

Update: 2009-09-25

With the ternary example number 2 above there is a small caveat that didn't occur to me until today. Consider the following code:

(a,b) = ('abc','xyz')
print True and a or b
print False and a or b

The above code is fine and behaves as expected. But what if the value of a turned out to be equal to a Python False? That would give us a wrong result... Consider the following code:

>>> (a,b) = ('', 'xyz')
>>> print True and a or b
>>> print False and a or b

See the conundrum? The real trick is to make sure that the value of a is never false. One way to fix this is to turn a and b into lists and the whole expression into a tuple and then read out the first element returned, like so:

>>> (a,b) = ('', 'xyz')
>>> print (True and [a] or [b])[0]

>>> print (False and [a] or [b])[0]

The first print statement now returns an empty string as expected.

Book Review: Expert Python Programming

When I find a technical book that looks interesting, I commit myself to reading one chapter of that book, but the occasion is rare when I actually finish the chapter. I usually go for the first chapter, but sometimes I pick one at random. I am looking for two things when I read it:

  1. teach me something new that I don't already know and
  2. get me excited about the book.

Expert Python Programming by Tarek Ziadé passed the test with flying colors. Not only did chapter one get me excited about the book, but it also taught me a couple of things I didn't know.

The book covers several topics that will make you a better Python programmer. Expert programmers are productive because they know how to use the language to express a solution in the least amount of code. Chapters two and three cover syntactic constructs and language features that will allow you to code like an expert pythonista. Chapter four focuses more on Python's naming conventions and coding style, which is an extremely important topic if you are distributing your code to a large audience. Chapter five and six are practical: they show the reader the best practices for writing packages and applications in Python, respectively.

Expert programmers are also productive because they are masters of leveraging tools for automating the more mundane aspects of software development. This book teaches you about the tools that will make you a more productive Python programmer. Chapter seven through eleven cover project lifecycle management tools such as build automation and distribution tools, source code control, testing, documentation and bug tracking. The focus is on how to leverage these tools in a Python project and it is very enlightening.

Chapters twelve and thirteen delve into performance and optimization in Python. Many treatments of the topic easily become fodder for a religious war, but Tarek's approach is well-grounded on two pillars: 1) Write code that works first and 2) Measure were the bottlenecks are before doing anything else. These chapters cover the tools you can use to find out where your performance problems are and the techniques you can use to get rid of the issues.

Chapter fourteen covers Design Patterns in Python, but it is not needed in this book. Others have written on the topic ad nauseum and Python syntax already supports a lot of the patterns. The canonical example of this are Python decorators, which allow the programmer to use the Decorator pattern by simply writing normal Python code, where most other imperative and object-oriented languages require you to write a complex hierarchy of cascading decorator objects.

The pièce de résistance of this tome is the fact that it outlines, in detail, the complete lifecycle of a professional software development project done in Python, using tools written in Python for serious pythonistas. The book is small when compared with most publications about Python, however, the insights that it provides are deep. Use it as a guide and a reference in your future Python projects and you are sure to become an Expert Python Programmer, as the title of the book implies.

In conclusion, Peter Norvig, who is the director of research at Google, has very eloquently written: ...the only sensible way to progress in any field is to get some practical experience first, and then acquire the theory necessary to understand what you did, and to allow you to do more. Expert Python Programming is the book that provides the necessary theory and practice to allow you to do more with Python.

The Blub Paradox of Screen Real Estate

Write your post here.

Paul Graham wrote and essay titled Beating the Averages in which he describes the concept of the Blub Paradox. The concept described in the essay is that programming languages vary greatly in power and the hypothetical language Blub falls smack dab in the middle of the spectrum.

If you are a Blub programmer and you are looking at less powerful languages, you know you are looking down the spectrum. The Blub programmer would say to a programmer of a lesser language: How can you do anything in language X… It doesn't even have feature Y? However, if you are a Blub programmer looking up to more powerful programming languages in the spectrum, all you can see are weird languages that look equivalent in power to Blub… at least on the surface.

The Sapir-Whorf Hypothesis

The concept expressed in the paradox is not new. Linguists Edward Sapir Benjamin Whorf developed the hypothesis

Many computer scientists and computational linguists believe that the hypothesis also applies to computer languages. Ken Iverson, the creator of APL, believed that the hypothesis applied to programming languages, and this was the central theme of his Turing Award lecture titled Notation as a Tool of Thought. This is also what Paul Graham is alluding to in his essay.

The Sinclair Basic

My first computer was a ZX Spectrum clone sold in Brazil circa 1985. Consequently, my first computer language was Sinclair BASIC. It had no concept of a function that languages such as C and Python depend on. How could anyone do anything in Sinclair BASIC without a fundamental feature such as functions?

What ends up happening is that you try to work around that limitation unconsciously. Sinclair BASIC provided the GO SUB construct with which you can effectively jump to a line of code that contains a subroutine, or a piece of code that may be called from several different locations within the program. The only problem with that type of construct is that it can lead to a severe case of spaghetti code in your program.

Multiple Monitors

My point really is that there is a direct correlation between my productivity and the amount of screen real estate I have at my disposal. There are those who find a multiple monitor setup hugely wasteful and are not able to see the benefits of such setup. I've been there myself.

But what really happens is that you work around the limited screen area unconsciously… much like what you do when working with a restrictive programming language. It takes a fraction of a second to Alt+Tab through a few applications to go from Emacs to a browser window to check my work. But over time, these small actions become a huge pain and break the flow of coding. This is the Blub Paradox of screen real estate.

My current setup at home consists of a 21" and a 19" ViewSonic LCDs with 1680x1050 and 1440x900 resolutions respectively. This setup is in dire need of an upgrade and I base that on the upgrade I recently got at work: a Dell 27" with 1920x1200 resolution and a NEC SyncMaster 21" in portrait mode with 1200x1600 resolution. All of a sudden my home setup is so puny.

In essence, once you have a X monitor setup, it is really easy to look down on the screen real estate spectrum and understand how lesser setups are inferior to X, but not so much when you are looking up the spectrum. It took me about a week of interacting with the new work setup to truly understand the need to upgrade at home. Now I just need to come up with the money for the upgrade.

Links for 2009-02-15

Here are some interesting links:

Fascinating interview with an adware developer
(this is from the Crypto-Gram Newsletter by Bruce Schneier). The take away from the interview for me is how easily exploitable the Windows API is… and how easy it is to hide malicious code from malware protection software. It's almost like Microsoft did it this way on purpose.
What do companies want?
The take away from this article for me is that it doesn't really matter so much what companies or people want, but what you, the programmer, want. Work on something you are passionate about and be your own number one user. But do put your idea out there for the world and take in the criticism to improve it. User feedback and criticism will tell you if you are working on the correct implementation of the idea. As Paul Graham has so eloquently articulated Figure out what the real problem is, and make sure you solve that.

My Take On CSS vs. Tables

Recently, there has been quite a bit of churn at Hacker News regarding the old debate about CSS versus Table-based layouts in web pages. What started the recent flame war seems to be this article by Ron Garret.

I want to make two points about this topic.

First, regarding some of the comments that accuse companies like Google, Yahoo and Microsoft as heavy offenders, who use table-based layouts every chance they get. In the real world your manager gives you a design or programming task and she says "The end-product needs to look like this… How it gets done is up to you. Two things: deliver on time and do not go over budget!" This is a typical interaction, and I base it on my experience as a Microsoft employee.

The developer or designer is going to fall back on something that she knows works: Table-based layout. Tinker with CSS for too long and she risks blowing the budget and the deadline. It's an easy choice to make, and I suspect that developers and designers in other companies are faced with the same pressures.

Second, go to and look at the source. This is the website of Dr. Jakob Nielsen. He is the foremost authority in web and software usability today. This website is one giant table-based layout, and it is quite usable and it applies the principles that Dr. Nielsen evangelizes.

In the battle between CSS and table-based layouts, pragmatism wins.

Python Decorators as Closures

Python decorators are a feature of the language that allows the programmer to alter the behavior of a function or class at runtime and without having to use techniques such as subclassing.

For example, let’s assume I need to trace the code path over several functions. This is easily accomplished with a decorator:

def myTrace(f):
    def trace_func():
        print "Entered", f.__name__
        print "Exiting", f.__name__
    return trace_func

def function1():
    print "Inside Function1"

def function2():
    print "Inside Function2"


The output of this script is:

Entered function1
Inside Function1
Entered function2
Inside Function2
Exiting function2
Exiting function1

Decorators in Python allow me to do more than just simple text substitution (C macros). Decorators effectively replaced one function for another in the example above. I can prove that there is actual substitution by adding the statement print function1.__name__ to the end of the above script. What is the current name of function1?

The more interesting thing that decorators allow me to do is to close over the parent scope of the decorator. Let’s look a this example:

def helloWorld(f):
    s = "World!"

    def new_func():
        print f(), s

    return new_func

def func():
    return "Hello"


The output of the script above is Hello World!, but the only call is to func() and that function does not print anything. This is because I substitute func() for the new_func() definition that I created inside the decorator. The new definition is, for all intents and purposes, a closure, because it encapsulates the context of the decorator and of func().

I can prove this theory: comment out the @helloWorld decorator before the func() definition and run the script again. What is the output now?

Screen Size Calculations

A friend of mine today asked me the following: "I am looking to buy a 52 inch TV with an aspect ratio of 16 by 9. I need to figure out the horizontal and vertical sizes in inches in order to know if it fits in my entertainment center. How do I do that?"

I told him to take a tape measure to each side of the TV and write down the measurements. That wasn't the answer he was looking for. He is looking at the TVs online. I told him he would need a little bit of knowledge about the Pythagorean Theorem and a little bit of trigonometry.

The Setup

This is what my friend is looking at:


When we take all the shiny parts away, we end up with the following setup for our problem:


What We Know

Given the problem statement, we know that side c is 52 inches, angle C is 90 degrees, and that side b is 16 units and side a is 9 units, which is screen aspect ratio. We also know that in order to find the length of sides a and b in inches we need at least two values:

  • The value of one of the sides, which we know c to be 52 inches
  • The value of one of the angles of the triangle, which we can get to by using the ratio 16:9

Calculating The Angle

We can calculate the angle A by using trigonometry functions. We can use the Tangent function on angle A, which is to say that the Tangent of A is equal to a divided by b. From the screen ratio we know that a and b are 9 and 16 respectively, therefore Tan(A) = a/b which is 0.5625 in radians.

We also know from trigonometry class that the value of angle A is the inverse of the Tangent of A, or the ArcTangent of the Tangent of A. Therefore, ATan(0.5625) = 29.35 in degrees. I cheated and used a calculator to get to the value of A.

Now that we have both the value of side c and of angle A, we can calculate the values of the sides a and b.

Calculating The Sides

Let us focus on side a first. We know that the Sine of angle A is the quotient of a divided by c, or Sin(A) = a/c. We can rewrite the equation like a = Sin(A)c_ in order to find the value of side a. So _a = Sin(29.35)52 which is 25.49.

Now that we have the value of a we can find b by using the Tangent of A again. When we calculate the angle A we used the Tangent function which is equivalent to a divided by b, or Tan(A) = a/b. In order to find the value of side b, we can rewrite this equation to b = a/Tan(A). So b = 25.49/0.5625 which is 45.32.

Hence, the value of side a is 25.49 inches and the value of side b is 45.32 inches. But are we sure about that?


We can verify this results by using the Pythagorean Theorem. We know that the square of the hypotenuse is equal to the sum of the squares of the sides, or c2 = a2 + b2. Let us plug in the numbers that we have for sides a and b to see what our value for c is.

Hypotenuse Formula

We have a and b equal to 25.49 and 45.32 respectively. The value of a squared is 649.9228. The value of b squared is 2054.0771. The sum of the squares of a and b is 2704. The square root of 2704 is 52.

I used a python session in idle to do all this math and this is what it looks like:

>>> import math
>>> A = math.degrees(math.atan(9.0/16.0))
>>> A
>>> a = math.sin(math.atan(9.0/16.0)) * 52
>>> a
>>> b = a/(9.0/16.0)
>>> b
>>> c = math.sqrt(a**2 + b**2)
>>> c

Please let me know if you see any flaws in my logic.

UI Prototyping with Emacs

A while back Jeff Atwood wrote about prototyping a web site that he was working on. It turned out that he was talking about his new venture, Stack Overflow. There are several approaches to UI prototyping, but one of the best regarded is Paper Prototyping. Both Jeff and Jakob Nielsen are proponents of the idea.

There are also many other techniques and tools for UI prototyping and Jeff mentions a one of them in his article: Powerpoint or Keynote. Recently, a new breed of UI mockup tool has been getting a lot of attention. This is the Balsamiq Mockup tool and it looks really great. I want to play with it first and then I'll write about it here.

Today, though, I'd like to talk about and propose a different approach to User Interface Prototyping:

UI Prototyping in Emacs Artist Mode

UI Prototyping in Emacs Artist Mode

Artist mode is a minor Emacs mode that turns an emacs buffer into a canvas you can draw all sorts of geometrical shapes on. You can use the mouse or keyboard and the shapes are drawn using the |, \, /, -, +, and x characters.

Artist mode ships with the latest version of Emacs and you can activate it by issuing the M-x artist-mode sequence in the minibuffer. I created a screen mockup of this site in about 15 minutes and you can view it by clicking the image on the right or by downloading the text file at the bottom.

Once artist mode is active within your buffer, you can use the middle mouse button (or the wheel button is most modern mice) to view the artist context menu. From there you can pick the action you want to perform: draw a line, rectangle, elipse, fill an area, etc. To jump out of artist mode, all you have to do is execute M-x artist-mode-off in the minibuffer.

Artist mode has many other uses for programmers as is illustrated in this great screencast by Seong-Kook Shin. The creator of artist mode is Tomas Abrahamsson.

Let me know how you are using artist mode in your projects. The text file with the UI mockup of this blog can be dowloaded here.