1 ;;; debbugs-org.el --- Org-mode interface for the GNU bug tracker
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.org>
6 ;; Keywords: comm, hypermedia, maint, outlines
10 ;; This file is not part of GNU Emacs.
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.
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.
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/>.
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.
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
34 ;; (autoload 'debbugs-org "debbugs-org" "" 'interactive)
35 ;; (autoload 'debbugs-org-search "debbugs-org" "" 'interactive)
36 ;; (autoload 'debbugs-org-bugs "debbugs-org" "" 'interactive)
38 ;; The bug tracker is called interactively by
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.
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.
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.
62 ;; M-x debbugs-org-search
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
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.
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'.
80 ;; The commands create a TODO list. Besides the usual handling of
81 ;; TODO items, you could apply the following actions by the following
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
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".
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).
99 ;; Finally, if you simply want to list some bugs with known bug
100 ;; numbers, call the command
102 ;; M-x debbugs-org-bugs
104 ;; The bug numbers to be shown shall be entered as comma separated list.
108 (require 'debbugs-gnu)
110 (eval-when-compile (require 'cl))
112 (defconst debbugs-org-severity-priority
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.")
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))))
125 (defconst debbugs-org-priority-faces
126 '(("A" . org-warning)
128 "Highlighting of prioritized TODO items.")
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
133 (defvar debbugs-org-ids nil
134 "The list of bug ids to be shown following the elisp link.")
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
146 (let ((date-format "\\([[:digit:]]\\{4\\}\\)-\\([[:digit:]]\\{1,2\\}\\)-\\([[:digit:]]\\{1,2\\}\\)")
147 key val1 val2 phrase severities packages archivedp)
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))
153 ;; The other queries.
156 (setq key (completing-read
158 '("severity" "package" "tags" "submitter" "author"
162 ;; Server-side queries.
163 ((equal key "severity")
166 (completing-read-multiple
167 "Enter severities: " debbugs-gnu-all-severities nil t
168 (mapconcat 'identity debbugs-gnu-default-severities ","))))
170 ((equal key "package")
173 (completing-read-multiple
174 "Enter packages: " debbugs-gnu-all-packages nil t
175 (mapconcat 'identity debbugs-gnu-default-packages ","))))
177 ((member key '("tags" "subject"))
178 (setq val1 (read-string (format "Enter %s: " key)))
179 (when (not (zerop (length val1)))
181 'debbugs-gnu-current-query (cons (intern key) val1))))
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)))
188 'debbugs-gnu-current-query (cons (intern key) val1))))
190 ((equal key "status")
193 (completing-read "Enter status: " '("done" "forwarded" "open")))
194 (when (not (zerop (length val1)))
196 'debbugs-gnu-current-query (cons (intern key) val1))))
199 (t (throw :finished nil)))))
202 (debbugs-org severities packages))
204 ;; Reset query and filter.
205 (setq debbugs-gnu-current-query nil)))
208 (defun debbugs-org (severities &optional packages archivedp tags)
209 "List all outstanding bugs."
211 (let (severities archivedp)
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)))))
229 ;; Initialize variables.
230 (when (and (file-exists-p debbugs-gnu-persistency-file)
231 (not debbugs-gnu-local-tags))
233 (insert-file-contents debbugs-gnu-persistency-file)
234 (eval (read (current-buffer)))))
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))))
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))))
249 (with-current-buffer (get-buffer-create "*Org Bugs*")
253 (let ((hits debbugs-gnu-default-hits-per-page))
254 (setq debbugs-org-ids (debbugs-gnu-get-bugs debbugs-gnu-current-query))
256 (when (> (length debbugs-org-ids) hits)
257 (let ((cursor-in-echo-area nil))
262 "How many reports (available %d, default %d): "
263 (length debbugs-org-ids) hits)
266 (number-to-string hits))))))
268 (debbugs-org-show-next-reports hits))
271 (setq debbugs-gnu-current-query nil)))
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*"))
278 ;; FIXME: Does not show any effect.
279 (set (make-local-variable 'org-priority-faces) debbugs-org-priority-faces)
281 (let ((inhibit-read-only t)
282 (debbugs-port "gnu.org"))
286 (apply 'debbugs-get-status bug-numbers)
287 (lambda (x y) (< (cdr (assq 'id x)) (cdr (assq 'id y))))))
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))))
311 (when (string-match "^\\([0-9.]+\\); \\(.+\\)$" subject)
312 (add-to-list 'tags (match-string 1 subject))
313 (setq subject (match-string 2 subject)))
315 (add-to-list 'tags "ARCHIVE"))
318 ;; Replace all invalid TAG characters by "_".
319 (lambda (x) (replace-regexp-in-string "[^A-Za-z0-9_@]" "_" x))
326 (if done "DONE" "TODO")
328 (if tags (mapconcat 'identity (append '("") tags '("")) ":") "")))
334 " [%Y-%m-%d %a] Submitted\n" (seconds-to-time date))))
337 (insert " :PROPERTIES:\n")
338 (insert (format " :DEBBUGS_ID: %s\n" id))
342 " :MERGED_WITH: %s\n"
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)))
352 " [[elisp:(debbugs-gnu-select-report)][Messages]]\n")
358 " [%Y-%m-%d %a] Last modified\n"
359 (seconds-to-time last-modified))))
361 ;; Add text properties.
362 (add-text-properties beg (point) `(tabulated-list-id ,status))))
364 (goto-char (point-min))
366 (set-buffer-modified-p nil)))
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.)"
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))))))
386 (defun debbugs-org-show-next-reports (hits)
387 "Show next HITS of bug reports."
388 (with-current-buffer (get-buffer-create "*Org Bugs*")
390 (goto-char (point-max))
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))
401 "* [[elisp:(debbugs-org-show-next-reports %s)][Next bugs]]\n"
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)
411 "Keymap for the `debbugs-org-mode' minor mode.")
413 ;; Make byte-compiler quiet.
414 (defvar gnus-posting-styles)
416 (define-minor-mode debbugs-org-mode
417 "Minor mode for providing a debbugs interface in org-mode buffers.
419 \\{debbugs-org-mode-map}"
420 :lighter " Debbugs" :keymap debbugs-org-mode-map
421 (set (make-local-variable 'gnus-posting-styles)
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))))))))
436 (defun debbugs-org-bugs (&rest bugs)
437 "List all BUGS, a list of bug numbers."
439 (mapcar 'string-to-number
440 (completing-read-multiple "Bug numbers: " nil 'natnump)))
442 (unless (natnump elt) (signal 'wrong-type-argument (list 'natnump elt))))
443 (add-to-list 'debbugs-gnu-current-query (cons 'bugs bugs))
448 ;; - Refactor it in order to avoid code duplication with debbugs-gnu.el.
449 ;; - Make headline customizable.
450 ;; - Sort according to different TODO properties.
452 (provide 'debbugs-org)