From 9c1156dbf8593b37bbbc8f8e6d987b1ce748bd6d Mon Sep 17 00:00:00 2001 From: Edward Knyshov Date: Sun, 30 Aug 2015 11:23:49 +0300 Subject: [PATCH] error handling for use-package GitHub-reference: https://github.com/jwiegley/use-package/issues/246 --- lisp/use-package/use-package.el | 59 +++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/lisp/use-package/use-package.el b/lisp/use-package/use-package.el index 97b6ab30351..2b52054fd59 100644 --- a/lisp/use-package/use-package.el +++ b/lisp/use-package/use-package.el @@ -165,10 +165,10 @@ convert it to a string and return that." (if (stringp string-or-symbol) string-or-symbol (symbol-name string-or-symbol))) -(defun use-package-load-name (name &optional noerror) +(defun use-package-load-name (name) "Return a form which will load or require NAME depending on whether it's a string or symbol." - (if (stringp name) `(load ,name 'noerror) `(require ',name nil 'noerror))) + (if (stringp name) `(use-package-load ,name) `(use-package-require ',name))) (defun use-package-expand (name label form) "FORM is a list of forms, so `((foo))' if only `foo' is being called." @@ -292,6 +292,24 @@ This is in contrast to merely setting it to 0." (setq result (cons (car x) (cons (cdr x) result)))) result))) +(defun use-package-require (package) + "Require a package and handle any error from it." + (condition-case e + (require package) + (error (progn (use-package-require-error-handler package e) + nil)))) + +(defun use-package-load (name) + "Load a file and handle any error from it." + (condition-case e + (load name) + (error (progn (use-package-require-error-handler name e) + nil)))) + +(defun use-package-require-error-handler (package error) + "Main use package error handler for package loading." + (use-package-error (format "Could not load package.el: %s error: %s" package error))) + (defsubst use-package-concat (&rest elems) "Delete all empty lists from ELEMS (nil or (list nil)), and append them." (apply #'nconc (delete nil (delete (list nil) elems)))) @@ -697,21 +715,20 @@ works by binding the given key sequence to an invocation of this function for a particular keymap. The keymap is expected to be defined by the package. In this way, loading the package is deferred until the prefix key sequence is pressed." - (if (not (require package nil t)) - (use-package-error (format "Could not load package.el: %s" package)) - (if (and (boundp keymap-symbol) - (keymapp (symbol-value keymap-symbol))) - (let* ((kv (this-command-keys-vector)) - (key (key-description kv)) - (keymap (symbol-value keymap-symbol))) - (if override - (bind-key* key keymap) - (bind-key key keymap)) - (setq unread-command-events - (listify-key-sequence kv))) - (use-package-error - (format "use-package: package.el %s failed to define keymap %s" - package keymap-symbol))))) + (if (use-package-require package) + (if (and (boundp keymap-symbol) + (keymapp (symbol-value keymap-symbol))) + (let* ((kv (this-command-keys-vector)) + (key (key-description kv)) + (keymap (symbol-value keymap-symbol))) + (if override + (bind-key* key keymap) + (bind-key key keymap)) + (setq unread-command-events + (listify-key-sequence kv))) + (use-package-error + (format "use-package: package.el %s failed to define keymap %s" + package keymap-symbol))))) (defun use-package-handler/:bind-keymap (name keyword arg rest state &optional override) @@ -836,7 +853,7 @@ deferred until the prefix key sequence is pressed." ;; Load the package after a set amount of idle time, if the argument to ;; `:defer' was a number. (when (numberp arg) - `((run-with-idle-timer ,arg nil #'require ',(use-package-as-symbol name) nil t))) + `((run-with-idle-timer ,arg nil #'use-package-require ',(use-package-as-symbol name)))) ;; Since we deferring load, establish any necessary autoloads, and also ;; keep the byte-compiler happy. @@ -908,10 +925,8 @@ deferred until the prefix key sequence is pressed." (use-package-concat (list (use-package-load-name name)) config-body) - `((if (not ,(use-package-load-name name t)) - (ignore - (message (format "Could not load %s" ',name))) - ,@config-body))))))) + `((if (not (null ,(use-package-load-name name))) + ,@config-body))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -- 2.39.2