texi-docstring-magic.el 14 KB

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