Sunday, April 09, 2006

 

Emacs as a Java IDE

A while back, I set out on an endeavor to replace my current Java IDE. The IDE I used certainly did the job but something just kept nagging at me that there had to be a better way. I felt as if the IDE was making me work the way the IDE designers felt I should work and not how I wanted too. After trying many IDE and editors built for programmers, I was a bit surprised with my final choice, Emacs. There are numerous Java IDEs to choose from so why would any self respecting Java developer want anything to do with an editor that is almost 30 years old? I found Emacs not as hard to learn as I expected and let me work the way I wanted to work. First, it is one of the most powerful text editors in existence and with a few add-ons it becomes a first class IDE. My requirements where simple, I wanted a tool that worked on a variety of platforms, be extensible, and would not require a tremendous investment in time to learn. I expected Emacs to meet the first two requirements but I was surprised to find it was quite easy to learn. Emacs has continued to evolve over the years. If you tried Emacs in the past and found it daunting, you will be surprised to find it a much friendlier place. Gone is the day where learning Emacs was a right of passage where only until you mastered the key combinations where you found worthy.

The choice a programmer makes for his/her tools is very personal. My intent is not to tell you that Emacs is the only true editor for all but to introduce you to something you may have not considered. If you view writing software as a craft then without a doubt your view of your editor is an important aspect in performing your craft. Just as a master painter would not use any old brush, a software craftsman shouldn't use any old editor. Surpassingly, with all the new and flashy development tools that have come about, Emacs is still a popular choice.

Emacs has been around for over a generation going all the way back to 1974 with Richard Stallman. Since then Emacs has been ported to many platforms and has been a popular choice amongst programmers of many types. Today there are two main versions of Emacs around today, GNU Emacs and XEmacs. The fork of GNU Emacs to XEmacs has somewhat become open source folk lore. The projects have different ideology and design philosophies but from a users perspective both are good implementations. In my opinion, XEmacs can be easier to setup but the latest version of GNU Emacs looks better. My suggestion is to give both a try and see which one you like better. Both versions will make excellent working environment for Java development. For practical purposes, in this article GNU Emacs and XEmacs can be interchanged.

Emacs has a few advantages over most modern IDEs. Probably the most profound is in resources used. The bloat has gotten out of hand with some Java tools. Typically, you have time to get a cup of coffee while your IDE starts up all the while gobs of memory are being sucked up. On my system, GNU Emacs takes up 10% of the memory of a popular Java IDE and starts in a matter of a few seconds. Likewise, something has to be said about the stability of a project that has been around for almost three decades. You can be assured that there is a stable code base and community. Emacs was not built with the sole purpose of being a Java IDE but the ultimate text editor. Since us programmers work with text as our raw material it has become a good fit. Chances are if you can think of a language, Emacs has a mode for it. This is a plus, after all, there is a life outside of Java right? Even if there is not for you, there are others things we work with. Emacs has some excellent tools for working with XML and HTML along with an interactive SQL. The biggest advantage Emacs has over modern Java IDE is the the flexibility of Emacs, but more on that later. However, one feature is noticeably missing. Since Emacs primary purpose is text it lacks a GUI Swing builder. However, I suspect that this will not be an issue for most front-end developers.

Emacs was the original do it all environment. Theoretically, you could spend your entire day in Emacs and never have to leave which was important before there were windowed environments. Besides just being an editor, it has a file manger, shell, version control client, web browser, email client, calendar, psychiatrist, games and the list goes on. Both GNU Emacs and XEmacs come with a plethora of tools. Not to mention the countless add-ons that have been developed, some even being commercial. The most significant add on for Java developers is the Java Development Environment for Emacs or JDEE. JDEE transforms Emacs from a good programmer's editor to a full-blown Java IDE. JDEE adds everything you expect in an IDE such as syntax highlighting, class browser, keyword completion, and debugger just to name a few.

There is a good chance that you already have Emacs on your system. It is component of almost every Unix system, including OS X. Nevertheless, you will probably want to get a fresh copy since the version on your system will likely be out of date. GNU Emacs especially has had some drastic improvements as of late. Go to either www.xemacs.org or www.gnu.org/software/emacs/ to download the latest source. Pretty much the standard ./configure and make install will do. Windows users have not been left out. There are precompiled native installs and those for Cygwin. Do not worry if you have problems with the installation, the Emacs communities have an plenty of people willing to help if a Google search doesn't solve your problem.

