]> code.delx.au - gnu-emacs-elpa/blob - doc/yas-doc-helper.el
fix exporting of doc strings for org manual
[gnu-emacs-elpa] / doc / yas-doc-helper.el
1 ;;; yas-doc-helper.el --- Help generate documentation for YASnippet
2
3 ;; Copyright (C) 2012 João Távora
4
5 ;; Author: João Távora <joaotavora@gmail.com>
6 ;; Keywords: convenience
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; Some functions to help generate YASnippet docs
24
25 ;;; Code:
26
27 (defun yas--document-symbol (symbol level)
28 (flet ((concat-lines (&rest lines)
29 (mapconcat #'identity lines "\n")))
30 (let* ((stars (make-string level ?*))
31 (heading (cond ((fboundp symbol)
32 (format "%s =%s= (%s)"
33 stars
34 symbol
35 (mapconcat #'symbol-name
36 (help-function-arglist symbol t) " ")))
37 (t
38 (format "%s =%s=\n" stars symbol))))
39 (after-heading
40 (concat-lines ":PROPERTIES:"
41 (format ":CUSTOM_ID: %s" symbol)
42 ":END:"))
43 (body (or (cond ((boundp symbol)
44 (documentation-property symbol 'variable-documentation t))
45 ((fboundp symbol)
46 (documentation-property symbol 'function-documentation t))
47 (t
48 (format "*WARNING*: no symbol named =%s=" symbol)))
49 (format "*WARNING*: no doc for symbol =%s=" symbol)))
50 (case-fold-search nil))
51 ;; do some transformations on the body: FOO becomes /foo/ and
52 ;; `bar' becomes [[#bar][=bar=]]
53 (setq body (replace-regexp-in-string
54 "[A-Z][A-Z-]+" #'(lambda (match)
55 (format "/%s/" (downcase match)))
56 body)
57 body (replace-regexp-in-string "`\\([a-z-]+\\)'" #'(lambda (match)
58 (let* ((name (downcase (match-string 1 match)))
59 (sym (intern name)))
60 (if (and (or (boundp sym)
61 (fboundp sym))
62 (save-match-data
63 (string-match "^yas-" name)))
64 (format "[[#%s][=%s=]]"
65 name name)
66 (format "=%s=" name))))
67 body))
68 ;; output the paragraph
69 ;;
70 (concat-lines heading
71 after-heading
72 body))))
73
74 (defun yas--document-symbols (level &rest names-and-predicates)
75 (let ((sym-lists (make-vector (length names-and-predicates) nil)))
76 (loop for sym in yas--exported-syms
77 do (loop for test in (mapcar #'cdr names-and-predicates)
78 for i from 0
79 do (when (funcall test sym)
80 (push sym (aref sym-lists i))
81 (return))))
82 (loop for slist across sym-lists
83 for name in (mapcar #'car names-and-predicates)
84 concat (format "\n** %s\n" name)
85 concat (mapconcat (lambda (sym)
86 (yas--document-symbol sym (1+ level)))
87 slist "\n\n"))))
88
89 (defun yas--internal-link-snippet ()
90 (interactive)
91 (yas-expand-snippet "[[#$1][=${1:`yas/selected-text`}=]]"))
92
93 (define-key org-mode-map [M-f8] 'yas--internal-link-snippet)
94
95 (provide 'yas-doc-helper)
96 ;;; yas-doc-helper.el ends here
97 ;; Local Variables:
98 ;; coding: utf-8
99 ;; End: