]> code.delx.au - gnu-emacs-elpa/blob - packages/debbugs/debbugs-gnu.el
Cache and reuse bug entries in debbugs
[gnu-emacs-elpa] / packages / debbugs / debbugs-gnu.el
1 ;;; debbugs-gnu.el --- interface for the GNU bug tracker
2
3 ;; Copyright (C) 2011-2016 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Michael Albinus <michael.albinus@gmx.org>
7 ;; Keywords: comm, hypermedia, maint
8 ;; Package: debbugs
9
10 ;; This file is not part of GNU Emacs.
11
12 ;; This program is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides an interface to bug reports which are located
28 ;; on the GNU bug tracker debbugs.gnu.org. Its main purpose is to
29 ;; show and manipulate bug reports from Emacs, but it could be used
30 ;; also for other GNU projects which use the same bug tracker.
31
32 ;; If you have `debbugs-gnu.el' in your load-path, you could enable
33 ;; the bug tracker commands by the following lines in your ~/.emacs
34 ;;
35 ;; (autoload 'debbugs-gnu "debbugs-gnu" "" 'interactive)
36 ;; (autoload 'debbugs-gnu-search "debbugs-gnu" "" 'interactive)
37 ;; (autoload 'debbugs-gnu-usertags "debbugs-gnu" "" 'interactive)
38 ;; (autoload 'debbugs-gnu-bugs "debbugs-gnu" "" 'interactive)
39
40 ;; The bug tracker is called interactively by
41 ;;
42 ;; M-x debbugs-gnu
43
44 ;; It asks for the severities, for which bugs shall be shown. This can
45 ;; be either just one severity, or a list of severities, separated by
46 ;; comma. Valid severities are "serious", "important", "normal",
47 ;; "minor" or "wishlist". Severities "critical" and "grave" are not
48 ;; used, although configured on the GNU bug tracker. If no severity
49 ;; is given, all bugs are selected.
50
51 ;; There is also the pseudo severity "tagged". When it is used, the
52 ;; function will ask for user tags (a comma separated list), and shows
53 ;; just the bugs which are tagged with them. In general, user tags
54 ;; shall be strings denoting to subprojects of the package, like
55 ;; "cedet" or "tramp" of the package "emacs". If no user tag is
56 ;; given, locally tagged bugs are shown.
57
58 ;; If a prefix is given to the command, more search parameters are
59 ;; asked for, like packages (also a comma separated list, "emacs" is
60 ;; the default), whether archived bugs shall be shown, and whether
61 ;; closed bugs shall be suppressed from being retrieved.
62
63 ;; Another command is
64 ;;
65 ;; M-x debbugs-gnu-search
66
67 ;; It behaves like `debbugs-gnu', but asks at the beginning for a
68 ;; search phrase to be used for full text search. Additionally, it
69 ;; asks for key-value pairs to filter bugs. Keys are as described in
70 ;; `debbugs-get-status', the corresponding value must be a regular
71 ;; expression to match for. The other parameters are as described in
72 ;; `debbugs-gnu'. Usually, there is just one value except for the
73 ;; attribute "date", which needs two arguments specifying a period in
74 ;; which the bug has been submitted or modified.
75
76 ;; The bug reports are downloaded from the bug tracker. In order to
77 ;; not generate too much load of the server, up to 500 bugs will be
78 ;; downloaded at once. If there are more hits, several downloads will
79 ;; be performed, until all bugs are retrieved.
80
81 ;; These default values could be changed also by customer options
82 ;; `debbugs-gnu-default-severities', `debbugs-gnu-default-packages'
83 ;; and `debbugs-gnu-default-suppress-bugs'.
84
85 ;; The commands create a page of bug lists. Every bug is shown in one
86 ;; line, including the bug number, the status (combining merged bug
87 ;; numbers, keywords and severities), the name of the submitter, and
88 ;; the title of the bug. On every bug line you could apply the
89 ;; following actions by the following keystrokes:
90
91 ;; RET: Show corresponding messages in Gnus/Rmail
92 ;; "C": Send a control message
93 ;; "t": Mark the bug locally as tagged
94 ;; "b": Show bugs this bug is blocked by
95 ;; "B": Show bugs this bug is blocking
96 ;; "d": Show bug attributes
97
98 ;; Furthermore, you could apply the global actions
99
100 ;; "g": Rescan bugs
101 ;; "q": Quit the buffer
102 ;; "s": Toggle bug sorting for age or for state
103 ;; "x": Toggle suppressing of bugs
104 ;; "/": Display only bugs matching a string
105 ;; "R": Display only bugs blocking the current release
106 ;; "w": Display all the currently selected bug reports
107
108 ;; When you visit the related bug messages in Gnus or Rmail, you could
109 ;; also send control messages by keystroke "C".
110
111 ;; In the header line of every bug list page, you can toggle sorting
112 ;; per column by selecting a column with the mouse. The sorting
113 ;; happens as expected for the respective column; sorting in the Title
114 ;; column is depending on whether you are the owner of a bug.
115
116 ;; Another approach for listing bugs is calling the command
117 ;;
118 ;; M-x debbugs-gnu-usertags
119
120 ;; This command shows you all existing user tags for the packages
121 ;; defined in `debbugs-gnu-default-packages'. A prefix for the
122 ;; command allows you to use other package names, or an arbitrary
123 ;; string for a user who has tagged bugs. The command returns the
124 ;; list of existing user tags for the given user(s) or package
125 ;; name(s), respectively. Applying RET on a user tag, all bugs tagged
126 ;; with this user tag are shown.
127
128 ;; Unfortunately, it is not possible with the SOAP interface to show
129 ;; all users who have tagged bugs. This list can be retrieved via
130 ;; <http://debbugs.gnu.org/cgi/pkgindex.cgi?indexon=users>.
131
132 ;; Finally, if you simply want to list some bugs with known bug
133 ;; numbers, call the command
134 ;;
135 ;; M-x debbugs-gnu-bugs
136
137 ;; The bug numbers to be shown shall be entered as comma separated list.
138
139 ;;; Code:
140
141 (require 'debbugs)
142 (require 'tabulated-list)
143 (require 'add-log)
144 (require 'subr-x)
145 (eval-when-compile (require 'cl))
146
147 (autoload 'article-decode-charset "gnus-art")
148 (autoload 'diff-goto-source "diff-mode")
149 (autoload 'diff-hunk-file-names "diff-mode")
150 (autoload 'gnus-article-mime-handles "gnus-art")
151 (autoload 'gnus-read-ephemeral-emacs-bug-group "gnus-group")
152 (autoload 'gnus-summary-article-header "gnus-sum")
153 (autoload 'gnus-summary-select-article "gnus-sum")
154 (autoload 'gnus-summary-show-article "gnus-sum")
155 (autoload 'gnus-with-article-buffer "gnus-art")
156 (autoload 'log-edit-insert-changelog "log-edit")
157 (autoload 'mail-header-subject "nnheader")
158 (autoload 'message-goto-body "message")
159 (autoload 'message-make-from "message")
160 (autoload 'rmail-get-new-mail "rmail")
161 (autoload 'rmail-show-message "rmail")
162 (autoload 'rmail-summary "rmailsum")
163 (autoload 'vc-dir-hide-up-to-date "vc-dir")
164 (autoload 'vc-dir-mark "vc-dir")
165
166 (defvar compilation-in-progress)
167 (defvar diff-file-header-re)
168 (defvar gnus-article-buffer)
169 (defvar gnus-posting-styles)
170 (defvar gnus-save-duplicate-list)
171 (defvar gnus-suppress-duplicates)
172 (defvar rmail-current-message)
173 (defvar rmail-mode-map)
174 (defvar rmail-summary-mode-map)
175 (defvar rmail-total-messages)
176
177 ;; Buffer-local variables.
178 (defvar debbugs-gnu-local-query)
179 (defvar debbugs-gnu-local-filter)
180 (defvar debbugs-gnu-local-suppress)
181 (defvar debbugs-gnu-sort-state)
182 (defvar debbugs-gnu-limit)
183
184 (defgroup debbugs-gnu ()
185 "UI for the debbugs.gnu.org bug tracker."
186 :group 'debbugs
187 :version "24.1")
188
189 (defvar debbugs-gnu-blocking-report 19759
190 "The ID of the current release report used to track blocking bug reports.")
191
192 (defcustom debbugs-gnu-default-severities '("serious" "important" "normal")
193 "*The list severities bugs are searched for.
194 \"tagged\" is not a severity but marks locally tagged bugs."
195 ;; <http://debbugs.gnu.org/Developer.html#severities>
196 ;; /ssh:debbugs:/etc/debbugs/config @gSeverityList
197 ;; We don't use "critical" and "grave".
198 :group 'debbugs-gnu
199 :type '(set (const "serious")
200 (const "important")
201 (const "normal")
202 (const "minor")
203 (const "wishlist")
204 (const "tagged"))
205 :version "24.1")
206
207 (defconst debbugs-gnu-all-severities
208 (mapcar 'cadr (cdr (get 'debbugs-gnu-default-severities 'custom-type)))
209 "*List of all possible severities.")
210
211 (defcustom debbugs-gnu-default-packages '("emacs")
212 "*The list of packages to be searched for."
213 ;; <http://debbugs.gnu.org/Packages.html>
214 ;; <http://debbugs.gnu.org/cgi/pkgindex.cgi>
215 :group 'debbugs-gnu
216 :type '(set (const "adns")
217 (const "auctex")
218 (const "automake")
219 (const "cc-mode")
220 (const "coreutils")
221 (const "cppi")
222 (const "debbugs.gnu.org")
223 (const "diffutils")
224 (const "emacs")
225 (const "emacs-xwidgets")
226 (const "fm")
227 (const "gnus")
228 (const "grep")
229 (const "guile")
230 (const "guix")
231 (const "gzip")
232 (const "idutils")
233 (const "libtool")
234 (const "mh-e")
235 (const "org-mode")
236 (const "parted")
237 (const "sed")
238 (const "vc-dwim")
239 (const "woodchuck"))
240 :version "25.1")
241
242 (defconst debbugs-gnu-all-packages
243 (mapcar 'cadr (cdr (get 'debbugs-gnu-default-packages 'custom-type)))
244 "*List of all possible package names.")
245
246 (defcustom debbugs-gnu-default-suppress-bugs
247 '((pending . "done"))
248 "*A list of specs for bugs to be suppressed.
249 An element of this list is a cons cell \(KEY . REGEXP\), with key
250 being returned by `debbugs-get-status', and REGEXP a regular
251 expression matching the corresponding value, a string. Showing
252 suppressed bugs is toggled by `debbugs-gnu-toggle-suppress'."
253 :group 'debbugs-gnu
254 :type '(alist :key-type symbol :value-type regexp)
255 :version "24.1")
256
257 (defcustom debbugs-gnu-mail-backend 'gnus
258 "*The email backend to use for reading bug report email exchange.
259 If this is 'gnus, the default, use Gnus.
260 If this is 'rmail, use Rmail instead."
261 :group 'debbugs-gnu
262 :type '(choice (const :tag "Use Gnus" 'gnus)
263 (const :tag "Use Rmail" 'rmail))
264 :version "25.1")
265
266 (defface debbugs-gnu-archived '((t (:inverse-video t)))
267 "Face for archived bug reports.")
268
269 (defface debbugs-gnu-new '((t (:foreground "red")))
270 "Face for new reports that nobody has answered.")
271
272 (defface debbugs-gnu-handled '((t (:foreground "ForestGreen")))
273 "Face for reports that have been modified recently.")
274
275 (defface debbugs-gnu-pending '((t (:foreground "MidnightBlue")))
276 "Face for reports that are pending.")
277
278 (defface debbugs-gnu-stale '((t (:foreground "orange")))
279 "Face for reports that have not been touched for a week.")
280
281 (defface debbugs-gnu-done '((t (:foreground "DarkGrey")))
282 "Face for closed bug reports.")
283
284 (defface debbugs-gnu-tagged '((t (:foreground "red")))
285 "Face for reports that have been tagged locally.")
286
287 (defvar debbugs-gnu-local-tags nil
288 "List of bug numbers tagged locally, and kept persistent.")
289
290 (defvar debbugs-gnu-persistency-file
291 (expand-file-name (locate-user-emacs-file "debbugs"))
292 "File name of a persistency store for debbugs variables")
293
294 (defun debbugs-gnu-dump-persistency-file ()
295 "Function to store debbugs variables persistently."
296 (with-temp-file debbugs-gnu-persistency-file
297 (insert
298 ";; -*- emacs-lisp -*-\n"
299 ";; Debbugs tags connection history. Don't change this file.\n\n"
300 (format "(setq debbugs-gnu-local-tags '%S)"
301 (sort (copy-sequence debbugs-gnu-local-tags) '>)))))
302
303 (defvar debbugs-gnu-current-query nil
304 "The query object of the current search.
305 It will be applied server-side, when calling `debbugs-get-bugs'.
306 It has the same format as `debbugs-gnu-default-suppress-bugs'.")
307
308 (defvar debbugs-gnu-current-filter nil
309 "The filter object for the current search.
310 It will be applied client-side, when parsing the results of
311 `debbugs-get-status'. It has a similar format as
312 `debbugs-gnu-default-suppress-bugs'. In case of keys representing
313 a date, value is the cons cell \(BEFORE . AFTER\).")
314
315 (defvar debbugs-gnu-current-suppress nil
316 "Whether bugs shall be suppressed.
317 The specification which bugs shall be suppressed is taken from
318 `debbugs-gnu-default-suppress-bugs'.")
319
320 (defun debbugs-gnu-calendar-read (prompt acceptable &optional initial-contents)
321 "Return a string read from the minibuffer.
322 Derived from `calendar-read'."
323 (let ((value (read-string prompt initial-contents)))
324 (while (not (funcall acceptable value))
325 (setq value (read-string prompt initial-contents)))
326 value))
327
328 (defconst debbugs-gnu-phrase-prompt
329 (propertize
330 "Enter search phrase: "
331 'help-echo "\
332 The search phrase contains words to be searched for, combined by
333 operators like AND, ANDNOT and OR. If there is no operator
334 between the words, AND is used by default. The phrase can also
335 be empty, in this case only the following attributes are used for
336 search."))
337
338 ;;;###autoload
339 (defun debbugs-gnu-search ()
340 "Search for Emacs bugs interactively.
341 Search arguments are requested interactively. The \"search
342 phrase\" is used for full text search in the bugs database.
343 Further key-value pairs are requested until an empty key is
344 returned. If a key cannot be queried by a SOAP request, it is
345 marked as \"client-side filter\"."
346 (interactive)
347
348 (unwind-protect
349 (let ((date-format "\\([[:digit:]]\\{4\\}\\)-\\([[:digit:]]\\{1,2\\}\\)-\\([[:digit:]]\\{1,2\\}\\)")
350 key val1 val2 phrase severities packages archivedp)
351
352 ;; Check for the phrase.
353 (setq phrase (read-string debbugs-gnu-phrase-prompt))
354 (if (zerop (length phrase))
355 (setq phrase nil)
356 (add-to-list 'debbugs-gnu-current-query (cons 'phrase phrase)))
357 ;; We suppress the bugs if there is no phrase.
358 (setq debbugs-gnu-current-suppress (null phrase))
359
360 ;; The other queries.
361 (catch :finished
362 (while t
363 (setq key (completing-read
364 "Enter attribute: "
365 (if phrase
366 '("severity" "package" "tags" "submitter" "date"
367 "subject" "status")
368 '("severity" "package" "archive" "src" "tag"
369 "owner" "submitter" "maint" "correspondent"
370 "date" "log_modified" "last_modified"
371 "found_date" "fixed_date" "unarchived"
372 "subject" "done" "forwarded" "msgid" "summary"))
373 nil t))
374 (cond
375 ;; Server-side queries.
376 ((equal key "severity")
377 (setq
378 severities
379 (completing-read-multiple
380 "Enter severities: " debbugs-gnu-all-severities nil t
381 (mapconcat 'identity debbugs-gnu-default-severities ","))))
382
383 ((equal key "package")
384 (setq
385 packages
386 (completing-read-multiple
387 "Enter packages: " debbugs-gnu-all-packages nil t
388 (mapconcat 'identity debbugs-gnu-default-packages ","))))
389
390 ((equal key "archive")
391 ;; We simplify, by assuming just archived bugs are requested.
392 (setq archivedp t))
393
394 ((member key '("src" "tag" "tags"))
395 (setq val1 (read-string (format "Enter %s: " key)))
396 (when (not (zerop (length val1)))
397 (add-to-list
398 'debbugs-gnu-current-query (cons (intern key) val1))))
399
400 ((member key '("owner" "submitter" "maint" "correspondent"))
401 (setq val1 (read-string "Enter email address: "))
402 (when (not (zerop (length val1)))
403 (add-to-list
404 'debbugs-gnu-current-query (cons (intern key) val1))))
405
406 ((equal key "status")
407 (setq
408 val1
409 (completing-read "Enter status: " '("done" "forwarded" "open")))
410 (when (not (zerop (length val1)))
411 (add-to-list
412 'debbugs-gnu-current-query (cons (intern key) val1))))
413
414 ;; Client-side filters.
415 ((member key '("date" "log_modified" "last_modified"
416 "found_date" "fixed_date" "unarchived"))
417 (setq val1
418 (debbugs-gnu-calendar-read
419 (format "Enter %s before YYYY-MM-DD%s: "
420 key (if phrase "" " (client-side filter)"))
421 (lambda (x)
422 (string-match (concat "^\\(" date-format "\\|\\)$") x))))
423 (if (string-match date-format val1)
424 (setq val1 (floor
425 (float-time
426 (encode-time
427 0 0 0
428 (string-to-number (match-string 3 val1))
429 (string-to-number (match-string 2 val1))
430 (string-to-number (match-string 1 val1))))))
431 (setq val1 nil))
432 (setq val2
433 (debbugs-gnu-calendar-read
434 (format "Enter %s after YYYY-MM-DD%s: "
435 key (if phrase "" " (client-side filter)"))
436 (lambda (x)
437 (string-match (concat "^\\(" date-format "\\|\\)$") x))))
438 (if (string-match date-format val2)
439 (setq val2 (floor
440 (float-time
441 (encode-time
442 0 0 0
443 (string-to-number (match-string 3 val2))
444 (string-to-number (match-string 2 val2))
445 (string-to-number (match-string 1 val2))))))
446 (setq val2 nil))
447 (when (or val1 val2)
448 (add-to-list
449 (if phrase
450 'debbugs-gnu-current-query 'debbugs-gnu-current-filter)
451 (cons (intern key) (cons val1 val2)))))
452
453 ((not (zerop (length key)))
454 (setq val1
455 (funcall
456 (if phrase 'read-string 'read-regexp)
457 (format "Enter %s%s"
458 key (if phrase ": " " (client-side filter)"))))
459 (when (not (zerop (length val1)))
460 (add-to-list
461 (if phrase
462 'debbugs-gnu-current-query 'debbugs-gnu-current-filter)
463 (cons (intern key) val1))))
464
465 ;; The End.
466 (t (throw :finished nil)))))
467
468 ;; Do the search.
469 (debbugs-gnu severities packages archivedp))))
470
471 ;;;###autoload
472 (defun debbugs-gnu (severities &optional packages archivedp suppress tags)
473 "List all outstanding bugs."
474 (interactive
475 (let (severities archivedp)
476 (list
477 (setq severities
478 (completing-read-multiple
479 "Severities: " debbugs-gnu-all-severities nil t
480 (mapconcat 'identity debbugs-gnu-default-severities ",")))
481 ;; The next parameters are asked only when there is a prefix.
482 (if current-prefix-arg
483 (completing-read-multiple
484 "Packages: " debbugs-gnu-all-packages nil t
485 (mapconcat 'identity debbugs-gnu-default-packages ","))
486 debbugs-gnu-default-packages)
487 (when current-prefix-arg
488 (setq archivedp (y-or-n-p "Show archived bugs?")))
489 (when (and current-prefix-arg (not archivedp))
490 (y-or-n-p "Suppress unwanted bugs?"))
491 ;; This one must be asked for severity "tagged".
492 (when (member "tagged" severities)
493 (split-string (read-string "User tag(s): ") "," t)))))
494
495 ;; Initialize variables.
496 (when (and (file-exists-p debbugs-gnu-persistency-file)
497 (not debbugs-gnu-local-tags))
498 (with-temp-buffer
499 (insert-file-contents debbugs-gnu-persistency-file)
500 (eval (read (current-buffer)))))
501 ;; Per default, we suppress retrieved unwanted bugs.
502 (when (called-interactively-p 'any)
503 (setq debbugs-gnu-current-suppress t))
504
505 ;; Add queries.
506 (dolist (severity (if (consp severities) severities (list severities)))
507 (when (not (zerop (length severity)))
508 (when (string-equal severity "tagged")
509 (setq debbugs-gnu-current-suppress nil))
510 (add-to-list 'debbugs-gnu-current-query (cons 'severity severity))))
511 (dolist (package (if (consp packages) packages (list packages)))
512 (when (not (zerop (length package)))
513 (add-to-list 'debbugs-gnu-current-query (cons 'package package))))
514 (when archivedp
515 (setq debbugs-gnu-current-suppress nil)
516 (add-to-list 'debbugs-gnu-current-query '(archive . "1")))
517 (when suppress
518 (setq debbugs-gnu-current-suppress t)
519 (add-to-list 'debbugs-gnu-current-query '(status . "open"))
520 (add-to-list 'debbugs-gnu-current-query '(status . "forwarded")))
521 (dolist (tag (if (consp tags) tags (list tags)))
522 (when (not (zerop (length tag)))
523 (add-to-list 'debbugs-gnu-current-query (cons 'tag tag))))
524
525 ;; Show result.
526 (debbugs-gnu-show-reports)
527
528 ;; Reset query, filter and suppress.
529 (setq debbugs-gnu-current-query nil
530 debbugs-gnu-current-filter nil
531 debbugs-gnu-current-suppress nil))
532
533 (defun debbugs-gnu-get-bugs (query)
534 "Retrieve bugs numbers from debbugs.gnu.org according search criteria."
535 (let* ((debbugs-port "gnu.org")
536 (bugs (assoc 'bugs query))
537 (tags (assoc 'tag query))
538 (local-tags (and (member '(severity . "tagged") query) (not tags)))
539 (phrase (assoc 'phrase query))
540 args)
541 ;; Compile query arguments.
542 (unless (or query tags)
543 (dolist (elt debbugs-gnu-default-packages)
544 (setq args (append args (list :package elt)))))
545 (dolist (elt query)
546 (unless (equal elt '(severity . "tagged"))
547 (setq args
548 (append
549 args
550 (if phrase
551 (cond
552 ((eq (car elt) 'phrase)
553 (list (list :phrase (cdr elt) :max 500)))
554 ((eq (car elt) 'date)
555 (list (list :date (cddr elt) (cadr elt)
556 :operator "NUMBT")))
557 (t
558 (list (list (intern (concat ":" (symbol-name (car elt))))
559 (cdr elt) :operator "ISTRINC"))))
560 (list (intern (concat ":" (symbol-name (car elt))))
561 (cdr elt)))))))
562
563 (cond
564 ;; If the query is just a list of bug numbers, we return them.
565 (bugs (cdr bugs))
566 ;; If the query contains the pseudo-severity "tagged", we return
567 ;; just the local tagged bugs.
568 (local-tags (copy-sequence debbugs-gnu-local-tags))
569 ;; A full text query.
570 (phrase
571 (mapcar
572 (lambda (x) (cdr (assoc "id" x)))
573 (apply 'debbugs-search-est args)))
574 ;; User tags.
575 (tags
576 (setq args (mapcar (lambda (x) (if (eq x :package) :user x)) args))
577 (apply 'debbugs-get-usertag args))
578 ;; Otherwise, we retrieve the bugs from the server.
579 (t (apply 'debbugs-get-bugs args)))))
580
581 (defun debbugs-gnu-show-reports ()
582 "Show bug reports."
583 (let ((inhibit-read-only t)
584 (debbugs-port "gnu.org")
585 (buffer-name "*Emacs Bugs*"))
586 ;; The tabulated mode sets several local variables. We must get
587 ;; rid of them.
588 (when (get-buffer buffer-name)
589 (kill-buffer buffer-name))
590 (switch-to-buffer (get-buffer-create buffer-name))
591 (debbugs-gnu-mode)
592
593 ;; Print bug reports.
594 (dolist (status
595 (apply 'debbugs-get-status
596 (debbugs-gnu-get-bugs debbugs-gnu-local-query)))
597 (let* ((id (cdr (assq 'id status)))
598 (words
599 (mapconcat
600 'identity
601 (cons (cdr (assq 'severity status))
602 (cdr (assq 'keywords status)))
603 ","))
604 (address (if (cdr (assq 'originator status))
605 (mail-header-parse-address
606 (decode-coding-string (cdr (assq 'originator status))
607 'utf-8))))
608 (owner (if (cdr (assq 'owner status))
609 (car (mail-header-parse-address
610 (decode-coding-string (cdr (assq 'owner status))
611 'utf-8)))))
612 (subject (if (cdr (assq 'subject status))
613 (decode-coding-string (cdr (assq 'subject status))
614 'utf-8)))
615 merged)
616 (unless (equal (cdr (assq 'pending status)) "pending")
617 (setq words (concat words "," (cdr (assq 'pending status)))))
618 (let ((packages (delete "emacs" (cdr (assq 'package status)))))
619 (when packages
620 (setq words (concat words "," (mapconcat 'identity packages ",")))))
621 (when (setq merged (cdr (assq 'mergedwith status)))
622 (setq words (format "%s,%s"
623 (if (numberp merged)
624 merged
625 (mapconcat 'number-to-string merged ","))
626 words)))
627 (when (or (not merged)
628 (not (let ((found nil))
629 (dolist (id (if (listp merged)
630 merged
631 (list merged)))
632 (dolist (entry tabulated-list-entries)
633 (when (equal id (cdr (assq 'id (car entry))))
634 (setq found t))))
635 found)))
636 (add-to-list
637 'tabulated-list-entries
638 (list
639 status
640 (vector
641 (propertize
642 (format "%5d" id)
643 'face
644 ;; Mark tagged bugs.
645 (if (memq id debbugs-gnu-local-tags)
646 'debbugs-gnu-tagged
647 'default))
648 (propertize
649 ;; Mark status and age.
650 (or words "")
651 'face
652 (cond
653 ((cdr (assq 'archived status))
654 'debbugs-gnu-archived)
655 ((equal (cdr (assq 'pending status)) "done")
656 'debbugs-gnu-done)
657 ((member "pending" (cdr (assq 'keywords status)))
658 'debbugs-gnu-pending)
659 ((= (cdr (assq 'date status))
660 (cdr (assq 'log_modified status)))
661 'debbugs-gnu-new)
662 ((< (- (float-time)
663 (cdr (assq 'log_modified status)))
664 (* 60 60 24 7 2))
665 'debbugs-gnu-handled)
666 (t
667 'debbugs-gnu-stale)))
668 (propertize
669 ;; Prefer the name over the address.
670 (or (cdr address)
671 (car address)
672 "")
673 'face
674 ;; Mark own submitted bugs.
675 (if (and (stringp (car address))
676 (string-equal (car address) user-mail-address))
677 'debbugs-gnu-tagged
678 'default))
679 (propertize
680 (or subject "")
681 'face
682 ;; Mark owned bugs.
683 (if (and (stringp owner)
684 (string-equal owner user-mail-address))
685 'debbugs-gnu-tagged
686 'default))))
687 'append))))
688
689 (tabulated-list-init-header)
690 (tabulated-list-print)
691
692 (set-buffer-modified-p nil)
693 (goto-char (point-min))))
694
695 (defun debbugs-gnu-print-entry (list-id cols)
696 "Insert a debbugs entry at point.
697 Used instead of `tabulated-list-print-entry'."
698 (let ((beg (point))
699 (pos 0)
700 (case-fold-search t)
701 (id (aref cols 0))
702 (id-length (nth 1 (aref tabulated-list-format 0)))
703 (state (aref cols 1))
704 (state-length (nth 1 (aref tabulated-list-format 1)))
705 (submitter (aref cols 2))
706 (submitter-length (nth 1 (aref tabulated-list-format 2)))
707 (title (aref cols 3))
708 (title-length (nth 1 (aref tabulated-list-format 3))))
709 (when (and
710 ;; We may have a narrowing in effect.
711 (or (not debbugs-gnu-limit)
712 (memq (cdr (assq 'id list-id)) debbugs-gnu-limit))
713 ;; Filter suppressed bugs.
714 (or (not debbugs-gnu-local-suppress)
715 (not (catch :suppress
716 (dolist (check debbugs-gnu-default-suppress-bugs)
717 (when
718 (string-match
719 (cdr check)
720 (or (cdr (assq (car check) list-id)) ""))
721 (throw :suppress t))))))
722 ;; Filter search list.
723 (not (catch :suppress
724 (dolist (check debbugs-gnu-local-filter)
725 (let ((val (cdr (assq (car check) list-id))))
726 (if (stringp (cdr check))
727 ;; Regular expression.
728 (when (not (string-match (cdr check) (or val "")))
729 (throw :suppress t))
730 ;; Time value.
731 (when (or (and (numberp (cadr check))
732 (< (cadr check) val))
733 (and (numberp (cddr check))
734 (> (cddr check) val)))
735 (throw :suppress t))))))))
736
737 ;; Insert id.
738 (indent-to (- id-length (length id)))
739 (insert id)
740 ;; Insert state.
741 (indent-to (setq pos (+ pos id-length 1)) 1)
742 (insert (if (> (length state) state-length)
743 (propertize (substring state 0 state-length)
744 'help-echo state)
745 state))
746 ;; Insert submitter.
747 (indent-to (setq pos (+ pos state-length 1)) 1)
748 (insert "[" (if (> (length submitter) (- submitter-length 2))
749 (propertize (substring submitter 0 (- submitter-length 2))
750 'help-echo submitter)
751 submitter))
752 (indent-to (+ pos (1- submitter-length)))
753 (insert "]")
754 ;; Insert title.
755 (indent-to (setq pos (+ pos submitter-length 1)) 1)
756 (insert (propertize title 'help-echo title))
757 ;; Add properties.
758 (add-text-properties
759 beg (point)
760 `(tabulated-list-id ,list-id mouse-face highlight))
761 (insert ?\n))))
762
763 (defun debbugs-gnu-menu-map-emacs-enabled ()
764 "Whether \"Show Release Blocking Bugs\" is enabled in the menu."
765 (or ;; No package discriminator has been used.
766 (not (assq 'package debbugs-gnu-local-query))
767 ;; Package "emacs" has been selected.
768 (member '(package . "emacs") debbugs-gnu-local-query)))
769
770 (defconst debbugs-gnu-bug-triage-file
771 (expand-file-name "../admin/notes/bug-triage" data-directory)
772 "The \"bug-triage\" file.")
773
774 (defun debbugs-gnu-menu-map-bug-triage-enabled ()
775 "Whether \"Describe Bug Triage Procedure\" is enabled in the menu."
776 (and (debbugs-gnu-menu-map-emacs-enabled)
777 (stringp debbugs-gnu-bug-triage-file)
778 (file-readable-p debbugs-gnu-bug-triage-file)))
779
780 (defun debbugs-gnu-view-bug-triage ()
781 "Show \"bug-triage\" file."
782 (interactive)
783 (view-file debbugs-gnu-bug-triage-file))
784
785 (defvar debbugs-gnu-mode-map
786 (let ((map (make-sparse-keymap))
787 (menu-map (make-sparse-keymap)))
788 (set-keymap-parent map tabulated-list-mode-map)
789 (define-key map "\r" 'debbugs-gnu-select-report)
790 (define-key map [mouse-1] 'debbugs-gnu-select-report)
791 (define-key map [mouse-2] 'debbugs-gnu-select-report)
792 (define-key map "g" 'debbugs-gnu-rescan)
793 (define-key map "R" 'debbugs-gnu-show-all-blocking-reports)
794 (define-key map "C" 'debbugs-gnu-send-control-message)
795
796 (define-key map "s" 'debbugs-gnu-toggle-sort)
797 (define-key map "t" 'debbugs-gnu-toggle-tag)
798 (define-key map "x" 'debbugs-gnu-toggle-suppress)
799 (define-key map "/" 'debbugs-gnu-narrow-to-status)
800 (define-key map "w" 'debbugs-gnu-widen)
801
802 (define-key map "b" 'debbugs-gnu-show-blocked-by-reports)
803 (define-key map "B" 'debbugs-gnu-show-blocking-reports)
804 (define-key map "d" 'debbugs-gnu-display-status)
805
806 (define-key map [menu-bar debbugs] (cons "Debbugs" menu-map))
807 (define-key menu-map [debbugs-gnu-select-report]
808 '(menu-item "Show Reports" debbugs-gnu-select-report
809 :help "Show all reports belonging to this bug"))
810 (define-key-after menu-map [debbugs-gnu-rescan]
811 '(menu-item "Refresh Bugs" debbugs-gnu-rescan
812 :help "Refresh bug list")
813 'debbugs-gnu-select-report)
814 (define-key-after menu-map [debbugs-gnu-show-all-blocking-reports]
815 '(menu-item "Show Release Blocking Bugs"
816 debbugs-gnu-show-all-blocking-reports
817 :enable (debbugs-gnu-menu-map-emacs-enabled)
818 :help "Show all bugs blocking next Emacs release")
819 'debbugs-gnu-rescan)
820 (define-key-after menu-map [debbugs-gnu-send-control-message]
821 '(menu-item "Send Control Message"
822 debbugs-gnu-send-control-message
823 :help "Send control message to debbugs.gnu.org")
824 'debbugs-gnu-show-all-blocking-reports)
825
826 (define-key-after menu-map [debbugs-gnu-separator1]
827 '(menu-item "--") 'debbugs-gnu-send-control-message)
828 (define-key-after menu-map [debbugs-gnu-search]
829 '(menu-item "Search Bugs" debbugs-gnu-search
830 :help "Search bugs on debbugs.gnu.org")
831 'debbugs-gnu-separator1)
832 (define-key-after menu-map [debbugs-gnu]
833 '(menu-item "Retrieve Bugs" debbugs-gnu
834 :help "Retrieve bugs from debbugs.gnu.org")
835 'debbugs-gnu-search)
836 (define-key-after menu-map [debbugs-gnu-bugs]
837 '(menu-item "Retrieve Bugs by Number" debbugs-gnu-bugs
838 :help "Retrieve selected bugs from debbugs.gnu.org")
839 'debbugs-gnu)
840
841 (define-key-after menu-map [debbugs-gnu-separator2]
842 '(menu-item "--") 'debbugs-gnu-bugs)
843 (define-key-after menu-map [debbugs-gnu-view-bug-triage]
844 '(menu-item "Describe Bug Triage Procedure"
845 debbugs-gnu-view-bug-triage
846 :enable (debbugs-gnu-menu-map-bug-triage-enabled)
847 :help "Show procedure of triaging bugs")
848 'debbugs-gnu-separator2)
849 map))
850
851 (defun debbugs-gnu-rescan ()
852 "Rescan the current set of bug reports."
853 (interactive)
854 (let ((id (debbugs-gnu-current-id))
855 (debbugs-gnu-current-query debbugs-gnu-local-query)
856 (debbugs-gnu-current-filter debbugs-gnu-local-filter)
857 (debbugs-gnu-current-suppress debbugs-gnu-local-suppress))
858 (debbugs-gnu-show-reports)
859 (when id
860 (debbugs-gnu-goto id))))
861
862 (define-derived-mode debbugs-gnu-mode tabulated-list-mode "Debbugs"
863 "Major mode for listing bug reports.
864
865 All normal editing commands are switched off.
866 \\<debbugs-gnu-mode-map>
867
868 The following commands are available:
869
870 \\{debbugs-gnu-mode-map}"
871 (set (make-local-variable 'debbugs-gnu-sort-state) 'number)
872 (set (make-local-variable 'debbugs-gnu-limit) nil)
873 (set (make-local-variable 'debbugs-gnu-local-query)
874 debbugs-gnu-current-query)
875 (set (make-local-variable 'debbugs-gnu-local-filter)
876 debbugs-gnu-current-filter)
877 (set (make-local-variable 'debbugs-gnu-local-suppress)
878 debbugs-gnu-current-suppress)
879 (setq tabulated-list-format [("Id" 5 debbugs-gnu-sort-id)
880 ("State" 20 debbugs-gnu-sort-state)
881 ("Submitter" 25 t)
882 ("Title" 10 debbugs-gnu-sort-title)])
883 (setq tabulated-list-sort-key (cons "Id" nil))
884 (setq tabulated-list-printer 'debbugs-gnu-print-entry)
885 (buffer-disable-undo)
886 (setq truncate-lines t)
887 (setq buffer-read-only t))
888
889 (defun debbugs-gnu-sort-id (s1 s2)
890 (> (cdr (assq 'id (car s1)))
891 (cdr (assq 'id (car s2)))))
892
893 (defconst debbugs-gnu-state-preference
894 '((debbugs-gnu-new . 1)
895 (debbugs-gnu-stale . 2)
896 (debbugs-gnu-handled . 3)
897 (debbugs-gnu-done . 4)
898 (debbugs-gnu-pending . 5)))
899
900 (defun debbugs-gnu-get-state-preference (face-string)
901 (or (cdr (assq (get-text-property 0 'face face-string)
902 debbugs-gnu-state-preference))
903 10))
904
905 (defconst debbugs-gnu-severity-preference
906 '(("serious" . 1)
907 ("important" . 2)
908 ("normal" . 3)
909 ("minor" . 4)
910 ("wishlist" . 5)))
911
912 (defun debbugs-gnu-get-severity-preference (state)
913 (or (cdr (assoc (cdr (assq 'severity state))
914 debbugs-gnu-severity-preference))
915 10))
916
917 (defun debbugs-gnu-sort-state (s1 s2)
918 (let ((id1 (cdr (assq 'id (car s1))))
919 (age1 (debbugs-gnu-get-state-preference (aref (nth 1 s1) 1)))
920 (id2 (cdr (assq 'id (car s2))))
921 (age2 (debbugs-gnu-get-state-preference (aref (nth 1 s2) 1))))
922 (cond
923 ;; Tagged bugs go to the end.
924 ((and (not (memq id1 debbugs-gnu-local-tags))
925 (memq id2 debbugs-gnu-local-tags))
926 t)
927 ((and (memq id1 debbugs-gnu-local-tags)
928 (not (memq id2 debbugs-gnu-local-tags)))
929 nil)
930 ;; Then, we check the age of the bugs.
931 ((< age1 age2)
932 t)
933 ((> age1 age2)
934 nil)
935 ;; If they have the same age, we check for severity.
936 ((< (debbugs-gnu-get-severity-preference (car s1))
937 (debbugs-gnu-get-severity-preference (car s2)))
938 t)
939 (t nil))))
940
941 (defun debbugs-gnu-sort-title (s1 s2)
942 (let ((owner (if (cdr (assq 'owner (car s1)))
943 (car (mail-header-parse-address
944 (decode-coding-string (cdr (assq 'owner (car s1)))
945 'utf-8))))))
946 (and (stringp owner)
947 (string-equal owner user-mail-address))))
948
949 (defun debbugs-gnu-toggle-sort ()
950 "Toggle sorting by age and by state."
951 (interactive)
952 (if (eq debbugs-gnu-sort-state 'number)
953 (progn
954 (setq debbugs-gnu-sort-state 'state)
955 (setq tabulated-list-sort-key (cons "Id" nil)))
956 (setq debbugs-gnu-sort-state 'number)
957 (setq tabulated-list-sort-key (cons "State" nil)))
958 (tabulated-list-init-header)
959 (tabulated-list-print))
960
961 (defun debbugs-gnu-widen ()
962 "Display all the currently selected bug reports."
963 (interactive)
964 (let ((id (debbugs-gnu-current-id t))
965 (inhibit-read-only t))
966 (setq debbugs-gnu-limit nil)
967 (tabulated-list-init-header)
968 (tabulated-list-print)
969 (when id
970 (debbugs-gnu-goto id))))
971
972 (defun debbugs-gnu-show-blocked-by-reports ()
973 "Display all bug reports this report is blocked by."
974 (interactive)
975 (let ((id (debbugs-gnu-current-id))
976 (status (debbugs-gnu-current-status)))
977 (if (null (cdr (assq 'blockedby status)))
978 (message "Bug %d is not blocked by any other bug" id)
979 (apply 'debbugs-gnu-bugs (cdr (assq 'blockedby status))))))
980
981 (defun debbugs-gnu-show-blocking-reports ()
982 "Display all bug reports this report is blocking."
983 (interactive)
984 (let ((id (debbugs-gnu-current-id))
985 (status (debbugs-gnu-current-status)))
986 (if (null (cdr (assq 'blocks status)))
987 (message "Bug %d is not blocking any other bug" id)
988 (apply 'debbugs-gnu-bugs (cdr (assq 'blocks status))))))
989
990 (defun debbugs-gnu-show-all-blocking-reports ()
991 "Narrow the display to just the reports that are blocking a release."
992 (interactive)
993 (let ((blockers (cdr (assq 'blockedby
994 (car (debbugs-get-status
995 debbugs-gnu-blocking-report)))))
996 (id (debbugs-gnu-current-id t))
997 (inhibit-read-only t)
998 status)
999 (setq debbugs-gnu-limit nil)
1000 (goto-char (point-min))
1001 (while (not (eobp))
1002 (setq status (debbugs-gnu-current-status))
1003 (if (not (memq (cdr (assq 'id status)) blockers))
1004 (delete-region (point) (progn (forward-line 1) (point)))
1005 (push (cdr (assq 'id status)) debbugs-gnu-limit)
1006 (forward-line 1)))
1007 (when id
1008 (debbugs-gnu-goto id))))
1009
1010 (defun debbugs-gnu-narrow-to-status (string &optional status-only)
1011 "Only display the bugs matching STRING.
1012 If STATUS-ONLY (the prefix), ignore matches in the From and
1013 Subject fields."
1014 (interactive "sNarrow to: \nP")
1015 (let ((id (debbugs-gnu-current-id t))
1016 (inhibit-read-only t)
1017 status)
1018 (setq debbugs-gnu-limit nil)
1019 (if (equal string "")
1020 (debbugs-gnu-toggle-suppress)
1021 (goto-char (point-min))
1022 (while (not (eobp))
1023 (setq status (debbugs-gnu-current-status))
1024 (if (and (not (member string (assq 'keywords status)))
1025 (not (equal string (cdr (assq 'severity status))))
1026 (or status-only
1027 (not (string-match
1028 string (cdr (assq 'originator status)))))
1029 (or status-only
1030 (not (string-match string (cdr (assq 'subject status))))))
1031 (delete-region (point) (progn (forward-line 1) (point)))
1032 (push (cdr (assq 'id status)) debbugs-gnu-limit)
1033 (forward-line 1)))
1034 (when id
1035 (debbugs-gnu-goto id)))))
1036
1037 (defun debbugs-gnu-goto (id)
1038 "Go to the line displaying bug ID."
1039 (goto-char (point-min))
1040 (while (and (not (eobp))
1041 (not (equal (debbugs-gnu-current-id t) id)))
1042 (forward-line 1)))
1043
1044 (defun debbugs-gnu-toggle-tag ()
1045 "Toggle the local tag of the report in the current line.
1046 If a report is tagged locally, it is presumed to be of little
1047 interest to you."
1048 (interactive)
1049 (save-excursion
1050 (beginning-of-line)
1051 (let ((inhibit-read-only t)
1052 (id (debbugs-gnu-current-id)))
1053 (if (memq id debbugs-gnu-local-tags)
1054 (progn
1055 (setq debbugs-gnu-local-tags (delq id debbugs-gnu-local-tags))
1056 (put-text-property (point) (+ (point) 5) 'face 'default))
1057 (add-to-list 'debbugs-gnu-local-tags id)
1058 (put-text-property
1059 (+ (point) (- 5 (length (number-to-string id)))) (+ (point) 5)
1060 'face 'debbugs-gnu-tagged))
1061 (debbugs-gnu--update-tag-face id)))
1062 (debbugs-gnu-dump-persistency-file))
1063
1064 (defun debbugs-gnu--update-tag-face (id)
1065 (dolist (entry tabulated-list-entries)
1066 (when (equal (cdr (assq 'id (car entry))) id)
1067 (aset (cadr entry) 0
1068 (propertize
1069 (format "%5d" id)
1070 'face
1071 ;; Mark tagged bugs.
1072 (if (memq id debbugs-gnu-local-tags)
1073 'debbugs-gnu-tagged
1074 'default))))))
1075
1076 (defun debbugs-gnu-toggle-suppress ()
1077 "Suppress bugs marked in `debbugs-gnu-suppress-bugs'."
1078 (interactive)
1079 (setq debbugs-gnu-local-suppress (not debbugs-gnu-local-suppress))
1080 (tabulated-list-init-header)
1081 (tabulated-list-print))
1082
1083 (defvar debbugs-gnu-bug-number nil)
1084 (defvar debbugs-gnu-subject nil)
1085
1086 (defun debbugs-gnu-current-id (&optional noerror)
1087 (or (cdr (assq 'id (debbugs-gnu-current-status)))
1088 (and (not noerror)
1089 (error "No bug on the current line"))))
1090
1091 (defun debbugs-gnu-current-status ()
1092 (get-text-property (line-beginning-position) 'tabulated-list-id))
1093
1094 (defun debbugs-gnu-display-status (query filter status)
1095 "Display the query, filter and status of the report on the current line."
1096 (interactive (list debbugs-gnu-local-query
1097 debbugs-gnu-local-filter
1098 (debbugs-gnu-current-status)))
1099 (switch-to-buffer "*Bug Status*")
1100 (let ((inhibit-read-only t))
1101 (erase-buffer)
1102 (when query
1103 (pp query (current-buffer))
1104 (insert "\n"))
1105 (when filter
1106 (pp filter (current-buffer))
1107 (insert "\n"))
1108 (when status (pp status (current-buffer)))
1109 (goto-char (point-min)))
1110 (set-buffer-modified-p nil)
1111 (special-mode))
1112
1113 (defun debbugs-read-emacs-bug-with-rmail (id status merged)
1114 "Read email exchange for debbugs bug ID.
1115 STATUS is the bug's status list.
1116 MERGED is the list of bugs merged with this one."
1117 (let* ((mbox-dir (make-temp-file "debbugs" t))
1118 (mbox-fname (format "%s/bug_%d.mbox" mbox-dir id)))
1119 (debbugs-get-mbox id 'mboxmaint mbox-fname)
1120 (rmail mbox-fname)
1121 ;; Download messages of all the merged bug reports and append them
1122 ;; to the mailbox of the requested bug.
1123 (when merged
1124 (dolist (bugno merged)
1125 (let ((fn (make-temp-file "url")))
1126 (debbugs-get-mbox bugno 'mboxmaint fn)
1127 (rmail-get-new-mail fn)
1128 (delete-file fn)
1129 ;; Remove the 'unseen' attribute from all the messages we've
1130 ;; just read, so that all of them appear in the summary with
1131 ;; the same face.
1132 (while (< rmail-current-message rmail-total-messages)
1133 (rmail-show-message (1+ rmail-current-message))))))
1134 (set (make-local-variable 'debbugs-gnu-bug-number) id)
1135 (set (make-local-variable 'debbugs-gnu-subject)
1136 (format "Re: bug#%d: %s" id (cdr (assq 'subject status))))
1137 (rmail-summary)
1138 (define-key rmail-summary-mode-map "C" 'debbugs-gnu-send-control-message)
1139 (set-window-text-height nil 10)
1140 (other-window 1)
1141 (define-key rmail-mode-map "C" 'debbugs-gnu-send-control-message)
1142 (rmail-show-message 1)))
1143
1144 (defun debbugs-read-emacs-bug-with-gnus (id status merged)
1145 "Read email exchange for debbugs bug ID.
1146 STATUS is the bug's status list.
1147 MERGED is the list of bugs merged with this one."
1148 (require 'gnus-dup)
1149 (setq gnus-suppress-duplicates t
1150 gnus-save-duplicate-list t)
1151 ;; Use Gnus.
1152 (gnus-read-ephemeral-emacs-bug-group
1153 (cons id (if (listp merged) merged (list merged)))
1154 (cons (current-buffer)
1155 (current-window-configuration)))
1156 (with-current-buffer (window-buffer (selected-window))
1157 (set (make-local-variable 'debbugs-gnu-bug-number) id)
1158 (set (make-local-variable 'debbugs-gnu-subject)
1159 (format "Re: bug#%d: %s" id (cdr (assq 'subject status))))
1160 (debbugs-gnu-summary-mode 1)))
1161
1162 (defun debbugs-gnu-select-report ()
1163 "Select the report on the current line."
1164 (interactive)
1165 ;; We open the report messages.
1166 (let* ((status (debbugs-gnu-current-status))
1167 (id (cdr (assq 'id status)))
1168 (merged (cdr (assq 'mergedwith status))))
1169 (setq merged (if (listp merged) merged (list merged)))
1170 (cond
1171 ((not id)
1172 (message "No bug report on the current line"))
1173 ((eq debbugs-gnu-mail-backend 'rmail)
1174 (debbugs-read-emacs-bug-with-rmail id status merged))
1175 ((eq debbugs-gnu-mail-backend 'gnus)
1176 (debbugs-read-emacs-bug-with-gnus id status merged))
1177 (t (error "No valid mail backend specified")))))
1178
1179 (defvar debbugs-gnu-summary-mode-map
1180 (let ((map (make-sparse-keymap)))
1181 (define-key map "C" 'debbugs-gnu-send-control-message)
1182 (define-key map [(meta m)] 'debbugs-gnu-apply-patch)
1183 map))
1184
1185 (define-minor-mode debbugs-gnu-summary-mode
1186 "Minor mode for providing a debbugs interface in Gnus summary buffers.
1187
1188 \\{debbugs-gnu-summary-mode-map}"
1189 :lighter " Debbugs" :keymap debbugs-gnu-summary-mode-map
1190 (set (make-local-variable 'gnus-posting-styles)
1191 `((".*"
1192 (eval
1193 (when (buffer-live-p gnus-article-copy)
1194 (with-current-buffer gnus-article-copy
1195 (set (make-local-variable 'message-prune-recipient-rules)
1196 '((".*@debbugs.*" "emacs-pretest-bug")
1197 (".*@debbugs.*" "bug-gnu-emacs")
1198 ("[0-9]+@debbugs.*" "submit@debbugs.gnu.org")
1199 ("[0-9]+@debbugs.*" "quiet@debbugs.gnu.org")))
1200 (set (make-local-variable 'message-alter-recipients-function)
1201 (lambda (address)
1202 (if (string-match "\\([0-9]+\\)@donarmstrong"
1203 (car address))
1204 (let ((new (format "%s@debbugs.gnu.org"
1205 (match-string 1 (car address)))))
1206 (cons new new))
1207 address)))
1208 ;; `gnus-posting-styles' is eval'ed after
1209 ;; `message-simplify-subject'. So we cannot use m-s-s.
1210 (setq subject ,debbugs-gnu-subject))))))))
1211
1212 (defun debbugs-gnu-guess-current-id ()
1213 "Guess the ID based on \"#23\"."
1214 (save-excursion
1215 (beginning-of-line)
1216 (and
1217 (or (re-search-forward "#\\([0-9]+\\)" (line-end-position) t)
1218 (progn
1219 (goto-char (point-min))
1220 (re-search-forward "#\\([0-9]+\\)" nil t)))
1221 (string-to-number (match-string 1)))))
1222
1223 (defun debbugs-gnu-send-control-message (message &optional reverse)
1224 "Send a control message for the current bug report.
1225 You can set the severity or add a tag, or close the report. If
1226 you use the special \"done\" MESSAGE, the report will be marked as
1227 fixed, and then closed.
1228
1229 If given a prefix, and given a tag to set, the tag will be
1230 removed instead."
1231 (interactive
1232 (list (completing-read
1233 "Control message: "
1234 '("serious" "important" "normal" "minor" "wishlist"
1235 "done" "donenotabug" "donewontfix" "doneunreproducible"
1236 "unarchive" "unmerge" "reopen" "close"
1237 "merge" "forcemerge"
1238 "block" "unblock"
1239 "owner" "noowner"
1240 "invalid"
1241 "reassign"
1242 "patch" "wontfix" "moreinfo" "unreproducible" "fixed" "notabug"
1243 "pending" "help" "security" "confirmed"
1244 "usertag")
1245 nil t)
1246 current-prefix-arg))
1247 (let* ((id (or debbugs-gnu-bug-number ; Set on group entry.
1248 (debbugs-gnu-guess-current-id)
1249 (debbugs-gnu-current-id)))
1250 (version
1251 (when (member message '("close" "done"))
1252 (read-string
1253 "Version: "
1254 (cond
1255 ;; Emacs development versions.
1256 ((string-match
1257 "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\." emacs-version)
1258 (format "%s.%d"
1259 (match-string 1 emacs-version)
1260 (1+ (string-to-number (match-string 2 emacs-version)))))
1261 ;; Emacs release versions.
1262 ((string-match
1263 "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" emacs-version)
1264 (format "%s.%s"
1265 (match-string 1 emacs-version)
1266 (match-string 2 emacs-version)))
1267 (t emacs-version)))))
1268 (status (debbugs-gnu-current-status)))
1269 (with-temp-buffer
1270 (insert "To: control@debbugs.gnu.org\n"
1271 "From: " (message-make-from) "\n"
1272 (format "Subject: control message for bug #%d\n" id)
1273 "\n"
1274 (cond
1275 ((member message '("unarchive" "unmerge" "reopen" "noowner"))
1276 (format "%s %d\n" message id))
1277 ((member message '("merge" "forcemerge"))
1278 (format "%s %d %s\n" message id
1279 (read-string "Merge with bug #: ")))
1280 ((member message '("block" "unblock"))
1281 (format
1282 "%s %d by %s\n" message id
1283 (mapconcat
1284 'identity
1285 (completing-read-multiple
1286 (format "%s with bug(s) #: " (capitalize message))
1287 (if (equal message "unblock")
1288 (mapcar 'number-to-string
1289 (cdr (assq 'blockedby status))))
1290 nil (and (equal message "unblock") status))
1291 " ")))
1292 ((equal message "owner")
1293 (format "owner %d !\n" id))
1294 ((equal message "reassign")
1295 (format "reassign %d %s\n" id (read-string "Package(s): ")))
1296 ((equal message "close")
1297 (format "close %d %s\n" id version))
1298 ((equal message "done")
1299 (format "tags %d fixed\nclose %d %s\n" id id version))
1300 ((member message '("donenotabug" "donewontfix"
1301 "doneunreproducible"))
1302 (format "tags %d %s\nclose %d\n" id (substring message 4) id))
1303 ((member message '("serious" "important" "normal"
1304 "minor" "wishlist"))
1305 (format "severity %d %s\n" id message))
1306 ((equal message "invalid")
1307 (format "tags %d notabug\ntags %d wontfix\nclose %d\n"
1308 id id id))
1309 ((equal message "usertag")
1310 (format "user %s\nusertag %d %s\n"
1311 (completing-read
1312 "Package name or email address: "
1313 (append
1314 debbugs-gnu-all-packages (list user-mail-address))
1315 nil nil (car debbugs-gnu-default-packages))
1316 id (read-string "User tag: ")))
1317 (t
1318 (format "tags %d%s %s\n"
1319 id (if reverse " -" "")
1320 message))))
1321 (funcall send-mail-function)
1322 (remhash id debbugs-cache-data)
1323 (message-goto-body)
1324 (message "Control message sent:\n%s"
1325 (buffer-substring-no-properties (point) (1- (point-max)))))))
1326
1327 (defvar debbugs-gnu-usertags-mode-map
1328 (let ((map (make-sparse-keymap)))
1329 (set-keymap-parent map tabulated-list-mode-map)
1330 (define-key map "\r" 'debbugs-gnu-select-usertag)
1331 (define-key map [mouse-1] 'debbugs-gnu-select-usertag)
1332 (define-key map [mouse-2] 'debbugs-gnu-select-usertag)
1333 map))
1334
1335 (define-derived-mode debbugs-gnu-usertags-mode tabulated-list-mode "Usertags"
1336 "Major mode for listing user tags.
1337
1338 All normal editing commands are switched off.
1339 \\<debbugs-gnu-usertags-mode-map>
1340
1341 The following commands are available:
1342
1343 \\{debbugs-gnu-usertags-mode-map}"
1344 (buffer-disable-undo)
1345 (setq truncate-lines t)
1346 (setq buffer-read-only t))
1347
1348 ;;;###autoload
1349 (defun debbugs-gnu-usertags (&rest users)
1350 "List all user tags for USERS, which is \(\"emacs\"\) by default."
1351 (interactive
1352 (if current-prefix-arg
1353 (completing-read-multiple
1354 "Package name(s) or email address: "
1355 (append debbugs-gnu-all-packages (list user-mail-address)) nil nil
1356 (mapconcat 'identity debbugs-gnu-default-packages ","))
1357 debbugs-gnu-default-packages))
1358
1359 (unwind-protect
1360 (let ((inhibit-read-only t)
1361 (debbugs-port "gnu.org")
1362 (buffer-name "*Emacs User Tags*")
1363 (user-tab-length
1364 (1+ (apply 'max (length "User") (mapcar 'length users)))))
1365
1366 ;; Initialize variables.
1367 (when (and (file-exists-p debbugs-gnu-persistency-file)
1368 (not debbugs-gnu-local-tags))
1369 (with-temp-buffer
1370 (insert-file-contents debbugs-gnu-persistency-file)
1371 (eval (read (current-buffer)))))
1372
1373 ;; Create buffer.
1374 (when (get-buffer buffer-name)
1375 (kill-buffer buffer-name))
1376 (switch-to-buffer (get-buffer-create buffer-name))
1377 (debbugs-gnu-usertags-mode)
1378 (setq tabulated-list-format `[("User" ,user-tab-length t)
1379 ("Tag" 10 t)])
1380 (setq tabulated-list-sort-key (cons "User" nil))
1381 ;(setq tabulated-list-printer 'debbugs-gnu-print-entry)
1382
1383 ;; Retrieve user tags.
1384 (dolist (user users)
1385 (dolist (tag (sort (debbugs-get-usertag :user user) 'string<))
1386 (add-to-list
1387 'tabulated-list-entries
1388 ;; `tabulated-list-id' is the parameter list for `debbugs-gnu'.
1389 `((("tagged") (,user) nil nil (,tag))
1390 ,(vector (propertize user 'mouse-face 'highlight)
1391 (propertize tag 'mouse-face 'highlight)))
1392 'append)))
1393
1394 ;; Add local tags.
1395 (when debbugs-gnu-local-tags
1396 (add-to-list
1397 'tabulated-list-entries
1398 `((("tagged"))
1399 ,(vector
1400 "" (propertize "(local tags)" 'mouse-face 'highlight)))))
1401
1402 ;; Show them.
1403 (tabulated-list-init-header)
1404 (tabulated-list-print)
1405
1406 (set-buffer-modified-p nil)
1407 (goto-char (point-min)))))
1408
1409 (defun debbugs-gnu-select-usertag ()
1410 "Select the user tag on the current line."
1411 (interactive)
1412 ;; We open the bug reports.
1413 (let ((args (get-text-property (line-beginning-position) 'tabulated-list-id)))
1414 (when args (apply 'debbugs-gnu args))))
1415
1416 ;;;###autoload
1417 (defun debbugs-gnu-bugs (&rest bugs)
1418 "List all BUGS, a list of bug numbers."
1419 (interactive
1420 (mapcar 'string-to-number
1421 (completing-read-multiple "Bug numbers: " nil 'natnump)))
1422 (dolist (elt bugs)
1423 (unless (natnump elt) (signal 'wrong-type-argument (list 'natnump elt))))
1424 (add-to-list 'debbugs-gnu-current-query (cons 'bugs bugs))
1425 ;; We do not suppress bugs requested explicitely.
1426 (setq debbugs-gnu-current-suppress nil)
1427 (debbugs-gnu nil))
1428
1429 (defvar debbugs-gnu-trunk-directory "~/src/emacs/trunk/"
1430 "The directory where the main source tree lives.")
1431
1432 (defvar debbugs-gnu-branch-directory "~/src/emacs/emacs-25/"
1433 "The directory where the previous source tree lives.")
1434
1435 (defun debbugs-gnu-apply-patch (&optional branch)
1436 "Apply the patch from the current message.
1437 If given a prefix, patch in the branch directory instead."
1438 (interactive "P")
1439 (add-hook 'emacs-lisp-mode-hook 'debbugs-gnu-lisp-mode)
1440 (add-hook 'diff-mode-hook 'debbugs-gnu-diff-mode)
1441 (add-hook 'change-log-mode-hook 'debbugs-gnu-change-mode)
1442 (let ((rej "/tmp/debbugs-gnu.rej")
1443 (output-buffer (get-buffer-create "*debbugs patch*"))
1444 (dir (if branch
1445 debbugs-gnu-branch-directory
1446 debbugs-gnu-trunk-directory))
1447 (patch-buffers nil))
1448 (when (file-exists-p rej)
1449 (delete-file rej))
1450 (with-current-buffer output-buffer
1451 (erase-buffer))
1452 (gnus-summary-select-article nil t)
1453 ;; The patches are either in MIME attachements or the main article
1454 ;; buffer. Determine which.
1455 (gnus-with-article-buffer
1456 (dolist (handle (mapcar 'cdr (gnus-article-mime-handles)))
1457 (when (string-match "diff\\|patch" (mm-handle-media-type handle))
1458 (push (cons (mm-handle-encoding handle)
1459 (mm-handle-buffer handle))
1460 patch-buffers))))
1461 (unless patch-buffers
1462 (gnus-summary-show-article 'raw)
1463 (article-decode-charset)
1464 (push (cons nil gnus-article-buffer) patch-buffers))
1465 (dolist (elem patch-buffers)
1466 (with-temp-buffer
1467 (insert-buffer-substring (cdr elem))
1468 (cond ((eq (car elem) 'base64)
1469 (base64-decode-region (point-min) (point-max)))
1470 ((eq (car elem) 'qp)
1471 (quoted-printable-decode-region (point-min) (point-max))))
1472 (debbugs-gnu-fix-patch dir)
1473 (call-process-region (point-min) (point-max)
1474 "patch" nil output-buffer nil
1475 "-r" rej "--no-backup-if-mismatch"
1476 "-l" "-f"
1477 "-d" (expand-file-name dir)
1478 "-p1")))
1479 (set-buffer output-buffer)
1480 (when (file-exists-p rej)
1481 (goto-char (point-max))
1482 (insert-file-contents-literally rej))
1483 (goto-char (point-max))
1484 (save-some-buffers t)
1485 (require 'compile)
1486 (mapc 'kill-process compilation-in-progress)
1487 (compile (format "cd %s; make -k" (expand-file-name "lisp" dir)))
1488 (vc-dir dir)
1489 (vc-dir-hide-up-to-date)
1490 (goto-char (point-min))
1491 (sit-for 1)
1492 (vc-diff)
1493 ;; All these commands are asynchronous, so just wait a bit. This
1494 ;; should be done properly a different way.
1495 (sit-for 2)
1496 ;; We've now done everything, so arrange the windows we need to see.
1497 (delete-other-windows)
1498 (switch-to-buffer output-buffer)
1499 (split-window)
1500 (split-window)
1501 (other-window 1)
1502 (switch-to-buffer "*compilation*")
1503 (goto-char (point-max))
1504 (other-window 1)
1505 (switch-to-buffer "*vc-diff*")
1506 (goto-char (point-min))))
1507
1508 (defun debbugs-gnu-fix-patch (dir)
1509 (setq dir (directory-file-name (expand-file-name dir)))
1510 (goto-char (point-min))
1511 (while (re-search-forward diff-file-header-re nil t)
1512 (goto-char (match-beginning 0))
1513 (let ((target-name (car (diff-hunk-file-names))))
1514 (when (and target-name
1515 (or (not (string-match "/" target-name))
1516 (and (string-match "^[ab]/" target-name)
1517 (not (file-exists-p
1518 (expand-file-name (substring target-name 2)
1519 dir))))
1520 (file-exists-p (expand-file-name target-name dir))))
1521 ;; We have a simple patch that refers to a file somewhere in the
1522 ;; tree. Find it.
1523 (when-let ((files (directory-files-recursively
1524 dir
1525 (concat "^" (regexp-quote
1526 (file-name-nondirectory target-name))
1527 "$"))))
1528 (when (re-search-forward (concat "^[+]+ "
1529 (regexp-quote target-name)
1530 "\\([ \t\n]\\)")
1531 nil t)
1532 (replace-match (concat "+++ a"
1533 (substring (car files) (length dir))
1534 (match-string 1))
1535 nil t)))))
1536 (forward-line 2)))
1537
1538 (defun debbugs-gnu-find-contributor (string)
1539 "Search through ChangeLogs to find contributors."
1540 (interactive "sContributor match: ")
1541 (let ((found 0)
1542 (match (concat "^[0-9].*" string)))
1543 (dolist (file (directory-files-recursively
1544 debbugs-gnu-trunk-directory "ChangeLog\\(.[0-9]+\\)?$"))
1545 (with-temp-buffer
1546 (when (file-exists-p file)
1547 (insert-file-contents file))
1548 (goto-char (point-min))
1549 (while (and (re-search-forward match nil t)
1550 (not (looking-at ".*tiny change")))
1551 (cl-incf found))))
1552 (message "%s is a contributor %d times" string found)
1553 found))
1554
1555 (defun debbugs-gnu-insert-changelog ()
1556 "Add a ChangeLog from a recently applied patch from a third party."
1557 (interactive)
1558 (let (from subject)
1559 (gnus-with-article-buffer
1560 (widen)
1561 (goto-char (point-min))
1562 (setq from (mail-extract-address-components (gnus-fetch-field "from"))
1563 subject (gnus-fetch-field "subject")))
1564 (let ((add-log-full-name (car from))
1565 (add-log-mailing-address (cadr from)))
1566 (add-change-log-entry-other-window)
1567 (let ((point (point)))
1568 (when (string-match "\\(bug#[0-9]+\\)" subject)
1569 (insert " (" (match-string 1 subject) ")."))
1570 (when (zerop (debbugs-gnu-find-contributor
1571 (let ((bits (split-string (car from))))
1572 (cond
1573 ((>= (length bits) 2)
1574 (format "%s.*%s" (car bits) (car (last bits))))
1575 ((= (length bits) 1)
1576 (car bits))
1577 ;; Fall back on the email address.
1578 (t
1579 (cadr from))))))
1580 (goto-char (point-max))
1581 (end-of-line)
1582 (insert " (tiny change"))
1583 (goto-char point)))))
1584
1585 (defvar debbugs-gnu-lisp-mode-map
1586 (let ((map (make-sparse-keymap)))
1587 (define-key map [(meta m)] 'debbugs-gnu-insert-changelog)
1588 map))
1589
1590 (define-minor-mode debbugs-gnu-lisp-mode
1591 "Minor mode for providing a debbugs interface in Lisp buffers.
1592 \\{debbugs-gnu-lisp-mode-map}"
1593 :lighter " Debbugs" :keymap debbugs-gnu-lisp-mode-map)
1594
1595 (defvar debbugs-gnu-diff-mode-map
1596 (let ((map (make-sparse-keymap)))
1597 (define-key map [(meta m)] 'debbugs-gnu-diff-select)
1598 map))
1599
1600 (define-minor-mode debbugs-gnu-diff-mode
1601 "Minor mode for providing a debbugs interface in diff buffers.
1602 \\{debbugs-gnu-diff-mode-map}"
1603 :lighter " Debbugs" :keymap debbugs-gnu-diff-mode-map)
1604
1605 (defun debbugs-gnu-diff-select ()
1606 "Select the diff under point."
1607 (interactive)
1608 (delete-other-windows)
1609 (diff-goto-source))
1610
1611 (defvar debbugs-gnu-change-mode-map
1612 (let ((map (make-sparse-keymap)))
1613 (define-key map [(meta m)] 'debbugs-gnu-change-checkin)
1614 map))
1615
1616 (define-minor-mode debbugs-gnu-change-mode
1617 "Minor mode for providing a debbugs interface in ChangeLog buffers.
1618 \\{debbugs-gnu-change-mode-map}"
1619 :lighter " Debbugs" :keymap debbugs-gnu-change-mode-map)
1620
1621 (defun debbugs-gnu-change-checkin ()
1622 "Prepare checking in the current changes."
1623 (interactive)
1624 (save-some-buffers t)
1625 (when (get-buffer "*vc-dir*")
1626 (kill-buffer (get-buffer "*vc-dir*")))
1627 (let ((trunk (expand-file-name debbugs-gnu-trunk-directory)))
1628 (if (equal (cl-subseq default-directory 0 (length trunk))
1629 trunk)
1630 (vc-dir debbugs-gnu-trunk-directory)
1631 (vc-dir debbugs-gnu-branch-directory)))
1632 (goto-char (point-min))
1633 (while (not (search-forward "edited" nil t))
1634 (sit-for 0.01))
1635 (beginning-of-line)
1636 (while (search-forward "edited" nil t)
1637 (vc-dir-mark)
1638 (beginning-of-line))
1639 (vc-diff nil)
1640 (vc-next-action nil)
1641 (log-edit-insert-changelog t)
1642 (delete-other-windows)
1643 (split-window)
1644 (other-window 1)
1645 (switch-to-buffer "*vc-diff*")
1646 (other-window 1))
1647
1648 (provide 'debbugs-gnu)
1649
1650 ;;; TODO:
1651
1652 ;; * Another random thought - is it possible to implement some local
1653 ;; cache, so only changed bugs are fetched? Glenn Morris.
1654
1655 ;;; debbugs-gnu.el ends here