11

I'm changing up the API of my package pretty frequently, but company-mode pulls every defined symbol (as it should) in its completions. I don't want to accidentally use an unbound name, so how can I unbind all variables and functions that start with, say, my-package-? After this, I'll simply be able to load-file again.

Sean Allred
  • 6,861
  • 16
  • 85

2 Answers2

10

Call unload-feature to undefine all symbols that were defined as part of loading an Elisp source or byte-compiled file. Make sure that your file ends by calling provide at the end. This assumes that you loaded the file with one of the load functions or via require, it won't undefined symbols defined by C-M-x (eval-defun) or similar mechanisms.

If you really want to unbind symbols based on their name rather than based on the package that defined them, you can use mapatoms to iterate over all symbols.

(mapatoms (lambda (symbol)
            (if (string-prefix-p "foo-" (symbol-name symbol))
                (unintern symbol))))
  • Useful, but I don't believe this will be effective with interactive development as I describe. I'm one level deeper than the `feature` system; I'm working only with functions and variables without any explicit grouping. I have not bothered evaluating `(provide 'my-package)`. – Sean Allred Oct 30 '14 at 00:50
  • 2
    @SeanAllred I can't think of a good reason not to use `provide`. But anyway, if you really want to do things the dirty way, see my edit. – Gilles 'SO- stop being evil' Oct 30 '14 at 00:57
  • Oh, I love `provide`, I just don't think it would work in this case. Do functions remember what file they were defined in if they were evaluated interactively? – Sean Allred Oct 30 '14 at 00:59
  • 2
    `unload-feature` has no effect if you evaluate code without loading it from a file (that has `provide`). – Drew Oct 30 '14 at 01:00
  • 2
    @SeanAllred No, functions only remember where they were loaded from if they were loaded as part of loading a whole file. – Gilles 'SO- stop being evil' Oct 30 '14 at 01:10
7
  • Use makunbound to remove the use of a symbol as a dynamic variable (i.e., to void its symbol-value).

  • Use fmakunbound to remove the use of a symbol as a function (i.e., to void its symbol-function).

  • Use mapatoms to iterate over all symbols. Act on each whose symbol-name is matched by the particular prefix you are interested in: call both makunbound and fmakunbound on it.

  • You do not need to unintern the symbol, unless you are also using completion against symbols themselves, and not just over their use as variables or functions. But if you do want to completely remove a symbol then use unintern.

Drew
  • 75,699
  • 9
  • 109
  • 225