As I already mentioned, getting started with Emacs is much easier today. XEmacs was probably always a little easier but GNU Emacs has made great strides to catch up. It used to be you needed a cheat sheet of keyboard contortions to do anything in Emacs. Ultimately many people only bothered to memorize one sequence, C-x C-c to exit Emacs when they accidentally found themselves in it! It also did not help that out of the boxîthe "normal" keys did not work as one would expect. For example to scroll down one might expect that the down arrow would be used instead of C-n or if you press ìhomeî you expect to go to the beginning of the line. Today many of the key sequences, a newbie coming from a typical windowed application, will find today's Emacs work as they may expect. Additionally if you turn on CUA mode the familiar C-c to copy, C-z and such will work like most apps these days. Purists may balk at such "cheats" but if they can help you get used to Emacs quicker and less painfully then by all means use them. However, the whole idea of using a tool like Emacs is to become more efficient by using all of those key sequences. They might seem intimidating at first but it will not take long before your fingers get used to them. After a few hours you will start understanding that the key combinations are not just random selections but thought out patterns. It will not take long before you start realizing that once foreign key combinations actually make you more productive. There really is a method to the madness!

Emacs has tremendous customization capabilities. Just about anything you see or enter in Emacs can be customized. In the past you had to versed in Lisp or at least know how to copy and paste Lisp statements into your .emacs file, the startup file with your own customizations. Here is another area where GNU Emacs has made great progress in helping new users. A point and click environment called Custom has been added. Custom does not have every possible customization but it has plenty. The best part is that each option has a help paragraph along with the actual Lisp statement so you will learn as you go along. When you apply your changes, your customizations are appended to you .emacs file.

Do not get me wrong, even though Emacs is much easier today for the beginner, there still is some things the novice will have to adjust to. For users that are used to the common GUI environments like Windows, Gnome, KDE, or OS X some things in Emacs will seem quite strange. Remember that Emacs was built before there was any idea of GUI environments, heck the idea of seeing a page of the file you are working on was novel! So it is no surprise that Emacs has concepts that are unique. For example, you "kill" text and ìyankî it back or a window is a frame and frame is a window. It is not terribly hard to get used, just be prepared.

OK, so maybe you get the point that Emacs is a great text editor but what makes it so good as a Java development? Well that would be the Java Development Environment for Emacs or JDEE. If you are using GNU Emacs you will also need to visit jdee.sunsite.dk to get JDEE and its prerequisites. The installation is straightforward and just requires you to compile a couple of packages and put things in a place where Emacs can find them. A few changes to your .emacs file will also be needed. For XEmacs you can get the appropriate packages from the XEmacs site. Be forewarned that the XEmacs packages of JDEE might be behind what is current. The transformation that JDEE provides is really a wonder. The first thing you'll notice the first time you open a Java file in Emacs after JDEE is installed is that now your code is color coded and a new menu is available. Before you can start using the full power of JDEE, you will need to setup a few things up such as the location of your JDK and classpaths. JDEE uses an easy to use interface like Custom. Here again there are plenty of options, enough to make your scroll mouse finger tired.

Like any good IDE, Emacs with JDEE lets you do all of your development tasks from one place. With JDEE of course you can compile your classes right there and if you have an error in the compile it can take you right to the error. It goes one step further and will integrate with your builds in make or ant. There are also commands to execute your application or run it in a debugger. There is also a class browser. Arguably, the most important tool that has graced the development community recently is code completion. No longer do you have to memorize the contents of the thousands of classes we use on daily basis. It also saves us time and effort in typing. JDEE does a good job with this. Besides that, JDEE provides many convenient code generation utilities. It does more than just creating class stubs it has some great features once you are already editing a class. My favorite is the template to create get/set pairs but there are several such as auto Java doc creation or a create a "System.out.println" statement all with a single keystroke sequence. There are also has some neat shorts cuts like typing in ìifeî creates an if/else stub with parentheses and brackets in your preferred style.

Here is an example of tremendous value of Emacs, the community. Naturally, because most of the users of Emacs are programmers and like all programmers they are always finding better ways of improving things. One disappointment I initially had with Emacs/JDEE was there really wasn't much support for working with JSP's. The particular problem at the time was indenting for a particular nasty JSP. I did a quick Google search and I found Walter Higgins' site, www.xanadb.com/archive/emacs/20040622. Here he had similar frustrations as I did and provided some Lisp for insertion into my .emacs file. It was exactly what I needed. This type of thing I found is typical. Every time I ran into a problem or I tried to find a different way of doing things, it was easy to find the solution.

I found that it there was some initial culture shock when I first tried Emacs. However, it did not take long before my productivity matched what I had in my old IDE. What really sold me was when I encountered something I did not like it was easy to change and there were plenty of tools to make my work easier. I still consider myself a beginner and know that I still have plenty of room to grow with my new environment of choice. There is no possible way to do justice in short article about the power of Emacs. My suggestion to you is to give it a try. You may, as I did, find it as you tool of choice.

Comments:
The problem with emacs is that one does have to be versed in Lisp, or else one'll never be able to harness Emacs' true power.

The power of Emacs lies in his programming back end, Lisp. Emacs can be made to do almost anything, but the learning curve to do that is very steep, because not only does it involve learning Emacs-specific key bindings, but it involves learning a programming language on top of that.

