<?xml version="1.0" encoding="UTF-8"?>
<essay xml:lang="en" version="pto" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:gal="http://norman.walsh.name/rdf/gallery#">
<info>
    
    
    
    
    
    
    
    
    
    
    
<title>Emacs, XML, Unicode</title><biblioid class="uri">http://norman.walsh.name/2003/09/29/xmlchars</biblioid>
<volumenum>6</volumenum>
<issuenum>90</issuenum>
<pubdate>2003-09-29</pubdate>
<date>$Date: 2005-09-11 10:27:02 -0400 (Sun, 11 Sep 2005) $</date>
<author>
      <personname>
<firstname>Norman</firstname>
	<surname>Walsh</surname>
</personname>
    </author>
<copyright>
      <year>2003</year>
      <holder>Norman Walsh</holder>
    </copyright>
<abstract>
<para>Inserting Unicode characters into emacs.
Input methods like this greatly reduce the need for entity declarations,
the last remaining holdouts from my life with DTDs.</para>
</abstract>
<dc:subject rdf:resource="http://norman.walsh.name/knows/taxonomy#Emacs"/>
<dc:subject rdf:resource="http://norman.walsh.name/knows/taxonomy#XML"/>
</info>

<epigraph>
<attribution>rlr at panix.com</attribution>
<para xml:id="p1">Some people do their laundry in emacs, but I find
typing ^C-^X-^W-q-L-TT to add the fabric softener to be a bit cumbersome.
</para>
</epigraph>

<para xml:id="p2"><personname>
      <firstname>Tim</firstname>
      <surname>Bray</surname>
    </personname>
<link xlink:href="http://www.tbray.org/ongoing/When/200x/2003/09/27/UniEmacs">followed
up</link> (in a sense) to
<link xlink:href="../27/dtds">my essay</link> on moving beyond DTDs with some
nifty <application>emacs</application>
code for inserting special
Unicode<indexterm>
      <primary>Unicode</primary>
    </indexterm>
characters directly into a buffer.
</para>

<para xml:id="p3">Input methods like this greatly reduce the need for entity declarations,
the last remaining holdouts from my life with DTDs. Who needs:</para>

<screen>&lt;!DOCTYPE article [
&lt;!ENTITY euro "&amp;#x20AC;"&gt;
]&gt;</screen>

<para xml:id="p4">And the corresponding <quote>&amp;euro;</quote>, if you can just
stuff a <quote>€</quote> into your buffer!</para>

<para xml:id="p5">I read Tim’s essay and decided that he was right about some things,
like “smart quotes,” but he didn’t do it quite the way I would have.
So I banged away for a bit and coded up
<link xlink:href="http://nwalsh.com/emacs/xmlchars/">XML Characters</link>, my
own solution.</para>

<para xml:id="p6">XML Characters provides four functions:</para>

<variablelist>
<varlistentry>
      <term>
	<literal>smart-double-quote</literal>
      </term>
<listitem>
<para xml:id="p7">This function, which I bind to <literal>"</literal> in
<systemitem role="emacs-mode">nxml-mode</systemitem>, inserts the appropriate
double quote. Called after a space, newline, or &gt;, it inserts a left double
quote. Called after a double quote, it cycles through the three possible
quote styles: left, straight, or right.
Called anywhere else, it inserts a right double quote.</para>
<para xml:id="p8">Inside a start tag, it always inserts just a vanilla ".</para>
</listitem>
</varlistentry>

<varlistentry>
      <term>
	<literal>smart-single-quote</literal>
      </term>
<listitem>
<para xml:id="p9">I bind this to <literal>'</literal> in
<systemitem role="emacs-mode">nxml-mode</systemitem> and it does just
what you think it does.
</para>
</listitem>
</varlistentry>

<varlistentry>
      <term>
	<literal>insert-xml-char</literal>
      </term>
<listitem>
<para xml:id="p10">This function inserts a named XML character. For example,
<literal>(insert-xml-char "sect")</literal> inserts a section mark (§).
The set of names is
maintained in a couple of associative lists, so you can easily tweak them.
Called with no arguments, it pops up a menu, somewhat like Tim’s code.
</para>
<para xml:id="p11">I bind this to <literal>C-t c</literal> because I have a pretty extensive
Ctrl-T map that I’m used to using.</para>
</listitem>
</varlistentry>

<varlistentry>
      <term>
	<literal>shortcut-xml-char</literal>
      </term>
<listitem>
<para xml:id="p12">Where Tim seems content to have a selection of accented characters in a menu, I decided
I wanted more complete and uniform access to all the ISO Latin 1 accented
characters (plus a few other things; there’s another list, so you’re free to
tweak).</para>
<para xml:id="p13">For my function, I chose to read two more keystrokes and compose the
approprate character that way. I bind this to <literal>C-t e</literal> at the
moment.
</para>
<para xml:id="p14">So, for example, I can type <literal>C-t e e '</literal> to insert
“e acute”. Or <literal>C-t e $ y</literal> to insert a yen symbol.</para>
</listitem>
</varlistentry>
</variablelist>

<para xml:id="p15">Thanks, Tim! I didn’t know how much I needed these functions before I wrote
them. In the course of writing one essay, I’ve decided I wouldn’t want to live
without them.</para>

</essay>

