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