1 ;;; debbugs-org.el --- Org-mode interface for the GNU bug tracker
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.org>
6 ;; Keywords: comm, hypermedia, maint, outlines
9 ;; This file is not part of GNU Emacs.
11 ;; This program is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package provides an interface to bug reports which are located
27 ;; on the GNU bug tracker debbugs.gnu.org. Its main purpose is to
28 ;; show and manipulate bug reports as org-mode TODO items.
30 ;; If you have `debbugs-org.el' in your load-path, you could enable
31 ;; the bug tracker commands by the following lines in your ~/.emacs
33 ;; (autoload 'debbugs-org "debbugs-org" "" 'interactive)
34 ;; (autoload 'debbugs-org-search "debbugs-org" "" 'interactive)
35 ;; (autoload 'debbugs-org-bugs "debbugs-org" "" 'interactive)
37 ;; The bug tracker is called interactively by
41 ;; It asks for the severities, for which bugs shall be shown. This can
42 ;; be either just one severity, or a list of severities, separated by
43 ;; comma. Valid severities are "serious", "important", "normal",
44 ;; "minor" or "wishlist". Severities "critical" and "grave" are not
45 ;; used, although configured on the GNU bug tracker. If no severity
46 ;; is given, all bugs are selected.
48 ;; There is also the pseudo severity "tagged". When it is used, the
49 ;; function will ask for user tags (a comma separated list), and shows
50 ;; just the bugs which are tagged with them. In general, user tags
51 ;; shall be strings denoting to subprojects of the package, like
52 ;; "cedet" or "tramp" of the package "emacs". If no user tag is
53 ;; given, locally tagged bugs are shown.
55 ;; If a prefix is given to the command, more search parameters are
56 ;; asked for, like packages (also a comma separated list, "emacs" is
57 ;; the default), whether archived bugs shall be shown, and whether
58 ;; closed bugs shall be suppressed from being retrieved.
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, several downloads will
74 ;; be performed, until all bugs are retrieved.
76 ;; These default values could be changed also by customer options
77 ;; `debbugs-gnu-default-severities' and `debbugs-gnu-default-packages'.
79 ;; The commands create a TODO list. Besides the usual handling of
80 ;; TODO items, you could apply the following actions by the following
83 ;; "C-c # C": Send a debbugs control message
84 ;; "C-c # t": Mark the bug locally as tagged
85 ;; "C-c # d": Show bug attributes
87 ;; The last entry in a TODO record is the link [[Messages]]. If you
88 ;; follow this link, a Gnus ephemeral group or an Rmail buffer is
89 ;; opened presenting all related messages for this bug. Here you
90 ;; could also send debbugs control messages by keystroke "C".
92 ;; Finally, if you simply want to list some bugs with known bug
93 ;; numbers, call the command
95 ;; M-x debbugs-org-bugs
97 ;; The bug numbers to be shown shall be entered as comma separated list.
101 (require 'debbugs-gnu)
103 (eval-when-compile (require 'cl))
105 ;; Buffer-local variables.
106 (defvar debbugs-gnu-local-query)
107 (defvar debbugs-gnu-local-filter)
109 (defconst debbugs-org-severity-priority
112 (lambda (x) (prog1 (cons x (char-to-string priority)) (incf priority)))
113 debbugs-gnu-all-severities))
114 "Mapping of debbugs severities to TODO priorities.")
116 (defun debbugs-org-get-severity-priority (state)
117 "Returns the TODO priority of STATE."
118 (or (cdr (assoc (cdr (assq 'severity state))
119 debbugs-org-severity-priority))
120 (cdr (assoc "minor" debbugs-org-severity-priority))))
122 (defconst debbugs-org-priority-faces
123 '(("A" . org-warning)
125 "Highlighting of prioritized TODO items.")
127 (defvar debbugs-org-buffer-name "*Org Bugs*"
128 "The buffer name we present the bug reports.
129 This could be a temporary buffer, or a buffer linked with a file.")
132 (defun debbugs-org-search ()
133 "Search for bugs interactively.
134 Search arguments are requested interactively. The \"search
135 phrase\" is used for full text search in the bugs database.
136 Further key-value pairs are requested until an empty key is
141 ;; Check for the phrase.
142 (let ((phrase (read-string debbugs-gnu-phrase-prompt))
143 key val1 severities packages)
145 (add-to-list 'debbugs-gnu-current-query (cons 'phrase phrase))
147 ;; The other queries.
150 (setq key (completing-read
152 '("severity" "package" "tags" "submitter" "author"
156 ;; Server-side queries.
157 ((equal key "severity")
160 (completing-read-multiple
161 "Enter severities: " debbugs-gnu-all-severities nil t
162 (mapconcat 'identity debbugs-gnu-default-severities ","))))
164 ((equal key "package")
167 (completing-read-multiple
168 "Enter packages: " debbugs-gnu-all-packages nil t
169 (mapconcat 'identity debbugs-gnu-default-packages ","))))
171 ((member key '("tags" "subject"))
172 (setq val1 (read-string (format "Enter %s: " key)))
173 (when (not (zerop (length val1)))
175 'debbugs-gnu-current-query (cons (intern key) val1))))
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)))
182 'debbugs-gnu-current-query (cons (intern key) val1))))
184 ((equal key "status")
187 (completing-read "Enter status: " '("done" "forwarded" "open")))
188 (when (not (zerop (length val1)))
190 'debbugs-gnu-current-query (cons (intern key) val1))))
193 (t (throw :finished nil)))))
196 (debbugs-org severities packages))))
199 (defun debbugs-org (severities &optional packages archivedp suppress tags)
200 "List all outstanding bugs."
202 (let (severities archivedp)
205 (completing-read-multiple
206 "Severities: " debbugs-gnu-all-severities nil t
207 (mapconcat 'identity debbugs-gnu-default-severities ",")))
208 ;; The next parameters are asked only when there is a prefix.
209 (if current-prefix-arg
210 (completing-read-multiple
211 "Packages: " debbugs-gnu-all-packages nil t
212 (mapconcat 'identity debbugs-gnu-default-packages ","))
213 debbugs-gnu-default-packages)
214 (when current-prefix-arg
215 (setq archivedp (y-or-n-p "Show archived bugs?")))
216 (when (and current-prefix-arg (not archivedp))
217 (y-or-n-p "Suppress unwanted bugs?"))
218 ;; This one must be asked for severity "tagged".
219 (when (member "tagged" severities)
220 (split-string (read-string "User tag(s): ") "," t)))))
222 ;; Initialize variables.
223 (when (and (file-exists-p debbugs-gnu-persistency-file)
224 (not debbugs-gnu-local-tags))
226 (insert-file-contents debbugs-gnu-persistency-file)
227 (eval (read (current-buffer)))))
230 (dolist (severity (if (consp severities) severities (list severities)))
231 (when (not (zerop (length severity)))
232 (add-to-list 'debbugs-gnu-current-query (cons 'severity severity))))
233 (dolist (package (if (consp packages) packages (list packages)))
234 (when (not (zerop (length package)))
235 (add-to-list 'debbugs-gnu-current-query (cons 'package package))))
237 (add-to-list 'debbugs-gnu-current-query '(archive . "1")))
239 (add-to-list 'debbugs-gnu-current-query '(status . "open"))
240 (add-to-list 'debbugs-gnu-current-query '(status . "forwarded")))
241 (dolist (tag (if (consp tags) tags (list tags)))
242 (when (not (zerop (length tag)))
243 (add-to-list 'debbugs-gnu-current-query (cons 'tag tag))))
246 (debbugs-org-show-reports)
249 (setq debbugs-gnu-current-query nil))
251 (defun debbugs-org-show-reports ()
252 "Show bug reports as retrieved via `debbugs-gnu-current-query'."
253 (let ((inhibit-read-only t)
254 (org-startup-folded t))
255 (when (get-buffer debbugs-org-buffer-name)
256 (kill-buffer debbugs-org-buffer-name))
257 (switch-to-buffer (get-buffer-create debbugs-org-buffer-name))
262 ;; `debbugs-get-status' returns in random order, so we must sort.
264 (apply 'debbugs-get-status
265 (debbugs-gnu-get-bugs debbugs-gnu-local-query))
266 (lambda (a b) (> (cdr (assq 'id a)) (cdr (assq 'id b))))))
268 (id (cdr (assq 'id status)))
269 (done (string-equal (cdr (assq 'pending status)) "done"))
270 (priority (debbugs-org-get-severity-priority status))
271 (archived (cdr (assq 'archived status)))
272 (tags (append (cdr (assq 'found_versions status))
273 (cdr (assq 'tags status))))
274 (subject (when (cdr (assq 'subject status))
275 (decode-coding-string
276 (cdr (assq 'subject status)) 'utf-8)))
277 (date (cdr (assq 'date status)))
278 (last-modified (cdr (assq 'last_modified status)))
279 (originator (when (cdr (assq 'originator status))
280 (decode-coding-string
281 (cdr (assq 'originator status)) 'utf-8)))
282 (owner (when (cdr (assq 'owner status))
283 (decode-coding-string (cdr (assq 'owner status)) 'utf-8)))
284 (closed-by (when (cdr (assq 'done status))
285 (decode-coding-string
286 (cdr (assq 'done status)) 'utf-8)))
287 (merged (cdr (assq 'mergedwith status))))
290 (when (string-match "^\\([0-9.]+\\); \\(.+\\)$" subject)
291 (let ((x (match-string 1 subject))) (pushnew x tags :test #'equal))
292 (setq subject (match-string 2 subject)))
294 (pushnew "ARCHIVE" tags :test #'equal))
297 ;; Replace all invalid TAG characters by "_".
298 (lambda (x) (replace-regexp-in-string "[^A-Za-z0-9_@]" "_" x))
305 (if done "DONE" "TODO")
307 (if tags (mapconcat 'identity (append '("") tags '("")) ":") "")))
313 " [%Y-%m-%d %a] Submitted\n" (seconds-to-time date))))
316 (insert " :PROPERTIES:\n")
317 (insert (format " :DEBBUGS_ID: %s\n" id))
321 " :MERGED_WITH: %s\n"
323 merged (mapconcat 'number-to-string merged " ")))))
324 (insert (format " :CREATOR: %s\n" originator))
325 (when owner (insert (format " :OWNER: %s\n" owner)))
326 (when closed-by (insert (format " :CLOSED_BY: %s\n" closed-by)))
331 " [[elisp:(debbugs-gnu-select-report)][Messages]]\n")
337 " [%Y-%m-%d %a] Last modified\n"
338 (seconds-to-time last-modified))))
340 ;; Add text properties.
341 (add-text-properties beg (point) `(tabulated-list-id ,status))))
344 (insert "* COMMENT Local " "Variables\n"
345 "# Local " "Variables:\n"
347 "# eval: (debbugs-org-mode 1)\n"
349 (goto-char (point-min))
351 (set-buffer-modified-p nil)))
353 (defun debbugs-org-regenerate-status ()
354 "Regenerate the `tabulated-list-id' text property.
355 This property is used when following the [Messages] link, so you
356 need to regenerate it when opening an .org file after you killed
357 the corresponding buffer (e.g. by closing Emacs)."
359 (goto-char (point-min))
360 (while (re-search-forward ":DEBBUGS_ID:[ \t]*\\([0-9]+\\)" nil t)
361 (let* ((bugnum (string-to-number (match-string 1)))
362 (mw (org-entry-get (point) "MERGEDWIDTH"))
363 (tli (list (cons 'id bugnum)
364 (cons 'bug_num bugnum)
365 (cons 'mergedwidth (if mw (string-to-number mw)))))
366 (beg (org-back-to-heading t))
367 (end (org-end-of-subtree t)))
368 (add-text-properties beg end `(tabulated-list-id ,tli))))))
370 (defconst debbugs-org-mode-map
371 (let ((map (make-sparse-keymap)))
372 (define-key map (kbd "C-c # t") 'debbugs-gnu-toggle-tag)
373 (define-key map (kbd "C-c # C") 'debbugs-gnu-send-control-message)
374 (define-key map (kbd "C-c # d") 'debbugs-gnu-display-status)
376 "Keymap for the `debbugs-org-mode' minor mode.")
378 ;; Make byte-compiler quiet.
379 (defvar gnus-posting-styles)
382 (define-minor-mode debbugs-org-mode
383 "Minor mode for providing a debbugs interface in org-mode buffers.
385 \\{debbugs-org-mode-map}"
386 :lighter " Debbugs" :keymap debbugs-org-mode-map
387 (set (make-local-variable 'debbugs-gnu-local-query) debbugs-gnu-current-query)
388 (set (make-local-variable 'debbugs-gnu-local-filter)
389 debbugs-gnu-current-filter)
390 ;; FIXME: Does not show any effect.
391 (set (make-local-variable 'org-priority-faces) debbugs-org-priority-faces)
392 (set (make-local-variable 'gnus-posting-styles)
395 (when (buffer-live-p gnus-article-copy)
396 (with-current-buffer gnus-article-copy
397 (set (make-local-variable 'message-prune-recipient-rules)
398 '((".*@debbugs.*" "emacs-pretest-bug")
399 (".*@debbugs.*" "bug-gnu-emacs")
400 ("[0-9]+@debbugs.*" "submit@debbugs.gnu.org")
401 ("[0-9]+@debbugs.*" "quiet@debbugs.gnu.org")))
402 ;; `gnus-posting-styles' is eval'ed after
403 ;; `message-simplify-subject'. So we cannot use m-s-s.
404 (setq subject ,debbugs-gnu-subject)))))))
405 (debbugs-org-regenerate-status))
408 (defun debbugs-org-bugs (&rest bugs)
409 "List all BUGS, a list of bug numbers."
411 (mapcar 'string-to-number
412 (completing-read-multiple "Bug numbers: " nil 'natnump)))
414 (unless (natnump elt) (signal 'wrong-type-argument (list 'natnump elt))))
415 (add-to-list 'debbugs-gnu-current-query (cons 'bugs bugs))
420 ;; - Refactor it in order to avoid code duplication with debbugs-gnu.el.
421 ;; - Make headline customizable.
422 ;; - Sort according to different TODO properties.
424 (provide 'debbugs-org)