]> code.delx.au - gnu-emacs-elpa/blob - packages/debbugs/debbugs-gnu.el
* Debbugs.wsdl: Add get_usertag specification.
[gnu-emacs-elpa] / packages / debbugs / debbugs-gnu.el
1 ;;; debbugs-gnu.el --- interface for the GNU bug tracker
2
3 ;; Copyright (C) 2011, 2012 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: comm, hypermedia, maint
7 ;; Package: debbugs
8 ;; Version: 0.3
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs 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 ;; GNU Emacs 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
38 ;; The bug tracker is called interactively by
39 ;;
40 ;; M-x debbugs-gnu
41
42 ;; It asks for the severities, for which bugs shall be shown. This can
43 ;; be either just one severity, or a list of severities, separated by
44 ;; comma. Valid severities are "serious", "important", "normal",
45 ;; "minor" or "wishlist". Severities "critical" and "grave" are not
46 ;; used, although configured on the GNU bug tracker. If no severity
47 ;; is given, all bugs are selected.
48
49 ;; There is also the pseudo severity "tagged". When it is used, the
50 ;; function will ask for user tags (a comma separated list), and shows
51 ;; just the bugs which are tagged with them. In general, user tags
52 ;; shall be strings denoting to subprojects of the package, like
53 ;; "cedet" or "tramp" of the package "emacs. If no user tag is given,
54 ;; locally tagged bugs are shown.
55
56 ;; If a prefix is given to the command, more search parameters are
57 ;; asked for, like packages (also a comma separated list, "emacs" is
58 ;; the default), whether archived bugs shall be shown, and whether
59 ;; closed bugs shall be shown.
60
61 ;; Another command is
62 ;;
63 ;; M-x debbugs-gnu-search
64
65 ;; It behaves like `debbugs-gnu', but asks at the beginning for a
66 ;; search phrase to be used for full text search. Additionally, it
67 ;; asks for key-value pairs to filter bugs. Keys are as described in
68 ;; `debbugs-get-status', the corresponding value must be a regular
69 ;; expression to match for. The other parameters are as described in
70 ;; `debbugs-gnu'. Usually, there is just one value except for the
71 ;; attribute "date", which needs two arguments specifying a period in
72 ;; which the bug has been submitted or modified.
73
74 ;; The bug reports are downloaded from the bug tracker. In order to
75 ;; not generate too much load of the server, up to 500 bugs will be
76 ;; downloaded at once. If there are more hits, you will be asked to
77 ;; change this limit, but please don't increase this number too much.
78
79 ;; These default values could be changed also by customer options
80 ;; `debbugs-gnu-default-severities', `debbugs-gnu-default-packages',
81 ;; `debbugs-gnu-default-hits-per-page' and `debbugs-gnu-default-suppress-bugs'.
82
83 ;; The commands create one or more pages of bug lists. Every bug is
84 ;; shown in one line, including the bug number, the status (combining
85 ;; merged bug numbers, keywords and severities), the name of the
86 ;; submitter, and the title of the bug. On every bug line you could
87 ;; apply the following actions by the following keystrokes:
88
89 ;; RET: Show corresponding messages in Gnus
90 ;; "C": Send a control message
91 ;; "t": Mark the bug locally as tagged
92 ;; "d": Show bug attributes
93
94 ;; Furthermore, you could apply the global actions
95
96 ;; "g": Rescan bugs
97 ;; "q": Quit the buffer
98 ;; "s": Toggle bug sorting for age or for state
99 ;; "x": Toggle suppressing of bugs
100 ;; "/": Display only bugs matching a string
101 ;; "w": Display all the currently selected bug reports
102
103 ;; When you visit the related bug messages in Gnus, you could also
104 ;; send control messages by keystroke "C".
105
106 ;; In the header line of every bug list page, you can toggle sorting
107 ;; per column by selecting a column with the mouse. The sorting
108 ;; happens as expected for the respective column; sorting in the Title
109 ;; column is depending on whether you are the owner of a bug.
110
111 ;;; Code:
112
113 (require 'debbugs)
114 (require 'widget)
115 (require 'tabulated-list)
116 (eval-when-compile (require 'cl))
117
118 (autoload 'widget-convert "wid-edit.el")
119 (autoload 'gnus-read-ephemeral-emacs-bug-group "gnus-group")
120 (autoload 'mail-header-subject "nnheader")
121 (autoload 'gnus-summary-article-header "gnus-sum")
122 (autoload 'message-make-from "message")
123
124 (defgroup debbugs-gnu ()
125 "UI for the debbugs.gnu.org bug tracker."
126 :group 'debbugs
127 :version "24.1")
128
129 (defcustom debbugs-gnu-default-severities '("serious" "important" "normal")
130 "*The list severities bugs are searched for.
131 \"tagged\" is not a severity but marks locally tagged bugs."
132 ;; <http://debbugs.gnu.org/Developer.html#severities>
133 :group 'debbugs-gnu
134 :type '(set (const "serious")
135 (const "important")
136 (const "normal")
137 (const "minor")
138 (const "wishlist")
139 (const "tagged"))
140 :version "24.1")
141
142 (defconst debbugs-gnu-all-severities
143 "*List of all possible severities."
144 (mapcar 'cadr (cdr (get 'debbugs-gnu-default-severities 'custom-type))))
145
146 (defcustom debbugs-gnu-default-packages '("emacs")
147 "*The list of packages to be searched for."
148 ;; <http://debbugs.gnu.org/Packages.html>
149 :group 'debbugs-gnu
150 :type '(set (const "automake")
151 (const "coreutils")
152 (const "debbugs.gnu.org")
153 (const "emacs")
154 (const "emacs-xwidgets")
155 (const "fm")
156 (const "gnus")
157 (const "guile")
158 (const "libtool")
159 (const "woodchuck"))
160 :version "24.1")
161
162 (defconst debbugs-gnu-all-packages
163 "*List of all possible package names."
164 (mapcar 'cadr (cdr (get 'debbugs-gnu-default-packages 'custom-type))))
165
166 (defcustom debbugs-gnu-default-hits-per-page 500
167 "*The number of bugs shown per page."
168 :group 'debbugs-gnu
169 :type 'integer
170 :version "24.1")
171
172 (defcustom debbugs-gnu-default-suppress-bugs
173 '((pending . "done"))
174 "*A list of specs for bugs to be suppressed.
175 An element of this list is a cons cell \(KEY . REGEXP\), with key
176 being returned by `debbugs-get-status', and VAL a regular
177 expression matching the corresponding value, a string. Showing
178 suppressed bugs is toggled by `debbugs-gnu-toggle-suppress'."
179 :group 'debbugs-gnu
180 :type '(alist :key-type symbol :value-type regexp)
181 :version "24.1")
182
183 (defface debbugs-gnu-new '((t (:foreground "red")))
184 "Face for new reports that nobody has answered.")
185
186 (defface debbugs-gnu-handled '((t (:foreground "ForestGreen")))
187 "Face for reports that have been modified recently.")
188
189 (defface debbugs-gnu-pending '((t (:foreground "MidnightBlue")))
190 "Face for reports that are pending.")
191
192 (defface debbugs-gnu-stale '((t (:foreground "orange")))
193 "Face for reports that have not been touched for a week.")
194
195 (defface debbugs-gnu-done '((t (:foreground "DarkGrey")))
196 "Face for closed bug reports.")
197
198 (defface debbugs-gnu-tagged '((t (:foreground "red")))
199 "Face for reports that have been tagged locally.")
200
201 (defvar debbugs-gnu-widgets nil)
202
203 (defvar debbugs-gnu-widget-map
204 (let ((map (make-sparse-keymap)))
205 (define-key map "\r" 'widget-button-press)
206 (define-key map [mouse-1] 'widget-button-press)
207 (define-key map [mouse-2] 'widget-button-press)
208 map))
209
210 (defvar debbugs-gnu-local-tags nil
211 "List of bug numbers tagged locally, and kept persistent.")
212
213 (defvar debbugs-gnu-persistency-file
214 (expand-file-name (locate-user-emacs-file "debbugs"))
215 "File name of a persistency store for debbugs variables")
216
217 (defun debbugs-gnu-dump-persistency-file ()
218 "Function to store debbugs variables persistently."
219 (with-temp-file debbugs-gnu-persistency-file
220 (insert
221 ";; -*- emacs-lisp -*-\n"
222 ";; Debbugs tags connection history. Don't change this file.\n\n"
223 (format "(setq debbugs-gnu-local-tags '%S)"
224 (sort (copy-sequence debbugs-gnu-local-tags) '<)))))
225
226 (defvar debbugs-gnu-current-query nil
227 "The query object of the current search.
228 It will be applied server-side, when calling `debbugs-get-bugs'.
229 It has the same format as `debbugs-gnu-default-suppress-bugs'.")
230
231 (defvar debbugs-gnu-current-filter nil
232 "The filter object for the current search.
233 It will be applied client-side, when parsing the results of
234 `debbugs-get-status'. It has a similar format as
235 `debbugs-gnu-default-suppress-bugs'. In case of keys representing
236 a date, value is the cons cell \(BEFORE . AFTER\).")
237
238 (defun debbugs-gnu-calendar-read (prompt acceptable &optional initial-contents)
239 "Return a string read from the minibuffer.
240 Derived from `calendar-read'."
241 (let ((value (read-string prompt initial-contents)))
242 (while (not (funcall acceptable value))
243 (setq value (read-string prompt initial-contents)))
244 value))
245
246 (defconst debbugs-gnu-phrase-prompt
247 (propertize
248 "Enter search phrase: "
249 'help-echo "\
250 The search phrase contains words to be searched for, combined by
251 operators like AND, ANDNOT and OR. If there is no operator
252 between the words, AND is used by default. The phrase can also
253 be empty, in this case only the following attributes are used for
254 search."))
255
256 ;;;###autoload
257 (defun debbugs-gnu-search ()
258 "Search for Emacs bugs interactively.
259 Search arguments are requested interactively. The \"search
260 phrase\" is used for full text search in the bugs database.
261 Further key-value pairs are requested until an empty key is
262 returned. If a key cannot be queried by a SOAP request, it is
263 marked as \"client-side filter\"."
264 (interactive)
265
266 (unwind-protect
267 (let ((date-format "\\([[:digit:]]\\{4\\}\\)-\\([[:digit:]]\\{1,2\\}\\)-\\([[:digit:]]\\{1,2\\}\\)")
268 key val1 val2 phrase severities packages archivedp)
269
270 ;; Check for the phrase.
271 (setq phrase (read-string debbugs-gnu-phrase-prompt))
272 (if (zerop (length phrase))
273 (setq phrase nil)
274 (add-to-list 'debbugs-gnu-current-query (cons 'phrase phrase)))
275
276 ;; The other queries.
277 (catch :finished
278 (while t
279 (setq key (completing-read
280 "Enter attribute: "
281 (if phrase
282 '("severity" "package" "tags" "submitter" "date"
283 "subject" "status")
284 '("severity" "package" "archive" "src" "tag"
285 "owner" "submitter" "maint" "correspondent"
286 "date" "log_modified" "last_modified"
287 "found_date" "fixed_date" "unarchived"
288 "subject" "done" "forwarded" "msgid" "summary"))
289 nil t))
290 (cond
291 ;; Server-side queries.
292 ((equal key "severity")
293 (setq
294 severities
295 (completing-read-multiple
296 "Enter severities: " debbugs-gnu-all-severities nil t
297 (mapconcat 'identity debbugs-gnu-default-severities ","))))
298
299 ((equal key "package")
300 (setq
301 packages
302 (completing-read-multiple
303 "Enter packages: " debbugs-gnu-all-packages nil t
304 (mapconcat 'identity debbugs-gnu-default-packages ","))))
305
306 ((equal key "archive")
307 ;; We simplify, by assuming just archived bugs are requested.
308 (setq archivedp t))
309
310 ((member key '("src" "tag" "tags"))
311 (setq val1 (read-string (format "Enter %s: " key)))
312 (when (not (zerop (length val1)))
313 (add-to-list
314 'debbugs-gnu-current-query (cons (intern key) val1))))
315
316 ((member key '("owner" "submitter" "maint" "correspondent"))
317 (setq val1 (read-string "Enter email address: "))
318 (when (not (zerop (length val1)))
319 (add-to-list
320 'debbugs-gnu-current-query (cons (intern key) val1))))
321
322 ((equal key "status")
323 (setq
324 val1
325 (completing-read "Enter status: " '("done" "forwarded" "open")))
326 (when (not (zerop (length val1)))
327 (add-to-list
328 'debbugs-gnu-current-query (cons (intern key) val1))))
329
330 ;; Client-side filters.
331 ((member key '("date" "log_modified" "last_modified"
332 "found_date" "fixed_date" "unarchived"))
333 (setq val1
334 (debbugs-gnu-calendar-read
335 (format "Enter %s before YYYY-MM-DD%s: "
336 key (if phrase "" " (client-side filter)"))
337 (lambda (x)
338 (string-match (concat "^\\(" date-format "\\|\\)$") x))))
339 (if (string-match date-format val1)
340 (setq val1 (floor
341 (float-time
342 (encode-time
343 0 0 0
344 (string-to-number (match-string 3 val1))
345 (string-to-number (match-string 2 val1))
346 (string-to-number (match-string 1 val1))))))
347 (setq val1 nil))
348 (setq val2
349 (debbugs-gnu-calendar-read
350 (format "Enter %s after YYYY-MM-DD%s: "
351 key (if phrase "" " (client-side filter)"))
352 (lambda (x)
353 (string-match (concat "^\\(" date-format "\\|\\)$") x))))
354 (if (string-match date-format val2)
355 (setq val2 (floor
356 (float-time
357 (encode-time
358 0 0 0
359 (string-to-number (match-string 3 val2))
360 (string-to-number (match-string 2 val2))
361 (string-to-number (match-string 1 val2))))))
362 (setq val2 nil))
363 (when (or val1 val2)
364 (add-to-list
365 (if phrase
366 'debbugs-gnu-current-query 'debbugs-gnu-current-filter)
367 (cons (intern key) (cons val1 val2)))))
368
369 ((not (zerop (length key)))
370 (setq val1
371 (funcall
372 (if phrase 'read-string 'read-regexp)
373 (format "Enter %s%s"
374 key (if phrase ": " " (client-side filter)"))))
375 (when (not (zerop (length val1)))
376 (add-to-list
377 (if phrase
378 'debbugs-gnu-current-query 'debbugs-gnu-current-filter)
379 (cons (intern key) val1))))
380
381 ;; The End.
382 (t (throw :finished nil)))))
383
384 ;; Do the search.
385 (debbugs-gnu severities packages archivedp))
386
387 ;; Reset query and filter.
388 (setq debbugs-gnu-current-query nil
389 debbugs-gnu-current-filter nil)))
390
391 ;;;###autoload
392 (defun debbugs-gnu (severities &optional packages archivedp suppress usertags)
393 "List all outstanding Emacs bugs."
394 (interactive
395 (let (severities archivedp)
396 (list
397 (setq severities
398 (completing-read-multiple
399 "Severities: " debbugs-gnu-all-severities nil t
400 (mapconcat 'identity debbugs-gnu-default-severities ",")))
401 ;; The next parameters are asked only when there is a prefix.
402 (if current-prefix-arg
403 (completing-read-multiple
404 "Packages: " debbugs-gnu-all-packages nil t
405 (mapconcat 'identity debbugs-gnu-default-packages ","))
406 debbugs-gnu-default-packages)
407 (when current-prefix-arg
408 (setq archivedp (y-or-n-p "Show archived bugs?")))
409 (when (and current-prefix-arg (not archivedp))
410 (y-or-n-p "Suppress unwanted bugs?"))
411 ;; This one must be asked for severity "tagged".
412 (when (member "tagged" severities)
413 (split-string (read-string "User tag(s): ") "," t)))))
414
415 ;; Initialize variables.
416 (when (and (file-exists-p debbugs-gnu-persistency-file)
417 (not debbugs-gnu-local-tags))
418 (with-temp-buffer
419 (insert-file-contents debbugs-gnu-persistency-file)
420 (eval (read (current-buffer)))))
421 (setq debbugs-gnu-widgets nil)
422
423 ;; Add queries.
424 (dolist (severity (if (consp severities) severities (list severities)))
425 (when (not (zerop (length severity)))
426 (add-to-list 'debbugs-gnu-current-query (cons 'severity severity))))
427 (dolist (package (if (consp packages) packages (list packages)))
428 (when (not (zerop (length package)))
429 (add-to-list 'debbugs-gnu-current-query (cons 'package package))))
430 (when archivedp
431 (add-to-list 'debbugs-gnu-current-query '(archive . "1")))
432 (dolist (usertag (if (consp usertags) usertags (list usertags)))
433 (when (not (zerop (length usertag)))
434 (add-to-list 'debbugs-gnu-current-query (cons 'usertag usertag))))
435
436 (unwind-protect
437 (let ((hits debbugs-gnu-default-hits-per-page)
438 (ids (debbugs-gnu-get-bugs debbugs-gnu-current-query)))
439
440 (if (> (length ids) hits)
441 (let ((cursor-in-echo-area nil))
442 (setq hits
443 (string-to-number
444 (read-string
445 (format
446 "How many reports (available %d, default %d): "
447 (length ids) hits)
448 nil
449 nil
450 (number-to-string hits))))))
451
452 (if (> (length ids) hits)
453 (let ((i 0)
454 curr-ids)
455 (while ids
456 (setq i (1+ i)
457 curr-ids (butlast ids (- (length ids) hits)))
458 (add-to-list
459 'debbugs-gnu-widgets
460 (widget-convert
461 'push-button
462 :follow-link 'mouse-face
463 :notify (lambda (widget &rest ignore)
464 (debbugs-gnu-show-reports widget))
465 :keymap debbugs-gnu-widget-map
466 :suppress suppress
467 :buffer-name (format "*Emacs Bugs*<%d>" i)
468 :bug-ids curr-ids
469 :query debbugs-gnu-current-query
470 :filter debbugs-gnu-current-filter
471 :help-echo (format "%d-%d" (car ids) (car (last curr-ids)))
472 :format " %[%v%]"
473 (number-to-string i))
474 'append)
475 (setq ids (last ids (- (length ids) hits))))
476 (debbugs-gnu-show-reports (car debbugs-gnu-widgets)))
477
478 (debbugs-gnu-show-reports
479 (widget-convert
480 'const
481 :suppress suppress
482 :buffer-name "*Emacs Bugs*"
483 :bug-ids ids
484 :query debbugs-gnu-current-query
485 :filter debbugs-gnu-current-filter))))
486
487 ;; Reset query and filter.
488 (setq debbugs-gnu-current-query nil
489 debbugs-gnu-current-filter nil)))
490
491 (defun debbugs-gnu-get-bugs (query)
492 "Retrieve bugs numbers from debbugs.gnu.org according search criteria."
493 (let ((debbugs-port "gnu.org")
494 (tagged (when (member '(severity . "tagged") query)
495 (copy-sequence debbugs-gnu-local-tags)))
496 (phrase (assoc 'phrase query))
497 usertags args)
498 ;; Compile query and usertags arguments.
499 (dolist (elt query)
500 (when (equal (car elt) 'usertag)
501 (add-to-list 'usertags (cdr elt))))
502 (unless (or query usertags)
503 (dolist (elt debbugs-gnu-default-packages)
504 (setq args (append args (list :package elt)))))
505 (dolist (elt query)
506 (unless (equal elt '(severity . "tagged"))
507 (setq args
508 (append
509 args
510 (if phrase
511 (cond
512 ((eq (car elt) 'phrase)
513 (list (list :phrase (cdr elt) :max 500)))
514 ((eq (car elt) 'date)
515 (list (list :date (cddr elt) (cadr elt)
516 :operator "NUMBT")))
517 (t
518 (list (list (intern (concat ":" (symbol-name (car elt))))
519 (cdr elt) :operator "ISTRINC"))))
520 (list (intern (concat ":" (symbol-name (car elt))))
521 (cdr elt)))))))
522
523 (sort
524 (cond
525 ;; If the query contains only the pseudo-severity "tagged", we
526 ;; return just the local tagged bugs.
527 ((and tagged (not usertags) (not (memq :severity args))) tagged)
528 ;; A full text query.
529 (phrase
530 (append
531 (mapcar
532 (lambda (x) (cdr (assoc "id" x)))
533 (apply 'debbugs-search-est args))
534 tagged))
535 ;; User tags.
536 (usertags
537 (let (result)
538 (dolist (elt packages result)
539 (setq result
540 (append result (apply 'debbugs-get-usertag elt usertags))))))
541 ;; Otherwise, we retrieve the bugs from the server.
542 (t (append (apply 'debbugs-get-bugs args) tagged)))
543 ;; Sort function.
544 '<)))
545
546 (defvar debbugs-gnu-current-widget nil)
547 (defvar debbugs-gnu-current-limit nil)
548
549 (defvar widget-mouse-face)
550
551 (defun debbugs-gnu-show-reports (widget)
552 "Show bug reports as given in WIDGET property :bug-ids."
553 ;; The tabulated mode sets several local variables. We must get rid
554 ;; of them.
555 (when (get-buffer (widget-get widget :buffer-name))
556 (kill-buffer (widget-get widget :buffer-name)))
557 (pop-to-buffer (get-buffer-create (widget-get widget :buffer-name)))
558 (debbugs-gnu-mode)
559 (let ((inhibit-read-only t)
560 (debbugs-port "gnu.org"))
561 (erase-buffer)
562 (set (make-local-variable 'debbugs-gnu-current-widget) widget)
563
564 (dolist (status (apply 'debbugs-get-status (widget-get widget :bug-ids)))
565 (let* ((id (cdr (assq 'id status)))
566 (words
567 (mapconcat
568 'identity
569 (cons (cdr (assq 'severity status))
570 (cdr (assq 'keywords status)))
571 ","))
572 (address (mail-header-parse-address
573 (decode-coding-string (cdr (assq 'originator status))
574 'utf-8)))
575 (owner (if (cdr (assq 'owner status))
576 (car (mail-header-parse-address
577 (decode-coding-string (cdr (assq 'owner status))
578 'utf-8)))))
579 (subject (decode-coding-string (cdr (assq 'subject status))
580 'utf-8))
581 merged)
582 (unless (equal (cdr (assq 'pending status)) "pending")
583 (setq words
584 (concat words "," (cdr (assq 'pending status)))))
585 (let ((packages (delete "emacs" (cdr (assq 'package status)))))
586 (when packages
587 (setq words (concat words "," (mapconcat 'identity packages ",")))))
588 (when (setq merged (cdr (assq 'mergedwith status)))
589 (setq words (format "%s,%s"
590 (if (numberp merged)
591 merged
592 (mapconcat 'number-to-string merged ","))
593 words)))
594 (when (or (not merged)
595 (not (let ((found nil))
596 (dolist (id (if (listp merged)
597 merged
598 (list merged)))
599 (dolist (entry tabulated-list-entries)
600 (when (equal id (cdr (assq 'id (car entry))))
601 (setq found t))))
602 found)))
603 (add-to-list
604 'tabulated-list-entries
605 (list
606 status
607 (vector
608 (propertize
609 (format "%5d" id)
610 'face
611 ;; Mark tagged bugs.
612 (if (memq id debbugs-gnu-local-tags)
613 'debbugs-gnu-tagged
614 'default))
615 (propertize
616 ;; Mark status and age.
617 words
618 'face
619 (cond
620 ((equal (cdr (assq 'pending status)) "done")
621 'debbugs-gnu-done)
622 ((member "pending" (cdr (assq 'keywords status)))
623 'debbugs-gnu-pending)
624 ((= (cdr (assq 'date status))
625 (cdr (assq 'log_modified status)))
626 'debbugs-gnu-new)
627 ((< (- (float-time)
628 (cdr (assq 'log_modified status)))
629 (* 60 60 24 7 2))
630 'debbugs-gnu-handled)
631 (t
632 'debbugs-gnu-stale)))
633 (propertize
634 ;; Prefer the name over the address.
635 (or (cdr address)
636 (car address))
637 'face
638 ;; Mark own submitted bugs.
639 (if (and (stringp (car address))
640 (string-equal (car address) user-mail-address))
641 'debbugs-gnu-tagged
642 'default))
643 (propertize
644 subject
645 'face
646 ;; Mark owned bugs.
647 (if (and (stringp owner)
648 (string-equal owner user-mail-address))
649 'debbugs-gnu-tagged
650 'default))))
651 'append))))
652 (tabulated-list-init-header)
653 (tabulated-list-print)
654
655 (set-buffer-modified-p nil)
656 (goto-char (point-min))))
657
658 (defun debbugs-gnu-print-entry (list-id cols)
659 "Insert a debbugs entry at point.
660 Used instead of `tabulated-list-print-entry'."
661 ;; This shall be in `debbugs-gnu-show-reports'. But
662 ;; `tabulated-list-print' erases the buffer, therefore we do it
663 ;; here. (bug#9047)
664 (when (and debbugs-gnu-widgets (= (point) (point-min)))
665 (widget-insert "Page:")
666 (mapc
667 (lambda (obj)
668 (if (eq obj debbugs-gnu-current-widget)
669 (widget-put obj :button-face 'widget-button-pressed)
670 (widget-put obj :button-face 'widget-button-face))
671 (widget-apply obj :create))
672 debbugs-gnu-widgets)
673 (widget-insert "\n\n")
674 (save-excursion
675 (widget-insert "\nPage:")
676 (mapc (lambda (obj) (widget-apply obj :create)) debbugs-gnu-widgets)
677 (widget-setup)))
678
679 (let ((beg (point))
680 (pos 0)
681 (case-fold-search t)
682 (id (aref cols 0))
683 (id-length (nth 1 (aref tabulated-list-format 0)))
684 (state (aref cols 1))
685 (state-length (nth 1 (aref tabulated-list-format 1)))
686 (submitter (aref cols 2))
687 (submitter-length (nth 1 (aref tabulated-list-format 2)))
688 (title (aref cols 3))
689 (title-length (nth 1 (aref tabulated-list-format 3))))
690 (when (and
691 ;; We may have a narrowing in effect.
692 (or (not debbugs-gnu-current-limit)
693 (memq (cdr (assq 'id list-id)) debbugs-gnu-current-limit))
694 ;; Filter suppressed bugs.
695 (or (not (widget-get debbugs-gnu-current-widget :suppress))
696 (not (catch :suppress
697 (dolist (check debbugs-gnu-default-suppress-bugs)
698 (when
699 (string-match
700 (cdr check)
701 (or (cdr (assq (car check) list-id)) ""))
702 (throw :suppress t))))))
703 ;; Filter search list.
704 (not (catch :suppress
705 (dolist (check
706 (widget-get debbugs-gnu-current-widget :filter))
707 (let ((val (cdr (assq (car check) list-id))))
708 (if (stringp (cdr check))
709 ;; Regular expression.
710 (when (not (string-match (cdr check) (or val "")))
711 (throw :suppress t))
712 ;; Time value.
713 (when (or (and (numberp (cadr check))
714 (< (cadr check) val))
715 (and (numberp (cddr check))
716 (> (cddr check) val)))
717 (throw :suppress t))))))))
718
719 ;; Insert id.
720 (indent-to (- id-length (length id)))
721 (insert id)
722 ;; Insert state.
723 (indent-to (setq pos (+ pos id-length 1)) 1)
724 (insert (if (> (length state) state-length)
725 (propertize (substring state 0 state-length)
726 'help-echo state)
727 state))
728 ;; Insert submitter.
729 (indent-to (setq pos (+ pos state-length 1)) 1)
730 (insert "[" (if (> (length submitter) (- submitter-length 2))
731 (propertize (substring submitter 0 (- submitter-length 2))
732 'help-echo submitter)
733 submitter))
734 (indent-to (+ pos (1- submitter-length)))
735 (insert "]")
736 ;; Insert title.
737 (indent-to (setq pos (+ pos submitter-length 1)) 1)
738 (insert (propertize title 'help-echo title))
739 ;; Add properties.
740 (add-text-properties
741 beg (point) `(tabulated-list-id ,list-id mouse-face ,widget-mouse-face))
742 (insert ?\n))))
743
744 (defvar debbugs-gnu-mode-map
745 (let ((map (make-sparse-keymap)))
746 (set-keymap-parent map tabulated-list-mode-map)
747 (define-key map "\r" 'debbugs-gnu-select-report)
748 (define-key map [mouse-1] 'debbugs-gnu-select-report)
749 (define-key map [mouse-2] 'debbugs-gnu-select-report)
750 (define-key map "s" 'debbugs-gnu-toggle-sort)
751 (define-key map "t" 'debbugs-gnu-toggle-tag)
752 (define-key map "d" 'debbugs-gnu-display-status)
753 (define-key map "g" 'debbugs-gnu-rescan)
754 (define-key map "x" 'debbugs-gnu-toggle-suppress)
755 (define-key map "/" 'debbugs-gnu-narrow-to-status)
756 (define-key map "w" 'debbugs-gnu-widen)
757 (define-key map "C" 'debbugs-gnu-send-control-message)
758 map))
759
760 (defun debbugs-gnu-rescan ()
761 "Rescan the current set of bug reports."
762 (interactive)
763
764 ;; The last page will be provided with new bug ids.
765 ;; TODO: Do it also for the other pages.
766 (when (and debbugs-gnu-widgets
767 (eq debbugs-gnu-current-widget (car (last debbugs-gnu-widgets))))
768 (let ((first-id (car (widget-get debbugs-gnu-current-widget :bug-ids)))
769 (last-id (car
770 (last (widget-get debbugs-gnu-current-widget :bug-ids))))
771 (ids (debbugs-gnu-get-bugs
772 (widget-get debbugs-gnu-current-widget :query))))
773
774 (while (and (<= first-id last-id) (not (memq first-id ids)))
775 (setq first-id (1+ first-id)))
776
777 (when (<= first-id last-id)
778 (widget-put debbugs-gnu-current-widget :bug-ids (memq first-id ids)))))
779
780 ;; Refresh the buffer. `save-excursion' does not work, so we
781 ;; remember the position.
782 (let ((pos (point)))
783 (debbugs-gnu-show-reports debbugs-gnu-current-widget)
784 (goto-char pos)))
785
786 (defvar debbugs-gnu-sort-state 'number)
787
788 (define-derived-mode debbugs-gnu-mode tabulated-list-mode "Debbugs"
789 "Major mode for listing bug reports.
790
791 All normal editing commands are switched off.
792 \\<debbugs-gnu-mode-map>
793
794 The following commands are available:
795
796 \\{debbugs-gnu-mode-map}"
797 (set (make-local-variable 'debbugs-gnu-sort-state) 'number)
798 (set (make-local-variable 'debbugs-gnu-current-limit) nil)
799 (setq tabulated-list-format [("Id" 5 debbugs-gnu-sort-id)
800 ("State" 20 debbugs-gnu-sort-state)
801 ("Submitter" 25 t)
802 ("Title" 10 debbugs-gnu-sort-title)])
803 (setq tabulated-list-sort-key (cons "Id" nil))
804 (setq tabulated-list-printer 'debbugs-gnu-print-entry)
805 (buffer-disable-undo)
806 (setq truncate-lines t)
807 (setq buffer-read-only t))
808
809 (defun debbugs-gnu-sort-id (s1 s2)
810 (< (cdr (assq 'id (car s1)))
811 (cdr (assq 'id (car s2)))))
812
813 (defconst debbugs-gnu-state-preference
814 '((debbugs-gnu-new . 1)
815 (debbugs-gnu-stale . 2)
816 (debbugs-gnu-handled . 3)
817 (debbugs-gnu-done . 4)
818 (debbugs-gnu-pending . 5)))
819
820 (defun debbugs-gnu-get-state-preference (face-string)
821 (or (cdr (assq (get-text-property 0 'face face-string)
822 debbugs-gnu-state-preference))
823 10))
824
825 (defconst debbugs-gnu-severity-preference
826 '(("serious" . 1)
827 ("important" . 2)
828 ("normal" . 3)
829 ("minor" . 4)
830 ("wishlist" . 5)))
831
832 (defun debbugs-gnu-get-severity-preference (state)
833 (or (cdr (assoc (cdr (assq 'severity state))
834 debbugs-gnu-severity-preference))
835 10))
836
837 (defun debbugs-gnu-sort-state (s1 s2)
838 (let ((id1 (cdr (assq 'id (car s1))))
839 (age1 (debbugs-gnu-get-state-preference (aref (nth 1 s1) 1)))
840 (id2 (cdr (assq 'id (car s2))))
841 (age2 (debbugs-gnu-get-state-preference (aref (nth 1 s2) 1))))
842 (cond
843 ;; Tagged bugs go to the end.
844 ((and (not (memq id1 debbugs-gnu-local-tags))
845 (memq id2 debbugs-gnu-local-tags))
846 t)
847 ((and (memq id1 debbugs-gnu-local-tags)
848 (not (memq id2 debbugs-gnu-local-tags)))
849 nil)
850 ;; Then, we check the age of the bugs.
851 ((< age1 age2)
852 t)
853 ((> age1 age2)
854 nil)
855 ;; If they have the same age, we check for severity.
856 ((< (debbugs-gnu-get-severity-preference (car s1))
857 (debbugs-gnu-get-severity-preference (car s2)))
858 t)
859 (t nil))))
860
861 (defun debbugs-gnu-sort-title (s1 s2)
862 (let ((owner (if (cdr (assq 'owner (car s1)))
863 (car (mail-header-parse-address
864 (decode-coding-string (cdr (assq 'owner (car s1)))
865 'utf-8))))))
866 (and (stringp owner)
867 (string-equal owner user-mail-address))))
868
869 (defun debbugs-gnu-toggle-sort ()
870 "Toggle sorting by age and by state."
871 (interactive)
872 (if (eq debbugs-gnu-sort-state 'number)
873 (progn
874 (setq debbugs-gnu-sort-state 'state)
875 (setq tabulated-list-sort-key (cons "Id" nil)))
876 (setq debbugs-gnu-sort-state 'number)
877 (setq tabulated-list-sort-key (cons "State" nil)))
878 (tabulated-list-init-header)
879 (tabulated-list-print))
880
881 (defun debbugs-gnu-widen ()
882 "Display all the currently selected bug reports."
883 (interactive)
884 (let ((id (debbugs-gnu-current-id t))
885 (inhibit-read-only t))
886 (setq debbugs-gnu-current-limit nil)
887 (tabulated-list-init-header)
888 (tabulated-list-print)
889 (when id
890 (debbugs-gnu-goto id))))
891
892 (defun debbugs-gnu-narrow-to-status (string &optional status-only)
893 "Only display the bugs matching STRING.
894 If STATUS-ONLY (the prefix), ignore matches in the From and
895 Subject fields."
896 (interactive "sNarrow to: \np")
897 (let ((id (debbugs-gnu-current-id t))
898 (inhibit-read-only t)
899 status)
900 (setq debbugs-gnu-current-limit nil)
901 (goto-char (point-min))
902 (while (not (eobp))
903 (setq status (debbugs-gnu-current-status))
904 (if (and (not (member string (assq 'keywords status)))
905 (not (member string (assq 'severity status)))
906 (or status-only
907 (not (string-match string (cdr (assq 'originator status)))))
908 (or status-only
909 (not (string-match string (cdr (assq 'subject status))))))
910 (delete-region (point) (progn (forward-line 1) (point)))
911 (push (cdr (assq 'id status)) debbugs-gnu-current-limit)
912 (forward-line 1)))
913 (when id
914 (debbugs-gnu-goto id))))
915
916 (defun debbugs-gnu-goto (id)
917 "Go to the line displaying bug ID."
918 (goto-char (point-min))
919 (while (and (not (eobp))
920 (not (equal (debbugs-gnu-current-id t) id)))
921 (forward-line 1)))
922
923 (defun debbugs-gnu-toggle-tag ()
924 "Toggle tag of the report in the current line."
925 (interactive)
926 (save-excursion
927 (beginning-of-line)
928 (let ((inhibit-read-only t)
929 (id (debbugs-gnu-current-id)))
930 (if (memq id debbugs-gnu-local-tags)
931 (progn
932 (setq debbugs-gnu-local-tags (delq id debbugs-gnu-local-tags))
933 (put-text-property (point) (+ (point) 5) 'face 'default))
934 (add-to-list 'debbugs-gnu-local-tags id)
935 (put-text-property
936 (+ (point) (- 5 (length (number-to-string id)))) (+ (point) 5)
937 'face 'debbugs-gnu-tagged))))
938 (debbugs-gnu-dump-persistency-file))
939
940 (defun debbugs-gnu-toggle-suppress ()
941 "Suppress bugs marked in `debbugs-gnu-suppress-bugs'."
942 (interactive)
943 (widget-put debbugs-gnu-current-widget :suppress
944 (not (widget-get debbugs-gnu-current-widget :suppress)))
945 (tabulated-list-init-header)
946 (tabulated-list-print))
947
948 (defvar debbugs-gnu-bug-number nil)
949 (defvar debbugs-gnu-subject nil)
950
951 (defun debbugs-gnu-current-id (&optional noerror)
952 (or (cdr (assq 'id (debbugs-gnu-current-status)))
953 (and (not noerror)
954 (error "No bug on the current line"))))
955
956 (defun debbugs-gnu-current-status ()
957 (get-text-property (line-beginning-position) 'tabulated-list-id))
958
959 (defun debbugs-gnu-current-query ()
960 (widget-get debbugs-gnu-current-widget :query))
961
962 (defun debbugs-gnu-display-status (query status)
963 "Display the query and status of the report on the current line."
964 (interactive (list (debbugs-gnu-current-query)
965 (debbugs-gnu-current-status)))
966 (pop-to-buffer "*Bug Status*")
967 (erase-buffer)
968 (when query (pp query (current-buffer)))
969 (when status (pp status (current-buffer)))
970 (goto-char (point-min))
971 (special-mode))
972
973 (defun debbugs-gnu-select-report ()
974 "Select the report on the current line."
975 (interactive)
976 ;; We open the report messages.
977 (let* ((status (debbugs-gnu-current-status))
978 (id (cdr (assq 'id status)))
979 (merged (cdr (assq 'mergedwith status))))
980 (gnus-read-ephemeral-emacs-bug-group
981 (cons id (if (listp merged)
982 merged
983 (list merged)))
984 (cons (current-buffer)
985 (current-window-configuration)))
986 (with-current-buffer (window-buffer (selected-window))
987 (set (make-local-variable 'debbugs-gnu-bug-number) id)
988 (set (make-local-variable 'debbugs-gnu-subject)
989 (format "Re: bug#%d: %s" id (cdr (assq 'subject status))))
990 (debbugs-gnu-summary-mode 1))))
991
992 (defvar debbugs-gnu-summary-mode-map
993 (let ((map (make-sparse-keymap)))
994 (define-key map "C" 'debbugs-gnu-send-control-message)
995 map))
996
997 (defvar gnus-posting-styles)
998
999 (define-minor-mode debbugs-gnu-summary-mode
1000 "Minor mode for providing a debbugs interface in Gnus summary buffers.
1001
1002 \\{debbugs-gnu-summary-mode-map}"
1003 :lighter " Debbugs" :keymap debbugs-gnu-summary-mode-map
1004 (set (make-local-variable 'gnus-posting-styles)
1005 `((".*"
1006 (eval
1007 (when (buffer-live-p gnus-article-copy)
1008 (with-current-buffer gnus-article-copy
1009 (set (make-local-variable 'message-prune-recipient-rules)
1010 '((".*@debbugs.*" "emacs-pretest-bug")
1011 (".*@debbugs.*" "bug-gnu-emacs")
1012 ("[0-9]+@debbugs.*" "submit@debbugs.gnu.org")
1013 ("[0-9]+@debbugs.*" "quiet@debbugs.gnu.org")))
1014 (set (make-local-variable 'message-alter-recipients-function)
1015 (lambda (address)
1016 (if (string-match "\\([0-9]+\\)@donarmstrong"
1017 (car address))
1018 (let ((new (format "%s@debbugs.gnu.org"
1019 (match-string 1 (car address)))))
1020 (cons new new))
1021 address)))
1022 ;; `gnus-posting-styles' is eval'ed after
1023 ;; `message-simplify-subject'. So we cannot use m-s-s.
1024 (setq subject ,debbugs-gnu-subject))))))))
1025
1026 (defun debbugs-gnu-guess-current-id ()
1027 "Guess the ID based on \"#23\"."
1028 (save-excursion
1029 (beginning-of-line)
1030 (and
1031 (or (re-search-forward "#\\([0-9]+\\)" (line-end-position) t)
1032 (progn
1033 (goto-char (point-min))
1034 (re-search-forward "#\\([0-9]+\\)" nil t)))
1035 (string-to-number (match-string 1)))))
1036
1037 (defun debbugs-gnu-send-control-message (message &optional reverse)
1038 "Send a control message for the current bug report.
1039 You can set the severity or add a tag, or close the report. If
1040 you use the special \"done\" MESSAGE, the report will be marked as
1041 fixed, and then closed.
1042
1043 If given a prefix, and given a tag to set, the tag will be
1044 removed instead."
1045 (interactive
1046 (list (completing-read
1047 "Control message: "
1048 '("serious" "important" "normal" "minor" "wishlist"
1049 "done" "donenotabug" "donewontfix" "doneunreproducible"
1050 "unarchive" "reopen" "close"
1051 "merge" "forcemerge"
1052 "owner" "noowner"
1053 "invalid"
1054 "reassign"
1055 "patch" "wontfix" "moreinfo" "unreproducible" "fixed" "notabug"
1056 "pending" "help" "security" "confirmed")
1057 nil t)
1058 current-prefix-arg))
1059 (let* ((id (or debbugs-gnu-bug-number ; Set on group entry.
1060 (debbugs-gnu-guess-current-id)
1061 (debbugs-gnu-current-id)))
1062 (version
1063 (when (member message '("close" "done"))
1064 (read-string
1065 "Version: "
1066 (cond
1067 ;; Emacs development versions.
1068 ((string-match
1069 "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\." emacs-version)
1070 (format "%s.%d"
1071 (match-string 1 emacs-version)
1072 (1+ (string-to-number (match-string 2 emacs-version)))))
1073 ;; Emacs release versions.
1074 ((string-match
1075 "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" emacs-version)
1076 (format "%s.%s"
1077 (match-string 1 emacs-version)
1078 (match-string 2 emacs-version)))
1079 (t emacs-version))))))
1080 (with-temp-buffer
1081 (insert "To: control@debbugs.gnu.org\n"
1082 "From: " (message-make-from) "\n"
1083 (format "Subject: control message for bug #%d\n" id)
1084 "\n"
1085 (cond
1086 ((member message '("unarchive" "reopen" "noowner"))
1087 (format "%s %d\n" message id))
1088 ((member message '("merge" "forcemerge"))
1089 (format "%s %d %s\n" message id
1090 (read-string "Merge with bug #: ")))
1091 ((equal message "owner")
1092 (format "owner %d !\n" id))
1093 ((equal message "reassign")
1094 (format "reassign %d %s\n" id (read-string "Package(s): ")))
1095 ((equal message "close")
1096 (format "close %d %s\n" id version))
1097 ((equal message "done")
1098 (format "tags %d fixed\nclose %d %s\n" id id version))
1099 ((member message '("donenotabug" "donewontfix"
1100 "doneunreproducible"))
1101 (format "tags %d %s\nclose %d\n" id (substring message 4) id))
1102 ((member message '("serious" "important" "normal"
1103 "minor" "wishlist"))
1104 (format "severity %d %s\n" id message))
1105 ((equal message "invalid")
1106 (format "tags %d notabug\ntags %d wontfix\nclose %d\n"
1107 id id id))
1108 (t
1109 (format "tags %d%s %s\n"
1110 id (if reverse " -" "")
1111 message))))
1112 (funcall send-mail-function))))
1113
1114 (provide 'debbugs-gnu)
1115
1116 ;;; TODO:
1117
1118 ;; * Reorganize pages after client-side filtering.
1119
1120 ;;; debbugs-gnu.el ends here