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