Skip to content

Instantly share code, notes, and snippets.

Created February 19, 2015 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/171c6b67f901ea515e89 to your computer and use it in GitHub Desktop.
Save anonymous/171c6b67f901ea515e89 to your computer and use it in GitHub Desktop.
Load a library, or load then install a library.
(defmacro with-library (options &rest body)
"Try and require a library, if it's not found, install if
requested. If all goes well, then eval body.
(with-library '((require 'ag)
(package-naem 'ag))
(ag-specific-code))"
`(let ((symbol (cdr (assoc 'require ,options))))
(if (or (featurep symbol)
(require symbol nil t))
(progn ,@body)
(save-excursion
(progn
(let ((pkg (cdr (assoc 'package-name ,options))))
(if (not pkg)
(progn
(switch-to-buffer (get-buffer-create "*with-library errors*"))
(insert (format "Failed to load '%s'\n"
(symbol-name symbol))))
(package-install pkg)
(message (concat "Installed " (symbol-name pkg)))
(progn ,@body))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment