texi-docstring-magic.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. ;; texi-docstring-magic.el -- munge internal docstrings into texi
  2. ;;
  3. ;; Keywords: lisp, docs, tex
  4. ;; Author: David Aspinall <da@dcs.ed.ac.uk>
  5. ;; Copyright (C) 1998 David Aspinall
  6. ;; Maintainer: David Aspinall <da@dcs.ed.ac.uk>
  7. ;;
  8. ;; This package is distributed under the terms of the
  9. ;; GNU General Public License, Version 3.
  10. ;; You should have a copy of the GPL with your version of
  11. ;; GNU Emacs or the Texinfo distribution.
  12. ;;
  13. ;;
  14. ;; This package generates Texinfo source fragments from Emacs
  15. ;; docstrings. This avoids documenting functions and variables
  16. ;; in more than one place, and automatically adds Texinfo markup
  17. ;; to docstrings.
  18. ;;
  19. ;; It relies heavily on you following the Elisp documentation
  20. ;; conventions to produce sensible output, check the Elisp manual
  21. ;; for details. In brief:
  22. ;;
  23. ;; * The first line of a docstring should be a complete sentence.
  24. ;; * Arguments to functions should be written in upper case: ARG1..ARGN
  25. ;; * User options (variables users may want to set) should have docstrings
  26. ;; beginning with an asterisk.
  27. ;;
  28. ;; Usage:
  29. ;;
  30. ;; Write comments of the form:
  31. ;;
  32. ;; @c TEXI DOCSTRING MAGIC: my-package-function-or-variable-name
  33. ;;
  34. ;; In your texi source, mypackage.texi. From within an Emacs session
  35. ;; where my-package is loaded, visit mypackage.texi and run
  36. ;; M-x texi-docstring-magic to update all of the documentation strings.
  37. ;;
  38. ;; This will insert @defopt, @deffn and the like underneath the
  39. ;; magic comment strings.
  40. ;;
  41. ;; The default value for user options will be printed.
  42. ;;
  43. ;; Symbols are recognized if they are defined for faces, functions,
  44. ;; or variables (in that order).
  45. ;;
  46. ;; Automatic markup rules:
  47. ;;
  48. ;; 1. Indented lines are gathered into @lisp environment.
  49. ;; 2. Pieces of text `stuff' or surrounded in quotes marked up with @samp.
  50. ;; 3. Words *emphasized* are made @strong{emphasized}
  51. ;; 4. Words sym-bol which are symbols become @code{sym-bol}.
  52. ;; 5. Upper cased words ARG corresponding to arguments become @var{arg}.
  53. ;; In fact, you can any word longer than three letters, so that
  54. ;; metavariables can be used easily.
  55. ;; FIXME: to escape this, use `ARG'
  56. ;; 6. Words 'sym which are lisp-quoted are marked with @code{'sym}.
  57. ;;
  58. ;; -----
  59. ;;
  60. ;; Useful key binding when writing Texinfo:
  61. ;;
  62. ;; (define-key TeXinfo-mode-map "C-cC-d" 'texi-docstring-magic-insert-magic)
  63. ;;
  64. ;; -----
  65. ;;
  66. ;; Useful enhancements to do:
  67. ;;
  68. ;; * Use customize properties (e.g. group, simple types)
  69. ;; * Look for a "texi-docstring" property for symbols
  70. ;; so TeXInfo can be defined directly in case automatic markup
  71. ;; goes badly wrong.
  72. ;; * Add tags to special comments so that user can specify face,
  73. ;; function, or variable binding for a symbol in case more than
  74. ;; one binding exists.
  75. ;;
  76. ;; ------
  77. (defun texi-docstring-magic-splice-sep (strings sep)
  78. "Return concatenation of STRINGS spliced together with separator SEP."
  79. (let (str)
  80. (while strings
  81. (setq str (concat str (car strings)))
  82. (if (cdr strings)
  83. (setq str (concat str sep)))
  84. (setq strings (cdr strings)))
  85. str))
  86. (defconst texi-docstring-magic-munge-table
  87. '(;; 1. Indented lines are gathered into @lisp environment.
  88. ("\\(^.*\\S-.*$\\)"
  89. t
  90. (let
  91. ((line (match-string 0 docstring)))
  92. (if (eq (char-syntax (string-to-char line)) ?\ )
  93. ;; whitespace
  94. (if in-quoted-region
  95. line
  96. (setq in-quoted-region t)
  97. (concat "@lisp\n" line))
  98. ;; non-white space
  99. (if in-quoted-region
  100. (progn
  101. (setq in-quoted-region nil)
  102. (concat "@end lisp\n" line))
  103. line))))
  104. ;; 2. Pieces of text `stuff' or surrounded in quotes
  105. ;; are marked up with @samp. NB: Must be backquote
  106. ;; followed by forward quote for this to work.
  107. ;; Can't use two forward quotes else problems with
  108. ;; symbols.
  109. ;; Odd hack: because ' is a word constituent in text/texinfo
  110. ;; mode, putting this first enables the recognition of args
  111. ;; and symbols put inside quotes.
  112. ("\\(`\\([^']+\\)'\\)"
  113. t
  114. (concat "@samp{" (match-string 2 docstring) "}"))
  115. ;; 3. Words *emphasized* are made @strong{emphasized}
  116. ("\\(\\*\\(\\w+\\)\\*\\)"
  117. t
  118. (concat "@strong{" (match-string 2 docstring) "}"))
  119. ;; 4. Words sym-bol which are symbols become @code{sym-bol}.
  120. ;; Must have at least one hyphen to be recognized,
  121. ;; terminated in whitespace, end of line, or punctuation.
  122. ;; (Only consider symbols made from word constituents
  123. ;; and hyphen.
  124. ("\\(\\(\\w+\\-\\(\\w\\|\\-\\)+\\)\\)\\(\\s\)\\|\\s-\\|\\s.\\|$\\)"
  125. (or (boundp (intern (match-string 2 docstring)))
  126. (fboundp (intern (match-string 2 docstring))))
  127. (concat "@code{" (match-string 2 docstring) "}"
  128. (match-string 4 docstring)))
  129. ;; 5. Upper cased words ARG corresponding to arguments become
  130. ;; @var{arg}
  131. ;; In fact, include any word so long as it is more than 3 characters
  132. ;; long. (Comes after symbols to avoid recognizing the
  133. ;; lowercased form of an argument as a symbol)
  134. ;; FIXME: maybe we don't want to downcase stuff already
  135. ;; inside @samp
  136. ;; FIXME: should - terminate? should _ be included?
  137. ("\\([A-Z0-9\\-]+\\)\\(/\\|\)\\|}\\|\\s-\\|\\s.\\|$\\)"
  138. (or (> (length (match-string 1 docstring)) 3)
  139. (member (downcase (match-string 1 docstring)) args))
  140. (concat "@var{" (downcase (match-string 1 docstring)) "}"
  141. (match-string 2 docstring)))
  142. ;; 6. Words 'sym which are lisp quoted are
  143. ;; marked with @code.
  144. ("\\(\\(\\s-\\|^\\)'\\(\\(\\w\\|\\-\\)+\\)\\)\\(\\s\)\\|\\s-\\|\\s.\\|$\\)"
  145. t
  146. (concat (match-string 2 docstring)
  147. "@code{'" (match-string 3 docstring) "}"
  148. (match-string 5 docstring)))
  149. ;; 7,8. Clean up for @lisp environments left with spurious newlines
  150. ;; after 1.
  151. ("\\(\\(^\\s-*$\\)\n@lisp\\)" t "@lisp")
  152. ("\\(\\(^\\s-*$\\)\n@end lisp\\)" t "@end lisp"))
  153. "Table of regexp matches and replacements used to markup docstrings.
  154. Format of table is a list of elements of the form
  155. (regexp predicate replacement-form)
  156. If regexp matches and predicate holds, then replacement-form is
  157. evaluated to get the replacement for the match.
  158. predicate and replacement-form can use variables arg,
  159. and forms such as (match-string 1 docstring)
  160. Match string 1 is assumed to determine the
  161. length of the matched item, hence where parsing restarts from.
  162. The replacement must cover the whole match (match string 0),
  163. including any whitespace included to delimit matches.")
  164. (defun texi-docstring-magic-munge-docstring (docstring args)
  165. "Markup DOCSTRING for texi according to regexp matches."
  166. (let ((case-fold-search nil))
  167. (dolist (test texi-docstring-magic-munge-table docstring)
  168. (let ((regexp (nth 0 test))
  169. (predicate (nth 1 test))
  170. (replace (nth 2 test))
  171. (i 0)
  172. in-quoted-region)
  173. (while (and
  174. (< i (length docstring))
  175. (string-match regexp docstring i))
  176. (setq i (match-end 1))
  177. (if (eval predicate)
  178. (let* ((origlength (- (match-end 0) (match-beginning 0)))
  179. (replacement (eval replace))
  180. (newlength (length replacement)))
  181. (setq docstring
  182. (replace-match replacement t t docstring))
  183. (setq i (+ i (- newlength origlength))))))
  184. (if in-quoted-region
  185. (setq docstring (concat docstring "\n@end lisp"))))))
  186. ;; Force a new line after (what should be) the first sentence,
  187. ;; if not already a new paragraph.
  188. (let*
  189. ((pos (string-match "\n" docstring))
  190. (needscr (and pos
  191. (not (string= "\n"
  192. (substring docstring
  193. (1+ pos)
  194. (+ pos 2)))))))
  195. (if (and pos needscr)
  196. (concat (substring docstring 0 pos)
  197. "@*\n"
  198. (substring docstring (1+ pos)))
  199. docstring)))
  200. (defun texi-docstring-magic-texi (env grp name docstring args &optional endtext)
  201. "Make a texi def environment ENV for entity NAME with DOCSTRING."
  202. (concat "@def" env (if grp (concat " " grp) "") " " name
  203. " "
  204. (texi-docstring-magic-splice-sep args " ")
  205. ;; " "
  206. ;; (texi-docstring-magic-splice-sep extras " ")
  207. "\n"
  208. (texi-docstring-magic-munge-docstring docstring args)
  209. "\n"
  210. (or endtext "")
  211. "@end def" env "\n"))
  212. (defun texi-docstring-magic-format-default (default)
  213. "Make a default value string for the value DEFAULT.
  214. Markup as @code{stuff} or @lisp stuff @end lisp."
  215. (let ((text (format "%S" default)))
  216. (concat
  217. "\nThe default value is "
  218. (if (string-match "\n" text)
  219. ;; Carriage return will break @code, use @lisp
  220. (if (stringp default)
  221. (concat "the string: \n@lisp\n" default "\n@end lisp\n")
  222. (concat "the value: \n@lisp\n" text "\n@end lisp\n"))
  223. (concat "@code{" text "}.\n")))))
  224. (defun texi-docstring-magic-texi-for (symbol)
  225. (cond
  226. ;; Faces
  227. ((find-face symbol)
  228. (let*
  229. ((face symbol)
  230. (name (symbol-name face))
  231. (docstring (or (face-doc-string face)
  232. "Not documented."))
  233. (useropt (eq ?* (string-to-char docstring))))
  234. ;; Chop off user option setting
  235. (if useropt
  236. (setq docstring (substring docstring 1)))
  237. (texi-docstring-magic-texi "fn" "Face" name docstring nil)))
  238. ((fboundp symbol)
  239. ;; Functions.
  240. ;; We don't handle macros, aliases, or compiled fns properly.
  241. (let*
  242. ((function symbol)
  243. (name (symbol-name function))
  244. (docstring (or (documentation function)
  245. "Not documented."))
  246. (def (symbol-function function))
  247. (argsyms (cond ((eq (car-safe def) 'lambda)
  248. (nth 1 def))))
  249. (args (mapcar 'symbol-name argsyms)))
  250. (if (commandp function)
  251. (texi-docstring-magic-texi "fn" "Command" name docstring args)
  252. (texi-docstring-magic-texi "un" nil name docstring args))))
  253. ((boundp symbol)
  254. ;; Variables.
  255. (let*
  256. ((variable symbol)
  257. (name (symbol-name variable))
  258. (docstring (or (documentation-property variable
  259. 'variable-documentation)
  260. "Not documented."))
  261. (useropt (eq ?* (string-to-char docstring)))
  262. (default (if useropt
  263. (texi-docstring-magic-format-default
  264. (default-value symbol)))))
  265. ;; Chop off user option setting
  266. (if useropt
  267. (setq docstring (substring docstring 1)))
  268. (texi-docstring-magic-texi
  269. (if useropt "opt" "var") nil name docstring nil default)))
  270. (t
  271. (error "Don't know anything about symbol %s" (symbol-name symbol)))))
  272. (defconst texi-docstring-magic-comment
  273. "@c TEXI DOCSTRING MAGIC:"
  274. "Magic string in a texi buffer expanded into @defopt, or @deffn.")
  275. (defun texi-docstring-magic ()
  276. "Update all texi docstring magic annotations in buffer."
  277. (interactive)
  278. (save-excursion
  279. (goto-char (point-min))
  280. (let ((magic (concat "^"
  281. (regexp-quote texi-docstring-magic-comment)
  282. "\\s-*\\(\\(\\w\\|\\-\\)+\\)$"))
  283. p
  284. symbol)
  285. (while (re-search-forward magic nil t)
  286. (setq symbol (intern (match-string 1)))
  287. (forward-line)
  288. (setq p (point))
  289. ;; If comment already followed by an environment, delete it.
  290. (if (and
  291. (looking-at "@def\\(\\w+\\)\\s-")
  292. (search-forward (concat "@end def" (match-string 1)) nil t))
  293. (progn
  294. (forward-line)
  295. (delete-region p (point))))
  296. (insert
  297. (texi-docstring-magic-texi-for symbol))))))
  298. (defun texi-docstring-magic-face-at-point ()
  299. (ignore-errors
  300. (let ((stab (syntax-table)))
  301. (unwind-protect
  302. (save-excursion
  303. (set-syntax-table emacs-lisp-mode-syntax-table)
  304. (or (not (zerop (skip-syntax-backward "_w")))
  305. (eq (char-syntax (char-after (point))) ?w)
  306. (eq (char-syntax (char-after (point))) ?_)
  307. (forward-sexp -1))
  308. (skip-chars-forward "'")
  309. (let ((obj (read (current-buffer))))
  310. (and (symbolp obj) (find-face obj) obj)))
  311. (set-syntax-table stab)))))
  312. (defun texi-docstring-magic-insert-magic (symbol)
  313. (interactive
  314. (let* ((v (or (variable-at-point)
  315. (function-at-point)
  316. (texi-docstring-magic-face-at-point)))
  317. (val (let ((enable-recursive-minibuffers t))
  318. (completing-read
  319. (if v
  320. (format "Magic docstring for symbol (default %s): " v)
  321. "Magic docstring for symbol: ")
  322. obarray '(lambda (sym)
  323. (or (boundp sym)
  324. (fboundp sym)
  325. (find-face sym)))
  326. t nil 'variable-history))))
  327. (list (if (equal val "") v (intern val)))))
  328. (insert "\n" texi-docstring-magic-comment " " (symbol-name symbol)))
  329. (provide 'texi-docstring-magic)