Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / mail / emacsbug.el
bloba0b968c200c6264734bd6384c261775174f33396
1 ;;; emacsbug.el --- command to report Emacs bugs to appropriate mailing list
3 ;; Copyright (C) 1985, 1994, 1997-1998, 2000-2014 Free Software
4 ;; Foundation, Inc.
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: FSF
8 ;; Keywords: maint mail
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;;; Commentary:
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.
33 ;;; Code:
35 (require 'sendmail)
36 (require 'message)
38 (defgroup emacsbug nil
39 "Sending Emacs bug reports."
40 :group 'maint
41 :group 'mail)
43 (define-obsolete-variable-alias 'report-emacs-bug-pretest-address
44 'report-emacs-bug-address "24.1")
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)
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)
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)
61 ;; User options end here.
63 (defvar report-emacs-bug-orig-text nil
64 "The automatically-created initial text of the bug report.")
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)
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)
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)
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))))
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)))))
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")))))
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 (can-insert-mail (or (report-emacs-bug-can-use-xdg-email)
160 (report-emacs-bug-can-use-osx-open)))
161 user-point message-end-point)
162 (setq message-end-point
163 (with-current-buffer (messages-buffer)
164 (point-max-marker)))
165 (compose-mail report-emacs-bug-address topic)
166 ;; The rest of this does not execute if the user was asked to
167 ;; confirm and said no.
168 (when (eq major-mode 'message-mode)
169 ;; Message-mode sorts the headers before sending. We sort now so
170 ;; that report-emacs-bug-orig-text remains valid. (Bug#5178)
171 (message-sort-headers)
172 ;; Stop message-mode stealing the properties we will add.
173 (set (make-local-variable 'message-strip-special-text-properties) nil))
174 (rfc822-goto-eoh)
175 (forward-line 1)
176 ;; Move the mail signature to the proper place.
177 (let ((signature (buffer-substring (point) (point-max)))
178 (inhibit-read-only t))
179 (delete-region (point) (point-max))
180 (insert signature)
181 (backward-char (length signature)))
182 (unless report-emacs-bug-no-explanations
183 ;; Insert warnings for novice users.
184 (if (not (equal "bug-gnu-emacs@gnu.org" report-emacs-bug-address))
185 (insert (format "The report will be sent to %s.\n\n"
186 report-emacs-bug-address))
187 (insert "This bug report will be sent to the ")
188 (insert-text-button
189 "Bug-GNU-Emacs"
190 'face 'link
191 'help-echo (concat "mouse-2, RET: Follow this link")
192 'action (lambda (button)
193 (browse-url "http://lists.gnu.org/archive/html/bug-gnu-emacs/"))
194 'follow-link t)
195 (insert " mailing list\nand the GNU bug tracker at ")
196 (insert-text-button
197 "debbugs.gnu.org"
198 'face 'link
199 'help-echo (concat "mouse-2, RET: Follow this link")
200 'action (lambda (button)
201 (browse-url "http://debbugs.gnu.org/"))
202 'follow-link t)
204 (insert ". Please check that
205 the From: line contains a valid email address. After a delay of up
206 to one day, you should receive an acknowledgment at that address.
208 Please write in English if possible, as the Emacs maintainers
209 usually do not have translators for other languages.\n\n")))
211 (insert "Please describe exactly what actions triggered the bug, and\n"
212 "the precise symptoms of the bug. If you can, give a recipe\n"
213 "starting from `emacs -Q':\n\n")
214 (let ((txt (delete-and-extract-region
215 (save-excursion (rfc822-goto-eoh) (line-beginning-position 2))
216 (point))))
217 (insert (propertize "\n" 'display txt)))
218 (setq user-point (point))
219 (insert "\n\n")
221 (insert "If Emacs crashed, and you have the Emacs process in the gdb debugger,\n"
222 "please include the output from the following gdb commands:\n"
223 " `bt full' and `xbacktrace'.\n")
225 (let ((debug-file (expand-file-name "DEBUG" data-directory)))
226 (if (file-readable-p debug-file)
227 (insert "For information about debugging Emacs, please read the file\n"
228 debug-file ".\n")))
229 (let ((txt (delete-and-extract-region (1+ user-point) (point))))
230 (insert (propertize "\n" 'display txt)))
232 (insert "\n\nIn " (emacs-version) "\n")
233 (if (stringp emacs-bzr-version)
234 (insert "Bzr revision: " emacs-bzr-version "\n"))
235 (if (fboundp 'x-server-vendor)
236 (condition-case nil
237 ;; This is used not only for X11 but also W32 and others.
238 (insert "Windowing system distributor `" (x-server-vendor)
239 "', version "
240 (mapconcat 'number-to-string (x-server-version) ".") "\n")
241 (error t)))
242 (let ((lsb (with-temp-buffer
243 (if (eq 0 (ignore-errors
244 (call-process "lsb_release" nil '(t nil)
245 nil "-d")))
246 (buffer-string)))))
247 (if (stringp lsb)
248 (insert "System " lsb "\n")))
249 (when (and system-configuration-options
250 (not (equal system-configuration-options "")))
251 (insert "Configured using:\n `configure "
252 system-configuration-options "'\n\n")
253 (fill-region (line-beginning-position -1) (point)))
254 (insert "Important settings:\n")
255 (mapc
256 (lambda (var)
257 (let ((val (getenv var)))
258 (if val (insert (format " value of $%s: %s\n" var val)))))
259 '("EMACSDATA" "EMACSDOC" "EMACSLOADPATH" "EMACSPATH"
260 "LC_ALL" "LC_COLLATE" "LC_CTYPE" "LC_MESSAGES"
261 "LC_MONETARY" "LC_NUMERIC" "LC_TIME" "LANG" "XMODIFIERS"))
262 (insert (format " locale-coding-system: %s\n" locale-coding-system))
263 ;; Only ~ 0.2% of people from a sample of 3200 changed this from
264 ;; the default, t.
265 (or (default-value 'enable-multibyte-characters)
266 (insert (format " default enable-multibyte-characters: %s\n"
267 (default-value 'enable-multibyte-characters))))
268 (insert "\n")
269 (insert (format "Major mode: %s\n"
270 (format-mode-line
271 (buffer-local-value 'mode-name from-buffer)
272 nil nil from-buffer)))
273 (insert "\n")
274 (insert "Minor modes in effect:\n")
275 (dolist (mode minor-mode-list)
276 (and (boundp mode) (buffer-local-value mode from-buffer)
277 (insert (format " %s: %s\n" mode
278 (buffer-local-value mode from-buffer)))))
279 (insert "\n")
280 (insert "Recent input:\n")
281 (let ((before-keys (point)))
282 (insert (mapconcat (lambda (key)
283 (if (or (integerp key)
284 (symbolp key)
285 (listp key))
286 (single-key-description key)
287 (prin1-to-string key nil)))
288 (or recent-keys (recent-keys))
289 " "))
290 (save-restriction
291 (narrow-to-region before-keys (point))
292 (goto-char before-keys)
293 (while (progn (move-to-column 50) (not (eobp)))
294 (search-forward " " nil t)
295 (insert "\n"))))
296 (let ((message-buf (get-buffer "*Messages*")))
297 (if message-buf
298 (let (beg-pos
299 (end-pos message-end-point))
300 (with-current-buffer message-buf
301 (goto-char end-pos)
302 (forward-line -10)
303 (setq beg-pos (point)))
304 (insert "\n\nRecent messages:\n")
305 (insert-buffer-substring message-buf beg-pos end-pos))))
306 ;; After Recent messages, to avoid the messages produced by
307 ;; list-load-path-shadows.
308 (unless (looking-back "\n")
309 (insert "\n"))
310 (insert "\n")
311 (insert "Load-path shadows:\n")
312 (let* ((msg "Checking for load-path shadows...")
313 (result "done")
314 (shadows (progn (message "%s" msg)
315 (condition-case nil (list-load-path-shadows t)
316 (error
317 (setq result "error")
318 "Error during checking")))))
319 (message "%s%s" msg result)
320 (insert (if (zerop (length shadows))
321 "None found.\n"
322 shadows)))
323 (insert (format "\nFeatures:\n%s\n" features))
324 (fill-region (line-beginning-position 0) (point))
325 ;; This is so the user has to type something in order to send easily.
326 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
327 (define-key (current-local-map) "\C-c\C-i" 'info-emacs-bug)
328 (if can-insert-mail
329 (define-key (current-local-map) "\C-c\M-i"
330 'report-emacs-bug-insert-to-mailer))
331 (setq report-emacs-bug-send-command (get mail-user-agent 'sendfunc)
332 report-emacs-bug-send-hook (get mail-user-agent 'hookvar))
333 (if report-emacs-bug-send-command
334 (setq report-emacs-bug-send-command
335 (symbol-name report-emacs-bug-send-command)))
336 (unless report-emacs-bug-no-explanations
337 (with-output-to-temp-buffer "*Bug Help*"
338 (princ "While in the mail buffer:\n\n")
339 (if report-emacs-bug-send-command
340 (princ (substitute-command-keys
341 (format " Type \\[%s] to send the bug report.\n"
342 report-emacs-bug-send-command))))
343 (princ (substitute-command-keys
344 " Type \\[kill-buffer] RET to cancel (don't send it).\n"))
345 (if can-insert-mail
346 (princ (substitute-command-keys
347 " Type \\[report-emacs-bug-insert-to-mailer] to copy text to your preferred mail program.\n")))
348 (terpri)
349 (princ (substitute-command-keys
350 " Type \\[info-emacs-bug] to visit in Info the Emacs Manual section
351 about when and how to write a bug report, and what
352 information you should include to help fix the bug.")))
353 (shrink-window-if-larger-than-buffer (get-buffer-window "*Bug Help*")))
354 ;; Make it less likely people will send empty messages.
355 (if report-emacs-bug-send-hook
356 (add-hook report-emacs-bug-send-hook 'report-emacs-bug-hook nil t))
357 (goto-char (point-max))
358 (skip-chars-backward " \t\n")
359 (make-local-variable 'report-emacs-bug-orig-text)
360 (setq report-emacs-bug-orig-text
361 (buffer-substring-no-properties (point-min) (point)))
362 (goto-char user-point)))
364 (define-obsolete-function-alias 'report-emacs-bug-info 'info-emacs-bug "24.3")
366 ;; It's the default mail mode, so it seems OK to use its features.
367 (autoload 'message-bogus-recipient-p "message")
368 (defvar message-send-mail-function)
370 (defun report-emacs-bug-hook ()
371 "Do some checking before sending a bug report."
372 (save-excursion
373 (goto-char (point-max))
374 (skip-chars-backward " \t\n")
375 (and (= (- (point) (point-min))
376 (length report-emacs-bug-orig-text))
377 (string-equal (buffer-substring-no-properties (point-min) (point))
378 report-emacs-bug-orig-text)
379 (error "No text entered in bug report"))
380 ;; Warning for novice users.
381 (unless (or report-emacs-bug-no-confirmation
382 (yes-or-no-p
383 "Send this bug report to the Emacs maintainers? "))
384 (goto-char (point-min))
385 (if (search-forward "To: ")
386 (delete-region (point) (line-end-position)))
387 (if report-emacs-bug-send-hook
388 (kill-local-variable report-emacs-bug-send-hook))
389 (with-output-to-temp-buffer "*Bug Help*"
390 (princ (substitute-command-keys
391 (format "\
392 You invoked the command M-x report-emacs-bug,
393 but you decided not to mail the bug report to the Emacs maintainers.
395 If you want to mail it to someone else instead,
396 please insert the proper e-mail address after \"To: \",
397 and send the mail again%s."
398 (if report-emacs-bug-send-command
399 (format " using \\[%s]"
400 report-emacs-bug-send-command)
401 "")))))
402 (error "M-x report-emacs-bug was cancelled, please read *Bug Help* buffer"))
403 ;; Query the user for the SMTP method, so that we can skip
404 ;; questions about From header validity if the user is going to
405 ;; use mailclient, anyway.
406 (when (or (and (derived-mode-p 'message-mode)
407 (eq message-send-mail-function 'sendmail-query-once))
408 (and (not (derived-mode-p 'message-mode))
409 (eq send-mail-function 'sendmail-query-once)))
410 (sendmail-query-user-about-smtp)
411 (when (derived-mode-p 'message-mode)
412 (setq message-send-mail-function (message-default-send-mail-function))))
413 (or report-emacs-bug-no-confirmation
414 ;; mailclient.el does not need a valid From
415 (if (derived-mode-p 'message-mode)
416 (eq message-send-mail-function 'message-send-mail-with-mailclient)
417 (eq send-mail-function 'mailclient-send-it))
418 ;; Not narrowing to the headers, but that's OK.
419 (let ((from (mail-fetch-field "From")))
420 (and (or (not from)
421 (message-bogus-recipient-p from)
422 ;; This is the default user-mail-address. On today's
423 ;; systems, it seems more likely to be wrong than right,
424 ;; since most people don't run their own mail server.
425 (string-match (format "\\<%s@%s\\>"
426 (regexp-quote (user-login-name))
427 (regexp-quote (system-name)))
428 from))
429 (not (yes-or-no-p
430 (format "Is `%s' really your email address? " from)))
431 (error "Please edit the From address and try again"))))))
434 (provide 'emacsbug)
436 ;;; emacsbug.el ends here