Emacs-fu Emacs Tips

Using/Extending Core Libraries

There are a few emacs libraries that store current state in global variables. For example, ido stores the list of current matches in ido-matches. Accessing this variable while filtering using ido can be a little convoluted. I gave an example of how to do this in Shell Command on Multiple Buffers.

Dabbrev also uses global variables although it provides [internal] functions for completing an abbreviation. I have a complete example at Autocomplete with a Dabbrev Twist but the core is very simple:

Multi-file Search/Replace

I really liked this post from Ian Eure demonstrating how to do multi-file search and replace in emacs. I frequently see emacs proponents saying its awesome and you’ll know when you reach emacs nirvana so it is nice to see a practical demonstration of emacs power. And of course I know it is possible in shell but the simplicity and the interactive nature of emacs makes this a much more pleasant experience. A quick summary:

Emacs Popularity

So first of all I found this post referring to a thread where a guy says he recommends nano as he has used Unix-like systems that don’t have vi. Hmmm… okay, and then that post has this one from 2007 in the auto-generated links list about Emacs losing in popularity.

And from there I get to this one which has a poll where 56% of the almost 750 respondants chose vim1 as their favourite Linux text editor compared to 9% choosing emacs. Of course it is a highly unscientific result, but do you think more than 6 times as many people use vim as emacs?

So much for good intentions. I get myself all geared up to
the problem I need to solve today? Well, I want to find out about how emacs networking works.

So first, I need a simple server to play with. Now normally when I think server I automatically think Apache but this time I want something a bit more basic. And if I was thinking enterprise1 I might reach for C++/ACE. However, for something basic, Perl is ideal.

I’ve just upgraded to Ubuntu 9.04 on this box and Perl is unused so let’s see if it has what I need.

06.52 Ubuntu finishes booting

I waste a few minutes on the internet.

06.57 I start Emacs

and remind myself just how gorgeous emacs-23 looks.

06.59 I check for the Net::Server package

$ perldoc Net::Server
You need to install the perl-doc package to use this program.
$ sudo apt-get install perl-doc
$ perldoc Net::Server
No documentation found for "Net::Server".

It is not installed. I could install it using apt (it is called libnet-server-perl) but I’ve got in the habit of using Perl’s CPAN module which provides package management facilities too. The advantage is that it is somewhat consistent across platforms.

Yes, Emacs is my OS, but I still need to run external programs not written in emacs-lisp from time to time. For example, emacs doesn’t have a method for processing a section of a file – without help from an external program, you need to load the whole thing into a buffer.

Why learn a new programming language?

And no, because the pragmatic programmers told me to learn a new one every year is not a good reason. If it is something you need for work, then the choices is normally straightforward – learn whatever they tell you to learn at your firm.

Languages Used in Industry

If you don’t have a job and want to get one, choose C# if you like Microsoft, Java if you don’t or perhaps PHP if you want to do web programming. C++, Python or Perl might be reasonable choices depending on which industry you want to go into. And no, Python did not "kill" Perl nor did Ruby "kill" Python.

Language Complements

If you already know a systems language such as Java, C# or C++ it is worth learning either Python or Perl. The reverse is a weaker proposition but still true in my opinion. However, if you already know both a systems language and a scripting language, any time invested in learning a new language could be used to improve your knowledge of one of your existing languages – maybe learn a framework such as ACE, Twisted or POE.

Studying For Fun

It may be worth learning a new language if you are just learning for fun and your existing languages don’t cover a particular problem area that you are interested in. For example, if you want to write GUIs and only know C++ and Perl maybe you should learn Python so you can use wxPython (just kidding!).

The problem then is that you can’t do a thorough evaluation of a new language in a small amount time. Perhaps the best you can do is to list features that are important to you and exclude languages on that basis.

So for me, I don’t like to write C++ in my limited spare time and my second language, Perl has a few minor flaws such as threading1. I’m looking for a replacement for pottering about with. My big ticket requirements are:

Nothing to do with .NET

Portable to at least Linux and Windows

I would prefer run-time size to be small for simple program

It must have a REPL I can run from Emacs

It needs to have fairly complete libraries (which implies)

It must have a reasonable size user community

Pre-emptive threading would be nice

The JVM Languages

JVM based languages neatly solve the libraries issue and you get threading and portability too. The two languages I would consider here are Clojure and Scala. I’m more interested in the former but I’m a bit concerned about run-time size. From what people tell me, three or four Java clients is all a decent PC can take before it falls over.

D, Haskell, Ocaml

I suspect all of these make relatively small runtimes. Haskell immediately excludes itself as any language where I can’t put a Printf where I want sucksis not the language for me.

Scheme / Common Lisp

There are simply too many scheme implementations which must affect the size of the user community so I’m ruling scheme out. And if I’m going to run a Common Lisp VM, why not use Clojure instead and get the Java Libraries for free?

Lua

Lua covers most of the points. I just need to check if it has a REPL.

So following that set of (weak) justifications, my shortlist consists of Clojure, Ocaml, Lua and Perl of course. Next I’m going to do a very basic check of runtime size.

1. Since discovering POE, I haven’t needed threads so perhaps I should admit that whenever raw speed is unimportant (and that is usually the case) Perl might be my perfect language.

I came across this old post recently and I’m always a little curious about people redefining their keys so extensively. So what did they lose? They changed C-oC-nC-sC-wC-f and M-s. Well, C-o is open-line – no big deal. C-n and C-f are both movement keys. I guess you could use cursor keys fairly easily (although for me both of those are burned into muscle memory).

C-s is isearch-forward which I, and probably almost all other emacs users, use all the time. However, in this configuration it has just moved to C-f which fits in with Windows terminology (find instead of search). M-s is a prefix key for a bunch of things. The only one I actually use is occur which I call using M-x anyway.

C-w (kill region) is really the only loss. And I’d prefer to have a longer combo to exit emacs just in case I hit it by mistake. Often enough I have scratch-pad work stored in fileless buffers (e.g. *temp*) which emacs would not warn me about if I tried to exit.

Overall this is a pretty radical overhaul of the keys and it doesn’t really break anything. They didn’t make the classic mistake of redefining C-x, but instead used cua-mode. Maybe it is worth considering moving keys around to fit my workflow better. What does eveyone else think?