* mail/vms-pmail.el (insert-signature): Don't use end-of-buffer.
[bpt/emacs.git] / lisp / mail / reporter.el
CommitLineData
bf3b07d3
RS
1;;; reporter.el --- customizable bug reporting of lisp programs
2
f2e3589a 3;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2001, 2002, 2003,
d7a0267c 4;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
b578f267 5
72fe4615 6;; Author: 1993-1998 Barry A. Warsaw
87617309 7;; Maintainer: FSF
bf3b07d3 8;; Created: 19-Apr-1993
c71437cf 9;; Keywords: maint mail tools
bf3b07d3 10
54d2ecd3 11;; This file is part of GNU Emacs.
9ee7c69d
RS
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
bf3b07d3 14;; it under the terms of the GNU General Public License as published by
ceaeecb0 15;; the Free Software Foundation; either version 3, or (at your option)
9ee7c69d
RS
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
bf3b07d3
RS
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.
9ee7c69d 22
bf3b07d3 23;; You should have received a copy of the GNU General Public License
b578f267 24;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
bf3b07d3 27
4f40f169 28;;; Commentary:
b578f267 29
c71437cf
MB
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
72fe4615
RS
34;; mail. See that variable for details (it is no longer defined in
35;; this file).
c71437cf 36
c71437cf
MB
37;; Lisp Package Authors
38;; ====================
72fe4615
RS
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
c71437cf 47;; configuration variables.
bf3b07d3
RS
48;;
49;; Do a "C-h f reporter-submit-bug-report" for more information.
50;; Here's an example usage:
51;;
4f40f169
RS
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)
72fe4615 57;; (require 'reporter)
4f40f169
RS
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
31257c3f 66;;; Code:
c71437cf 67
72fe4615 68\f
4b4052b2
RS
69;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
70;; Package author interface variables
71
4f40f169
RS
72(defvar reporter-prompt-for-summary-p nil
73 "Interface variable controlling prompting for problem summary.
74When non-nil, `reporter-submit-bug-report' prompts the user for a
75brief summary of the problem, and puts this summary on the Subject:
c71437cf
MB
76line. If this variable is a string, that string is used as the prompt
77string.
4f40f169 78
87617309 79Default behavior is to not prompt (i.e. nil). If you want reporter to
c71437cf 80prompt, you should `let' bind this variable before calling
4f40f169
RS
81`reporter-submit-bug-report'. Note that this variable is not
82buffer-local so you should never just `setq' it.")
83
313f3cb4 84(defvar reporter-dont-compact-list nil
22cc6690 85 "Interface variable controlling compacting of list values.
313f3cb4
RS
86When non-nil, this must be a list of variable symbols. When a
87variable containing a list value is formatted in the bug report mail
88buffer, it normally is compacted so that its value fits one the fewest
89number of lines. If the variable's symbol appears in this list, its
90value is printed in a more verbose style, specifically, one elemental
91sexp per line.
92
93Note 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'
95bind it.")
bf3b07d3 96
c71437cf 97;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4b4052b2
RS
98;; End of editable variables
99
c71437cf 100\f
9ee9b53e
RS
101(defvar reporter-eval-buffer nil
102 "Buffer to retrieve variable's value from.
103This is necessary to properly support the printing of buffer-local
104variables. Current buffer will always be the mail buffer being
105composed.")
9ee7c69d 106
4f40f169
RS
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
c71437cf 111
313f3cb4 112\f
c71437cf 113;; status feedback to the user
313f3cb4
RS
114(defvar reporter-status-message nil)
115(defvar reporter-status-count nil)
116
117(defun reporter-update-status ()
87617309 118 "Periodically output a status message."
313f3cb4
RS
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)))
4f40f169 124
bf3b07d3 125\f
c71437cf 126;; dumping/pretty printing of values
313f3cb4 127(defun reporter-beautify-list (maxwidth compact-p)
87617309 128 "Pretty print a list."
313f3cb4 129 (reporter-update-status)
72fe4615
RS
130 (let ((move t)
131 linebreak indent-enclosing-p indent-p here)
313f3cb4
RS
132 (condition-case nil ;loop exit
133 (progn
134 (down-list 1)
135 (setq indent-enclosing-p t)
72fe4615 136 (while move
313f3cb4 137 (setq here (point))
72fe4615
RS
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)
313f3cb4
RS
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)
87617309 166 "A better lisp indentation style for bug reporting."
313f3cb4
RS
167 (save-excursion
168 (goto-char (1+ (nth 1 state)))
169 (current-column)))
170
4f40f169 171(defun reporter-dump-variable (varsym mailbuf)
87617309
DL
172 "Pretty-print the value of the variable in symbol VARSYM.
173MAILBUF is the mail buffer being composed."
313f3cb4 174 (reporter-update-status)
4f40f169
RS
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)
313f3cb4 181 (maxwidth (1- (window-width)))
4f40f169
RS
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))
313f3cb4 190 (lisp-indent-line)
4f40f169
RS
191 ;; clean up lists, but only if the line as printed was long
192 ;; enough to wrap
313f3cb4
RS
193 (if (and val ;nil is a list, but short
194 (listp val)
195 (<= maxwidth (current-column)))
4f40f169 196 (save-excursion
313f3cb4
RS
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))))
4f40f169
RS
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) " ")))
c71437cf
MB
208 (error
209 (error ""))))
bf3b07d3
RS
210
211(defun reporter-dump-state (pkgname varlist pre-hooks post-hooks)
87617309
DL
212 "Dump the state of the mode specific variables.
213PKGNAME contains the name of the mode as it will appear in the bug
214report (you must explicitly concat any version numbers).
215
216VARLIST is the list of variables to dump. Each element in
217VARLIST can be a variable symbol, or a cons cell. If a symbol,
218this will be passed to `reporter-dump-variable' for insertion
219into the mail buffer. If a cons cell, the car must be a variable
220symbol and the cdr must be a function which will be `funcall'd
221with arguments the symbol and the mail buffer being composed. Use
222this to write your own custom variable value printers for
223specific variables.
224
225Note that the global variable `reporter-eval-buffer' will be bound to
226the buffer in which `reporter-submit-bug-report' was invoked. If you
227want to print the value of a buffer local variable, you should wrap
228the `eval' call in your custom printer inside a `set-buffer' (and
229probably a `save-excursion'). `reporter-dump-variable' handles this
230properly.
231
232PRE-HOOKS is run after the Emacs version and PKGNAME are inserted, but
233before the VARLIST is dumped. POST-HOOKS is run after the VARLIST is
234dumped."
bf3b07d3
RS
235 (let ((buffer (current-buffer)))
236 (set-buffer buffer)
4f40f169
RS
237 (insert "Emacs : " (emacs-version) "\n")
238 (and pkgname
239 (insert "Package: " pkgname "\n"))
bf3b07d3 240 (run-hooks 'pre-hooks)
4f40f169
RS
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)
cfedf6cc 255 (mapc
4f40f169
RS
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)
313f3cb4
RS
265 (lisp-indent-line)
266 (insert ")\n"))
134c6d58 267 (insert-buffer-substring elbuf))
4f40f169
RS
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."))))
bf3b07d3
RS
272 (run-hooks 'post-hooks)
273 ))
274
4f40f169 275\f
c71437cf 276(defun reporter-compose-outgoing ()
87617309
DL
277 "Compose the outgoing mail buffer.
278
279Return the selected paradigm, with the current buffer tacked onto the
280beginning of the list."
c71437cf
MB
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.
c6d354e7 285 (if (not (and compose (functionp compose)))
c71437cf
MB
286 (progn
287 (setq agent 'sendmail-user-agent
288 compose (get agent 'composefunc))
c6d354e7 289 (if (not (and compose (functionp compose)))
31257c3f 290 (error "Could not find a valid `mail-user-agent'")
c71437cf 291 (ding)
31257c3f 292 (message "`%s' is an invalid `mail-user-agent'; using `sendmail-user-agent'"
c71437cf
MB
293 mail-user-agent)
294 )))
295 (funcall compose)
296 agent))
297
298\f
4f40f169 299;;;###autoload
bf3b07d3
RS
300(defun reporter-submit-bug-report
301 (address pkgname varlist &optional pre-hooks post-hooks salutation)
93baa0ea 302"Begin submitting a bug report via email.
4f40f169 303
93baa0ea
GM
304ADDRESS is the email address for the package's maintainer. PKGNAME is
305the name of the package (if you want to include version numbers,
306you must put them into PKGNAME before calling this function).
87617309
DL
307Optional PRE-HOOKS and POST-HOOKS are passed to `reporter-dump-state'.
308Optional SALUTATION is inserted at the top of the mail buffer,
309and point is left after the salutation.
bf3b07d3 310
93baa0ea
GM
311VARLIST is the list of variables to dump (see `reporter-dump-state'
312for details). The optional argument PRE-HOOKS and POST-HOOKS are
313passed to `reporter-dump-state'. Optional argument SALUTATION is text
314to be inserted at the top of the mail buffer; in that case, point is
315left after that text.
bf3b07d3 316
93baa0ea
GM
317This function prompts for a summary if `reporter-prompt-for-summary-p'
318is non-nil.
319
320This function does not send a message; it uses the given information
c6c8cba4 321to initialize a message, which the user can then edit and finally send
1407571d 322\(or decline to send). The variable `mail-user-agent' controls which
93baa0ea 323mail-sending package is used for editing and sending the message."
9ee9b53e 324 (let ((reporter-eval-buffer (current-buffer))
4f40f169
RS
325 final-resting-place
326 after-sep-pos
313f3cb4
RS
327 (reporter-status-message "Formatting bug report buffer...")
328 (reporter-status-count 0)
4f40f169 329 (problem (and reporter-prompt-for-summary-p
c71437cf
MB
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
bf3b07d3 337 (require 'sendmail)
c71437cf
MB
338 ;; If mailbuf did not get made visible before, make it visible now.
339 (let (same-window-buffer-names same-window-regexps)
4302ef50 340 (pop-to-buffer mailbuf)
c71437cf
MB
341 ;; Just in case the original buffer is not visible now, bring it
342 ;; back somewhere
72fe4615 343 (and pop-up-windows (display-buffer reporter-eval-buffer)))
bf3b07d3 344 (goto-char (point-min))
fc48f381
RS
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))
4f40f169
RS
364
365 ;; save initial text and set up the `no-empty-submission' hook.
c71437cf
MB
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))
1407571d 374 (add-hook hookvar 'reporter-bug-hook nil t))
c71437cf
MB
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))
bf3b07d3
RS
390 ))
391
4f40f169 392(defun reporter-bug-hook ()
87617309 393 "Prohibit sending mail if empty bug report."
4f40f169
RS
394 (let ((after-sep-pos
395 (save-excursion
0b28758d 396 (rfc822-goto-eoh)
4f40f169
RS
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))
72fe4615 406 (error "Empty bug report cannot be sent"))
4f40f169
RS
407 )))
408
409\f
c71437cf 410(provide 'reporter)
ab5796a9
MB
411
412;;; arch-tag: 33612ff4-fbbc-4be2-b183-560ce9e0199b
bf3b07d3 413;;; reporter.el ends here