Surely one might do just fine using basic functionality, but be honest: would You, as a programmer, ever feel comfortable using a tool that You do not fully understand?

Not everybody is prepared to invest the amount of time needed to fully master such a powerful yet complex tool like Emacs.
 
Not entirely sure that this is true; like any extensible application, learning lisp is the best way to make Emacs do things that it won't do already, but as it does do many things this is not necessary in general.

Having said that, I do know emacs lisp, so perhaps I am not the best person to comment.
 
How many people "fully understand" Eclipse, JBuilder, or whatever? Do I need to have written a plug-in for Eclipse before I am comfortable using it? No. That's ridiculous. Nor do I have to learn Lisp to be an expert Emacs user.
 
No, I don't feel particularly comfortable using a tool that doesn't do quite what I want and requires that I learn some new, opaque programming environment in order to bend it to my will.

That's why I'm moving away from Eclipse, back to emacs (except for symbolic debugging).
 
I love emacs very much. In fact, I'm using it right now to write this
comment. I've been using it for quite some time now, and I try to use
it as much as I can. However, when it comes to editing Java -- which
I do at work -- is still missing some essential features that other
IDEs have had for some time now.

At work I use IntelliJ. I remaped all of the keys to match emacs, and
it also has a not-that-bad emacs mode that adds support for kill
rings, but it still doesn't have the same ease of use in dealing with
a single source file that emacs has. However, when moving around a
large project, IntelliJ has some really cool features that I would
hope emacs and JDEE could match one day.

First, is the ability easily find any Java (or other) file in the
project, without having to write out the entire name, or knowing where
the file is on your drive. By the press of a key, a search box popes
up, there you can use wildcards or partial camel back to filter the
list of classes you're looking at. Once the list is short enough, you
can arrow (or Ctrl-n) down, and select the right class. So for
example: to find the class DatabaseConnectionPoolFactory, I can write
"Data" to get all the classes starting with that. Then
"DataCon"..."DataConP", and that would probably leave about 3 classes
on any given system. Now, I might want to do "DataConP*Test" to see
all the test classes I wrote for this class, and so on. This is
_extremely_ convenient, and once I got used to it, I became
increasingly frustrated with having to tab around my file system to
find classes.

Second, is ability to seamlessly (and mouselessly) complete method and
variable names. Now, this could be done better in IntelliJ as well,
but it is still much better then emacs. First of, I can find any
method or variable without touching the mouse. Which, unfortunately
(and surprisingly) is not as easy in emacs. In emacs, you either have
the choice of getting a static pop-up (and by static I mean that it
cannot be filtered further once it poped-up), which you can't navigate
using the keyboard, or continuously complete inline. The first choice
is useless for any real emacs user, since it entails touching the
mouse, and the second is useless for anything more then trivial cases.
It's very hard to figure out which of 50 "get" methods I need by
clicking through each one. Furthermore, in IntelliJ, once the method
has been chosen, you get a nice, unintrusive, pop-up that shows you
all the parameters, and also follows along as you fill them in. In
emacs, you have to choose ahead of time which overloaded signature you
want, and then it just inserts the types (without out names) into the
buffer. That is just not as good.

Third, the refactoring features in IntelliJ are superb. It happens to
me in many cases, where I need to come up with a name for a method or
variable, but I sometimes don't think of the best name right away. It
is so convenient that I can later rename it with zero hassle, and have
all references to it be renamed as well as comments. It just takes
the pressure off. The same goes for moving static variables around,
moving classes from package to package, etc.

There are others, but these are the main ones for me. I believe the
JDEE team are doing the best they can with the features emacs has to
offer, and they are definedly finding very creative solution to meet
its shortcomings. The day emacs offers, the same, or even similar
facilities, I would switch back in a heartbeat. Because, deep inside,
I want to be an emacs user. But, I'm not willing to compromise to do
it. I'm willing to work hard, learn complicated new features and
languages, write a little of my own lisp code, but I can't stand the
lack of good looking, easy to use graphical facilities missing in
emacs today.
 
Your belief in emacs is totally eccentric. I had similar feelings until I got out of my emacs cocoon and used Eclipse and IDEA earnestly. I would never go back to emacs for Java programming until emacs provided an integrated debugging environment, refactoring, integration with CVS and Perforce or any other source control system so I can tell from a glance if a file is old/locked/changed, display all my code formating options in a coherent and organized manner, allow me to filter error and warning messages, allow me to filter searches graphically, and the list goes on and on! Work needs to be done now, not in 10 years when someone decides to write some lisp code
 
Too bad the link to the JSP stuff is broken. That's what I've been looking for!
 
I have been trying a few weeks to get a decent java development environment setup for emacs. I see that eclipse sucks
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?