[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: require a package only if present
From: |
Fabrice Niessen |
Subject: |
Re: require a package only if present |
Date: |
Mon, 13 Jul 2009 11:12:55 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.94 (gnu/linux) |
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