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