Emacs Lisp Script, Count Function Frequency πŸ“œ

By Xah Lee. Date: . Last updated: .
;; -*- coding: utf-8; lexical-binding: t; -*-

;; file name: elisp_count_func_frequency.el
;; run it by
;; emacs --script elisp_count_func_frequency.el
;; 

;; Emacs Lisp Script, Count Function Frequency πŸ“œ
;; http://xahlee.info/emacs/emacs/elisp_count_function_frequency.html
;; created: 2022-05-28
;; version: 2025-08-31

(require 'map)

(defvar x-lisp-dir)
(setq x-lisp-dir "c:/Users/xah/bin/emacs-30.1/share/emacs/30.1/lisp/")

(defvar x-output-file-name)
(setq x-output-file-name "x-elisp-func-freq.txt")

(set-default-coding-systems 'utf-8-unix)

(defun xah-report-func-freq (zinputdir)
  "go thru emacs lisp source code dir, compute function usage frequency.
version 2022-05-28"
  (interactive)
  (let (
        xfuncname
        (xhash (make-hash-table :test 'equal))
        xkeycount xFnSortedList)
    ;; walk the directory. for each file, open it. find text like γ€Œ(some-thing」 put it in a hashtable
    (mapc
     (lambda (fPath)
       (with-temp-buffer
         (insert-file-contents fPath)
         (goto-char (point-min))
         (while (re-search-forward "(+\\([^ )\n]+\\)" nil t)
           (setq xfuncname (match-string 1))
           (setq xkeycount (gethash xfuncname xhash))
           (if xkeycount
               (puthash xfuncname (1+ xkeycount) xhash)
             (puthash xfuncname 1 xhash)))))
     (directory-files zinputdir t "\\.el$"))
    (setq xFnSortedList (sort
                         (map-filter (lambda (key _) (let ((xsym (intern-soft key))) (if xsym (fboundp xsym) nil))) xhash)
                         :key 'cdr :reverse t))

    (with-temp-file x-output-file-name
      (mapc
       (lambda (x)
         (insert (format "γ€Œ%s」 %s\n" (car x) (cdr x))))
       xFnSortedList))))

(xah-report-func-freq x-lisp-dir)

(find-file x-output-file-name)

Elisp function frequency