5

In Elisp packages, we sometimes want to use a feature only if the user has installed another package without specifying that package as a dependency for our own. E.g.

(when (require 'markdown-mode nil 'noerror)
  (markdown-mode))

This, however, causes a byte-compiler warning:

Warning: the function markdown-mode is not known to be defined.

What's the right way to address this? with-no-warnings is one option, but it needs to be wrapped around every such use. Is there a better way?

2 Answers 2

5

IMHO, the right way is to ignore it. ;-)

But the standard Emacs answer is to use declare-function - see (elisp) Declaring Functions.

All you need to do is add a declare-function statement before the first use of the function in question:

 (declare-function gud-find-c-expr "gud.el" nil)

This says that gud-find-c-expr is defined in gud.el (the .el can be omitted). The compiler takes for granted that that file really defines the function, and does not check.

The optional third argument specifies the argument list of gud-find-c-expr. In this case, it takes no arguments (nil is different from not specifying a value). In other cases, this might be something like (file &optional overwrite). You don’t have to specify the argument list, but if you do the byte compiler can check that the calls match the declaration.

3

Here's how I'd do it:

(require 'markdown-mode nil 'noerror)
...
...
  ...
  (if (fboundp 'markdown-mode)
      ..use markdown-mode..)

Your Answer

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

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