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