help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: prevent function execution on Emacs startup if not connected to inte


From: Pascal J. Bourguignon
Subject: Re: prevent function execution on Emacs startup if not connected to internet
Date: Fri, 18 Apr 2014 14:13:56 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Brady Trainor <algebrat@uw.edu> writes:

> On 4/18/2014 12:27 AM, Brady Trainor wrote:
>> Hi, I have the following lines of code in my my init files:
> ...
>> I think the `package-refresh-contents' or the `package-install'
>> functions will create an error and prevent my init files from finishing
>> loading.
> ...
>> Brady
>
>
> I forgot to say that I get the error when I start Emacs without an
> internet connection, which makes sense, but I was hoping a simple
> remedy would be obvious to someone.


In general, when errors are generated according to external state (such
as "not connected to the Internet" whatever that may mean), there is not
point in testing for it, you should just do it, and handle the error.

The reason is that between the test and the do it, the situation may
change, so the test may be valid (you are "connected"), and when you do
it it's not valid anymore (oops you are "disconnected" now), so testing
doesn't help.


To handle errors, I use handler-case:

(defmacro handler-case (expression &rest clauses)
  "Common-Lisp
IMPLEMENTATION: The clause variable symbols are substituted by one single
                condition-case variable symbol.  This may cause problems
                if the same symbol is used as data or if it's a dynamic 
                variable.
"
  (let* ((var (gensym))
         (neclause (assoc :NO-ERROR clauses))
         (nell     (cadr neclause))
         (nebody   (cddr neclause))
         (handlers (mapcar (lambda (clause)
                             (let ((typespec (car clause))
                                   (clausvar (cadr clause))
                                   (body     (cddr clause)))
                               (cons (if (and (consp typespec)
                                              (eq 'or (car typespec)))
                                         (cdr typespec) 
                                         typespec)
                                     (if (null clausvar)
                                         body
                                         (subst  var (car clausvar) body)))))
                           (remove neclause clauses))))
    (if neclause
        `(condition-case ,var
             (multiple-value-bind ,nell ,expression ,@nebody)
           ,@handlers)
        `(condition-case ,var
             ,expression
           ,@handlers))))

For example, you could deal differently with program-errors than with
network errors.

If you just want to ignore the error, you can use ignore-errors.  Or you
can also write emacs-lisp instead of Common Lisp, and use directly
condition-case.

#+BEGIN_SRC emacs-lisp

(when (notevery (lambda (pkg (or (package-installed-p pkg)
                              (assoc pkg package-archive-contents))))
                pkgs-2b-present)
  (ignore-errors (package-refresh-contents)))

(dolist (pkg pkgs-2b-present)
  (when (and (not (package-installed-p pkg))
             (assoc pkg package-archive-contents))
    (ignore-errors (package-install pkg))))

#+END_SRC


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ?  C'est le moment d'acheter !"


reply via email to

[Prev in Thread] Current Thread [Next in Thread]