Skip to content

Instantly share code, notes, and snippets.

@cmoore
Created February 21, 2012 06:59
Show Gist options
  • Save cmoore/1874568 to your computer and use it in GitHub Desktop.
Save cmoore/1874568 to your computer and use it in GitHub Desktop.
;; If the file exists, execute the block.
;;
;;
;; My SEKRET SETTINGS OMG
;;(ext*with-file-exists-f "~/Dropbox/.github.config.el"
;; (load "~/Dropbox/.github.config.el"))
;;
(defmacro ext-with-file-exists (file block)
"Execute the form if the file exists."
`(and (file-exists-p ,file)
(progn ,block)))
;;
;; Same as above, but with an argument passed to
;; the block that is the name of the file in question.
;;
;; My SEKRET SETTINGS OMG
;;(ext*with-file-exists-f "~/Dropbox/.github.config.el"
;; (load the-file))
;;
(defmacro ext-with-file-exists-f (file &rest block)
"Like with-file-exists, except that it makes a (let) variable available called 'the-file' with the name of the checked file."
`(let ((the-file ,file))
(and (file-exists-p ,file)
(funcall (lambda ()
,@block)))))
;;
;;
;; HEY! Hey, you!
;; Do you need to do some really stupid shit?
;; Well, then you should wrap it in this!
;;
;;
;; http://curiousprogrammer.wordpress.com/2009/06/08/error-handling-in-emacs-lisp/
;;
(defmacro ext-safe-wrap (fn &rest clean-up)
`(unwind-protect
(let (retval)
(condition-case ex
(setq retval (progn ,fn))
('error
(message (format "Caught exception: [%s]" ex))
(setq retval (cons 'exception (list ex)))))
retval)
,@clean-up))
;;
;; package.el helpers...
;;
(unless (featurep 'package)
(progn
(require 'package)
(package-initialize)))
;;
;; If pkg is installed already, don't do anything.
;; Otherwise, install it.
;;
(defun ext-package-ensure (pkg)
(or (progn
(ignore-errors (require pkg))
(ext-package-get-version-for-name pkg))
(ignore-errors
(and (package-install pkg)
(message (symbol-name pkg))))))
(defun ext-package-delete (pkg)
(let ((v (ext-package-get-version-for-name pkg)))
(and v (package-delete pkg v))))
(defun ext-package-get-version-for-name (pkg)
(let ((lx (assq pkg package-alist)))
(and lx
(package-version-join
(package-desc-vers (cdr lx))))))
;;
;;
;; Maybe in addition I should
;; do an (or (featurep ..)
;; (boundp package-alist)
;; instead of (and (featurep ..)
;;
;;
(defmacro ext-with-has-package-el (block)
`(and (featurep 'package)
,block))
(defun ext-package-what-packages ()
(mapcar (lambda (x)
(symbol-name (car x))) package-alist))
(provide 'clm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment