unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Fabrice Niessen" <fgdmjuzqrjgh-geNee64TY+gS+FvcfC7Uqw@public.gmane.org>
To: help-gnu-emacs-mXXj517/zsQ@public.gmane.org
Subject: Re: require a package only if present
Date: Mon, 13 Jul 2009 11:12:55 +0200	[thread overview]
Message-ID: <87vdlxeyjs.fsf@mundaneum.com> (raw)
In-Reply-To: slrnh5i1q0.1rf.oudeis@isis.thalatta.eme

Hello,

Will Parsons wrote:
> Edward O'Connor wrote:
>>> I am trying to use the same copy of .emacs across various machines. Some,
>>> however, don't have certain packages. So I need to replace lines like:
>>>
>>> (require 'tex-site)
>>>
>>> with code that checks whether tex-site is present and only loads it if
>>> there. How can I do that? Any pointers welcome.
>>
>> (require 'tex-site nil t)
>
> I don't believe that works for XEmacs (which may be irrelevant for the OP),
> so I use instead:
>
> (when (locate-library "lib-name")
>   (require 'lib-name))

I'm using the following code, both compatible with GNU Emacs and XEmacs:

--8<---------------cut here---------------start------------->8---
(defvar missing-packages-list nil
  "List of packages that `try-require' can't find.")

;; attempt to load a feature/library, failing silently
(defun try-require (feature)
  "Attempt to load a library or module. Return true if the
library given as argument is successfully loaded. If not, instead
of an error, just add the package to a list of missing packages."
  (condition-case err
      ;; protected form
      (progn
        (message "Checking for library `%s'..." feature)
        (if (stringp feature)
            (load-library feature)
          (require feature))
        (message "Checking for library `%s'... Found" feature))
    ;; error handler
    (file-error  ; condition
     (progn
       (message "Checking for library `%s'... Missing" feature)
       (add-to-list 'missing-packages-list feature))
     nil)))
--8<---------------cut here---------------end--------------->8---

and, for every package I wanna load if present:

--8<---------------cut here---------------start------------->8---
(try-require 'tex-site)
--8<---------------cut here---------------end--------------->8---

(for example).

See http://www.mygooglest.com/fni/dot-emacs.html for more examples.

Fabrice

_________________________________________________________________________
Fabrice Niessen
Search the Web with "My Google Search Tools" on http://www.MyGooglest.com


      reply	other threads:[~2009-07-13  9:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-11 17:44 require a package only if present Andrei Alexandrescu
2009-07-11 18:46 ` Edward O'Connor
     [not found] ` <mailman.2314.1247338027.2239.help-gnu-emacs@gnu.org>
2009-07-11 21:14   ` Andrei Alexandrescu
2009-07-11 21:42   ` Will Parsons
2009-07-13  9:12     ` Fabrice Niessen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vdlxeyjs.fsf@mundaneum.com \
    --to=fgdmjuzqrjgh-genee64ty+gs+fvcfc7uqw@public.gmane.org \
    --cc=help-gnu-emacs-mXXj517/zsQ@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).