Merge from emacs-24; up to 2012-11-24T16:58:43Z!cyd@gnu.org
[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-2012
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: FSF
8 ;; Keywords: maint mail
9 ;; Package: emacs
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 3 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; `M-x report-emacs-bug' starts an email note to the Emacs maintainers
29 ;; describing a problem. You need to be able to send mail from Emacs
30 ;; to complete the process. Alternatively, compose the bug report in
31 ;; Emacs then paste it into your normal mail client.
32
33 ;;; Code:
34
35 (require 'sendmail)
36 (require 'message)
37
38 (defgroup emacsbug nil
39 "Sending Emacs bug reports."
40 :group 'maint
41 :group 'mail)
42
43 (define-obsolete-variable-alias 'report-emacs-bug-pretest-address
44 'report-emacs-bug-address "24.1")
45
46 (defcustom report-emacs-bug-address "bug-gnu-emacs@gnu.org"
47 "Address of mailing list for GNU Emacs bugs."
48 :group 'emacsbug
49 :type 'string)
50
51 (defcustom report-emacs-bug-no-confirmation nil
52 "If non-nil, suppress the confirmations asked for the sake of novice users."
53 :group 'emacsbug
54 :type 'boolean)
55
56 (defcustom report-emacs-bug-no-explanations nil
57 "If non-nil, suppress the explanations given for the sake of novice users."
58 :group 'emacsbug
59 :type 'boolean)
60
61 ;; User options end here.
62
63 (defvar report-emacs-bug-orig-text nil
64 "The automatically-created initial text of the bug report.")
65
66 (defvar report-emacs-bug-send-command nil
67 "Name of the command to send the bug report, as a string.")
68 (make-variable-buffer-local 'report-emacs-bug-send-command)
69
70 (defvar report-emacs-bug-send-hook nil
71 "Hook run before sending the bug report.")
72 (make-variable-buffer-local 'report-emacs-bug-send-hook)
73
74 (declare-function x-server-vendor "xfns.c" (&optional terminal))
75 (declare-function x-server-version "xfns.c" (&optional terminal))
76 (declare-function message-sort-headers "message" ())
77 (defvar message-strip-special-text-properties)
78
79 (defun report-emacs-bug-can-use-osx-open ()
80 "Return non-nil if the OS X \"open\" command is available for mailing."
81 (and (featurep 'ns)
82 (equal (executable-find "open") "/usr/bin/open")
83 (memq system-type '(darwin))))
84
85 ;; FIXME this duplicates much of the logic from browse-url-can-use-xdg-open.
86 (defun report-emacs-bug-can-use-xdg-email ()
87 "Return non-nil if the \"xdg-email\" command can be used.
88 xdg-email is a desktop utility that calls your preferred mail client.
89 This requires you to be running either Gnome, KDE, or Xfce4."
90 (and (getenv "DISPLAY")
91 (executable-find "xdg-email")
92 (or (getenv "GNOME_DESKTOP_SESSION_ID")
93 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
94 (condition-case nil
95 (eq 0 (call-process
96 "dbus-send" nil nil nil
97 "--dest=org.gnome.SessionManager"
98 "--print-reply"
99 "/org/gnome/SessionManager"
100 "org.gnome.SessionManager.CanShutdown"))
101 (error nil))
102 (equal (getenv "KDE_FULL_SESSION") "true")
103 ;; FIXME? browse-url-can-use-xdg-open also accepts LXDE.
104 ;; Is that no good here, or just overlooked?
105 (condition-case nil
106 (eq 0 (call-process
107 "/bin/sh" nil nil nil
108 "-c"
109 ;; FIXME use string-match rather than grep.
110 "xprop -root _DT_SAVE_MODE|grep xfce4"))
111 (error nil)))))
112
113 (defun report-emacs-bug-insert-to-mailer ()
114 "Send the message to your preferred mail client.
115 This requires either the OS X \"open\" command, or the freedesktop
116 \"xdg-email\" command to be available."
117 (interactive)
118 (save-excursion
119 ;; FIXME? use mail-fetch-field?
120 (let* ((to (progn
121 (goto-char (point-min))
122 (forward-line)
123 (and (looking-at "^To: \\(.*\\)")
124 (match-string-no-properties 1))))
125 (subject (progn
126 (forward-line)
127 (and (looking-at "^Subject: \\(.*\\)")
128 (match-string-no-properties 1))))
129 (body (progn
130 (forward-line 2)
131 (if (> (point-max) (point))
132 (buffer-substring-no-properties (point) (point-max))))))
133 (if (and to subject body)
134 (if (report-emacs-bug-can-use-osx-open)
135 (start-process "/usr/bin/open" nil "open"
136 (concat "mailto:" to
137 "?subject=" (url-hexify-string subject)
138 "&body=" (url-hexify-string body)))
139 (start-process "xdg-email" nil "xdg-email"
140 "--subject" subject
141 "--body" body
142 (concat "mailto:" to)))
143 (error "Subject, To or body not found")))))
144
145 ;;;###autoload
146 (defun report-emacs-bug (topic &optional recent-keys)
147 "Report a bug in GNU Emacs.
148 Prompts for bug subject. Leaves you in a mail buffer."
149 ;; This strange form ensures that (recent-keys) is the value before
150 ;; the bug subject string is read.
151 (interactive (reverse (list (recent-keys) (read-string "Bug Subject: "))))
152 ;; The syntax `version;' is preferred to `[version]' because the
153 ;; latter could be mistakenly stripped by mailing software.
154 (if (eq system-type 'ms-dos)
155 (setq topic (concat emacs-version "; " topic))
156 (when (string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
157 (setq topic (concat (match-string 1 emacs-version) "; " topic))))
158 (let ((from-buffer (current-buffer))
159 ;; Put these properties on semantically-void text.
160 ;; report-emacs-bug-hook deletes these regions before sending.
161 (prompt-properties '(field emacsbug-prompt
162 intangible but-helpful
163 rear-nonsticky t))
164 (can-insert-mail (or (report-emacs-bug-can-use-xdg-email)
165 (report-emacs-bug-can-use-osx-open)))
166 user-point message-end-point)
167 (setq message-end-point
168 (with-current-buffer (get-buffer-create "*Messages*")
169 (point-max-marker)))
170 (compose-mail report-emacs-bug-address topic)
171 ;; The rest of this does not execute if the user was asked to
172 ;; confirm and said no.
173 (when (eq major-mode 'message-mode)
174 ;; Message-mode sorts the headers before sending. We sort now so
175 ;; that report-emacs-bug-orig-text remains valid. (Bug#5178)
176 (message-sort-headers)
177 ;; Stop message-mode stealing the properties we will add.
178 (set (make-local-variable 'message-strip-special-text-properties) nil))
179 (rfc822-goto-eoh)
180 (forward-line 1)
181 ;; Move the mail signature to the proper place.
182 (let ((signature (buffer-substring (point) (point-max)))
183 (inhibit-read-only t))
184 (delete-region (point) (point-max))
185 (insert signature)
186 (backward-char (length signature)))
187 (unless report-emacs-bug-no-explanations
188 ;; Insert warnings for novice users.
189 (if (not (equal "bug-gnu-emacs@gnu.org" report-emacs-bug-address))
190 (insert (format "The report will be sent to %s.\n\n"
191 report-emacs-bug-address))
192 (insert "This bug report will be sent to the ")
193 (insert-button
194 "Bug-GNU-Emacs"
195 'face 'link
196 'help-echo (concat "mouse-2, RET: Follow this link")
197 'action (lambda (button)
198 (browse-url "http://lists.gnu.org/archive/html/bug-gnu-emacs/"))
199 'follow-link t)
200 (insert " mailing list\nand the GNU bug tracker at ")
201 (insert-button
202 "debbugs.gnu.org"
203 'face 'link
204 'help-echo (concat "mouse-2, RET: Follow this link")
205 'action (lambda (button)
206 (browse-url "http://debbugs.gnu.org/"))
207 'follow-link t)
208
209 (insert ". Please check that
210 the From: line contains a valid email address. After a delay of up
211 to one day, you should receive an acknowledgment at that address.
212
213 Please write in English if possible, as the Emacs maintainers
214 usually do not have translators for other languages.\n\n")))
215
216 (insert "Please describe exactly what actions triggered the bug, and\n"
217 "the precise symptoms of the bug. If you can, give a recipe\n"
218 "starting from `emacs -Q':\n\n")
219 (add-text-properties (save-excursion
220 (rfc822-goto-eoh)
221 (line-beginning-position 2))
222 (point)
223 prompt-properties)
224 (setq user-point (point))
225 (insert "\n\n")
226
227 (insert "If Emacs crashed, and you have the Emacs process in the gdb debugger,\n"
228 "please include the output from the following gdb commands:\n"
229 " `bt full' and `xbacktrace'.\n")
230
231 (let ((debug-file (expand-file-name "DEBUG" data-directory)))
232 (if (file-readable-p debug-file)
233 (insert "For information about debugging Emacs, please read the file\n"
234 debug-file ".\n")))
235 (add-text-properties (1+ user-point) (point) prompt-properties)
236
237 (insert "\n\nIn " (emacs-version) "\n")
238 (if (stringp emacs-bzr-version)
239 (insert "Bzr revision: " emacs-bzr-version "\n"))
240 (if (fboundp 'x-server-vendor)
241 (condition-case nil
242 ;; This is used not only for X11 but also W32 and others.
243 (insert "Windowing system distributor `" (x-server-vendor)
244 "', version "
245 (mapconcat 'number-to-string (x-server-version) ".") "\n")
246 (error t)))
247 (let ((lsb (with-temp-buffer
248 (if (eq 0 (ignore-errors
249 (call-process "lsb_release" nil '(t nil)
250 nil "-d")))
251 (buffer-string)))))
252 (if (stringp lsb)
253 (insert "System " lsb "\n")))
254 (when (and system-configuration-options
255 (not (equal system-configuration-options "")))
256 (insert "Configured using:\n `configure "
257 system-configuration-options "'\n\n")
258 (fill-region (line-beginning-position -1) (point)))
259 (insert "Important settings:\n")
260 (mapc
261 (lambda (var)
262 (let ((val (getenv var)))
263 (if val (insert (format " value of $%s: %s\n" var val)))))
264 '("EMACSDATA" "EMACSDOC" "EMACSLOADPATH" "EMACSPATH"
265 "LC_ALL" "LC_COLLATE" "LC_CTYPE" "LC_MESSAGES"
266 "LC_MONETARY" "LC_NUMERIC" "LC_TIME" "LANG" "XMODIFIERS"))
267 (insert (format " locale-coding-system: %s\n" locale-coding-system))
268 (insert (format " default enable-multibyte-characters: %s\n"
269 (default-value 'enable-multibyte-characters)))
270 (insert "\n")
271 (insert (format "Major mode: %s\n"
272 (format-mode-line
273 (buffer-local-value 'mode-name from-buffer)
274 nil nil from-buffer)))
275 (insert "\n")
276 (insert "Minor modes in effect:\n")
277 (dolist (mode minor-mode-list)
278 (and (boundp mode) (buffer-local-value mode from-buffer)
279 (insert (format " %s: %s\n" mode
280 (buffer-local-value mode from-buffer)))))
281 (insert "\n")
282 (insert "Recent input:\n")
283 (let ((before-keys (point)))
284 (insert (mapconcat (lambda (key)
285 (if (or (integerp key)
286 (symbolp key)
287 (listp key))
288 (single-key-description key)
289 (prin1-to-string key nil)))
290 (or recent-keys (recent-keys))
291 " "))
292 (save-restriction
293 (narrow-to-region before-keys (point))
294 (goto-char before-keys)
295 (while (progn (move-to-column 50) (not (eobp)))
296 (search-forward " " nil t)
297 (insert "\n"))))
298 (let ((message-buf (get-buffer "*Messages*")))
299 (if message-buf
300 (let (beg-pos
301 (end-pos message-end-point))
302 (with-current-buffer message-buf
303 (goto-char end-pos)
304 (forward-line -10)
305 (setq beg-pos (point)))
306 (insert "\n\nRecent messages:\n")
307 (insert-buffer-substring message-buf beg-pos end-pos))))
308 ;; After Recent messages, to avoid the messages produced by
309 ;; list-load-path-shadows.
310 (unless (looking-back "\n")
311 (insert "\n"))
312 (insert "\n")
313 (insert "Load-path shadows:\n")
314 (let* ((msg "Checking for load-path shadows...")
315 (result "done")
316 (shadows (progn (message "%s" msg)
317 (condition-case nil (list-load-path-shadows t)
318 (error
319 (setq result "error")
320 "Error during checking")))))
321 (message "%s%s" msg result)
322 (insert (if (zerop (length shadows))
323 "None found.\n"
324 shadows)))
325 (insert (format "\nFeatures:\n%s\n" features))
326 (fill-region (line-beginning-position 0) (point))
327 ;; This is so the user has to type something in order to send easily.
328 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
329 (define-key (current-local-map) "\C-c\C-i" 'info-emacs-bug)
330 (if can-insert-mail
331 (define-key (current-local-map) "\C-cm"
332 'report-emacs-bug-insert-to-mailer))
333 (setq report-emacs-bug-send-command (get mail-user-agent 'sendfunc)
334 report-emacs-bug-send-hook (get mail-user-agent 'hookvar))
335 (if report-emacs-bug-send-command
336 (setq report-emacs-bug-send-command
337 (symbol-name report-emacs-bug-send-command)))
338 (unless report-emacs-bug-no-explanations
339 (with-output-to-temp-buffer "*Bug Help*"
340 (princ "While in the mail buffer:\n\n")
341 (if report-emacs-bug-send-command
342 (princ (substitute-command-keys
343 (format " Type \\[%s] to send the bug report.\n"
344 report-emacs-bug-send-command))))
345 (princ (substitute-command-keys
346 " Type \\[kill-buffer] RET to cancel (don't send it).\n"))
347 (if can-insert-mail
348 (princ (substitute-command-keys
349 " Type \\[report-emacs-bug-insert-to-mailer] to copy text to your preferred mail program.\n")))
350 (terpri)
351 (princ (substitute-command-keys
352 " Type \\[info-emacs-bug] to visit in Info the Emacs Manual section
353 about when and how to write a bug report, and what
354 information you should include to help fix the bug.")))
355 (shrink-window-if-larger-than-buffer (get-buffer-window "*Bug Help*")))
356 ;; Make it less likely people will send empty messages.
357 (if report-emacs-bug-send-hook
358 (add-hook report-emacs-bug-send-hook 'report-emacs-bug-hook nil t))
359 (goto-char (point-max))
360 (skip-chars-backward " \t\n")
361 (make-local-variable 'report-emacs-bug-orig-text)
362 (setq report-emacs-bug-orig-text
363 (buffer-substring-no-properties (point-min) (point)))
364 (goto-char user-point)))
365
366 (define-obsolete-function-alias 'report-emacs-bug-info 'info-emacs-bug "24.3")
367
368 ;; It's the default mail mode, so it seems OK to use its features.
369 (autoload 'message-bogus-recipient-p "message")
370 (defvar message-send-mail-function)
371
372 (defun report-emacs-bug-hook ()
373 "Do some checking before sending a bug report."
374 (save-excursion
375 (goto-char (point-max))
376 (skip-chars-backward " \t\n")
377 (and (= (- (point) (point-min))
378 (length report-emacs-bug-orig-text))
379 (string-equal (buffer-substring-no-properties (point-min) (point))
380 report-emacs-bug-orig-text)
381 (error "No text entered in bug report"))
382 ;; Warning for novice users.
383 (unless (or report-emacs-bug-no-confirmation
384 (yes-or-no-p
385 "Send this bug report to the Emacs maintainers? "))
386 (goto-char (point-min))
387 (if (search-forward "To: ")
388 (delete-region (point) (line-end-position)))
389 (if report-emacs-bug-send-hook
390 (kill-local-variable report-emacs-bug-send-hook))
391 (with-output-to-temp-buffer "*Bug Help*"
392 (princ (substitute-command-keys
393 (format "\
394 You invoked the command M-x report-emacs-bug,
395 but you decided not to mail the bug report to the Emacs maintainers.
396
397 If you want to mail it to someone else instead,
398 please insert the proper e-mail address after \"To: \",
399 and send the mail again%s."
400 (if report-emacs-bug-send-command
401 (format " using \\[%s]"
402 report-emacs-bug-send-command)
403 "")))))
404 (error "M-x report-emacs-bug was cancelled, please read *Bug Help* buffer"))
405 ;; Query the user for the SMTP method, so that we can skip
406 ;; questions about From header validity if the user is going to
407 ;; use mailclient, anyway.
408 (when (or (and (derived-mode-p 'message-mode)
409 (eq message-send-mail-function 'sendmail-query-once))
410 (and (not (derived-mode-p 'message-mode))
411 (eq send-mail-function 'sendmail-query-once)))
412 (sendmail-query-user-about-smtp)
413 (when (derived-mode-p 'message-mode)
414 (setq message-send-mail-function (message-default-send-mail-function))))
415 (or report-emacs-bug-no-confirmation
416 ;; mailclient.el does not need a valid From
417 (if (derived-mode-p 'message-mode)
418 (eq message-send-mail-function 'message-send-mail-with-mailclient)
419 (eq send-mail-function 'mailclient-send-it))
420 ;; Not narrowing to the headers, but that's OK.
421 (let ((from (mail-fetch-field "From")))
422 (and (or (not from)
423 (message-bogus-recipient-p from)
424 ;; This is the default user-mail-address. On today's
425 ;; systems, it seems more likely to be wrong than right,
426 ;; since most people don't run their own mail server.
427 (string-match (format "\\<%s@%s\\>"
428 (regexp-quote (user-login-name))
429 (regexp-quote (system-name)))
430 from))
431 (not (yes-or-no-p
432 (format "Is `%s' really your email address? " from)))
433 (error "Please edit the From address and try again"))))
434 ;; Delete the uninteresting text that was just to help fill out the report.
435 (rfc822-goto-eoh)
436 (forward-line 1)
437 (let ((pos (1- (point))))
438 (while (setq pos (text-property-any pos (point-max)
439 'field 'emacsbug-prompt))
440 (delete-region pos (field-end (1+ pos)))))))
441
442
443 (provide 'emacsbug)
444
445 ;;; emacsbug.el ends here