unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* prevent function execution on Emacs startup if not connected to internet
@ 2014-04-18  7:27 Brady Trainor
  2014-04-18  7:50 ` Brady Trainor
       [not found] ` <mailman.19815.1397807453.10748.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 6+ messages in thread
From: Brady Trainor @ 2014-04-18  7:27 UTC (permalink / raw)
  To: help-gnu-emacs

Hi, I have the following lines of code in my my init files:

if an uninstalled package is not in the list, refresh list
#+BEGIN_SRC emacs-lisp
(setq n 0)                                  ; set n as 0
(dolist (pkg pkgs-2b-present)               ; for each pkg in list
   (unless (or                               ; unless
            (package-installed-p pkg)        ; pkg is installed or
            (assoc pkg                       ; pkg is in the archive list
                   package-archive-contents))
     (setq n (+ n 1))))                      ; add one to n
(when (> n 0)                               ; if n > 0,
   (package-refresh-contents))               ; refresh packages
#+END_SRC

install any uninstalled packages that are in the list
#+BEGIN_SRC emacs-lisp
(dolist (pkg pkgs-2b-present)               ; for each pkg in list
   (when                                     ; if pkg is
       (and
        (not (package-installed-p pkg))      ; not installed
        (assoc pkg package-archive-contents) ; and in the archive list
        )
     (package-install pkg)))                 ; then install it
#+END_SRC

I think the `package-refresh-contents' or the `package-install' 
functions will create an error and prevent my init files from finishing 
loading.

I would guess that I am not the first one to have this problem, or 
someone can tell me what would fix this.


Brady




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: prevent function execution on Emacs startup if not connected to internet
  2014-04-18  7:27 prevent function execution on Emacs startup if not connected to internet Brady Trainor
@ 2014-04-18  7:50 ` Brady Trainor
       [not found] ` <mailman.19815.1397807453.10748.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 6+ messages in thread
From: Brady Trainor @ 2014-04-18  7:50 UTC (permalink / raw)
  To: help-gnu-emacs

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.


Brady





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: prevent function execution on Emacs startup if not connected to internet
       [not found] ` <mailman.19815.1397807453.10748.help-gnu-emacs@gnu.org>
@ 2014-04-18 12:13   ` Pascal J. Bourguignon
  2014-04-22  4:45     ` Brady Trainor
  2014-04-24  2:22     ` Brady Trainor
  0 siblings, 2 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2014-04-18 12:13 UTC (permalink / raw)
  To: help-gnu-emacs

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 !"


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: prevent function execution on Emacs startup if not connected to internet
  2014-04-18 12:13   ` Pascal J. Bourguignon
@ 2014-04-22  4:45     ` Brady Trainor
  2014-04-24  2:22     ` Brady Trainor
  1 sibling, 0 replies; 6+ messages in thread
From: Brady Trainor @ 2014-04-22  4:45 UTC (permalink / raw)
  To: help-gnu-emacs

On 04/18/2014 05:13 AM, Pascal J. Bourguignon wrote:

> If you just want to ignore the error, you can use ignore-errors.


I will wait until I can recreate the error, then try this first, to at 
least try to narrow it down.


Thank you for your help.


Brady





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: prevent function execution on Emacs startup if not connected to internet
  2014-04-18 12:13   ` Pascal J. Bourguignon
  2014-04-22  4:45     ` Brady Trainor
@ 2014-04-24  2:22     ` Brady Trainor
  2014-04-24 17:09       ` John Mastro
  1 sibling, 1 reply; 6+ messages in thread
From: Brady Trainor @ 2014-04-24  2:22 UTC (permalink / raw)
  To: help-gnu-emacs

On 4/18/2014 5:13 AM, Pascal J. Bourguignon wrote:

>    (ignore-errors (package-refresh-contents)))

>      (ignore-errors (package-install pkg))))

So I was able to recreate my issue. Your method worked at these 
functions, but then the issue propagates to `require's for packages not 
installed. (Every once in a while I switch OS', and simultaneously am 
sitting down somewhere without an internet connection.)

I imagine putting `ignore-errors' at every `require' would be bad form.

Maybe I can start up Emacs with an option to ignore errors? "In 
pseudocode",

     emacs --ignore-errors

Or even better, maybe I can have in my init file, a line to check for a 
connection, and if there is none, prompt me to allow a global 
`ignore-errors'. Or no prompt, but give a message that it occurred.


Brady


PS

Thank you for the code improvement, I did not notice it the first time,

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








^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: prevent function execution on Emacs startup if not connected to internet
  2014-04-24  2:22     ` Brady Trainor
@ 2014-04-24 17:09       ` John Mastro
  0 siblings, 0 replies; 6+ messages in thread
From: John Mastro @ 2014-04-24 17:09 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Brady,

Brady Trainor <algebrat@uw.edu> wrote:
> So I was able to recreate my issue. Your method worked at these
 > functions, but then the issue propagates to `require's for packages
> not installed. (Every once in a while I switch OS', and simultaneously
> am sitting down somewhere without an internet connection.)

You could make use of the optional "noerror" argument to `require'. For
instance, (require 'the-package nil t).

Of course, there will still be errors later if you try to use things
from packages that weren't loaded.

Best regards,

John


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-04-24 17:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-18  7:27 prevent function execution on Emacs startup if not connected to internet Brady Trainor
2014-04-18  7:50 ` Brady Trainor
     [not found] ` <mailman.19815.1397807453.10748.help-gnu-emacs@gnu.org>
2014-04-18 12:13   ` Pascal J. Bourguignon
2014-04-22  4:45     ` Brady Trainor
2014-04-24  2:22     ` Brady Trainor
2014-04-24 17:09       ` John Mastro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).