about summary refs log tree commit diff
path: root/users/tazjin/elisp-deps/deps.el
blob: 954d71cfbaddacd768944d818c6ce6db7edb6def (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;; Visualise the internal structure of an Emacs Lisp file using
;; Graphviz.
;;
;; Entry point is the function `edeps-analyse-file'.

(require 'map)

(defun edeps-read-defs (file-name)
  "Stupidly read all definitions from an Emacs Lisp file. This only
considers top-level forms, where the first element of the form is
a symbol whose name contains the string `def', and where the
second element is a symbol.

Returns a hashmap of all these symbols with the remaining forms
in their bodies."

  (with-temp-buffer
    (insert-file-contents file-name)
    (goto-char (point-min))

    (let ((symbols (make-hash-table)))
      (condition-case _err
          (while t
            (let ((form (read (current-buffer))))
              (when (and (listp form)
                         (symbolp (car form))
                         (string-match "def" (symbol-name (car form)))
                         (symbolp (cadr form)))
                (when (and (map-contains-key symbols (cadr form))
                           ;; generic methods have multiple definitions
                           (not (eq (car form) 'cl-defmethod)))
                  (error "Duplicate symbol: %s" (symbol-name (cadr form))))

                (map-put! symbols (cadr form)
                          (cons (car form) (cddr form))))))
        (end-of-file symbols)))))

(defun edeps-analyse-structure (symbols)
  "Analyse the internal structure of the symbols found by
edeps-read-defs, and return a hashmap with the results of the
analysis. The hashmap uses the symbols as keys, "
  (let ((deps (make-hash-table)))
    (map-do
     (lambda (sym val)
       (dolist (expr (flatten-list (cdr val)))
         (when (map-contains-key symbols expr)
           (map-put! deps expr (cons sym (ht-get deps expr))))))
     symbols)
    deps))

(defun edeps-graph-deps (symbols deps)
  (with-temp-buffer
    (insert "digraph edeps {\n")

    ;; List all symbols first
    (insert "  subgraph {\n")
    (map-do
     (lambda (sym val)
       (insert "    " (format "\"%s\" [label=\"%s\\n(%s)\"];\n" sym sym (car val))))
     symbols)
    (insert "  }\n\n")

    ;; Then drop all the edges in there ..
    (insert "  subgraph {\n")
    (map-do
     (lambda (sym deps)
       (dolist (dep deps)
         (insert "    " (format "\"%s\" -> \"%s\";\n" dep sym))))
     deps)
    (insert "  }\n")

    (insert "}\n")
    (buffer-string)))

(defun edeps-analyse-file (infile outfile)
  "Produces a dot-graph in OUTFILE from an internal structural
analysis of INFILE. This can be graphed using the graphviz
package."
  (let* ((symbols (edeps-read-defs infile))
         (deps (edeps-analyse-structure symbols)))
    (with-temp-buffer
      (insert (edeps-graph-deps symbols deps))
      (write-file outfile))))