* mail/emacsbug.el (report-emacs-bug): Try to handle some other mail clients.
[emacs.git] / lisp / mail / emacsbug.el
blob40a5c081cfd7299e357f5e65592d72fc24bad87a
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 ;; Could test major-mode instead.
283 (cond ((memq mail-user-agent '(message-user-agent gnus-user-agent))
284 (setq report-emacs-bug-send-command "message-send-and-exit"
285 report-emacs-bug-send-hook 'message-send-hook))
286 ((eq mail-user-agent 'sendmail-user-agent)
287 (setq report-emacs-bug-send-command "mail-send-and-exit"
288 report-emacs-bug-send-hook 'mail-send-hook))
289 ((eq mail-user-agent 'mh-e-user-agent)
290 (setq report-emacs-bug-send-command "mh-send-letter"
291 report-emacs-bug-send-hook 'mh-before-send-letter-hook))
292 ((eq mail-user-agent 'vm-user-agent)
293 (setq report-emacs-bug-send-command "vm-mail-send-and-exit"
294 report-emacs-bug-send-hook 'vm-mail-send-hook))
295 ((eq mail-user-agent 'wl-user-agent)
296 (setq report-emacs-bug-send-command "wl-draft-send-and-exit"
297 report-emacs-bug-send-hook 'wl-draft-send-hook)))
298 (unless report-emacs-bug-no-explanations
299 (with-output-to-temp-buffer "*Bug Help*"
300 (princ "While in the mail buffer:\n\n")
301 (if report-emacs-bug-send-command
302 (princ (substitute-command-keys
303 (format " Type \\[%s] to send the bug report.\n"
304 report-emacs-bug-send-command))))
305 (princ (substitute-command-keys
306 " Type \\[kill-buffer] RET to cancel (don't send it).\n"))
307 (if can-xdg-email
308 (princ (substitute-command-keys
309 " Type \\[report-emacs-bug-insert-to-mailer] to insert text to you preferred mail program.\n")))
310 (terpri)
311 (princ (substitute-command-keys
312 " Type \\[report-emacs-bug-info] to visit in Info the Emacs Manual section
313 about when and how to write a bug report, and what
314 information you should include to help fix the bug.")))
315 (shrink-window-if-larger-than-buffer (get-buffer-window "*Bug Help*")))
316 ;; Make it less likely people will send empty messages.
317 (if report-emacs-bug-send-hook
318 (add-hook report-emacs-bug-send-hook 'report-emacs-bug-hook nil t))
319 (goto-char (point-max))
320 (skip-chars-backward " \t\n")
321 (make-local-variable 'report-emacs-bug-orig-text)
322 (setq report-emacs-bug-orig-text
323 (buffer-substring-no-properties (point-min) (point)))
324 (goto-char user-point)))
326 (defun report-emacs-bug-info ()
327 "Go to the Info node on reporting Emacs bugs."
328 (interactive)
329 (info "(emacs)Bugs"))
331 (defun report-emacs-bug-hook ()
332 "Do some checking before sending a bug report."
333 (save-excursion
334 (goto-char (point-max))
335 (skip-chars-backward " \t\n")
336 (and (= (- (point) (point-min))
337 (length report-emacs-bug-orig-text))
338 (string-equal (buffer-substring-no-properties (point-min) (point))
339 report-emacs-bug-orig-text)
340 (error "No text entered in bug report"))
341 ;; Check the buffer contents and reject non-English letters.
342 ;; FIXME message-mode probably does this anyway.
343 (goto-char (point-min))
344 (skip-chars-forward "\0-\177")
345 (unless (eobp)
346 (if (or report-emacs-bug-no-confirmation
347 (y-or-n-p "Convert non-ASCII letters to hexadecimal? "))
348 (while (progn (skip-chars-forward "\0-\177")
349 (not (eobp)))
350 (let ((ch (following-char)))
351 (delete-char 1)
352 (insert (format "=%02x" ch))))))
354 ;; The last warning for novice users.
355 (unless (or report-emacs-bug-no-confirmation
356 (yes-or-no-p
357 "Send this bug report to the Emacs maintainers? "))
358 (goto-char (point-min))
359 (if (search-forward "To: ")
360 (delete-region (point) (line-end-position)))
361 (if report-emacs-bug-send-hook
362 (kill-local-variable report-emacs-bug-send-hook))
363 (with-output-to-temp-buffer "*Bug Help*"
364 (princ (substitute-command-keys
365 (format "\
366 You invoked the command M-x report-emacs-bug,
367 but you decided not to mail the bug report to the Emacs maintainers.
369 If you want to mail it to someone else instead,
370 please insert the proper e-mail address after \"To: \",
371 and send the mail again%s."
372 (if report-emacs-bug-send-command
373 (format " using \\[%s]"
374 report-emacs-bug-send-command)
375 "")))))
376 (error "M-x report-emacs-bug was cancelled, please read *Bug Help* buffer"))
378 ;; Delete the uninteresting text that was just to help fill out the report.
379 (rfc822-goto-eoh)
380 (forward-line 1)
381 (let ((pos (1- (point))))
382 (while (setq pos (text-property-any pos (point-max)
383 'field 'emacsbug-prompt))
384 (delete-region pos (field-end (1+ pos)))))))
387 ;; Querying the bug database
389 (defun report-emacs-bug-create-existing-bugs-buffer (bugs keywords)
390 (switch-to-buffer (get-buffer-create "*Existing Emacs Bugs*"))
391 (setq buffer-read-only t)
392 (let ((inhibit-read-only t))
393 (erase-buffer)
394 (make-local-variable 'bug-alist)
395 (setq bug-alist bugs)
396 (make-local-variable 'bug-choice-widget)
397 (widget-insert (propertize (concat "Already known bugs ("
398 keywords "):\n\n")
399 'face 'bold))
400 (if bugs
401 (setq bug-choice-widget
402 (apply 'widget-create 'radio-button-choice
403 :value (caar bugs)
404 (let (items)
405 (dolist (bug bugs)
406 (push (list
407 'url-link
408 :format (concat "Bug#" (number-to-string (nth 2 bug))
409 ": " (cadr bug) "\n %[%v%]\n")
410 ;; FIXME: Why is only the link of the
411 ;; active item clickable?
412 (car bug))
413 items))
414 (nreverse items))))
415 (widget-insert "No bugs maching your keywords found.\n"))
416 (widget-insert "\n")
417 (widget-create 'push-button
418 :notify (lambda (&rest ignore)
419 ;; TODO: Do something!
420 (message "Reporting new bug!"))
421 "Report new bug")
422 (when bugs
423 (widget-insert " ")
424 (widget-create 'push-button
425 :notify (lambda (&rest ignore)
426 (let ((val (widget-value bug-choice-widget)))
427 ;; TODO: Do something!
428 (message "Appending to bug %s!"
429 (nth 2 (assoc val bug-alist)))))
430 "Append to chosen bug"))
431 (widget-insert " ")
432 (widget-create 'push-button
433 :notify (lambda (&rest ignore)
434 (kill-buffer))
435 "Quit reporting bug")
436 (widget-insert "\n"))
437 (use-local-map widget-keymap)
438 (widget-setup)
439 (goto-char (point-min)))
441 (defun report-emacs-bug-parse-query-results (status keywords)
442 (goto-char (point-min))
443 (let (buglist)
444 (while (re-search-forward "<a href=\"bugreport\\.cgi\\?bug=\\([[:digit:]]+\\)\">\\([^<]+\\)</a>" nil t)
445 (let ((number (match-string 1))
446 (subject (match-string 2)))
447 (when (not (string-match "^#" subject))
448 (push (list
449 ;; first the bug URL
450 (concat report-emacs-bug-tracker-url
451 "bugreport.cgi?bug=" number)
452 ;; then the subject and number
453 subject (string-to-number number))
454 buglist))))
455 (report-emacs-bug-create-existing-bugs-buffer (nreverse buglist) keywords)))
457 (defun report-emacs-bug-query-existing-bugs (keywords)
458 "Query for KEYWORDS at `report-emacs-bug-tracker-url', and return the result.
459 The result is an alist with items of the form (URL SUBJECT NO)."
460 (interactive "sBug keywords (comma separated): ")
461 (url-retrieve (concat report-emacs-bug-tracker-url
462 "pkgreport.cgi?include=subject%3A"
463 (replace-regexp-in-string "[[:space:]]+" "+" keywords)
464 ";package=emacs")
465 'report-emacs-bug-parse-query-results (list keywords)))
467 (provide 'emacsbug)
469 ;;; emacsbug.el ends here