Use derived-mode-p in previous change.
[emacs.git] / lisp / mail / emacsbug.el
blob6b062f2298f9aa9409e50294c9480bb8056bccbb
1 ;;; emacsbug.el --- command to report Emacs bugs to appropriate mailing list
3 ;; Copyright (C) 1985, 1994, 1997-1998, 2000-2011
4 ;; Free Software 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 (defgroup emacsbug nil
36 "Sending Emacs bug reports."
37 :group 'maint
38 :group 'mail)
40 (define-obsolete-variable-alias 'report-emacs-bug-pretest-address
41 'report-emacs-bug-address "24.1")
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)
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)
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)
58 ;; User options end here.
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.")
64 (defvar report-emacs-bug-orig-text nil
65 "The automatically-created initial text of the bug report.")
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)
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)
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)
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))))
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)))))
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")))))
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 (when (string-match "@gnu\\.org$" report-emacs-bug-address)
179 (insert "This bug report will be sent to the Free Software Foundation,\n")
180 (let ((pos (point)))
181 (insert "not to your local site managers!")
182 (overlay-put (make-overlay pos (point)) 'face 'highlight)))
183 (insert "\nPlease write in ")
184 (let ((pos (point)))
185 (insert "English")
186 (overlay-put (make-overlay pos (point)) 'face 'highlight))
187 (insert " if possible, because the Emacs maintainers
188 usually do not have translators to read other languages for them.\n\n")
189 (insert "Please check that the From: line gives an address where you can be reached.\n")
190 (insert (format "Your report will be posted to the %s mailing list"
191 report-emacs-bug-address))
192 (insert "\nand the gnu.emacs.bug news group, and at http://debbugs.gnu.org.\n\n"))
194 (insert "Please describe exactly what actions triggered the bug\n"
195 "and the precise symptoms of the bug. If you can, give\n"
196 "a recipe starting from `emacs -Q':\n\n")
197 (add-text-properties (save-excursion
198 (rfc822-goto-eoh)
199 (line-beginning-position 2))
200 (point)
201 prompt-properties)
202 (setq user-point (point))
203 (insert "\n\n")
205 (insert "If Emacs crashed, and you have the Emacs process in the gdb debugger,\n"
206 "please include the output from the following gdb commands:\n"
207 " `bt full' and `xbacktrace'.\n")
209 (let ((debug-file (expand-file-name "DEBUG" data-directory)))
210 (if (file-readable-p debug-file)
211 (insert "For information about debugging Emacs, please read the file\n"
212 debug-file ".\n")))
213 (add-text-properties (1+ user-point) (point) prompt-properties)
215 (insert "\n\nIn " (emacs-version) "\n")
216 (if (fboundp 'x-server-vendor)
217 (condition-case nil
218 ;; This is used not only for X11 but also W32 and others.
219 (insert "Windowing system distributor `" (x-server-vendor)
220 "', version "
221 (mapconcat 'number-to-string (x-server-version) ".") "\n")
222 (error t)))
223 (if (and system-configuration-options
224 (not (equal system-configuration-options "")))
225 (insert "configured using `configure "
226 system-configuration-options "'\n\n"))
227 (insert "Important settings:\n")
228 (mapc
229 (lambda (var)
230 (insert (format " value of $%s: %s\n" var (getenv var))))
231 '("LC_ALL" "LC_COLLATE" "LC_CTYPE" "LC_MESSAGES"
232 "LC_MONETARY" "LC_NUMERIC" "LC_TIME" "LANG" "XMODIFIERS"))
233 (insert (format " locale-coding-system: %s\n" locale-coding-system))
234 (insert (format " default enable-multibyte-characters: %s\n"
235 (default-value 'enable-multibyte-characters)))
236 (insert "\n")
237 (insert (format "Major mode: %s\n"
238 (format-mode-line
239 (buffer-local-value 'mode-name from-buffer)
240 nil nil from-buffer)))
241 (insert "\n")
242 (insert "Minor modes in effect:\n")
243 (dolist (mode minor-mode-list)
244 (and (boundp mode) (buffer-local-value mode from-buffer)
245 (insert (format " %s: %s\n" mode
246 (buffer-local-value mode from-buffer)))))
247 (insert "\n")
248 (insert "Recent input:\n")
249 (let ((before-keys (point)))
250 (insert (mapconcat (lambda (key)
251 (if (or (integerp key)
252 (symbolp key)
253 (listp key))
254 (single-key-description key)
255 (prin1-to-string key nil)))
256 (or recent-keys (recent-keys))
257 " "))
258 (save-restriction
259 (narrow-to-region before-keys (point))
260 (goto-char before-keys)
261 (while (progn (move-to-column 50) (not (eobp)))
262 (search-forward " " nil t)
263 (insert "\n"))))
264 (let ((message-buf (get-buffer "*Messages*")))
265 (if message-buf
266 (let (beg-pos
267 (end-pos message-end-point))
268 (with-current-buffer message-buf
269 (goto-char end-pos)
270 (forward-line -10)
271 (setq beg-pos (point)))
272 (insert "\n\nRecent messages:\n")
273 (insert-buffer-substring message-buf beg-pos end-pos))))
274 ;; After Recent messages, to avoid the messages produced by
275 ;; list-load-path-shadows.
276 (unless (looking-back "\n")
277 (insert "\n"))
278 (insert "\n")
279 (insert "Load-path shadows:\n")
280 (message "Checking for load-path shadows...")
281 (let ((shadows (list-load-path-shadows t)))
282 (message "Checking for load-path shadows...done")
283 (insert (if (zerop (length shadows))
284 "None found.\n"
285 shadows)))
286 (insert (format "\nFeatures:\n%s\n" features))
287 (fill-region (line-beginning-position 0) (point))
288 ;; This is so the user has to type something in order to send easily.
289 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
290 (define-key (current-local-map) "\C-c\C-i" 'report-emacs-bug-info)
291 (if can-insert-mail
292 (define-key (current-local-map) "\C-cm"
293 'report-emacs-bug-insert-to-mailer))
294 (setq report-emacs-bug-send-command (get mail-user-agent 'sendfunc)
295 report-emacs-bug-send-hook (get mail-user-agent 'hookvar))
296 (if report-emacs-bug-send-command
297 (setq report-emacs-bug-send-command
298 (symbol-name report-emacs-bug-send-command)))
299 (unless report-emacs-bug-no-explanations
300 (with-output-to-temp-buffer "*Bug Help*"
301 (princ "While in the mail buffer:\n\n")
302 (if report-emacs-bug-send-command
303 (princ (substitute-command-keys
304 (format " Type \\[%s] to send the bug report.\n"
305 report-emacs-bug-send-command))))
306 (princ (substitute-command-keys
307 " Type \\[kill-buffer] RET to cancel (don't send it).\n"))
308 (if can-insert-mail
309 (princ (substitute-command-keys
310 " Type \\[report-emacs-bug-insert-to-mailer] to insert text to you preferred mail program.\n")))
311 (terpri)
312 (princ (substitute-command-keys
313 " Type \\[report-emacs-bug-info] to visit in Info the Emacs Manual section
314 about when and how to write a bug report, and what
315 information you should include to help fix the bug.")))
316 (shrink-window-if-larger-than-buffer (get-buffer-window "*Bug Help*")))
317 ;; Make it less likely people will send empty messages.
318 (if report-emacs-bug-send-hook
319 (add-hook report-emacs-bug-send-hook 'report-emacs-bug-hook nil t))
320 (goto-char (point-max))
321 (skip-chars-backward " \t\n")
322 (make-local-variable 'report-emacs-bug-orig-text)
323 (setq report-emacs-bug-orig-text
324 (buffer-substring-no-properties (point-min) (point)))
325 (goto-char user-point)))
327 (defun report-emacs-bug-info ()
328 "Go to the Info node on reporting Emacs bugs."
329 (interactive)
330 (info "(emacs)Bugs"))
332 ;; It's the default mail mode, so it seems OK to use its features.
333 (autoload 'message-bogus-recipient-p "message")
334 (defvar message-send-mail-function)
336 (defun report-emacs-bug-hook ()
337 "Do some checking before sending a bug report."
338 (save-excursion
339 (goto-char (point-max))
340 (skip-chars-backward " \t\n")
341 (and (= (- (point) (point-min))
342 (length report-emacs-bug-orig-text))
343 (string-equal (buffer-substring-no-properties (point-min) (point))
344 report-emacs-bug-orig-text)
345 (error "No text entered in bug report"))
346 (or report-emacs-bug-no-confirmation
347 ;; mailclient.el does not handle From (at present).
348 (if (derived-mode-p 'message-mode)
349 (eq message-send-mail-function 'message-send-mail-with-mailclient)
350 (eq send-mail-function 'mailclient-send-it))
351 ;; Not narrowing to the headers, but that's OK.
352 (let ((from (mail-fetch-field "From")))
353 (and (or (not from)
354 (message-bogus-recipient-p from)
355 ;; This is the default user-mail-address. On today's
356 ;; systems, it seems more likely to be wrong than right,
357 ;; since most people don't run their own mail server.
358 (string-match (format "\\<%s@%s\\>"
359 (regexp-quote (user-login-name))
360 (regexp-quote (system-name)))
361 from))
362 (not (yes-or-no-p
363 (format "Is `%s' really your email address? " from)))
364 (error "Please edit the From address and try again"))))
365 ;; The last warning for novice users.
366 (unless (or report-emacs-bug-no-confirmation
367 (yes-or-no-p
368 "Send this bug report to the Emacs maintainers? "))
369 (goto-char (point-min))
370 (if (search-forward "To: ")
371 (delete-region (point) (line-end-position)))
372 (if report-emacs-bug-send-hook
373 (kill-local-variable report-emacs-bug-send-hook))
374 (with-output-to-temp-buffer "*Bug Help*"
375 (princ (substitute-command-keys
376 (format "\
377 You invoked the command M-x report-emacs-bug,
378 but you decided not to mail the bug report to the Emacs maintainers.
380 If you want to mail it to someone else instead,
381 please insert the proper e-mail address after \"To: \",
382 and send the mail again%s."
383 (if report-emacs-bug-send-command
384 (format " using \\[%s]"
385 report-emacs-bug-send-command)
386 "")))))
387 (error "M-x report-emacs-bug was cancelled, please read *Bug Help* buffer"))
389 ;; Delete the uninteresting text that was just to help fill out the report.
390 (rfc822-goto-eoh)
391 (forward-line 1)
392 (let ((pos (1- (point))))
393 (while (setq pos (text-property-any pos (point-max)
394 'field 'emacsbug-prompt))
395 (delete-region pos (field-end (1+ pos)))))))
398 ;; Querying the bug database
400 (defvar report-emacs-bug-bug-alist nil)
401 (make-variable-buffer-local 'report-emacs-bug-bug-alist)
402 (defvar report-emacs-bug-choice-widget nil)
403 (make-variable-buffer-local 'report-emacs-bug-choice-widget)
405 (defun report-emacs-bug-create-existing-bugs-buffer (bugs keywords)
406 (switch-to-buffer (get-buffer-create "*Existing Emacs Bugs*"))
407 (setq buffer-read-only t)
408 (let ((inhibit-read-only t))
409 (erase-buffer)
410 (setq report-emacs-bug-bug-alist bugs)
411 (widget-insert (propertize (concat "Already known bugs ("
412 keywords "):\n\n")
413 'face 'bold))
414 (if bugs
415 (setq report-emacs-bug-choice-widget
416 (apply 'widget-create 'radio-button-choice
417 :value (caar bugs)
418 (let (items)
419 (dolist (bug bugs)
420 (push (list
421 'url-link
422 :format (concat "Bug#" (number-to-string (nth 2 bug))
423 ": " (cadr bug) "\n %[%v%]\n")
424 ;; FIXME: Why is only the link of the
425 ;; active item clickable?
426 (car bug))
427 items))
428 (nreverse items))))
429 (widget-insert "No bugs maching your keywords found.\n"))
430 (widget-insert "\n")
431 (widget-create 'push-button
432 :notify (lambda (&rest ignore)
433 ;; TODO: Do something!
434 (message "Reporting new bug!"))
435 "Report new bug")
436 (when bugs
437 (widget-insert " ")
438 (widget-create 'push-button
439 :notify (lambda (&rest ignore)
440 (let ((val (widget-value report-emacs-bug-choice-widget)))
441 ;; TODO: Do something!
442 (message "Appending to bug %s!"
443 (nth 2 (assoc val report-emacs-bug-bug-alist)))))
444 "Append to chosen bug"))
445 (widget-insert " ")
446 (widget-create 'push-button
447 :notify (lambda (&rest ignore)
448 (kill-buffer))
449 "Quit reporting bug")
450 (widget-insert "\n"))
451 (use-local-map widget-keymap)
452 (widget-setup)
453 (goto-char (point-min)))
455 (defun report-emacs-bug-parse-query-results (status keywords)
456 (goto-char (point-min))
457 (let (buglist)
458 (while (re-search-forward "<a href=\"bugreport\\.cgi\\?bug=\\([[:digit:]]+\\)\">\\([^<]+\\)</a>" nil t)
459 (let ((number (match-string 1))
460 (subject (match-string 2)))
461 (when (not (string-match "^#" subject))
462 (push (list
463 ;; first the bug URL
464 (concat report-emacs-bug-tracker-url
465 "bugreport.cgi?bug=" number)
466 ;; then the subject and number
467 subject (string-to-number number))
468 buglist))))
469 (report-emacs-bug-create-existing-bugs-buffer (nreverse buglist) keywords)))
471 (defun report-emacs-bug-query-existing-bugs (keywords)
472 "Query for KEYWORDS at `report-emacs-bug-tracker-url', and return the result.
473 The result is an alist with items of the form (URL SUBJECT NO)."
474 (interactive "sBug keywords (comma separated): ")
475 (url-retrieve (concat report-emacs-bug-tracker-url
476 "pkgreport.cgi?include=subject%3A"
477 (replace-regexp-in-string "[[:space:]]+" "+" keywords)
478 ";package=emacs")
479 'report-emacs-bug-parse-query-results (list keywords)))
481 (provide 'emacsbug)
483 ;;; emacsbug.el ends here