Skip to content

Instantly share code, notes, and snippets.

@jorgenschaefer
Created June 10, 2014 12:21
Show Gist options
  • Save jorgenschaefer/84bc04d947e83b2b9a01 to your computer and use it in GitHub Desktop.
Save jorgenschaefer/84bc04d947e83b2b9a01 to your computer and use it in GitHub Desktop.
Trivial elisp code to create a basic dot file call graph
;; Save to foo.dot, run dot -Tpng foo.dot -o foo.png, done
(defun eldot ()
(interactive)
(let ((nodes (make-hash-table :test #'eq))
(expr nil))
(catch 'done
(save-excursion
(goto-char (point-min))
(while t
(condition-case err
(setq expr (read (current-buffer)))
(error (throw 'done t)))
(when (and (consp expr)
(symbolp (car expr))
(string-match "^\\(defun\\|define-minor-mode\\)"
(symbol-name (car expr)))
(symbolp (cadr expr)))
(let ((calls (make-hash-table :test #'eq)))
(puthash (cadr expr) calls nodes)
(eldot-add-callsites calls (cddr expr)))))))
(with-current-buffer (get-buffer-create "*Dot*")
(erase-buffer)
(insert "graph eldot {\n")
(maphash (lambda (function calls)
(maphash (lambda (call _)
(when (gethash call nodes)
(insert (format " \"%s\" -- \"%s\";\n"
function call))))
calls))
nodes)
(insert "\n}\n")
(display-buffer (current-buffer)))))
(defun eldot-add-callsites (calls body)
(while (consp body)
(let ((expr (car body)))
(when (and (consp expr)
(symbolp (car expr)))
(puthash (car expr) t calls))
(when (consp expr)
(eldot-add-callsites calls expr)))
(setq body (cdr body))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment