1

When you install clojure-mode via package-install, you can then open any .clj file and automatically enter the mode.

And this works for every subsequent Emacs launch ; Yet, no (require 'clojure-mode) (or a custom definition, nothing) has been inserted in the init file.

I carefully reviewed the clojure-mode.el file, and found no clue as to how this works. Yes, the main interactive functions and the major modes definitions are autoloaded, and the common clojure file extensions are added to auto-mode-alist:

;;;###autoload
(progn
  (add-to-list 'auto-mode-alist
               '("\\.\\(clj\\|dtm\\|edn\\)\\'" . clojure-mode))
  (add-to-list 'auto-mode-alist '("\\.cljc\\'" . clojurec-mode))
  (add-to-list 'auto-mode-alist '("\\.cljx\\'" . clojurex-mode))
  (add-to-list 'auto-mode-alist '("\\.cljs\\'" . clojurescript-mode)))

But this is not enough for the mode I'm working on. I found no way of loading it without explicitly requiring it in the init file..?

What is this magic?

3

You discovered most of the answer yourself. When you install clojure-mode, you also get a file clojure-mode-autoloads.el. That file besides adding the entries you mentioned to auto-mode-alist, also defines autoloads for the functions clojure-mode, clojurec-mode, etc.

Once the autoload file has been loaded (which normally package-initialize does on start up), the first time you open a file with extension .clj, say, the auto-mode-alist variable is consulted. That says that the clojure-mode function should be run and the attempt to run it causes clojure-mode.el to be loaded (because of the autoload on the clojure-mode function).

You can create a similar file of autoloads for your mode (look into update-file-autoloads and update-directory-autoloads to automate this process) and then require that autoload file from your init file (since package-initialize won't do that for you).

  • Wait, did you say "and then require that autoload file from your init file"? Closure-mode doesn't do that. The idea is to understand how this works without touching the init file..? – yPhil Jul 30 '17 at 9:48
  • Thanks for you answer, Omar ; I don't get this "autoloads in a separate file" business anyway, do you know of some documentation to read about it? I mean, it can't work differently from "normal" plain autoloads, right? – yPhil Jul 30 '17 at 10:07
  • Wait, it works now. I had an error in my extension file regexp. No require, just two ;;;###autoloads: The major mode definition, and the add to auto-mode-alist. Happy camper :) – yPhil Jul 30 '17 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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