4

What's the idiomatic Emacs way to soft-require a feature, then use that feature if it's available?

The require function allows “soft require” by specifying a non-nil third argument:

(require 'projectile nil 'missing-okay)

What's the best way to then use that feature to do more things, only if the feature is now present?

(if succeeds
 (require 'projectile nil 'missing-okay)
 (do-some-stuff-that-needs-projectile))

or

(require 'projectile nil 'missing-okay)
(when feature-is-available 'projectile
 (do-some-stuff-that-needs-projectile))

or something else?

1 Answer 1

8

You just test with featurep:

(require 'projectile nil t)
(when (featurep 'projectile)
 (do-some-stuff-that-needs-projectile))

or you use that the require returns nil if it is not possible to load:

(when (require 'projectile nil t)
  (do-some-stuff-that-needs-projectile)
2
  • 1
    +1 for mentioning the latter - the best idiom for this.
    – Drew
    Jan 19, 2017 at 14:50
  • Both of those work exactly as I want, thank you.
    – bignose
    Jan 20, 2017 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.