Update copyright notices for 2013.
[emacs.git] / lisp / mail / emacsbug.el
blobf0b671ec386276d75bc1b0520a72ff17242e93b5
1 ;;; emacsbug.el --- command to report Emacs bugs to appropriate mailing list
3 ;; Copyright (C) 1985, 1994, 1997-1998, 2000-2013 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-tracker-url "http://debbugs.gnu.org/cgi/"
64 "Base URL of the GNU bugtracker.
65 Used for querying duplicates and linking to existing bugs.")
67 (defvar report-emacs-bug-orig-text nil
68 "The automatically-created initial text of the bug report.")
70 (defvar report-emacs-bug-send-command nil
71 "Name of the command to send the bug report, as a string.")
72 (make-variable-buffer-local 'report-emacs-bug-send-command)
74 (defvar report-emacs-bug-send-hook nil
75 "Hook run before sending the bug report.")
76 (make-variable-buffer-local 'report-emacs-bug-send-hook)
78 (declare-function x-server-vendor "xfns.c" (&optional terminal))
79 (declare-function x-server-version "xfns.c" (&optional terminal))
80 (declare-function message-sort-headers "message" ())
81 (defvar message-strip-special-text-properties)
83 (defun report-emacs-bug-can-use-osx-open ()
84 "Return non-nil if the OS X \"open\" command is available for mailing."
85 (and (featurep 'ns)
86 (equal (executable-find "open") "/usr/bin/open")
87 (memq system-type '(darwin))))
89 ;; FIXME this duplicates much of the logic from browse-url-can-use-xdg-open.
90 (defun report-emacs-bug-can-use-xdg-email ()
91 "Return non-nil if the \"xdg-email\" command can be used.
92 xdg-email is a desktop utility that calls your preferred mail client.
93 This requires you to be running either Gnome, KDE, or Xfce4."
94 (and (getenv "DISPLAY")
95 (executable-find "xdg-email")
96 (or (getenv "GNOME_DESKTOP_SESSION_ID")
97 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
98 (condition-case nil
99 (eq 0 (call-process
100 "dbus-send" nil nil nil
101 "--dest=org.gnome.SessionManager"
102 "--print-reply"
103 "/org/gnome/SessionManager"
104 "org.gnome.SessionManager.CanShutdown"))
105 (error nil))
106 (equal (getenv "KDE_FULL_SESSION") "true")
107 ;; FIXME? browse-url-can-use-xdg-open also accepts LXDE.
108 ;; Is that no good here, or just overlooked?
109 (condition-case nil
110 (eq 0 (call-process
111 "/bin/sh" nil nil nil
112 "-c"
113 ;; FIXME use string-match rather than grep.
114 "xprop -root _DT_SAVE_MODE|grep xfce4"))
115 (error nil)))))
117 (defun report-emacs-bug-insert-to-mailer ()
118 "Send the message to your preferred mail client.
119 This requires either the OS X \"open\" command, or the freedesktop
120 \"xdg-email\" command to be available."
121 (interactive)
122 (save-excursion
123 ;; FIXME? use mail-fetch-field?
124 (let* ((to (progn
125 (goto-char (point-min))
126 (forward-line)
127 (and (looking-at "^To: \\(.*\\)")
128 (match-string-no-properties 1))))
129 (subject (progn
130 (forward-line)
131 (and (looking-at "^Subject: \\(.*\\)")
132 (match-string-no-properties 1))))
133 (body (progn
134 (forward-line 2)
135 (if (> (point-max) (point))
136 (buffer-substring-no-properties (point) (point-max))))))
137 (if (and to subject body)
138 (if (report-emacs-bug-can-use-osx-open)
139 (start-process "/usr/bin/open" nil "open"
140 (concat "mailto:" to
141 "?subject=" (url-hexify-string subject)
142 "&body=" (url-hexify-string body)))
143 (start-process "xdg-email" nil "xdg-email"
144 "--subject" subject
145 "--body" body
146 (concat "mailto:" to)))
147 (error "Subject, To or body not found")))))
149 ;;;###autoload
150 (defun report-emacs-bug (topic &optional recent-keys)
151 "Report a bug in GNU Emacs.
152 Prompts for bug subject. Leaves you in a mail buffer."
153 ;; This strange form ensures that (recent-keys) is the value before
154 ;; the bug subject string is read.
155 (interactive (reverse (list (recent-keys) (read-string "Bug Subject: "))))
156 ;; The syntax `version;' is preferred to `[version]' because the
157 ;; latter could be mistakenly stripped by mailing software.
158 (if (eq system-type 'ms-dos)
159 (setq topic (concat emacs-version "; " topic))
160 (when (string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
161 (setq topic (concat (match-string 1 emacs-version) "; " topic))))
162 (let ((from-buffer (current-buffer))
163 ;; Put these properties on semantically-void text.
164 ;; report-emacs-bug-hook deletes these regions before sending.
165 (prompt-properties '(field emacsbug-prompt
166 intangible but-helpful
167 rear-nonsticky t))
168 (can-insert-mail (or (report-emacs-bug-can-use-xdg-email)
169 (report-emacs-bug-can-use-osx-open)))
170 user-point message-end-point)
171 (setq message-end-point
172 (with-current-buffer (get-buffer-create "*Messages*")
173 (point-max-marker)))
174 (compose-mail report-emacs-bug-address topic)
175 ;; The rest of this does not execute if the user was asked to
176 ;; confirm and said no.
177 (when (eq major-mode 'message-mode)
178 ;; Message-mode sorts the headers before sending. We sort now so
179 ;; that report-emacs-bug-orig-text remains valid. (Bug#5178)
180 (message-sort-headers)
181 ;; Stop message-mode stealing the properties we will add.
182 (set (make-local-variable 'message-strip-special-text-properties) nil))
183 (rfc822-goto-eoh)
184 (forward-line 1)
185 ;; Move the mail signature to the proper place.
186 (let ((signature (buffer-substring (point) (point-max)))
187 (inhibit-read-only t))
188 (delete-region (point) (point-max))
189 (insert signature)
190 (backward-char (length signature)))
191 (unless report-emacs-bug-no-explanations
192 ;; Insert warnings for novice users.
193 (if (not (equal "bug-gnu-emacs@gnu.org" report-emacs-bug-address))
194 (insert (format "The report will be sent to %s.\n\n"
195 report-emacs-bug-address))
196 (insert "This bug report will be sent to the ")
197 (insert-button
198 "Bug-GNU-Emacs"
199 'face 'link
200 'help-echo (concat "mouse-2, RET: Follow this link")
201 'action (lambda (button)
202 (browse-url "http://lists.gnu.org/archive/html/bug-gnu-emacs/"))
203 'follow-link t)
204 (insert " mailing list\nand the GNU bug tracker at ")
205 (insert-button
206 "debbugs.gnu.org"
207 'face 'link
208 'help-echo (concat "mouse-2, RET: Follow this link")
209 'action (lambda (button)
210 (browse-url "http://debbugs.gnu.org/"))
211 'follow-link t)
213 (insert ". Please check that
214 the From: line contains a valid email address. After a delay of up
215 to one day, you should receive an acknowledgment at that address.
217 Please write in English if possible, as the Emacs maintainers
218 usually do not have translators for other languages.\n\n")))
220 (insert "Please describe exactly what actions triggered the bug, and\n"
221 "the precise symptoms of the bug. If you can, give a recipe\n"
222 "starting from `emacs -Q':\n\n")
223 (add-text-properties (save-excursion
224 (rfc822-goto-eoh)
225 (line-beginning-position 2))
226 (point)
227 prompt-properties)
228 (setq user-point (point))
229 (insert "\n\n")
231 (insert "If Emacs crashed, and you have the Emacs process in the gdb debugger,\n"
232 "please include the output from the following gdb commands:\n"
233 " `bt full' and `xbacktrace'.\n")
235 (let ((debug-file (expand-file-name "DEBUG" data-directory)))
236 (if (file-readable-p debug-file)
237 (insert "For information about debugging Emacs, please read the file\n"
238 debug-file ".\n")))
239 (add-text-properties (1+ user-point) (point) prompt-properties)
241 (insert "\n\nIn " (emacs-version) "\n")
242 (if (stringp emacs-bzr-version)
243 (insert "Bzr revision: " emacs-bzr-version "\n"))
244 (if (fboundp 'x-server-vendor)
245 (condition-case nil
246 ;; This is used not only for X11 but also W32 and others.
247 (insert "Windowing system distributor `" (x-server-vendor)
248 "', version "
249 (mapconcat 'number-to-string (x-server-version) ".") "\n")
250 (error t)))
251 (let ((lsb (with-temp-buffer
252 (if (eq 0 (ignore-errors
253 (call-process "lsb_release" nil '(t nil)
254 nil "-d")))
255 (buffer-string)))))
256 (if (stringp lsb)
257 (insert "System " lsb "\n")))
258 (when (and system-configuration-options
259 (not (equal system-configuration-options "")))
260 (insert "Configured using:\n `configure "
261 system-configuration-options "'\n\n")
262 (fill-region (line-beginning-position -1) (point)))
263 (insert "Important settings:\n")
264 (mapc
265 (lambda (var)
266 (let ((val (getenv var)))
267 (if val (insert (format " value of $%s: %s\n" var val)))))
268 '("EMACSDATA" "EMACSDOC" "EMACSLOADPATH" "EMACSPATH"
269 "LC_ALL" "LC_COLLATE" "LC_CTYPE" "LC_MESSAGES"
270 "LC_MONETARY" "LC_NUMERIC" "LC_TIME" "LANG" "XMODIFIERS"))
271 (insert (format " locale-coding-system: %s\n" locale-coding-system))
272 (insert (format " default enable-multibyte-characters: %s\n"
273 (default-value 'enable-multibyte-characters)))
274 (insert "\n")
275 (insert (format "Major mode: %s\n"
276 (format-mode-line
277 (buffer-local-value 'mode-name from-buffer)
278 nil nil from-buffer)))
279 (insert "\n")
280 (insert "Minor modes in effect:\n")
281 (dolist (mode minor-mode-list)
282 (and (boundp mode) (buffer-local-value mode from-buffer)
283 (insert (format " %s: %s\n" mode
284 (buffer-local-value mode from-buffer)))))
285 (insert "\n")
286 (insert "Recent input:\n")
287 (let ((before-keys (point)))
288 (insert (mapconcat (lambda (key)
289 (if (or (integerp key)
290 (symbolp key)
291 (listp key))
292 (single-key-description key)
293 (prin1-to-string key nil)))
294 (or recent-keys (recent-keys))
295 " "))
296 (save-restriction
297 (narrow-to-region before-keys (point))
298 (goto-char before-keys)
299 (while (progn (move-to-column 50) (not (eobp)))
300 (search-forward " " nil t)
301 (insert "\n"))))
302 (let ((message-buf (get-buffer "*Messages*")))
303 (if message-buf
304 (let (beg-pos
305 (end-pos message-end-point))
306 (with-current-buffer message-buf
307 (goto-char end-pos)
308 (forward-line -10)
309 (setq beg-pos (point)))
310 (insert "\n\nRecent messages:\n")
311 (insert-buffer-substring message-buf beg-pos end-pos))))
312 ;; After Recent messages, to avoid the messages produced by
313 ;; list-load-path-shadows.
314 (unless (looking-back "\n")
315 (insert "\n"))
316 (insert "\n")
317 (insert "Load-path shadows:\n")
318 (let* ((msg "Checking for load-path shadows...")
319 (result "done")
320 (shadows (progn (message "%s" msg)
321 (condition-case nil (list-load-path-shadows t)
322 (error
323 (setq result "error")
324 "Error during checking")))))
325 (message "%s%s" msg result)
326 (insert (if (zerop (length shadows))
327 "None found.\n"
328 shadows)))
329 (insert (format "\nFeatures:\n%s\n" features))
330 (fill-region (line-beginning-position 0) (point))
331 ;; This is so the user has to type something in order to send easily.
332 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
333 (define-key (current-local-map) "\C-c\C-i" 'info-emacs-bug)
334 (if can-insert-mail
335 (define-key (current-local-map) "\C-cm"
336 'report-emacs-bug-insert-to-mailer))
337 (setq report-emacs-bug-send-command (get mail-user-agent 'sendfunc)
338 report-emacs-bug-send-hook (get mail-user-agent 'hookvar))
339 (if report-emacs-bug-send-command
340 (setq report-emacs-bug-send-command
341 (symbol-name report-emacs-bug-send-command)))
342 (unless report-emacs-bug-no-explanations
343 (with-output-to-temp-buffer "*Bug Help*"
344 (princ "While in the mail buffer:\n\n")
345 (if report-emacs-bug-send-command
346 (princ (substitute-command-keys
347 (format " Type \\[%s] to send the bug report.\n"
348 report-emacs-bug-send-command))))
349 (princ (substitute-command-keys
350 " Type \\[kill-buffer] RET to cancel (don't send it).\n"))
351 (if can-insert-mail
352 (princ (substitute-command-keys
353 " Type \\[report-emacs-bug-insert-to-mailer] to copy text to your preferred mail program.\n")))
354 (terpri)
355 (princ (substitute-command-keys
356 " Type \\[info-emacs-bug] to visit in Info the Emacs Manual section
357 about when and how to write a bug report, and what
358 information you should include to help fix the bug.")))
359 (shrink-window-if-larger-than-buffer (get-buffer-window "*Bug Help*")))
360 ;; Make it less likely people will send empty messages.
361 (if report-emacs-bug-send-hook
362 (add-hook report-emacs-bug-send-hook 'report-emacs-bug-hook nil t))
363 (goto-char (point-max))
364 (skip-chars-backward " \t\n")
365 (make-local-variable 'report-emacs-bug-orig-text)
366 (setq report-emacs-bug-orig-text
367 (buffer-substring-no-properties (point-min) (point)))
368 (goto-char user-point)))
370 (define-obsolete-function-alias 'report-emacs-bug-info 'info-emacs-bug "24.3")
372 ;; It's the default mail mode, so it seems OK to use its features.
373 (autoload 'message-bogus-recipient-p "message")
374 (defvar message-send-mail-function)
376 (defun report-emacs-bug-hook ()
377 "Do some checking before sending a bug report."
378 (save-excursion
379 (goto-char (point-max))
380 (skip-chars-backward " \t\n")
381 (and (= (- (point) (point-min))
382 (length report-emacs-bug-orig-text))
383 (string-equal (buffer-substring-no-properties (point-min) (point))
384 report-emacs-bug-orig-text)
385 (error "No text entered in bug report"))
386 ;; Warning for novice users.
387 (unless (or report-emacs-bug-no-confirmation
388 (yes-or-no-p
389 "Send this bug report to the Emacs maintainers? "))
390 (goto-char (point-min))
391 (if (search-forward "To: ")
392 (delete-region (point) (line-end-position)))
393 (if report-emacs-bug-send-hook
394 (kill-local-variable report-emacs-bug-send-hook))
395 (with-output-to-temp-buffer "*Bug Help*"
396 (princ (substitute-command-keys
397 (format "\
398 You invoked the command M-x report-emacs-bug,
399 but you decided not to mail the bug report to the Emacs maintainers.
401 If you want to mail it to someone else instead,
402 please insert the proper e-mail address after \"To: \",
403 and send the mail again%s."
404 (if report-emacs-bug-send-command
405 (format " using \\[%s]"
406 report-emacs-bug-send-command)
407 "")))))
408 (error "M-x report-emacs-bug was cancelled, please read *Bug Help* buffer"))
409 ;; Query the user for the SMTP method, so that we can skip
410 ;; questions about From header validity if the user is going to
411 ;; use mailclient, anyway.
412 (when (or (and (derived-mode-p 'message-mode)
413 (eq message-send-mail-function 'sendmail-query-once))
414 (and (not (derived-mode-p 'message-mode))
415 (eq send-mail-function 'sendmail-query-once)))
416 (sendmail-query-user-about-smtp)
417 (when (derived-mode-p 'message-mode)
418 (setq message-send-mail-function (message-default-send-mail-function))))
419 (or report-emacs-bug-no-confirmation
420 ;; mailclient.el does not need a valid From
421 (if (derived-mode-p 'message-mode)
422 (eq message-send-mail-function 'message-send-mail-with-mailclient)
423 (eq send-mail-function 'mailclient-send-it))
424 ;; Not narrowing to the headers, but that's OK.
425 (let ((from (mail-fetch-field "From")))
426 (and (or (not from)
427 (message-bogus-recipient-p from)
428 ;; This is the default user-mail-address. On today's
429 ;; systems, it seems more likely to be wrong than right,
430 ;; since most people don't run their own mail server.
431 (string-match (format "\\<%s@%s\\>"
432 (regexp-quote (user-login-name))
433 (regexp-quote (system-name)))
434 from))
435 (not (yes-or-no-p
436 (format "Is `%s' really your email address? " from)))
437 (error "Please edit the From address and try again"))))
438 ;; Delete the uninteresting text that was just to help fill out the report.
439 (rfc822-goto-eoh)
440 (forward-line 1)
441 (let ((pos (1- (point))))
442 (while (setq pos (text-property-any pos (point-max)
443 'field 'emacsbug-prompt))
444 (delete-region pos (field-end (1+ pos)))))))
447 ;; Querying the bug database
449 (defvar report-emacs-bug-bug-alist nil)
450 (make-variable-buffer-local 'report-emacs-bug-bug-alist)
451 (defvar report-emacs-bug-choice-widget nil)
452 (make-variable-buffer-local 'report-emacs-bug-choice-widget)
454 (defun report-emacs-bug-create-existing-bugs-buffer (bugs keywords)
455 (switch-to-buffer (get-buffer-create "*Existing Emacs Bugs*"))
456 (setq buffer-read-only t)
457 (let ((inhibit-read-only t))
458 (erase-buffer)
459 (setq report-emacs-bug-bug-alist bugs)
460 (widget-insert (propertize (concat "Already known bugs ("
461 keywords "):\n\n")
462 'face 'bold))
463 (if bugs
464 (setq report-emacs-bug-choice-widget
465 (apply 'widget-create 'radio-button-choice
466 :value (caar bugs)
467 (let (items)
468 (dolist (bug bugs)
469 (push (list
470 'url-link
471 :format (concat "Bug#" (number-to-string (nth 2 bug))
472 ": " (cadr bug) "\n %[%v%]\n")
473 ;; FIXME: Why is only the link of the
474 ;; active item clickable?
475 (car bug))
476 items))
477 (nreverse items))))
478 (widget-insert "No bugs matching your keywords found.\n"))
479 (widget-insert "\n")
480 (widget-create 'push-button
481 :notify (lambda (&rest ignore)
482 ;; TODO: Do something!
483 (message "Reporting new bug!"))
484 "Report new bug")
485 (when bugs
486 (widget-insert " ")
487 (widget-create 'push-button
488 :notify (lambda (&rest ignore)
489 (let ((val (widget-value report-emacs-bug-choice-widget)))
490 ;; TODO: Do something!
491 (message "Appending to bug %s!"
492 (nth 2 (assoc val report-emacs-bug-bug-alist)))))
493 "Append to chosen bug"))
494 (widget-insert " ")
495 (widget-create 'push-button
496 :notify (lambda (&rest ignore)
497 (kill-buffer))
498 "Quit reporting bug")
499 (widget-insert "\n"))
500 (use-local-map widget-keymap)
501 (widget-setup)
502 (goto-char (point-min)))
504 (defun report-emacs-bug-parse-query-results (status keywords)
505 (goto-char (point-min))
506 (let (buglist)
507 (while (re-search-forward "<a href=\"bugreport\\.cgi\\?bug=\\([[:digit:]]+\\)\">\\([^<]+\\)</a>" nil t)
508 (let ((number (match-string 1))
509 (subject (match-string 2)))
510 (when (not (string-match "^#" subject))
511 (push (list
512 ;; first the bug URL
513 (concat report-emacs-bug-tracker-url
514 "bugreport.cgi?bug=" number)
515 ;; then the subject and number
516 subject (string-to-number number))
517 buglist))))
518 (report-emacs-bug-create-existing-bugs-buffer (nreverse buglist) keywords)))
520 (defun report-emacs-bug-query-existing-bugs (keywords)
521 "Query for KEYWORDS at `report-emacs-bug-tracker-url', and return the result.
522 The result is an alist with items of the form (URL SUBJECT NO)."
523 (interactive "sBug keywords (comma separated): ")
524 (url-retrieve (concat report-emacs-bug-tracker-url
525 "pkgreport.cgi?include=subject%3A"
526 (replace-regexp-in-string "[[:space:]]+" "+" keywords)
527 ";package=emacs")
528 'report-emacs-bug-parse-query-results (list keywords)))
529 (make-obsolete 'report-emacs-bug-query-existing-bugs
530 "use the `debbugs' package from GNU ELPA instead." "24.3")
532 (provide 'emacsbug)
534 ;;; emacsbug.el ends here