0

I feel like this question is super basic, but I haven't been able to figure out how to automatically make a simple interactive command available in an Emacs session...

This is in ~/random/exploration/exploration.el.

;;; Code:

;;;###autoload
(defun exploration ()
  "a test package"
  (interactive)
  (message "hi"))

(provide 'exploration)
;;; exploration.el ends here

This is in init.el:

(add-to-list 'load-path (expand-file-name  "~/random/exploration"))

exploration isn't available via M-x though. I have to do M-: (require 'exploration) before it appears.

How would I make this command available automatically, like plugins do? I've been poring over the docs for load-path and autoload but can't figure out how to make this happen.

I want to do this so I can put other functions in exploration.el and have them only available after the user first does M-x exploration. That implies that exploration needs to be autoloaded.

Comments on any redundancies in what I've done here, or tips on how I could have debugged this on my own would also be welcome.

user1953221
  • 419
  • 1
  • 3
  • 9
  • 1
    see http://stackoverflow.com/questions/4189159/emacs23-elisp-how-to-properly-autoload-this-library – npostavs Sep 12 '15 at 15:19

3 Answers3

1

You have to either load the file instead of adding it to the load-path or you need to put (require 'exploration) to your init.el file after adding the folder to the load-path.

ayckoster
  • 6,707
  • 6
  • 32
  • 45
1

Use autoload function:

(autoload 'exploration "exploration")

If you don't modify load-path, you need an absolute path as the second argument.

Alternatively, install your script as a package, either through MELPA (assuming you can get it there), or locally with package-install-file. This will take care about autoloads for you.

1

The ;;;###autloload comment is just a comment. When Emacs is built (or a suitably intelligent package manager installs your code as a package) it generates an actual autoload, but outside of that, you need to do it yourself.

(autoload 'exploration "exploration" nil t)

Specify an explicit path in the second parameter and you won't actually need the load-path manipulation to accomplish what you describe.

See also https://www.gnu.org/software/emacs/manual/html_node/eintr/Autoload.html

tripleee
  • 175,061
  • 34
  • 275
  • 318