-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
scimax-org-eldoc.el
233 lines (204 loc) · 6.24 KB
/
scimax-org-eldoc.el
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
;;; scimax-org-eldoc.el --- Generate org-mode elisp documentation
;;; Commentary:
;;
;;; Code:
(defun scimax-org-eldoc-org-escape (s)
"Escape some org syntax in S.
Headlines and # in the first position."
(if (stringp s)
(progn
(setq s (replace-regexp-in-string "^*" ",*" s))
(setq s (replace-regexp-in-string "^#" ",#" s))
s)
(format "%s" s)))
(defun scimax-org-eldoc (library)
"Generate documentation for LIBRARY in an org buffer.
LIBRARY must be loaded before running this function."
(interactive
(list (completing-read
"Library to generate org-doc for: "
(-flatten
(delq nil
(mapcar
(lambda (x)
(delq nil
(mapcar
(lambda (y)
(when (and (consp y)
(eq (car y) 'provide))
(cdr y)))
x)))
load-history))))))
(let* ((lib-file (locate-library library))
;; these are the things defined in the library
(elements (cdr
(assoc
(locate-library library)
load-history)))
;; variables
(vars (-filter 'symbolp elements))
;; things the library requires
(requires
(mapcar 'cdr
(-filter (lambda (x)
(and (consp x)
(eq 'require (car x))))
elements)))
;; functions defined in the library
(funcs (mapcar
'cdr
(-filter (lambda (x)
(and (consp x)
(eq 'defun (car x))))
elements))))
(switch-to-buffer "*org-doc*")
(erase-buffer)
(insert (format "#+TITLE: Documentation for %s
#+OPTIONS: toc:nil
\\maketitle
\\tableofcontents
%s
" library (cond
;; regular lisp file
((string= "el" (file-name-extension lib-file))
(format "Source code: [[file:%s][%s]]" lib-file library))
;; compiled file. these are not easy to read so we try plain el file
((and (string= "elc" (file-name-extension lib-file))
(file-exists-p
(concat (file-name-sans-extension lib-file) ".el")))
(format "Source code: [[file:%s][%s]]"
(concat (file-name-sans-extension lib-file) ".el")
library))
;; catch anything we cannot figure out
(t
(format "Source code: file:%s" lib-file)))))
(insert "* Requires\n\n")
;; insert link to generate a scimax-org-eldoc buffer for each require
(dolist (req requires)
(insert (format "- [[elisp:(scimax-org-eldoc \"%s\")][%s]]\n" req req)))
(insert "* Custom Variables\n\n")
(dolist (var (sort (-filter 'custom-variable-p vars) 'string-lessp))
(insert (format "** %s
Documentation: %s
Value:
%S\n\n"
var
(scimax-org-eldoc-org-escape
(documentation-property var 'variable-documentation))
(scimax-org-eldoc-org-escape (symbol-value var)))))
(insert "* Regular Variables\n\n")
(dolist (var (sort (-filter (lambda (x)
(not (custom-variable-p x)))
vars)
'string-lessp))
(insert (format "** %s\n" var))
(insert (scimax-org-eldoc-org-escape
(format "Documentation: %s
Value:
%s\n\n"
(documentation-property var 'variable-documentation)
(symbol-value var)))))
(insert "* Interactive Functions\n\n")
(dolist (func (sort (-filter 'commandp funcs) 'string-lessp))
(insert (format "** %s %s
Documentation: %s
Code:
#+BEGIN_SRC emacs-lisp
%s
#+END_SRC
"
func
(or (help-function-arglist func) "")
(scimax-org-eldoc-org-escape (documentation func))
;; code defining the function
(let ((code (save-window-excursion
;; we do not have c-source, so check if func
;; is defined in a c file here.
(if (and (stringp (find-lisp-object-file-name
func
(symbol-function func)))
(string= "c"
(file-name-extension
(find-lisp-object-file-name
func
(symbol-function func)))))
(symbol-function func)
;;else
(condition-case nil
(let ((bp (find-function-noselect
func t)))
(set-buffer (car bp))
(goto-char (cdr bp))
(when (sexp-at-point)
(mark-sexp)
(buffer-substring (point) (mark))))
(error func))))))
(scimax-org-eldoc-org-escape code)))))
(insert "* Non-interactive Functions\n\n")
(dolist (func (sort (-filter (lambda (x) (not (commandp x)))
funcs)
'string-lessp))
(insert (format "** %s %s
Documentation: %s
Code:
#+BEGIN_SRC emacs-lisp
%s
#+END_SRC
"
func
(or (help-function-arglist func) "")
;; escape some org-syntax
(scimax-org-eldoc-org-escape (documentation func))
;; code defining the function
(let ((code (save-window-excursion
;; we do not have c-source, so check if func
;; is defined in a c file here.
(if
(and (stringp (find-lisp-object-file-name
func
(symbol-function func)))
(string= "c"
(file-name-extension
(find-lisp-object-file-name
func
(symbol-function func)))))
(symbol-function func)
;;else
(condition-case nil
(let ((bp (find-function-noselect func t)))
(set-buffer (car bp))
(goto-char (cdr bp))
(when (sexp-at-point)
(mark-sexp)
(buffer-substring (point) (mark))))
(error func))))))
;; escape org syntax
(scimax-org-eldoc-org-escape code)))))
(org-mode)
;; replace `' with links to describe function or variable, unless
;; they are in a code block, then leave them alone.
(goto-char (point-min))
(while (re-search-forward "`\\([^' ]*\\)'" nil t)
(let ((result (match-string 1))
(bg (match-beginning 1))
(end (match-end 1)))
;; checking for code block changes match data, so
;; we save it here.
(unless (save-match-data
(eq 'src-block (car (org-element-at-point))))
(cond
;; known function
((fboundp (intern result))
(cl--set-buffer-substring bg end (format "[[elisp:(describe-function '%s)][%s]]"
result result)))
;; known variable
((boundp (intern result))
(cl--set-buffer-substring bg end (format "[[elisp:(describe-variable '%s)][%s]]"
result result)))
;; unknown quoted thing, just return it back
(t
result)))))
;; finally jump to Requires section
(org-link-open-from-string "[[*Requires]]")))
(provide 'scimax-org-eldoc)
;;; scimax-org-eldoc.el ends here