1 ;;; eldo.el --- Elisp Doc-to-Org converter
3 ;; Copyright (C) 2012, 2013 Bastien Guerry
5 ;; Author: Bastien Guerry <bzg at gnu dot org>
6 ;; Keywords: elisp, documentation, org
7 ;; Homepage: http://orgmode.org
9 ;; This file is not part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; M-x eldo-make-doc RET will create a file with documentation for hooks,
28 ;; commands and options, given a subset of *.el files. Use an .org file
29 ;; to write the documentation.
31 ;; This file is inspired by Nic Ferrier's wikidoc.el, with contributions
32 ;; from Eric Schulte and Thorsten Jolitz.
36 ;; - refactor and add customizable variables?
38 (defvar eldo-keymaps nil
)
40 (defvar eldo-git-raw-file
41 "http://orgmode.org/w/?p=org-mode.git;a=blob_plain;f=lisp/%s;hb=HEAD")
43 (defvar eldo-git-search-string
44 "http://orgmode.org/w/?p=org-mode.git&a=search&h=HEAD&st=commit&s=%s&sr=1")
46 (defvar eldo-file nil
)
48 (defun eldo-load (dir prefix
)
49 "Load Elisp files in DIR with PREFIX."
50 (if (string= (file-name-extension dir
) "el")
52 (dolist (file (directory-files
53 (expand-file-name dir
)
54 'full
(concat (or prefix
"") ".*\.el$")))
57 (defun eldo-make-doc (dir prefix
)
58 "Insert documentation in the current buffer."
59 (interactive "fDirectory or file: \nsPrefix: ")
60 (eldo-load dir prefix
)
61 (let (eldo-keymaps hks cmds opts vars funcs
)
64 (when (string-prefix-p prefix
(symbol-name a
))
65 (cond ((keymapp a
) (setq eldo-keymaps
(cons a eldo-keymaps
)))
66 ((string-match "hook$\\|functions$" (symbol-name a
))
67 (setq hks
(cons a hks
)))
68 ((commandp a
) (setq cmds
(cons a cmds
)))
69 ((get a
'custom-type
) (setq opts
(cons a opts
)))
70 ((fboundp a
) (setq funcs
(cons a funcs
)))
71 (t (setq vars
(cons a vars
)))))))
72 (find-file (or eldo-file
(read-file-name "Write to file: ")))
74 (eldo-write-hooks hks
)
75 (eldo-write-commands cmds
)
76 (eldo-write-options opts
)))
78 (defun eldo-write-hooks (hooks)
79 "Write hooks documentation in the current buffer."
81 (org-set-property "CUSTOM_ID" "hooks")
83 (unless (null (find-lisp-object-file-name h
'defvar
))
84 (insert "\n\n** " (symbol-name h
))
85 (let ((f (file-name-nondirectory (find-lisp-object-file-name h
'defvar
)))
86 (val (replace-regexp-in-string
88 (prin1-to-string (car (get h
'standard-value
)))))
89 (version (get h
'custom-version
))
90 (d (get h
'variable-documentation
)))
91 (if (> (length val
) 30) (setq val
(concat (substring val
0 30) "...")))
94 (if version
(format "- *Since:* Emacs version %s\n" version
) "")
95 (format (concat "- *In file:* [[" eldo-git-raw-file
"][%s]]\n") f f
)
96 (format (concat "- [[" eldo-git-search-string
97 "][Find modifications in git logs]]\n\n") (symbol-name h
)))
98 (when (stringp d
) (insert (eldo-make-verbatim d
)))))
99 (org-set-property "CUSTOM_ID" (symbol-name h
))
100 (goto-char (point-max))))
102 (defun eldo-write-commands (commands)
103 "Write commands documentaiton in the current buffer."
104 (insert "\n* Commands\n")
105 (org-set-property "CUSTOM_ID" "commands")
107 (when (find-lisp-object-file-name c
'defun
)
108 (let ((f (file-name-nondirectory (find-lisp-object-file-name c
'defun
)))
109 (key (mapconcat 'key-description
(where-is-internal c eldo-keymaps
) ", "))
110 (args (help-function-arglist c t
))
111 (d (documentation c
)))
112 (insert "\n** " (symbol-name c
) (if args
(format " =%s=\n" args
) "\n"))
113 (org-set-property "CUSTOM_ID" (symbol-name c
))
115 (if (and key
(not (string= key
""))) (format "\n- *Access:* ~%s~" key
) "")
116 (format (concat "\n- *In file:* [[" eldo-git-raw-file
"][%s]]\n") f f
)
117 (format (concat "- [[" eldo-git-search-string
118 "][Find modifications in git logs]]\n\n") (symbol-name c
)))
119 (when (stringp d
) (insert (eldo-make-verbatim d
))))
120 (goto-char (point-max)))))
122 (defun eldo-write-options (options)
123 "Write options documentation in the current buffer."
124 (insert "\n* Options\n")
125 (org-set-property "CUSTOM_ID" "options")
127 (when (find-lisp-object-file-name o
'defvar
)
128 (insert "\n\n** " (symbol-name o
))
129 (let ((f (file-name-nondirectory (find-lisp-object-file-name o
'defvar
)))
130 (val (replace-regexp-in-string
132 (prin1-to-string (car (get o
'standard-value
)))))
133 (version (get o
'custom-version
))
134 (type (prin1-to-string (get o
'custom-type
)))
135 (d (get o
'variable-documentation
)))
136 (if (> (length val
) 30) (setq val
(concat (substring val
0 30) "...")))
137 (if (> (length type
) 30) (setq type
(concat (substring type
0 30) "...")))
140 (format "- *Type:* %s\n" type
)
141 (if version
(format "- *Since:* Emacs version %s\n" version
) "")
142 (format (concat "- *In file:* [[" eldo-git-raw-file
"][%s]]\n") f f
)
143 (format (concat "- [[" eldo-git-search-string
144 "][Find modifications in git logs]]\n\n") (symbol-name o
)))
145 (when (stringp d
) (insert (eldo-make-verbatim d
)))))
146 (org-set-property "CUSTOM_ID" (symbol-name o
))
147 (goto-char (point-max))))
149 (defun eldo-make-verbatim (string)
150 "Convert STRING to a verbatim region in Org."
151 (let ((str (split-string string
"\n")))
152 (mapconcat (lambda(s) (concat ": " s
)) str
"\n")))