Function: define-keymap

define-keymap is a byte-compiled function defined in keymap.el.gz.

Signature

(define-keymap &key FULL PARENT SUPPRESS NAME PREFIX KEYMAP &rest [KEY DEFINITION]...)

Documentation

Create a new keymap and define KEY/DEFINITION pairs as key bindings.

Return the new keymap.

Options can be given as keywords before the KEY/DEFINITION pairs. Available keywords are:

:full If non-nil, create a chartable alist (see make-keymap).
             If nil (i.e., the default), create a sparse keymap (see
             make-sparse-keymap).

:suppress If non-nil, the keymap will be suppressed (see suppress-keymap).
             If nodigits, treat digits like other chars.

:parent If non-nil, this should be a keymap to use as the parent
             (see set-keymap-parent).

:keymap If non-nil, instead of creating a new keymap, the given keymap
             will be destructively modified instead.

:name If non-nil, this should be a string to use as the menu for
             the keymap in case you use it as a menu with x-popup-menu.

:prefix If non-nil, this should be a symbol to be used as a prefix
             command (see define-prefix-command). If this is the case,
             this symbol is returned instead of the map itself.

KEY/DEFINITION pairs are as KEY and DEF in keymap-set. KEY can also be the special symbol :menu, in which case DEFINITION should be a MENU form as accepted by easy-menu-define.

Other relevant functions are documented in the keymaps group.

View in manual

Probably introduced at or before Emacs version 29.1.

Shortdoc

;; keymaps
(define-keymap "C-c C-c" #'quit-buffer)

Source Code

;; Defined in /usr/src/emacs/lisp/keymap.el.gz
(defun define-keymap (&rest definitions)
  "Create a new keymap and define KEY/DEFINITION pairs as key bindings.
Return the new keymap.

Options can be given as keywords before the KEY/DEFINITION
pairs.  Available keywords are:

:full      If non-nil, create a chartable alist (see `make-keymap').
             If nil (i.e., the default), create a sparse keymap (see
             `make-sparse-keymap').

:suppress  If non-nil, the keymap will be suppressed (see `suppress-keymap').
             If `nodigits', treat digits like other chars.

:parent    If non-nil, this should be a keymap to use as the parent
             (see `set-keymap-parent').

:keymap    If non-nil, instead of creating a new keymap, the given keymap
             will be destructively modified instead.

:name      If non-nil, this should be a string to use as the menu for
             the keymap in case you use it as a menu with `x-popup-menu'.

:prefix    If non-nil, this should be a symbol to be used as a prefix
             command (see `define-prefix-command').  If this is the case,
             this symbol is returned instead of the map itself.

KEY/DEFINITION pairs are as KEY and DEF in `keymap-set'.  KEY can
also be the special symbol `:menu', in which case DEFINITION
should be a MENU form as accepted by `easy-menu-define'.

\(fn &key FULL PARENT SUPPRESS NAME PREFIX KEYMAP &rest [KEY DEFINITION]...)"
  (declare (indent defun)
           (compiler-macro define-keymap--compile))
  (let (full suppress parent name prefix keymap)
    ;; Handle keywords.
    (while (and definitions
                (keywordp (car definitions))
                (not (eq (car definitions) :menu)))
      (let ((keyword (pop definitions)))
        (unless definitions
          (error "Missing keyword value for %s" keyword))
        (let ((value (pop definitions)))
          (pcase keyword
            (:full (setq full value))
            (:keymap (setq keymap value))
            (:parent (setq parent value))
            (:suppress (setq suppress value))
            (:name (setq name value))
            (:prefix (setq prefix value))
            (_ (error "Invalid keyword: %s" keyword))))))

    (when (and prefix
               (or full parent suppress keymap))
      (error "A prefix keymap can't be defined with :full/:parent/:suppress/:keymap keywords"))

    (when (and keymap full)
      (error "Invalid combination: :keymap with :full"))

    (let ((keymap (cond
                   (keymap keymap)
                   (prefix (define-prefix-command prefix nil name))
                   (full (make-keymap name))
                   (t (make-sparse-keymap name))))
          seen-keys)
      (when suppress
        (suppress-keymap keymap (eq suppress 'nodigits)))
      (when parent
        (set-keymap-parent keymap parent))

      ;; Do the bindings.
      (while definitions
        (let ((key (pop definitions)))
          (unless definitions
            (error "Uneven number of key/definition pairs"))
          (let ((def (pop definitions)))
            (if (eq key :menu)
                (easy-menu-define nil keymap "" def)
              (when (member key seen-keys)
                ;; Since the keys can be computed dynamically, it can
                ;; very well happen that we get duplicate definitions
                ;; due to some unfortunate configuration rather than
                ;; due to an actual bug.  While such duplicates are
                ;; not desirable, they shouldn't prevent the users
                ;; from getting their job done.
                (message "Duplicate definition for key: %S %s" key keymap))
              (push key seen-keys)
              (keymap-set keymap key def)))))
      keymap)))