File: derived.el.html

GNU Emacs is already, in a sense, object oriented -- each object
(buffer) belongs to a class (major mode), and that class defines
the relationship between messages (input events) and methods
(commands) by means of a keymap.

The only thing missing is a good scheme of inheritance. It is possible to simulate a single level of inheritance with generous use of hooks and a bit of work -- sgml-mode, for example, also runs the hooks for text-mode, and keymaps can inherit from other keymaps
-- but generally, each major mode ends up reinventing the wheel.
Ideally, someone should redesign all of Emacs's major modes to follow a more conventional object-oriented system: when defining a new major mode, the user should need only to name the existing mode it is most similar to, then list the (few) differences.

In the mean time, this package offers most of the advantages of full inheritance with the existing major modes. The macro define-derived-mode allows the user to make a variant of an existing major mode, with its own keymap. The new mode will inherit the key bindings of its parent, and will, in fact, run its parent first every time it is called. For example, the commands

 (define-derived-mode hypertext-mode text-mode "Hypertext"
   "Major mode for hypertext.\\n\\n\\\\{hypertext-mode-map}"
   (setq case-fold-search nil))

 (define-key hypertext-mode-map [down-mouse-3] 'do-hyper-link)

will create a function hypertext-mode with its own (sparse) keymap hypertext-mode-map. The command M-x hypertext-mode will perform the following actions:

- run the command (text-mode) to get its default setup
- replace the current keymap with 'hypertext-mode-map,' which will
  inherit from 'text-mode-map'.
- replace the current syntax table with
  'hypertext-mode-syntax-table', which will borrow its defaults
  from the current text-mode-syntax-table.
- replace the current abbrev table with
  'hypertext-mode-abbrev-table', which will borrow its defaults
  from the current text-mode-abbrev table
- change the mode line to read "Hypertext"
- assign the value 'hypertext-mode' to the 'major-mode' variable
- run the body of commands provided in the macro -- in this case,
  set the local variable case-fold-search to nil.

The advantages of this system are threefold. First, text mode is untouched -- if you had added the new keystroke to text-mode-map, possibly using hooks, you would have added it to all text buffers
-- here, it appears only in hypertext buffers, where it makes
sense. Second, it is possible to build even further, and make a derived mode from a derived mode. The commands

  (define-derived-mode html-mode hypertext-mode "HTML")
  [various key definitions]

will add a new major mode for HTML with very little fuss.

Note also the function derived-mode-p which can tell if the current mode derives from another. In a hypertext-mode, buffer, for example,
(derived-mode-p 'text-mode) would return non-nil. This should always
be used in place of (eq major-mode 'text-mode).

Defined variables (0)

Defined functions (6)

define-derived-mode(CHILD PARENT NAME [DOCSTRING] [KEYWORD-ARGS...] &rest BODY)
derived-mode-abbrev-table-name(MODE)
derived-mode-hook-name(MODE)
derived-mode-make-docstring(PARENT CHILD &optional DOCSTRING SYNTAX ABBREV)
derived-mode-map-name(MODE)
derived-mode-syntax-table-name(MODE)

Defined faces (0)