Skip to content

Instantly share code, notes, and snippets.

@10sr
Created November 28, 2012 06:20
Show Gist options
  • Select an option

  • Save 10sr/4159369 to your computer and use it in GitHub Desktop.

Select an option

Save 10sr/4159369 to your computer and use it in GitHub Desktop.
(defmacro lazy-load-eval (feature &optional functions &rest body)
"Define each FUNCTIONS to autoload from FEATURE.
FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
the function same as FEATURE is defined as autoloaded function. BODY is passed
to `eval-after-load'.
When this macro is evaluated, this returns the path to library if FEATURE
found, otherwise returns nil."
(let* ((libname (symbol-name (eval feature)))
(libpath (locate-library libname)))
(and libpath
`(progn
,@(mapcar (lambda (f)
(or (fboundp f)
`(autoload (quote ,f)
,libname
,(concat "Autoloaded function defined in \""
libpath
"\".")
t)))
(or (eval functions)
`(,(eval feature))))
(eval-after-load ,feature
(quote (progn
,@body)))
,libpath))))
(put 'lazy-load-eval 'lisp-indent-function 2)
(when (lazy-load-eval 'tetris nil
(message "Tetris loaded!"))
(message "Tetris found!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment