Connexions

You are here: Home » Content » Using Emacs with PSGML to edit CNXML
Content Actions

Using Emacs with PSGML to edit CNXML

Module by: Brent Hendricks

Summary: This module explains how to use Emacs's XML editing mode (provided by PSGML) to easily edit CNXML documents

Emacs is an extremely versatile editing environment. In particular, it can use customized "modes" in which new commands are available. PSGML is an emacs mode for editing SGML and XML documents. It can speed up the process of marking up a document considerably. Using emacs and PSGML to edit CNXML documents requires the following setup steps:
  1. Installing Emacs
  2. Installing PSGML
  3. Setting up an SGML catalog
  4. Setting up your environment
  5. Editing your document

Installing Emacs

Most Unix systems come with emacs already installed. Most Microsoft Windows systems do not. A precompiled version for Windows is available from the Gnu ftp site here. (If you are on a Unix system that does not have emacs installed you will have to compile it yourself, sorry!) Extract the files using a program such as WinZip and install them somewhere. A good place on Windows would be C:\Program Files.

Installing PSGML

The next step is to ensure that you have PSGML installed on your system. PSGML is a special SGML/XML editing mode for Emacs, written by Lennart Staflin. Again, many Unix systems have it installed already, but if yours does not, it is available online. If you installed your own emacs, extract the PSGML files in the site-lisp directory under your emacs installation.
Note: If you are on a Unix system and do not have permission to modify the emacs installation, create an emacs directory in your home directory, extract the PSGML files there, and add the line (setq load-path (cons "~/emacs" load-path)) to the .emacs file in your home directory.

Setting up an SGML catalog

Once you have PSGML installed, you need to setup an SGML catalog. This catalog provide the translation from public document types to physical file locations and allows emacs to use the tags from your specific language. For simplicity, Connexions has made available a catalog containing the CNXML, MathML, and QML DTDs. You can download this from our website. Just download this file and extract it somewhere on you computer. A good place on Unix might be /usr/local/share, or your home-directory. On Windows you can extract it to C:\. Remember where you extracted the files, because you will need this information in the next step.

Setting up your environment

Now that everything is installed, you must configure emacs. The first step is to tell emacs where your SGML catalog is. This is most easily done by setting the environment variable SGML_CATALOG_FILES to the location of your catalog. For example, if you extracted the catalog in the directory /usr/local/share, you would set SGML_CATALOG_FILES to /usr/local/share/sgml/catalog. On Unix this is done in your shell initialization file (.cshrc for C-shell users and .profile for Bourne shell users). On Win9X, setting variables can be done in your C:\AUTOEXEC.BAT file. On WinNT, Win2K, and WinXP this is done from the System item in the control panel.
You also need to add some instructions to your emacs initialization file. Download the xml.emacs file and append it to your emacs init file. On Unix, the emacs init file is named .emacs and resides in your home directory. On Windows, it is named _emacs and does not have a default location.
Note: On Windows, you must set the environment variable HOME to tell the system where your _emacs file is located.
If you don't have an emacs init file already, just rename xml.emacs to .emacs or _emacs as appropriate.

Simple editing with PSGML

Now you are ready to begin writing CNXML with emacs. See The Basic CNXML for an introduction to writing documents in CNXML. You must begin the document with the XML declaration and specifying the document type definition to use. For example, to begin a document using CNXML, type the following lines:
Example 1 
<?xml version="1.0" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5//EN" 
          "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_plain.dtd">
	
Once you have started the file out with this header you can begin adding markup elements.
warning: On new documents you will need to enter XML-mode explicitly by entering M-x xml-mode. ("M" represents the meta key, commonly mapped to 'Alt' on PC keyboards). Once this file is saved, successive editing sessions will bring up xml-mode automatically
You can enter a start tag by typing C-c < ("C" represents the control key). Emacs will then prompt you for the tag-name. Type the name of the tag you want, or use TAB-completion. To insert a close tag for the current element, type C-C /. Emacs will automatically close the current tag. For those who prefer menu commands, you can select Insert Start-Tag or Insert End-Tag from the Markup menu. Emacs will drop-down a list of currently available tags.
Alternatively, to ensure that you have matched start and end tags with the appropriate attributes and children, you may insert an entire element at once. To do this, type C-c C-e (or select Insert Element from the menu) and type the name of the desired element. This will insert opening and closing tags for the element and prompt you for any required attributes. It will also insert any required child elements. Conversely, to delete an entire element with its children, type C-M-k.
There are many other commands that you may find useful. Explore the Markup and View menus to get a feel for the available commands. You may also want to look at the reference manual distributed with PSGML.

Adding Tables using Emacs and PSGML

Another useful feature of emacs is the ability to add tables to your CNXML document quickly and easily. This requires the emacs table package to be installed.
  • Include the following line in your .emacs file:(require 'table)
  • Restart emacs, and you'll notice that at the bottom of the Tools menu list is a Table tool. From there you can insert a table or you can type M-x table-insert.
  • After inserting a table you will be asked questions about the table columns, rows, sizes, etc., specify this information based on your needs.
  • Finally, creating your table you will want to type M-x table-generate-source. When it asks for the language type cals. This will save the file as a CALS table valid document which is the format that CNXML uses. This table can now be cut and pasted into a CNXML document.

Non-English characters

If your CNXML document is not written in English, you will probably need to enter non-English characters. If you have a non-US keyboard you can simply type the desired characters as you normally would. Many systems also include a "character picker" of some sort, allowing you to copy occasionally used characters or symbols to the clipboard. You can then paste them into emacs.
iso-accents-mode For accented characters appearing in many western languages, you may want to use emacs' iso-accents-mode Just enter the command M-x iso-accents-mode. You can then enter accented characters by typing ' (apostrophe) followed by a vowel. Check the instructions for more details.

Comments, questions, feedback, criticisms?

Discussion forum

Send feedback