old-www/LDP/LG/issue31/marsden.html

523 lines
22 KiB
HTML

<!--startcut ==========================================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<head>
<title>EMACSulation LG #31</title>
<link rev="made" href="mailto:emarsden@mail.dotcom.fr">
<meta name="keywords" content="emacs, customize, customization">
<meta name="description" content="Various ways of customizing Emacs">
<meta name="author" content="Eric Marsden">
<meta name="generator" content="Emacs">
</head>
<body bgColor="#FFFFFF" text="#000000">
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<h1>EMACSulation</h1>
<h4>by Eric Marsden,
<a href="mailto:emarsden@mail.dotcom.fr">emarsden@mail.dotcom.fr</a>
</h4>
</center>
<p>
<blockquote><small>
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.
</small></blockquote>
<P> <HR> <P>
<h1>Customizing Emacs</h1>
<p> Typical applications have a configuration file which allows you to set
the value of a few variables. Emacs goes a lot further, since the user
can customize pretty much any feature of the system. This flexibility
is due to a majority of the system being written in its extension
language. The first implementations of Emacs used a cryptic string
processing language named <a
href="http://www.netmeg.net/jargon/terms/t.html#teco">TECO</a> (you
thought <a href="http://www.perl.com/">Perl</a> was cryptic? <a
href="ftp://ftp.mindlink.net/pub/teco/">Look at</a> TECO), with support
code in <a
href="http://www.netmeg.net/jargon/terms/p/pdp-10.html">PDP-10</a>
assembly; later versions use Emacs Lisp as an extension language, with
some components (the Lisp interpreter and the display code) written in
C.
<p> This extensibility is fundamental to Emacs. Users can experiment with
modifications and improvements to the system and pass them on to
friends if they work well; ideas which catch on can be integrated in
the core distribution. The now defunct <a
href="http://www.eval-apply.com/LispOS/memo444.htm">Lisp Machines</a>
at <a href="http://web.mit.edu/">MIT</a> extended this openness to the
level of the operating system, which was written in Lisp (and ran on <a
href="http://www.lavielle.com/~joswig/symbolic-computing.html">custom
hardware</a>, specially designed to run Lisp efficiently). Lisp Machine
users had a far greater degree of control over their machine than Linux
users today. The <a href="http://squeak.cs.uiuc.edu/">Smalltalk</a>
systems at <a href="http://www.parc.xerox.com/history.html">Xerox
Parc</a> provided a similarly deep level of customization.
<p> Indeed, there is a profound difference between the development
philosophy which led to Emacs (the MIT approach), and that which led to
Unix and C (the New Jersey approach). These are compared in an
excellent paper by Richard Gabriel called <a
href="http://www.naggum.no/worse-is-better.html">Worse is Better</a>,
from which I have extracted the following:
<!-- hope nobody is concerned about copyright on this ... -->
<blockquote><font face="Helvetica">
<p> Two famous people, one from MIT and another from Berkeley (but working on
Unix) once met to discuss operating system issues. The person from MIT was
knowledgeable about ITS (the MIT AI Lab operating system) and had been
reading the Unix sources. He was interested in how Unix solved the PC
loser-ing problem. The PC loser-ing problem occurs when a user program
invokes a system routine to perform a lengthy operation that might have
significant state, such as IO buffers. If an interrupt occurs during the
operation, the state of the user program must be saved. Because the
invocation of the system routine is usually a single instruction, the PC of
the user program does not adequately capture the state of the process. The
system routine must either back out or press forward. The right thing is to
back out and restore the user program PC to the instruction that invoked
the system routine so that resumption of the user program after the
interrupt, for example, re-enters the system routine. It is called PC
loser-ing because the PC is being coerced into loser mode, where loser is
the affectionate name for user at MIT.
<p> The MIT guy did not see any code that handled this case and asked the New
Jersey guy how the problem was handled. The New Jersey guy said that the
Unix folks were aware of the problem, but the solution was for the system
routine to always finish, but sometimes an error code would be returned
that signaled that the system routine had failed to complete its action. A
correct user program, then, had to check the error code to determine
whether to simply try the system routine again. The MIT guy did not like
this solution because it was not the right thing.
<p> The New Jersey guy said that the Unix solution was right because the design
philosophy of Unix was simplicity and that the right thing was too complex.
Besides, programmers could easily insert this extra test and loop. The MIT
guy pointed out that the implementation was simple but the interface to the
functionality was complex. The New Jersey guy said that the right tradeoff
has been selected in Unix: namely, implementation simplicity was more
important than interface simplicity.
</font></blockquote>
<p> Let me come back to more concrete issues. The traditional way of
customizing [X]Emacs is to write simple Emacs Lisp expressions in a
file called <tt>.emacs</tt> in your home directory. These expressions
can either set the value of a variable, or call a function, or load a
library&nbsp;: </p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td>
<pre class="programlisting">
<font color="red">
;; set the values of a few variables. `t' stands for true and `nil' for
;; false </font>
(setq dired-listing-switches "-alF")
(setq tab-width 4)
(setq line-number-mode t)
(setq global-font-lock-mode t)
(setq next-line-add-newlines nil)
<font color="red">
;; call a function which will organize to have the time displayed in
;; the modeline</font>
(display-time)
<font color="red">
;; load an Emacs Lisp library and call its initialization function</font>
(require 'jka-compr)
(auto-compression-mode 1)</pre></td></tr></table>
<p> The syntax tends to irritate people (who deride the presence of
``<b>L</b>ots of <b>i</b>rritating <b>s</b>purious
<b>p</b>arentheses''), but using a fully-featured programming language
in a configuration file has genuine and significant advantages. It
allows you to test for features of the local setup, for example&nbsp;:
</p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td>
<pre class="programlisting">
(if (file-exists-p "/bin/bash")
(setq explicit-shell-file-name "/bin/bash"))</pre>
</td></tr></table>
<p> which makes Emacs use the bash in <tt>*shell*</tt> buffers if it
exists on the machine. Another reason for using a real language for
setup files is that it avoids the endless proliferation of
configuration files, each with its own ideosyntractic syntax (think
~/.Xdefaults, window manager setup files, ~/.inputrc,
~/.procmailrc, etc). Hopefully the spreading use of <a
href="http://www.red-bean.com/guile/">Guile</a> as an embedded
scripting language will resolve this problem. A last advantage is that
a genuine programming language empowers the user. Indeed, in one of the
<a
href="ftp://ftp.dl.ac.uk/pub/fx/emacs/emacs-AI-memo/AI-519a.html">first
technical reports</a> about Emacs, <a
href="http://www.gnu.org/people/rms.html">Richard Stallman</a> presents
this as an important goal:
<blockquote><font face="Helvetica">
When large numbers of nontechnical workers are using a programmable
editor, they will be tempted constantly to begin programming in the
course of their day-to-day lives. This should contribute greatly to
computer literacy, especially because many of the people thus exposed
will be secretaries taught by society that they are incapable of doing
mathematics, and unable to imagine for a moment that they can learn to
program. But that won't stop them from learning it if they don't know
that it is programming that they are learning!
</font></blockquote>
<h2>Customize</h2>
<p> Recent versions of [X]Emacs include a package called
<em>Customize</em>, which helps you adapt Emacs to your liking without
writing any Lisp. Customize is written by <a
href="http://www.dina.kvl.dk/~abraham/">Per Abrahamsen</a>, also the
author of the popular <a
href="http://sunsite.auc.dk/auctex/">Auc-TeX</a> package for TeXnical
typists. It allows users to visualize the list of all the
user-configurable variables in Emacs, and to modify them to their
liking. You can reach Customize (it only exists on recent Emacsen) from
the Help <tt>-></tt> Customize menubar. In XEmacs it looks like this:
<p align="center"> <img src="./gx/marsden/customize.gif" alt="Customize screenshot"
width="535" height="668">
<p> Customize requires each Emacs Lisp library to declare the
user-modifiable variables it exports. The type of the variable is
specified, whether boolean or integer or string or selection from
several options, and several variables can be put in the same group,
which allows a hierarchical presentation of information. The
modification screens are then generated automatically on demand. It's
an elegant design, since programming the thousands of dialog boxes
manually would be a huge job, and require large amounts of storage. The
modifications made by the user are stored in a file which is read by
[X]Emacs at initialization time.
<p> An unfortunate aspect of Customize is that it introduces two methods of
doing the same thing. Users wanting to go beyond the simple
customizations possible with the graphical interface will be confronted
with an entirely new way of doing things, and may be put off. There is
a difficult path to tread between making <a
href="http://lieber.www.media.mit.edu/people/lieber/Teaching/Simple-Simple/Simple-Simple-Intro.html">
simple things simple and complex things possible</a> and the old adage
<i>Build a system that a fool can use, and only a fool will use it</i>.
<h2>Keybindings</h2>
<p> Keybindings are a particularly treacherous field for customization, the
principal problems comings from differences between X11 and console
operation (particularly with the <a
href="http://www.via.ecp.fr/LDP/HOWTO/Keyboard-and-Console-HOWTO-5.html">backspace
key</a>), and between the syntax used by Emacs and XEmacs. One of the
first things you might want to do is set the Delete, Home and EndOfLine
keys to their standard meanings in the PC world&nbsp;: </p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td>
<pre class="programlisting">
(pc-selection-mode) ; pc-select.el</pre>
</td></tr>
</table>
<p> This will also allow you to select regions of text with shift and the
cursor keys, and Cut/Copy/Paste with Shift-Delete, Control-Insert and
Shift-Insert respectively. You can bind function keys as follows (this
syntax should work equally well with Emacs and XEmacs)&nbsp;: </p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td><pre>
(define-key global-map [(f2)] 'save-buffer)
(define-key global-map [(f4)] (lambda () (interactive) (kill-buffer nil)))</pre>
</td></tr>
</table>
<p> The first line makes the <tt>F2</tt> key save the current buffer (by
binding to the built-in function <tt>save-buffer</tt>), and the second
shows how to bind to your own function: the <tt>lambda</tt> introduces
an anonymous function in Lisp; the <tt>(interactive)</tt> means that
you will be able to access the function interactively, the rest kills
the current buffer without asking confirmation. The <tt>global-map</tt>
means that these bindings apply everywhere in Emacs, whether you're
reading email or composing some HTML. You can also define local key
bindings, which apply only to buffers which are in specific
modes&nbsp;: </p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td><pre>
(define-key emacs-lisp-mode-map [(control c) (control e)] 'eval-buffer)
(define-key c-mode-map [(f5)] 'compile)</pre>
</td></tr>
</table>
<p> The Emacs FAQ (available online by saying <tt>C-h F</tt>) presents
another method of binding keys, which may not work across Emacs
versions. Jari Aalto has written a <a
href="ftp://cs.uta.fi/pub/ssjaaa/ema-keys.html">long guide to
keybindings</a> which describes many different ways of rebinding keys.
<p> If you are lucky enough to have a Windows 95 keyboard you can put those
lovely keys to use in X11 with a little <a
href="http://hoth.stsci.edu/man/man1/xmodmap.html">xmodmap</a>ping. The
X Window System has five possible key modifiers, the first three being
Shift, Control and Alt on PC keyboards. You can set the windows keys to
Super and Hyper modifiers as follows (this is for a French keyboard;
use <a href="http://www.rob.cs.tu-bs.de/man/xev(6)">xev</a> to work out
the keycodes for your keyboard)&nbsp;:</p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td>
<pre>
(shell-command (concat "xmodmap "
"-e 'keycode 115 = Hyper_L' "
"-e 'keycode 116 = Hyper_R' "
"-e 'keycode 117 = Super_L' "
"-e 'add mod4 = Hyper_L' "
"-e 'add mod4 = Hyper_R' "
"-e 'add mod3 = Super_L' "))
(message "Setting up Hyper and Super keys")
(define-key global-map [(hyper tab)] 'complete-tag)
(define-key global-map [(super !)] 'speedbar-get-focus)
</pre></td></tr>
</table>
<h2>Backups</h2>
<p> In its default configuration, Emacs makes backups with a tilde appended
to the filename, in the same directory as the original file. If you
would prefer having all backups in one spot, try the following code.
Emacs can also keep a series of numbered backups <i>&agrave; la</i> VMS; look
at the variable <tt>version-control</tt>.</p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td>
<pre class="programlisting">
<font color="red">
;; make backup files in ~/.backups/ rather than scattered around all
;; over the filesystem. </font>
(defun make-backup-file-name (file-name) <font color="brown">
"Create the non-numeric backup file name for `file-name'." </font>
(require 'dired)
(if (file-exists-p "~/.backups")
(concat (expand-file-name "~/.backups/")
(dired-replace-in-string "/" "|" file-name))
(concat file-name "~")))
<font color="red">
;; disable backups for files in /tmp or in my Mail or News directories. </font>
(defun ecm-backup-enable-predicate (filename)
(and (not (string= "/tmp/" (substring filename 0 5)))
(not (string-match "/Mail/" filename))
(not (string-match "/News/" filename))))
(setq backup-enable-predicate 'ecm-backup-enable-predicate)
</pre></td></tr>
</table>
<h2>Further information</h2>
<p> These are only a few examples of things which can be customized in
[X]Emacs. Here are a few pointers to further sources of
inspiration&nbsp;:
<ul>
<li> The online manuals contain extensive, well-written explanations which
can be browsed from within [X]Emacs (type <tt>C-h i</tt>), and include
a page of examples to put in your <tt>~/.emacs</tt>.
<li> The <a
href="http://hill.ucs.ualberta.ca/Documentation/Info/by-node/elisp-19-2.4.2/elisp_toc.html">Emacs
Lisp manual</a> describes the details and ideosyncraties of Emacs'
extension language.
<li> The <a href="http://www.cis.ohio-state.edu/hypertext/faq/usenet/GNU-Emacs-FAQ/top.html">Emacs FAQ</a>.
<li> The <a href="http://www.xemacs.org/faq/xemacs-faq.html">XEmacs FAQ</a>.
<li> The newsgroup <a
href="news:gnu.emacs.help">gnu.emacs.help</a> for GNU Emacs-specific
questions, <a href="news:comp.emacs.xemacs">comp.emacs.xemacs</a> for
<a href="http://www.XEmacs.org/">XEmacs</a> users, <a
href="news:comp.emacs">comp.emacs</a> for general questions, and <a
href="news:alt.religion.emacs">alt.religion.emacs</a> for questions
regarding the <a
href="http://www.dina.kvl.dk/~abraham/religion/">Church of Emacs</a>.
<li> Typically a new package you can pick up will include comments near the
beginning of the source code explaining which variables may usefully
be modified.
<li> XEmacs is distributed with a <a
href="http://petaxp.rug.ac.be/~erik/xemacs/sample.emacs">sample
<tt>~/.emacs</tt></a> file.
<li> Erik Sundermann's <a href="http://petaxp.rug.ac.be/~erik/xemacs/">
XEmacs Customization Page</a> is mentioned in the XEmacs FAQ.
</ul>
<h2>Feedback</h2>
<p> <a href="mailto:zweije@xs4all.nl">Vincent Zweije</a> wrote to me
regarding my gnuserv column, where I suggested using <tt>cat
/etc/passwd | md5sum</tt> as a means of generating a cookie for xauth.
<hr width="0">
<table bgColor="#FAF0E6">
<tr><td>
<font color="darkslateblue">
<p> In &lt;URL:http://www.linuxgazette.com/issue29/marsden.html&gt;, you wrote:
</font>
<blockquote>
<p> [...]
<p> While allowing access to your X display is bad enough (someone could
capture all your keystrokes, for example), giving remote access to
your Emacs process is much more worrying, since Emacs can execute
arbitrary commands under your id, delete files, send insulting email
to the President of the United States, etc.
</blockquote>
<font color="darkslateblue">
<p> Or maybe more dangerous, send insulting email to the president of an
arbitrary banana republic. :-)
</font>
<blockquote>
<p> Since release 2.1, gnuserv is able to use MIT-MAGIC-COOKIE-1
authentication for remote requests. This protocol uses the contents of
your ~/.Xauthority file, as described in the xauth(1) man page.
Gnuserv requires a cookie for display number 999, which you can create
as follows (blade is the name of the machine) :
<pre>
~$ xauth add blade:999 . `cat /etc/passwd | md5sum`
~$ xauth list
blade/unix:0 MIT-MAGIC-COOKIE-1 bc1d627babdbabe9d1f288d2b57c348f
blade:999 MIT-MAGIC-COOKIE-1 d89570b20925d401c05a79be67159cae
</pre>
</blockquote>
<font color="darkslateblue">
<p> You have picked an unlucky example. There is a real danger that
/etc/passwd is stable over a long period. This means that it will
generate the same magic cookie for many times.
<p> This is a problem when you have given a cookie away (either voluntarily
or involuntarily), and intend to revoke permissions by generating a
new cookie.
<p> Best is to select a source of data that is volatile such as the output
of ps -al or /proc/interrupts, or use mcookie, as you indicated later.
</font>
</td></tr>
</table>
<hr width="0">
<p> These remarks are particularly important if the site is using shadow
passwords. Vincent noted that security issues are too important to be
left to ordinary users&nbsp;:
<hr width="0">
<table bgColor="#FAF0E6">
<tr><td><font color="darkslateblue">
<p> Come to think of it, it's probably just a bad idea to let ordinary users
arrange their own security, as with magic cookies. They're just not
enough concerned with security - they want to get their job done.
<p> You'd be amazed at how many times on usenet I've seen suggested to do
&quot;xhost +&quot; to allow X connections from anywhere. Such people
often don't even realise that they're dealing with security. They see
disallowing X (gnuserv) connections as a hindrance, and just want a
magic incantation to remove it. It's a perception thing.
<p> Someone who deals with security simply has to know what he's doing.
However, first you have to realise that it is security that you're
playing with.
</font>
</td></tr></table>
<hr width="0">
<h2>Next time ...</h2>
<p> Next month I'll look at the different abbreviation facilities in
Emacs. Don't hesitate to contact me at
<tt>&lt;emarsden@mail.dotcom.fr&gt;</tt> with comments, corrections or
suggestions (what's <em>your</em> favorite couldn't-do-without Emacs
extension package?). <code>C-u 1000 M-x hail-emacs</code> !
<p> <strong>PS</strong> : Emacs isn't in any way limited to Linux, since
implementations exist for many other operating systems (and some
systems which only halfway operate). However, as one of the leading
bits of free software, one of the most powerful, complex and
customizable, I feel it has its place in the <i>Linux Gazette</i>.
<!--===================================================================-->
<P> <hr> <P>
<A HREF="../issue25/marsden.html">EMACSulation #1, February 1998</A><br>
<A HREF="../issue26/marsden.html">EMACSulation #2, March 1998</A><br>
<A HREF="../issue27/marsden.html">EMACSulation #3, April 1998</A><br>
<A HREF="../issue29/marsden.html">EMACSulation #4, June 1998</A>
<P> <hr> <P>
<center><H5>Copyright &copy; 1998, Eric Marsden <BR>
Published in Issue 31 of <i>Linux Gazette</i>, August 1998</H5></center>
<!--===================================================================-->
<P> <hr> <P>
<A HREF="./index.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif"
ALT="[ TABLE OF CONTENTS ]"></A>
<A HREF="../index.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
ALT="[ FRONT PAGE ]"></A>
<A HREF="./canal.html"><IMG SRC="../gx/back2.gif"
ALT=" Back "></A>
<A HREF="./roelofs.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
<P> <hr> <P>
<!--startcut ==========================================================-->
</BODY>
</HTML>
<!--endcut ============================================================-->