I wrote an elisp function and a few defvar‘s that I use to interactively add boost unit test macros. Since it is used with Boost, it will only be needed in c++-mode. Currently it is in my init file so it is always loaded in every mode.
How do I make it so this functionality is only available in c++-mode, i.e. as if I added it to the major mode itself.
If you want the code you wrote to only be loaded when needed, you can put it into a file of its own (say
boost-unit-test.el), and from your.emacsyou could add:Note, this assumes the file is in your
load-pathand has(provide 'boost-unit-test)as one of the lines.Now, once this file is loaded, those variables and those commands are defined globally, and that’s just the way Emacs is. This is no different than if you left the code in your
.emacsfile. And, either way, it will only be loaded/evaluated once (unless you force it to load multiple times – but you have to work to do that).