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