]> code.delx.au - gnu-emacs-elpa/blob - packages/debbugs/debbugs-org.el
Merge commit 'f0b45f4a010d8e5680f66c9a22f433386d023466' from context-coloring
[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-2016 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.org>
6 ;; Keywords: comm, hypermedia, maint, outlines
7 ;; Package: debbugs
8
9 ;; This file is not part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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.
29
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
32 ;;
33 ;; (autoload 'debbugs-org "debbugs-org" "" 'interactive)
34 ;; (autoload 'debbugs-org-search "debbugs-org" "" 'interactive)
35 ;; (autoload 'debbugs-org-bugs "debbugs-org" "" 'interactive)
36
37 ;; The bug tracker is called interactively by
38 ;;
39 ;; M-x debbugs-org
40
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.
47
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.
54
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.
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, several downloads will
74 ;; be performed, until all bugs are retrieved.
75
76 ;; These default values could be changed also by customer options
77 ;; `debbugs-gnu-default-severities' and `debbugs-gnu-default-packages'.
78
79 ;; The commands create a TODO list. Besides the usual handling of
80 ;; TODO items, you could apply the following actions by the following
81 ;; keystrokes:
82
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
86
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".
91
92 ;; Finally, if you simply want to list some bugs with known bug
93 ;; numbers, call the command
94 ;;
95 ;; M-x debbugs-org-bugs
96
97 ;; The bug numbers to be shown shall be entered as comma separated list.
98
99 ;;; Code:
100
101 (require 'debbugs-gnu)
102 (require 'org)
103 (eval-when-compile (require 'cl))
104
105 ;; Buffer-local variables.
106 (defvar debbugs-gnu-local-query)
107 (defvar debbugs-gnu-local-filter)
108
109 (defconst debbugs-org-severity-priority
110 (let ((priority ?A))
111 (mapcar
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.")
115
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))))
121
122 (defconst debbugs-org-priority-faces
123 '(("A" . org-warning)
124 ("B" . org-warning))
125 "Highlighting of prioritized TODO items.")
126
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.")
130
131 ;;;###autoload
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
137 returned."
138 (interactive)
139
140 (unwind-protect
141 ;; Check for the phrase.
142 (let ((phrase (read-string debbugs-gnu-phrase-prompt))
143 key val1 severities packages)
144
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 ;;;###autoload
199 (defun debbugs-org (severities &optional packages archivedp suppress tags)
200 "List all outstanding bugs."
201 (interactive
202 (let (severities archivedp)
203 (list
204 (setq severities
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)))))
221
222 ;; Initialize variables.
223 (when (and (file-exists-p debbugs-gnu-persistency-file)
224 (not debbugs-gnu-local-tags))
225 (with-temp-buffer
226 (insert-file-contents debbugs-gnu-persistency-file)
227 (eval (read (current-buffer)))))
228
229 ;; Add queries.
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))))
236 (when archivedp
237 (add-to-list 'debbugs-gnu-current-query '(archive . "1")))
238 (when suppress
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))))
244
245 ;; Show result.
246 (debbugs-org-show-reports)
247
248 ;; Reset query.
249 (setq debbugs-gnu-current-query nil))
250
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))
258 (org-mode)
259 (debbugs-org-mode 1)
260
261 (dolist (status
262 ;; `debbugs-get-status' returns in random order, so we must sort.
263 (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))))))
267 (let* ((beg (point))
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))))
288
289 ;; Handle tags.
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)))
293 (when archived
294 (pushnew "ARCHIVE" tags :test #'equal))
295 (setq tags
296 (mapcar
297 ;; Replace all invalid TAG characters by "_".
298 (lambda (x) (replace-regexp-in-string "[^A-Za-z0-9_@]" "_" x))
299 tags))
300
301 ;; Headline.
302 (insert
303 (format
304 "* %s [#%s] %s %s\n"
305 (if done "DONE" "TODO")
306 priority subject
307 (if tags (mapconcat 'identity (append '("") tags '("")) ":") "")))
308
309 ;; Submitted.
310 (when date
311 (insert
312 (format-time-string
313 " [%Y-%m-%d %a] Submitted\n" (seconds-to-time date))))
314
315 ;; Properties.
316 (insert " :PROPERTIES:\n")
317 (insert (format " :DEBBUGS_ID: %s\n" id))
318 (when merged
319 (insert
320 (format
321 " :MERGED_WITH: %s\n"
322 (if (numberp merged)
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)))
327 (insert " :END:\n")
328
329 ;; Messages.
330 (insert
331 " [[elisp:(debbugs-gnu-select-report)][Messages]]\n")
332
333 ;; Last modified.
334 (when last-modified
335 (insert
336 (format-time-string
337 " [%Y-%m-%d %a] Last modified\n"
338 (seconds-to-time last-modified))))
339
340 ;; Add text properties.
341 (add-text-properties beg (point) `(tabulated-list-id ,status))))
342
343 ;; The end.
344 (insert "* COMMENT Local " "Variables\n"
345 "# Local " "Variables:\n"
346 "# mode: org\n"
347 "# eval: (debbugs-org-mode 1)\n"
348 "# End:\n")
349 (goto-char (point-min))
350 (org-overview)
351 (set-buffer-modified-p nil)))
352
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)."
358 (save-excursion
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))))))
369
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)
375 map)
376 "Keymap for the `debbugs-org-mode' minor mode.")
377
378 ;; Make byte-compiler quiet.
379 (defvar gnus-posting-styles)
380
381 ;;;###autoload
382 (define-minor-mode debbugs-org-mode
383 "Minor mode for providing a debbugs interface in org-mode buffers.
384
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)
393 `((".*"
394 (eval
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))
406
407 ;;;###autoload
408 (defun debbugs-org-bugs (&rest bugs)
409 "List all BUGS, a list of bug numbers."
410 (interactive
411 (mapcar 'string-to-number
412 (completing-read-multiple "Bug numbers: " nil 'natnump)))
413 (dolist (elt bugs)
414 (unless (natnump elt) (signal 'wrong-type-argument (list 'natnump elt))))
415 (add-to-list 'debbugs-gnu-current-query (cons 'bugs bugs))
416 (debbugs-org nil))
417
418 ;; TODO
419
420 ;; - Refactor it in order to avoid code duplication with debbugs-gnu.el.
421 ;; - Make headline customizable.
422 ;; - Sort according to different TODO properties.
423
424 (provide 'debbugs-org)