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

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

Re: test for presence of library


From: Will Parsons
Subject: Re: test for presence of library
Date: 21 Feb 2010 20:30:50 GMT
User-agent: slrn/0.9.9p1 (FreeBSD)

Pascal J. Bourguignon wrote:
> Harry Putnam <reader@newsguy.com> writes:
>
>> Can one test for the presence of a library before requiring it?
>>
>> I mean in the way one might test for the presence of a file in common
>> shell script; if [ -f /my/file ];then
>>                   blab blab
>>               fi
>>
>> Only here we are testing for the presence of a library somewhere in
>> the load path rather than a specific file location.
>>
>> The lisp equivalent of:
>>
>>  if some-lib 
>>  then
>>     (require some-lib)
>>  fi
>
>
> require does the test itself!
>
> C-h f require RET
>
>
> Now, assume that we wrote:
>
> (when (library-exists-p 'some-lib)
>    (require 'some-lib))
>
> and that just after your emacs executed (library-exists-p 'some-lib)
> and returned true, some other process would delete that some-lib.el
> file.  What would happen to your (require 'some-lib)?

I find it hard to imagine a scenario where I have to worry about random
processes deleting Emacs .el files behind my back.

> That is, basically, your above shell script with if [ -f /my/file ] is
> just WRONG!  If you see such tests in scripts, you are allowed to think
> poorly of their authors.

That's a pretty strong statement.  I use constructions like this in my Emacs
initialization file:

(when (locate-library "some-library")
  (require 'some-library)

Why not use the NOERROR argument to require?  Because it doesn't exist in the
XEmacs version of require, and although I don't use XEmacs a lot, it's nice
to keep my initialization file compatible between the two versions.

-- 
Will


reply via email to

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