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