PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads

  Valid HTML 4.01 Transitional

   Download Kubuntu Today


   The Power Of KDE + Ubuntu

PGTS Elephantine Blog

Thread: Open Source Software

Author Image Gerry Patterson. The world's most humble blogger
Some folks are born, silver spoon in hand ... Creedence Clearwater

Emacs Nuclear Submarine

Chronogical Blog Entries:

Date: Fri, 25 Jul 2008 23:55:13 +1000

Back in the days of Windows 3.1, I was using Coherent, a Unix package marketed by Mark Williams. I purchased it by mail order from the USA. And at the time, I thought it was the best $99 (US) that I had ever spent. My preferred editor was "vi" of course. And even though it was possible to get a version of Emacs working on Coherent, I had long ago decided that Emacs was too resource hungry and too complex for my liking, and I did not want to burden my little 386 computer with such a load.

Eventually I switched to FreeBSD. This was about the time the rest of the world was switching to Windows 95. At the time, I have to admit to being extremely unimpressed with Windows 95. In fact if it had have been shipped several years earlier, Windows 3.1, would have seemed like a marvellous improvement, when it was shipped.

Throughout all of this my preferred editor was "vi". Although I had encountered Emacs (and on one occasion micro-emacs).

But then I started to experiment with Linux. And I was persuaded to change to "vim", because of the "syntax highlighting", that came bundled with vim distributions.

Of course as I was gradually wooed from BSD to Linux, I started to use vim more often. And now that I have converted to mostly Ubuntu, it is Vim all the way Vim is a great editor and the GUI version, Gvim, will run on most systems with X11 and on Microsoft Windows.

Every now and then I venture into the deep dark undergrowth of Emacs, just to see how the other half lives. And if vim is like a hardy Seacat ferry speedily delivering her passengers across the Bass Straight. Emacs is more like a nuclear-powered submarine cruising in the unfathomable depths. Veteran Emacs submariners can stay submerged in Emacs for months or even years. You don't have to return to the surface. You can read your emails, run scripts, browse the Internet, write your thesis, start the Free Software Foundation etc and remain in the deepest depths of Emacs all the time.

Furthermore the complexity of Emacs probably does rival the technical specs for a nuclear sub. With the expansion of GNU, it seems these days that Emacs is ubiquitous. Many features have been incorporated into other programs (for example the auto-completion components of bash and the default settings of many readline libraries) On a few occasions I have had to use Emacs. And whenever I do, I have found the sheer size and complexity of the package daunting.

Most of my experience with Emacs has been in writing C programs (quite a long time ago) and writing text files.

Just recently I tried to use Emacs to write some perl code. As a hardened vi veteran, one of the hardest things to get used to is not pressing the <ESC> key (just to make sure that I am in the command mode).

I had already figured out that I need to put the following lisp code in my ~/.emacs file:

;; Turn syntax highlighting on
(global-font-lock-mode t)

And so I opened a perl script that I had been working on with vi, and here is what I saw:
while (<TMP>){
        if (/^From /) {
                my $foo = $';
                if ($foo eq "bar") {
                        my $bar = "barbarcan";

Well that's not exactly what I saw. I have modified the script for the purpose of this blog, and I have used Gvim to dump the HTML (since I don't know how to do that in Emacs). But you get the general idea. It seemed that the Emacs syntax highlighting had completely missed the Postmatch indicator. Those of you who are expert perl programmers will no doubt recognise the special perl variable "$'" in the above code, which is the abbreviation for the Postmatch variable (c.f. the perlvar man page).

What the code the code should have looked like, would have been more like this:
while (<TMP>){
        if (/^From /) {
                my $foo = $';
                if ($foo eq "bar") {
                        my $bar = "barbarcan";

So how could Emacs get it so wrong? Surely this couldn't be the case? I would have expected the great and powerful Emacs to do much better than this!

Well, it turns out that the problem is with the major mode. But I only discovered this after a fair bit of searching on the Internet. I was not able to find the answer in the Emacs on-line help. However I should say that there is a manual called the The Emacs Beginner's HOWTO, written by Jeremy D. Zawodny, which explains all of this in detail. And if you are an Emacs beginner (especially if you are a vi user), than you could do a lot worse than Jeremy's excellent manual.

It seems that if you use the standard Emacs distribution, anything that looks like a perl script is processed using the major mode called perl-mode. "Well that seems to be fair enough", you might think if you were an Emacs beginner. But you would be wrong. As Jeremy patiently explains, most Emacs perl experts use cperl-mode, which has been better maintained than the older (and probably obsolete) perl-mode.

It is relatively easy to change the major mode. And of course, (with the code M-x cperl-mode). But it gets a little tedious doing that every time I load a perl script. So the question I now had was how do I persuade Emacs to load the cperl-mode every time I open a perl script? Of course, all you Emacs nuclear submariners would know how to do it. I quickly found the lists interpreter-mode-alist and auto-mode-alist, but once again had to resort to Google to find out how to override the default. It turns out that all I required was this in my ~/.emacs file:

(fset 'perl-mode 'cperl-mode)

Nevertheless, the cperl-mode lisp that ships with Emacs 22.1.1 has several annoying little bugs. And in my humble opinion it doesn't look as good or render as quickly as the vim syntax highlighting. I found that it got confused with POD (Plain Ordinary Documentation) comments (which begin with a "=". And once such confusion arose, it would reign to the very end of the file.

The best results were obtained with the latest stable copy of cperl-mode, which can be downloaded here. Replace your existing library as follows:

First enter this command:

find /usr -name cperl-mode.elc

On my system it was in /usr/share/emacs/22.1/lisp/progmodes/ I made a backup copy of the cperl-mode.elc, and copied the latest stable code to cperl-mode.el in the library folder. This was the command I used on my system (it will vary according to which distribution and version you are using):

sudo cp cperl-mode.el.6.2 /usr/share/emacs/22.1/lisp/progmodes/cperl-mode.el

I then started up emacs as root. If you type sudo emacs this will start a non-X session of emacs as root. Load the file cperl-mode.el (using the full path). In order to compile with the the byte-compile-file macro, enter the command:

M-x byte-compile-file

You will be prompted for the file name. Type in the cperl-mode.el source file and the cperl-mode.elc will be replaced (with warnings).

Note: Apart from the HTML fragments (created with Gvim) this blog posting was created entirely with Emacs.

Other Blog Posts In This Thread:

Copyright     2008, Gerry Patterson. All Rights Reserved.