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, "emacs" is
58 ;; the default), whether archived bugs shall be shown, and whether
59 ;; closed bugs shall be shown.
63 ;; M-x debbugs-org-search
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
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.
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'.
81 ;; The commands create a TODO list. Besides the usual handling of
82 ;; TODO items, you could apply the following actions by the following
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
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 ;; Finally, if you simply want to list some bugs with known bug
95 ;; numbers, call the command
97 ;; M-x debbugs-org-bugs
99 ;; The bug numbers to be shown shall be entered as comma separated list.
103 (require 'debbugs-gnu)
105 (eval-when-compile (require 'cl))
107 (defconst debbugs-org-severity-priority
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.")
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))))
120 (defconst debbugs-org-priority-faces
121 '(("A" . org-warning)
123 "Highlighting of prioritized TODO items.")
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
128 (defvar debbugs-org-ids nil
129 "The list of bug ids to be shown following the elisp link.")
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.")
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
145 (let ((date-format "\\([[:digit:]]\\{4\\}\\)-\\([[:digit:]]\\{1,2\\}\\)-\\([[:digit:]]\\{1,2\\}\\)")
146 key val1 val2 phrase severities packages archivedp)
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))
152 ;; The other queries.
155 (setq key (completing-read
157 '("severity" "package" "tags" "submitter" "author"
161 ;; Server-side queries.
162 ((equal key "severity")
165 (completing-read-multiple
166 "Enter severities: " debbugs-gnu-all-severities nil t
167 (mapconcat 'identity debbugs-gnu-default-severities ","))))
169 ((equal key "package")
172 (completing-read-multiple
173 "Enter packages: " debbugs-gnu-all-packages nil t
174 (mapconcat 'identity debbugs-gnu-default-packages ","))))
176 ((member key '("tags" "subject"))
177 (setq val1 (read-string (format "Enter %s: " key)))
178 (when (not (zerop (length val1)))
180 'debbugs-gnu-current-query (cons (intern key) val1))))
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)))
187 'debbugs-gnu-current-query (cons (intern key) val1))))
189 ((equal key "status")
192 (completing-read "Enter status: " '("done" "forwarded" "open")))
193 (when (not (zerop (length val1)))
195 'debbugs-gnu-current-query (cons (intern key) val1))))
198 (t (throw :finished nil)))))
201 (debbugs-org severities packages))
203 ;; Reset query and filter.
204 (setq debbugs-gnu-current-query nil)))
207 (defun debbugs-org (severities &optional packages archivedp suppress tags)
208 "List all outstanding bugs."
210 (let (severities archivedp)
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)))))
230 ;; Initialize variables.
231 (when (and (file-exists-p debbugs-gnu-persistency-file)
232 (not debbugs-gnu-local-tags))
234 (insert-file-contents debbugs-gnu-persistency-file)
235 (eval (read (current-buffer)))))
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))))
245 (add-to-list 'debbugs-gnu-current-query '(archive . "1")))
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))))
253 (with-current-buffer (get-buffer-create debbugs-org-show-buffer-name)
257 (let ((hits debbugs-gnu-default-hits-per-page))
258 (setq debbugs-org-ids (debbugs-gnu-get-bugs debbugs-gnu-current-query))
260 (when (> (length debbugs-org-ids) hits)
261 (let ((cursor-in-echo-area nil))
266 "How many reports (available %d, default %d): "
267 (length debbugs-org-ids) hits)
270 (number-to-string hits))))))
272 (debbugs-org-show-next-reports hits))
275 (setq debbugs-gnu-current-query nil)))
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))
282 ;; FIXME: Does not show any effect.
283 (set (make-local-variable 'org-priority-faces) debbugs-org-priority-faces)
285 (let ((inhibit-read-only t)
286 (debbugs-port "gnu.org"))
288 (when (= (point) (point-min))
289 (insert "# -*- eval: (debbugs-org-mode 1); -*-\n\n"))
293 (apply 'debbugs-get-status bug-numbers)
294 (lambda (x y) (< (cdr (assq 'id x)) (cdr (assq 'id y))))))
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))))
318 (when (string-match "^\\([0-9.]+\\); \\(.+\\)$" subject)
319 (add-to-list 'tags (match-string 1 subject))
320 (setq subject (match-string 2 subject)))
322 (add-to-list 'tags "ARCHIVE"))
325 ;; Replace all invalid TAG characters by "_".
326 (lambda (x) (replace-regexp-in-string "[^A-Za-z0-9_@]" "_" x))
333 (if done "DONE" "TODO")
335 (if tags (mapconcat 'identity (append '("") tags '("")) ":") "")))
341 " [%Y-%m-%d %a] Submitted\n" (seconds-to-time date))))
344 (insert " :PROPERTIES:\n")
345 (insert (format " :DEBBUGS_ID: %s\n" id))
349 " :MERGED_WITH: %s\n"
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)))
359 " [[elisp:(debbugs-gnu-select-report)][Messages]]\n")
365 " [%Y-%m-%d %a] Last modified\n"
366 (seconds-to-time last-modified))))
368 ;; Add text properties.
369 (add-text-properties beg (point) `(tabulated-list-id ,status))))
371 (goto-char (point-min))
373 (set-buffer-modified-p nil)))
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)."
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))))))
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)
396 (goto-char (point-max))
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))
407 "* [[elisp:(debbugs-org-show-next-reports %s)][Next bugs]]\n"
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)
416 "Keymap for the `debbugs-org-mode' minor mode.")
418 ;; Make byte-compiler quiet.
419 (defvar gnus-posting-styles)
422 (define-minor-mode debbugs-org-mode
423 "Minor mode for providing a debbugs interface in org-mode buffers.
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)
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))
445 (defun debbugs-org-bugs (&rest bugs)
446 "List all BUGS, a list of bug numbers."
448 (mapcar 'string-to-number
449 (completing-read-multiple "Bug numbers: " nil 'natnump)))
451 (unless (natnump elt) (signal 'wrong-type-argument (list 'natnump elt))))
452 (add-to-list 'debbugs-gnu-current-query (cons 'bugs bugs))
457 ;; - Refactor it in order to avoid code duplication with debbugs-gnu.el.
458 ;; - Make headline customizable.
459 ;; - Sort according to different TODO properties.
461 (provide 'debbugs-org)