Function: defun

defun is a macro defined in byte-run.el.gz.

Signature

(defun NAME ARGLIST [DOCSTRING] [DECL] [INTERACTIVE] BODY...)

Documentation

Define NAME as a function.

The definition is (lambda ARGLIST [DOCSTRING] [INTERACTIVE] BODY...). DECL is a declaration, optional, of the form (declare DECLS...) where DECLS is a list of elements of the form (PROP . VALUES). These are interpreted according to defun-declarations-alist. INTERACTIVE is an optional interactive specification. The return value is undefined.

View in manual

Probably introduced at or before Emacs version 17.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/byte-run.el.gz
(function-put 'defmacro 'autoload-macro 'expand) ; Since we cannot `declare' it

;; Now that we defined defmacro we can use it!
(defmacro defun (name arglist &rest body)
  "Define NAME as a function.
The definition is (lambda ARGLIST [DOCSTRING] [INTERACTIVE] BODY...).
DECL is a declaration, optional, of the form (declare DECLS...) where
DECLS is a list of elements of the form (PROP . VALUES).  These are
interpreted according to `defun-declarations-alist'.
INTERACTIVE is an optional `interactive' specification.
The return value is undefined.

\(fn NAME ARGLIST [DOCSTRING] [DECL] [INTERACTIVE] BODY...)"
  (declare (doc-string 3) (indent 2)
           ;; Expand to defalias on autoload gen
           (autoload-macro expand))
  (or name (error "Cannot define '%s' as a function" name))
  (if (null
       (and (listp arglist)
            (null (delq t (mapcar #'symbolp arglist)))))
      (error "Malformed arglist: %s" arglist))
  (let* ((parse (byte-run--parse-body body t))
         (docstring (nth 0 parse))
         (declare-form (nth 1 parse))
         (interactive-form (nth 2 parse))
         (body (nth 3 parse))
         (warnings (nth 4 parse))
         (declarations
          (and declare-form (byte-run--parse-declarations
                             name arglist (cdr declare-form) 'defun
                             defun-declarations-alist))))
    (setq body (nconc warnings body))
    (setq body (nconc (cdr declarations) body))
    (if interactive-form
        (setq body (cons interactive-form body)))
    (if docstring
        (setq body (cons docstring body)))
    (if (null body)
        (setq body '(nil)))
    (let ((def (list 'defalias
                     (list 'quote name)
                     (list 'function
                           (cons 'lambda
                                 (cons arglist body))))))
      (if declarations
          (cons 'prog1 (cons def (car declarations)))
        def))))