Merge branch 'master' of ssh://repo.or.cz/srv/git/planner-el
[planner-el.git] / planner-trunk.el
blob7a945eda05cb8799f1f4773a4b7e9224fd16bf93
1 ;;; planner-trunk.el --- Trunk tasks for the Emacs planner
2 ;;
4 ;; Copyright (C) 2005, 2008 Dryice Dong Liu . All rights reserved.
5 ;; Parts copyright (C) 2005, 2008 Keith Amidon
6 ;; Parts copyright (C) 2005, 2008 Free Software Foundation, Inc.
8 ;; Keywords: emacs planner trunk group tasks
9 ;; Authors: Dryice Liu <dryice AT liu DOT com DOT cn>
10 ;; Keith Amidon <camalot AT picnicpark dot org>
11 ;; Description: trunk(group) tasks for the Emacs planner
13 ;; This file is part of Planner. It is not part of GNU Emacs.
15 ;; Planner is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 3, or (at your option)
18 ;; any later version.
20 ;; Planner is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with Planner; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
30 ;;; Commentary:
32 ;; This file provides `planner-trunk-tasks', which groups the tasks
33 ;; according to `planner-trunk-rule-list'. Please see the docstring
34 ;; for details. Remember to customize `planner-trunk-rule-list' before
35 ;; trying it out.
37 ;; It sorts and splits your tasks, adding a blank line between groups
38 ;; of tasks.
40 ;; WARNING: planner-trunk will delete *ALL* non-task lines from the
41 ;; tasks section of your plan page if it attempts to trunk
42 ;; the tasks. Do NOT use it if you want to preserve this
43 ;; information.
45 ;;; Things that would be nice to do:
47 ;; - Respect hidden outline sections when trunking and rehide after
48 ;; trunk is complete if they are present.
49 ;; - If point is in the tasks section, keep point on the same line.
50 ;; Maybe can do by saving entire line text and searching for it
51 ;; afterwards. Only problem is if it is whitespace only line. If
52 ;; so, maybe can move cursor to previous non-whitespace line? Point
53 ;; obviously shouldn't move if not in Tasks section.
55 ;;; Contributors:
57 ;; Keith Amidon worked on a number of aspects of this file.
59 ;; Sergey Vlasov contributed a fix that corrected regexp syntax and
60 ;; kept the "Tasks" string from being hard-coded.
62 ;;; Code:
64 (require 'planner)
66 ;;; USER VARIABLES -----------------------------------------------------------
68 (defgroup planner-trunk nil
69 "Grouping tasks for planner.el."
70 :prefix "planner-trunk"
71 :group 'planner)
73 (defcustom planner-trunk-rule-list
74 `(("\\`[0-9][0-9][0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9]\\'" nil
75 ("HomeWork" "WorkStuff"
76 "EmacsHack\\|PlannerHack")))
77 "List of rules for trunking tasks.
79 Each rule is a sublist of the form:
81 (PAGE-REGEXP COMPLETE-HEADING TRUNK-SECTIONS-LIST)
83 PAGE-REGEXP is used to select the set of trunk sections that should be
84 used. It is matched against the name of the current planner page. If
85 no matching PAGE-REGEXP is found, no trunking is done. If there is
86 more than one match, the first one in the list is used.
88 If COMPLETE-HEADING is nil, completed and not completed tasks will be
89 in the same trunk, sorted according to `planner-sort-tasks-key-function'.
90 If it is a string, it is the name of a sub-heading of the tasks
91 section under which to sort completed tasks separately, in which
92 case it will be the last subsection of the tasks section of the page.
94 Each element of TRUNK-SECTIONS-LIST describes a trunk of the page.
95 Elements can either be a simple TASK-PLAN-REGEXP, or a sublist of the form:
97 (TASK-PLAN-REGEXP TRUNK-HEADING)
99 The TASK-PLAN-REGEXP is matched against the plan page (or pages if you
100 are using planner-multi) for the task. If more than one
101 TASK-PLAN-REGEXP matches, the first one in the list is used. All
102 tasks that match the same TASK-PLAN-REGEXP will be grouped together.
103 The order of the groups will match the order of TRUNK-SECTIONS-LIST.
104 Tasks that do not match any entry in TRUNK-SECTIONS-LIST will be in a
105 separate group at the end of the tasks section. If the sublist form
106 of an entry is used, TRUNK-HEADING is a name for the outline heading
107 to be inserted at the top of the trunk. If TRUNK-SECTIONS-LIST
108 contains a mix of items in the regexp and sublist forms, when tasks
109 are present that match a regexp form entry, but no tasks are present
110 that match the most recent preceeding sublist form entry in the list,
111 the heading from the sublist form entry will be inserted anyway. In
112 use, it will become obvious why this is desirable."
113 :type '(repeat (list
114 :tag "Trunk rule"
115 (choice :tag "Page regexp"
116 (const "\\`[0-9][0-9][0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9]\\'"
117 :tag "Day pages")
118 (const "." :tag "All pages")
119 (regexp :tag "Regexp"))
120 (choice
121 :tag "Completed tasks"
122 (const :tag "With incomplete tasks" nil)
123 (string :tag "Under section heading"))
124 (repeat (choice (regexp :tag "Regexp")
125 (list
126 :tag "Regexp and section heading"
127 (regexp :tag "Regexp")
128 (string :tag "Section heading"))))))
129 :group 'planner-trunk)
131 (defcustom planner-trunk-tasks-before-hook nil
132 "Functions to run before doing the trunk."
133 :type 'hook
134 :group 'planner-trunk)
136 (defcustom planner-trunk-tasks-after-hook nil
137 "Functions to run after the trunk is done."
138 :type 'hook
139 :group 'planner-trunk)
141 ;;;_+ Internal variables and utility functions
143 (defun planner-trunk-rule-page-regexp (rule)
144 "Regular expression matching the page in RULE."
145 (elt rule 0))
147 (defun planner-trunk-rule-completed-heading (rule)
148 "Sub-heading for completed tasks in RULE."
149 (elt rule 1))
151 (defun planner-trunk-rule-trunk-sections (rule)
152 "Trunk section in RULE."
153 (elt rule 2))
155 (defun planner-trunk-list-regexp (trunk)
156 "Plan page regular expression for TRUNK."
157 (if (listp trunk)
158 (car trunk)
159 trunk))
161 (defun planner-trunk-list-heading (trunk)
162 "Heading for TRUNK."
163 (if (listp trunk)
164 (cadr trunk)
165 nil))
167 (defun planner-trunk-task-plan-str (task-info)
168 "Return plan string for TASK-INFO."
169 (or
170 (if (fboundp 'planner-multi-task-link-as-list)
171 (mapconcat 'identity
172 (planner-multi-task-link-as-list task-info) " ")
173 (or (planner-task-link task-info)
174 (planner-task-plan task-info)))
175 ""))
177 (defun planner-trunk-completed-p (task-info)
178 "Return non-nil if TASK-INFO is a completed task."
179 (or (equal (planner-task-status task-info) "X")
180 (equal (planner-task-status task-info) "C")))
182 (defun planner-trunk-delete-all-blank-lines ()
183 "Delete all blank lines and insert one at the end."
184 (goto-char (point-min))
185 (delete-blank-lines)
186 (while (= (forward-line 1) 0)
187 (delete-blank-lines))
188 (insert "\n"))
190 (defun planner-trunk-delete-line-if-not-task ()
191 "Delete the current line if it is not a task."
192 (if (planner-current-task-info)
193 (not (equal (forward-line) 1))
194 (let ((bol (planner-line-beginning-position)))
195 (let ((at-end (equal (forward-line) 1)))
196 (beginning-of-line)
197 (delete-region bol (point))
198 (not at-end)))))
200 (defun planner-trunk-delete-non-task-lines ()
201 "Delete all lines that are not tasks. DANGEROUS."
202 (goto-char (point-min))
203 (forward-line) ; Skip Tasks heading
204 ;; (keep-lines "^#[A-C][0-9]*\\s-+.\\s-") or
205 (while (planner-trunk-delete-line-if-not-task))
206 (insert "\n")
207 (forward-line -1))
209 (defun planner-trunk-sort-tasks (rule)
210 "Sort tasks by plan name according to the given RULE list."
211 (let ((trunk-list (planner-trunk-rule-trunk-sections rule))
212 (completed-heading (planner-trunk-rule-completed-heading rule))
213 (task-info (planner-current-task-info)))
214 (let ((trunk-count (length trunk-list))
215 (plan (planner-trunk-task-plan-str task-info))
216 (task-completed (planner-trunk-completed-p task-info)))
217 (if (not plan)
218 (+ 2 (if (and completed-heading task-completed)
219 (* 2 trunk-count)
220 trunk-count))
221 (catch 'done
222 (let ((count 1))
223 (when (and completed-heading task-completed)
224 (setq count (+ count trunk-count 2)))
225 (mapc
226 (lambda (trunk-entry)
227 (let ((plan-regexp (planner-trunk-list-regexp trunk-entry)))
228 (if (string-match plan-regexp plan)
229 (throw 'done count)
230 (setq count (1+ count)))))
231 trunk-list)
232 count))))))
234 (defun planner-trunk-ins-heading (completed-heading task-info heading)
235 "Insert the task heading.
236 If COMPLETED-HEADING is non-nil and TASK-INFO is a completed task,
237 use COMPLETED-HEADING instead of HEADING."
238 (when heading
239 (insert "\n")
240 (when (and completed-heading
241 (planner-trunk-completed-p task-info))
242 (insert "*"))
243 (insert "** " heading))
244 (insert "\n"))
246 (defun planner-trunk-do-trunk-section (rule)
247 "Really do the trunk.
249 Adds new lines and optionally outline mode subheadings according to
250 the trunk RULE. Point must be at the beginning of the section to
251 trunk, typically either the beginning of the tasks section or the
252 beginning of the completed subsection."
253 (let ((not-done t)
254 (completed-hdr (planner-trunk-rule-completed-heading rule))
255 ;; Following adds a dummy first entry to get rid of special
256 ;; case to handle headings otherwise. It prevents anyone from
257 ;; having a plan page named (_-), which I hope no-one wants to
258 ;; do...
259 (trunk-list (cons "^\\\\(_-\\\\)$"
260 (planner-trunk-rule-trunk-sections rule)))
261 (first-trunk (car (planner-trunk-rule-trunk-sections rule)))
262 (ntasks 0))
263 (while (and trunk-list not-done)
264 (let ((task-info (planner-current-task-info)))
265 (when task-info
266 (setq ntasks (1+ ntasks))
267 (let ((plan (planner-trunk-task-plan-str task-info))
268 (plan-regexp (planner-trunk-list-regexp (car trunk-list))))
269 (unless (string-match plan-regexp plan)
270 (let ((hdr nil))
271 (while (and trunk-list
272 (not (string-match plan-regexp plan)))
273 (setq trunk-list (cdr trunk-list))
274 (setq plan-regexp
275 (planner-trunk-list-regexp (car trunk-list)))
276 (when (planner-trunk-list-heading (car trunk-list))
277 (setq hdr
278 (planner-trunk-list-heading (car trunk-list)))))
279 (when (planner-trunk-list-heading (car trunk-list))
280 (setq hdr (planner-trunk-list-heading (car trunk-list))))
281 (planner-trunk-ins-heading completed-hdr task-info hdr)))))
282 (when (or (null trunk-list)
283 (not (equal 0 (forward-line 1)))
284 (and completed-hdr
285 (not (planner-trunk-completed-p task-info))
286 (planner-trunk-completed-p (planner-current-task-info))))
287 (setq not-done nil))))
288 ntasks))
290 (defun planner-trunk-do-trunk (rule)
291 "Really do the trunk following RULE."
292 (goto-char (point-min))
293 (planner-trunk-do-trunk-section rule)
294 (when (planner-trunk-rule-completed-heading rule)
295 (while (let ((task-info (planner-current-task-info)))
296 (and (not (planner-trunk-completed-p task-info))
297 (equal 0 (forward-line)))))
298 (let ((start-completed-pos (point)))
299 (when (> (planner-trunk-do-trunk-section rule) 0)
300 (when (stringp (planner-trunk-rule-completed-heading rule))
301 (goto-char start-completed-pos)
302 (insert "\n** " (planner-trunk-rule-completed-heading rule) "\n"))))))
304 ;; user visible functions
306 ;;;###autoload
307 (defun planner-trunk-tasks (&optional force)
308 "Trunk(group) tasks in the current page.
309 Please refer the docstring of `planner-trunk-rule-list' for how
310 it works. You may want to call this function before you sort tasks
311 and/or after you create new tasks. If a prefix is given or FORCE is not
312 nil, trunk completed tasks together with non-completed tasks not
313 matter what the `planner-trunk-rule-list' said."
314 (interactive "P")
315 (let ((page-name (planner-page-name))
316 (rule-list planner-trunk-rule-list))
317 (let ((rule (catch 'done
318 (while rule-list
319 (if (string-match (caar rule-list) page-name)
320 (throw 'done (car rule-list))
321 (setq rule-list (cdr rule-list))))
322 nil)))
323 (if rule
324 (save-excursion
325 (save-restriction
326 (run-hooks 'planner-trunk-tasks-before-hook)
327 (when (planner-narrow-to-section 'tasks)
328 (planner-trunk-delete-non-task-lines)
329 (if force
330 (setq rule
331 (list (planner-trunk-rule-page-regexp rule)
333 (planner-trunk-rule-trunk-sections rule))))
334 (let ((planner-sort-tasks-key-function
335 (lambda ()
336 (planner-trunk-sort-tasks rule))))
337 (planner-sort-tasks))
338 (planner-trunk-do-trunk rule))
339 (run-hooks 'planner-trunk-tasks-after-hook)))))))
341 (provide 'planner-trunk)
343 ;;; planner-trunk.el ends here