1 ;;; reporter.el --- customizable bug reporting of lisp programs
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: 1993-1998 Barry A. Warsaw
8 ;; Created: 19-Apr-1993
9 ;; Keywords: maint mail tools
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;; The variable `mail-user-agent' contains a symbol indicating which
31 ;; Emacs mail package end users would like to use to compose outgoing
32 ;; mail. See that variable for details (it is no longer defined in
35 ;; Lisp Package Authors
36 ;; ====================
37 ;; reporter.el was written primarily for Emacs Lisp package authors so
38 ;; that their users can more easily report bugs. When invoked,
39 ;; `reporter-submit-bug-report' will set up an outgoing mail buffer
40 ;; with the appropriate bug report address, including a lisp
41 ;; expression the maintainer of the package can evaluate to completely
42 ;; reproduce the environment in which the bug was observed (e.g. by
43 ;; using `eval-last-sexp'). This package proved especially useful
44 ;; during my development of CC Mode, which is highly dependent on its
45 ;; configuration variables.
47 ;; Do a "C-h f reporter-submit-bug-report" for more information.
48 ;; Here's an example usage:
50 ;;(defconst mypkg-version "9.801")
51 ;;(defconst mypkg-maintainer-address "mypkg-help@foo.com")
52 ;;(defun mypkg-submit-bug-report ()
53 ;; "Submit via mail a bug report on mypkg"
55 ;; (require 'reporter)
56 ;; (reporter-submit-bug-report
57 ;; mypkg-maintainer-address
58 ;; (concat "mypkg.el " mypkg-version)
59 ;; (list 'mypkg-variable-1
62 ;; 'mypkg-variable-last)))
67 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
68 ;; Package author interface variables
70 (defvar reporter-prompt-for-summary-p nil
71 "Interface variable controlling prompting for problem summary.
72 When non-nil, `reporter-submit-bug-report' prompts the user for a
73 brief summary of the problem, and puts this summary on the Subject:
74 line. If this variable is a string, that string is used as the prompt
77 Default behavior is to not prompt (i.e. nil). If you want reporter to
78 prompt, you should `let' bind this variable before calling
79 `reporter-submit-bug-report'. Note that this variable is not
80 buffer-local so you should never just `setq' it.")
82 (defvar reporter-dont-compact-list nil
83 "Interface variable controlling compacting of list values.
84 When non-nil, this must be a list of variable symbols. When a
85 variable containing a list value is formatted in the bug report mail
86 buffer, it normally is compacted so that its value fits one the fewest
87 number of lines. If the variable's symbol appears in this list, its
88 value is printed in a more verbose style, specifically, one elemental
91 Note that this variable is not buffer-local so you should never just
92 `setq' it. If you want to changes its default value, you should `let'
95 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96 ;; End of editable variables
99 (defvar reporter-eval-buffer nil
100 "Buffer to retrieve variable's value from.
101 This is necessary to properly support the printing of buffer-local
102 variables. Current buffer will always be the mail buffer being
105 (defvar reporter-initial-text nil
106 "The automatically created initial text of a bug report.")
107 (make-variable-buffer-local 'reporter-initial-text
)
111 ;; status feedback to the user
112 (defvar reporter-status-message nil
)
113 (defvar reporter-status-count nil
)
115 (defun reporter-update-status ()
116 "Periodically output a status message."
117 (if (zerop (% reporter-status-count
10))
119 (message "%s" reporter-status-message
)
120 (setq reporter-status-message
(concat reporter-status-message
"."))))
121 (setq reporter-status-count
(1+ reporter-status-count
)))
124 ;; dumping/pretty printing of values
125 (defun reporter-beautify-list (maxwidth compact-p
)
126 "Pretty print a list."
127 (reporter-update-status)
129 linebreak indent-enclosing-p indent-p here
)
130 (condition-case nil
;loop exit
133 (setq indent-enclosing-p t
)
136 ;; The following line is how we break out of the while
137 ;; loop, in one of two ways. Either we've hit the end of
138 ;; the buffer, in which case scan-sexps returns nil, or
139 ;; we've crossed unbalanced parens and it will raise an
140 ;; error we're expecting to catch.
141 (setq move
(scan-sexps (point) 1))
143 (if (<= maxwidth
(current-column))
146 (goto-char linebreak
)
148 (setq linebreak nil
))
150 (setq indent-p
(reporter-beautify-list maxwidth compact-p
))
154 (newline-and-indent))
157 (setq linebreak
(point))
158 (newline-and-indent))
161 (error indent-enclosing-p
))))
163 (defun reporter-lisp-indent (indent-point state
)
164 "A better lisp indentation style for bug reporting."
166 (goto-char (1+ (nth 1 state
)))
169 (declare-function mail-position-on-field
"sendmail" (field &optional soft
))
170 (declare-function mail-text
"sendmail" ())
172 (defun reporter-dump-variable (varsym mailbuf
)
173 "Pretty-print the value of the variable in symbol VARSYM.
174 MAILBUF is the mail buffer being composed."
175 (reporter-update-status)
177 (let ((val (with-current-buffer reporter-eval-buffer
178 (symbol-value varsym
)))
179 (sym (symbol-name varsym
))
180 (print-escape-newlines t
)
181 (maxwidth (1- (window-width)))
185 ((memq val
'(t nil
)) "")
189 (prin1-to-string val
))
191 ;; clean up lists, but only if the line as printed was long
193 (if (and val
;nil is a list, but short
195 (<= maxwidth
(current-column)))
197 (let ((compact-p (not (memq varsym reporter-dont-compact-list
)))
198 (lisp-indent-function 'reporter-lisp-indent
))
200 (reporter-beautify-list maxwidth compact-p
))))
203 (with-current-buffer mailbuf
204 (mail-position-on-field "X-Reporter-Void-Vars-Found")
206 (insert (symbol-name varsym
) " ")))
210 (defun reporter-dump-state (pkgname varlist pre-hooks post-hooks
)
211 "Dump the state of the mode specific variables.
212 PKGNAME contains the name of the mode as it will appear in the bug
213 report (you must explicitly concat any version numbers).
215 VARLIST is the list of variables to dump. Each element in
216 VARLIST can be a variable symbol, or a cons cell. If a symbol,
217 this will be passed to `reporter-dump-variable' for insertion
218 into the mail buffer. If a cons cell, the car must be a variable
219 symbol and the cdr must be a function which will be `funcall'd
220 with arguments the symbol and the mail buffer being composed. Use
221 this to write your own custom variable value printers for
224 Note that the global variable `reporter-eval-buffer' will be bound to
225 the buffer in which `reporter-submit-bug-report' was invoked. If you
226 want to print the value of a buffer local variable, you should wrap
227 the `eval' call in your custom printer inside a `set-buffer' (and
228 probably a `save-excursion'). `reporter-dump-variable' handles this
231 PRE-HOOKS is run after the Emacs version and PKGNAME are inserted, but
232 before the VARLIST is dumped. POST-HOOKS is run after the VARLIST is
234 (let ((buffer (current-buffer)))
236 (insert "Emacs : " (emacs-version) "\n")
238 (insert "Package: " pkgname
"\n"))
239 (run-hooks 'pre-hooks
)
242 (insert "\ncurrent state:\n==============\n")
243 ;; create an emacs-lisp-mode buffer to contain the output, which
244 ;; we'll later insert into the mail buffer
245 (condition-case fault
246 (let ((mailbuf (current-buffer))
247 (elbuf (get-buffer-create " *tmp-reporter-buffer*")))
248 (with-current-buffer elbuf
255 (lambda (varsym-or-cons-cell)
256 (let ((varsym (or (car-safe varsym-or-cons-cell
)
257 varsym-or-cons-cell
))
258 (printer (or (cdr-safe varsym-or-cons-cell
)
259 'reporter-dump-variable
)))
260 (funcall printer varsym mailbuf
)
265 (insert-buffer-substring elbuf
))
267 (insert "State could not be dumped due to the following error:\n\n"
269 "\n\nYou should still send this bug report."))))
270 (run-hooks 'post-hooks
)
274 (defun reporter-compose-outgoing ()
275 "Compose the outgoing mail buffer.
277 Return the selected paradigm, with the current buffer tacked onto the
278 beginning of the list."
279 (let* ((agent mail-user-agent
)
280 (compose (get mail-user-agent
'composefunc
)))
281 ;; Sanity check. If this fails then we'll try to use the SENDMAIL
282 ;; protocol, otherwise we must signal an error.
283 (if (not (and compose
(functionp compose
)))
285 (setq agent
'sendmail-user-agent
286 compose
(get agent
'composefunc
))
287 (if (not (and compose
(functionp compose
)))
288 (error "Could not find a valid `mail-user-agent'")
290 (message "`%s' is an invalid `mail-user-agent'; using `sendmail-user-agent'"
298 (defun reporter-submit-bug-report
299 (address pkgname varlist
&optional pre-hooks post-hooks salutation
)
300 "Begin submitting a bug report via email.
302 ADDRESS is the email address for the package's maintainer. PKGNAME is
303 the name of the package (if you want to include version numbers,
304 you must put them into PKGNAME before calling this function).
305 Optional PRE-HOOKS and POST-HOOKS are passed to `reporter-dump-state'.
306 Optional SALUTATION is inserted at the top of the mail buffer,
307 and point is left after the salutation.
309 VARLIST is the list of variables to dump (see `reporter-dump-state'
310 for details). The optional argument PRE-HOOKS and POST-HOOKS are
311 passed to `reporter-dump-state'. Optional argument SALUTATION is text
312 to be inserted at the top of the mail buffer; in that case, point is
313 left after that text.
315 This function prompts for a summary if `reporter-prompt-for-summary-p'
318 This function does not send a message; it uses the given information
319 to initialize a message, which the user can then edit and finally send
320 \(or decline to send). The variable `mail-user-agent' controls which
321 mail-sending package is used for editing and sending the message."
322 (let ((reporter-eval-buffer (current-buffer))
325 (reporter-status-message "Formatting bug report buffer...")
326 (reporter-status-count 0)
327 (problem (and reporter-prompt-for-summary-p
328 (read-string (if (stringp reporter-prompt-for-summary-p
)
329 reporter-prompt-for-summary-p
330 "(Very) brief summary of problem: "))))
331 (agent (reporter-compose-outgoing))
332 (mailbuf (current-buffer))
336 ;; If mailbuf did not get made visible before, make it visible now.
337 (let (same-window-buffer-names same-window-regexps
)
338 (pop-to-buffer mailbuf
)
339 ;; Just in case the original buffer is not visible now, bring it
341 (and pop-up-windows
(display-buffer reporter-eval-buffer
)))
342 (goto-char (point-min))
343 (mail-position-on-field "to")
345 ;; insert problem summary if available
346 (if (and reporter-prompt-for-summary-p problem pkgname
)
348 (mail-position-on-field "subject")
349 (insert pkgname
"; " problem
)))
350 ;; move point to the body of the message
353 (setq after-sep-pos
(point))
354 (and salutation
(insert "\n" salutation
"\n\n"))
357 (setq final-resting-place
(point-marker))
359 (reporter-dump-state pkgname varlist pre-hooks post-hooks
)
360 (goto-char final-resting-place
))
361 (set-marker final-resting-place nil
))
363 ;; save initial text and set up the `no-empty-submission' hook.
364 ;; This only works for mailers that support a pre-send hook, and
365 ;; for which the paradigm has a non-nil value for the `hookvar'
366 ;; key in its agent (i.e. sendmail.el's mail-send-hook).
368 (goto-char (point-max))
369 (skip-chars-backward " \t\n")
370 (setq reporter-initial-text
(buffer-substring after-sep-pos
(point))))
371 (if (setq hookvar
(get agent
'hookvar
))
372 (add-hook hookvar
'reporter-bug-hook nil t
))
374 ;; compose the minibuf message and display this.
375 (let* ((sendkey-whereis (where-is-internal
376 (get agent
'sendfunc
) nil t
))
377 (abortkey-whereis (where-is-internal
378 (get agent
'abortfunc
) nil t
))
379 (sendkey (if sendkey-whereis
380 (key-description sendkey-whereis
)
381 "C-c C-c")) ; TBD: BOGUS hardcode
382 (abortkey (if abortkey-whereis
383 (key-description abortkey-whereis
)
384 "M-x kill-buffer")) ; TBD: BOGUS hardcode
386 (message "Please enter your report. Type %s to send, %s to abort."
390 (defun reporter-bug-hook ()
391 "Prohibit sending mail if empty bug report."
398 (goto-char (point-max))
399 (skip-chars-backward " \t\n")
400 (if (and (= (- (point) after-sep-pos
)
401 (length reporter-initial-text
))
402 (string= (buffer-substring after-sep-pos
(point))
403 reporter-initial-text
))
404 (error "Empty bug report cannot be sent"))
410 ;; arch-tag: 33612ff4-fbbc-4be2-b183-560ce9e0199b
411 ;;; reporter.el ends here