Why would you choose HTML helper mode over Emacs's own HTML mode? Although HTML mode makes it easy to write basic HTML, it provides little support for programmatic, interactive web pages. HTML helper mode supports ASP, JSP (and JDE, the Java Development Environment, discussed in Chapter 9), and PHP, to name a few more advanced features. If you're writing HTML in Emacs, you're likely to be a developer of such pages rather than a more text-oriented author. For this reason, HTML helper mode continues to be popular among Emacs users.
Html helper mode is not part of Emacs by default. You can download it from its homepage at http://www.nongnu.org/baol-hth. Download the file into a directory such as ~/elisp, move to that directory, and then type:
% tar xvzf html-helper-mode.tar.gz
The system unpacks the tar file for you. (Of course, if you are installing on Windows, you can simply use WinZip to decompress and unpack the file.) The tar file contains several components, including:
• html-helper-mode.el—the Lisp file for HTML helper mode
• hhm-changelog—changes that have been made
• hhm-config.el—a Lisp file that allows Emacs customization to work[47]
8.3.2.1 Starting HTML helper mode
Before you can start HTML helper mode, you have to load it into Emacs. (For a complete discussion of this topic, see "Building Your Own Lisp Library" in Chapter 11; we describe it briefly here.) Begin by typing M-x load-file Enter. Emacs asks which file to load and you enter ~/elisp/html-helper-mode.el and press Enter, adjusting the path to reflect the location where you installed html-helper-mode.el. You enter the mode by typing M-x html-helper-mode Enter. HTML helper appears on the mode line.
Making HTML helper mode part of your startup is easier. Put the following lines in your .emacs file:
(setq load-path (cons "~/elisp " load-path))
(autoload 'html-helper-mode "html-helper-mode" "Yay HTML" t)
In the first line, insert the complete path for the directory in which html-helper-mode.el is located in quotation marks, replacing ~/elisp to the correct value for your system. The second line tells Emacs to load HTML helper mode automatically when you start Emacs.
If you want to use HTML helper mode for editing HTML files by default, add this line to .emacs as welclass="underline"
(setq auto-mode-alist (cons '("\\.html?$" . html-helper-mode) auto-mode-alist))
If you edit other types of files with HTML helper mode, you may want to add lines to include all the types of files you edit. Adding more lines is the easiest way. For example, to make HTML helper mode the default for PHP files, add this line to .emacs:
(setq auto-mode-alist (cons '("\\.php$" . html-helper-mode) auto-mode-alist))
8.3.2.2 A brief tour of HTML helper mode
The main reason people like HTML helper mode is that it provides easy menu access to a wide variety of options. Realizing that having a crowded menu with many submenus could overwhelm new users, the authors created an option called Turn on Novice Menu. Selecting this option from the HTML menu provides a barebones menu, as shown in Figure 8-1. Novice HTML writers can use these options to create a basic HTML document without worrying about what forms, JSPs, PHP, and the like mean.
Figure 8-1. HTML helper mode's Novice menu (Mac OS X)
Selecting Turn on Expert Menu from the HTML menu returns the larger menu with its numerous submenus, as shown in Figure 8-2.
Figure 8-2. HTML helper mode's Expert menu (Mac OS X)
8.3.2.3 Inserting an HTML template
HTML helper mode inserts a template for you every time you create a new HTML file.
Type: C-x C-f new.html
HTML helper mode inserts a template with all the basic elements needed for a valid HTML document (Windows).
The template contains all the basic HTML elements. The entire document is surrounded by <html></html> tags. Then the head and the body are separated. Following an <hr> tag that tells the browser to insert a horizontal line, called a horizontal rule, the <address> tag leaves a place for the author to put in his or her email address. In these days of spam, it's unlikely you'll want to do that. (You can leave the <address> tag blank or delete it.)
If you do want to include an email address, enter a line like this in your .emacs file (substituting your own email address, of course):
(setq html-helper-address-string
"<a href=\"mailto:cdickens@great-beyond.com "\>Charles Dickens</a>")
Type: C-x C-f newfile.html
Emacs inserts the HTML template, including the address.
Normally you begin filling out the template by entering title and a level-one header (these are often the same). You can then begin writing paragraphs of text. Before you start typing, press M-Enter. Emacs inserts <p></p> and positions the cursor between them. You can see from the ending paragraph tag that HTML helper mode is working toward XHTML compliance.
Type: M-Enter
Emacs positions the cursor between <p> and </p> so you can start insert text.
8.3.2.4 Putting tags around a region
When editing HTML files, you often spend a lot of time marking up existing text. If you preface any of the tag commands with C-u, Emacs inserts the tags around a region rather than putting them at the cursor position.[48] To demonstrate, we'll start a new HTML file and insert text from our dickens file.
Type: C-x C-f ataleoftwocities.html
Emacs inserts the HTML template.
Move the cursor past the <h1> pair and type C-x C-i dickens.
Emacs inserts the dickens text file, to which we can add HTML tags.
If you were really doing this properly, you'd type something like "A Tale of Two Cities, Chapter 1 as the title and the first-level header. But for now, you just want to see how to mark up a region of existing text. Begin by marking the Dickens paragraph as a region and type C-u M-Enter.
47
The version we downloaded in August 2004 marked this file as alpha code, so don't be surprised if you find bugs. Visit the file to see if its status has changed.
48
For this to work, you must invoke the command through the keyboard, either using its key binding or its command name. Using a menu option doesn't work.