]> code.delx.au - gnu-emacs-elpa/blob - packages/debbugs/debbugs-org.el
New command `debbugs-org-regenerate-status'
[gnu-emacs-elpa] / packages / debbugs / debbugs-org.el
1 ;;; debbugs-org.el --- Org-mode interface for the GNU bug tracker
2
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.org>
6 ;; Keywords: comm, hypermedia, maint, outlines
7 ;; Package: debbugs
8 ;; Version: 0.5
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 as org-mode TODO items.
30
31 ;; If you have `debbugs-org.el' in your load-path, you could enable
32 ;; the bug tracker commands by the following lines in your ~/.emacs
33 ;;
34 ;; (autoload 'debbugs-org "debbugs-org" "" 'interactive)
35 ;; (autoload 'debbugs-org-search "debbugs-org" "" 'interactive)
36 ;; (autoload 'debbugs-org-bugs "debbugs-org" "" 'interactive)
37
38 ;; The bug tracker is called interactively by
39 ;;
40 ;; M-x debbugs-org
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
54 ;; given, 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, "org-mode" is
58 ;; the default), or whether archived bugs shall be shown.
59
60 ;; Another command is
61 ;;
62 ;; M-x debbugs-org-search
63
64 ;; It behaves like `debbugs-org', but asks at the beginning for a
65 ;; search phrase to be used for full text search. Additionally, it
66 ;; asks for key-value pairs to filter bugs. Keys are as described in
67 ;; `debbugs-get-status', the corresponding value must be a regular
68 ;; expression to match for. The other parameters are as described in
69 ;; `debbugs-org'.
70
71 ;; The bug reports are downloaded from the bug tracker. In order to
72 ;; not generate too much load of the server, up to 500 bugs will be
73 ;; downloaded at once. If there are more hits, you will be asked to
74 ;; change this limit, but please don't increase this number too much.
75
76 ;; These default values could be changed also by customer options
77 ;; `debbugs-gnu-default-severities', `debbugs-gnu-default-packages'
78 ;; and `debbugs-gnu-default-hits-per-page'.
79
80 ;; The commands create a TODO list. Besides the usual handling of
81 ;; TODO items, you could apply the following actions by the following
82 ;; keystrokes:
83
84 ;; "C-c # C": Send a debbugs control message
85 ;; "C-c # t": Mark the bug locally as tagged
86 ;; "C-c # d": Show bug attributes
87 ;; "C-c # g": Regenerate text properties with status
88
89 ;; The last entry in a TODO record is the link [[Messages]]. If you
90 ;; follow this link, a Gnus ephemeral group is opened presenting all
91 ;; related messages for this bug. Here you could also send debbugs
92 ;; control messages by keystroke "C".
93
94 ;; Note that following the [[Messages]] link will only work if you
95 ;; generated the list of bugs from the current session, or if you hit
96 ;; C-c # g which will regenerate the text properties containing the
97 ;; bug status (i.e. the bug number).
98
99 ;; Finally, if you simply want to list some bugs with known bug
100 ;; numbers, call the command
101 ;;
102 ;; M-x debbugs-org-bugs
103
104 ;; The bug numbers to be shown shall be entered as comma separated list.
105
106 ;;; Code:
107
108 (require 'debbugs-gnu)
109 (require 'org)
110 (eval-when-compile (require 'cl))
111
112 (defconst debbugs-org-severity-priority
113 (let ((priority ?A))
114 (mapcar
115 (lambda (x) (prog1 (cons x (char-to-string priority)) (incf priority)))
116 debbugs-gnu-all-severities))
117 "Mapping of debbugs severities to TODO priorities.")
118
119 (defun debbugs-org-get-severity-priority (state)
120 "Returns the TODO priority of STATE."
121 (or (cdr (assoc (cdr (assq 'severity state))
122 debbugs-org-severity-priority))
123 (cdr (assoc "minor" debbugs-org-severity-priority))))
124
125 (defconst debbugs-org-priority-faces
126 '(("A" . org-warning)
127 ("B" . org-warning))
128 "Highlighting of prioritized TODO items.")
129
130 ;; We do not add the bug numbers list to the elisp:link, because this
131 ;; would be much too long. Instead, this variable shall keep the bug
132 ;; numbers.
133 (defvar debbugs-org-ids nil
134 "The list of bug ids to be shown following the elisp link.")
135
136 ;;;###autoload
137 (defun debbugs-org-search ()
138 "Search for bugs interactively.
139 Search arguments are requested interactively. The \"search
140 phrase\" is used for full text search in the bugs database.
141 Further key-value pairs are requested until an empty key is
142 returned."
143 (interactive)
144
145 (unwind-protect
146 (let ((date-format "\\([[:digit:]]\\{4\\}\\)-\\([[:digit:]]\\{1,2\\}\\)-\\([[:digit:]]\\{1,2\\}\\)")
147 key val1 val2 phrase severities packages archivedp)
148
149 ;; Check for the phrase.
150 (setq phrase (read-string debbugs-gnu-phrase-prompt))
151 (add-to-list 'debbugs-gnu-current-query (cons 'phrase phrase))
152
153 ;; The other queries.
154 (catch :finished
155 (while t
156 (setq key (completing-read
157 "Enter attribute: "
158 '("severity" "package" "tags" "submitter" "author"
159 "subject" "status")
160 nil t))
161 (cond
162 ;; Server-side queries.
163 ((equal key "severity")
164 (setq
165 severities
166 (completing-read-multiple
167 "Enter severities: " debbugs-gnu-all-severities nil t
168 (mapconcat 'identity debbugs-gnu-default-severities ","))))
169
170 ((equal key "package")
171 (setq
172 packages
173 (completing-read-multiple
174 "Enter packages: " debbugs-gnu-all-packages nil t
175 (mapconcat 'identity debbugs-gnu-default-packages ","))))
176
177 ((member key '("tags" "subject"))
178 (setq val1 (read-string (format "Enter %s: " key)))
179 (when (not (zerop (length val1)))
180 (add-to-list
181 'debbugs-gnu-current-query (cons (intern key) val1))))
182
183 ((member key '("submitter" "author"))
184 (when (equal key "author") (setq key "@author"))
185 (setq val1 (read-string "Enter email address: "))
186 (when (not (zerop (length val1)))
187 (add-to-list
188 'debbugs-gnu-current-query (cons (intern key) val1))))
189
190 ((equal key "status")
191 (setq
192 val1
193 (completing-read "Enter status: " '("done" "forwarded" "open")))
194 (when (not (zerop (length val1)))
195 (add-to-list
196 'debbugs-gnu-current-query (cons (intern key) val1))))
197
198 ;; The End.
199 (t (throw :finished nil)))))
200
201 ;; Do the search.
202 (debbugs-org severities packages))
203
204 ;; Reset query and filter.
205 (setq debbugs-gnu-current-query nil)))
206
207 ;;;###autoload
208 (defun debbugs-org (severities &optional packages archivedp tags)
209 "List all outstanding bugs."
210 (interactive
211 (let (severities archivedp)
212 (list
213 (setq severities
214 (completing-read-multiple
215 "Severities: " debbugs-gnu-all-severities nil t
216 (mapconcat 'identity debbugs-gnu-default-severities ",")))
217 ;; The next parameters are asked only when there is a prefix.
218 (if current-prefix-arg
219 (completing-read-multiple
220 "Packages: " debbugs-gnu-all-packages nil t
221 (mapconcat 'identity debbugs-gnu-default-packages ","))
222 debbugs-gnu-default-packages)
223 (when current-prefix-arg
224 (setq archivedp (y-or-n-p "Show archived bugs?")))
225 ;; This one must be asked for severity "tagged".
226 (when (member "tagged" severities)
227 (split-string (read-string "User tag(s): ") "," t)))))
228
229 ;; Initialize variables.
230 (when (and (file-exists-p debbugs-gnu-persistency-file)
231 (not debbugs-gnu-local-tags))
232 (with-temp-buffer
233 (insert-file-contents debbugs-gnu-persistency-file)
234 (eval (read (current-buffer)))))
235
236 ;; Add queries.
237 (dolist (severity (if (consp severities) severities (list severities)))
238 (when (not (zerop (length severity)))
239 (add-to-list 'debbugs-gnu-current-query (cons 'severity severity))))
240 (dolist (package (if (consp packages) packages (list packages)))
241 (when (not (zerop (length package)))
242 (add-to-list 'debbugs-gnu-current-query (cons 'package package))))
243 (when archivedp
244 (add-to-list 'debbugs-gnu-current-query '(archive . "1")))
245 (dolist (tag (if (consp tags) tags (list tags)))
246 (when (not (zerop (length tag)))
247 (add-to-list 'debbugs-gnu-current-query (cons 'tag tag))))
248
249 (with-current-buffer (get-buffer-create "*Org Bugs*")
250 (erase-buffer))
251
252 (unwind-protect
253 (let ((hits debbugs-gnu-default-hits-per-page))
254 (setq debbugs-org-ids (debbugs-gnu-get-bugs debbugs-gnu-current-query))
255
256 (when (> (length debbugs-org-ids) hits)
257 (let ((cursor-in-echo-area nil))
258 (setq hits
259 (string-to-number
260 (read-string
261 (format
262 "How many reports (available %d, default %d): "
263 (length debbugs-org-ids) hits)
264 nil
265 nil
266 (number-to-string hits))))))
267
268 (debbugs-org-show-next-reports hits))
269
270 ;; Reset query.
271 (setq debbugs-gnu-current-query nil)))
272
273 (defun debbugs-org-show-reports (bug-numbers)
274 "Show bug reports as given in BUG-NUMBERS."
275 (pop-to-buffer (get-buffer-create "*Org Bugs*"))
276 (org-mode)
277 (debbugs-org-mode 1)
278 ;; FIXME: Does not show any effect.
279 (set (make-local-variable 'org-priority-faces) debbugs-org-priority-faces)
280
281 (let ((inhibit-read-only t)
282 (debbugs-port "gnu.org"))
283
284 (dolist (status
285 (sort
286 (apply 'debbugs-get-status bug-numbers)
287 (lambda (x y) (< (cdr (assq 'id x)) (cdr (assq 'id y))))))
288 (let* ((beg (point))
289 (id (cdr (assq 'id status)))
290 (done (string-equal (cdr (assq 'pending status)) "done"))
291 (priority (debbugs-org-get-severity-priority status))
292 (archived (cdr (assq 'archived status)))
293 (tags (append (cdr (assq 'found_versions status))
294 (cdr (assq 'tags status))))
295 (subject (when (cdr (assq 'subject status))
296 (decode-coding-string
297 (cdr (assq 'subject status)) 'utf-8)))
298 (date (cdr (assq 'date status)))
299 (last-modified (cdr (assq 'last_modified status)))
300 (originator (when (cdr (assq 'originator status))
301 (decode-coding-string
302 (cdr (assq 'originator status)) 'utf-8)))
303 (owner (when (cdr (assq 'owner status))
304 (decode-coding-string (cdr (assq 'owner status)) 'utf-8)))
305 (closed-by (when (cdr (assq 'done status))
306 (decode-coding-string
307 (cdr (assq 'done status)) 'utf-8)))
308 (merged (cdr (assq 'mergedwith status))))
309
310 ;; Handle tags.
311 (when (string-match "^\\([0-9.]+\\); \\(.+\\)$" subject)
312 (add-to-list 'tags (match-string 1 subject))
313 (setq subject (match-string 2 subject)))
314 (when archived
315 (add-to-list 'tags "ARCHIVE"))
316 (setq tags
317 (mapcar
318 ;; Replace all invalid TAG characters by "_".
319 (lambda (x) (replace-regexp-in-string "[^A-Za-z0-9_@]" "_" x))
320 tags))
321
322 ;; Headline.
323 (insert
324 (format
325 "* %s [#%s] %s %s\n"
326 (if done "DONE" "TODO")
327 priority subject
328 (if tags (mapconcat 'identity (append '("") tags '("")) ":") "")))
329
330 ;; Submitted.
331 (when date
332 (insert
333 (format-time-string
334 " [%Y-%m-%d %a] Submitted\n" (seconds-to-time date))))
335
336 ;; Properties.
337 (insert " :PROPERTIES:\n")
338 (insert (format " :DEBBUGS_ID: %s\n" id))
339 (when merged
340 (insert
341 (format
342 " :MERGED_WITH: %s\n"
343 (if (numberp merged)
344 merged (mapconcat 'number-to-string merged " ")))))
345 (insert (format " :CREATOR: %s\n" originator))
346 (when owner (insert (format " :OWNER: %s\n" owner)))
347 (when closed-by (insert (format " :CLOSED_BY: %s\n" closed-by)))
348 (insert " :END:\n")
349
350 ;; Messages.
351 (insert
352 " [[elisp:(debbugs-gnu-select-report)][Messages]]\n")
353
354 ;; Last modified.
355 (when last-modified
356 (insert
357 (format-time-string
358 " [%Y-%m-%d %a] Last modified\n"
359 (seconds-to-time last-modified))))
360
361 ;; Add text properties.
362 (add-text-properties beg (point) `(tabulated-list-id ,status))))
363
364 (goto-char (point-min))
365 (org-overview)
366 (set-buffer-modified-p nil)))
367
368 (defun debbugs-org-regenerate-status ()
369 "Regenerate the `tabulated-list-id' text property.
370 This property is used when following the [Messages] link, so you
371 need to regenerate it when opening an .org file after you killed
372 the corresponding buffer (e.g. by closing Emacs.)"
373 (interactive)
374 (save-excursion
375 (goto-char (point-min))
376 (while (re-search-forward ":DEBBUGS_ID:[ \t]*\\([0-9]+\\)" nil t)
377 (let* ((bugnum (string-to-number (match-string 1)))
378 (mw (org-entry-get (point) "MERGEDWIDTH"))
379 (tli (list (cons 'id bugnum)
380 (cons 'bug_num bugnum)
381 (cons 'mergedwidth (if mw (string-to-number mw)))))
382 (beg (org-back-to-heading t))
383 (end (org-end-of-subtree t)))
384 (add-text-properties beg end `(tabulated-list-id ,tli))))))
385
386 (defun debbugs-org-show-next-reports (hits)
387 "Show next HITS of bug reports."
388 (with-current-buffer (get-buffer-create "*Org Bugs*")
389 (save-excursion
390 (goto-char (point-max))
391 (forward-line -1)
392 (delete-region (point) (point-max))
393 (debbugs-org-show-reports
394 (butlast debbugs-org-ids (- (length debbugs-org-ids) hits)))
395 (setq debbugs-org-ids
396 (last debbugs-org-ids (- (length debbugs-org-ids) hits)))
397 (when debbugs-org-ids
398 (goto-char (point-max))
399 (insert
400 (format
401 "* [[elisp:(debbugs-org-show-next-reports %s)][Next bugs]]\n"
402 hits))))))
403
404 (defconst debbugs-org-mode-map
405 (let ((map (make-sparse-keymap)))
406 (define-key map (kbd "C-c # t") 'debbugs-gnu-toggle-tag)
407 (define-key map (kbd "C-c # C") 'debbugs-gnu-send-control-message)
408 (define-key map (kbd "C-c # d") 'debbugs-gnu-display-status)
409 (define-key map (kbd "C-c # g") 'debbugs-org-regenerate-status)
410 map)
411 "Keymap for the `debbugs-org-mode' minor mode.")
412
413 ;; Make byte-compiler quiet.
414 (defvar gnus-posting-styles)
415
416 (define-minor-mode debbugs-org-mode
417 "Minor mode for providing a debbugs interface in org-mode buffers.
418
419 \\{debbugs-org-mode-map}"
420 :lighter " Debbugs" :keymap debbugs-org-mode-map
421 (set (make-local-variable 'gnus-posting-styles)
422 `((".*"
423 (eval
424 (when (buffer-live-p gnus-article-copy)
425 (with-current-buffer gnus-article-copy
426 (set (make-local-variable 'message-prune-recipient-rules)
427 '((".*@debbugs.*" "emacs-pretest-bug")
428 (".*@debbugs.*" "bug-gnu-emacs")
429 ("[0-9]+@debbugs.*" "submit@debbugs.gnu.org")
430 ("[0-9]+@debbugs.*" "quiet@debbugs.gnu.org")))
431 ;; `gnus-posting-styles' is eval'ed after
432 ;; `message-simplify-subject'. So we cannot use m-s-s.
433 (setq subject ,debbugs-gnu-subject))))))))
434
435 ;;;###autoload
436 (defun debbugs-org-bugs (&rest bugs)
437 "List all BUGS, a list of bug numbers."
438 (interactive
439 (mapcar 'string-to-number
440 (completing-read-multiple "Bug numbers: " nil 'natnump)))
441 (dolist (elt bugs)
442 (unless (natnump elt) (signal 'wrong-type-argument (list 'natnump elt))))
443 (add-to-list 'debbugs-gnu-current-query (cons 'bugs bugs))
444 (debbugs-org nil))
445
446 ;; TODO
447
448 ;; - Refactor it in order to avoid code duplication with debbugs-gnu.el.
449 ;; - Make headline customizable.
450 ;; - Sort according to different TODO properties.
451
452 (provide 'debbugs-org)