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