3

Is it possible to get a list of functions defined in an emacs-lisp file? I found this sort of related answer: How do I get a list of Emacs lisp non-interactive functions?, but it involves a map over all of the atoms defined, not just what is in a file.

5 Answers 5

5

If the file in question has already been loaded, then you can modify the code in the question you link to filter out the symbols defined in other files:

(let ((funclist ()))
  (mapatoms
   (lambda (x)
     (when (and (fboundp x)                     ; does x name a function?
                (let ((f (symbol-file x)))
                  (and f (string= (file-name-base f) "my-file.el"))))
       (push x funclist))))
  funclist)

If the file has not been loaded, you would have to scan it with scan-sexps and find defun forms.

You might, however, prefer to use etags or imenu instead of scanning the file yourself.

2
  • 1
    that is close, I had to change (and f (string= (file-name-base f) "my-file.el")))) to (and f (string= (file-name-base f) "my-file")))) That does work, although it is pretty slow. I will look into imenu. It looks like a good start. Oct 12, 2014 at 23:25
  • @JohnKitchin That should have been file-name-nondirectory.
    – vfclists
    Sep 26, 2021 at 10:27
3

Maybe a faster way is to look for the file in load-history, which will then give you the list of variables and functions defined therein.

1
  • Also interesting. (cdr (assoc "/Users/jkitchin/Dropbox/kitchingroup/jmax/jmax-bibtex.el" load-history )) leads to something like this. #+RESULTS: | jmax-bibtex-journal-abbreviations | (defun . jmax-bibtex-generate-longtitles) | (defun . jmax-bibtex-generate-shorttitles) | (defun . jmax-stringify-journal-name) | (defun . jmax-set-journal-string) | jmax-nonascii-latex-replacements | (defun . jmax-replace-nonascii) | jmax-lower-case-words | (defun . jmax-title-case-article) | (defun . jmax-sentence-case-article) |` Oct 15, 2014 at 20:49
0

Not sure if your asking for a non interactive approach.

With M-x occur ENT (defun.* ENT you get a buffer with more or less all function-definitions found in (current-buffer).

1
  • This has all the same problems as Mirzhan Irkegulov's answer (which was likewise based on a regexp search for defun).
    – phils
    Mar 13, 2018 at 1:23
0

use Imenu M-g i it will list all functions and variables in buffer

-1

The quick&dirty way: extract all defuns via regex. It works instantly on a buffer with 5000 lines.

(-map 'cadr (s-match-strings-all "defun \\(.*?\\) " (buffer-string)))

This returns a list of function names that are defined via defun in the current open buffer. buffer-string returns content of a current buffer in a string, -map and s-match-string-all are taken from dash and s third party libraries (their GitHub pages explain how to install them), cadr returns a 2nd element of a list.

-map is analogous to Emacs built-in mapcar, it applies a function to each element of a list and returns a new list, s-match-string-all returns all possible regex matches in a string, parentheses in a regex denote a group (read more how to form Emacs regular expressions from EmacsWiki).

If you run it in eval-expression (Alt+:), it will just throw it into echo area, but that's not what you need. So below are variations that work with custom buffer or file. with-current-buffer allows to temporarily switch a buffer, while some code does actions inside it, f-read is a file reading function form another third-party library f.

(defun list-defined-functions (buffer)
  (with-current-buffer buffer
    (-map 'cadr (s-match-strings-all "defun \\(.*?\\) "
                                     (buffer-string)))))

(defun list-defined-functions-in-file (file)
  (-map 'cadr (s-match-strings-all "defun \\(.*?\\) "
                                   (f-read file))))

Read Emacs Lisp manual and try to come up with whatever is useful for you.

2
  • 1
    Good luck obtaining the functions defined by macros with that approach (e.g. virtually all mode functions, for starters).
    – phils
    Feb 24, 2015 at 10:04
  • 1
    And in any case, using regular expressions to parse s-expressions is crazy.
    – phils
    Feb 24, 2015 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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