1

With a package that defines a minor mode which calls an external function at run-time.

Is it reasonable to move this:
(require 'my-runtime-dependency) in the body of the code which enables the minor mode? or should it always be at the top of the file?

e.g:

(eval-when-compile
  ;; Quiet warnings.
  (require 'my-runtime-dependency))

;; --- Snip

(defun my-mode-enable ()
  (require 'my-runtime-dependency)
  ;; body.
  )

(defun my-mode-disable ()
  ;; body.
  )

;;;###autoload
(define-minor-mode my-mode
  "Toggle `my-mode' in the current buffer."
  :global nil
  (cond
    (my-mode (my-mode-enable))
    (t       (my-mode-disable))))

1 Answer 1

1

In general it's fine to require things inside functions, assuming the function in question is not necessarily going to be used, and the rest of your library doesn't need that dependency.

Your example wouldn't necessitate that, though. Based on the autoload cookie, you're intending that your library doesn't get loaded at all until my-mode is invoked, at which point you're wanting to load the dependency immediately -- so there's no obvious reason why you wouldn't put the require at the top level.

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.