Is there a way to define a macro in an emacs-lisp file in such a way that the macro would not be visible anywhere else once the file is read and evaluated?
Kind of like a temporary macro that goes out of scope at the end of a file?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
You can always unbind the macro when the file ends. As far as I know, Emacs Lisp doesn't have to worry about asynchronous complexities; the flow of execution is purely linear. Therefore, you can do this:
(defmacro my-test-macro (secret sauce recipe) ;; super secret stuff here ;; wouldn't want it to LEAK out ;; and get stolen by some other files ) ;; [...] blabitty blah blah [...] (fmakunbound 'my-test-macro)
The other solution you should look at (which is less hacky) uses
cl-macrolet, which is basically a
let for macros.
(eval-when-compile (require 'cl-lib)) (cl-macrolet ((my-test-macro (secret sauce recipie) ;; super secret stuff here ;; wouldn't want it to LEAK out ;; and get stolen by some other files )) ;; [...] blabitty blah blah [...] )
No doubt, this solution is cleaner, although you pay for it by adding another level of indentation. Also, the macro is dynamically bound and could in theory be run asynchronously (although Emacs is a long way from multi-threading).