Function: cl-block

cl-block is an autoloaded macro defined in cl-macs.el.gz.

Signature

(cl-block NAME &rest BODY)

Documentation

Define a lexically-scoped block named NAME.

NAME may be any symbol. Code inside the BODY forms can call cl-return-from to jump prematurely out of the block. This differs from catch and throw
in two respects: First, the NAME is an unevaluated symbol rather than a
quoted symbol or other form; and second, NAME is lexically rather than
dynamically scoped: Only references to it within BODY will work. These
references may appear inside macro expansions, but not inside functions called from BODY.

View in manual

Probably introduced at or before Emacs version 31.1.

Aliases

block (obsolete since 27.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-macs.el.gz
;;; Blocks and exits.

;;;###autoload
(defmacro cl-block (name &rest body)
  "Define a lexically-scoped block named NAME.
NAME may be any symbol.  Code inside the BODY forms can call `cl-return-from'
to jump prematurely out of the block.  This differs from `catch' and `throw'
in two respects:  First, the NAME is an unevaluated symbol rather than a
quoted symbol or other form; and second, NAME is lexically rather than
dynamically scoped:  Only references to it within BODY will work.  These
references may appear inside macro expansions, but not inside functions
called from BODY."
  (declare (indent 1) (debug (symbolp body)))
  (if (cl--safe-expr-p `(progn ,@body)) `(progn ,@body)
    (let ((var (intern (format "--cl-block-%s--" name))))
      `(cl--block-wrapper
        ;; Build a unique "tag" in the form of a fresh cons.
        ;; We include `var' in the cons, just in case it help debugging.
        (let ((,var (cons ',var nil)))
         (catch ,var
           ,@body))))))