This column is devoted to making the best use of Emacs, text editor extraordinaire. Each issue I plan to present an Emacs extension which can improve your productivity, make the sun shine more brightly and the grass greener.
You’ve probably heard the hype about « Internet-ready » operating systems where you can access the Internet with a click of your mouse. Well, users of the customizable Emacs user interface have had the Net only a keypress away since as early as 1989!
Emacs has built-in networking capability, which it uses for connecting to news or SMTP servers and for web browsing. To illustrate its use, here is a bit of code which opens a TCP connection to port 13 of your local machine to request the current time :
;; usage: M-x display-date (defun display-date () (interactive) (let ((stream (open-network-stream "DAYTIME" nil "localhost" "daytime"))) (set-process-filter stream 'my-process-filter)) (defun my-process-filter (proc string) (message "Current date is %s" (substring string 0 -1)))
This code –which assumes you’re running inetd– should display something like the format of
date in the minibuffer. The rich set of primitives and the integrated error management provided by Emacs make it easy to program useful utilities; consider how many lines of code would have been required to provide the equivalent functionality in C.
Ange-ftp is a package by Andy Norman which allows Emacs to see the entire Internet as a virtual filesystem. It adds remote editing capability by mapping requests for remote files to FTP commands. For example, if you ask Emacs to open a file named
then ange-ftp will spawn an FTP process, connect to the host salines.cict.fr as user marsden, CWD to my home directory, GET my Emacs initialization file and display the file as if it were on your local filesystem. If ange-ftp needs a password it will read one from the minibuffer. If you make changes to the file and save it, it will be PUT back to the server for you. You can even copy files from one remote machine to another by typing
M-x copy-file RET /user1@host1:/path/to/file1 RET /user2@host2:/path/file2 ; ange-ftp looks after opening two ftp connections for you.
Ange-ftp comes pre-installed with Emacs (XEmacs features efs, a complete rewrite by the same author). The only customization you might need to make is to configure a gateway, if you don’t have direct Internet access. You can use ~/.netrc to configure default logins for oft used hosts in the traditional way (and even passwords if you’re foolhardy).
Perhaps the most elegant feature of ange-ftp is its seamless integration with Emacs; the only visible change it introduces is the extended filename syntax. Filename completion (by pressing TAB in the minibuffer) is available on remote hosts in the same way as on your local machine. Ange-ftp works well with Dired, the directory editor, allowing you to browse though distant machines, operate on several remote files at once, etc. It also works with bookmarks, so you can memorize an interesting spot on your favorite ftp server, and jump back to the same spot next week with ease. Take a typical usage: ask Emacs to open the following directory (with
C-x C-f or from the Files menubar) :
You will be presented with a directory listing many different releases of Linux kernels (if you have a line like
default login anonymous password user@site in ~/.netrc then ange-ftp can infer the anonymous@ for you automatically). Type
C-x r m to bookmark the location. There’s more on bookmarks in Jesper Pedersen’s article in issue 7 of the Linux Gazette.
Emacs-w3 (also referred to by some as Gnuscape) is a web browser written by William Perry in Emacs Lisp. It is fairly sophisticated in certain respects, having been the first production browser to support cascading style sheets. It understands tables, and can display images inline under XEmacs, or by invoking external viewers when hosted by GNU Emacs. Its author notes that Emacs-w3 is yet another reason never to leave the comfort of the One True Editor, but to me it serves more as a reminder of the deficiencies of Emacs Lisp : it is slow, and has a tendency to block while waiting on a slow link (unfortunately Emacs is not multi-threaded, though you can set the variable url-be-asynchronous to t to reduce this annoyance). If you want to try it out get the latest version from the betas directory, which has many improvements over the version distributed on most Linux CDROMs.
browse-url is an nifty Emacs extension which can dispatch references to URLs to Mozilla or to Emacs-w3. It does this by using Netscape’s remote invocation protocol, which as a side note even works when you’re running the browser on a distant machine (the implementation uses the X11 inter-application communication protocol). Emacs features its own remote control mechanism which lets you send commands to a running Emacs (even on another machine), called gnuserv/emacsclient, which I might talk about another time.
Recent versions of Emacs are set up to use browse-url in mail and news reading modes. URLs should be highlighted when you pass the mouse over them, and you can click on them with the middle mouse button to invoke your favorite browser. Here’s how you can set up browse-url to use Mozilla when you’re running X11 and Emacs-w3 otherwise :
(if (eq window-system 'x) (setq browse-url-browser-function 'browse-url-netscape browse-url-new-window-p t) (setq browse-url-browser-function 'browse-url-w3))
Another more indirect use of browse-url is WebJump by Neil W. Van Dyke. This Emacs plugin provides a programmable hotlist of interesting web sites with which to feed your browser. Perhaps its most interesting feature is the ability to send a query to Internet search engines such as AltaVista and Yahoo! without having to load the first page of ads, but it also includes features for dispatching searches to FAQ and RFC archives, to the online Webster or Thesaurus, or to bring up an appropriate page of the Java API. Naturally (this is Emacs) you can extend it to include your own favorite sites. You might find yourself using it more than your browser’s bookmarks.
Files at your Fingertips
ffap is a powerful package which extends the
find-file command (the one which prompts for a file name in the minibuffer, normally bound to
C-x C-f). It searches the text around the cursor position for something which might represent a filename — a file in the current directory, a C #included file, a newsgroup reference, an ange-ftp style reference to a file on a remote machine or an URL — and prompts you either to open that file, or to send the URL to a browser (via browse-url). Once experiencing this you quickly get sick of typing filenames into the minibuffer, and may find yourself inserting « hyperlinks » in strategic places in your files to save typing. ffap is distributed with both Emacs and XEmacs; I bind it to the F3 key as follows :
(autoload 'find-file-at-point "ffap" nil t) (define-key global-map [(f3)] 'find-file-at-point)
or if you prefer you can simply override the traditional find-file by saying
(autoload 'find-file-at-point "ffap" nil t) (define-key global-map [(control x) (control f)] 'find-file-at-point)
ffap is pretty good at determining interesting filenames; it even knows how to recognize RFC names, and from which server they may be obtained. It goes to the trouble of pinging remote machines to determine whether they are alive, and can naturally be extended to recognize personal types of filename references. To conclude on filename shortcuts, you might enjoy Noah Friedman’s fff (Fast File Finder) which helps you find files hidden somewhere deep in inode-space by querying your locate database (part of the GNU findutils).
Several people wrote to me with comments on last month’s article on jka-compr. Chistopher B. Browne told me he prefers crypt++, which provides on-the-fly decryption and encryption as well as automatic compression and decompression. Whereas jka-compr trusts the filename extension, crypt++ reads the first few bytes of the file to determine its type. The package also has functions for dealing with DOS-style linefeeds which you might find useful if you have to exchange files with other operating systems, although you could just as well say
(standard-display-ascii 13 ""), which simply hides those ^M characters. Crypt++ is not a standard part of Emacs (it’s not included in the GNU Emacs distribution, though it is bundled with XEmacs). I haven’t tested its cryptographic capabilities, because
As a French citizen I am prohibited from using any form of encryption. In France encryption requires authorization from the President, which is accorded only to large military companies and to financial institutions (and then only if the keys are held in escrow). These laws are one of the reasons holding back the incorporation of kernel-level support for encryption in Linux. Before accusing France of being backward, please consider the fact that countries such as Iran, China and Russia impose similar restrictions on the freedom of their citizens.
If you’re using the latest version of XEmacs (20.3 stable as of this writing), the suggestion I make last month for enabling jka-compr won’t work. The XEmacs maintainers have decided that the behaviour of your editor shouldn’t be modified by loading an extension module, but by calling an appropriate initialization function. The correct way of enabling jka-compr is to say
(toggle-auto-compression 1 t). Sorry ’bout that, folks.
Next time …
In the next issue I’ll review ediff, a powerful interface to diff and patch. Don’t hesitate to contact me at with comments, corrections or suggestions (what’s your favorite couldn’t-do-without Emacs extension package?).
C-u 1000 M-x hail-emacs !
PS : Emacs isn’t in any way limited to Linux, since implementations exist for many other operating systems. However, as one of the leading bits of open-source software, one of the most powerful, complex and customizable, I feel it has its place in the Linux Gazette.
EMACSulation #1, February 1998