new version
[emacs.git] / lisp / mail / reporter.el
blobff9dd924f75657a0b595d95df01bdb0111d8d964
1 ;;; reporter.el --- customizable bug reporting of lisp programs
3 ;; Copyright (C) 1993 1994 1995 1996 Free Software Foundation, Inc.
5 ;; Author: 1993-1996 Barry A. Warsaw
6 ;; Maintainer: FSF
7 ;; Created: 19-Apr-1993
8 ;; Version: 3.3
9 ;; Last Modified: 1996/07/02 00:39:09
10 ;; Keywords: maint mail tools
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
29 ;;; Commentary:
31 ;; End User Interface
32 ;; ==================
33 ;; The variable `mail-user-agent' contains a symbol indicating which
34 ;; Emacs mail package end users would like to use to compose outgoing
35 ;; mail. See that variable for details.
37 ;; Lisp Package Authors
38 ;; ====================
39 ;; Reporter was written primarily for Emacs Lisp package authors so
40 ;; that their users can easily report bugs. When invoked,
41 ;; reporter-submit-bug-report will set up an outgoing mail buffer with
42 ;; the appropriate bug report address, including a lisp expression the
43 ;; maintainer of the package can eval to completely reproduce the
44 ;; environment in which the bug was observed (e.g. by using
45 ;; eval-last-sexp). This package proved especially useful during my
46 ;; development of cc-mode, which is highly dependent on its
47 ;; configuration variables.
49 ;; Do a "C-h f reporter-submit-bug-report" for more information.
50 ;; Here's an example usage:
52 ;;(defconst mypkg-version "9.801")
53 ;;(defconst mypkg-maintainer-address "mypkg-help@foo.com")
54 ;;(defun mypkg-submit-bug-report ()
55 ;; "Submit via mail a bug report on mypkg"
56 ;; (interactive)
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 ;; Mailing List
66 ;; ============
67 ;; I've set up a Majordomo mailing list to report bugs or suggest
68 ;; enhancements, etc. This list's intended audience is elisp package
69 ;; authors who are using reporter and want to stay current with
70 ;; releases. Here are the relevant addresses:
72 ;; Administrivia: reporter-request@python.org
73 ;; Submissions: reporter@python.org
75 ;; Packages that currently use reporter are: cc-mode, supercite, elp,
76 ;; tcl, ediff, crypt++ (crypt), dired-x, rmailgen, mode-line, vm,
77 ;; mh-e, edebug, archie, viper, w3-mode, framepop, hl319, hilit19,
78 ;; pgp, eos, hm--html, efs.
80 ;; If you know of others, please email me!
82 ;;; Code:
84 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
85 ;; Package author interface variables
87 (defvar reporter-prompt-for-summary-p nil
88 "Interface variable controlling prompting for problem summary.
89 When non-nil, `reporter-submit-bug-report' prompts the user for a
90 brief summary of the problem, and puts this summary on the Subject:
91 line. If this variable is a string, that string is used as the prompt
92 string.
94 Default behavior is to not prompt (i.e. nil). If you want reporter to
95 prompt, you should `let' bind this variable before calling
96 `reporter-submit-bug-report'. Note that this variable is not
97 buffer-local so you should never just `setq' it.")
99 (defvar reporter-dont-compact-list nil
100 "Interface variable controlling compacting of list values.
101 When non-nil, this must be a list of variable symbols. When a
102 variable containing a list value is formatted in the bug report mail
103 buffer, it normally is compacted so that its value fits one the fewest
104 number of lines. If the variable's symbol appears in this list, its
105 value is printed in a more verbose style, specifically, one elemental
106 sexp per line.
108 Note that this variable is not buffer-local so you should never just
109 `setq' it. If you want to changes its default value, you should `let'
110 bind it.")
112 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
113 ;; End of editable variables
116 (defvar reporter-eval-buffer nil
117 "Buffer to retrieve variable's value from.
118 This is necessary to properly support the printing of buffer-local
119 variables. Current buffer will always be the mail buffer being
120 composed.")
122 (defconst reporter-version "3.2"
123 "Reporter version number.")
125 (defvar reporter-initial-text nil
126 "The automatically created initial text of a bug report.")
127 (make-variable-buffer-local 'reporter-initial-text)
131 ;; status feedback to the user
132 (defvar reporter-status-message nil)
133 (defvar reporter-status-count nil)
135 (defun reporter-update-status ()
136 ;; periodically output a status message
137 (if (zerop (% reporter-status-count 10))
138 (progn
139 (message reporter-status-message)
140 (setq reporter-status-message (concat reporter-status-message "."))))
141 (setq reporter-status-count (1+ reporter-status-count)))
144 ;; dumping/pretty printing of values
145 (defun reporter-beautify-list (maxwidth compact-p)
146 ;; pretty print a list
147 (reporter-update-status)
148 (let (linebreak indent-enclosing-p indent-p here)
149 (condition-case nil ;loop exit
150 (progn
151 (down-list 1)
152 (setq indent-enclosing-p t)
153 (while t
154 (setq here (point))
155 (forward-sexp 1)
156 (if (<= maxwidth (current-column))
157 (if linebreak
158 (progn
159 (goto-char linebreak)
160 (newline-and-indent)
161 (setq linebreak nil))
162 (goto-char here)
163 (setq indent-p (reporter-beautify-list maxwidth compact-p))
164 (goto-char here)
165 (forward-sexp 1)
166 (if indent-p
167 (newline-and-indent))
169 (if compact-p
170 (setq linebreak (point))
171 (newline-and-indent))
174 (error indent-enclosing-p))))
176 (defun reporter-lisp-indent (indent-point state)
177 ;; a better lisp indentation style for bug reporting
178 (save-excursion
179 (goto-char (1+ (nth 1 state)))
180 (current-column)))
182 (defun reporter-dump-variable (varsym mailbuf)
183 ;; Pretty-print the value of the variable in symbol VARSYM. MAILBUF
184 ;; is the mail buffer being composed
185 (reporter-update-status)
186 (condition-case nil
187 (let ((val (save-excursion
188 (set-buffer reporter-eval-buffer)
189 (symbol-value varsym)))
190 (sym (symbol-name varsym))
191 (print-escape-newlines t)
192 (maxwidth (1- (window-width)))
193 (here (point)))
194 (insert " " sym " "
195 (cond
196 ((memq val '(t nil)) "")
197 ((listp val) "'")
198 ((symbolp val) "'")
199 (t ""))
200 (prin1-to-string val))
201 (lisp-indent-line)
202 ;; clean up lists, but only if the line as printed was long
203 ;; enough to wrap
204 (if (and val ;nil is a list, but short
205 (listp val)
206 (<= maxwidth (current-column)))
207 (save-excursion
208 (let ((compact-p (not (memq varsym reporter-dont-compact-list)))
209 (lisp-indent-function 'reporter-lisp-indent))
210 (goto-char here)
211 (reporter-beautify-list maxwidth compact-p))))
212 (insert "\n"))
213 (void-variable
214 (save-excursion
215 (set-buffer mailbuf)
216 (mail-position-on-field "X-Reporter-Void-Vars-Found")
217 (end-of-line)
218 (insert (symbol-name varsym) " ")))
219 (error
220 (error ""))))
222 (defun reporter-dump-state (pkgname varlist pre-hooks post-hooks)
223 ;; Dump the state of the mode specific variables.
224 ;; PKGNAME contains the name of the mode as it will appear in the bug
225 ;; report (you must explicitly concat any version numbers).
227 ;; VARLIST is the list of variables to dump. Each element in
228 ;; VARLIST can be a variable symbol, or a cons cell. If a symbol,
229 ;; this will be passed to `reporter-dump-variable' for insertion
230 ;; into the mail buffer. If a cons cell, the car must be a variable
231 ;; symbol and the cdr must be a function which will be `funcall'd
232 ;; with arguments the symbol and the mail buffer being composed. Use
233 ;; this to write your own custom variable value printers for
234 ;; specific variables.
236 ;; Note that the global variable `reporter-eval-buffer' will be bound to
237 ;; the buffer in which `reporter-submit-bug-report' was invoked. If you
238 ;; want to print the value of a buffer local variable, you should wrap
239 ;; the `eval' call in your custom printer inside a `set-buffer' (and
240 ;; probably a `save-excursion'). `reporter-dump-variable' handles this
241 ;; properly.
243 ;; PRE-HOOKS is run after the emacs-version and PKGNAME are inserted, but
244 ;; before the VARLIST is dumped. POST-HOOKS is run after the VARLIST is
245 ;; dumped.
246 (let ((buffer (current-buffer)))
247 (set-buffer buffer)
248 (insert "Emacs : " (emacs-version) "\n")
249 (and pkgname
250 (insert "Package: " pkgname "\n"))
251 (run-hooks 'pre-hooks)
252 (if (not varlist)
254 (insert "\ncurrent state:\n==============\n")
255 ;; create an emacs-lisp-mode buffer to contain the output, which
256 ;; we'll later insert into the mail buffer
257 (condition-case fault
258 (let ((mailbuf (current-buffer))
259 (elbuf (get-buffer-create " *tmp-reporter-buffer*")))
260 (save-excursion
261 (set-buffer elbuf)
262 (emacs-lisp-mode)
263 (erase-buffer)
264 (insert "(setq\n")
265 (lisp-indent-line)
266 (mapcar
267 (function
268 (lambda (varsym-or-cons-cell)
269 (let ((varsym (or (car-safe varsym-or-cons-cell)
270 varsym-or-cons-cell))
271 (printer (or (cdr-safe varsym-or-cons-cell)
272 'reporter-dump-variable)))
273 (funcall printer varsym mailbuf)
275 varlist)
276 (lisp-indent-line)
277 (insert ")\n"))
278 (insert-buffer elbuf))
279 (error
280 (insert "State could not be dumped due to the following error:\n\n"
281 (format "%s" fault)
282 "\n\nYou should still send this bug report."))))
283 (run-hooks 'post-hooks)
287 (defun reporter-calculate-separator ()
288 ;; returns the string regexp matching the mail separator
289 (save-excursion
290 (re-search-forward
291 (concat
292 "^\\(" ;beginning of line
293 (mapconcat
294 'identity
295 (list "[\t ]*" ;simple SMTP form
296 "-+" ;mh-e form
297 (regexp-quote
298 mail-header-separator)) ;sendmail.el form
299 "\\|") ;or them together
300 "\\)$") ;end of line
302 'move) ;search for and move
303 (buffer-substring (match-beginning 0) (match-end 0))))
306 (defun reporter-compose-outgoing ()
307 ;; compose the outgoing mail buffer, and return the selected
308 ;; paradigm, with the current-buffer tacked onto the beginning of
309 ;; the list.
310 (let* ((agent mail-user-agent)
311 (compose (get mail-user-agent 'composefunc)))
312 ;; Sanity check. If this fails then we'll try to use the SENDMAIL
313 ;; protocol, otherwise we must signal an error.
314 (if (not (and compose (fboundp compose)))
315 (progn
316 (setq agent 'sendmail-user-agent
317 compose (get agent 'composefunc))
318 (if (not (and compose (fboundp compose)))
319 (error "Could not find a valid `mail-user-agent'")
320 (ding)
321 (message "`%s' is an invalid `mail-user-agent'; using `sendmail-user-agent'"
322 mail-user-agent)
324 (funcall compose)
325 agent))
328 ;;;###autoload
329 (defun reporter-submit-bug-report
330 (address pkgname varlist &optional pre-hooks post-hooks salutation)
331 ;; Submit a bug report via mail.
333 ;; ADDRESS is the email address for the package's maintainer. PKGNAME is
334 ;; the name of the mode (you must explicitly concat any version numbers).
335 ;; VARLIST is the list of variables to dump (see `reporter-dump-state'
336 ;; for details). Optional PRE-HOOKS and POST-HOOKS are passed to
337 ;; `reporter-dump-state'. Optional SALUTATION is inserted at the top of the
338 ;; mail buffer, and point is left after the salutation.
340 ;; This function will prompt for a summary if
341 ;; reporter-prompt-for-summary-p is non-nil.
343 ;; The mailer used is described in by the variable `mail-user-agent'.
344 (let ((reporter-eval-buffer (current-buffer))
345 final-resting-place
346 after-sep-pos
347 (reporter-status-message "Formatting bug report buffer...")
348 (reporter-status-count 0)
349 (problem (and reporter-prompt-for-summary-p
350 (read-string (if (stringp reporter-prompt-for-summary-p)
351 reporter-prompt-for-summary-p
352 "(Very) brief summary of problem: "))))
353 (agent (reporter-compose-outgoing))
354 (mailbuf (current-buffer))
355 hookvar)
356 ;; do the work
357 (require 'sendmail)
358 ;; If mailbuf did not get made visible before, make it visible now.
359 (let (same-window-buffer-names same-window-regexps)
360 (pop-to-buffer mailbuf)
361 ;; Just in case the original buffer is not visible now, bring it
362 ;; back somewhere
363 (display-buffer reporter-eval-buffer))
364 (goto-char (point-min))
365 ;; different mailers use different separators, some may not even
366 ;; use mail-header-separator, but sendmail.el stuff must have this
367 ;; variable bound.
368 (let ((mail-header-separator (reporter-calculate-separator)))
369 (mail-position-on-field "to")
370 (insert address)
371 ;; insert problem summary if available
372 (if (and reporter-prompt-for-summary-p problem pkgname)
373 (progn
374 (mail-position-on-field "subject")
375 (insert pkgname "; " problem)))
376 ;; move point to the body of the message
377 (mail-text)
378 (forward-line 1)
379 (setq after-sep-pos (point))
380 (and salutation (insert "\n" salutation "\n\n"))
381 (unwind-protect
382 (progn
383 (setq final-resting-place (point-marker))
384 (insert "\n\n")
385 (reporter-dump-state pkgname varlist pre-hooks post-hooks)
386 (goto-char final-resting-place))
387 (set-marker final-resting-place nil)))
389 ;; save initial text and set up the `no-empty-submission' hook.
390 ;; This only works for mailers that support a pre-send hook, and
391 ;; for which the paradigm has a non-nil value for the `hookvar'
392 ;; key in its agent (i.e. sendmail.el's mail-send-hook).
393 (save-excursion
394 (goto-char (point-max))
395 (skip-chars-backward " \t\n")
396 (setq reporter-initial-text (buffer-substring after-sep-pos (point))))
397 (if (setq hookvar (get agent 'hookvar))
398 (progn
399 (make-variable-buffer-local hookvar)
400 (add-hook hookvar 'reporter-bug-hook)))
402 ;; compose the minibuf message and display this.
403 (let* ((sendkey-whereis (where-is-internal
404 (get agent 'sendfunc) nil t))
405 (abortkey-whereis (where-is-internal
406 (get agent 'abortfunc) nil t))
407 (sendkey (if sendkey-whereis
408 (key-description sendkey-whereis)
409 "C-c C-c")) ; TBD: BOGUS hardcode
410 (abortkey (if abortkey-whereis
411 (key-description abortkey-whereis)
412 "M-x kill-buffer")) ; TBD: BOGUS hardcode
414 (message "Please enter your report. Type %s to send, %s to abort."
415 sendkey abortkey))
418 (defun reporter-bug-hook ()
419 ;; prohibit sending mail if empty bug report
420 (let ((after-sep-pos
421 (save-excursion
422 (beginning-of-buffer)
423 (re-search-forward (reporter-calculate-separator) (point-max) 'move)
424 (forward-line 1)
425 (point))))
426 (save-excursion
427 (goto-char (point-max))
428 (skip-chars-backward " \t\n")
429 (if (and (= (- (point) after-sep-pos)
430 (length reporter-initial-text))
431 (string= (buffer-substring after-sep-pos (point))
432 reporter-initial-text))
433 (error "Bug report was empty--not sent"))
437 (provide 'reporter)
438 ;;; reporter.el ends here