Function: evil-define-command

evil-define-command is a macro defined in evil-common.el.

Signature

(evil-define-command COMMAND (ARGS...) DOC [[KEY VALUE]...] BODY...)

Documentation

Define a command COMMAND.

Source Code

;; Defined in ~/.emacs.d/elpa/evil-20251108.138/evil-common.el
;;; Command properties

(defmacro evil-define-command (command &rest body)
  "Define a command COMMAND.

\(fn COMMAND (ARGS...) DOC [[KEY VALUE]...] BODY...)"
  (declare (indent defun)
           (doc-string 3)
           (debug (&define name
                           [&optional lambda-list]
                           [&optional stringp]
                           [&rest keywordp sexp]
                           [&optional ("interactive" [&rest form])]
                           def-body)))
  (let ((interactive '(interactive))
        args doc doc-form keys)
    ;; collect arguments
    (when (listp (car body))
      (setq args (pop body)))
    ;; collect docstring
    (cond ((stringp (car body)) (setq doc (pop body)))
          ((eq (car-safe (car body)) #'format) (setq doc-form (pop body))))
    ;; collect keywords
    (while (keywordp (car body))
      (let* ((key (pop body))
             (arg (pop body)))
        (setq keys (plist-put keys key arg)))) ; TODO: add keyword check
    ;; collect `interactive' form
    (when (eq (caar body) 'interactive)
      (cl-destructuring-bind (form . attrs)
          (apply #'evil-interactive-form (cdr (pop body)))
        (setq interactive `(interactive ,form)
              keys (evil-concat-plists keys attrs))))
    `(progn
       ;; the compiler does not recognize `defun' inside `let'
       ,(when (and command body)
          `(defun ,command ,args
             ,@(when doc `(,doc))
             ,interactive
             ,@body))
       ,(when (and command doc-form)
          `(put ',command 'function-documentation ,doc-form))
       ;; set command properties for symbol or lambda function
       (let ((func ,(if (and (null command) body)
                        `(lambda ,args
                           ,interactive
                           ,@body)
                      `#',command)))
         (apply #'evil-set-command-properties func ',keys)
         func))))