reporter.el 14.5 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2
;;; reporter.el --- customizable bug reporting of lisp programs

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2001, 2002, 2003,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2004, 2005, 2006, 2007 Free Software Foundation, Inc.
Erik Naggum's avatar
Erik Naggum committed
5

6
;; Author:          1993-1998 Barry A. Warsaw
Dave Love's avatar
Dave Love committed
7
;; Maintainer:      FSF
Richard M. Stallman's avatar
Richard M. Stallman committed
8
;; Created:         19-Apr-1993
Miles Bader's avatar
Miles Bader committed
9
;; Keywords: maint mail tools
Richard M. Stallman's avatar
Richard M. Stallman committed
10

David J. MacKenzie's avatar
David J. MacKenzie committed
11
;; This file is part of GNU Emacs.
12 13

;; GNU Emacs is free software; you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
14
;; it under the terms of the GNU General Public License as published by
15
;; the Free Software Foundation; either version 3, or (at your option)
16 17 18
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
Richard M. Stallman's avatar
Richard M. Stallman committed
19 20 21
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
22

Richard M. Stallman's avatar
Richard M. Stallman committed
23
;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
24
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
25 26
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
27

28
;;; Commentary:
Erik Naggum's avatar
Erik Naggum committed
29

Miles Bader's avatar
Miles Bader committed
30 31 32 33
;; End User Interface
;; ==================
;; The variable `mail-user-agent' contains a symbol indicating which
;; Emacs mail package end users would like to use to compose outgoing
34 35
;; mail.  See that variable for details (it is no longer defined in
;; this file).
Miles Bader's avatar
Miles Bader committed
36 37 38

;; Lisp Package Authors
;; ====================
39 40 41 42 43 44 45 46
;; reporter.el was written primarily for Emacs Lisp package authors so
;; that their users can more easily report bugs.  When invoked,
;; `reporter-submit-bug-report' will set up an outgoing mail buffer
;; with the appropriate bug report address, including a lisp
;; expression the maintainer of the package can evaluate to completely
;; reproduce the environment in which the bug was observed (e.g. by
;; using `eval-last-sexp').  This package proved especially useful
;; during my development of CC Mode, which is highly dependent on its
Miles Bader's avatar
Miles Bader committed
47
;; configuration variables.
Richard M. Stallman's avatar
Richard M. Stallman committed
48 49 50 51
;;
;; Do a "C-h f reporter-submit-bug-report" for more information.
;; Here's an example usage:
;;
52 53 54 55 56
;;(defconst mypkg-version "9.801")
;;(defconst mypkg-maintainer-address "mypkg-help@foo.com")
;;(defun mypkg-submit-bug-report ()
;;  "Submit via mail a bug report on mypkg"
;;  (interactive)
57
;;  (require 'reporter)
58 59 60 61 62 63 64 65
;;  (reporter-submit-bug-report
;;   mypkg-maintainer-address
;;   (concat "mypkg.el " mypkg-version)
;;   (list 'mypkg-variable-1
;;         'mypkg-variable-2
;;         ;; ...
;;         'mypkg-variable-last)))

66
;;; Code:
Miles Bader's avatar
Miles Bader committed
67

68

Richard M. Stallman's avatar
Richard M. Stallman committed
69 70 71
;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
;; Package author interface variables

72 73 74 75
(defvar reporter-prompt-for-summary-p nil
  "Interface variable controlling prompting for problem summary.
When non-nil, `reporter-submit-bug-report' prompts the user for a
brief summary of the problem, and puts this summary on the Subject:
Miles Bader's avatar
Miles Bader committed
76 77
line.  If this variable is a string, that string is used as the prompt
string.
78

Dave Love's avatar
Dave Love committed
79
Default behavior is to not prompt (i.e. nil).  If you want reporter to
Miles Bader's avatar
Miles Bader committed
80
prompt, you should `let' bind this variable before calling
81 82 83
`reporter-submit-bug-report'.  Note that this variable is not
buffer-local so you should never just `setq' it.")

84
(defvar reporter-dont-compact-list nil
85
  "Interface variable controlling compacting of list values.
86 87 88 89 90 91 92 93 94 95
When non-nil, this must be a list of variable symbols.  When a
variable containing a list value is formatted in the bug report mail
buffer, it normally is compacted so that its value fits one the fewest
number of lines.  If the variable's symbol appears in this list, its
value is printed in a more verbose style, specifically, one elemental
sexp per line.

Note that this variable is not buffer-local so you should never just
`setq' it.  If you want to changes its default value, you should `let'
bind it.")
Richard M. Stallman's avatar
Richard M. Stallman committed
96

Miles Bader's avatar
Miles Bader committed
97
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Richard M. Stallman's avatar
Richard M. Stallman committed
98 99
;; End of editable variables

Miles Bader's avatar
Miles Bader committed
100

Richard M. Stallman's avatar
Richard M. Stallman committed
101 102 103 104 105
(defvar reporter-eval-buffer nil
  "Buffer to retrieve variable's value from.
This is necessary to properly support the printing of buffer-local
variables.  Current buffer will always be the mail buffer being
composed.")
106

107 108 109 110
(defvar reporter-initial-text nil
  "The automatically created initial text of a bug report.")
(make-variable-buffer-local 'reporter-initial-text)

Miles Bader's avatar
Miles Bader committed
111

112

Miles Bader's avatar
Miles Bader committed
113
;; status feedback to the user
114 115 116 117
(defvar reporter-status-message nil)
(defvar reporter-status-count nil)

(defun reporter-update-status ()
Dave Love's avatar
Dave Love committed
118
  "Periodically output a status message."
119 120 121 122 123
  (if (zerop (% reporter-status-count 10))
      (progn
	(message reporter-status-message)
	(setq reporter-status-message (concat reporter-status-message "."))))
  (setq reporter-status-count (1+ reporter-status-count)))
124

Richard M. Stallman's avatar
Richard M. Stallman committed
125

Miles Bader's avatar
Miles Bader committed
126
;; dumping/pretty printing of values
127
(defun reporter-beautify-list (maxwidth compact-p)
Dave Love's avatar
Dave Love committed
128
  "Pretty print a list."
129
  (reporter-update-status)
130 131
  (let ((move t)
	linebreak indent-enclosing-p indent-p here)
132 133 134 135
    (condition-case nil			;loop exit
	(progn
	  (down-list 1)
	  (setq indent-enclosing-p t)
136
	  (while move
137
	    (setq here (point))
138 139 140 141 142 143 144
	    ;; The following line is how we break out of the while
	    ;; loop, in one of two ways.  Either we've hit the end of
	    ;; the buffer, in which case scan-sexps returns nil, or
	    ;; we've crossed unbalanced parens and it will raise an
	    ;; error we're expecting to catch.
	    (setq move (scan-sexps (point) 1))
	    (goto-char move)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	    (if (<= maxwidth (current-column))
		(if linebreak
		    (progn
		      (goto-char linebreak)
		      (newline-and-indent)
		      (setq linebreak nil))
		  (goto-char here)
		  (setq indent-p (reporter-beautify-list maxwidth compact-p))
		  (goto-char here)
		  (forward-sexp 1)
		  (if indent-p
		      (newline-and-indent))
		  t)
	      (if compact-p
		  (setq linebreak (point))
		(newline-and-indent))
	      ))
	  t)
      (error indent-enclosing-p))))

(defun reporter-lisp-indent (indent-point state)
Dave Love's avatar
Dave Love committed
166
  "A better lisp indentation style for bug reporting."
167 168 169 170
  (save-excursion
    (goto-char (1+ (nth 1 state)))
    (current-column)))

171 172 173
(declare-function mail-position-on-field "sendmail" (field &optional soft))
(declare-function mail-text "sendmail" ())

174
(defun reporter-dump-variable (varsym mailbuf)
Dave Love's avatar
Dave Love committed
175 176
  "Pretty-print the value of the variable in symbol VARSYM.
MAILBUF is the mail buffer being composed."
177
  (reporter-update-status)
178 179 180 181 182 183
  (condition-case nil
      (let ((val (save-excursion
		   (set-buffer reporter-eval-buffer)
		   (symbol-value varsym)))
	    (sym (symbol-name varsym))
	    (print-escape-newlines t)
184
	    (maxwidth (1- (window-width)))
185 186 187 188 189 190 191 192
	    (here (point)))
	(insert "     " sym " "
		(cond
		 ((memq val '(t nil)) "")
		 ((listp val) "'")
		 ((symbolp val) "'")
		 (t ""))
		(prin1-to-string val))
193
	(lisp-indent-line)
194 195
	;; clean up lists, but only if the line as printed was long
	;; enough to wrap
196 197 198
	(if (and val			;nil is a list, but short
		 (listp val)
		 (<= maxwidth (current-column)))
199
	    (save-excursion
200 201 202 203
	      (let ((compact-p (not (memq varsym reporter-dont-compact-list)))
		    (lisp-indent-function 'reporter-lisp-indent))
		(goto-char here)
		(reporter-beautify-list maxwidth compact-p))))
204 205 206 207 208 209 210
	(insert "\n"))
    (void-variable
     (save-excursion
       (set-buffer mailbuf)
       (mail-position-on-field "X-Reporter-Void-Vars-Found")
       (end-of-line)
       (insert (symbol-name varsym) " ")))
Miles Bader's avatar
Miles Bader committed
211 212
    (error
     (error ""))))
Richard M. Stallman's avatar
Richard M. Stallman committed
213 214

(defun reporter-dump-state (pkgname varlist pre-hooks post-hooks)
Dave Love's avatar
Dave Love committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  "Dump the state of the mode specific variables.
PKGNAME contains the name of the mode as it will appear in the bug
report (you must explicitly concat any version numbers).

VARLIST is the list of variables to dump.  Each element in
VARLIST can be a variable symbol, or a cons cell.  If a symbol,
this will be passed to `reporter-dump-variable' for insertion
into the mail buffer.  If a cons cell, the car must be a variable
symbol and the cdr must be a function which will be `funcall'd
with arguments the symbol and the mail buffer being composed.  Use
this to write your own custom variable value printers for
specific variables.

Note that the global variable `reporter-eval-buffer' will be bound to
the buffer in which `reporter-submit-bug-report' was invoked.  If you
want to print the value of a buffer local variable, you should wrap
the `eval' call in your custom printer inside a `set-buffer' (and
probably a `save-excursion').  `reporter-dump-variable' handles this
properly.

PRE-HOOKS is run after the Emacs version and PKGNAME are inserted, but
before the VARLIST is dumped.  POST-HOOKS is run after the VARLIST is
dumped."
Richard M. Stallman's avatar
Richard M. Stallman committed
238 239
  (let ((buffer (current-buffer)))
    (set-buffer buffer)
240 241 242
    (insert "Emacs  : " (emacs-version) "\n")
    (and pkgname
	 (insert "Package: " pkgname "\n"))
Richard M. Stallman's avatar
Richard M. Stallman committed
243
    (run-hooks 'pre-hooks)
244 245 246 247 248 249 250 251 252 253 254 255 256 257
    (if (not varlist)
	nil
      (insert "\ncurrent state:\n==============\n")
      ;; create an emacs-lisp-mode buffer to contain the output, which
      ;; we'll later insert into the mail buffer
      (condition-case fault
	  (let ((mailbuf (current-buffer))
		(elbuf (get-buffer-create " *tmp-reporter-buffer*")))
	    (save-excursion
	      (set-buffer elbuf)
	      (emacs-lisp-mode)
	      (erase-buffer)
	      (insert "(setq\n")
	      (lisp-indent-line)
258
	      (mapc
259 260 261 262 263 264 265 266 267
	       (function
		(lambda (varsym-or-cons-cell)
		  (let ((varsym (or (car-safe varsym-or-cons-cell)
				    varsym-or-cons-cell))
			(printer (or (cdr-safe varsym-or-cons-cell)
				     'reporter-dump-variable)))
		    (funcall printer varsym mailbuf)
		    )))
	       varlist)
268 269
	      (lisp-indent-line)
	      (insert ")\n"))
270
	    (insert-buffer-substring elbuf))
271 272 273 274
	(error
	 (insert "State could not be dumped due to the following error:\n\n"
		 (format "%s" fault)
		 "\n\nYou should still send this bug report."))))
Richard M. Stallman's avatar
Richard M. Stallman committed
275 276 277
    (run-hooks 'post-hooks)
    ))

Miles Bader's avatar
Miles Bader committed
278 279

(defun reporter-compose-outgoing ()
Dave Love's avatar
Dave Love committed
280 281 282 283
  "Compose the outgoing mail buffer.

Return the selected paradigm, with the current buffer tacked onto the
beginning of the list."
Miles Bader's avatar
Miles Bader committed
284 285 286 287
  (let* ((agent mail-user-agent)
	 (compose (get mail-user-agent 'composefunc)))
    ;; Sanity check.  If this fails then we'll try to use the SENDMAIL
    ;; protocol, otherwise we must signal an error.
288
    (if (not (and compose (functionp compose)))
Miles Bader's avatar
Miles Bader committed
289 290 291
	(progn
	  (setq agent 'sendmail-user-agent
		compose (get agent 'composefunc))
292
	  (if (not (and compose (functionp compose)))
293
	      (error "Could not find a valid `mail-user-agent'")
Miles Bader's avatar
Miles Bader committed
294
	    (ding)
295
	    (message "`%s' is an invalid `mail-user-agent'; using `sendmail-user-agent'"
Miles Bader's avatar
Miles Bader committed
296 297 298 299 300 301
		     mail-user-agent)
	    )))
    (funcall compose)
    agent))


302
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
303 304
(defun reporter-submit-bug-report
  (address pkgname varlist &optional pre-hooks post-hooks salutation)
Gerd Moellmann's avatar
Gerd Moellmann committed
305
"Begin submitting a bug report via email.
306

Gerd Moellmann's avatar
Gerd Moellmann committed
307 308 309
ADDRESS is the email address for the package's maintainer.  PKGNAME is
the name of the package (if you want to include version numbers,
you must put them into PKGNAME before calling this function).
Dave Love's avatar
Dave Love committed
310 311 312
Optional PRE-HOOKS and POST-HOOKS are passed to `reporter-dump-state'.
Optional SALUTATION is inserted at the top of the mail buffer,
and point is left after the salutation.
Richard M. Stallman's avatar
Richard M. Stallman committed
313

Gerd Moellmann's avatar
Gerd Moellmann committed
314 315 316 317 318
VARLIST is the list of variables to dump (see `reporter-dump-state'
for details).  The optional argument PRE-HOOKS and POST-HOOKS are
passed to `reporter-dump-state'.  Optional argument SALUTATION is text
to be inserted at the top of the mail buffer; in that case, point is
left after that text.
Richard M. Stallman's avatar
Richard M. Stallman committed
319

Gerd Moellmann's avatar
Gerd Moellmann committed
320 321 322 323
This function prompts for a summary if `reporter-prompt-for-summary-p'
is non-nil.

This function does not send a message; it uses the given information
324
to initialize a message, which the user can then edit and finally send
Stefan Monnier's avatar
Stefan Monnier committed
325
\(or decline to send).  The variable `mail-user-agent' controls which
Gerd Moellmann's avatar
Gerd Moellmann committed
326
mail-sending package is used for editing and sending the message."
Richard M. Stallman's avatar
Richard M. Stallman committed
327
  (let ((reporter-eval-buffer (current-buffer))
328 329
	final-resting-place
	after-sep-pos
330 331
	(reporter-status-message "Formatting bug report buffer...")
	(reporter-status-count 0)
332
	(problem (and reporter-prompt-for-summary-p
Miles Bader's avatar
Miles Bader committed
333 334 335 336 337 338 339
		      (read-string (if (stringp reporter-prompt-for-summary-p)
				       reporter-prompt-for-summary-p
				     "(Very) brief summary of problem: "))))
	(agent (reporter-compose-outgoing))
	(mailbuf (current-buffer))
	hookvar)
    ;; do the work
Richard M. Stallman's avatar
Richard M. Stallman committed
340
    (require 'sendmail)
Miles Bader's avatar
Miles Bader committed
341 342
    ;; If mailbuf did not get made visible before, make it visible now.
    (let (same-window-buffer-names same-window-regexps)
343
      (pop-to-buffer mailbuf)
Miles Bader's avatar
Miles Bader committed
344 345
      ;; Just in case the original buffer is not visible now, bring it
      ;; back somewhere
346
      (and pop-up-windows (display-buffer reporter-eval-buffer)))
Richard M. Stallman's avatar
Richard M. Stallman committed
347
    (goto-char (point-min))
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    (mail-position-on-field "to")
    (insert address)
    ;; insert problem summary if available
    (if (and reporter-prompt-for-summary-p problem pkgname)
	(progn
	  (mail-position-on-field "subject")
	  (insert pkgname "; " problem)))
    ;; move point to the body of the message
    (mail-text)
    (forward-line 1)
    (setq after-sep-pos (point))
    (and salutation (insert "\n" salutation "\n\n"))
    (unwind-protect
	(progn
	  (setq final-resting-place (point-marker))
	  (insert "\n\n")
	  (reporter-dump-state pkgname varlist pre-hooks post-hooks)
	  (goto-char final-resting-place))
      (set-marker final-resting-place nil))
367 368

    ;; save initial text and set up the `no-empty-submission' hook.
Miles Bader's avatar
Miles Bader committed
369 370 371 372 373 374 375 376
    ;; This only works for mailers that support a pre-send hook, and
    ;; for which the paradigm has a non-nil value for the `hookvar'
    ;; key in its agent (i.e. sendmail.el's mail-send-hook).
    (save-excursion
      (goto-char (point-max))
      (skip-chars-backward " \t\n")
      (setq reporter-initial-text (buffer-substring after-sep-pos (point))))
    (if (setq hookvar (get agent 'hookvar))
Stefan Monnier's avatar
Stefan Monnier committed
377
	(add-hook hookvar 'reporter-bug-hook nil t))
Miles Bader's avatar
Miles Bader committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

    ;; compose the minibuf message and display this.
    (let* ((sendkey-whereis (where-is-internal
			     (get agent 'sendfunc) nil t))
	   (abortkey-whereis (where-is-internal
			      (get agent 'abortfunc) nil t))
	   (sendkey (if sendkey-whereis
			(key-description sendkey-whereis)
		      "C-c C-c"))   ; TBD: BOGUS hardcode
	   (abortkey (if abortkey-whereis
			 (key-description abortkey-whereis)
		       "M-x kill-buffer"))  ; TBD: BOGUS hardcode
	   )
      (message "Please enter your report.  Type %s to send, %s to abort."
	       sendkey abortkey))
Richard M. Stallman's avatar
Richard M. Stallman committed
393 394
    ))

395
(defun reporter-bug-hook ()
Dave Love's avatar
Dave Love committed
396
  "Prohibit sending mail if empty bug report."
397 398
  (let ((after-sep-pos
	 (save-excursion
399
	   (rfc822-goto-eoh)
400 401 402 403 404 405 406 407 408
	   (forward-line 1)
	   (point))))
    (save-excursion
      (goto-char (point-max))
      (skip-chars-backward " \t\n")
      (if (and (= (- (point) after-sep-pos)
		  (length reporter-initial-text))
	       (string= (buffer-substring after-sep-pos (point))
			reporter-initial-text))
409
	  (error "Empty bug report cannot be sent"))
410 411
      )))

Miles Bader's avatar
Miles Bader committed
412 413

(provide 'reporter)
Miles Bader's avatar
Miles Bader committed
414 415

;;; arch-tag: 33612ff4-fbbc-4be2-b183-560ce9e0199b
Richard M. Stallman's avatar
Richard M. Stallman committed
416
;;; reporter.el ends here