reporter.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. ;;; reporter.el --- customizable bug reporting of lisp programs
  2. ;; Copyright (C) 1993-1998, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: 1993-1998 Barry A. Warsaw
  4. ;; Maintainer: FSF
  5. ;; Created: 19-Apr-1993
  6. ;; Keywords: maint mail tools
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; End User Interface
  20. ;; ==================
  21. ;; The variable `mail-user-agent' contains a symbol indicating which
  22. ;; Emacs mail package end users would like to use to compose outgoing
  23. ;; mail. See that variable for details (it is no longer defined in
  24. ;; this file).
  25. ;; Lisp Package Authors
  26. ;; ====================
  27. ;; reporter.el was written primarily for Emacs Lisp package authors so
  28. ;; that their users can more easily report bugs. When invoked,
  29. ;; `reporter-submit-bug-report' will set up an outgoing mail buffer
  30. ;; with the appropriate bug report address, including a lisp
  31. ;; expression the maintainer of the package can evaluate to completely
  32. ;; reproduce the environment in which the bug was observed (e.g. by
  33. ;; using `eval-last-sexp'). This package proved especially useful
  34. ;; during my development of CC Mode, which is highly dependent on its
  35. ;; configuration variables.
  36. ;;
  37. ;; Do a "C-h f reporter-submit-bug-report" for more information.
  38. ;; Here's an example usage:
  39. ;;
  40. ;;(defconst mypkg-version "9.801")
  41. ;;(defconst mypkg-maintainer-address "mypkg-help@foo.com")
  42. ;;(defun mypkg-submit-bug-report ()
  43. ;; "Submit via mail a bug report on mypkg"
  44. ;; (interactive)
  45. ;; (require 'reporter)
  46. ;; (reporter-submit-bug-report
  47. ;; mypkg-maintainer-address
  48. ;; (concat "mypkg.el " mypkg-version)
  49. ;; (list 'mypkg-variable-1
  50. ;; 'mypkg-variable-2
  51. ;; ;; ...
  52. ;; 'mypkg-variable-last)))
  53. ;;; Code:
  54. ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  55. ;; Package author interface variables
  56. (defvar reporter-prompt-for-summary-p nil
  57. "Interface variable controlling prompting for problem summary.
  58. When non-nil, `reporter-submit-bug-report' prompts the user for a
  59. brief summary of the problem, and puts this summary on the Subject:
  60. line. If this variable is a string, that string is used as the prompt
  61. string.
  62. Default behavior is to not prompt (i.e. nil). If you want reporter to
  63. prompt, you should `let' bind this variable before calling
  64. `reporter-submit-bug-report'. Note that this variable is not
  65. buffer-local so you should never just `setq' it.")
  66. (defvar reporter-dont-compact-list nil
  67. "Interface variable controlling compacting of list values.
  68. When non-nil, this must be a list of variable symbols. When a
  69. variable containing a list value is formatted in the bug report mail
  70. buffer, it normally is compacted so that its value fits one the fewest
  71. number of lines. If the variable's symbol appears in this list, its
  72. value is printed in a more verbose style, specifically, one elemental
  73. sexp per line.
  74. Note that this variable is not buffer-local so you should never just
  75. `setq' it. If you want to changes its default value, you should `let'
  76. bind it.")
  77. ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  78. ;; End of editable variables
  79. (defvar reporter-eval-buffer nil
  80. "Buffer to retrieve variable's value from.
  81. This is necessary to properly support the printing of buffer-local
  82. variables. Current buffer will always be the mail buffer being
  83. composed.")
  84. (defvar reporter-initial-text nil
  85. "The automatically created initial text of a bug report.")
  86. (make-variable-buffer-local 'reporter-initial-text)
  87. ;; status feedback to the user
  88. (defvar reporter-status-message nil)
  89. (defvar reporter-status-count nil)
  90. (defun reporter-update-status ()
  91. "Periodically output a status message."
  92. (if (zerop (% reporter-status-count 10))
  93. (progn
  94. (message "%s" reporter-status-message)
  95. (setq reporter-status-message (concat reporter-status-message "."))))
  96. (setq reporter-status-count (1+ reporter-status-count)))
  97. ;; dumping/pretty printing of values
  98. (defun reporter-beautify-list (maxwidth compact-p)
  99. "Pretty print a list."
  100. (reporter-update-status)
  101. (let ((move t)
  102. linebreak indent-enclosing-p indent-p here)
  103. (condition-case nil ;loop exit
  104. (progn
  105. (down-list 1)
  106. (setq indent-enclosing-p t)
  107. (while move
  108. (setq here (point))
  109. ;; The following line is how we break out of the while
  110. ;; loop, in one of two ways. Either we've hit the end of
  111. ;; the buffer, in which case scan-sexps returns nil, or
  112. ;; we've crossed unbalanced parens and it will raise an
  113. ;; error we're expecting to catch.
  114. (setq move (scan-sexps (point) 1))
  115. (goto-char move)
  116. (if (<= maxwidth (current-column))
  117. (if linebreak
  118. (progn
  119. (goto-char linebreak)
  120. (newline-and-indent)
  121. (setq linebreak nil))
  122. (goto-char here)
  123. (setq indent-p (reporter-beautify-list maxwidth compact-p))
  124. (goto-char here)
  125. (forward-sexp 1)
  126. (if indent-p
  127. (newline-and-indent))
  128. t)
  129. (if compact-p
  130. (setq linebreak (point))
  131. (newline-and-indent))
  132. ))
  133. t)
  134. (error indent-enclosing-p))))
  135. (defun reporter-lisp-indent (indent-point state)
  136. "A better lisp indentation style for bug reporting."
  137. (save-excursion
  138. (goto-char (1+ (nth 1 state)))
  139. (current-column)))
  140. (declare-function mail-position-on-field "sendmail" (field &optional soft))
  141. (declare-function mail-text "sendmail" ())
  142. (defun reporter-dump-variable (varsym mailbuf)
  143. "Pretty-print the value of the variable in symbol VARSYM.
  144. MAILBUF is the mail buffer being composed."
  145. (reporter-update-status)
  146. (condition-case nil
  147. (let ((val (with-current-buffer reporter-eval-buffer
  148. (symbol-value varsym)))
  149. (sym (symbol-name varsym))
  150. (print-escape-newlines t)
  151. (maxwidth (1- (window-width)))
  152. (here (point)))
  153. (insert " " sym " "
  154. (cond
  155. ((memq val '(t nil)) "")
  156. ((listp val) "'")
  157. ((symbolp val) "'")
  158. (t ""))
  159. (prin1-to-string val))
  160. (lisp-indent-line)
  161. ;; clean up lists, but only if the line as printed was long
  162. ;; enough to wrap
  163. (if (and val ;nil is a list, but short
  164. (listp val)
  165. (<= maxwidth (current-column)))
  166. (save-excursion
  167. (let ((compact-p (not (memq varsym reporter-dont-compact-list)))
  168. (lisp-indent-function 'reporter-lisp-indent))
  169. (goto-char here)
  170. (reporter-beautify-list maxwidth compact-p))))
  171. (insert "\n"))
  172. (void-variable
  173. (with-current-buffer mailbuf
  174. (mail-position-on-field "X-Reporter-Void-Vars-Found")
  175. (end-of-line)
  176. (insert (symbol-name varsym) " ")))
  177. (error
  178. (error ""))))
  179. (defun reporter-dump-state (pkgname varlist pre-hooks post-hooks)
  180. "Dump the state of the mode specific variables.
  181. PKGNAME contains the name of the mode as it will appear in the bug
  182. report (you must explicitly concat any version numbers).
  183. VARLIST is the list of variables to dump. Each element in
  184. VARLIST can be a variable symbol, or a cons cell. If a symbol,
  185. this will be passed to `reporter-dump-variable' for insertion
  186. into the mail buffer. If a cons cell, the car must be a variable
  187. symbol and the cdr must be a function which will be `funcall'd
  188. with arguments the symbol and the mail buffer being composed. Use
  189. this to write your own custom variable value printers for
  190. specific variables.
  191. Note that the global variable `reporter-eval-buffer' will be bound to
  192. the buffer in which `reporter-submit-bug-report' was invoked. If you
  193. want to print the value of a buffer local variable, you should wrap
  194. the `eval' call in your custom printer inside a `set-buffer' (and
  195. probably a `save-excursion'). `reporter-dump-variable' handles this
  196. properly.
  197. PRE-HOOKS is run after the Emacs version and PKGNAME are inserted, but
  198. before the VARLIST is dumped. POST-HOOKS is run after the VARLIST is
  199. dumped."
  200. (let ((buffer (current-buffer)))
  201. (set-buffer buffer)
  202. (insert "Emacs : " (emacs-version) "\n")
  203. (and pkgname
  204. (insert "Package: " pkgname "\n"))
  205. (run-hooks 'pre-hooks)
  206. (if (not varlist)
  207. nil
  208. (insert "\ncurrent state:\n==============\n")
  209. ;; create an emacs-lisp-mode buffer to contain the output, which
  210. ;; we'll later insert into the mail buffer
  211. (condition-case fault
  212. (let ((mailbuf (current-buffer))
  213. (elbuf (get-buffer-create " *tmp-reporter-buffer*")))
  214. (with-current-buffer elbuf
  215. (emacs-lisp-mode)
  216. (erase-buffer)
  217. (insert "(setq\n")
  218. (lisp-indent-line)
  219. (mapc
  220. (function
  221. (lambda (varsym-or-cons-cell)
  222. (let ((varsym (or (car-safe varsym-or-cons-cell)
  223. varsym-or-cons-cell))
  224. (printer (or (cdr-safe varsym-or-cons-cell)
  225. 'reporter-dump-variable)))
  226. (funcall printer varsym mailbuf)
  227. )))
  228. varlist)
  229. (lisp-indent-line)
  230. (insert ")\n"))
  231. (insert-buffer-substring elbuf))
  232. (error
  233. (insert "State could not be dumped due to the following error:\n\n"
  234. (format "%s" fault)
  235. "\n\nYou should still send this bug report."))))
  236. (run-hooks 'post-hooks)
  237. ))
  238. (defun reporter-compose-outgoing ()
  239. "Compose the outgoing mail buffer.
  240. Return the selected paradigm, with the current buffer tacked onto the
  241. beginning of the list."
  242. (let* ((agent mail-user-agent)
  243. (compose (get mail-user-agent 'composefunc)))
  244. ;; Sanity check. If this fails then we'll try to use the SENDMAIL
  245. ;; protocol, otherwise we must signal an error.
  246. (if (not (and compose (functionp compose)))
  247. (progn
  248. (setq agent 'sendmail-user-agent
  249. compose (get agent 'composefunc))
  250. (if (not (and compose (functionp compose)))
  251. (error "Could not find a valid `mail-user-agent'")
  252. (ding)
  253. (message "`%s' is an invalid `mail-user-agent'; using `sendmail-user-agent'"
  254. mail-user-agent)
  255. )))
  256. (funcall compose)
  257. agent))
  258. ;;;###autoload
  259. (defun reporter-submit-bug-report
  260. (address pkgname varlist &optional pre-hooks post-hooks salutation)
  261. "Begin submitting a bug report via email.
  262. ADDRESS is the email address for the package's maintainer. PKGNAME is
  263. the name of the package (if you want to include version numbers,
  264. you must put them into PKGNAME before calling this function).
  265. Optional PRE-HOOKS and POST-HOOKS are passed to `reporter-dump-state'.
  266. Optional SALUTATION is inserted at the top of the mail buffer,
  267. and point is left after the salutation.
  268. VARLIST is the list of variables to dump (see `reporter-dump-state'
  269. for details). The optional argument PRE-HOOKS and POST-HOOKS are
  270. passed to `reporter-dump-state'. Optional argument SALUTATION is text
  271. to be inserted at the top of the mail buffer; in that case, point is
  272. left after that text.
  273. This function prompts for a summary if `reporter-prompt-for-summary-p'
  274. is non-nil.
  275. This function does not send a message; it uses the given information
  276. to initialize a message, which the user can then edit and finally send
  277. \(or decline to send). The variable `mail-user-agent' controls which
  278. mail-sending package is used for editing and sending the message."
  279. (let ((reporter-eval-buffer (current-buffer))
  280. final-resting-place
  281. after-sep-pos
  282. (reporter-status-message "Formatting bug report buffer...")
  283. (reporter-status-count 0)
  284. (problem (and reporter-prompt-for-summary-p
  285. (read-string (if (stringp reporter-prompt-for-summary-p)
  286. reporter-prompt-for-summary-p
  287. "(Very) brief summary of problem: "))))
  288. (agent (reporter-compose-outgoing))
  289. (mailbuf (current-buffer))
  290. hookvar)
  291. ;; do the work
  292. (require 'sendmail)
  293. ;; Just in case the original buffer is not visible now, bring it
  294. ;; back somewhere
  295. (display-buffer reporter-eval-buffer)
  296. ;; If mailbuf did not get made visible before, make it visible now.
  297. (pop-to-buffer mailbuf)
  298. (goto-char (point-min))
  299. (mail-position-on-field "to")
  300. (insert address)
  301. ;; insert problem summary if available
  302. (if (and reporter-prompt-for-summary-p problem pkgname)
  303. (progn
  304. (mail-position-on-field "subject")
  305. (insert pkgname "; " problem)))
  306. ;; move point to the body of the message
  307. (mail-text)
  308. (forward-line 1)
  309. (setq after-sep-pos (point))
  310. (and salutation (insert "\n" salutation "\n\n"))
  311. (unwind-protect
  312. (progn
  313. (setq final-resting-place (point-marker))
  314. (insert "\n\n")
  315. (reporter-dump-state pkgname varlist pre-hooks post-hooks)
  316. (goto-char final-resting-place))
  317. (set-marker final-resting-place nil))
  318. ;; save initial text and set up the `no-empty-submission' hook.
  319. ;; This only works for mailers that support a pre-send hook, and
  320. ;; for which the paradigm has a non-nil value for the `hookvar'
  321. ;; key in its agent (i.e. sendmail.el's mail-send-hook).
  322. (save-excursion
  323. (goto-char (point-max))
  324. (skip-chars-backward " \t\n")
  325. (setq reporter-initial-text (buffer-substring after-sep-pos (point))))
  326. (if (setq hookvar (get agent 'hookvar))
  327. (add-hook hookvar 'reporter-bug-hook nil t))
  328. ;; compose the minibuf message and display this.
  329. (let* ((sendkey-whereis (where-is-internal
  330. (get agent 'sendfunc) nil t))
  331. (abortkey-whereis (where-is-internal
  332. (get agent 'abortfunc) nil t))
  333. (sendkey (if sendkey-whereis
  334. (key-description sendkey-whereis)
  335. "C-c C-c")) ; TBD: BOGUS hardcode
  336. (abortkey (if abortkey-whereis
  337. (key-description abortkey-whereis)
  338. "M-x kill-buffer")) ; TBD: BOGUS hardcode
  339. )
  340. (message "Please enter your report. Type %s to send, %s to abort."
  341. sendkey abortkey))
  342. ))
  343. (defun reporter-bug-hook ()
  344. "Prohibit sending mail if empty bug report."
  345. (let ((after-sep-pos
  346. (save-excursion
  347. (rfc822-goto-eoh)
  348. (forward-line 1)
  349. (point))))
  350. (save-excursion
  351. (goto-char (point-max))
  352. (skip-chars-backward " \t\n")
  353. (if (and (= (- (point) after-sep-pos)
  354. (length reporter-initial-text))
  355. (string= (buffer-substring after-sep-pos (point))
  356. reporter-initial-text))
  357. (error "Empty bug report cannot be sent"))
  358. )))
  359. (provide 'reporter)
  360. ;;; reporter.el ends here