Skip to content

Instantly share code, notes, and snippets.

@fukamachi
Last active December 22, 2015 10:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fukamachi/6462977 to your computer and use it in GitHub Desktop.
Save fukamachi/6462977 to your computer and use it in GitHub Desktop.
"Autoload" facility for Common Lisp which is similar to what Emacs Lisp has.
;; Macros in this file provides a "autoload" facility like Emacs Lisp has.
;; These are supposed to be used in a Lisp init file.
;;
;; Without those, you have to load all libraries you not sure whether they will be used or not.
;;
;; (ql:quickload :repl-utilities)
;; (use-package :repl-utilities)
;;
;; "Autoload" facility delays loading libraries until they are needed.
;; See the following 3 macros and their documentations.
(defmacro defun-autoload (name depends-on &key (package depends-on))
"Defines a function named `name' which loads `depends-on' library and replace itself by the real one.
Usage:
(defun-autoload de :repl-utilities)
"
`(defun ,name (&rest args)
#+quicklisp (ql:quickload ,depends-on)
#-quicklisp (asdf:load-system ,depends-on)
(let ((symbol (intern ,(string name) ,package)))
(shadowing-import symbol)
(apply (symbol-function symbol) args))))
(defmacro defmacro-autoload (name depends-on &key (package depends-on))
"Defines a macro named `name' which loads `depends-on' library and replace itself by the real one.
Usage:
(defmacro-autoload readme :repl-utilities)
"
`(defmacro ,name (&rest args)
`(progn
#+quicklisp (ql:quickload ,depends-on)
#-quicklisp (asdf:load-system ,depends-on)
(let ((symbol (intern ,,(string name) ,,package)))
(shadowing-import symbol)
(eval
(funcall (macro-function symbol)
(list* symbol ',args)
nil))))))
(defmacro define-autoload (name depends-on &key (package depends-on) documentation)
"Defines a macro named `name' which loads `depends-on' library and replace itself by the real one.
This macro is the same as `defun-autoload' and `defmacro-autoload' except that this can be used for both of functions and macros.
Usage:
(define-autoload de :repl-utilities)
(define-autoload readme :repl-utilities)
"
`(defmacro ,name (&rest args)
,documentation
`(progn
#+quicklisp (ql:quickload ,,depends-on)
#-quicklisp (asdf:load-system ,,depends-on)
(let ((symbol (intern ,,(string name) ,,package)))
(shadowing-import symbol ,,(symbol-package name))
(if (macro-function symbol)
(eval
(macroexpand-1 (list* symbol ',args)))
(apply (symbol-function symbol)
(list ,@args)))))))
(define-autoload de :repl-utilities)
(define-autoload readme :repl-utilities)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment