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