;;; html-helper-mode.el --- Major mode for composing html files.
;; Author: Nelson Minar
;; Maintainer: Nelson Minar
;; Created: 01 Feb 1994
;; Version: $Revision: 1.26 $
;; Keywords: HTML major-mode
;; LCD Archive Entry:
;; html-helper-mode|Nelson Minar|nelson@santafe.edu|
;; Major mode for editing HTML.|
;; 16-Mar-94|Version 2.?|ftp://ftp.reed.edu/pub/src/html-helper-mode.tar.Z
;; Copyright (C) 1994 Nelson Minar
;; Copyright (C) 1995 Nelson Minar and Ulrik Dickow
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, 675 Massachusettes Ave,
;; Cambridge, MA 02139, USA.
;;; Commentary:
;;{{{
;; Installation:
;; Add this line in your .emacs:
;; (autoload 'html-helper-mode "html-helper-mode" "Yay HTML" t)
;; To invoke html-helper-mode automatically on .html files, do this:
;; (setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist))
;;
;; This mode requires another lisp file, tempo.el. This can be
;; retrieved from ftp://ftp.lysator.liu.se/pub/emacs/tempo.el
;; Xemacs users need to have auc-menu installed.
;; Emacs 18 users need to have auc-menu and add-hook installed.
;; If your OS has broken 14 character filenames
;; this mode will also work with the name "html-mode.el".
;; Configuration:
;; see the "user variables" section, or the documentation on configuration
;; in http://www.santafe.edu/~nelson/tools/. There are variables you want to
;; configure, particularly html-helper-address-string and
;; html-helper-use-expert-menu
;; Description:
;; html-helper-mode makes it easier to write HTML documents. This mode
;; handles inserting HTML codes in a variety of ways (keybindings, menus,
;; completion in the buffer). It also supports indentation, timestamps,
;; skeletons for new documents, hilit19 patterns, and a variety of other
;; things. For the full skinny, see the HTML documentation that comes
;; with the package or is at http://www.santafe.edu/~nelson/tools/
;; Thank yous:
;; David Kågedal for the tempo code which
;; forms the core of the HTML insertion, as well as the HTML+ tags.
;; Marc Hedlund for general encouragement and
;; many helpful suggestions, especially with HTML/2.0 compliance
;; and form design.
;; Ulrik Dickow for the font-lock code
;; Denis Howe for writing browse-url.
;; Magnus Homann and Jamshid Afshar
;; for timestamp suggestions.
;; Everyone who sent me a version of menus (16 in all!)
;; Marc Andreessen for writing the original html-mode
;; The newest version of html-helper-mode should always be available from
;; http://www.santafe.edu/~nelson/tools/
;; This code was writting using folding.el, a wonderful folding editor
;; minor mode for emacs. That's what the strange {{{ comments are for.
;;}}}
;;; Code:
;;{{{ user variables
;; Set this to be whatever signature you want on the bottom of your pages.
(defvar html-helper-address-string ""
"*The default author string of each file.")
;; Features; these are all good to have on. (see also tempo.el)
(defvar html-helper-use-expert-menu nil
"*If not nil, then use the full HTML menu.")
(defvar html-helper-do-write-file-hooks t
"*If not nil, then modify `local-write-file-hooks' to do timestamps.")
(defvar html-helper-build-new-buffer t
"*If not nil, then insert `html-helper-new-buffer-strings' for new buffers.")
;; variables to configure (these defaults are reasonable.)
(defvar html-helper-htmldtd-version "\n"
"*Version of HTML DTD you're using.")
(defvar html-helper-user-menu nil
"*Extra items to put in the HTML expert menu.
The value of this symbol is appended to the beginning of the expert
menu that is handed off to easymenu for definition. It should be a
list of vectors or lists which themselves are vectors (for submenus).")
(defvar html-helper-basic-offset 2
"*Basic indentation size used for list indentation")
(defvar html-helper-item-continue-indent 4
"*Indentation of lines that follow a
item.
Default is 4, the length of things like \"
\" and \"
\".")
(defvar html-helper-never-indent nil
"*If not nil, the indentation code for html-helper is turned off.")
;; hooks (see also tempo.el)
(defvar html-helper-mode-hook nil
"*Hook run when html-helper-mode is started.")
(defvar html-helper-load-hook nil
"*Hook run when html-helper-mode is loaded.")
(defvar html-helper-timestamp-hook 'html-helper-default-insert-timestamp
"*Hook called for timestamp insertion.
Override this for your own timestamp styles.")
;; strings you might want to change
(defvar html-helper-new-buffer-template
'(html-helper-htmldtd-version
" \n"
"" p "\n\n\n"
"\n"
"
" p "
\n\n"
p
"\n\n\n"
"" html-helper-address-string "\n"
html-helper-timestamp-start
html-helper-timestamp-end
"\n \n")
"*Template for new buffers.
Inserted by `html-helper-insert-new-buffer-strings' if
`html-helper-build-new-buffer' is set to t")
(defvar html-helper-timestamp-start "\n"
"*Start delimiter for timestamps.
Everything between `html-helper-timestamp-start' and
`html-helper-timestamp-end' will be deleted and replaced with the output
of the functions `html-helper-timestamp-hook' if
`html-helper-do-write-file-hooks' is t")
(defvar html-helper-timestamp-end ""
"*End delimiter for timestamps.
Everything between `html-helper-timestamp-start' and
`html-helper-timestamp-end' will be deleted and replaced with the output
of the function `html-helper-insert-timestamp' if
`html-helper-do-write-file-hooks' is t")
;; control over what types of tags to load. By default, we load all the
;; ones we know of.
(defvar html-helper-types-to-install
'(anchor list header logical phys textel entity image head form)
"*List of tag types to install when html-helper-mode is first loaded.
If you want to not install some type of tag, override this variable.
Order is significant: menus go in this order.")
;; emacs18 detection.
(defvar html-helper-emacs18
(and (boundp 'emacs-version)
(or (and (boundp 'epoch::version) epoch::version)
(string-lessp emacs-version "19")))
"I'll do minimal emacs18 support, grumble.")
;;}}} end of user variables
(require 'tempo) ;essential part of html-helper-mode
(condition-case nil ;menu support, standard in emacs19
(require 'auc-menu) ;add-on for XEmacs. *why* does this
(error (require 'easymenu))) ;package have to have two names?
;;{{{ html-helper-mode-syntax-table and html-helper-mode-abbrev-table
;; emacs doesn't seem to be able to really handle SGML like syntax. In
;; particular, comments are a loss.
;; We do try this, though: give < and > matching semantics
(defvar html-helper-mode-syntax-table nil
"Syntax table for html-helper.")
(if html-helper-mode-syntax-table
()
(setq html-helper-mode-syntax-table (make-syntax-table text-mode-syntax-table))
(modify-syntax-entry ?< "(> " html-helper-mode-syntax-table)
(modify-syntax-entry ?> ")< " html-helper-mode-syntax-table)
(modify-syntax-entry ?\" ". " html-helper-mode-syntax-table)
(modify-syntax-entry ?\\ ". " html-helper-mode-syntax-table)
(modify-syntax-entry ?' "w " html-helper-mode-syntax-table))
(defvar html-helper-mode-abbrev-table nil
"Abbrev table used while in html-helper-mode.")
(define-abbrev-table 'html-helper-mode-abbrev-table ())
;;}}}
;;{{{ type based keymap and menu variable and function setup
;; Our basic keymap.
(defvar html-helper-mode-map (make-sparse-keymap)
"Keymap for html-helper")
(defvar html-helper-mode-menu nil
"Menu for html-helper. Clobbered and rebuilt by `html-helper-install-menu'")
;; html-helper-mode has a concept of "type" of tags. Each type is a
;; list of tags that all go together in one keymap and one menu.
;; Types can be added to the system after html-helper has been loaded,
;; briefly by doing html-helper-add-type-to-alist, then
;; html-helper-install-type, then html-helper-add-tag (for each tag)
;; then html-helper-rebuild-menu. See the mode documentation for more detail.
(defconst html-helper-type-alist nil
"Alist: type of tag -> keymap, keybinding, menu, menu string.
Add to this with `html-helper-add-type-to-alist'.")
;;{{{ accessor functions for html-helper-type-alist
(defun html-helper-keymap-for (type)
"Accessor function for alist: for type, return keymap or nil"
(nth 0 (cdr-safe (assq type html-helper-type-alist))))
(defun html-helper-key-for (type)
"Accessor function for alist: for type, return keybinding or nil"
(nth 1 (cdr-safe (assq type html-helper-type-alist))))
(defun html-helper-menu-for (type)
"Accessor function for alist: for type, return menu or nil"
(nth 2 (cdr-safe (assq type html-helper-type-alist))))
(defun html-helper-menu-string-for (type)
"Accessor function for alist: for type, return menustring or nil"
(nth 3 (cdr-safe (assq type html-helper-type-alist))))
(defun html-helper-normalized-menu-for (type)
"Helper function for building menus from submenus: add on string to menu."
(cons (html-helper-menu-string-for type)
(eval (html-helper-menu-for type))))
;;}}}
(defun html-helper-add-type-to-alist (type)
"Add a type specification to the alist.
The spec goes (type . (keymap-symbol keyprefix menu-symbol menu-string)).
See code for an example."
(setq html-helper-type-alist (cons type html-helper-type-alist)))
;; Here are the types provided by html-helper-mode.
(mapcar 'html-helper-add-type-to-alist
'((entity . (nil nil html-helper-entity-menu "Insert Character Entities"))
(textel . (nil nil html-helper-textel-menu "Insert Text Elements"))
(head . (html-helper-head-map "\C-c\C-b" html-helper-head-menu "Insert Structural Elements"))
(header . (html-helper-header-map "\C-c\C-t" html-helper-header-menu "Insert Headers"))
(anchor . (html-helper-anchor-map "\C-c\C-a" html-helper-anchor-menu "Insert Hyperlinks"))
(logical . (html-helper-logical-map "\C-c\C-s" html-helper-logical-menu "Insert Logical Styles"))
(phys . (html-helper-phys-map "\C-c\C-p" html-helper-phys-menu "Insert Physical Styles"))
(list . (html-helper-list-map "\C-c\C-l" html-helper-list-menu "Insert List Elements"))
(form . (html-helper-form-map "\C-c\C-f" html-helper-form-menu "Insert Form Elements"))
(image . (html-helper-image-map "\C-c\C-i" html-helper-image-menu "Insert Inlined Images"))))
;; Once html-helper-mode is aware of a type, it can then install the
;; type: arrange for keybindings, menus, etc.
(defconst html-helper-installed-types nil
"The types that have been installed (used when building menus).
There is no support for removing a type once it has been installed.")
(defun html-helper-install-type (type)
"Install a new tag type: add it to the keymap, menu structures, etc.
For this to work, the type must first have been added to the list of types
with html-helper-add-type-to-alist."
(setq html-helper-installed-types (cons type html-helper-installed-types))
(let ((keymap (html-helper-keymap-for type))
(key (html-helper-key-for type))
(menu (html-helper-menu-for type))
(menu-string (html-helper-menu-string-for type)))
(and key
(progn
(set keymap nil)
(define-prefix-command keymap)
(if html-helper-emacs18
(progn
(set keymap (make-sparse-keymap))
(define-key html-helper-mode-map key (eval keymap)))
(define-key html-helper-mode-map key keymap))))
(and menu
(progn
(set menu nil)))))
;; install the default types.
(mapcar 'html-helper-install-type html-helper-types-to-install)
;; special mode keys
(mapcar
(function (lambda (l) (define-key html-helper-mode-map (car l) (nth 1 l))))
'(("\M-\C-f" tempo-forward-mark)
("\M-\C-b" tempo-backward-mark)
("\M-\t" tempo-complete-tag)))
;; Extra commands that HTML helper supports that aren't insertions
(defvar html-helper-mode-functions-map nil
"Keymap for extra HTML mode functions")
(define-prefix-command 'html-helper-mode-functions-map)
(define-key html-helper-mode-map "\C-c\C-z"
'html-helper-mode-functions-map)
(define-key html-helper-mode-functions-map "t"
'html-helper-insert-timestamp-delimiter-at-point)
;; indentation keys - only rebind these if the user wants indentation
(if html-helper-never-indent
()
(define-key html-helper-mode-map "\t" 'html-helper-indent-command)
(define-key html-helper-mode-map "\C-m" 'newline-and-indent))
;; browse url stuff
(if (fboundp 'browse-url-of-file)
(define-key html-helper-mode-functions-map "v" 'browse-url-of-file))
(if (and (boundp 'browse-url-browser-function) (fboundp browse-url-browser-function))
(define-key html-helper-mode-functions-map "u" browse-url-browser-function))
;;}}}
;;{{{ html-helper-add-tag function for building basic tags
(defvar html-helper-tempo-tags nil
"List of tags used in completion.")
;; this while loop is awfully Cish
;; isn't there an emacs lisp function to do this?
(defun html-helper-string-to-symbol (input-string)
"Given a string, downcase it and replace spaces with -.
We use this to turn menu entries into good symbols for functions.
It's not entirely successful, but fortunately emacs lisp is forgiving."
(let* ((s (copy-sequence input-string))
(l (1- (length s))))
(while (> l 0)
(if (char-equal (aref s l) ?\ )
(aset s l ?\-))
(setq l (1- l)))
(concat "html-" (downcase s))))
(defun html-helper-add-tag (l)
"Add a new tag to html-helper-mode.
Builds a tempo-template for the tag and puts it into the
appropriate keymap if a key is requested. Format:
`(html-helper-add-tag '(type keybinding completion-tag menu-name template doc)'"
(let* ((type (car l))
(keymap (html-helper-keymap-for type))
(menu (html-helper-menu-for type))
(key (nth 1 l))
(completer (nth 2 l))
(name (nth 3 l))
(tag (nth 4 l))
(doc (nth 5 l))
(command (tempo-define-template (html-helper-string-to-symbol name)
tag completer doc
'html-helper-tempo-tags)))
(if (null (memq type html-helper-installed-types)) ;type loaded?
t ;no, do nothing.
(if (stringp key) ;bind key somewhere?
(if keymap ;special keymap?
(define-key (eval keymap) key command) ;t: bind to prefix
(define-key html-helper-mode-map key command));nil: bind to global
t)
(if menu ;is there a menu?
(set menu ;good, cons it in
(cons (vector name command t) (eval menu))))
)))
;; for backwards compatability
(fset 'html-helper-add-cookie 'html-helper-add-tag)
;;}}}
;;{{{ most of the HTML tags
;; These tags are an attempt to be HTML/2.0 compliant, with the exception
;; of container