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