Function: evil-define-keymap

evil-define-keymap is a macro defined in evil-core.el.

Signature

(evil-define-keymap KEYMAP DOC [[KEY VAL]...] BODY...)

Documentation

Define a keymap KEYMAP listed in evil-mode-map-alist.

That means it will have precedence over regular keymaps.

DOC is the documentation for the variable. BODY, if specified, is executed after toggling the mode. Optional keyword arguments may be specified before the body code:

:mode VAR Mode variable. If unspecified, the variable
                is based on the keymap name.
:local BOOLEAN Whether the keymap should be buffer-local, that is,
                reinitialized for each buffer.
:func BOOLEAN Create a toggle function even if BODY is empty.

Source Code

;; Defined in ~/.emacs.d/elpa/evil-20251108.138/evil-core.el
(defmacro evil-define-keymap (keymap doc &rest body)
  "Define a keymap KEYMAP listed in `evil-mode-map-alist'.
That means it will have precedence over regular keymaps.

DOC is the documentation for the variable. BODY, if specified,
is executed after toggling the mode. Optional keyword arguments
may be specified before the body code:

:mode VAR       Mode variable. If unspecified, the variable
                is based on the keymap name.
:local BOOLEAN  Whether the keymap should be buffer-local, that is,
                reinitialized for each buffer.
:func BOOLEAN   Create a toggle function even if BODY is empty.

\(fn KEYMAP DOC [[KEY VAL]...] BODY...)"
  (declare (indent defun)
           (doc-string 2)
           (debug (&define name
                           [&optional stringp]
                           [&rest [keywordp sexp]]
                           def-body)))
  (let ((func t)
        arg intercept key local mode overriding)
    (while (keywordp (car-safe body))
      (setq key (pop body)
            arg (pop body))
      (cond
       ((eq key :mode) (setq mode arg))
       ((eq key :local) (setq local arg))
       ((eq key :func) (setq func arg))
       ((eq key :intercept) (setq intercept arg))
       ((eq key :overriding) (setq overriding arg))))
    (setq mode (or mode
                   (intern (replace-regexp-in-string
                            "\\(?:-\\(?:mode-\\)?\\(?:key\\)?map\\)?$"
                            "-mode"
                            (symbol-name keymap)))))
    `(progn
       (defvar ,keymap ,(unless local '(make-sparse-keymap)))
       (unless (get ',keymap 'variable-documentation)
         (put ',keymap 'variable-documentation ,doc))
       (defvar ,mode nil)
       (unless (get ',mode 'variable-documentation)
         (put ',mode 'variable-documentation ,doc))
       (make-variable-buffer-local ',mode)
       (put ',mode 'permanent-local t)
       (when ,intercept
         (evil-make-intercept-map ,keymap))
       (when ,overriding
         (evil-make-overriding-map ,keymap))
       ,@(if local
             `((make-variable-buffer-local ',keymap)
               (put ',keymap 'permanent-local t)
               (evil--add-to-alist evil-local-keymaps-alist ',mode ',keymap))
           `((evil--add-to-alist evil-global-keymaps-alist ',mode ',keymap)
             (evil--add-to-alist evil-mode-map-alist ',mode ,keymap)))
       ,(when (or body func)
          `(defun ,mode (&optional arg)
             ,@(when doc `(,doc))
             (interactive)
             (cond
              ((numberp arg) (setq ,mode (> arg 0)))
              (t (setq ,mode (not ,mode))))
             ,@body))
       ',keymap)))