Major Mode Skeleton

These are the steps involved in defining a major mode.

  1. Choose a name. The name for our mode is quip.

  2. Create a file called name.el to contain the code for the mode.

  3. Define a variable called name-mode-hook. This will contain the user's hook functions to execute when entering the mode.

    (defvar quip-mode-hook nil
      "*List of functions to call when entering Quip mode.")
  4. If appropriate, define a mode-specific keymap (see Keymaps later in this chapter). Put it in a variable called name-mode-map. Create a mode's keymap like this:

    (defvar name-mode-map nil
      "Keymap for name major mode.")
    (if name-mode-map
        nil
      (setq name-mode-map (make-keymap))
      (define-key name-mode-map  keysequence  command)
      ...)

    Instead of make-keymap, you could use make-sparse-keymap, which is better suited to keymaps that contain only a few keybindings.

  5. If appropriate, define a mode-specific syntax table (see the section called Minor Adjustments in Chapter 7). Put it in a variable named name-mode-syntax-table.

  6. If appropriate, define a mode-specific abbrev table. Put it in a variable named name-mode-abbrev-table.

  7. Define a command named name-mode. This is the major mode command, and it takes no arguments (unlike a minor mode command, which takes one optional argument). When executed, it should cause the current buffer to enter name-mode by performing the following steps:

    1. It must call kill-all-local-variables, which removes the definitions for all buffer-local variables. This effectively turns off whatever modes,

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.