Skip to content

Instantly share code, notes, and snippets.

@kurohuku
Created October 15, 2010 05:36
Show Gist options
  • Save kurohuku/627686 to your computer and use it in GitHub Desktop.
Save kurohuku/627686 to your computer and use it in GitHub Desktop.
labels
(defmacro dynamic-labels
((&rest definitions) &body body)
(let ((olds (mapcar #'(lambda (x)
(declare (ignore x))
(gensym))
definitions)))
`(let ,(mapcar
#'(lambda (sym def)
`(,sym ,(and (fboundp (car def)) (symbol-function (car def)))))
olds
definitions)
,@(mapcar
#'(lambda (def)
`(,(if (generic-function-p (car def))
`cl:defmethod
`cl:defun)
,@def))
definitions)
(unwind-protect
(progn
,@body)
,@(mapcar
#'(lambda (old def)
`(if ,old
(setf (symbol-function ',(car def))
,old)
(fmakunbound ',(car def))))
olds
definitions)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment