;; See Example 19-4 for keybindings.

(defvar enriched-text-types '((?b . "bold") (?i . "italic") (?f . "fixed")
                              (?s . "smaller") (?B . "bigger")
                              (?u . "underline") (?c . "center"))
  "Alist of (final-character . directive) choices for add-enriched-text.
Additional types can be found in RFC 1563.")

(defun add-enriched-text (begin end)
  "Add enriched text directives around region. The directive used
comes from the list enriched-text-types and is specified by the last
keystroke of the command.  When called from Lisp, arguments are BEGIN
and END."
  (interactive "r")
  ;; Set type to the directive indicated by the last keystroke.
  (let ((type (cdr (assoc (logior last-input-char ?`) enriched-text-types))))
    (save-restriction			; restores state from narrow-to-region
      (narrow-to-region begin end)	; narrow view to region
      (goto-char (point-min))		; move to beginning of text
      (insert "<" type ">")		; insert beginning directive
      (goto-char (point-max))		; move to end of text
      (insert "</" type ">"))))		; insert terminating directive
