]> code.delx.au - gnu-emacs-elpa/blob - packages/debbugs/debbugs-gnu.el
Add a command to save the bugs list
[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 two weeks.")
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 closed 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 bug 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))))
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 (&optional offline)
582 "Show bug reports.
583 If OFFLINE is non-nil, the query is not sent to the server. Bugs
584 are taken from the cache instead."
585 (let ((inhibit-read-only t)
586 (buffer-name "*Emacs Bugs*"))
587 ;; The tabulated mode sets several local variables. We must get
588 ;; rid of them.
589 (when (get-buffer buffer-name)
590 (kill-buffer buffer-name))
591 (switch-to-buffer (get-buffer-create buffer-name))
592 (debbugs-gnu-mode)
593
594 ;; Print bug reports.
595 (dolist (status
596 (let ((debbugs-cache-expiry (if offline nil debbugs-cache-expiry))
597 ids)
598 (apply 'debbugs-get-status
599 (if offline
600 (progn
601 (maphash (lambda (key _elem)
602 (push key ids))
603 debbugs-cache-data)
604 (sort ids '<))
605 (debbugs-gnu-get-bugs debbugs-gnu-local-query)))))
606 (let* ((id (cdr (assq 'id status)))
607 (words
608 (mapconcat
609 'identity
610 (cons (cdr (assq 'severity status))
611 (cdr (assq 'keywords status)))
612 ","))
613 (address (if (cdr (assq 'originator status))
614 (mail-header-parse-address
615 (decode-coding-string (cdr (assq 'originator status))
616 'utf-8))))
617 (owner (if (cdr (assq 'owner status))
618 (car (mail-header-parse-address
619 (decode-coding-string (cdr (assq 'owner status))
620 'utf-8)))))
621 (subject (if (cdr (assq 'subject status))
622 (decode-coding-string (cdr (assq 'subject status))
623 'utf-8)))
624 merged)
625 (unless (equal (cdr (assq 'pending status)) "pending")
626 (setq words (concat words "," (cdr (assq 'pending status)))))
627 (let ((packages (delete "emacs" (cdr (assq 'package status)))))
628 (when packages
629 (setq words (concat words "," (mapconcat 'identity packages ",")))))
630 (when (setq merged (cdr (assq 'mergedwith status)))
631 (setq words (format "%s,%s"
632 (if (numberp merged)
633 merged
634 (mapconcat 'number-to-string merged ","))
635 words)))
636 (when (or (not merged)
637 (not (let ((found nil))
638 (dolist (id (if (listp merged)
639 merged
640 (list merged)))
641 (dolist (entry tabulated-list-entries)
642 (when (equal id (cdr (assq 'id (car entry))))
643 (setq found t))))
644 found)))
645 (add-to-list
646 'tabulated-list-entries
647 (list
648 status
649 (vector
650 (propertize
651 (format "%5d" id)
652 'face
653 ;; Mark tagged bugs.
654 (if (memq id debbugs-gnu-local-tags)
655 'debbugs-gnu-tagged
656 'default))
657 (propertize
658 ;; Mark status and age.
659 (or words "")
660 'face
661 (cond
662 ((cdr (assq 'archived status))
663 'debbugs-gnu-archived)
664 ((equal (cdr (assq 'pending status)) "done")
665 'debbugs-gnu-done)
666 ((member "pending" (cdr (assq 'keywords status)))
667 'debbugs-gnu-pending)
668 ((= (cdr (assq 'date status))
669 (cdr (assq 'log_modified status)))
670 'debbugs-gnu-new)
671 ((< (- (float-time)
672 (cdr (assq 'log_modified status)))
673 (* 60 60 24 7 2))
674 'debbugs-gnu-handled)
675 (t
676 'debbugs-gnu-stale)))
677 (propertize
678 ;; Prefer the name over the address.
679 (or (cdr address)
680 (car address)
681 "")
682 'face
683 ;; Mark own submitted bugs.
684 (if (and (stringp (car address))
685 (string-equal (car address) user-mail-address))
686 'debbugs-gnu-tagged
687 'default))
688 (propertize
689 (or subject "")
690 'face
691 ;; Mark owned bugs.
692 (if (and (stringp owner)
693 (string-equal owner user-mail-address))
694 'debbugs-gnu-tagged
695 'default))))
696 'append))))
697
698 (tabulated-list-init-header)
699 (tabulated-list-print)
700
701 (set-buffer-modified-p nil)
702 (goto-char (point-min))))
703
704 (defun debbugs-gnu-print-entry (list-id cols)
705 "Insert a debbugs entry at point.
706 Used instead of `tabulated-list-print-entry'."
707 (let ((beg (point))
708 (pos 0)
709 (case-fold-search t)
710 (id (aref cols 0))
711 (id-length (nth 1 (aref tabulated-list-format 0)))
712 (state (aref cols 1))
713 (state-length (nth 1 (aref tabulated-list-format 1)))
714 (submitter (aref cols 2))
715 (submitter-length (nth 1 (aref tabulated-list-format 2)))
716 (title (aref cols 3))
717 (title-length (nth 1 (aref tabulated-list-format 3))))
718 (when (and
719 ;; We may have a narrowing in effect.
720 (or (not debbugs-gnu-limit)
721 (memq (cdr (assq 'id list-id)) debbugs-gnu-limit))
722 ;; Filter suppressed bugs.
723 (or (not debbugs-gnu-local-suppress)
724 (not (catch :suppress
725 (dolist (check debbugs-gnu-default-suppress-bugs)
726 (when
727 (string-match
728 (cdr check)
729 (or (cdr (assq (car check) list-id)) ""))
730 (throw :suppress t))))))
731 ;; Filter search list.
732 (not (catch :suppress
733 (dolist (check debbugs-gnu-local-filter)
734 (let ((val (cdr (assq (car check) list-id))))
735 (if (stringp (cdr check))
736 ;; Regular expression.
737 (when (not (string-match (cdr check) (or val "")))
738 (throw :suppress t))
739 ;; Time value.
740 (when (or (and (numberp (cadr check))
741 (< (cadr check) val))
742 (and (numberp (cddr check))
743 (> (cddr check) val)))
744 (throw :suppress t))))))))
745
746 ;; Insert id.
747 (indent-to (- id-length (length id)))
748 (insert id)
749 ;; Insert state.
750 (indent-to (setq pos (+ pos id-length 1)) 1)
751 (insert (if (> (length state) state-length)
752 (propertize (substring state 0 state-length)
753 'help-echo state)
754 state))
755 ;; Insert submitter.
756 (indent-to (setq pos (+ pos state-length 1)) 1)
757 (insert "[" (if (> (length submitter) (- submitter-length 2))
758 (propertize (substring submitter 0 (- submitter-length 2))
759 'help-echo submitter)
760 submitter))
761 (indent-to (+ pos (1- submitter-length)))
762 (insert "]")
763 ;; Insert title.
764 (indent-to (setq pos (+ pos submitter-length 1)) 1)
765 (insert (propertize title 'help-echo title))
766 ;; Add properties.
767 (add-text-properties
768 beg (point)
769 `(tabulated-list-id ,list-id mouse-face highlight))
770 (insert ?\n))))
771
772 (defun debbugs-gnu-menu-map-emacs-enabled ()
773 "Whether \"Show Release Blocking Bugs\" is enabled in the menu."
774 (or ;; No package discriminator has been used.
775 (not (assq 'package debbugs-gnu-local-query))
776 ;; Package "emacs" has been selected.
777 (member '(package . "emacs") debbugs-gnu-local-query)))
778
779 (defconst debbugs-gnu-bug-triage-file
780 (expand-file-name "../admin/notes/bug-triage" data-directory)
781 "The \"bug-triage\" file.")
782
783 (defun debbugs-gnu-menu-map-bug-triage-enabled ()
784 "Whether \"Describe Bug Triage Procedure\" is enabled in the menu."
785 (and (debbugs-gnu-menu-map-emacs-enabled)
786 (stringp debbugs-gnu-bug-triage-file)
787 (file-readable-p debbugs-gnu-bug-triage-file)))
788
789 (defun debbugs-gnu-view-bug-triage ()
790 "Show \"bug-triage\" file."
791 (interactive)
792 (view-file debbugs-gnu-bug-triage-file))
793
794 (defvar debbugs-gnu-mode-map
795 (let ((map (make-sparse-keymap))
796 (menu-map (make-sparse-keymap)))
797 (set-keymap-parent map tabulated-list-mode-map)
798 (define-key map "\r" 'debbugs-gnu-select-report)
799 (define-key map [mouse-1] 'debbugs-gnu-select-report)
800 (define-key map [mouse-2] 'debbugs-gnu-select-report)
801 (define-key map "g" 'debbugs-gnu-rescan)
802 (define-key map "R" 'debbugs-gnu-show-all-blocking-reports)
803 (define-key map "C" 'debbugs-gnu-send-control-message)
804
805 (define-key map "s" 'debbugs-gnu-toggle-sort)
806 (define-key map "t" 'debbugs-gnu-toggle-tag)
807 (define-key map "x" 'debbugs-gnu-toggle-suppress)
808 (define-key map "/" 'debbugs-gnu-narrow-to-status)
809 (define-key map "w" 'debbugs-gnu-widen)
810
811 (define-key map "b" 'debbugs-gnu-show-blocked-by-reports)
812 (define-key map "B" 'debbugs-gnu-show-blocking-reports)
813 (define-key map "d" 'debbugs-gnu-display-status)
814
815 (define-key map [menu-bar debbugs] (cons "Debbugs" menu-map))
816 (define-key menu-map [debbugs-gnu-select-report]
817 '(menu-item "Show Reports" debbugs-gnu-select-report
818 :help "Show all reports belonging to this bug"))
819 (define-key-after menu-map [debbugs-gnu-rescan]
820 '(menu-item "Refresh Bugs" debbugs-gnu-rescan
821 :help "Refresh bug list")
822 'debbugs-gnu-select-report)
823 (define-key-after menu-map [debbugs-gnu-show-all-blocking-reports]
824 '(menu-item "Show Release Blocking Bugs"
825 debbugs-gnu-show-all-blocking-reports
826 :enable (debbugs-gnu-menu-map-emacs-enabled)
827 :help "Show all bugs blocking next Emacs release")
828 'debbugs-gnu-rescan)
829 (define-key-after menu-map [debbugs-gnu-send-control-message]
830 '(menu-item "Send Control Message"
831 debbugs-gnu-send-control-message
832 :help "Send control message to debbugs.gnu.org")
833 'debbugs-gnu-show-all-blocking-reports)
834
835 (define-key-after menu-map [debbugs-gnu-separator1]
836 '(menu-item "--") 'debbugs-gnu-send-control-message)
837 (define-key-after menu-map [debbugs-gnu-search]
838 '(menu-item "Search Bugs" debbugs-gnu-search
839 :help "Search bugs on debbugs.gnu.org")
840 'debbugs-gnu-separator1)
841 (define-key-after menu-map [debbugs-gnu]
842 '(menu-item "Retrieve Bugs" debbugs-gnu
843 :help "Retrieve bugs from debbugs.gnu.org")
844 'debbugs-gnu-search)
845 (define-key-after menu-map [debbugs-gnu-bugs]
846 '(menu-item "Retrieve Bugs by Number" debbugs-gnu-bugs
847 :help "Retrieve selected bugs from debbugs.gnu.org")
848 'debbugs-gnu)
849
850 (define-key-after menu-map [debbugs-gnu-separator2]
851 '(menu-item "--") 'debbugs-gnu-bugs)
852 (define-key-after menu-map [debbugs-gnu-view-bug-triage]
853 '(menu-item "Describe Bug Triage Procedure"
854 debbugs-gnu-view-bug-triage
855 :enable (debbugs-gnu-menu-map-bug-triage-enabled)
856 :help "Show procedure of triaging bugs")
857 'debbugs-gnu-separator2)
858 map))
859
860 (defun debbugs-gnu-rescan ()
861 "Rescan the current set of bug reports."
862 (interactive)
863 (let ((id (debbugs-gnu-current-id))
864 (debbugs-gnu-current-query debbugs-gnu-local-query)
865 (debbugs-gnu-current-filter debbugs-gnu-local-filter)
866 (debbugs-gnu-current-suppress debbugs-gnu-local-suppress)
867 (debbugs-cache-expiry (if current-prefix-arg t debbugs-cache-expiry)))
868 (debbugs-gnu-show-reports)
869 (when id
870 (debbugs-gnu-goto id))))
871
872 (define-derived-mode debbugs-gnu-mode tabulated-list-mode "Debbugs"
873 "Major mode for listing bug reports.
874
875 All normal editing commands are switched off.
876 \\<debbugs-gnu-mode-map>
877
878 The following commands are available:
879
880 \\{debbugs-gnu-mode-map}"
881 (set (make-local-variable 'debbugs-gnu-sort-state) 'number)
882 (set (make-local-variable 'debbugs-gnu-limit) nil)
883 (set (make-local-variable 'debbugs-gnu-local-query)
884 debbugs-gnu-current-query)
885 (set (make-local-variable 'debbugs-gnu-local-filter)
886 debbugs-gnu-current-filter)
887 (set (make-local-variable 'debbugs-gnu-local-suppress)
888 debbugs-gnu-current-suppress)
889 (setq tabulated-list-format [("Id" 5 debbugs-gnu-sort-id)
890 ("State" 20 debbugs-gnu-sort-state)
891 ("Submitter" 25 t)
892 ("Title" 10 debbugs-gnu-sort-title)])
893 (setq tabulated-list-sort-key (cons "Id" nil))
894 (setq tabulated-list-printer 'debbugs-gnu-print-entry)
895 (buffer-disable-undo)
896 (setq truncate-lines t)
897 (setq buffer-read-only t))
898
899 (defun debbugs-gnu-sort-id (s1 s2)
900 (> (cdr (assq 'id (car s1)))
901 (cdr (assq 'id (car s2)))))
902
903 (defconst debbugs-gnu-state-preference
904 '((debbugs-gnu-new . 1)
905 (debbugs-gnu-stale . 2)
906 (debbugs-gnu-handled . 3)
907 (debbugs-gnu-done . 4)
908 (debbugs-gnu-pending . 5)))
909
910 (defun debbugs-gnu-get-state-preference (face-string)
911 (or (cdr (assq (get-text-property 0 'face face-string)
912 debbugs-gnu-state-preference))
913 10))
914
915 (defconst debbugs-gnu-severity-preference
916 '(("serious" . 1)
917 ("important" . 2)
918 ("normal" . 3)
919 ("minor" . 4)
920 ("wishlist" . 5)))
921
922 (defun debbugs-gnu-get-severity-preference (state)
923 (or (cdr (assoc (cdr (assq 'severity state))
924 debbugs-gnu-severity-preference))
925 10))
926
927 (defun debbugs-gnu-sort-state (s1 s2)
928 (let ((id1 (cdr (assq 'id (car s1))))
929 (age1 (debbugs-gnu-get-state-preference (aref (nth 1 s1) 1)))
930 (id2 (cdr (assq 'id (car s2))))
931 (age2 (debbugs-gnu-get-state-preference (aref (nth 1 s2) 1))))
932 (cond
933 ;; Tagged bugs go to the end.
934 ((and (not (memq id1 debbugs-gnu-local-tags))
935 (memq id2 debbugs-gnu-local-tags))
936 t)
937 ((and (memq id1 debbugs-gnu-local-tags)
938 (not (memq id2 debbugs-gnu-local-tags)))
939 nil)
940 ;; Then, we check the age of the bugs.
941 ((< age1 age2)
942 t)
943 ((> age1 age2)
944 nil)
945 ;; If they have the same age, we check for severity.
946 ((< (debbugs-gnu-get-severity-preference (car s1))
947 (debbugs-gnu-get-severity-preference (car s2)))
948 t)
949 (t nil))))
950
951 (defun debbugs-gnu-sort-title (s1 s2)
952 (let ((owner (if (cdr (assq 'owner (car s1)))
953 (car (mail-header-parse-address
954 (decode-coding-string (cdr (assq 'owner (car s1)))
955 'utf-8))))))
956 (and (stringp owner)
957 (string-equal owner user-mail-address))))
958
959 (defun debbugs-gnu-toggle-sort ()
960 "Toggle sorting by age and by state."
961 (interactive)
962 (if (eq debbugs-gnu-sort-state 'number)
963 (progn
964 (setq debbugs-gnu-sort-state 'state)
965 (setq tabulated-list-sort-key (cons "Id" nil)))
966 (setq debbugs-gnu-sort-state 'number)
967 (setq tabulated-list-sort-key (cons "State" nil)))
968 (tabulated-list-init-header)
969 (tabulated-list-print))
970
971 (defun debbugs-gnu-widen ()
972 "Display all the currently selected bug reports."
973 (interactive)
974 (let ((id (debbugs-gnu-current-id t))
975 (inhibit-read-only t))
976 (setq debbugs-gnu-limit nil)
977 (tabulated-list-init-header)
978 (tabulated-list-print)
979 (when id
980 (debbugs-gnu-goto id))))
981
982 (defun debbugs-gnu-show-blocked-by-reports ()
983 "Display all bug reports this report is blocked by."
984 (interactive)
985 (let ((id (debbugs-gnu-current-id))
986 (status (debbugs-gnu-current-status)))
987 (if (null (cdr (assq 'blockedby status)))
988 (message "Bug %d is not blocked by any other bug" id)
989 (apply 'debbugs-gnu-bugs (cdr (assq 'blockedby status))))))
990
991 (defun debbugs-gnu-show-blocking-reports ()
992 "Display all bug reports this report is blocking."
993 (interactive)
994 (let ((id (debbugs-gnu-current-id))
995 (status (debbugs-gnu-current-status)))
996 (if (null (cdr (assq 'blocks status)))
997 (message "Bug %d is not blocking any other bug" id)
998 (apply 'debbugs-gnu-bugs (cdr (assq 'blocks status))))))
999
1000 (defun debbugs-gnu-show-all-blocking-reports ()
1001 "Narrow the display to just the reports that are blocking a release."
1002 (interactive)
1003 (let ((blockers (cdr (assq 'blockedby
1004 (car (debbugs-get-status
1005 debbugs-gnu-blocking-report)))))
1006 (id (debbugs-gnu-current-id t))
1007 (inhibit-read-only t)
1008 status)
1009 (setq debbugs-gnu-limit nil)
1010 (goto-char (point-min))
1011 (while (not (eobp))
1012 (setq status (debbugs-gnu-current-status))
1013 (if (not (memq (cdr (assq 'id status)) blockers))
1014 (delete-region (point) (progn (forward-line 1) (point)))
1015 (push (cdr (assq 'id status)) debbugs-gnu-limit)
1016 (forward-line 1)))
1017 (when id
1018 (debbugs-gnu-goto id))))
1019
1020 (defun debbugs-gnu-narrow-to-status (string &optional status-only)
1021 "Only display the bugs matching STRING.
1022 If STATUS-ONLY (the prefix), ignore matches in the From and
1023 Subject fields."
1024 (interactive "sNarrow to: \nP")
1025 (let ((id (debbugs-gnu-current-id t))
1026 (inhibit-read-only t)
1027 status)
1028 (setq debbugs-gnu-limit nil)
1029 (if (equal string "")
1030 (debbugs-gnu-toggle-suppress)
1031 (goto-char (point-min))
1032 (while (not (eobp))
1033 (setq status (debbugs-gnu-current-status))
1034 (if (and (not (member string (assq 'keywords status)))
1035 (not (equal string (cdr (assq 'severity status))))
1036 (or status-only
1037 (not (string-match
1038 string (cdr (assq 'originator status)))))
1039 (or status-only
1040 (not (string-match string (cdr (assq 'subject status))))))
1041 (delete-region (point) (progn (forward-line 1) (point)))
1042 (push (cdr (assq 'id status)) debbugs-gnu-limit)
1043 (forward-line 1)))
1044 (when id
1045 (debbugs-gnu-goto id)))))
1046
1047 (defun debbugs-gnu-goto (id)
1048 "Go to the line displaying bug ID."
1049 (goto-char (point-min))
1050 (while (and (not (eobp))
1051 (not (equal (debbugs-gnu-current-id t) id)))
1052 (forward-line 1)))
1053
1054 (defun debbugs-gnu-toggle-tag ()
1055 "Toggle the local tag of the report in the current line.
1056 If a report is tagged locally, it is presumed to be of little
1057 interest to you."
1058 (interactive)
1059 (save-excursion
1060 (beginning-of-line)
1061 (let ((inhibit-read-only t)
1062 (id (debbugs-gnu-current-id)))
1063 (if (memq id debbugs-gnu-local-tags)
1064 (progn
1065 (setq debbugs-gnu-local-tags (delq id debbugs-gnu-local-tags))
1066 (put-text-property (point) (+ (point) 5) 'face 'default))
1067 (add-to-list 'debbugs-gnu-local-tags id)
1068 (put-text-property
1069 (+ (point) (- 5 (length (number-to-string id)))) (+ (point) 5)
1070 'face 'debbugs-gnu-tagged))
1071 (debbugs-gnu--update-tag-face id)))
1072 (debbugs-gnu-dump-persistency-file))
1073
1074 (defun debbugs-gnu--update-tag-face (id)
1075 (dolist (entry tabulated-list-entries)
1076 (when (equal (cdr (assq 'id (car entry))) id)
1077 (aset (cadr entry) 0
1078 (propertize
1079 (format "%5d" id)
1080 'face
1081 ;; Mark tagged bugs.
1082 (if (memq id debbugs-gnu-local-tags)
1083 'debbugs-gnu-tagged
1084 'default))))))
1085
1086 (defun debbugs-gnu-toggle-suppress ()
1087 "Suppress bugs marked in `debbugs-gnu-suppress-bugs'."
1088 (interactive)
1089 (setq debbugs-gnu-local-suppress (not debbugs-gnu-local-suppress))
1090 (tabulated-list-init-header)
1091 (tabulated-list-print))
1092
1093 (defvar debbugs-gnu-bug-number nil)
1094 (defvar debbugs-gnu-subject nil)
1095
1096 (defun debbugs-gnu-current-id (&optional noerror)
1097 (or (cdr (assq 'id (debbugs-gnu-current-status)))
1098 (and (not noerror)
1099 (error "No bug on the current line"))))
1100
1101 (defun debbugs-gnu-current-status ()
1102 (get-text-property (line-beginning-position) 'tabulated-list-id))
1103
1104 (defun debbugs-gnu-display-status (query filter status)
1105 "Display the query, filter and status of the report on the current line."
1106 (interactive (list debbugs-gnu-local-query
1107 debbugs-gnu-local-filter
1108 (debbugs-gnu-current-status)))
1109 (switch-to-buffer "*Bug Status*")
1110 (let ((inhibit-read-only t))
1111 (erase-buffer)
1112 (when query
1113 (insert ";; Query\n")
1114 (pp query (current-buffer))
1115 (insert "\n"))
1116 (when filter
1117 (insert ";; Filter\n")
1118 (pp filter (current-buffer))
1119 (insert "\n"))
1120 (when status
1121 (insert ";; Status\n")
1122 (pp status (current-buffer)))
1123 (goto-char (point-min)))
1124 (set-buffer-modified-p nil)
1125 (special-mode))
1126
1127 (defun debbugs-read-emacs-bug-with-rmail (id status merged)
1128 "Read email exchange for debbugs bug ID.
1129 STATUS is the bug's status list.
1130 MERGED is the list of bugs merged with this one."
1131 (let* ((mbox-dir (make-temp-file "debbugs" t))
1132 (mbox-fname (format "%s/bug_%d.mbox" mbox-dir id)))
1133 (debbugs-get-mbox id 'mboxmaint mbox-fname)
1134 (rmail mbox-fname)
1135 ;; Download messages of all the merged bug reports and append them
1136 ;; to the mailbox of the requested bug.
1137 (when merged
1138 (dolist (bugno merged)
1139 (let ((fn (make-temp-file "url")))
1140 (debbugs-get-mbox bugno 'mboxmaint fn)
1141 (rmail-get-new-mail fn)
1142 (delete-file fn)
1143 ;; Remove the 'unseen' attribute from all the messages we've
1144 ;; just read, so that all of them appear in the summary with
1145 ;; the same face.
1146 (while (< rmail-current-message rmail-total-messages)
1147 (rmail-show-message (1+ rmail-current-message))))))
1148 (set (make-local-variable 'debbugs-gnu-bug-number) id)
1149 (set (make-local-variable 'debbugs-gnu-subject)
1150 (format "Re: bug#%d: %s" id (cdr (assq 'subject status))))
1151 (rmail-summary)
1152 (define-key rmail-summary-mode-map "C" 'debbugs-gnu-send-control-message)
1153 (set-window-text-height nil 10)
1154 (other-window 1)
1155 (define-key rmail-mode-map "C" 'debbugs-gnu-send-control-message)
1156 (rmail-show-message 1)))
1157
1158 (defun debbugs-read-emacs-bug-with-gnus (id status merged)
1159 "Read email exchange for debbugs bug ID.
1160 STATUS is the bug's status list.
1161 MERGED is the list of bugs merged with this one."
1162 (require 'gnus-dup)
1163 (setq gnus-suppress-duplicates t
1164 gnus-save-duplicate-list t)
1165 ;; Use Gnus.
1166 (gnus-read-ephemeral-emacs-bug-group
1167 (cons id (if (listp merged) merged (list merged)))
1168 (cons (current-buffer)
1169 (current-window-configuration)))
1170 (with-current-buffer (window-buffer (selected-window))
1171 (set (make-local-variable 'debbugs-gnu-bug-number) id)
1172 (set (make-local-variable 'debbugs-gnu-subject)
1173 (format "Re: bug#%d: %s" id (cdr (assq 'subject status))))
1174 (debbugs-gnu-summary-mode 1)))
1175
1176 (defun debbugs-gnu-select-report ()
1177 "Select the report on the current line."
1178 (interactive)
1179 ;; We open the report messages.
1180 (let* ((status (debbugs-gnu-current-status))
1181 (id (cdr (assq 'id status)))
1182 (merged (cdr (assq 'mergedwith status))))
1183 (setq merged (if (listp merged) merged (list merged)))
1184 (cond
1185 ((not id)
1186 (message "No bug report on the current line"))
1187 ((eq debbugs-gnu-mail-backend 'rmail)
1188 (debbugs-read-emacs-bug-with-rmail id status merged))
1189 ((eq debbugs-gnu-mail-backend 'gnus)
1190 (debbugs-read-emacs-bug-with-gnus id status merged))
1191 (t (error "No valid mail backend specified")))))
1192
1193 (defvar debbugs-gnu-summary-mode-map
1194 (let ((map (make-sparse-keymap)))
1195 (define-key map "C" 'debbugs-gnu-send-control-message)
1196 (define-key map [(meta m)] 'debbugs-gnu-apply-patch)
1197 map))
1198
1199 (define-minor-mode debbugs-gnu-summary-mode
1200 "Minor mode for providing a debbugs interface in Gnus summary buffers.
1201
1202 \\{debbugs-gnu-summary-mode-map}"
1203 :lighter " Debbugs" :keymap debbugs-gnu-summary-mode-map
1204 (set (make-local-variable 'gnus-posting-styles)
1205 `((".*"
1206 (eval
1207 (when (buffer-live-p gnus-article-copy)
1208 (with-current-buffer gnus-article-copy
1209 (set (make-local-variable 'message-prune-recipient-rules)
1210 '((".*@debbugs.*" "emacs-pretest-bug")
1211 (".*@debbugs.*" "bug-gnu-emacs")
1212 ("[0-9]+@debbugs.*" "submit@debbugs.gnu.org")
1213 ("[0-9]+@debbugs.*" "quiet@debbugs.gnu.org")))
1214 (set (make-local-variable 'message-alter-recipients-function)
1215 (lambda (address)
1216 (if (string-match "\\([0-9]+\\)@donarmstrong"
1217 (car address))
1218 (let ((new (format "%s@debbugs.gnu.org"
1219 (match-string 1 (car address)))))
1220 (cons new new))
1221 address)))
1222 ;; `gnus-posting-styles' is eval'ed after
1223 ;; `message-simplify-subject'. So we cannot use m-s-s.
1224 (setq subject ,debbugs-gnu-subject))))))))
1225
1226 (defun debbugs-gnu-guess-current-id ()
1227 "Guess the ID based on \"#23\"."
1228 (save-excursion
1229 (beginning-of-line)
1230 (and
1231 (or (re-search-forward "#\\([0-9]+\\)" (line-end-position) t)
1232 (progn
1233 (goto-char (point-min))
1234 (re-search-forward "#\\([0-9]+\\)" nil t)))
1235 (string-to-number (match-string 1)))))
1236
1237 (defvar debbugs-gnu-send-mail-function nil
1238 "A function to send control messages from debbugs.")
1239
1240 (defun debbugs-gnu-send-control-message (message &optional reverse)
1241 "Send a control message for the current bug report.
1242 You can set the severity or add a tag, or close the report. If
1243 you use the special \"done\" MESSAGE, the report will be marked as
1244 fixed, and then closed.
1245
1246 If given a prefix, and given a tag to set, the tag will be
1247 removed instead."
1248 (interactive
1249 (list (completing-read
1250 "Control message: "
1251 '("serious" "important" "normal" "minor" "wishlist"
1252 "done" "donenotabug" "donewontfix" "doneunreproducible"
1253 "unarchive" "unmerge" "reopen" "close"
1254 "merge" "forcemerge"
1255 "block" "unblock"
1256 "owner" "noowner"
1257 "invalid"
1258 "reassign"
1259 "retitle"
1260 "patch" "wontfix" "moreinfo" "unreproducible" "fixed" "notabug"
1261 "pending" "help" "security" "confirmed"
1262 "usertag")
1263 nil t)
1264 current-prefix-arg))
1265 (let* ((id (or debbugs-gnu-bug-number ; Set on group entry.
1266 (debbugs-gnu-guess-current-id)
1267 (debbugs-gnu-current-id)))
1268 (version
1269 (when (member message '("close" "done"))
1270 (read-string
1271 "Version: "
1272 (cond
1273 ;; Emacs development versions.
1274 ((string-match
1275 "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\." emacs-version)
1276 (format "%s.%d"
1277 (match-string 1 emacs-version)
1278 (1+ (string-to-number (match-string 2 emacs-version)))))
1279 ;; Emacs release versions.
1280 ((string-match
1281 "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" emacs-version)
1282 (format "%s.%s"
1283 (match-string 1 emacs-version)
1284 (match-string 2 emacs-version)))
1285 (t emacs-version)))))
1286 (status (debbugs-gnu-current-status)))
1287 (with-temp-buffer
1288 (insert "To: control@debbugs.gnu.org\n"
1289 "From: " (message-make-from) "\n"
1290 (format "Subject: control message for bug #%d\n" id)
1291 mail-header-separator
1292 "\n"
1293 (cond
1294 ((member message '("unarchive" "unmerge" "reopen" "noowner"))
1295 (format "%s %d\n" message id))
1296 ((member message '("merge" "forcemerge"))
1297 (format "%s %d %s\n" message id
1298 (read-string "Merge with bug #: ")))
1299 ((member message '("block" "unblock"))
1300 (format
1301 "%s %d by %s\n" message id
1302 (mapconcat
1303 'identity
1304 (completing-read-multiple
1305 (format "%s with bug(s) #: " (capitalize message))
1306 (if (equal message "unblock")
1307 (mapcar 'number-to-string
1308 (cdr (assq 'blockedby status))))
1309 nil (and (equal message "unblock") status))
1310 " ")))
1311 ((equal message "owner")
1312 (format "owner %d !\n" id))
1313 ((equal message "retitle")
1314 (format "retitle %d %s\n" id (read-string "New title: ")))
1315 ((equal message "reassign")
1316 (format "reassign %d %s\n" id (read-string "Package(s): ")))
1317 ((equal message "close")
1318 (format "close %d %s\n" id version))
1319 ((equal message "done")
1320 (format "tags %d fixed\nclose %d %s\n" id id version))
1321 ((member message '("donenotabug" "donewontfix"
1322 "doneunreproducible"))
1323 (format "tags %d %s\nclose %d\n" id (substring message 4) id))
1324 ((member message '("serious" "important" "normal"
1325 "minor" "wishlist"))
1326 (format "severity %d %s\n" id message))
1327 ((equal message "invalid")
1328 (format "tags %d notabug\ntags %d wontfix\nclose %d\n"
1329 id id id))
1330 ((equal message "usertag")
1331 (format "user %s\nusertag %d %s\n"
1332 (completing-read
1333 "Package name or email address: "
1334 (append
1335 debbugs-gnu-all-packages (list user-mail-address))
1336 nil nil (car debbugs-gnu-default-packages))
1337 id (read-string "User tag: ")))
1338 (t
1339 (format "tags %d%s %s\n"
1340 id (if reverse " -" "")
1341 message))))
1342 (funcall (or debbugs-gnu-send-mail-function send-mail-function))
1343 (remhash id debbugs-cache-data)
1344 (message-goto-body)
1345 (message "Control message sent:\n%s"
1346 (buffer-substring-no-properties (point) (1- (point-max)))))))
1347
1348 (defvar debbugs-gnu-usertags-mode-map
1349 (let ((map (make-sparse-keymap)))
1350 (set-keymap-parent map tabulated-list-mode-map)
1351 (define-key map "\r" 'debbugs-gnu-select-usertag)
1352 (define-key map [mouse-1] 'debbugs-gnu-select-usertag)
1353 (define-key map [mouse-2] 'debbugs-gnu-select-usertag)
1354 map))
1355
1356 (define-derived-mode debbugs-gnu-usertags-mode tabulated-list-mode "Usertags"
1357 "Major mode for listing user tags.
1358
1359 All normal editing commands are switched off.
1360 \\<debbugs-gnu-usertags-mode-map>
1361
1362 The following commands are available:
1363
1364 \\{debbugs-gnu-usertags-mode-map}"
1365 (buffer-disable-undo)
1366 (setq truncate-lines t)
1367 (setq buffer-read-only t))
1368
1369 ;;;###autoload
1370 (defun debbugs-gnu-usertags (&rest users)
1371 "List all user tags for USERS, which is \(\"emacs\"\) by default."
1372 (interactive
1373 (if current-prefix-arg
1374 (completing-read-multiple
1375 "Package name(s) or email address: "
1376 (append debbugs-gnu-all-packages (list user-mail-address)) nil nil
1377 (mapconcat 'identity debbugs-gnu-default-packages ","))
1378 debbugs-gnu-default-packages))
1379
1380 (unwind-protect
1381 (let ((inhibit-read-only t)
1382 (debbugs-port "gnu.org")
1383 (buffer-name "*Emacs User Tags*")
1384 (user-tab-length
1385 (1+ (apply 'max (length "User") (mapcar 'length users)))))
1386
1387 ;; Initialize variables.
1388 (when (and (file-exists-p debbugs-gnu-persistency-file)
1389 (not debbugs-gnu-local-tags))
1390 (with-temp-buffer
1391 (insert-file-contents debbugs-gnu-persistency-file)
1392 (eval (read (current-buffer)))))
1393
1394 ;; Create buffer.
1395 (when (get-buffer buffer-name)
1396 (kill-buffer buffer-name))
1397 (switch-to-buffer (get-buffer-create buffer-name))
1398 (debbugs-gnu-usertags-mode)
1399 (setq tabulated-list-format `[("User" ,user-tab-length t)
1400 ("Tag" 10 t)])
1401 (setq tabulated-list-sort-key (cons "User" nil))
1402 ;(setq tabulated-list-printer 'debbugs-gnu-print-entry)
1403
1404 ;; Retrieve user tags.
1405 (dolist (user users)
1406 (dolist (tag (sort (debbugs-get-usertag :user user) 'string<))
1407 (add-to-list
1408 'tabulated-list-entries
1409 ;; `tabulated-list-id' is the parameter list for `debbugs-gnu'.
1410 `((("tagged") (,user) nil nil (,tag))
1411 ,(vector (propertize user 'mouse-face 'highlight)
1412 (propertize tag 'mouse-face 'highlight)))
1413 'append)))
1414
1415 ;; Add local tags.
1416 (when debbugs-gnu-local-tags
1417 (add-to-list
1418 'tabulated-list-entries
1419 `((("tagged"))
1420 ,(vector
1421 "" (propertize "(local tags)" 'mouse-face 'highlight)))))
1422
1423 ;; Show them.
1424 (tabulated-list-init-header)
1425 (tabulated-list-print)
1426
1427 (set-buffer-modified-p nil)
1428 (goto-char (point-min)))))
1429
1430 (defun debbugs-gnu-select-usertag ()
1431 "Select the user tag on the current line."
1432 (interactive)
1433 ;; We open the bug reports.
1434 (let ((args (get-text-property (line-beginning-position) 'tabulated-list-id)))
1435 (when args (apply 'debbugs-gnu args))))
1436
1437 ;;;###autoload
1438 (defun debbugs-gnu-bugs (&rest bugs)
1439 "List all BUGS, a list of bug numbers."
1440 (interactive
1441 (mapcar 'string-to-number
1442 (completing-read-multiple "Bug numbers: " nil 'natnump)))
1443 (dolist (elt bugs)
1444 (unless (natnump elt) (signal 'wrong-type-argument (list 'natnump elt))))
1445 (add-to-list 'debbugs-gnu-current-query (cons 'bugs bugs))
1446 ;; We do not suppress bugs requested explicitely.
1447 (setq debbugs-gnu-current-suppress nil)
1448 (debbugs-gnu nil))
1449
1450 (defvar debbugs-gnu-trunk-directory "~/src/emacs/trunk/"
1451 "The directory where the main source tree lives.")
1452
1453 (defvar debbugs-gnu-branch-directory "~/src/emacs/emacs-25/"
1454 "The directory where the previous source tree lives.")
1455
1456 (defun debbugs-gnu-apply-patch (&optional branch)
1457 "Apply the patch from the current message.
1458 If given a prefix, patch in the branch directory instead."
1459 (interactive "P")
1460 (add-hook 'emacs-lisp-mode-hook 'debbugs-gnu-lisp-mode)
1461 (add-hook 'diff-mode-hook 'debbugs-gnu-diff-mode)
1462 (add-hook 'change-log-mode-hook 'debbugs-gnu-change-mode)
1463 (let ((rej "/tmp/debbugs-gnu.rej")
1464 (output-buffer (get-buffer-create "*debbugs patch*"))
1465 (dir (if branch
1466 debbugs-gnu-branch-directory
1467 debbugs-gnu-trunk-directory))
1468 (patch-buffers nil))
1469 (when (file-exists-p rej)
1470 (delete-file rej))
1471 (with-current-buffer output-buffer
1472 (erase-buffer))
1473 (gnus-summary-select-article nil t)
1474 ;; The patches are either in MIME attachements or the main article
1475 ;; buffer. Determine which.
1476 (gnus-with-article-buffer
1477 (dolist (handle (mapcar 'cdr (gnus-article-mime-handles)))
1478 (when (string-match "diff\\|patch\\|plain" (mm-handle-media-type handle))
1479 (push (cons (mm-handle-encoding handle)
1480 (mm-handle-buffer handle))
1481 patch-buffers))))
1482 (unless patch-buffers
1483 (gnus-summary-show-article 'raw)
1484 (article-decode-charset)
1485 (push (cons nil gnus-article-buffer) patch-buffers))
1486 (dolist (elem patch-buffers)
1487 (with-current-buffer (generate-new-buffer "*debbugs input patch*")
1488 (insert-buffer-substring (cdr elem))
1489 (cond ((eq (car elem) 'base64)
1490 (base64-decode-region (point-min) (point-max)))
1491 ((eq (car elem) 'quoted-printable)
1492 (quoted-printable-decode-region (point-min) (point-max))))
1493 (debbugs-gnu-fix-patch dir)
1494 (call-process-region (point-min) (point-max)
1495 "patch" nil output-buffer nil
1496 "-r" rej "--no-backup-if-mismatch"
1497 "-l" "-f"
1498 "-d" (expand-file-name dir)
1499 "-p1")))
1500 (set-buffer output-buffer)
1501 (when (file-exists-p rej)
1502 (goto-char (point-max))
1503 (insert-file-contents-literally rej))
1504 (goto-char (point-max))
1505 (save-some-buffers t)
1506 (require 'compile)
1507 (mapc 'kill-process compilation-in-progress)
1508 (compile (format "cd %s; make -k" (expand-file-name "lisp" dir)))
1509 (vc-dir dir)
1510 (vc-dir-hide-up-to-date)
1511 (goto-char (point-min))
1512 (sit-for 1)
1513 (vc-diff)
1514 ;; All these commands are asynchronous, so just wait a bit. This
1515 ;; should be done properly a different way.
1516 (sit-for 2)
1517 ;; We've now done everything, so arrange the windows we need to see.
1518 (delete-other-windows)
1519 (switch-to-buffer output-buffer)
1520 (split-window)
1521 (split-window)
1522 (other-window 1)
1523 (switch-to-buffer "*compilation*")
1524 (goto-char (point-max))
1525 (other-window 1)
1526 (switch-to-buffer "*vc-diff*")
1527 (goto-char (point-min))))
1528
1529 (defun debbugs-gnu-fix-patch (dir)
1530 (setq dir (directory-file-name (expand-file-name dir)))
1531 (goto-char (point-min))
1532 (while (re-search-forward diff-file-header-re nil t)
1533 (goto-char (match-beginning 0))
1534 (let ((target-name (car (diff-hunk-file-names))))
1535 (when (and target-name
1536 (or (not (string-match "/" target-name))
1537 (and (string-match "^[ab]/" target-name)
1538 (not (file-exists-p
1539 (expand-file-name (substring target-name 2)
1540 dir))))
1541 (file-exists-p (expand-file-name target-name dir))))
1542 ;; We have a simple patch that refers to a file somewhere in the
1543 ;; tree. Find it.
1544 (when-let ((files (directory-files-recursively
1545 dir
1546 (concat "^" (regexp-quote
1547 (file-name-nondirectory target-name))
1548 "$"))))
1549 (when (re-search-forward (concat "^[+]+ "
1550 (regexp-quote target-name)
1551 "\\([ \t\n]\\)")
1552 nil t)
1553 (replace-match (concat "+++ a"
1554 (substring (car files) (length dir))
1555 (match-string 1))
1556 nil t)))))
1557 (forward-line 2)))
1558
1559 (defun debbugs-gnu-find-contributor (string)
1560 "Search through ChangeLogs to find contributors."
1561 (interactive "sContributor match: ")
1562 (let ((found 0)
1563 (match (concat "^[0-9].*" string)))
1564 (dolist (file (directory-files-recursively
1565 debbugs-gnu-trunk-directory "ChangeLog\\(.[0-9]+\\)?$"))
1566 (with-temp-buffer
1567 (when (file-exists-p file)
1568 (insert-file-contents file))
1569 (goto-char (point-min))
1570 (while (and (re-search-forward match nil t)
1571 (not (looking-at ".*tiny change")))
1572 (cl-incf found))))
1573 (message "%s is a contributor %d times" string found)
1574 found))
1575
1576 (defun debbugs-gnu-insert-changelog ()
1577 "Add a ChangeLog from a recently applied patch from a third party."
1578 (interactive)
1579 (let (from subject)
1580 (gnus-with-article-buffer
1581 (widen)
1582 (goto-char (point-min))
1583 (setq from (mail-extract-address-components (gnus-fetch-field "from"))
1584 subject (gnus-fetch-field "subject")))
1585 (let ((add-log-full-name (car from))
1586 (add-log-mailing-address (cadr from)))
1587 (add-change-log-entry-other-window)
1588 (let ((point (point)))
1589 (when (string-match "\\(bug#[0-9]+\\)" subject)
1590 (insert " (" (match-string 1 subject) ")."))
1591 (when (zerop (debbugs-gnu-find-contributor
1592 (let ((bits (split-string (car from))))
1593 (cond
1594 ((>= (length bits) 2)
1595 (format "%s.*%s" (car bits) (car (last bits))))
1596 ((= (length bits) 1)
1597 (car bits))
1598 ;; Fall back on the email address.
1599 (t
1600 (cadr from))))))
1601 (goto-char (point-max))
1602 (end-of-line)
1603 (insert " Copyright-paperwork-exempt: yes"))
1604 (goto-char point)))))
1605
1606 (defvar debbugs-gnu-lisp-mode-map
1607 (let ((map (make-sparse-keymap)))
1608 (define-key map [(meta m)] 'debbugs-gnu-insert-changelog)
1609 map))
1610
1611 (define-minor-mode debbugs-gnu-lisp-mode
1612 "Minor mode for providing a debbugs interface in Lisp buffers.
1613 \\{debbugs-gnu-lisp-mode-map}"
1614 :lighter " Debbugs" :keymap debbugs-gnu-lisp-mode-map)
1615
1616 (defvar debbugs-gnu-diff-mode-map
1617 (let ((map (make-sparse-keymap)))
1618 (define-key map [(meta m)] 'debbugs-gnu-diff-select)
1619 map))
1620
1621 (define-minor-mode debbugs-gnu-diff-mode
1622 "Minor mode for providing a debbugs interface in diff buffers.
1623 \\{debbugs-gnu-diff-mode-map}"
1624 :lighter " Debbugs" :keymap debbugs-gnu-diff-mode-map)
1625
1626 (defun debbugs-gnu-diff-select ()
1627 "Select the diff under point."
1628 (interactive)
1629 (delete-other-windows)
1630 (diff-goto-source))
1631
1632 (defvar debbugs-gnu-change-mode-map
1633 (let ((map (make-sparse-keymap)))
1634 (define-key map [(meta m)] 'debbugs-gnu-change-checkin)
1635 map))
1636
1637 (define-minor-mode debbugs-gnu-change-mode
1638 "Minor mode for providing a debbugs interface in ChangeLog buffers.
1639 \\{debbugs-gnu-change-mode-map}"
1640 :lighter " Debbugs" :keymap debbugs-gnu-change-mode-map)
1641
1642 (defun debbugs-gnu-change-checkin ()
1643 "Prepare checking in the current changes."
1644 (interactive)
1645 (save-some-buffers t)
1646 (when (get-buffer "*vc-dir*")
1647 (kill-buffer (get-buffer "*vc-dir*")))
1648 (let ((trunk (expand-file-name debbugs-gnu-trunk-directory)))
1649 (if (equal (cl-subseq default-directory 0 (length trunk))
1650 trunk)
1651 (vc-dir debbugs-gnu-trunk-directory)
1652 (vc-dir debbugs-gnu-branch-directory)))
1653 (goto-char (point-min))
1654 (while (not (search-forward "edited" nil t))
1655 (sit-for 0.01))
1656 (beginning-of-line)
1657 (while (search-forward "edited" nil t)
1658 (vc-dir-mark)
1659 (beginning-of-line))
1660 (vc-diff nil)
1661 (vc-next-action nil)
1662 (log-edit-insert-changelog t)
1663 (delete-other-windows)
1664 (split-window)
1665 (other-window 1)
1666 (switch-to-buffer "*vc-diff*")
1667 (other-window 1))
1668
1669 (defun debbugs-gnu-save-cache ()
1670 "Save the bugs cache to a file."
1671 (interactive)
1672 (unless debbugs-cache-data
1673 (error "No data to cache"))
1674 (unless (file-exists-p "~/.emacs.d/debbugs-cache")
1675 (make-directory "~/.emacs.d/debbugs-cache" t))
1676 (let ((coding-system-for-write 'utf-8))
1677 (with-temp-file "~/.emacs.d/debbugs-cache/list"
1678 (prin1 debbugs-cache-data (current-buffer)))))
1679
1680 (provide 'debbugs-gnu)
1681
1682 ;;; TODO:
1683
1684 ;; * Another random thought - is it possible to implement some local
1685 ;; cache, so only changed bugs are fetched? Glenn Morris.
1686
1687 ;;; debbugs-gnu.el ends here