Fix some tests
[org-mode.git] / mk / eldo.el
blob41b590badfc34eea3a0b07c2ffb30365989a54b5
1 ;;; eldo.el --- Elisp Doc-to-Org converter
3 ;; Copyright (C) 2012--2014 Bastien Guerry
4 ;;
5 ;; Author: Bastien Guerry <bzg@gnu.org>
6 ;; Keywords: elisp, documentation, org
7 ;; Homepage: http://orgmode.org
8 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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.
34 ;;; Todo:
36 ;; - refactor and add customizable variables?
38 (defvar eldo-keymaps nil)
40 (defvar eldo-git-raw-file
41 "http://orgmode.org/cgit.cgi/org-mode.git/plain/lisp/%s")
43 (defvar eldo-git-search-string
44 "http://orgmode.org/cgit.cgi/org-mode.git/log/?qt=grep&q=%s")
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")
51 (load-file dir)
52 (dolist (file (directory-files
53 (expand-file-name dir)
54 'full (concat (or prefix "") ".*\.el$")))
55 (load-file file))))
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)
62 (mapatoms
63 (lambda(a)
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: ")))
73 (org-mode)
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."
80 (insert "* Hooks\n")
81 (org-set-property "CUSTOM_ID" "hooks")
82 (dolist (h 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
87 "\n" "\\\\n"
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) "...")))
92 (insert
93 " =" val "=\n"
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")
106 (dolist (c 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))
114 (insert
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")
126 (dolist (o 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
131 "\n" "\\\\n"
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) "...")))
138 (insert
139 " =" val "=\n\n"
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")))