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