Merge changes from emacs-23 branch.
[bpt/emacs.git] / lisp / mail / emacsbug.el
1 ;;; emacsbug.el --- command to report Emacs bugs to appropriate mailing list
2
3 ;; Copyright (C) 1985, 1994, 1997, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: K. Shane Hartman
8 ;; Maintainer: FSF
9 ;; Keywords: maint mail
10 ;; Package: emacs
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 3 of the License, or
17 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; `M-x report-emacs-bug' starts an email note to the Emacs maintainers
30 ;; describing a problem. You need to be able to send mail from Emacs
31 ;; to complete the process. Alternatively, compose the bug report in
32 ;; Emacs then paste it into your normal mail client.
33
34 ;;; Code:
35
36 (defgroup emacsbug nil
37 "Sending Emacs bug reports."
38 :group 'maint
39 :group 'mail)
40
41 (define-obsolete-variable-alias 'report-emacs-bug-pretest-address
42 'report-emacs-bug-address "24.1")
43
44 (defcustom report-emacs-bug-address "bug-gnu-emacs@gnu.org"
45 "Address of mailing list for GNU Emacs bugs."
46 :group 'emacsbug
47 :type 'string)
48
49 (defcustom report-emacs-bug-no-confirmation nil
50 "If non-nil, suppress the confirmations asked for the sake of novice users."
51 :group 'emacsbug
52 :type 'boolean)
53
54 (defcustom report-emacs-bug-no-explanations nil
55 "If non-nil, suppress the explanations given for the sake of novice users."
56 :group 'emacsbug
57 :type 'boolean)
58
59 ;; User options end here.
60
61
62 (defvar report-emacs-bug-orig-text nil
63 "The automatically-created initial text of the bug report.")
64
65 (defvar report-emacs-bug-send-command nil
66 "Name of the command to send the bug report, as a string.")
67 (make-variable-buffer-local 'report-emacs-bug-send-command)
68
69 (defvar report-emacs-bug-send-hook nil
70 "Hook run before sending the bug report.")
71 (make-variable-buffer-local 'report-emacs-bug-send-hook)
72
73 (declare-function x-server-vendor "xfns.c" (&optional terminal))
74 (declare-function x-server-version "xfns.c" (&optional terminal))
75 (declare-function message-sort-headers "message" ())
76 (defvar message-strip-special-text-properties)
77
78 (defun report-emacs-bug-can-use-xdg-email ()
79 "Check if xdg-email can be used, i.e. we are on Gnome, KDE or xfce4."
80 (and (getenv "DISPLAY")
81 (executable-find "xdg-email")
82 (or (getenv "GNOME_DESKTOP_SESSION_ID")
83 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
84 (condition-case nil
85 (eq 0 (call-process
86 "dbus-send" nil nil nil
87 "--dest=org.gnome.SessionManager"
88 "--print-reply"
89 "/org/gnome/SessionManager"
90 "org.gnome.SessionManager.CanShutdown"))
91 (error nil))
92 (equal (getenv "KDE_FULL_SESSION") "true")
93 (condition-case nil
94 (eq 0 (call-process
95 "/bin/sh" nil nil nil
96 "-c"
97 "xprop -root _DT_SAVE_MODE|grep xfce4"))
98 (error nil)))))
99
100 (defun report-emacs-bug-insert-to-mailer ()
101 (interactive)
102 (save-excursion
103 (let* ((to (progn
104 (goto-char (point-min))
105 (forward-line)
106 (and (looking-at "^To: \\(.*\\)")
107 (match-string-no-properties 1))))
108 (subject (progn
109 (forward-line)
110 (and (looking-at "^Subject: \\(.*\\)")
111 (match-string-no-properties 1))))
112 (body (progn
113 (forward-line 2)
114 (if (> (point-max) (point))
115 (buffer-substring-no-properties (point) (point-max))))))
116 (if (and to subject body)
117 (start-process "xdg-email" nil "xdg-email"
118 "--subject" subject
119 "--body" body
120 (concat "mailto:" to))
121 (error "Subject, To or body not found")))))
122
123
124 ;;;###autoload
125 (defun report-emacs-bug (topic &optional recent-keys)
126 "Report a bug in GNU Emacs.
127 Prompts for bug subject. Leaves you in a mail buffer."
128 ;; This strange form ensures that (recent-keys) is the value before
129 ;; the bug subject string is read.
130 (interactive (reverse (list (recent-keys) (read-string "Bug Subject: "))))
131 ;; The syntax `version;' is preferred to `[version]' because the
132 ;; latter could be mistakenly stripped by mailing software.
133 (if (eq system-type 'ms-dos)
134 (setq topic (concat emacs-version "; " topic))
135 (when (string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
136 (setq topic (concat (match-string 1 emacs-version) "; " topic))))
137 (let ((from-buffer (current-buffer))
138 ;; Put these properties on semantically-void text.
139 ;; report-emacs-bug-hook deletes these regions before sending.
140 (prompt-properties '(field emacsbug-prompt
141 intangible but-helpful
142 rear-nonsticky t))
143 (can-xdg-email (report-emacs-bug-can-use-xdg-email))
144 user-point message-end-point)
145 (setq message-end-point
146 (with-current-buffer (get-buffer-create "*Messages*")
147 (point-max-marker)))
148 (compose-mail report-emacs-bug-address topic)
149 ;; The rest of this does not execute if the user was asked to
150 ;; confirm and said no.
151 (when (eq major-mode 'message-mode)
152 ;; Message-mode sorts the headers before sending. We sort now so
153 ;; that report-emacs-bug-orig-text remains valid. (Bug#5178)
154 (message-sort-headers)
155 ;; Stop message-mode stealing the properties we will add.
156 (set (make-local-variable 'message-strip-special-text-properties) nil))
157 (rfc822-goto-eoh)
158 (forward-line 1)
159 (let ((signature (buffer-substring (point) (point-max))))
160 (delete-region (point) (point-max))
161 (insert signature)
162 (backward-char (length signature)))
163 (unless report-emacs-bug-no-explanations
164 ;; Insert warnings for novice users.
165 (when (string-match "@gnu\\.org$" report-emacs-bug-address)
166 (insert "This bug report will be sent to the Free Software Foundation,\n")
167 (let ((pos (point)))
168 (insert "not to your local site managers!")
169 (overlay-put (make-overlay pos (point)) 'face 'highlight)))
170 (insert "\nPlease write in ")
171 (let ((pos (point)))
172 (insert "English")
173 (overlay-put (make-overlay pos (point)) 'face 'highlight))
174 (insert " if possible, because the Emacs maintainers
175 usually do not have translators to read other languages for them.\n\n")
176 (insert (format "Your report will be posted to the %s mailing list"
177 report-emacs-bug-address))
178 (insert "\nand the gnu.emacs.bug news group, and at http://debbugs.gnu.org.\n\n"))
179
180 (insert "Please describe exactly what actions triggered the bug\n"
181 "and the precise symptoms of the bug. If you can, give\n"
182 "a recipe starting from `emacs -Q':\n\n")
183 (add-text-properties (save-excursion
184 (rfc822-goto-eoh)
185 (line-beginning-position 2))
186 (point)
187 prompt-properties)
188 (setq user-point (point))
189 (insert "\n\n")
190
191 (insert "If Emacs crashed, and you have the Emacs process in the gdb debugger,\n"
192 "please include the output from the following gdb commands:\n"
193 " `bt full' and `xbacktrace'.\n")
194
195 (let ((debug-file (expand-file-name "DEBUG" data-directory)))
196 (if (file-readable-p debug-file)
197 (insert "For information about debugging Emacs, please read the file\n"
198 debug-file ".\n")))
199 (add-text-properties (1+ user-point) (point) prompt-properties)
200
201 (insert "\n\nIn " (emacs-version) "\n")
202 (if (fboundp 'x-server-vendor)
203 (condition-case nil
204 ;; This is used not only for X11 but also W32 and others.
205 (insert "Windowing system distributor `" (x-server-vendor)
206 "', version "
207 (mapconcat 'number-to-string (x-server-version) ".") "\n")
208 (error t)))
209 (if (and system-configuration-options
210 (not (equal system-configuration-options "")))
211 (insert "configured using `configure "
212 system-configuration-options "'\n\n"))
213 (insert "Important settings:\n")
214 (mapc
215 '(lambda (var)
216 (insert (format " value of $%s: %s\n" var (getenv var))))
217 '("LC_ALL" "LC_COLLATE" "LC_CTYPE" "LC_MESSAGES"
218 "LC_MONETARY" "LC_NUMERIC" "LC_TIME" "LANG" "XMODIFIERS"))
219 (insert (format " locale-coding-system: %s\n" locale-coding-system))
220 (insert (format " default enable-multibyte-characters: %s\n"
221 (default-value 'enable-multibyte-characters)))
222 (insert "\n")
223 (insert (format "Major mode: %s\n"
224 (format-mode-line
225 (buffer-local-value 'mode-name from-buffer)
226 nil nil from-buffer)))
227 (insert "\n")
228 (insert "Minor modes in effect:\n")
229 (dolist (mode minor-mode-list)
230 (and (boundp mode) (buffer-local-value mode from-buffer)
231 (insert (format " %s: %s\n" mode
232 (buffer-local-value mode from-buffer)))))
233 (insert "\n")
234 (insert "Recent input:\n")
235 (let ((before-keys (point)))
236 (insert (mapconcat (lambda (key)
237 (if (or (integerp key)
238 (symbolp key)
239 (listp key))
240 (single-key-description key)
241 (prin1-to-string key nil)))
242 (or recent-keys (recent-keys))
243 " "))
244 (save-restriction
245 (narrow-to-region before-keys (point))
246 (goto-char before-keys)
247 (while (progn (move-to-column 50) (not (eobp)))
248 (search-forward " " nil t)
249 (insert "\n"))))
250 (let ((message-buf (get-buffer "*Messages*")))
251 (if message-buf
252 (let (beg-pos
253 (end-pos message-end-point))
254 (with-current-buffer message-buf
255 (goto-char end-pos)
256 (forward-line -10)
257 (setq beg-pos (point)))
258 (insert "\n\nRecent messages:\n")
259 (insert-buffer-substring message-buf beg-pos end-pos))))
260 ;; After Recent messages, to avoid the messages produced by
261 ;; list-load-path-shadows.
262 (unless (looking-back "\n")
263 (insert "\n"))
264 (insert "\n")
265 (insert "Load-path shadows:\n")
266 (message "Checking for load-path shadows...")
267 (let ((shadows (list-load-path-shadows t)))
268 (message "Checking for load-path shadows...done")
269 (insert (if (zerop (length shadows))
270 "None found.\n"
271 shadows)))
272 (insert (format "\nFeatures:\n%s\n" features))
273 (fill-region (line-beginning-position 0) (point))
274 ;; This is so the user has to type something in order to send easily.
275 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
276 (define-key (current-local-map) "\C-c\C-i" 'report-emacs-bug-info)
277 (if can-xdg-email
278 (define-key (current-local-map) "\C-cm"
279 'report-emacs-bug-insert-to-mailer))
280 ;; Could test major-mode instead.
281 (cond ((memq mail-user-agent '(message-user-agent gnus-user-agent))
282 (setq report-emacs-bug-send-command "message-send-and-exit"
283 report-emacs-bug-send-hook 'message-send-hook))
284 ((eq mail-user-agent 'sendmail-user-agent)
285 (setq report-emacs-bug-send-command "mail-send-and-exit"
286 report-emacs-bug-send-hook 'mail-send-hook))
287 ((eq mail-user-agent 'mh-e-user-agent)
288 (setq report-emacs-bug-send-command "mh-send-letter"
289 report-emacs-bug-send-hook 'mh-before-send-letter-hook)))
290 (unless report-emacs-bug-no-explanations
291 (with-output-to-temp-buffer "*Bug Help*"
292 (princ "While in the mail buffer:\n\n")
293 (if report-emacs-bug-send-command
294 (princ (substitute-command-keys
295 (format " Type \\[%s] to send the bug report.\n"
296 report-emacs-bug-send-command))))
297 (princ (substitute-command-keys
298 " Type \\[kill-buffer] RET to cancel (don't send it).\n"))
299 (if can-xdg-email
300 (princ (substitute-command-keys
301 " Type \\[report-emacs-bug-insert-to-mailer] to insert text to you preferred mail program.\n")))
302 (terpri)
303 (princ (substitute-command-keys
304 " Type \\[report-emacs-bug-info] to visit in Info the Emacs Manual section
305 about when and how to write a bug report, and what
306 information you should include to help fix the bug.")))
307 (shrink-window-if-larger-than-buffer (get-buffer-window "*Bug Help*")))
308 ;; Make it less likely people will send empty messages.
309 (if report-emacs-bug-send-hook
310 (add-hook report-emacs-bug-send-hook 'report-emacs-bug-hook nil t))
311 (goto-char (point-max))
312 (skip-chars-backward " \t\n")
313 (make-local-variable 'report-emacs-bug-orig-text)
314 (setq report-emacs-bug-orig-text
315 (buffer-substring-no-properties (point-min) (point)))
316 (goto-char user-point)))
317
318 (defun report-emacs-bug-info ()
319 "Go to the Info node on reporting Emacs bugs."
320 (interactive)
321 (info "(emacs)Bugs"))
322
323 (defun report-emacs-bug-hook ()
324 "Do some checking before sending a bug report."
325 (save-excursion
326 (goto-char (point-max))
327 (skip-chars-backward " \t\n")
328 (and (= (- (point) (point-min))
329 (length report-emacs-bug-orig-text))
330 (string-equal (buffer-substring-no-properties (point-min) (point))
331 report-emacs-bug-orig-text)
332 (error "No text entered in bug report"))
333 ;; Check the buffer contents and reject non-English letters.
334 ;; FIXME message-mode probably does this anyway.
335 (goto-char (point-min))
336 (skip-chars-forward "\0-\177")
337 (unless (eobp)
338 (if (or report-emacs-bug-no-confirmation
339 (y-or-n-p "Convert non-ASCII letters to hexadecimal? "))
340 (while (progn (skip-chars-forward "\0-\177")
341 (not (eobp)))
342 (let ((ch (following-char)))
343 (delete-char 1)
344 (insert (format "=%02x" ch))))))
345
346 ;; The last warning for novice users.
347 (unless (or report-emacs-bug-no-confirmation
348 (yes-or-no-p
349 "Send this bug report to the Emacs maintainers? "))
350 (goto-char (point-min))
351 (if (search-forward "To: ")
352 (delete-region (point) (line-end-position)))
353 (if report-emacs-bug-send-hook
354 (kill-local-variable report-emacs-bug-send-hook))
355 (with-output-to-temp-buffer "*Bug Help*"
356 (princ (substitute-command-keys
357 (format "\
358 You invoked the command M-x report-emacs-bug,
359 but you decided not to mail the bug report to the Emacs maintainers.
360
361 If you want to mail it to someone else instead,
362 please insert the proper e-mail address after \"To: \",
363 and send the mail again%s."
364 (if report-emacs-bug-send-command
365 (format " using \\[%s]"
366 report-emacs-bug-send-command)
367 "")))))
368 (error "M-x report-emacs-bug was cancelled, please read *Bug Help* buffer"))
369
370 ;; Delete the uninteresting text that was just to help fill out the report.
371 (rfc822-goto-eoh)
372 (forward-line 1)
373 (let ((pos (1- (point))))
374 (while (setq pos (text-property-any pos (point-max)
375 'field 'emacsbug-prompt))
376 (delete-region pos (field-end (1+ pos)))))))
377
378 (provide 'emacsbug)
379
380 ;;; emacsbug.el ends here