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