11.8 Conditional Compilation

There will be times when you want certain code to be compiled only when a certain condition holds. This is particularly the case when maintaining Emacs packages; to keep the package compatible with older versions of Emacs you may need to use a function or variable which has become obsolete in the current version of Emacs.

You could just use a conditional form to select the old or new form at run time, but this tends to output annoying warning messages about the obsolete function/variable. For such situations, the macro static-if comes in handy. It is patterned after the special form if (see Conditionals).

To use this facility for an older version of Emacs, copy the source for static-if from the Emacs source file lisp/subr.el into your package.

Macro: static-if condition then-form else-forms...

Test condition at macro-expansion time. If its value is non-nil, expand the macro to then-form, otherwise expand it to else-forms enclosed in a progn. else-forms may be empty.

Macro: static-when condition body...

Test condition at macro-expansion time. If its value is non-nil, expand the macro to evaluate all body forms sequentially and return the value of the last one, or nil if there are none.

Macro: static-unless condition body...

Test condition at macro-expansion time. If its value is nil, expand the macro to evaluate all body forms sequentially and return the value of the last one, or nil if there are none.

Here is an example of its use from CC Mode, which prevents a defadvice form being compiled in newer versions of Emacs:

(static-if (boundp 'comment-line-break-function)
    (progn)
  (defvar c-inside-line-break-advice nil)
  (defadvice indent-new-comment-line (around c-line-break-advice
                                             activate preactivate)
    "Call `c-indent-new-comment-line' if in CC Mode."
    (if (or c-inside-line-break-advice
            (not c-buffer-is-cc-mode))
        ad-do-it
      (let ((c-inside-line-break-advice t))
        (c-indent-new-comment-line (ad-get-arg 0))))))