Fix #11948. Narrow to tasks section when carrying tasks forward.
[planner-el.git] / planner.el
blob94a73ceb0bb7514fc664028909c06de99642a73d
1 ;;; planner.el --- The Emacs Planner
3 ;;; Commentary:
5 ;;;_* Commentary
7 ;;;_ + Package description
9 ;; Copyright (C) 2001, 2003, 2004, 2005,
10 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
11 ;; Parts copyright (C) 2004, 2008 David D. Smith (davidsmith AT acm DOT org)
12 ;; Parts copyright (C) 2004, 2008 Yvonne Thomson (yvonne AT netbrains DOT com DOT au)
13 ;; Parts copyright (C) 2004, 2008 Maciej Kalisak (mac AT cs DOT toronto DOT edu)
14 ;; Parts copyright (C) 2004, 2008 Chris Parsons (chris.p AT rsons.org)
15 ;; Parts copyright (C) 2004, 2008 Dirk Bernhardt (nospam AT krid.de)
16 ;; Parts copyright (C) 2005, 2008 Dryice Dong Liu
17 ;; Parts copyright (C) 2005, 2008 Angus Lees (gus AT debian.org)
18 ;; Parts copyright (C) 2005, 2008 Sergey Vlasov (vsu AT altlinux.ru)
19 ;; Parts copyright (C) 2005, 2008 Yann Hodique (hodique AT lifl DOT fr)
20 ;; Parts copyright (C) 2005, 2008 Peter K. Lee
21 ;; Parts copyright (C) 2006, 2007 Software Freedom Law Center
23 ;; Emacs Lisp Archive Entry
24 ;; Filename: planner.el
25 ;; Version: 3.42
26 ;; Keywords: hypermedia
27 ;; Author: John Wiegley <johnw@gnu.org>
28 ;; Maintainer: John Sullivan <john@wjsullivan.net>
29 ;; Description: Use Emacs for life planning
30 ;; URL: http://www.wjsullivan.net/PlannerMode.html
31 ;; Bugs: https://gna.org/bugs/?group=planner-el
32 ;; Compatibility: Emacs20, Emacs21, Emacs22, XEmacs21
34 ;; This file is part of Planner. It is not part of GNU Emacs.
36 ;; Planner is free software; you can redistribute it and/or modify it
37 ;; under the terms of the GNU General Public License as published by
38 ;; the Free Software Foundation; either version 3, or (at your option)
39 ;; any later version.
41 ;; Planner is distributed in the hope that it will be useful, but
42 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
43 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 ;; General Public License for more details.
46 ;; You should have received a copy of the GNU General Public License
47 ;; along with Planner; see the file COPYING. If not, write to the
48 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
49 ;; Boston, MA 02110-1301, USA.
51 ;;;_ + Usage
53 ;; Place planner.el in your load path and add this to your .emacs:
55 ;; (require 'planner)
57 ;; By default and for backward compatibility, the following operations
58 ;; do not have keybindings, and are only accessible from the Planner
59 ;; menu:
61 ;; planner-copy-or-move-region
62 ;; planner-delete-task
63 ;; planner-task-delegated
64 ;; planner-task-pending
65 ;; planner-fix-tasks
67 ;; You may find it easier to install keybindings for those operations by
68 ;; inserting the following in your .emacs file:
70 ;; ;; Note: This changes some of the default key bindings for planner-mode
71 ;; (planner-install-extra-task-keybindings)
73 ;; If you want to change `planner-directory' and some other variables,
74 ;; either use Customize or use `setq'. For example:
76 ;; (setq planner-directory "~/Plans")
78 ;; You can customize Planner. M-x customize-group RET planner RET
79 ;; or see the Options section.
81 ;;; Note:
83 ;; This package extends Emacs Muse to act as a day planner, roughly
84 ;; equivalent to the one used by Franklin-Covey. If they have patents
85 ;; and trademarks and copyrights to prevent me even thinking in terms
86 ;; of their methodology, then I can't believe they care at all about
87 ;; productivity.
89 ;;;_ + Summary
91 ;; * Make a planning file
93 ;; Open a wiki file within your planning directory. By default,
94 ;; planner-directory is set to "~/Plans". You may have to use C-x C-f
95 ;; to open the file.
97 ;; A plan file generally describes a long-term plan. For example, you
98 ;; could make a plan file for your ThesisProject or your
99 ;; ContinuousLearning. Planner.el can help you organize related ideas,
100 ;; tasks and resources into a coherent plan.
102 ;; * Break your plan into stages
104 ;; Start the file with your "vision", or the goal you intend to
105 ;; accomplish. Break this up into parts, and create a Wiki file for
106 ;; each part, with defined milestones which constitute the "goal" for
107 ;; that part.
109 ;; * Write out the tasks for each stage
111 ;; In each sub-plan, list out the tasks necessary to accomplish the
112 ;; milestone. Write them into the file like this:
114 ;; #A _ 1h Call so and so to make a reservation
116 ;; * Decide on a priority for each task
118 ;; The A is the priority of the task. The _ means it isn't done yet,
119 ;; and the 1h is a quick estimate on how long it will task. The time
120 ;; estimates are optional.
122 ;; The priorities break down like this:
124 ;; A: if you don't do it, your plan will be compromised, and you
125 ;; will have to either abort, backtrack, or make profuse apologies
126 ;; to someone
128 ;; B: if you don't do it, your plan will be delayed
130 ;; C: the plan won't be complete until it's done, but there's no
131 ;; pressure to do it now
133 ;; * Schedule the tasks
135 ;; Put your cursor on a line containing a task, and type C-c C-c.
136 ;; This will copy the task to a specific day, which you will be
137 ;; prompted for. The Emacs Calendar pops up, so you can pick a free
138 ;; day (if you use the Emacs diary and appointment system, the
139 ;; Calendar is even more useful).
141 ;; You will now see your new task, with a link back to your planning
142 ;; page. Selecting this link will take you back to that task on the
143 ;; planning page, where you will see that the planning page's task now
144 ;; has a link to the particular day you scheduled the task for.
146 ;; The two tasks (the one on the planning page, and the one on the
147 ;; daily task list) are linked. Changing the status of one (using C-c
148 ;; C-x, or C-c C-s, for example) will change the status of the other.
149 ;; If you forward the task to another day (using C-c C-c on the daily
150 ;; task page), the planning page's link will be updated to refer to
151 ;; the new day. This is so that you can focus on your daily task list
152 ;; during the day, but see an overview of your plan's progress at any
153 ;; time.
155 ;; * Do the work
157 ;; That's it, as far as what planner.el can do. As you complete tasks
158 ;; each day, they will disappear from view. This only happens for
159 ;; today's completed and forwarded tasks.
161 ;; Planning is an art, just as estimating time is an art. It happens
162 ;; with practice, and by thinking about these things. The Commentary
163 ;; below provides a few of my own thoughts on the matter, although I
164 ;; will say that this an art I have yet to truly develop.
166 ;; The `COMMENTARY' file has John Wiegley's original commentary.
168 ;;;_ + And now back to technical matters
170 ;; In order to refresh and renumber all of your tasks according to their
171 ;; actual order in the buffer, simply save the file or call
172 ;; M-x planner-fix-tasks .
174 ;; Here is a summary of the keystrokes available, including a few I
175 ;; did not mention:
177 ;; M-x plan Begin your planning session. This goes to the last
178 ;; day for which there is any planning info (or today if
179 ;; none), allowing you to review, and create/move tasks
180 ;; from that day.
182 ;; C-M-p Raise a task's priority
183 ;; C-M-n Lower a task's priority
185 ;; C-c C-s Mark the task as in progress or delegated
186 ;; C-c C-x Mark the task as finished
188 ;; C-c C-t Create a task associated with the current Wiki page
189 ;; If you are on the opening line of a Note entry, it is
190 ;; assume that the note itself is the origin of the task.
191 ;; C-c C-c Move or copy the current task to another date
192 ;; If the current task is an original (meaning you are in
193 ;; the buffer where's defined, hopefully a planning page)
194 ;; then it will be copied, and the original task will also
195 ;; now point to the copy. If the current task is a copy,
196 ;; it will just be moved to the new day, and the original
197 ;; tasks link will be updated.
199 ;; C-c C-n Jump to today's task page
201 ;; If you call (planner-calendar-insinuate), typing 'n' in the Emacs
202 ;; calendar will jump to today's task page.
204 ;;;_ + Planning and schedules
206 ;; Sometimes you will have appointments during the day to schedule,
207 ;; which "block out" time that might otherwise be spent on tasks.
208 ;; Users are encouraged to use the Emacs Calendar for this, along with
209 ;; Diary Mode (see the Emacs manual)
212 ;; However, there is a way to do scheduling directly in planner-mode.
213 ;; It requires the external tool "remind" (Debian users type "apt-get
214 ;; install remind". All others go to
215 ;; http://www.roaringpenguin.com/penguin/open_source_remind.php)
217 ;; Once you have remind installed, you will need two scripts in your
218 ;; local bin directory (/usr/local/bin, $HOME/bin, wherever). These
219 ;; scripts can be downloaded from my web site:
221 ;; http://sacha.free.net.ph/notebook/emacs/plan2rem
222 ;; http://sacha.free.net.ph/notebook/emacs/rem2diary
224 ;; Also, download
226 ;; http://sacha.free.net.ph/notebook/emacs/remind.el
228 ;; and put it somewhere in your load path. Take a look at remind.el
229 ;; for more details. You will need to edit a few things to get it
230 ;; to work.
232 ;; Lastly, here is another snippet for your .emacs file. It creates a
233 ;; keybinding in planner-mode, C-c C-w, which jumps you to the
234 ;; Schedule section of that file.
236 ;; (defun planner-goto-schedule ()
237 ;; (interactive)
238 ;; (goto-char (point-min))
239 ;; (unless (re-search-forward "^\\* Schedule\n\n" nil t)
240 ;; (re-search-forward "^\\* Notes")
241 ;; (beginning-of-line)
242 ;; (insert "* Schedule\n\n\n\n")
243 ;; (forward-line -2)))
245 ;; (eval-after-load "planner"
246 ;; '(progn
247 ;; (define-key planner-mode-map [(control ?c) (control ?w)]
248 ;; 'planner-goto-schedule)))
250 ;; The contents of a scheduling section look like this, which is
251 ;; rendered in HTML as a table:
253 ;; * Schedule
255 ;; 8:00 | Wake up
256 ;; 14:00 | Go to the dentist (2:00)
257 ;; 18:00 | Watch TV
259 ;; The start time is given in 24-hour time, with an optional duration
260 ;; occuring in parentheses at the end of the description hs-show(in
261 ;; HOURS:MINUTES). And off you go!
263 ;; You can also organize this as
265 ;; 8:00 | 8:30 | Wake up
266 ;; 14:00 | 16:00 | Go to the dentist
267 ;; 18:00 | 21:00 | Watch TV
269 ;;;_ + Example planning file
271 ;; The format of a planning file is given below. You are responsible
272 ;; for keeping it looking like this. I intentionally did not make
273 ;; planner.el heavy on the UI side of things, too keep it more
274 ;; free-form and open. This lets you adapt it to whatever your
275 ;; particular preferences might be.
277 ;;----------------------------------------------------------------------
278 ;; * Tasks
280 ;; #A1 _ An open task, very important!
281 ;; #A2 X A closed task (MyPlan)
282 ;; #A3 o A task that's delayed, or delegated (MyPlan)
284 ;; * Notes
286 ;; .#1 This is note number one
288 ;; Notes on note number one!
290 ;; .#2 This weird ".#2" syntax is used because it's what allout.el
291 ;; likes for enumerated lists, and it makes using
292 ;; outline-minor-mode (with allout) very handy. You can omit the
293 ;; leading period if you like, though. It's optional.
295 ;; ----------------------------------------------------------------------
297 ;;;_ + Other packages that come with the Planner distribution
299 ;; planner-bbdb.el | Link to your contacts
300 ;; planner-diary.el | Thomas Gehrlein's diary integration
301 ;; planner-gnus.el | Link to your mail/news messages
302 ;; planner-id.el | Automatically add unique task IDs
303 ;; planner-notes.el | Create a note index
304 ;; planner-rss.el | Publish your notes as an RSS feed
305 ;; planner-schedule.el | Estimate task completion time
306 ;; planner-timeclock.el | Clock in and clock out
307 ;; planner-w3m.el | Make tasks based on W3M buffers
308 ;; remember.el | Easily remember short notes
310 ;;;_ + Thanks
312 ;; A short, partial list of contributors, those who reported bugs, and
313 ;; those who gave valuable suggestions can be found at
314 ;; http://sacha.free.net.ph/notebook/wiki/PlannerMode.php
316 ;;;_ + Contributors
318 ;; David D. Smith (davidsmith AT acm DOT org) helped links to planner
319 ;; pages be created properly, among other things.
321 ;; Frederik Fouvry fixed a match error by using grouping.
323 ;; Daniel Neri (dne AT mayonnaise DOT net) fixed a couple of typos.
325 ;; Mario Peter (email address unknown) made
326 ;; `planner-in-progress-task-face' use :bold instead of :slant if
327 ;; using XEmacs.
329 ;; Yvonne Thomson (yvonne AT netbrains DOT com DOT au) contributed
330 ;; `planner-annotation-from-info'.
332 ;; Hoan Ton-That (hoan AT ton-that DOT org) had the idea to strip the
333 ;; directory from planner file annotations and contributed the base
334 ;; patch.
336 ;; Michael Olson (mwolson AT gnu DOT org) contributed XHTML 1.1
337 ;; patches, fixed some bugs that irked him, and did a few other
338 ;; miscellaneous things.
340 ;; Maciej Kalisiak (mac AT cs DOT toronto DOT edu) made a patch that
341 ;; sorts dated tasks before undated ones. Maciej also helped with the
342 ;; separation of the sorting and renumbering processes.
344 ;; Dale P. Smith (dsmich AT adelphia DOT net) contributed a small
345 ;; patch that fixes tasks that are not true wiki names.
347 ;; Stefan Reichör (stefan AT xsteve DOT at) contributed a small patch
348 ;; that saves only modified buffers, and some other patches as well.
350 ;; Chris Parsons made it so that C-u means put note on plan page.
352 ;; Dirk Bernhardt contributed a patch that added the
353 ;; `planner-default-tasks-status' option.
355 ;; Jim Ottaway provided several bugfixes.
357 ;; Dryice Dong Liu made the place to put the annotation in the task
358 ;; description configurable.
360 ;; Angus Lees provided a patch to make planner-sort-tasks stop causing
361 ;; backtraces.
363 ;; Yann Hodique (hodique AT lifl DOT fr) fixed a number of problems
364 ;; with the Muse port of Planner.
366 ;; Peter K. Lee (saint AT corenova DOT com) fixed a few initial errors
367 ;; with missing and malformed functions like `planner-page-exists-p'
368 ;; and `planner-option-customized'
370 ;; Romain Francoise improved match data handling in
371 ;; `planner-browse-position-url'.
373 ;; Win Treese fixed a bug in `planner-save-buffers'.
375 ;; Sven Kloppenburg fixed a regexp.
377 ;; Sergey Vlasov fixed several bugs.
379 ;; Marco Gidde provided a patch that allows Planner to visit a link to
380 ;; a temporary file by visiting its buffer.
382 ;; Trent Buck made things work better when day pages are disabled.
384 ;; Andrew J. Korty made it so that task padding is only used if
385 ;; `planner-use-task-numbers' is non-nil.
387 ;;; Code:
389 ;;;_* Prerequisites
391 (require 'muse-colors)
392 (require 'muse-mode)
393 (require 'muse-project)
394 (require 'muse-html)
395 (require 'sort)
396 (require 'calendar)
397 (require 'font-lock)
398 (require 'info)
399 (require 'easymenu)
400 (condition-case nil
401 (require 'derived)
402 (error nil))
403 (condition-case nil
404 (require 'overlay)
405 (error nil))
407 (defvar planner-loaded nil)
408 (defvar planner-version "3.42"
409 "The version of Planner currently loaded.")
411 ;; Compatibility hacks -- these will be removed in the future
413 (defun planner-update-wiki-project ()
414 ;; do nothing
416 (defvar planner-markup-tags nil)
418 (defun planner-option-customized (sym val)
419 "Set SYM to VAL and update the WikiPlanner project."
420 (set sym val)
421 (when planner-loaded
422 (planner-update-wiki-project)))
424 (defun planner-split-string (string &optional separators omit-nulls)
425 ;; OMIT-NULLS was added in Emacs 22. Remove this when we stop supporting
426 ;; anything before 22, and replace all uses with `split-string'.
427 (let ((res (split-string string separators)))
428 (if omit-nulls
429 (delete "" res)
430 res)))
432 ;;;_* Options
434 (defgroup planner nil
435 "A personal information manager for Emacs."
436 :prefix "planner-"
437 :group 'applications)
439 (defcustom planner-project "WikiPlanner"
440 "The name of this project in `muse-project-alist'."
441 :type 'string
442 :group 'planner)
444 (defcustom planner-initial-page "WelcomePage"
445 "The name of the root plan page that `plan' will find when not
446 using day pages. If you are using day pages (the default), this
447 option is not used."
448 :type 'string
449 :group 'planner)
451 (defcustom planner-publish-dates-first-p nil
452 "Non-nil means put day pages at the top of the index."
453 :type 'boolean
454 :group 'planner)
456 (defcustom planner-use-day-pages t
457 "If non-nil, allow the use of day pages.
458 You can set this to nil if you use plan pages exclusively and
459 don't want to be prompted for dates. If so, then `plan' will
460 bring up the `planner-initial-page' of your planner wiki."
461 :type 'boolean
462 :group 'planner)
464 (defcustom planner-use-plan-pages t
465 "If non-nil, allow the use of plan pages.
466 You can set this to nil if you use day pages exclusively and
467 don't want to be prompted for plans."
468 :type 'boolean
469 :group 'planner)
471 (defcustom planner-mode-hook nil
472 "A hook for Planner mode."
473 :type 'hook
474 :group 'planner)
476 (defcustom planner-annotation-functions
477 '(planner-annotation-from-planner-note
478 planner-annotation-from-planner
479 planner-annotation-from-wiki
480 planner-annotation-from-dired
481 planner-annotation-from-info
482 planner-annotation-from-man
483 planner-annotation-from-woman
484 planner-annotation-from-file-with-position)
485 "Functions tried in order by `planner-create-task-from-buffer'.
486 To change the behavior of `planner-create-task-from-buffer',
487 remove, change the order of, or insert functions in this list."
488 :type 'hook
489 :group 'planner)
491 (defcustom planner-annotation-symbol-string "{}"
492 "The string to be replaced by annotation from `planner-annotation-functions'.
493 If nil or not found in the task title, the annotation will be
494 added to the end."
495 :type 'string
496 :group 'planner)
498 (defcustom planner-use-other-window t
499 "If non-nil, Planner will open planner files in another window."
500 :type 'boolean
501 :group 'planner)
503 (defcustom planner-show-only-existing t
504 "If non-nil, `planner-show' only shows existing files."
505 :type 'boolean
506 :group 'planner)
508 (defcustom planner-reverse-chronological-notes t
509 "*If non-nil, notes are added to the beginning of the section."
510 :type 'boolean
511 :group 'planner)
513 (defcustom planner-create-section-function 'planner-create-at-top
514 "Called when creating a new section.
515 Some functions you can use are `planner-create-at-top' and
516 `planner-create-at-bottom'."
517 :type 'function
518 :group 'planner)
520 (defcustom planner-template-fuzz-factor 5
521 "Controls the fuzziness of `planner-page-default-p'.
522 Right now, this is the number of additional characters over
523 `planner-day-page-template' allowed in a buffer before
524 `planner-page-default-p' assumes it has been modified."
525 :type 'integer
526 :group 'planner)
528 (defcustom planner-calendar-show-planner-files t
529 "If non-nil, shows a plan file every time a day is selected in Calendar."
530 :type 'boolean
531 :group 'planner)
533 (defcustom planner-day-page-template
534 "* Tasks\n\n\n* Schedule\n\n\n* Notes\n\n\n"
535 "Template to be inserted into blank daily pages.
536 If this is a string, it will be inserted into the blank page. If
537 this is a function, it will be called with no arguments from a
538 blank planner page and should insert the template.
540 If you want to change the name of special sections like Tasks and Notes,
541 update the `planner-sections' option as well."
542 :type '(choice
543 (string :tag "Template")
544 (function :tag "Function"))
545 :group 'planner)
547 (defcustom planner-plan-page-template "* Tasks\n\n\n* Notes\n\n\n"
548 "Template to be inserted into blank plan pages.
549 If this is a string, it will be inserted into the blank page. If
550 this is a function, it will be called with no arguments from a
551 blank planner page and should insert the template.
553 If you want to change the name of special sections like Tasks and Notes,
554 update the `planner-sections' option as well."
555 :type '(choice
556 (string :tag "Template")
557 (function :tag "Function"))
558 :group 'planner)
560 (defcustom planner-default-section 'tasks
561 "Default section when you use `planner-goto' to open a page.
562 If this is a string, it should be a section name. If this is a symbol,
563 the section name is looked up in `planner-sections'."
564 :type '(choice (string :tag "String")
565 (symbol :tag "Symbol"))
566 :group 'planner)
568 (defcustom planner-sections '((tasks . "Tasks")
569 (notes . "Notes"))
570 "Special sections in pages.
571 This option makes it easier to change the names of your sections
572 without modifying a lot of Planner code. If you change this, you
573 may also want to change `planner-day-page-template' and
574 `planner-plan-page-template'. You normally don't need to change
575 these, though."
576 :type '(alist :key symbol :value string)
577 :group 'planner)
579 (defcustom planner-ignored-from-addresses
580 (and user-mail-address
581 (not (string= user-mail-address ""))
582 (regexp-quote user-mail-address))
583 "Regexp of From headers that may be suppressed in favor of To headers."
584 :group 'planner
585 :type 'regexp)
587 (defcustom planner-dates-relative-to-today-flag nil
588 "Non-nil means relative dates (+1, -1) are always based on today.
589 By default, dates are based on the current page."
590 :group 'planner
591 :type 'boolean)
593 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
594 ;; Task options
595 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
597 (defgroup planner-tasks nil
598 "Planner options related to tasks."
599 :prefix "planner-"
600 :group 'planner)
602 ; Note that if you add another choice other than '.' or '-' here, you'll need
603 ; to grep through the code for [\\.\\-] and replace it with a regex that matches
604 ; all possible date-separators. I thought about making a variable that builds
605 ; all the regexes used elsewhere, but it made the code messy, it seemed, and
606 ; I didn't imagine too many other separators people would want. Myabe '/', but
607 ; that would cause it's own world of hurt. -- bkuhn, 2007-01-09
609 (defcustom planner-date-separator "."
610 "This character will be used to separate year, month and day numbers
611 when formatting dates"
612 :type '(choice
613 (const :tag "Dates should be in the form YYYY.MM.DD" ".")
614 (const :tag "Dates should be in the form YYYY-MM-DD" "-"))
615 :group 'planner)
617 (defcustom planner-carry-tasks-forward 3
618 "If non-nil, carry unfinished tasks forward automatically.
619 If a positive integer, scan that number of days in the past.
620 If 0, scan all days for unfinished tasks.
621 If t, scan one day in the past (old behavior).
622 If nil, do not carry unfinished tasks forward."
623 :type '(choice
624 (const :tag "Scan all days" 0)
625 (const :tag "Scan most recent day" t)
626 (const :tag "Do not carry tasks forward" nil)
627 (integer :tag "Number of days to scan"))
628 :group 'planner-tasks)
630 (defcustom planner-marks-regexp "[_oXDCP]"
631 "Regexp that matches status character for a task."
632 :type 'regexp
633 :group 'planner-tasks)
635 (defcustom planner-default-task-priority "B"
636 "Default priority for new tasks created with `planner-create-task'."
637 :type 'string
638 :group 'planner-tasks)
640 (defcustom planner-default-task-status "_"
641 "Default status for new tasks created with `planner-create-task'."
642 :type 'string
643 :group 'planner-tasks)
645 (defcustom planner-add-task-at-end-flag nil
646 "*Non-nil means create tasks at the bottom of the first task block."
647 :group 'planner-tasks
648 :type 'boolean)
650 ;;; Muse
652 ;;;_* Keybindings
655 (defvar planner-mode-map
656 (let ((map (make-sparse-keymap)))
657 (cond
658 ;; XEmacs
659 ((featurep 'xemacs)
660 (set-keymap-parents map muse-mode-map))
661 ;; Emacs
662 ((fboundp 'set-keymap-parent)
663 (set-keymap-parent map muse-mode-map))
664 ;; if we can't inherit the keymap, copy it instead
665 (t (setq map (copy-keymap muse-mode-map))))
666 (define-key map "\C-c\C-n" 'planner-goto-today)
667 ;; moving between daily pages C-c C-j for goto (used to be C-g,
668 ;; but that was confusing)
669 (define-key map "\C-c\C-j\C-d" 'planner-goto) ; goto date
670 (when planner-use-day-pages
671 (define-key map "\C-c\C-j\C-p" 'planner-goto-previous-daily-page)
672 (define-key map "\C-c\C-j\C-n" 'planner-goto-next-daily-page)
673 (define-key map "\C-c\C-j\C-j" 'planner-goto-today) ; for easy typing
674 (define-key map "\C-c\C-j\C-y" 'planner-goto-yesterday)
675 (define-key map "\C-c\C-j\C-t" 'planner-goto-tomorrow)
676 (define-key map "\C-c\C-j\C-r" 'planner-goto-most-recent)) ; recent
678 (define-key map "\C-c\C-t" 'planner-create-task-from-buffer)
679 (define-key map "\C-c\C-c" 'planner-copy-or-move-task)
680 (define-key map "\C-c\C-u" 'planner-raise-task)
681 (define-key map "\C-c\C-d" 'planner-lower-task)
683 (define-key map "\M-p" 'planner-raise-task)
684 (define-key map "\M-n" 'planner-lower-task)
686 (define-key map "\M-\C-p" 'planner-raise-task-priority)
687 (define-key map "\M-\C-n" 'planner-lower-task-priority)
689 (define-key map "\C-c\C-z" 'planner-task-in-progress)
690 (define-key map "\C-c\C-x" 'planner-task-done)
691 (define-key map '[(control ?c) (control ?X)] 'planner-task-cancelled)
692 map)
693 "Keymap used by Planner mode.")
695 (defun planner-install-extra-context-keybindings ()
696 "Install extra context-sensitive keybindings.
697 These keybindings conflict with windmove.el, but might
698 be useful.
700 On a task or note, the following keys will move around:
702 Shift-up: `planner-move-up'
703 Shift-down: `planner-move-down'
704 Shift-right: `planner-jump-to-link'"
705 (interactive)
706 (let ((map planner-mode-map))
707 (define-key map [(shift up)] 'planner-move-up)
708 (define-key map [(shift down)] 'planner-move-down)
709 (define-key map [(shift right)] 'planner-jump-to-link)))
711 ;;; Additional keybindings thanks to Thomas Gehrlein
713 (defun planner-install-extra-task-keybindings ()
714 "Install additional task key bindings.
715 Warning! Overwrites some standard key bindings. See function
716 definition for keys added."
717 (let ((map planner-mode-map))
718 (define-key map "\C-c\C-t" nil)
719 (define-key map "\C-c\C-t\C-t" 'planner-create-task-from-buffer)
720 (define-key map "\C-c\C-t\C-k" 'planner-delete-task)
721 (define-key map "\C-c\C-t\C-u" 'planner-update-task)
722 (define-key map "\C-c\C-t\C-c" 'planner-copy-or-move-task)
723 (define-key map '[(control ?c) (control ?t) (control ?C)]
724 'planner-copy-or-move-region)
725 (define-key map "\C-c\C-t\C-x" 'planner-task-done)
726 (define-key map '[(control ?c) (control ?t) (control ?X)]
727 'planner-task-cancelled)
728 (define-key map "\C-c\C-t\C-d" 'planner-task-delegated)
729 (define-key map "\C-c\C-t\C-p" 'planner-task-pending)
730 (define-key map "\C-c\C-t\C-o" 'planner-task-in-progress)
731 (define-key map "\C-c\C-t\C-r" 'planner-raise-task)
732 (define-key map "\C-c\C-t\C-l" 'planner-lower-task)
733 (define-key map "\C-c\C-t\C-n" 'planner-fix-tasks)))
735 ;;; We need some keybindings for note-related functions, too
737 (defun planner-install-extra-note-keybindings ()
738 "Install additional note-related key bindings.
739 See function definition for keys added."
740 (let ((map planner-mode-map))
741 (define-key map "\C-c\C-o" nil)
742 (define-key map "\C-c\C-o\C-o" 'planner-create-note)
743 (define-key map "\C-c\C-o\C-s" 'planner-search-notes)
744 (define-key map "\C-c\C-o\C-b" 'planner-search-notes-with-body)
745 (define-key map "\C-c\C-o\C-n" 'planner-renumber-notes)))
747 ;;;_* Menu
749 ;;; Menu thanks to Thomas Gehrlein
750 (easy-menu-define planner-menu planner-mode-map
751 "Menu of planner mode.
752 See `planner-install-extra-task-keybindings' for additional bindings
753 you can use."
754 (list
755 "Planner"
756 ;; moving between day plan pages
757 (if planner-use-day-pages
758 '("Goto"
759 ["Plan page" planner-goto-plan-page]
760 ["Date" planner-goto]
761 ["Previous page" planner-goto-previous-daily-page]
762 ["Next page" planner-goto-next-daily-page]
763 ["Today" planner-goto-today]
764 ;; do the next two make sense in a menu?
765 ["Yesterday" planner-goto-yesterday]
766 ["Tomorrow" planner-goto-tomorrow]
767 ["Most recent" planner-goto-most-recent])
768 '["Goto plan page" planner-goto-plan-page])
769 ;; handling tasks
770 '("Tasks"
771 ["Create" planner-create-task-from-buffer]
772 ["Create from note" planner-create-task-from-note]
773 ["Delete" planner-delete-task]
774 ["Update" planner-update-task]
775 ["Copy or move task" planner-copy-or-move-task]
776 ["Copy or move region" planner-copy-or-move-region]
777 "---"
778 ;; Roughly arranged by frequency, not by chronological sequence
779 ["Mark \"done\"" planner-task-done]
780 ["Mark \"delegated\"" planner-task-delegated]
781 ["Mark \"pending\"" planner-task-pending]
782 ["Mark \"in progress\"" planner-task-in-progress]
783 ["Mark \"cancelled\"" planner-task-cancelled]
784 ["Mark \"open\"" planner-task-open]
785 "---"
786 ["Raise task priority" planner-raise-task-priority]
787 ["Lower task priority" planner-lower-task-priority]
788 ["Format tasks nicely" planner-fix-tasks])
789 ;; notes
790 '("Notes"
791 ["Create" planner-create-note]
792 ["Create from task" planner-create-note-from-task]
793 "---"
794 ["Search" planner-search-notes]
795 ["Search with body" planner-search-notes-with-body]
796 ["Renumber" planner-renumber-notes])
797 "---"
798 ;; miscellaneous
799 '["Plan" plan]
800 "---"
801 ;; help/info (now that we have a manual, use it)
802 '["Info manual" (info "planner-el")]))
804 ;;;_* Internal functions
806 ;;;_ + Compatibility
808 ;;;_ + Emacs vs XEmacs
810 (eval-and-compile
811 (defun planner-derived-mode-p (&rest modes)
812 "Non-nil if the current major mode is derived from one of MODES.
813 Uses the `derived-mode-parent' property of the symbol to trace backwards."
814 (if (fboundp 'derived-mode-p)
815 (apply 'derived-mode-p modes)
816 ;; PUBLIC: find if the current mode derives from another.
817 ;; Taken from GNU Emacs 21 subr.el
818 (let ((parent major-mode))
819 (while (and (not (memq parent modes))
820 (setq parent (get parent 'derived-mode-parent))))
821 parent))))
823 (defalias 'planner-match-string-no-properties 'muse-match-string-no-properties)
824 (defalias 'planner-replace-regexp-in-string 'muse-replace-regexp-in-string)
825 (defalias 'planner-line-beginning-position 'muse-line-beginning-position)
826 (defalias 'planner-line-end-position 'muse-line-end-position)
828 ;;; Copied from subr.el
829 (defun planner-copy-overlay (o)
830 "Return a copy of overlay O."
831 (if (fboundp 'copy-overlay)
832 (copy-overlay o)
833 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
834 ;; FIXME: there's no easy way to find the
835 ;; insertion-type of the two markers.
836 (overlay-buffer o)))
837 (props (overlay-properties o)))
838 (while props
839 (overlay-put o1 (pop props) (pop props)))
840 o1)))
842 ;;; Copied from subr.el
843 (defun planner-remove-overlays (beg end name val)
844 "Clear BEG and END of overlays whose property NAME has value VAL.
845 Overlays might be moved and or split."
846 (if (fboundp 'remove-overlays)
847 (remove-overlays beg end name val)
848 (if (< end beg)
849 (setq beg (prog1 end (setq end beg))))
850 (save-excursion
851 (dolist (o (overlays-in beg end))
852 (when (eq (overlay-get o name) val)
853 ;; Either push this overlay outside beg...end
854 ;; or split it to exclude beg...end
855 ;; or delete it entirely (if it is contained in beg...end).
856 (if (< (overlay-start o) beg)
857 (if (> (overlay-end o) end)
858 (progn
859 (move-overlay (planner-copy-overlay o)
860 (overlay-start o) beg)
861 (move-overlay o end (overlay-end o)))
862 (move-overlay o (overlay-start o) beg))
863 (if (> (overlay-end o) end)
864 (move-overlay o end (overlay-end o))
865 (delete-overlay o))))))))
867 ;; Provide a simpler replacement for `remove-if-not'
868 (defun planner-remove-if-not (predicate seq)
869 "Remove all items not satisfying PREDICATE in SEQ.
870 This is a non-destructive function; it makes a copy of SEQ to
871 avoid corrupting the original SEQ."
872 (let (newseq)
873 (dolist (el seq)
874 (when (funcall predicate el)
875 (setq newseq (cons el newseq))))
876 (nreverse newseq)))
878 ;; Display a warning
879 (defun planner-display-warning (message)
880 "Display the given MESSAGE as a warning."
881 (if (fboundp 'display-warning)
882 (display-warning 'planner message
883 (if (featurep 'xemacs)
884 'warning
885 :warning))
886 (let ((buf (get-buffer-create "*Planner warnings*")))
887 (with-current-buffer buf
888 (goto-char (point-max))
889 (insert "Warning (planner): " message)
890 (unless (bolp)
891 (newline)))
892 (display-buffer buf)
893 (sit-for 0))))
895 (defun planner-unhighlight-region (begin end &optional verbose)
896 "Remove all visual highlights in the buffer (except font-lock)."
897 (planner-zap-overlays begin end)
898 (muse-unhighlight-region begin end verbose))
900 (defun planner-zap-overlays (beg end &optional verbose)
901 "Remove all the planner-related overlays/extents from BEG to END."
902 (if (featurep 'xemacs)
903 (mapcar-extents 'delete-extent nil nil beg end nil 'planner t)
904 (planner-remove-overlays beg end 'planner t)))
906 (defmacro with-planner (&rest body)
907 "Make sure BODY is evaluated in a `planner-mode' buffer."
908 `(if (planner-derived-mode-p 'planner-mode)
909 (progn ,@body)
910 (with-temp-buffer
911 (setq muse-current-project (muse-project planner-project))
912 (muse-project-set-variables)
913 (planner-mode)
914 ,@body)))
915 (put 'with-planner 'lisp-indent-function 0)
916 (put 'with-planner 'edebug-form-spec '(body))
918 ;; Use a macro for the setup around planner-update-task so
919 ;; the same setup can be used in planner-multi.el
920 (defmacro with-planner-update-setup (&rest body)
921 "Execute BODY then save buffers according to `planner-tasks-file-behavior'.
922 Also sets some variables to modify font-lock behaviour while updating."
923 (let ((live-buffers (make-symbol "live-buffers")))
924 `(save-window-excursion
925 (save-excursion
926 (save-restriction
927 (let ((,live-buffers (and (eq planner-tasks-file-behavior
928 'close)
929 (buffer-list)))
930 (current-buffer (current-buffer)))
931 (prog1
932 (let ((planner-tasks-file-behavior nil))
933 ,@body)
934 (when planner-tasks-file-behavior
935 (planner-save-buffers ,live-buffers t current-buffer)))))))))
937 ;; Manually expanded def-edebug-spec so that we don't have to pull
938 ;; edebug in
939 (put 'with-planner-update-setup 'edebug-form-spec '(body))
941 (defalias 'planner-current-file 'muse-current-file)
943 (defun planner-file-alist (&optional no-check-p pages)
944 "Return possible Wiki filenames in `planner-project'.
945 On UNIX, this list is only updated if one of the directories'
946 contents have changed or NO-CHECK-P is non-nil. On Windows, it is
947 always reread from disk.
948 PAGES is ignored."
949 (muse-project-file-alist planner-project no-check-p))
951 (defun planner-find-file (wiki &optional command directory)
952 "Open the Planner page WIKI by name.
953 If COMMAND is non-nil, it is the function used to visit the file.
954 If DIRECTORY is non-nil, it is the directory in which the Wiki
955 page will be created if it does not already exist."
956 (muse-project-find-file (planner-link-base wiki)
957 planner-project
958 command
959 directory))
961 (defalias 'planner-page-name 'muse-page-name)
963 (defun planner-link-base (link)
964 "Return the page or URL named by LINK."
965 (when (string-match muse-explicit-link-regexp link)
966 (setq link (planner-match-string-no-properties 1 link)))
967 (when (string-match "#" link)
968 (setq link (substring link 0 (match-beginning 0))))
969 link)
971 (defalias 'planner-time-less-p 'muse-time-less-p)
972 (defalias 'planner-private-p 'muse-project-private-p)
973 (defalias 'planner-follow-name-at-point 'muse-follow-name-at-point)
974 (defalias 'planner-next-reference 'muse-next-reference)
975 (defalias 'planner-previous-reference 'muse-previous-reference)
977 ;; FIXME: Code that uses `planner-directory' should be changed to deal
978 ;; with multiple directories.
979 (defun planner-directory ()
980 (car (cadr (muse-project planner-project))))
982 (defun planner-published-file (file &optional output-dir style)
983 (unless output-dir
984 (setq output-dir (and muse-publishing-current-output-path
985 (file-name-directory
986 muse-publishing-current-output-path))))
987 (when output-dir
988 (muse-publish-output-file file output-dir style)))
990 (defun planner-remove-links (description)
991 "Remove explicit links from DESCRIPTION."
992 (let (start)
993 (while (setq start (string-match muse-explicit-link-regexp description
994 start))
995 (setq description
996 (replace-match (or (match-string 2 description)
997 (match-string 1 description))
998 t t description)))
999 description))
1001 (defun planner-make-link (link &optional name single)
1002 "Return a Wiki link to LINK with NAME as the text.
1003 If SINGLE is non-nil, treat it as a single link.
1004 If LINK is already a valid link, replace it's description
1005 by NAME"
1006 (cond ((or (null link) (string= link ""))
1008 ((string-match muse-explicit-link-regexp link)
1009 (muse-make-link (match-string 1 link) name))
1011 (muse-make-link link name))))
1013 ;;;_ + Diary
1015 ;; In here instead of planner-diary because planner-appt and
1016 ;; planner-cyclic use it as well. Contributions from Sergey Vlasov.
1017 (defun planner-list-diary-entries (file date &optional number)
1018 "Get list of diary entries in FILE for NUMBER days starting with DATE.
1019 The list has the same form as returned by `list-diary-entries', but
1020 this function tries to undo the changes which `list-diary-entries'
1021 does to the diary buffer."
1022 (require 'diary-lib)
1023 ;; The code to restore the buffer is copied from `include-other-diary-files'
1024 (save-window-excursion
1025 (save-excursion
1026 (let* ((diary-file file)
1027 (list-diary-entries-hook '(include-other-diary-files))
1028 (diary-display-hook 'ignore)
1029 (diary-hook nil)
1030 (d-buffer (find-buffer-visiting diary-file))
1031 (diary-modified (when d-buffer
1032 (set-buffer d-buffer)
1033 (buffer-modified-p))))
1034 (unwind-protect
1035 (list-diary-entries date (or number 1))
1036 (let ((d-buffer (find-buffer-visiting diary-file)))
1037 (when d-buffer
1038 (set-buffer d-buffer)
1039 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
1040 (setq selective-display nil)
1041 (set-buffer-modified-p diary-modified))))))))
1043 ;;;_ + Mode
1045 (defcustom planner-align-tasks-automatically t
1046 "Non-nil means align tasks whenever a planner file is saved."
1047 :type 'boolean
1048 :group 'planner)
1049 (defcustom planner-sort-tasks-automatically t
1050 "Non-nil means sort tasks whenever a planner file is saved."
1051 :type 'boolean
1052 :group 'planner)
1053 (defcustom planner-renumber-tasks-automatically nil
1054 "Non-nil means renumber tasks whenever a planner file is saved."
1055 :type 'boolean
1056 :group 'planner)
1057 (defcustom planner-renumber-notes-automatically nil
1058 "Non-nil means renumber notes whenever a planner file is saved."
1059 :type 'boolean
1060 :group 'planner)
1062 ;;;###autoload
1064 (define-derived-mode planner-mode muse-mode "Planner"
1065 "A personal information manager for Emacs.
1066 \\{planner-mode-map}"
1067 ;; because we're not inheriting from normal-mode, we need to
1068 ;; explicitly run file variables if the user wants to
1069 (condition-case err
1070 (hack-local-variables)
1071 (error (message "File local-variables error: %s"
1072 (prin1-to-string err))))
1073 ;; check to see if the mode changed
1074 (when (eq major-mode 'planner-mode)
1075 (let ((hook (if (boundp 'write-file-functions)
1076 'write-file-functions
1077 'write-file-hooks)))
1078 (add-hook hook 'planner-renumber-notes-maybe t t)
1079 (add-hook hook 'planner-sort-tasks-maybe t t)
1080 (add-hook hook 'planner-renumber-tasks-maybe t t)
1081 (add-hook hook 'planner-align-tasks-maybe t t))
1082 (planner-setup-highlighting)
1083 (when (fboundp 'easy-menu-add)
1084 (easy-menu-add planner-menu planner-mode-map))
1085 (planner-prepare-file)))
1086 ;; (when (and font-lock-mode muse-mode-highlight-p)
1087 ;; (muse-colors-buffer))))
1089 (defvar planner-date-regexp
1090 "\\<\\([1-9][0-9][0-9][0-9]\\)[\\.\\-]\\([0-9]+\\)[\\.\\-]\\([0-9]+\\)\\>")
1092 (defun planner-setup-highlighting ()
1093 "Set up fontification for planner."
1094 (add-hook 'muse-colors-buffer-hook 'planner-zap-overlays t t)
1095 (add-hook 'muse-colors-buffer-hook 'planner-highlight-tasks t t)
1096 (add-hook 'muse-colors-buffer-hook 'planner-highlight-notes t t)
1097 (add-to-list 'muse-colors-markup
1098 (list planner-date-regexp t 'muse-colors-implicit-link)
1100 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)
1101 (set (make-local-variable 'font-lock-unfontify-region-function)
1102 'planner-unhighlight-region)
1103 (set (make-local-variable 'font-lock-defaults)
1104 '(nil t nil nil beginning-of-line
1105 (font-lock-fontify-region-function . muse-colors-region)
1106 (font-lock-unfontify-region-function
1107 . planner-unhighlight-region))))
1109 (defun planner-muse-handle-date-link (&optional string)
1110 "If STRING or point has a date, match and return it."
1111 (when (if string
1112 (string-match planner-date-regexp string)
1113 (looking-at planner-date-regexp))
1114 (match-string 0 string)))
1116 (custom-add-option 'muse-implicit-link-functions
1117 'planner-muse-handle-date-link)
1118 (add-hook 'muse-implicit-link-functions 'planner-muse-handle-date-link t)
1120 ;;;_ + Wiki pages
1122 (defun planner-strip-whitespace (string)
1123 "Remove all whitespace from STRING. Return the modified string."
1124 (with-temp-buffer
1125 (insert string)
1126 (goto-char (point-min))
1127 (while (re-search-forward "[\r\n\t ]+" nil t)
1128 (replace-match ""))
1129 (buffer-string)))
1131 (defun planner-page-default-p (&optional buffer)
1132 "Return t if this plan page can be safely deleted.
1133 If the contents of this plan page are the same as the value of
1134 `planner-day-page-template' or the plan page is empty, then no
1135 information has been added and the page can safely be removed.
1137 If BUFFER is given, considers the planner page in BUFFER instead.
1139 Override this if `planner-day-page-template' is a function
1140 instead of a string."
1141 (with-current-buffer (or buffer (current-buffer))
1142 (when (and (stringp planner-day-page-template)
1143 (not (> (buffer-size)
1144 (+ (length planner-day-page-template)
1145 planner-template-fuzz-factor))))
1146 (let ((body (planner-strip-whitespace (buffer-string))))
1147 (or (= (length body) 0)
1148 (string= body (planner-strip-whitespace
1149 planner-day-page-template)))))))
1151 (defvar planner-delete-file-function 'delete-file
1152 "Function called to remove a planner file from the current wiki.")
1154 (defun planner-maybe-remove-file ()
1155 "Delete the planner file if it does not contain new information."
1156 (if (planner-page-default-p (current-buffer))
1157 (let ((filename buffer-file-name))
1158 (set-buffer-modified-p nil)
1159 (kill-buffer (current-buffer))
1160 (when (file-exists-p filename)
1161 (funcall planner-delete-file-function filename)))
1162 (kill-buffer (current-buffer))))
1165 (defun planner-prepare-file ()
1166 "Insert some standard sections into an empty planner file."
1167 (when (= (buffer-size) 0)
1168 (let ((template
1169 (if (and (planner-page-name)
1170 (string-match planner-date-regexp (planner-page-name)))
1171 planner-day-page-template
1172 planner-plan-page-template)))
1173 (if (functionp template)
1174 (funcall template)
1175 (insert template))
1176 (set-buffer-modified-p nil))))
1178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1179 ;; Compatibility
1181 (defun planner-link-href (url name)
1182 "Return an href string for URL and NAME."
1183 (muse-publish-url url name))
1185 (defun planner-link-target (link)
1186 "Return the URL or page in LINK."
1187 (if (string-match muse-explicit-link-regexp link)
1188 (planner-match-string-no-properties 1 link)
1189 link))
1191 (defun planner-link-name (link)
1192 "Return the name for LINK."
1193 (if (string-match muse-explicit-link-regexp link)
1194 (planner-match-string-no-properties 2 link)
1195 link))
1197 (defun planner-link-anchor (link)
1198 "Return the anchor part of LINK."
1199 (setq link (planner-link-target link))
1200 (if (string-match "#" link)
1201 (substring link (1+ (match-beginning 0)))))
1203 (defun planner-visit-link (link &optional other-window)
1204 "Visit the URL or link named by LINK.
1205 REFRESH-BUFFER is an optional buffer to refresh on saving the visited page.
1206 This makes the bad link face in the linking buffer go away."
1207 (if (string-match muse-url-regexp link)
1208 (muse-browse-url link other-window)
1209 (setq link (planner-link-target link))
1210 (let ((tag (planner-link-anchor link))
1211 base-buffer)
1212 ;; use match data from planner-link-anchor
1213 (when tag
1214 (setq link (if (= (match-beginning 0) 0)
1215 ;; If there is an anchor but no link, default
1216 ;; to the current page.
1218 (substring link 0 (match-beginning 0)))))
1219 (when link
1220 (setq base-buffer (get-buffer link))
1221 (if (and base-buffer (not (buffer-file-name base-buffer)))
1222 ;; If file is temporary (no associated file), just switch to
1223 ;; the buffer
1224 (if other-window
1225 (switch-to-buffer-other-window base-buffer)
1226 (switch-to-buffer base-buffer))
1227 (let ((project (muse-project-of-file)))
1228 (if project
1229 (muse-project-find-file link project
1230 (and other-window
1231 'find-file-other-window))
1232 (if other-window
1233 (find-file-other-window link)
1234 (find-file link))))))
1235 (when tag
1236 (goto-char (point-min))
1237 (or (re-search-forward (concat "^\\.?#" (regexp-quote tag) "\\>")
1238 nil t)
1239 (when (string-match "^anchor-\\(.*\\)" tag)
1240 (re-search-forward
1241 (concat "^\\.?#" (regexp-quote (match-string 1 tag)) "\\>")
1242 nil t)))))))
1244 (defalias 'planner-add-protocol 'muse-protocol-add)
1245 (defalias 'planner-page-exists-p 'planner-page-file)
1247 (defun planner-local-page-p (link)
1248 "Return non-nil if LINK seems to belong to the current wiki."
1249 (and link
1250 (not (or (string-match ":\\|/"
1251 (planner-link-base link))))))
1253 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1254 (defun planner-list-daily-files (&optional exclude-temp)
1255 "Return an unsorted, unique list of daily files.
1256 If EXCLUDE-TEMP is non-nil, ignore unsaved buffers."
1257 ;; get a list of all files
1258 ;; (save-some-buffers t (lambda () (equal 'planner-mode major-mode)))
1259 (let ((buffers (buffer-list))
1260 files)
1261 (mapcar (lambda (item)
1262 (when (string-match planner-date-regexp (car item))
1263 (unless (rassoc (cdr item) files)
1264 (setq files (cons item files)))))
1265 (planner-file-alist))
1266 (unless exclude-temp
1267 (while buffers
1268 (with-current-buffer (car buffers)
1269 (when (and (equal 'planner-mode major-mode)
1270 (planner-page-name)
1271 (string-match planner-date-regexp (planner-page-name)))
1272 (unless (assoc (planner-page-name) files)
1273 (add-to-list 'files (cons (planner-page-name)
1274 (buffer-file-name))))))
1275 (setq buffers (cdr buffers))))
1276 files))
1278 (defun planner-get-day-pages (&optional from to exclude-temp)
1279 "Return a descending list of day pages from FROM to TO (inclusive).
1280 If EXCLUDE-TEMP is non-nil, ignore unsaved pages."
1281 (sort (delq nil
1282 (mapcar
1283 (lambda (item)
1284 (and
1285 (car item)
1286 (or (not from)
1287 (string-lessp from (car item))
1288 (equal from (car item)))
1289 (or (not to)
1290 (string-lessp (car item) to)
1291 (equal (car item) to))
1292 item))
1293 (planner-list-daily-files exclude-temp)))
1294 (lambda (l r)
1295 (string-lessp (car r) (car l)))))
1297 ;;;_ + Date
1299 (defvar planner-calendar-selected-date nil
1300 "Temporary storage for date selected from calendar.")
1302 (defvar planner-use-calendar-flag t
1303 "*If non-nil, show calendar popup when reading a date.")
1305 (defun planner-read-date (&optional prompt force-read)
1306 "Prompt for a date string in the minibuffer.
1307 If PROMPT is non-nil, display it as the prompt string.
1308 If FORCE-READ is non-nil, prompt for a date even when we are not
1309 using day pages."
1310 (save-window-excursion
1311 (when (or planner-use-day-pages force-read)
1312 (let ((old-buffer (current-buffer)))
1313 (when planner-use-calendar-flag (calendar))
1314 (let ((old-map (copy-keymap calendar-mode-map)))
1315 (unwind-protect
1316 (progn
1317 (define-key calendar-mode-map [return]
1318 'planner-calendar-select)
1319 (define-key calendar-mode-map [mouse-1]
1320 'planner-calendar-select)
1321 (setq planner-calendar-selected-date nil)
1322 (let ((text (read-string
1323 (format "%s %s"
1324 (or prompt "When")
1325 (format-time-string
1326 (concat "(%Y" planner-date-separator "%m"
1327 planner-date-separator "%d, %m"
1328 planner-date-separator "%d, %d): "))))))
1329 (or planner-calendar-selected-date
1330 (with-current-buffer old-buffer
1331 (planner-expand-name text)))))
1332 (setq calendar-mode-map old-map)))))))
1334 (defvar planner-timewarp-date nil
1335 "*Date to timewarp to for planner.
1336 Should be a string of the form YYYY.MM.DD. If nil, do not timewarp.")
1338 ;; This should be handy for remembering lots of notes onto particular days.
1339 (defun planner-timewarp (date)
1340 "Timewarp to DATE."
1341 (interactive (list (let ((planner-timewarp-date nil)) (planner-read-date))))
1342 (setq planner-timewarp-date date)
1343 (if date (message "Timewarped to %s" date)
1344 (message "Timewarped back to the present")))
1346 (defun planner-today ()
1347 "Return the filename of the current date."
1348 (if planner-use-day-pages
1349 (or planner-timewarp-date (planner-date-to-filename
1350 (decode-time (current-time))))
1351 planner-initial-page))
1353 (defun planner-date-to-filename (date)
1354 "Return the planner filename corresponding to DATE.
1355 DATE is a list (month day year) or an internal date representation."
1356 (let ( (date-form-string (concat "%04d" planner-date-separator "%02d"
1357 planner-date-separator "%02d") ) )
1358 (progn
1359 (if (= (length date) 3)
1360 (format date-form-string (elt date 2) (elt date 0) (elt date 1))
1361 (if (= (length date) 2)
1362 (setq date (decode-time date)))
1363 (format date-form-string
1364 (elt date 5) ; year
1365 (elt date 4) ; month
1366 (elt date 3)))))) ; day
1368 (defun planner-calculate-date-from-day-offset (origin offset)
1369 "From ORIGIN, calculate the date OFFSET days into the past or future.
1370 ORIGIN can be a buffer name, a list of the form (MONTH DAY YEAR),
1371 or an internal date representation. If OFFSET is positive,
1372 returns a date in the future. If OFFSET is negative, returns the
1373 date -OFFSET days in the past. Return an object that is the
1374 same type as ORIGIN."
1375 (cond
1376 ((stringp origin)
1377 (let ((date (planner-filename-to-calendar-date origin)))
1378 (planner-date-to-filename (encode-time 0 0 0 (+ (elt date 1) offset)
1379 (elt date 0) (elt date 2)))))
1380 ((= (length origin) 2)
1381 (encode-time 0 0 0 (+ (elt origin 1) offset)
1382 (elt origin 0) (elt origin 2)))
1383 ((= (length origin) 3)
1384 (let ((result
1385 (decode-time (encode-time 0 0 0 (+ (elt origin 1) offset)
1386 (elt origin 0) (elt origin 2)))))
1387 (list (elt result 4) (elt result 3) (elt result 5))))))
1389 (defun planner-get-previous-existing-day (date)
1390 "Return the planner file immediately before DATE.
1391 DATE is a filename or a list (month day year). When called from
1392 a planner file, DATE defaults to the date of this file, otherwise
1393 it defaults to today. Returns an object of the same type as
1394 DATE."
1395 (let ((newdate (if (listp date) (planner-date-to-filename date) date))
1396 (result nil))
1397 ;; beginning of hackish part
1398 (mapcar (lambda (elt)
1399 (when (and (or (not result)
1400 (not (or (string= elt result)
1401 (string< elt result))))
1402 (string< elt newdate))
1403 (setq result elt)))
1404 (mapcar 'car (planner-list-daily-files)))
1405 (if result
1406 (if (listp date)
1407 (planner-filename-to-calendar-date result)
1408 result)
1409 (error "No previous planner file"))))
1411 (defun planner-get-next-existing-day (date)
1412 "Return the existing planner file immediately after DATE.
1413 DATE is a filename or a list (month day year). When called from
1414 a planner file, DATE defaults to the date of this file, otherwise
1415 it defaults to today. Returns an object of the same type as
1416 DATE."
1417 (let ((newdate (if (listp date) (planner-date-to-filename date) date))
1418 (result nil))
1419 ;; beginning of hackish part
1420 (mapcar (lambda (elt)
1421 (when (and (or (not result)
1422 (string< elt result))
1423 (not (or (string= elt newdate)
1424 (string< elt newdate))))
1425 (setq result elt)))
1426 (mapcar 'car (planner-list-daily-files)))
1427 (if result
1428 (if (listp date)
1429 (planner-filename-to-calendar-date result)
1430 result)
1431 (error "No next planner file"))))
1433 (defun planner-yesterday ()
1434 "Return the date of yesterday."
1435 (planner-calculate-date-from-day-offset (planner-today) -1))
1437 (defun planner-tomorrow ()
1438 "Return the date of tomorrow."
1439 (planner-calculate-date-from-day-offset (planner-today) 1))
1441 (defcustom planner-expand-name-favor-future-p nil
1442 "If non-nil, `planner-expand-name' defaults to future dates."
1443 :type 'boolean
1444 :group 'planner)
1446 (defcustom planner-expand-name-default "."
1447 "What an empty string means in `planner-expand-name'.
1448 \".\" means today."
1449 :type '(choice
1450 (const :tag "Today" ".")
1451 (const :tag "None" nil)
1452 string)
1453 :group 'planner)
1455 (defvar planner-expand-name-days-alist '(("sun" . 0)
1456 ("mon" . 1)
1457 ("tue" . 2)
1458 ("wed" . 3)
1459 ("thu" . 4)
1460 ("fri" . 5)
1461 ("sat" . 6))
1462 "Abbreviations for `planner-expand-name'.")
1464 (defun planner-expand-name (name)
1465 "Expand the given NAME to its fullest form.
1466 This typically means that dates like 3.31 will become 2001.03.31.
1467 NOTE: This function no longer uses the current buffer filename for
1468 defaults."
1469 (let ((now (if planner-use-day-pages
1470 (planner-filename-to-calendar-date (planner-today))
1471 (planner-filename-to-calendar-date
1472 (planner-date-to-filename
1473 (decode-time (current-time))))))
1474 name-year name-month name-day)
1475 (when (string-match "^\\s-*$" name)
1476 (setq name (or planner-expand-name-default "nil")))
1477 (cond
1478 ((string= "nil" name) nil)
1479 ((string= "." name) (if (not planner-use-day-pages)
1480 (planner-date-to-filename now)
1481 (planner-today)))
1482 ((string-match (concat "^\\([1-9][0-9][0-9][0-9][\\.\\-]\\)?"
1483 "\\(\\([0-9]+\\)[\\.\\-]\\)?"
1484 "\\([0-9]+\\)\\(#.*\\)?$") name)
1485 (setq name-year
1486 (if (match-string 1 name)
1487 (string-to-number (match-string 1 name)) (nth 2 now)))
1488 (setq name-month
1489 (if (match-string 3 name)
1490 (string-to-number (match-string 3 name)) (nth 0 now)))
1491 (setq name-day
1492 (if (match-string 4 name)
1493 (string-to-number (match-string 4 name)) (nth 1 now)))
1494 (when (and planner-expand-name-favor-future-p
1495 (planner-time-less-p
1496 (encode-time 59 59 23
1497 name-day name-month name-year)
1498 (current-time)))
1499 (cond
1500 ((match-string 1 name)) ; Do nothing if the year is specified
1501 ((match-string 2 name)
1502 (setq name-year (1+ name-year)))
1503 ((match-string 4 name)
1504 (setq name-month (1+ name-month)))))
1505 (planner-date-to-filename (encode-time 59 59 23
1506 name-day name-month name-year)))
1507 ((string-match "^\\([-+]\\)\\s-*\\([0-9]+\\)$" name)
1508 ;; Today + or - that number of days
1509 (planner-calculate-date-from-day-offset
1510 (if (not planner-use-day-pages)
1511 (planner-date-to-filename now)
1512 (if (or planner-dates-relative-to-today-flag
1513 (not (planner-page-name))
1514 (not (save-match-data
1515 (string-match planner-date-regexp
1516 (planner-page-name)))))
1517 (planner-today)
1518 (planner-page-name)))
1519 (string-to-number
1520 (concat (match-string 1 name) (match-string 2 name)))))
1521 ((let ((case-fold-search nil))
1522 (string-match (concat
1523 "^\\([-+]\\)\\s-*\\([0-9]*\\)\\s-*\\("
1524 (mapconcat 'car planner-expand-name-days-alist "\\|")
1525 "\\)\\s-*\\([\\.\\-]\\|\\(\\(\\([0-9]+[\\.\\-]\\)?[0-9]+[\\-\\.]\\)?[0-9]+\\)\\)?$")
1526 name))
1527 (let* ((day (cdr (assoc (match-string 3 name)
1528 planner-expand-name-days-alist)))
1529 (offset (string-to-number
1530 (concat (match-string 1 name)
1531 (if (and
1532 (match-string 2 name)
1533 (not (string= (match-string 2 name) "")))
1534 (match-string 2 name)
1535 "1"))))
1536 (base-date (planner-filename-to-calendar-date
1537 (if (and (match-string 4 name)
1538 (not (string= (match-string 4 name) "")))
1539 (planner-expand-name (match-string 4 name))
1540 (if (not planner-use-day-pages)
1541 (planner-date-to-filename now)
1542 (if (or planner-dates-relative-to-today-flag
1543 (not (planner-page-name))
1544 (not (save-match-data
1545 (string-match
1546 planner-date-regexp
1547 (planner-page-name)))))
1548 (planner-today)
1549 (planner-page-name)))))))
1550 (planner-date-to-filename
1551 (calendar-gregorian-from-absolute
1552 (calendar-dayname-on-or-before
1554 (+ (calendar-absolute-from-gregorian base-date)
1555 (* offset 7)
1556 (if (< offset 0) 6 0)))))))
1557 (t name))))
1559 (defun planner-get-current-date-filename ()
1560 "Return the date of the daily page currently being viewed.
1561 If no daily page is being viewed, return today's date."
1562 (if (string-match planner-date-regexp (planner-page-name))
1563 (planner-page-name)
1564 (planner-today)))
1566 (defun planner-filename-to-calendar-date (filename)
1567 "Return the date of the planning file FILENAME.
1568 Date is a list (month day year)."
1569 (unless (string-match planner-date-regexp filename)
1570 (error "Not convertible to a date %s" filename))
1571 (list (string-to-number (match-string 2 filename)) ; month
1572 (string-to-number (match-string 3 filename)) ; day
1573 (string-to-number (match-string 1 filename)))) ; year
1575 ;;;_ + Sections
1577 (defun planner-narrow-to-section (section &optional create)
1578 "Widen to the whole page and narrow to the section labelled SECTION.
1579 If CREATE is non-nil, create the section if it is not found.
1580 Return non-nil if SECTION was found."
1581 (interactive "MSection: ")
1582 (widen)
1583 (unless (stringp section)
1584 (setq section (cdr (assoc section planner-sections))))
1585 (goto-char (point-min))
1586 (when (or
1587 (re-search-forward
1588 (concat "^*\\s-+" (regexp-quote section) "\\s-*$") nil t)
1589 (and create
1590 (funcall planner-create-section-function section)
1591 (goto-char (point-min))
1592 (re-search-forward (concat "^*\\s-+" (regexp-quote section)
1593 "\\s-*$") nil t)))
1594 (let ((beg (match-beginning 0))
1595 (end (if (re-search-forward "^*\\s-+" nil t)
1596 (match-beginning 0) (point-max))))
1597 (narrow-to-region beg end)
1598 t)))
1600 (defun planner-delete-section (section)
1601 "Delete the named SECTION."
1602 (unless (planner-derived-mode-p 'planner-mode)
1603 (error "This is not a planner buffer"))
1604 (unless (stringp section)
1605 (setq section (cdr (assoc section planner-sections))))
1606 (widen)
1607 (goto-char (point-min))
1608 (when (re-search-forward (concat "^\\*\\s-+" section "\\(\\s-*\\)$") nil t)
1609 (let ((beg (planner-line-beginning-position))
1610 (end (if (re-search-forward "^* " nil t)
1611 (planner-line-beginning-position)
1612 (point-max))))
1613 (delete-region beg end))))
1615 (defun planner-delete-section-text (section)
1616 "Delete the text of the named SECTION."
1617 (unless (planner-derived-mode-p 'planner-mode)
1618 (error "This is not a planner buffer"))
1619 (unless (stringp section)
1620 (setq section (cdr (assoc section planner-sections))))
1621 (widen)
1622 (goto-char (point-min))
1623 (when (re-search-forward (concat "^\\*\\s-+" section "\\(\\s-*\\)$") nil t)
1624 (let ((beg (point))
1625 (end (if (re-search-forward "^* " nil t)
1626 (planner-line-beginning-position)
1627 (point-max))))
1628 (delete-region beg end)
1629 (goto-char (planner-line-beginning-position)))))
1631 (defun planner-seek-to-first (&optional section)
1632 "Positions the point at the specified SECTION, or Tasks if not specified."
1633 (interactive)
1634 (unless section
1635 (setq section planner-default-section))
1636 (unless (stringp section)
1637 (setq section (cdr (assoc section planner-sections))))
1638 (widen)
1639 (goto-char (point-min))
1640 (if (re-search-forward (concat "^\\*\\s-+" section "\\(\\s-*?\\)$") nil t)
1641 (let ((old (point)) new)
1642 (forward-line 1)
1643 (if (re-search-forward "[^\\s-]" nil t)
1644 (progn
1645 (goto-char (planner-line-beginning-position))
1646 (unless (looking-at "^\\*\\s-")
1647 (setq new (point)))))
1648 (goto-char (or new old))
1649 (unless new
1650 (forward-line 1)
1651 (when (or (looking-at "^\\*\\s-+")
1652 (> (forward-line 1) 0)) (insert "\n"))
1653 (when (or (looking-at "^\\*\\s-+")
1654 (> (forward-line 1) 0)) (insert "\n"))
1655 (when (looking-at "^\\*\\s-+") (forward-line -1))))
1656 ;; Section not found, so create it.
1657 (funcall planner-create-section-function section)))
1659 (defun planner-create-at-top (section)
1660 "Create SECTION at top of file."
1661 (goto-char (point-min))
1662 (let ((buffer-status (buffer-modified-p)))
1663 (insert "* " section "\n\n")
1664 (set-buffer-modified-p buffer-status)))
1666 (defun planner-create-at-bottom (section)
1667 "Create SECTION at bottom of file."
1668 (goto-char (point-max))
1669 (let ((buffer-status (buffer-modified-p)))
1670 (insert "\n* " section "\n\n")
1671 (set-buffer-modified-p buffer-status)))
1673 ;;;_ + Basic annotation
1675 ;;;###autoload
1676 (defun planner-annotation-as-kill (arg)
1677 "Copy the current annotation into the kill ring.
1678 When called with a prefix argument, prompt for the link display name."
1679 (interactive "P")
1680 (let* ((link (run-hook-with-args-until-success
1681 'planner-annotation-functions))
1682 (link-name (if arg (read-string (format "Link name for %s: " link)))))
1683 (unless (or (not (stringp link-name)) (string= link-name ""))
1684 (setq link (planner-make-link link link-name t)))
1685 (if (not (stringp link))
1686 (message "No annotation can be made")
1687 (message "Copied '%s' to the kill-ring" link)
1688 (kill-new link))))
1689 (custom-add-option 'planner-annotation-functions
1690 'planner-annotation-as-kill)
1692 (defun planner-annotation-from-planner-note ()
1693 "Return a link to the current page.
1694 Call when the point is on the first line of the note."
1695 (when (and (planner-derived-mode-p 'planner-mode)
1696 (planner-page-name))
1697 (save-excursion
1698 (goto-char (planner-line-beginning-position))
1699 (when (looking-at ".\\(#[0-9]+\\)")
1700 (planner-make-link
1701 (concat (planner-page-name)
1702 (planner-match-string-no-properties 1))
1703 (concat (planner-page-name)
1704 (planner-match-string-no-properties 1))
1705 t)))))
1706 (custom-add-option 'planner-annotation-functions
1707 'planner-annotation-from-planner-note)
1709 (defun planner-annotation-from-planner ()
1710 "Return a wiki link to the current wiki page.
1711 Date pages are not linked."
1712 (when (and (planner-derived-mode-p 'planner-mode)
1713 (planner-page-name))
1714 (cond
1715 ((string-match planner-date-regexp (planner-page-name))
1716 "") ; None for date pages
1717 (t (planner-make-link (planner-page-name) nil t)))))
1718 (custom-add-option 'planner-annotation-functions
1719 'planner-annotation-from-planner)
1721 (defun planner-annotation-from-wiki ()
1722 "Return the interwiki link to the current wiki page."
1723 (when (and (planner-derived-mode-p 'muse-mode)
1724 muse-current-project
1725 (muse-page-name))
1726 (concat "[[" (car muse-current-project) "::" (muse-page-name) "]]")))
1727 (custom-add-option 'planner-annotation-functions
1728 'planner-annotation-from-wiki)
1730 (defun planner-annotation-from-dired ()
1731 "Return the `default-directory' of the current Dired buffer."
1732 (when (eq major-mode 'dired-mode)
1733 (planner-make-link default-directory)))
1735 (custom-add-option 'planner-annotation-functions
1736 'planner-annotation-from-dired)
1738 (defcustom planner-annotation-use-relative-file nil
1739 "If t, use relative file links always.
1740 If a function, it is called with the file name. Return value of t
1741 means use relative file links."
1742 :group 'planner
1743 :type '(choice (const :tag "Always use relative file links" t)
1744 (const :tag "Never use relative file links" nil)
1745 function))
1747 (defcustom planner-annotation-strip-directory nil
1748 "If non-nil, strip the directory part of the filename from link text."
1749 :group 'planner
1750 :type 'boolean)
1752 (defcustom planner-annotation-format-local-file-name nil
1753 "If non-nil, use the result of `planner-annotation-format-local-file-name'."
1754 :group 'planner
1755 :type '(choice (const :tag "Use filename as is" nil)
1756 function))
1758 (defun planner-annotation-from-file ()
1759 "Return the filename of the current buffer.
1760 If `planner-annotation-use-relative-file' is t or a function that
1761 returns non-nil, a relative link is used instead. If
1762 `planner-annotation-strip-directory' is non-nil, the directory is
1763 stripped from the link description."
1764 (when buffer-file-name
1765 (planner-make-link
1766 (if (or (and (functionp planner-annotation-use-relative-file)
1767 (funcall planner-annotation-use-relative-file
1768 (buffer-file-name)))
1769 (equal planner-annotation-use-relative-file t))
1770 (file-relative-name (buffer-file-name) (planner-directory))
1771 (if (functionp planner-annotation-format-local-file-name)
1772 (funcall planner-annotation-format-local-file-name buffer-file-name)
1773 buffer-file-name))
1774 (when planner-annotation-strip-directory
1775 (file-name-nondirectory buffer-file-name))
1776 t)))
1777 (custom-add-option 'planner-annotation-functions
1778 'planner-annotation-from-file)
1780 ;;;###autoload
1781 (defun planner-annotation-from-file-with-position ()
1782 "Return the filename and cursor position of the current buffer.
1783 If `planner-annotation-use-relative-file' is t or a function that
1784 returns non-nil, a relative link is used instead. If
1785 `planner-annotation-strip-directory' is non-nil, the directory is
1786 stripped from the link description."
1787 (when buffer-file-name
1788 (planner-make-link
1789 (concat
1790 "pos://"
1791 (if (or (and (functionp planner-annotation-use-relative-file)
1792 (funcall planner-annotation-use-relative-file
1793 (buffer-file-name)))
1794 (equal planner-annotation-use-relative-file t))
1795 (file-relative-name (buffer-file-name) (planner-directory))
1796 buffer-file-name)
1797 "#" (number-to-string (point)))
1798 (if planner-annotation-strip-directory
1799 (file-name-nondirectory buffer-file-name)
1800 buffer-file-name)
1801 t)))
1803 ;;;###autoload
1804 (defun planner-browse-position-url (url)
1805 "If this is a position URL, jump to it."
1806 (when (string-match "^pos://\\(.+\\)#\\([0-9]+\\)$" url)
1807 (let ((file (match-string 1 url))
1808 (pos (string-to-number (match-string 2 url))))
1809 (find-file file)
1810 (goto-char pos)
1811 t)))
1813 ;;;###autoload
1814 (defun planner-resolve-position-url (id)
1815 "Replace ID with the blog, web or e-mail address of the BBDB record."
1816 (save-match-data
1817 (when (string-match "\\`pos://\\(.+\\)#\\([0-9]+\\)" id)
1818 (match-string 1 id))))
1820 (planner-add-protocol "pos://" 'planner-browse-position-url
1821 'planner-resolve-position-url)
1822 (custom-add-option 'planner-annotation-functions
1823 'planner-annotation-from-file-with-position)
1825 ;;;_ + Tasks
1827 (defcustom planner-create-task-hook nil
1828 "Functions to run after a task has been created.
1829 Point will be on the same line as the task."
1830 :type 'hook
1831 :group 'planner-tasks)
1833 (defcustom planner-create-task-from-buffer-hook nil
1834 "Functions to run after a task has been created from a buffer.
1835 This will be run before `planner-create-task-hook'.
1836 Point will be on the same line as the task."
1837 :type 'hook
1838 :group 'planner-tasks)
1840 (defcustom planner-task-dates-favor-future-p nil
1841 "*If this is non-nil, favor future dates for task creation or movement."
1842 :type 'boolean
1843 :group 'planner-tasks)
1845 (defcustom planner-default-page "TaskPool"
1846 "Default page for tasks.
1847 This is set to the current planner page, or the last page used
1848 if not on a plan page."
1849 :type 'string
1850 :group 'planner-tasks)
1852 (defcustom planner-tasks-file-behavior 'close
1853 "Controls behavior of task creation and updates.
1854 If 'close, newly-opened files are saved and closed.
1855 If 'save, newly-opened files are saved and left open.
1856 If nil, no actions will be taken."
1857 :group 'planner-tasks
1858 :type '(choice (const :tag "Save and close opened files" 'close)
1859 (const :tag "Save opened files" 'save)
1860 (const :tag "Do nothing" nil)))
1862 (defcustom planner-tasks-never-suppress-fixing-flag t
1863 "Non-nil means always sort, renumber and align tasks whenever
1864 files are saved."
1865 :group 'planner-tasks
1866 :type 'boolean)
1868 (defcustom planner-sort-undated-tasks-equivalent "9999.99.99"
1869 "Date considered for undated tasks.
1870 This option controls task sorting on plan pages. By default,
1871 undated tasks are sorted after dated tasks."
1872 :group 'planner-tasks
1873 :type
1874 '(choice
1875 (const :tag "Sort undated tasks after dated tasks" "9999.99.99")
1876 (const :tag "Sort undated tasks before dated tasks" "")
1877 string))
1879 (defcustom planner-sort-tasks-key-function 'planner-sort-tasks-default-key
1880 "Function called to determine the sorting key for the current line."
1881 :group 'planner-tasks
1882 :type 'function)
1884 (defcustom planner-use-task-numbers nil
1885 "Non-nil means number tasks.
1886 This allows you to refer to past tasks if your tasks are numbered
1887 appropriately. If you set this to nil, you can save space in your
1888 plan files."
1889 :type 'boolean
1890 :group 'planner-tasks)
1892 ;;;_ + Information
1894 (defun planner-task-info-from-string (page-name string)
1895 "On the planner page PAGE-NAME, parse STRING and return the task as a list.
1896 Argument PAGE-NAME is used to determine whether this is a link
1897 from a plan page or a date page."
1898 (with-planner
1899 (when (string-match "#\\([A-C]\\)\\([0-9]*\\)\\s-+\\(.\\)\\s-+\\(.+\\)"
1900 string)
1901 (let ((priority (planner-match-string-no-properties 1 string))
1902 (number (planner-match-string-no-properties 2 string))
1903 (status (planner-match-string-no-properties 3 string))
1904 (description (planner-match-string-no-properties 4 string))
1905 (case-fold-search nil)
1906 link-text link plan date)
1907 (when (= (length number) 0)
1908 (setq number nil))
1909 (cond
1910 ((string-match
1911 "\\s-+(\\(\\[\\[\\([^])]+\\)\\]\\[\\([^])]+\\)\\]\\]\\))\\s-*$"
1912 description)
1913 (setq link-text (match-string 1 description))
1914 (setq link (match-string 2 description))
1915 (setq description (replace-match "" t t description)))
1916 ((string-match
1917 "\\s-+(\\(\\[\\[\\([^])]+\\)\\]\\]\\))\\s-*$" description)
1918 (setq link-text (match-string 1 description))
1919 (setq link (match-string 2 description))
1920 (setq description (replace-match "" t t description)))
1921 ((string-match "\\s-+(\\([^)]+\\))\\s-*$" description)
1922 (setq link-text (match-string 1 description))
1923 (setq link (match-string 1 description))
1924 (setq description (replace-match "" t t description)))
1925 ((string-match "\\s-+$" description)
1926 (setq description (replace-match "" t t description))))
1927 (when link
1928 (setq link (planner-link-base link-text)))
1929 (unless (planner-local-page-p link) (setq link nil))
1930 (if (string-match planner-date-regexp page-name)
1931 ;; We're on a date page, so the link page (if any) should be the
1932 ;; planner page.
1933 (progn
1934 (setq date page-name)
1935 (setq plan (and link
1936 (unless (string-match planner-date-regexp link)
1937 link))))
1938 ;; We're on a planner page, so the link page (if any) will be the plan
1939 (setq plan (and page-name (unless (string-match planner-date-regexp
1940 page-name)
1941 page-name)))
1942 (when (and link (string-match planner-date-regexp link))
1943 (setq date (match-string 0 link))))
1944 (list page-name
1945 priority number status description link link-text plan date)))))
1947 (defun planner-task-info-override (task-info properties)
1948 "Replace fields in TASK-INFO with PROPERTIES.
1949 Acceptable properties are: page-name priority number status
1950 description link link-text plan date."
1951 (let ((fields '(page-name priority number status description
1952 link link-text plan date))
1953 result)
1954 (while task-info
1955 (setq result
1956 (cons
1957 (car (let ((search (memq (car fields) properties)))
1958 (if search (cdr search) task-info)))
1959 result))
1960 (setq fields (cdr fields))
1961 (setq task-info (cdr task-info)))
1962 (nreverse result)))
1964 (defun planner-current-task-info ()
1965 "Parse the current line and return the task information as a list."
1966 (planner-task-info-from-string (planner-page-name)
1967 (buffer-substring
1968 (planner-line-beginning-position)
1969 (planner-line-end-position))))
1971 (defun planner-task-page (info)
1972 "Return the page of a task given INFO." (nth 0 info))
1973 (defun planner-task-priority (info)
1974 "Return the priority of a task given INFO." (nth 1 info))
1975 (defun planner-task-number (info)
1976 "Return the number of a task given INFO." (nth 2 info))
1977 (defun planner-task-status (info)
1978 "Return the status of a task given INFO." (nth 3 info))
1979 (defun planner-task-description (info)
1980 "Return the description of a task given INFO." (nth 4 info))
1981 (defun planner-task-link (info)
1982 "Return the page linked to by a task given INFO." (nth 5 info))
1983 (defun planner-task-link-text (info)
1984 "Return the link text of a task given INFO." (nth 6 info))
1985 (defun planner-task-plan (info)
1986 "Return the planner page of a task given INFO." (nth 7 info))
1987 (defun planner-task-date (info)
1988 "Return the planner date of a task given INFO." (nth 8 info))
1989 (defun planner-task-link-as-list (info)
1990 "Return a list of all the pages this task is on."
1991 (delq nil (list (nth 7 info) (nth 8 info))))
1993 ;;;_ + Creation
1995 (defvar planner-create-task-from-info-function
1996 'planner-create-task-from-info-basic
1997 "Function for creating tasks.
1998 Should accept the same arguments as `planner-create-task-from-info-basic'.")
2000 (defun planner-create-task-from-info (info &optional priority number status description link-text date plan)
2001 "Create a task in the date and plan pages based on INFO.
2002 Optional arguments PRIORITY, NUMBER, STATUS, DESCRIPTION,
2003 LINK-TEXT, DATE, and PLAN override those in INFO."
2004 (funcall planner-create-task-from-info-function info priority
2005 number status description link-text date plan))
2007 (defun planner-create-task-from-info-basic
2008 (info &optional priority number status description link-text date plan)
2009 "Create a task in the date and plan pages based on INFO.
2010 Optional arguments PRIORITY, NUMBER, STATUS, DESCRIPTION,
2011 LINK-TEXT, DATE, and PLAN override those in INFO."
2012 (save-window-excursion
2013 (save-excursion
2014 ;; page-name priority number status description
2015 ;; link link-text plan date
2016 ;; Create the task in the plan page
2017 (let ((plan-page (or plan (planner-task-plan info)))
2018 (date-page (or date (planner-task-date info)))
2019 (live-buffers
2020 (and (equal planner-tasks-file-behavior 'close)
2021 (buffer-list))))
2022 (when plan-page
2023 (if (string-match planner-date-regexp
2024 plan-page)
2025 (setq plan-page nil)))
2026 (when (and plan-page (not (string= plan-page "")))
2027 (planner-find-file plan-page)
2028 (planner-seek-task-creation-point)
2029 (insert (planner-format-task info priority number
2030 status description
2031 (planner-make-link date-page)
2032 (planner-make-link date-page))
2033 "\n"))
2034 ;; Create the task in the date page
2035 (when (and date-page (not (string= date-page "")))
2036 (planner-goto date-page)
2037 (planner-seek-task-creation-point)
2038 (insert (planner-format-task info priority number
2039 status description
2041 link-text
2042 (planner-task-link-text info))
2043 plan-page) "\n"))
2044 (forward-line -1)
2045 (run-hooks 'planner-create-task-hook)
2046 (when planner-tasks-file-behavior
2047 (planner-save-buffers live-buffers t))))))
2049 (defvar planner-task-format "#%s%s %s %s%s"
2050 "Format used by `planner-format-task' when inserting new tasks.")
2052 (defun planner-format-task
2053 (task-info &optional priority number status description link-text link)
2054 "Return a string containing TASK-INFO ready to be inserted into a page.
2055 Non-nil values of PRIORITY, NUMBER, STATUS, DESCRIPTION, LINK-TEXT,
2056 and LINK override TASK-INFO."
2057 (format planner-task-format
2058 (or priority (planner-task-priority task-info))
2059 (if planner-use-task-numbers
2060 (format "%-2s" (or number (planner-task-number task-info) ""))
2062 (or status (planner-task-status task-info))
2063 (or description (planner-task-description task-info))
2064 (let ((text (or link-text
2065 (and link (planner-make-link link))
2066 (planner-task-link-text task-info))))
2067 (if (and text (not (equal text "")))
2068 (concat " ("
2069 text
2070 ")")
2071 ""))))
2073 ;;;_ + Scheduling
2075 (defun planner-copy-or-move-region (beg end &optional date muffle-errors)
2076 "Move all tasks from BEG to END to DATE.
2077 If this is the original task, it copies it instead of moving.
2078 Most of the time, the original should be kept in a planning file,
2079 but this is not required. If BEG is at the end of a line, the
2080 first task moved will be the one on the following line. If END is
2081 at the beginning of a line, the last task moved will be the one
2082 on the previous line. If MUFFLE-ERRORS is non-nil, no errors will
2083 be reported."
2084 (interactive "r")
2085 (unless date (setq date
2086 (let ((planner-expand-name-favor-future-p
2087 (or planner-expand-name-favor-future-p
2088 planner-task-dates-favor-future-p)))
2089 (planner-read-date))))
2090 (let ((start (if (< beg end) beg end))
2091 (finish (if (< beg end) end beg))
2092 (buffer (current-buffer))
2093 (error-count 0)
2094 (count 0)
2095 (live-buffers (when (equal planner-tasks-file-behavior
2096 'close)
2097 (buffer-list))))
2098 ;; Invoke planner-copy-or-move-task on each line in reverse
2099 (let ((planner-tasks-file-behavior nil)
2100 (task-info)
2101 prev-point)
2102 (save-excursion
2103 (save-restriction
2104 (narrow-to-region
2105 (and (goto-char (1+ start)) (planner-line-beginning-position))
2106 (and (goto-char (1- finish))
2107 (min (point-max)
2108 (1+ (planner-line-end-position)))))
2109 (when planner-add-task-at-end-flag
2110 (reverse-region (point-min) (point-max)))
2111 (goto-char (point-max))
2112 (setq prev-point (point))
2113 (while (not (and (bobp)
2114 (= (point) prev-point)))
2115 (forward-line 0)
2116 (setq prev-point (point))
2117 (setq task-info (planner-current-task-info))
2118 ;; Don't move completed or cancelled tasks
2119 (if (and task-info
2120 (not (member (planner-task-status task-info) '("C" "X"))))
2121 (condition-case err
2122 (when (planner-copy-or-move-task date)
2123 (setq count (1+ count)))
2124 (error
2125 (unless (or muffle-errors (not (interactive-p)))
2126 (message
2127 "Error with %s: %s"
2128 (planner-task-description (planner-current-task-info)) err)
2129 (setq error-count (1+ error-count)))
2130 (forward-line -1)
2131 nil))
2132 (forward-line -1)))
2133 (when planner-add-task-at-end-flag
2134 (reverse-region (point-min) (point-max)))
2135 (when (and (not muffle-errors)
2136 (not error-count)
2137 (> error-count 0)
2138 (interactive-p))
2139 (message (if (> error-count 1) "%d errors." "%d error.")
2140 error-count)))))
2141 (when planner-tasks-file-behavior
2142 (planner-save-buffers live-buffers))
2143 (set-buffer buffer)
2144 count)) ; Return the number of tasks moved.
2146 ;;;_ + Navigation
2148 (defvar planner-jump-to-linked-task-function 'planner-jump-to-linked-task-basic
2149 "Function to jump to a linked task. Function should have one
2150 optional parameter: TASK-INFO.")
2152 (defun planner-jump-to-linked-task (&optional task-info)
2153 "Display the task page linked to by the current task or TASK-INFO."
2154 (funcall planner-jump-to-linked-task-function task-info))
2156 (defun planner-jump-to-linked-task-basic (&optional task-info)
2157 "Display the task page linked to by the current task or TASK-INFO."
2158 (interactive)
2159 (let* ((task-info (or task-info (planner-current-task-info)))
2160 (link (and task-info (planner-task-link task-info))))
2161 (when (planner-local-page-p link)
2162 (planner-find-file (planner-task-link task-info))
2163 (widen)
2164 (goto-char (point-min))
2165 (when (search-forward (planner-task-description task-info) nil t)
2166 (beginning-of-line)
2167 t))))
2169 ;;;_ + Convenience
2171 (defvar planner-history-list nil "History list for pages.")
2173 (defvar planner-read-name-function 'planner-read-name-single
2174 "Function to call in order to read the names of pages.")
2176 (defun planner-read-name (file-alist &optional prompt initial)
2177 "Read the name of a valid Wiki page from minibuffer.
2178 FILE-ALIST is a list of (page-name . filename) entries. If PROMPT
2179 is non-nil, it is used as the prompt string. If INITIAL is specified,
2180 it is used as a reasonable default."
2181 (funcall planner-read-name-function file-alist prompt initial))
2183 (defun planner-read-name-single (file-alist &optional prompt initial)
2184 "Read the name of a valid Wiki page from minibuffer with completion.
2185 FILE-ALIST is a list of (page-name . filename) entries. If PROMPT
2186 is non-nil, it is used as the prompt string. If INITIAL is specified,
2187 it is used as a reasonable default."
2188 (let* ((default planner-default-page)
2189 (str (completing-read
2190 (format "%s(default: %s) " (or prompt "Page: ") default)
2191 file-alist nil nil initial 'planner-history-list)))
2192 (cond
2193 ((or (null str) (= (length str) 0)) default)
2194 ((string= str "nil") nil)
2195 (t str))))
2197 (defun planner-read-name-no-completion (names &optional prompt initial)
2198 "Read the name of a valid Wiki page from minibuffer without completion.
2199 FILE-ALIST is a list of (page-name . filename) entries. If PROMPT
2200 is non-nil, it is used as the prompt string. If INITIAL is specified,
2201 it is used as a reasonable default."
2202 (let* ((default planner-default-page)
2203 (str (read-string
2204 (format "%s(default: %s) " (or prompt "Page: ") default)
2205 initial 'planner-history-list default)))
2206 (cond
2207 ((or (null str) (= (length str) 0)) default)
2208 ((string= str "nil") nil)
2209 (t str))))
2211 (defun planner-read-non-date-page (file-alist &optional prompt initial)
2212 "Prompt for a page name that does not match `planner-date-regexp'.
2213 Base completion on FILE-ALIST. If PROMPT is non-nil, use that as
2214 the prompt. If INITIAL is non-nil, use that as the initial contents
2215 of the minibuffer."
2216 (planner-read-name
2217 (delq nil
2218 (mapcar
2219 (lambda (item)
2220 (unless (string-match
2221 (concat "^\\(?:" planner-date-regexp "\\)$")
2222 (car item))
2223 item))
2224 (copy-alist file-alist)))
2225 prompt initial))
2227 (defvar planner-find-task-function 'planner-find-task-basic
2228 "Function to find a task based on INFO and POINT.")
2230 (defun planner-find-task (info &optional point)
2231 "Move point to the character before the task described by INFO.
2232 If POINT is specified, start search from that point."
2233 (funcall planner-find-task-function info point))
2235 (defun planner-find-task-basic (info &optional point)
2236 "Move point to the character before the task described by INFO.
2237 If POINT is specified, start search from that point."
2238 (goto-char (or point (point-min)))
2239 (when (re-search-forward
2240 (concat
2241 "^#[A-C][0-9]*\\s-+.\\s-+"
2242 (regexp-quote (planner-task-description info))) nil t)
2243 (goto-char (planner-line-beginning-position))))
2245 (defun planner-tasks-equal-p (task-a task-b)
2246 "Return t if TASK-A and TASK-B are equivalent.
2247 This is true if they have the same value for priority, status,
2248 description, plan and date."
2249 (and (string= (or (planner-task-priority task-a) "")
2250 (or (planner-task-priority task-b) ""))
2251 (string= (or (planner-task-status task-a) "")
2252 (or (planner-task-status task-b) ""))
2253 (string= (or (planner-task-description task-a) "")
2254 (or (planner-task-description task-b) ""))
2255 (string= (or (planner-task-plan task-a) "")
2256 (or (planner-task-plan task-b) ""))
2257 (string= (or (planner-task-date task-a) "")
2258 (or (planner-task-date task-b) ""))))
2260 (defun planner-save-buffers (&optional buffer-list suppress-fixing skip-buffer)
2261 "Save all planner buffers.
2262 If BUFFER-LIST is a list of buffers, close all buffers not found
2263 in that list. If SUPPRESS-FIXING is non-nil, do not perform any
2264 planner-related modifications such as task sorting. If
2265 SKIP-BUFFER is non-nil, do not save that buffer."
2266 (interactive)
2267 (setq suppress-fixing (and (not planner-tasks-never-suppress-fixing-flag)
2268 suppress-fixing))
2269 (mapcar
2270 (lambda (buffer)
2271 (unless (eq buffer skip-buffer)
2272 (with-current-buffer buffer
2273 ;; Save all planner buffers
2274 (when (and (planner-derived-mode-p 'planner-mode)
2275 buffer-file-name
2276 (planner-page-name)
2277 (not (string= "" (planner-page-name))))
2278 ;; SUPPRESS-FIXING is negated in the following forms because
2279 ;; it makes more sense to let planner-save-buffers do the
2280 ;; usual actions when the parameter is omitted.
2281 (let ((planner-sort-tasks-automatically
2282 (and planner-sort-tasks-automatically
2283 (not suppress-fixing)))
2284 (planner-renumber-tasks-automatically
2285 (and planner-renumber-tasks-automatically
2286 (not suppress-fixing)))
2287 (planner-align-tasks-automatically
2288 (and planner-align-tasks-automatically
2289 (not suppress-fixing)))
2290 (planner-renumber-notes-automatically
2291 (and planner-renumber-notes-automatically
2292 (not suppress-fixing)))
2293 (planner-tasks-save-behavior nil)
2294 (planner-id-update-automatically nil))
2295 (when (buffer-modified-p)
2296 (save-buffer)))
2297 (when (and buffer-list
2298 (not (memq buffer buffer-list)))
2299 (kill-buffer nil))))))
2300 (buffer-list)))
2302 ;;;_ + Extraction
2304 (defvar planner-task-regexp (concat "^#[A-C][0-9]*\\s-+.\\s-+")
2305 "Regexp used to match tasks.")
2307 (defvar planner-live-task-regexp "^#[ABC][0-9]*\\s-+[_oDP]\\s-+"
2308 "Regular expression matching \"live\" tasks.
2309 A task is live if it is not finished and it is not cancelled.")
2311 (defun planner-extract-tasks (pages &optional condition)
2312 "Parse PAGES and extract all tasks.
2313 If CONDITION is non-nil, it should be a function that
2314 accepts the task info as an argument and returns t if
2315 the task should be added to the list."
2316 (with-temp-buffer
2317 (unless (consp (car pages))
2318 (let ((list (planner-file-alist)))
2319 (setq pages (mapcar '(lambda (page)
2320 (cons page (cdr (assoc page list))))
2321 pages))))
2322 (let (result)
2323 (while pages
2324 (erase-buffer)
2325 (insert-file-contents (cdar pages))
2326 (goto-char (point-max))
2327 (while (re-search-backward "^#[A-C]" nil t)
2328 (let ((info
2329 (planner-task-info-from-string
2330 (caar pages)
2331 (buffer-substring
2332 (planner-line-beginning-position)
2333 (planner-line-end-position)))))
2334 (when (and info
2335 (if condition
2336 (funcall condition info)
2338 (setq result (append (list info) result)))))
2339 (setq pages (cdr pages)))
2340 result)))
2342 (defun planner-extract-tasks-with-status (pages status)
2343 "Return all tasks on PAGES with the specified STATUS."
2344 (planner-extract-tasks pages
2345 (lambda (item)
2346 (equal (planner-task-status item)
2347 status))))
2349 (defun planner-tasks-tag (beg end attrs)
2350 "Replace the region BEG to END with a report of tasks.
2351 If status is specified in ATTRS, list tasks matching that status only.
2352 To negate the sense of a match, use a regexp."
2353 (delete-region beg end)
2354 (let* ((status (cdr (assoc "status" attrs)))
2355 (tasks (planner-extract-tasks
2356 (planner-get-day-pages nil nil t)
2357 (if status
2358 (lambda (item)
2359 (string-match status (planner-task-status item)))
2360 nil))))
2361 (while tasks
2362 (insert
2363 (planner-make-link (planner-task-page (car tasks)) nil t)
2364 " | "
2365 (planner-task-priority (car tasks))
2366 " | "
2367 (planner-task-status (car tasks))
2368 " | "
2369 (planner-task-description (car tasks))
2370 "\n")
2371 (setq tasks (cdr tasks)))))
2373 (defvar planner-on-date-page nil
2374 "Internal variable used in `planner-sort-tasks-default-key'.")
2376 (defun planner-sort-tasks-default-key ()
2377 "Provide old sorting behavior.
2378 Day pages sort by status and priority. Plan pages sort by date,
2379 status and priority."
2380 (if planner-on-date-page
2381 (planner-sort-tasks-basic)
2382 (planner-sort-tasks-by-date)))
2384 (defun planner-sort-tasks-basic ()
2385 "Sort tasks by status (_PDXC) and priority (ABC)."
2386 (skip-chars-forward "#ABC")
2387 (let ((case-fold-search t)
2388 (ch (char-before))
2389 status)
2390 (skip-chars-forward "0123456789 ")
2391 (setq status (char-after))
2392 (+ ;(read (current-buffer))
2393 (cond
2394 ((eq status ?P) 1000)
2395 ((eq status ?D) 2000)
2396 ((eq status ?X) 3000)
2397 ((eq status ?C) 4000)
2398 (t 0))
2399 (cond ((eq ch ?A) 100)
2400 ((eq ch ?B) 200)
2401 ((eq ch ?C) 300)
2402 (t 0)))))
2404 (defun planner-sort-tasks-by-date ()
2405 "Sort tasks by date, status and priority."
2406 (skip-chars-forward "#ABC")
2407 (let ((ch (char-before))
2408 status)
2409 (skip-chars-forward "0123456789 ")
2410 (setq status (char-after))
2411 (goto-char (planner-line-end-position))
2412 (skip-chars-backward "]) ")
2413 (format "%1c%1c%10s"
2414 (if (or (= status ?X)
2415 (= status ?C))
2416 status ?\ )
2418 (if (= (skip-chars-backward "0123456789.")
2419 -10)
2420 (buffer-substring (point)
2421 (+ 10 (point)))
2422 planner-sort-undated-tasks-equivalent))))
2424 (defun planner-sort-tasks-by-link ()
2425 "Sort tasks by status, priority and link."
2426 (let ((info (planner-current-task-info)))
2427 (concat ;(read (current-buffer))
2428 (cond
2429 ((string= (planner-task-status info) "P") "1")
2430 ((string= (planner-task-status info) "D") "2")
2431 ((string= (planner-task-status info) "X") "3")
2432 ((string= (planner-task-status info) "C") "4")
2433 (t "0"))
2434 (planner-task-priority info)
2435 (or (planner-task-link info) ""))))
2437 (defun planner-sort-tasks ()
2438 "Sort the tasks.
2439 On day pages, sort according to priority and position. On plan
2440 pages, sort according to status, priority, date, and position."
2441 (interactive)
2442 (let ((case-fold-search nil)
2443 (planner-on-date-page (string-match planner-date-regexp
2444 (planner-page-name)))
2445 (old-task (planner-current-task-info))
2446 (line-offset (- (point) (planner-line-beginning-position)))
2447 (old-point (point)))
2448 (goto-char (point-min))
2449 (while (re-search-forward "^#[A-C][0-9]*" nil t)
2450 (goto-char (match-beginning 0))
2451 (let ((here (point)))
2452 (while (and (char-after) (= (char-after) ?#))
2453 (forward-line 1))
2454 (save-restriction
2455 (narrow-to-region here (point))
2456 (goto-char here)
2457 (condition-case err
2458 (sort-subr nil
2459 'forward-line 'end-of-line
2460 planner-sort-tasks-key-function nil
2461 nil)
2462 (wrong-number-of-arguments ; OLD EMACS, 5 args
2463 (sort-subr nil
2464 'forward-line 'end-of-line
2465 planner-sort-tasks-key-function nil)))
2466 (goto-char (point-max)))))
2467 (if old-task
2468 (progn
2469 (planner-find-task old-task)
2470 (forward-char line-offset))
2471 (goto-char old-point))
2472 nil)) ; Must return nil because of write-file-functions
2474 (defun planner-sort-tasks-maybe ()
2475 "Sort tasks depending on `planner-sort-tasks-automatically'."
2476 (when planner-sort-tasks-automatically
2477 (planner-sort-tasks)))
2479 (defun planner-renumber-tasks ()
2480 "Update task numbering to be in sequence once again."
2481 (interactive)
2482 (let ((old-point (point)))
2483 (goto-char (point-min))
2484 (let ((counters (list (cons "A" 1) (cons "B" 1) (cons "C" 1))))
2485 (while (re-search-forward "^#\\([A-C]\\)\\([0-9]+\\)" nil t)
2486 (let ((counter (assoc (match-string 1) counters)))
2487 (replace-match (number-to-string (cdr counter)) t t nil 2)
2488 (setcdr counter (1+ (cdr counter))))))
2489 (goto-char old-point))
2490 nil) ; Must return nil because of write-file-functions
2492 (defun planner-renumber-tasks-maybe ()
2493 "Renumber tasks depending on `planner-renumber-tasks-automatically'."
2494 (when planner-renumber-tasks-automatically
2495 (planner-renumber-tasks)))
2497 (defun planner-fix-tasks ()
2498 "Sort, renumber and align tasks."
2499 (interactive)
2500 (planner-sort-tasks)
2501 (planner-renumber-tasks)
2502 (planner-align-tasks))
2504 ;;;_ + Notes
2506 ;;;###autoload
2507 (defun planner-create-note (&optional page)
2508 "Create a note to be remembered in PAGE (today if PAGE is nil).
2509 If `planner-reverse-chronological-notes' is non-nil, create the
2510 note at the beginning of the notes section; otherwise, add it to
2511 the end. Position point after the anchor."
2512 (interactive (list (and (planner-derived-mode-p 'planner-mode)
2513 (planner-page-name))))
2514 (planner-goto (or page
2515 (and (planner-derived-mode-p 'planner-mode)
2516 (planner-page-name))
2517 (planner-today)))
2518 (planner-seek-to-first 'notes)
2519 (save-restriction
2520 (when (planner-narrow-to-section 'notes)
2521 (let ((total 0))
2522 (goto-char (point-min))
2523 (while (re-search-forward "^\.#\\([0-9]+\\)\\s-+" nil t)
2524 (setq total (max total (string-to-number (match-string 1)))))
2525 (if planner-reverse-chronological-notes
2526 (progn (goto-char (point-min))
2527 (forward-line 1)
2528 (skip-chars-forward "\n"))
2529 (goto-char (point-max))
2530 (skip-chars-backward "\n")
2531 (when (= (forward-line 1) 1) (insert "\n"))
2532 (when (= (forward-line 1) 1) (insert "\n")))
2533 (insert ".#" (number-to-string (1+ total)) " ")
2534 (unless (eobp) (save-excursion (insert "\n\n")))
2535 (1+ total)))))
2537 (defun planner-delete-note ()
2538 "Delete the current note."
2539 (interactive)
2540 (save-window-excursion
2541 (let ((info (planner-current-note-info)))
2542 (when info
2543 (save-window-excursion
2544 (when (planner-jump-to-linked-note info)
2545 (save-restriction
2546 (planner-narrow-to-note)
2547 (delete-region (point-min) (point-max)))))
2548 (save-restriction
2549 (planner-narrow-to-note)
2550 (delete-region (point-min) (point-max)))))))
2552 (defun planner-format-note (info &optional anchor title timestamp link body)
2553 "Return the string representation of INFO.
2554 ANCHOR, TITLE, TIMESTAMP, LINK and BODY override INFO if present."
2555 (unless anchor (setq anchor (planner-note-anchor info)))
2556 (unless title (setq title (planner-note-title info)))
2557 (unless timestamp (setq timestamp (planner-note-timestamp info)))
2558 (unless link (setq link (planner-note-link info)))
2559 (unless body (setq body (planner-note-body info)))
2560 (concat (if (and anchor (not (string= "" anchor)))
2561 (concat ".#" anchor " ")
2563 title
2564 (if (and timestamp (not (string= "" timestamp)))
2565 (concat " " timestamp)
2567 (if (and link (not (string= "" link))) (concat " (" link ")") "")
2568 (if (and body (not (string= "" body))) body "")))
2570 (defun planner-update-note ()
2571 "Copy the text from this note to the linked note, if any."
2572 (interactive)
2573 (save-window-excursion
2574 (let ((info (planner-current-note-info))
2575 text)
2576 (save-restriction
2577 (when (planner-narrow-to-note)
2578 (setq text (buffer-substring-no-properties (point-min) (point-max)))
2579 ;; Get rid of the anchor.
2580 (when (string-match "^\\.#[0-9]+\\s-+" text)
2581 (setq text (replace-match "" nil t text)))
2582 ;; Get rid of the link
2583 (when (string-match "\\s-+(\\[\\[.+?\\]\\])" text)
2584 (setq text (replace-match "" nil t text)))))
2585 ;; Insert the new body
2586 (when (planner-jump-to-linked-note)
2587 (save-restriction
2588 (when (planner-narrow-to-note)
2589 (goto-char (point-min))
2590 (skip-chars-forward ".#0-9")
2591 (delete-region (point) (point-max))
2592 (insert " " text)
2593 (goto-char (point-min))
2594 (goto-char (planner-line-end-position))
2595 (insert " ("
2596 (planner-make-link
2597 (concat (planner-note-page info) "#"
2598 (planner-note-anchor info)))
2599 ")")))))))
2601 ;; Case 1a: Date and plan page exist, new plan page wanted
2602 ;; Case 1b: Date page exists, no plan yet, plan page wanted
2603 ;; Case 2: Date and plan page exist, no plan page wanted
2604 ;; Case 3: No date, just plan page
2605 (defun planner-replan-note (page)
2606 "Change or assign the plan page for the current note.
2607 PAGE-NAME is the new plan page for the note."
2608 (interactive
2609 (list (planner-read-non-date-page
2610 (planner-file-alist) nil
2611 (planner-note-link-text (planner-current-note-info)))))
2612 (let ((info (planner-current-note-info t)))
2613 (when (and page
2614 (or (string= page (planner-note-plan info))
2615 (string= page (planner-note-date info))))
2616 (error "Same plan page"))
2617 (when (null (or page (planner-note-date info)))
2618 (error "Cannot unplan note without day page"))
2619 (save-window-excursion
2620 ;; Delete the old plan note
2621 (when (planner-note-plan info)
2622 (when (string-match planner-date-regexp (planner-note-page info))
2623 (planner-jump-to-linked-note info))
2624 (save-restriction
2625 (planner-narrow-to-note)
2626 (delete-region (point-min) (point-max))))
2627 (let (new)
2628 (when page
2629 ;; Create note on plan page
2630 (setq new (planner-create-note page))
2631 (insert (planner-format-note
2632 info "" nil nil
2633 (if (planner-note-date info)
2634 (planner-make-link
2635 (concat (planner-note-date info)
2637 (planner-note-anchor info)))
2638 ""))))
2639 ;; Update note on date page, if any
2640 (forward-line -1)
2641 (when (planner-note-date info)
2642 (if (string-match planner-date-regexp (planner-note-page info))
2643 (progn
2644 (planner-find-file (planner-note-date info))
2645 (goto-char (point-min))
2646 (re-search-forward (concat "^\\.#" (planner-note-anchor info)
2647 "\\s-")
2648 nil t))
2649 (planner-jump-to-linked-note info))
2650 (save-restriction
2651 (planner-narrow-to-note)
2652 (delete-region (point-min) (point-max))
2653 (insert (planner-format-note
2654 info nil nil nil
2655 (if new
2656 (planner-make-link
2657 (concat (planner-link-base page) "#"
2658 (number-to-string new)))
2659 "")))))))))
2661 ;; Improvements:
2663 ;; - Link back to the task? If we can figure out how to stably link to
2664 ;; a task in the first place...
2666 ;; - Should plan-page-p default to t? be a customizable variable? What
2667 ;; should it be called? I have the urge to write
2668 ;; planner-create-note-from-task-behavior which can have the
2669 ;; following values: 'day, 'plan, 'copy, 'xref ...
2671 (defun planner-create-note-from-task (&optional plan-page-p)
2672 "Create a note based on the current task.
2673 Argument PLAN-PAGE-P is used to determine whether we put the new
2674 note on the task's plan page or on the current page."
2675 (interactive "P")
2676 (let ((task-info (planner-current-task-info))
2677 note-num)
2678 (when task-info
2679 ;; If PLAN-PAGE-P and the task has a plan page, create a note on
2680 ;; the plan page. If not, create it on the current page.
2681 (when (and plan-page-p
2682 (string= (planner-task-date task-info)
2683 (planner-task-page task-info)))
2684 (planner-jump-to-linked-task task-info))
2685 (setq note-num (planner-create-note (planner-page-name)))
2686 (save-excursion
2687 (save-window-excursion
2688 (when (planner-find-task task-info)
2689 (planner-edit-task-description
2690 (concat (planner-task-description task-info) " "
2691 (planner-make-link
2692 (concat (planner-page-name) "#"
2693 (number-to-string note-num))
2694 (format "(%d)" note-num)))))))
2695 (insert " " (planner-task-description task-info) "\n\n"))))
2697 (defun planner-create-note-from-context (&optional plan-page-p)
2698 "Create a note based on the current context.
2699 If on a task item, call `planner-create-note-from-task'.
2700 Anywhere else, call `Footnote-add-footnote' if footnote has been
2701 loaded, else, call `planner-create-note'."
2704 (defun planner-narrow-to-note (&optional page note-number)
2705 "Narrow to the specified note. Widen and return nil if note is not found.
2706 If PAGE is nil, use current page.
2707 If NOTE-NUMBER is nil, use current note.
2708 Undefined behavior if PAGE is (non-nil and not today) and NOTE-NUMBER is nil."
2709 (when page (planner-goto page))
2710 (save-excursion
2711 (let (beginning)
2712 (if note-number
2713 (progn
2714 (goto-char (point-min))
2715 (when (re-search-forward (concat "^\\.#" note-number) nil t)
2716 (setq beginning (match-beginning 0))))
2717 (goto-char (planner-line-end-position))
2718 (when (re-search-backward "^\\.#[0-9]+" nil t)
2719 (setq beginning (planner-line-beginning-position))))
2720 (when beginning
2721 ;; Search for the end
2722 (forward-line 1)
2723 (narrow-to-region
2724 beginning
2725 (or (save-excursion
2726 (and (re-search-forward "^\\(\\.#\\|* \\)" nil t)
2727 (match-beginning 0)))
2728 (point-max)))
2729 t))))
2731 (defun planner-note-page (note-info)
2732 "Return the page specified by NOTE-INFO."
2733 (elt note-info 0))
2734 (defun planner-note-anchor (note-info)
2735 "Return the anchor specified by NOTE-INFO."
2736 (elt note-info 1))
2737 (defun planner-note-title (note-info)
2738 "Return the title specified by NOTE-INFO."
2739 (elt note-info 2))
2740 (defun planner-note-timestamp (note-info)
2741 "Return the timestamp specified by NOTE-INFO."
2742 (elt note-info 3))
2743 (defun planner-note-link (note-info)
2744 "Return the link specified by NOTE-INFO."
2745 (elt note-info 4))
2746 (defun planner-note-link-text (note-info)
2747 "Return the link text specified by NOTE-INFO."
2748 (elt note-info 4))
2749 (defun planner-note-body (note-info)
2750 "Return the timestamp specified by NOTE-INFO."
2751 (elt note-info 5))
2753 (defun planner-note-date (note-info)
2754 "Return the date for NOTE-INFO."
2755 (cond
2756 ((string-match planner-date-regexp (planner-note-page note-info))
2757 (planner-note-page note-info))
2758 ((and (planner-note-link note-info)
2759 (string-match planner-date-regexp (planner-note-link note-info)))
2760 (planner-link-base (planner-note-link note-info)))))
2762 (defun planner-note-plan (note-info)
2763 "Return the date for NOTE-INFO."
2764 (cond
2765 ((null (string-match planner-date-regexp (planner-note-page note-info)))
2766 (planner-note-page note-info))
2767 ((and (planner-note-link note-info)
2768 (null (string-match planner-date-regexp
2769 (planner-note-link note-info))))
2770 (planner-link-base (planner-note-link note-info)))))
2772 (defun planner-current-note-info (&optional include-body)
2773 "Parse the current note and return the note information as a list.
2774 The list is of the form (PAGE ANCHOR TITLE TIMESTAMP LINK BODY).
2775 If INCLUDE-BODY is non-nil, the list will include the body of the
2776 note."
2777 (save-excursion
2778 (save-restriction
2779 (when (planner-narrow-to-note)
2780 (goto-char (point-min))
2781 (when (looking-at "^\\.#\\([0-9]+\\)\\s-+\\(.+\\)")
2782 (let ((anchor (planner-match-string-no-properties 1))
2783 (title (planner-match-string-no-properties 2))
2784 timestamp link)
2785 (when (string-match
2786 (concat
2787 "\\s-+(\\("
2788 (if (featurep 'planner-multi)
2789 (concat "\\(" muse-explicit-link-regexp "\\)"
2790 "\\("
2791 (regexp-quote planner-multi-separator)
2792 muse-explicit-link-regexp
2793 "\\)*")
2794 muse-explicit-link-regexp)
2795 "\\))\\s-*$")
2796 title)
2797 (setq link (planner-match-string-no-properties 1 title))
2798 (setq title (replace-match "" nil t title)))
2799 (when (string-match "\\s-*\\([0-9]+:[0-9][0-9]\\)" title)
2800 (setq timestamp (planner-match-string-no-properties 1 title))
2801 (setq title (replace-match "" nil t title)))
2802 (list (planner-page-name) anchor title timestamp link
2803 (and include-body (buffer-substring-no-properties
2804 (planner-line-end-position)
2805 (point-max))))))))))
2807 (defun planner-search-notes-internal (regexp &optional limit include-body)
2808 "Return an alist of all notes in daily plan pages containing REGEXP.
2809 The alist is of the form ((REFERENCE TITLE BODY) (REFERENCE TITLE BODY)
2810 ...). If LIMIT is non-nil, do not search days before LIMIT. If
2811 INCLUDE-BODY is non-nil, return the body text, else return nil."
2812 (let ((pages (planner-get-day-pages limit t))
2813 filename
2814 page start anchor text results title page-results)
2815 (while pages
2816 (setq page (caar pages)
2817 filename (cdar pages))
2818 (with-temp-buffer
2819 (when (file-readable-p filename)
2820 (insert-file-contents filename)
2821 (setq start nil)
2822 (setq page-results nil)
2823 ;; Find the first note
2824 (when (re-search-forward "\\.\\(#[0-9]+\\)\\s-+\\(.*\\)" nil t)
2825 (setq start (match-beginning 2))
2826 (setq anchor (match-string 1))
2827 (setq title (match-string 2)))
2828 (while (re-search-forward "\\.\\(#[0-9]+\\)\\s-+\\(.*\\)" nil t)
2829 ;; The text between start and (1- (match-beginning 0))
2830 ;; is the note body.
2831 (when (save-excursion
2832 (goto-char (planner-line-beginning-position))
2833 (save-match-data (re-search-backward regexp start t)))
2834 (add-to-list 'page-results
2835 (list (concat filename anchor)
2836 (concat page anchor)
2837 title
2838 (if include-body
2839 (buffer-substring-no-properties
2840 start
2841 (planner-line-beginning-position))))))
2842 (setq start (match-beginning 2))
2843 (setq anchor (match-string 1))
2844 (setq title (match-string 2)))
2845 (when start
2846 (goto-char (point-max))
2847 (when (save-excursion (re-search-backward regexp start t))
2848 (add-to-list 'page-results
2849 (list (concat filename anchor)
2850 (concat page anchor)
2851 title
2852 (if include-body
2853 (buffer-substring-no-properties
2854 start
2855 (point)))))))
2856 (when planner-reverse-chronological-notes
2857 (setq page-results (nreverse page-results))))
2858 (setq results (append page-results results)))
2859 (setq pages (cdr pages)))
2860 results))
2862 (defun planner-jump-to-linked-note (&optional note-info)
2863 "Display the note linked to by the current note or NOTE-INFO if non-nil."
2864 (interactive)
2865 (setq note-info (or note-info (planner-current-note-info)))
2866 (when (and (planner-note-link note-info)
2867 (save-window-excursion
2868 (planner-visit-link (planner-note-link note-info))))
2869 (planner-visit-link (planner-note-link note-info))
2870 (widen)
2873 (defun planner-renumber-notes ()
2874 "Update note numbering."
2875 (interactive)
2876 (let ((old-point (point))
2877 (counter 1))
2878 (goto-char
2879 (if planner-reverse-chronological-notes (point-max) (point-min)))
2880 (while (if planner-reverse-chronological-notes
2881 (re-search-backward "^\\.#\\([0-9]+\\)" nil t)
2882 (re-search-forward "^\\.#\\([0-9]+\\)" nil t))
2883 (replace-match (number-to-string counter) t t nil 1)
2884 (when planner-reverse-chronological-notes
2885 (goto-char (planner-line-beginning-position)))
2886 (setq counter (1+ counter)))
2887 (goto-char old-point))
2888 nil) ; Must return nil because of write-file-functions
2890 (defun planner-renumber-notes-maybe ()
2891 "Renumber notes depending on `planner-renumber-notes-automatically'."
2892 (when planner-renumber-notes-automatically
2893 (planner-renumber-notes)))
2895 ;;;_ + Highlighting
2897 (defgroup planner-fontlock nil
2898 "Font-locking for planner.el pages."
2899 :prefix "planner-"
2900 :group 'planner)
2902 (defface planner-completed-task-face
2903 (if (featurep 'xemacs)
2904 '((t (:strikethru t :foreground "gray")))
2905 '((t (:strike-through t :foreground "gray"))))
2906 "Face for completed tasks."
2907 :group 'planner-fontlock)
2909 (defface planner-cancelled-task-face
2910 (if (featurep 'xemacs)
2911 '((t (:strikethru t :foreground "gray")))
2912 '((t (:strike-through t :foreground "gray"))))
2913 "Face for cancelled tasks."
2914 :group 'planner-fontlock)
2916 (defface planner-delegated-task-face
2917 '((t (:underline t)))
2918 "Face for delegated tasks."
2919 :group 'planner-fontlock)
2921 (defface planner-in-progress-task-face
2922 (if (featurep 'xemacs)
2923 '((t (:bold t)))
2924 '((t (:slant oblique))))
2925 "Face for tasks in progress."
2926 :group 'planner-fontlock)
2927 (defface planner-high-priority-task-face '((t (:foreground "red")))
2928 "Face for high-priority tasks."
2929 :group 'planner-fontlock)
2930 (defface planner-medium-priority-task-face '((t (:foreground "green")))
2931 "Face for medium-priority tasks."
2932 :group 'planner-fontlock)
2933 (defface planner-low-priority-task-face '((t (:foreground "blue")))
2934 "Face for low-priority tasks."
2935 :group 'planner-fontlock)
2937 (defface planner-note-headline-face
2938 '((((class color) (background light))
2939 (:foreground "dark slate blue" :bold t))
2940 (((class color) (background dark))
2941 (:foreground "pale turquoise" :bold t))
2942 (t (:bold t)))
2943 "Face for note headlines."
2944 :group 'planner-fontlock)
2946 ;; Thanks to Oliver (oik AT gmx DOT net)
2947 (defun planner-align-tasks ()
2948 "Align tasks neatly.
2949 You can add this to `write-file-functions'to have the tasks
2950 automatically lined up whenever you save. For best results,
2951 ensure `planner-align-tasks' is run after
2952 `planner-renumber-tasks'."
2953 (interactive)
2954 (save-excursion
2955 (goto-char (point-min))
2956 (while (re-search-forward "^#\\([A-C]\\)\\([0-9]*\\)\\(\\s-+\\)" nil t)
2957 (replace-match
2958 ;; Ugly hack!
2959 ;; (make-string (max (- (length (match-string 2))) 0) ?\s)
2960 ;; is better, but relies on a CVSism.
2961 (let ((length (length (match-string 2))))
2962 (cond
2963 ((and (= length 0) planner-use-task-numbers) " ")
2964 ((= length 1) " ")
2965 (t " ")))
2966 t t nil 3)))
2967 nil) ; Return nil so that we can add this to write-file-functions
2969 (defun planner-align-tasks-maybe ()
2970 "Align tasks depending on `planner-align-tasks-automatically'."
2971 (when planner-align-tasks-automatically
2972 (planner-align-tasks)))
2974 (defun planner-align-table ()
2975 "Align table neatly. Take account of links which hides characters when
2976 displayed.
2977 Perhaps, guts of this should really be inside muse..."
2978 (interactive)
2979 (save-excursion
2980 (when (fboundp 'align-regexp)
2981 (align-regexp (point-min) (point-max) "\\(\\s-*\\)|" 1 1 t))
2982 (goto-char (point-min))
2983 (while (re-search-forward (concat "^" muse-explicit-link-regexp) nil t)
2984 (let ((link (match-string 1))
2985 (desc (match-string 2)))
2986 (insert " "
2987 (make-string (if desc
2988 (+ 2 (length link))
2989 0) (aref " " 0)))))))
2991 ;; FIXME: Is there a better way to do this?
2993 (defun planner-highlight-region (beg end identifier priority properties)
2994 "Add the specified text properties to the overlay or region.
2995 BEG and END are the start and end of the region. IDENTIFIER is a
2996 symbol that identifies this particular overlay. PRIORITY controls
2997 how important this overlay is. PROPERTIES is a list of properties
2998 or attributes to apply."
2999 (if (featurep 'xemacs)
3000 (let ((extent (make-extent beg end)))
3001 (set-extent-properties extent properties)
3002 (set-extent-property extent 'priority priority))
3003 (if (functionp 'overlay-put)
3004 (progn
3005 (let ((overlay (make-overlay beg end)))
3006 (overlay-put overlay identifier t)
3007 (overlay-put overlay 'planner t)
3008 (overlay-put overlay 'priority priority)
3009 (while properties
3010 (overlay-put overlay (car properties) (cadr properties))
3011 (setq properties (cddr properties)))))
3012 (add-text-properties beg end properties))))
3014 (defcustom planner-hide-task-status-when-highlighting nil
3015 "*If non-nil, hide task status when font-locking."
3016 :type 'boolean
3017 :group 'planner-fontlock)
3019 (defun planner-highlight-tasks (beg end &optional verbose)
3020 "Highlight tasks from BEG to END. VERBOSE is ignored."
3021 (goto-char beg)
3022 (while (re-search-forward (concat "^#\\([A-C]\\)\\([0-9]*\\)\\s-+\\("
3023 planner-marks-regexp
3024 "\\)\\s-") end t)
3025 (let ((mark (match-string 3))
3026 (priority (match-string 1))
3027 faces)
3028 (setq faces
3029 (append
3030 (cond
3031 ((string= priority "A") '(planner-high-priority-task-face))
3032 ((string= priority "B") '(planner-medium-priority-task-face))
3033 ((string= priority "C") '(planner-low-priority-task-face)))
3034 (cond
3035 ((string= mark "X") '(planner-completed-task-face))
3036 ((string= mark "D") '(planner-delegated-task-face))
3037 ((string= mark "C") '(planner-cancelled-task-face))
3038 ((string= mark "o") '(planner-in-progress-task-face)))))
3039 (if (featurep 'xemacs)
3040 (mapcar (lambda (face)
3041 (when face
3042 (planner-highlight-region
3043 (match-beginning 0) (match-end 3) 'planner-task 50
3044 (list 'face face)))) faces)
3045 (planner-highlight-region
3046 (match-beginning 0) (match-end 3) 'planner-task 50
3047 (list 'face (mapcar 'face-attr-construct faces))))
3048 (planner-highlight-region
3049 (match-end 3) (planner-line-end-position)
3050 'planner-task
3052 (list 'face
3053 (cond
3054 ((string= mark "X") 'planner-completed-task-face)
3055 ((string= mark "D") 'planner-delegated-task-face)
3056 ((string= mark "C") 'planner-cancelled-task-face)
3057 ((string= mark "o") 'planner-in-progress-task-face))))
3058 (when planner-hide-task-status-when-highlighting
3059 (planner-highlight-region
3060 (match-beginning 3) (1+ (match-end 3))
3061 'planner-task
3063 (list 'invisible t))))))
3065 (defun planner-highlight-notes (beg end &optional verbose)
3066 "Highlight notes as second-level headers from BEG to END.
3067 VERBOSE is ignored."
3068 (goto-char beg)
3069 (while (re-search-forward "^\\.#\\([0-9]+\\) [^(\n]+" end t)
3070 (add-text-properties
3071 (match-beginning 0) (match-end 0)
3072 '(face planner-note-headline-face))))
3074 (defun planner-notes-get-headlines (&optional limit)
3075 "Return note headlines on the current page.
3076 If LIMIT is non-nil, return only that many from the top."
3077 (when (stringp limit) (setq limit (string-to-number limit)))
3078 (let (headlines)
3079 (save-excursion
3080 (save-restriction
3081 (widen)
3082 (goto-char (point-min))
3083 (while (and
3084 (re-search-forward "^.\\(#[0-9]+\\)\\s-+\\(.+\\)" nil t)
3085 (if limit
3086 (> limit 0)
3088 (add-to-list
3089 'headlines
3090 (cons
3091 (planner-match-string-no-properties 1)
3092 (planner-match-string-no-properties 2))
3094 (if limit (setq limit (1- limit))))))
3095 headlines))
3097 ;;;_* Indexing
3099 ;; I want to compress the list of day pages. Arranging them by month
3100 ;; may be a good strategy, although a calendar would be optimal.
3102 (defun planner-index ()
3103 "Display an index of all known Wiki pages."
3104 (interactive)
3105 (let ((muse-current-project (muse-project planner-project)))
3106 (message "Generating Muse index...")
3107 (pop-to-buffer (planner-generate-index))
3108 (goto-char (point-min))
3109 (planner-mode)
3110 (message "Generating Muse index...done")))
3112 (defun planner-generate-index (&optional as-list exclude-private)
3113 "Generate an index of all Wiki pages.
3114 List planner pages separately. If AS-LIST is non-nil, format it
3115 as a list. If EXCLUDE-PRIVATE is non-nil, exclude anything for
3116 which `muse-project-private-p' returns non-nil."
3117 (let ((index (planner-index-as-string as-list exclude-private)))
3118 (with-current-buffer (get-buffer-create "*Planner Index*")
3119 (erase-buffer)
3120 (insert index)
3121 (current-buffer))))
3123 (defun planner-index-as-string (&optional as-list exclude-private
3124 exclude-daypages)
3125 "Generate an index of all Wiki pages.
3126 Day pages are listed separately.
3128 If AS-LIST is non-nil, format it as a list.
3130 If EXCLUDE-PRIVATE is non-nil, exclude anything for which
3131 `muse-project-private-p' returns non-nil.
3133 If EXCLUDE-DAYPAGES is non-nil, exclude day pages from the list."
3134 (let ((index (muse-index-as-string as-list exclude-private)))
3135 (with-temp-buffer
3136 (insert index)
3137 (goto-char (point-min))
3138 (delete-matching-lines
3139 "\\[\\[[0-9][0-9][0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9]\\]\\]")
3140 (unless exclude-daypages
3141 (goto-char (point-max))
3142 (unless (bolp) (insert "\n"))
3143 (if planner-publish-dates-first-p
3144 (progn
3145 (goto-char (point-min))
3146 (insert "\n")
3147 (goto-char (point-min)))
3148 (insert "\n"))
3149 (let ((dates (mapcar 'car (planner-list-daily-files)))
3150 month last-month)
3151 (while dates
3152 (setq month (substring (car dates) 0 7))
3153 (unless (string= month last-month)
3154 (setq last-month month)
3155 (insert "\n" month " |"))
3156 (insert " [[" (car dates) "][."
3157 (substring (car dates) 8)
3158 " ]]")
3159 (setq dates (cdr dates)))
3160 (when planner-publish-dates-first-p
3161 (insert "\n"))))
3162 (buffer-string))))
3164 ;;;_ + Info
3166 (defun planner-annotation-from-info ()
3167 "If called from an info node, return an annotation.
3168 Suitable for use in `planner-annotation-functions'."
3169 (when (eq major-mode 'Info-mode)
3170 (planner-make-link
3171 (concat "info://" Info-current-file "#" Info-current-node)
3172 (if (and (not (equal Info-current-file "dir"))
3173 (equal Info-current-node "Top"))
3174 (file-name-nondirectory Info-current-file)
3175 Info-current-node)
3176 t)))
3178 (custom-add-option 'planner-annotation-functions 'planner-annotation-from-info)
3180 ;;;_ + Man
3182 (defun planner-annotation-from-man ()
3183 "If called from a man page, return an annotation.
3184 Suitable for use in `planner-annotation-functions'."
3185 (when (eq major-mode 'Man-mode)
3186 ;; There may be a better way to get the name of the current Man page.
3187 (let ((page (downcase (cadr (cdar Man-page-list)))))
3188 (planner-make-link
3189 (concat "man://" page)
3190 (concat "Man: " page) t))))
3192 (custom-add-option 'planner-annotation-functions 'planner-annotation-from-man)
3194 ;;;_ + WoMan
3196 (defun planner-annotation-from-woman ()
3197 "If called from a woman page, return an annotation.
3198 Suitable for use in `planner-annotation-functions'."
3199 (when (eq major-mode 'woman-mode)
3200 ;; You'd think there'd be a better way to get the page name.
3201 (let* ((buf (split-string (buffer-name)))
3202 (page (substring (nth 2 buf) 0 -1))
3203 (sec (nth 1 buf)))
3204 (planner-make-link
3205 (concat "woman://" page)
3206 (concat "WoMan: " page "(" sec ")") t))))
3208 (custom-add-option 'planner-annotation-functions 'planner-annotation-from-woman)
3210 ;;;_ + Common mail functions
3212 (defun planner-get-name-from-address (address)
3213 "Return the name for ADDRESS to be used in links."
3214 (let ((addr (mail-extract-address-components address)))
3215 (or (car addr) (cadr addr))))
3217 ;;;_* User functions
3219 ;;;_ + Navigation
3221 (defun planner-page-file (page &optional no-check-p)
3222 "Return a filename if PAGE exists within `planner-project'.
3223 If NO-CHECK-P is non-nil, the planner project files are always updated."
3224 (muse-project-page-file page planner-project))
3226 ;;;###autoload
3227 (defun plan (&optional force-days)
3228 "Start your planning for the day, carrying unfinished tasks forward.
3230 If FORCE-DAYS is a positive integer, search that many days in the past
3231 for unfinished tasks.
3232 If FORCE-DAYS is 0 or t, scan all days.
3233 If FORCE-DAYS is nil, use the value of `planner-carry-tasks-forward'
3234 instead, except t means scan only yesterday."
3235 ;; Special treatment of t for planner-carry-tasks-forward is for
3236 ;; backward compatibility.
3237 (interactive "P")
3238 (unless muse-project-alist
3239 (planner-display-warning
3240 (concat "The variable `muse-project-alist' has not defined.\n"
3241 "\nSee the \"Creating Your Planner\" chapter in the Planner"
3242 " manual\nfor details on how to set this up."))
3243 (error "The variable `muse-project-alist' has not been defined"))
3244 (if planner-use-day-pages
3245 (progn
3246 (unless force-days
3247 (setq force-days
3248 (if (equal planner-carry-tasks-forward t)
3250 planner-carry-tasks-forward)))
3251 (when (and (integerp force-days)
3252 (= force-days 0))
3253 (setq force-days t))
3254 (planner-goto-today)
3255 (let* ((today (planner-today))
3256 (names (planner-get-day-pages nil (planner-yesterday)))
3257 (today-buffer (current-buffer))
3258 (planner-tasks-file-behavior
3259 ;; Force saving so that the file list can be updated
3260 (or planner-tasks-file-behavior
3261 'save))
3262 (planner-use-other-window nil)
3263 (live-buffers (and
3264 (equal planner-tasks-file-behavior
3265 'close)
3266 (buffer-list))))
3267 ;; Limit the list for force-days
3268 (when (and (integerp force-days)
3269 (> (length names) force-days))
3270 (setcdr (nthcdr (1- force-days) names) nil))
3271 (when force-days
3272 (while names
3273 (find-file (cdar names))
3274 ;; Attempt to copy all the tasks
3275 (when (not (equal today (planner-page-name)))
3276 (let ((planner-tasks-file-behavior nil))
3277 (save-restriction
3278 (planner-narrow-to-section 'tasks)
3279 (planner-copy-or-move-region (point-min) (point-max)
3280 (planner-today) t)))
3281 (unless (buffer-modified-p)
3282 (kill-buffer (current-buffer))))
3283 (setq names (cdr names))))
3284 ;; Jump to the most recent daily page
3285 (if (or planner-carry-tasks-forward
3286 (planner-page-file today)
3287 (null names))
3288 (planner-goto-today)
3289 (planner-goto (caar names)))
3290 ;; Save/kill files if configured to do so
3291 (when planner-tasks-file-behavior
3292 (planner-save-buffers live-buffers))))
3293 (planner-find-file (or planner-default-page
3294 planner-initial-page))))
3296 (defvar planner-goto-hook '(planner-seek-to-first)
3297 "Functions called after a planner day page is opened.")
3299 ;;;###autoload
3300 (defun planner-goto (date &optional just-show)
3301 "Jump to the planning page for DATE.
3302 If no page for DATE exists and JUST-SHOW is non-nil, don't create
3303 a new page - simply return nil."
3304 (interactive (list (or
3305 (planner-read-date)
3306 (planner-read-non-date-page (planner-file-alist)))))
3307 (if (or (not just-show) (planner-page-exists-p date))
3308 (progn
3309 (planner-find-file date
3310 (if planner-use-other-window
3311 'find-file-other-window
3312 'find-file))
3313 (widen)
3314 (goto-char (point-min))
3315 (run-hooks 'planner-goto-hook)
3316 ;; planner-goto-hook returns nil
3318 ;; File not found, and not supposed to be created.
3319 (when (interactive-p)
3320 (message "No planner file for %s." date))
3321 ;; return nil
3322 nil))
3324 ;;;###autoload
3325 (defun planner-goto-plan-page (page)
3326 "Opens PAGE in the the `planner-project' wiki.
3327 Use `planner-goto' if you want fancy calendar completion."
3328 (interactive (list (planner-read-name (planner-file-alist))))
3329 (planner-find-file page))
3331 ;;;###autoload
3332 (defun planner-show (date)
3333 "Show the plan page for DATE in another window, but don't select it.
3334 If no page for DATE exists, return nil."
3335 (interactive (list (planner-read-date)))
3336 (save-selected-window
3337 (let ((planner-use-other-window t))
3338 (planner-goto date planner-show-only-existing))))
3340 ;;;###autoload
3341 (defun planner-goto-today ()
3342 "Jump to the planning page for today."
3343 (interactive)
3344 (planner-goto (planner-today)))
3346 ;;;###autoload
3347 (defun planner-goto-most-recent ()
3348 "Go to the most recent day with planning info."
3349 (interactive)
3350 (planner-goto
3351 (planner-get-previous-existing-day
3352 (planner-calculate-date-from-day-offset
3353 (planner-get-current-date-filename) 1))))
3355 ;;;###autoload
3356 (defun planner-goto-yesterday (&optional days)
3357 "Goto the planner page DAYS before the currently displayed date.
3358 If DAYS is nil, goes to the day immediately before the currently
3359 displayed date. If the current buffer is not a daily planner
3360 page, calculates date based on today."
3361 (interactive "p")
3362 (let ((planner-use-other-window nil))
3363 (planner-goto (planner-calculate-date-from-day-offset
3364 (planner-get-current-date-filename) (or (- days) -1)))))
3366 ;;;###autoload
3367 (defun planner-goto-tomorrow (&optional days)
3368 "Goto the planner page DAYS after the currently displayed date.
3369 If DAYS is nil, goes to the day immediately after the currently
3370 displayed date. If the current buffer is not a daily planner
3371 page, calculates date based on today."
3372 (interactive "p")
3373 (let ((planner-use-other-window nil))
3374 (planner-goto (planner-calculate-date-from-day-offset
3375 (planner-get-current-date-filename) (or days 1)))))
3377 ;;;###autoload
3378 (defun planner-goto-previous-daily-page ()
3379 "Goto the last plan page before the current date.
3380 The current date is taken from the day page in the current
3381 buffer, or today if the current buffer is not a planner page.
3382 Does not create pages if they do not yet exist."
3383 (interactive)
3384 (let ((planner-use-other-window nil))
3385 (planner-goto (planner-get-previous-existing-day
3386 (planner-get-current-date-filename)))))
3388 ;;;###autoload
3389 (defun planner-goto-next-daily-page ()
3390 "Goto the first plan page after the current date.
3391 The current date is taken from the day page in the current
3392 buffer, or today if the current buffer is not a planner page.
3393 Does not create pages if they do not yet exist."
3394 (interactive)
3395 (let ((planner-use-other-window nil))
3396 (planner-goto (planner-get-next-existing-day
3397 (planner-get-current-date-filename)))))
3399 ;;;_ + Tasks
3401 ;;;_ + Creating
3403 ;;;###autoload
3404 (defun planner-create-high-priority-task-from-buffer ()
3405 "Create a high-priority task based on this buffer.
3406 Do not use this in LISP programs. Instead, set the value of
3407 `planner-default-task-priority' and call `planner-create-task' or
3408 `planner-create-task-from-buffer'."
3409 (interactive)
3410 (let ((planner-default-task-priority "A"))
3411 (call-interactively 'planner-create-task-from-buffer)))
3413 ;;;###autoload
3414 (defun planner-create-medium-priority-task-from-buffer ()
3415 "Create a high-priority task based on this buffer.
3416 Do not use this in LISP programs. Instead, set the value of
3417 `planner-default-task-priority' and call `planner-create-task' or
3418 `planner-create-task-from-buffer'."
3419 (interactive)
3420 (let ((planner-default-task-priority "B"))
3421 (call-interactively 'planner-create-task-from-buffer)))
3423 ;;;###autoload
3424 (defun planner-create-low-priority-task-from-buffer ()
3425 "Create a high-priority task based on this buffer.
3426 Do not use this in LISP programs. Instead, set the value of
3427 `planner-default-task-priority' and call `planner-create-task' or
3428 `planner-create-task-from-buffer'."
3429 (interactive)
3430 (let ((planner-default-task-priority "C"))
3431 (call-interactively 'planner-create-task-from-buffer)))
3433 (defun planner-read-task ()
3434 "Return a list of information for a task."
3435 (list
3436 (read-string "Describe task: ")
3437 (when planner-use-day-pages
3438 (cond
3439 ;; Universal prefix means pick up from current page
3440 ((and current-prefix-arg
3441 (planner-derived-mode-p 'planner-mode)
3442 (string-match planner-date-regexp (planner-page-name)))
3443 (planner-page-name))
3444 ;; Date selected in calendar
3445 ((condition-case nil (calendar-cursor-to-date) (error nil))
3446 (planner-date-to-filename (calendar-cursor-to-date)))
3447 ;; Prompt for date
3448 (t (let ((planner-expand-name-favor-future-p
3449 (or planner-expand-name-favor-future-p
3450 planner-task-dates-favor-future-p)))
3451 (planner-read-date)))))
3452 (when planner-use-plan-pages
3453 (if (and current-prefix-arg (planner-derived-mode-p 'planner-mode)
3454 (not (string-match planner-date-regexp (planner-page-name))))
3455 ;; Universal prefix means pick up from current page
3456 (planner-page-name)
3457 (let ((planner-default-page
3458 (if (and (planner-derived-mode-p 'planner-mode)
3459 (planner-page-name)
3460 (not (string-match planner-date-regexp
3461 (planner-page-name))))
3462 (planner-page-name)
3463 planner-default-page)))
3464 (planner-read-non-date-page
3465 (planner-file-alist)))))
3466 planner-default-task-status))
3468 ;; NOTE: Prefix arg changed to prompt for PLAN-PAGE instead of
3469 ;; set to today
3470 ;;;###autoload
3471 (defun planner-create-task-from-buffer (title date &optional plan-page status)
3472 "Create a new task named TITLE on DATE based on the current buffer.
3473 With a prefix, do not prompt for PLAN-PAGE. The task is
3474 associated with PLAN-PAGE if non-nil. If STATUS is non-nil, use
3475 that as the status for the task. Otherwise, use
3476 `planner-default-task-status'. See `planner-create-task' for more
3477 information."
3478 (interactive (planner-read-task))
3479 (setq planner-default-page plan-page)
3480 (let ((planner-create-task-hook (append planner-create-task-from-buffer-hook
3481 planner-create-task-hook))
3482 (annotation (run-hook-with-args-until-success
3483 'planner-annotation-functions)))
3484 (when (and planner-annotation-symbol-string
3485 (string-match planner-annotation-symbol-string title))
3486 (setq title (replace-match (or annotation "") t t title)
3487 annotation nil))
3488 (planner-create-task title
3489 (when (and date
3490 (string-match planner-date-regexp date))
3491 date)
3492 annotation
3493 plan-page
3494 status)))
3496 (defun planner-create-task (title date &optional annotation plan-page status)
3497 "Create a new task named TITLE based on the current Wiki page.
3498 If DATE is non-nil, makes a daily entry on DATE, else makes an
3499 entry in today's planner page. It's assumed that the current Wiki
3500 page is the page you're using to plan an activity. Any time
3501 accrued to this task will be applied to that page's name in the
3502 timelog file, assuming you use timeclock. If ANNOTATION is
3503 non-nil, it will be used for the page annotation. If PLAN-PAGE is
3504 non-nil, the task is associated with the given page. If STATUS is
3505 non-nil, use that as the status for the task. Otherwise, use
3506 `planner-default-task-status'.
3508 If called with an interactive prefix argument, do not prompt for
3509 PLAN-PAGE.
3511 You probably want to call `planner-create-task-from-buffer' instead."
3512 (interactive
3513 (list
3514 (read-string "Describe task: ")
3515 (when planner-use-day-pages
3516 (cond
3517 ;; Universal prefix means pick up from current page
3518 ((and current-prefix-arg
3519 (planner-derived-mode-p 'planner-mode)
3520 (string-match planner-date-regexp (planner-page-name)))
3521 (planner-page-name))
3522 ;; Date selected in calendar
3523 ((condition-case nil (calendar-cursor-to-date) (error nil))
3524 (planner-date-to-filename (calendar-cursor-to-date)))
3525 ;; Prompt for date
3526 (t (let ((planner-expand-name-favor-future-p
3527 (or planner-expand-name-favor-future-p
3528 planner-task-dates-favor-future-p)))
3529 (planner-read-date)))))
3530 nil ;; No annotation, interactively
3531 (when planner-use-plan-pages
3532 (if (and current-prefix-arg (planner-derived-mode-p 'planner-mode)
3533 (not (string-match planner-date-regexp (planner-page-name))))
3534 ;; Universal prefix means pick up from current page
3535 (planner-page-name)
3536 (let ((planner-default-page
3537 (if (and (planner-derived-mode-p 'planner-mode)
3538 (planner-page-name)
3539 (not (string-match planner-date-regexp
3540 (planner-page-name))))
3541 (planner-page-name)
3542 planner-default-page)))
3543 (planner-read-non-date-page
3544 (planner-file-alist)))))
3545 planner-default-task-status))
3546 (setq planner-default-page plan-page)
3547 (planner-create-task-from-info
3548 nil ; info
3549 planner-default-task-priority ; priority
3550 "0" ; number
3551 (or status planner-default-task-status) ; status
3552 (if (and annotation
3553 (not (string= annotation ""))
3554 (or (not plan-page)
3555 (and
3556 (not (string= plan-page annotation))
3557 (not (string= (concat "[[" plan-page "]]") annotation)))))
3558 ;; Used C-u to make a plan-page annotation, so preserve
3559 ;; the context
3560 (concat title " : " annotation)
3561 title) ; description
3562 ;; link: If creating this from a planner plan page, use the
3563 ;; current page name
3564 (and plan-page (planner-make-link plan-page)) ; link text
3565 date
3566 plan-page))
3568 (defun planner-create-task-from-note (title date &optional plan-page status)
3569 "Create a task based on the current note with TITLE, DATE, PLAN-PAGE and
3570 STATUS.
3572 A more do-what-I-mean way to do this is to position point on the first
3573 line of a note (.#1 ...) and call `planner-create-task-from-buffer'."
3574 (interactive (let* ((info (planner-current-note-info))
3575 (planner-default-page (and info
3576 (planner-note-plan info))))
3577 (if info
3578 (planner-read-task)
3579 (error "Not in a planner note"))))
3580 (let* ((info (planner-current-note-info))
3581 (annotation (planner-make-link (concat (planner-note-page info)
3583 (planner-note-anchor info)))))
3584 (when (and planner-annotation-symbol-string
3585 (string-match planner-annotation-symbol-string title))
3586 (setq title (replace-match (or annotation "") t t title)
3587 annotation nil))
3588 (planner-create-task title
3589 (when (and date (string-match planner-date-regexp
3590 date))
3591 date)
3592 annotation
3593 plan-page
3594 status)))
3597 ;;;_ + Rescheduling
3599 (defvar planner-copy-or-move-task-suppress-duplicates t
3600 "*If non-nil, do not create duplicate tasks.")
3602 (defun planner-replan-task-basic (page-name)
3603 "Change or assign the plan page for the current task.
3604 PAGE-NAME is the new plan page for the task. Use
3605 `planner-copy-or-move-task' if you want to change the date.
3606 With a prefix, provide the current link text for editing."
3607 (interactive (list
3608 (planner-read-name
3609 (planner-file-alist) nil
3610 (when current-prefix-arg
3611 (planner-task-plan (planner-current-task-info))))))
3612 (let ((info (planner-current-task-info)))
3613 (when (and info (not (equal page-name (planner-task-plan info))))
3614 (with-planner-update-setup
3615 ;; Delete from old plan page
3616 (when (planner-task-plan info)
3617 (planner-find-file (planner-task-plan info))
3618 (when (planner-find-task info)
3619 (delete-region (planner-line-beginning-position)
3620 (1+ (planner-line-end-position)))))
3621 ;; Add to new plan page, if any, and update date
3622 (if page-name
3623 (progn
3624 (planner-find-file page-name)
3625 (planner-seek-task-creation-point)
3626 (insert (planner-format-task info nil nil nil nil
3627 (or (planner-task-date info)
3629 (or (planner-task-date info)
3630 ""))
3631 "\n")
3632 (forward-line -1)
3633 (planner-update-task))
3634 ;; Else, go to day page and update line
3635 (planner-find-file (planner-task-date info))
3636 (if (planner-find-task info)
3637 (delete-region (planner-line-beginning-position)
3638 (1+ (planner-line-end-position)))
3639 (planner-seek-task-creation-point))
3640 (insert (planner-format-task info nil nil nil nil
3641 (or (planner-make-link page-name) "")
3642 (or page-name ""))
3643 "\n"))))))
3644 (defalias 'planner-replan-task 'planner-replan-task-basic)
3646 (defun planner-seek-task-creation-point ()
3647 "Jump to point where task would be created."
3648 (planner-seek-to-first (cdr (assoc 'tasks planner-sections)))
3649 (when planner-add-task-at-end-flag
3650 (while (looking-at "^#")
3651 (forward-line))
3652 (unless (bolp) (insert "\n"))))
3654 (defun planner-copy-or-move-task-basic (&optional date force)
3655 "Move the current task to DATE.
3656 If this is the original task, it copies it instead of moving.
3657 Most of the time, the original should be kept in a planning file,
3658 but this is not required. If FORCE is non-nil, the task is moved
3659 regardless of status. It also works for creating tasks from a
3660 Note. Use `planner-replan-task' if you want to change the plan
3661 page in order to get better completion.
3662 This function is the most complex aspect of planner.el."
3663 (interactive (list (let ((planner-expand-name-favor-future-p
3664 (or planner-expand-name-favor-future-p
3665 planner-task-dates-favor-future-p)))
3666 (planner-read-date))
3667 current-prefix-arg))
3668 (if (or (null date)
3669 (string-match planner-date-regexp date))
3670 (let ((live-buffers (when (equal planner-tasks-file-behavior 'close)
3671 (buffer-list))))
3672 (when (equal date (planner-page-name))
3673 (error "Cannot move a task back to the same day"))
3674 (save-excursion
3675 (save-window-excursion
3676 (save-restriction
3677 (beginning-of-line)
3678 (let* ((task-info (planner-current-task-info))
3679 (plan-page (planner-task-plan task-info))
3680 (date-page (planner-task-date task-info)))
3681 (unless task-info
3682 (error "There is no task on the current line"))
3683 (unless force
3684 (when (equal date-page date)
3685 (error "Cannot move a task back to the same day"))
3686 (when (equal (planner-task-status task-info) "X")
3687 (error "Cannot reschedule a completed task"))
3688 (when (equal (planner-task-status task-info) "C")
3689 (error "Cannot reschedule a cancelled task")))
3690 (when (and (or (null date) (string= date "nil"))
3691 (not plan-page))
3692 (error "Cannot unset date in task not associated with plan"))
3693 ;; Delete it from the old date page
3694 (when date-page
3695 (planner-goto date-page)
3696 (goto-char (point-min))
3697 (when (planner-find-task task-info)
3698 (beginning-of-line)
3699 (delete-region (point)
3700 (min (point-max)
3701 (1+ (planner-line-end-position))))))
3702 ;; Update the new date page
3703 (unless (null date)
3704 (planner-goto date)
3705 (when (or (not planner-copy-or-move-task-suppress-duplicates)
3706 (and (not (planner-find-task task-info))))
3707 (planner-seek-task-creation-point)
3708 (insert
3709 (planner-format-task task-info
3710 nil nil nil nil
3711 (when plan-page
3712 (planner-make-link plan-page)))
3713 "\n")))
3714 ;; Update planner page
3715 (when (and plan-page
3716 (not (string-match planner-date-regexp plan-page)))
3717 (planner-find-file plan-page)
3718 (goto-char (point-min))
3719 (if (planner-find-task task-info)
3720 (progn
3721 (beginning-of-line)
3722 (delete-region (point)
3723 (min (point-max)
3724 (1+ (planner-line-end-position)))))
3725 (planner-seek-task-creation-point))
3726 (insert (planner-format-task
3727 task-info
3728 nil nil nil nil
3729 (planner-make-link date)) "\n"))
3730 t))))
3731 ;; Operation successful.
3732 (when planner-tasks-file-behavior
3733 (planner-save-buffers live-buffers t)))
3734 (when (planner-replan-task date) t)))
3735 (defalias 'planner-copy-or-move-task 'planner-copy-or-move-task-basic)
3737 ;;;_ + Deleting
3739 (defcustom planner-delete-task-hook nil
3740 "Hook called after a task has been deleted.
3741 Argument is a task information list. If a function returns nil,
3742 no other functions will be processed."
3743 :type 'hook
3744 :group 'planner-tasks)
3746 (defun planner-delete-task ()
3747 "Deletes this task from the current page and the linked page."
3748 (interactive)
3749 (save-excursion
3750 (save-window-excursion
3751 (beginning-of-line)
3752 (let* ((task-info (planner-current-task-info))
3753 (task-link (and task-info (planner-task-link task-info)))
3754 (live-buffers
3755 (and (equal planner-tasks-file-behavior 'close)
3756 (buffer-list))))
3757 (unless task-info
3758 (error "There is no task on the current line"))
3759 (beginning-of-line)
3760 (delete-region (point) (min (point-max)
3761 (1+ (planner-line-end-position))))
3762 (when (and task-link (assoc task-link (planner-file-alist))
3763 (planner-jump-to-linked-task task-info))
3764 (delete-region (planner-line-beginning-position)
3765 (min (point-max) (1+ (planner-line-end-position)))))
3766 (when planner-tasks-file-behavior
3767 (planner-save-buffers live-buffers t))
3768 (run-hook-with-args-until-failure
3769 'planner-delete-task-hook task-info)))))
3771 ;;;_ + Updating
3773 (defun planner-edit-task-description-basic (description)
3774 "Change the current task to use DESCRIPTION."
3775 (interactive (list
3776 (let* ((info (planner-current-task-info))
3777 (planner-task-history
3778 (list
3779 (planner-task-description info))))
3780 (unless info (error "No task on current line"))
3781 (read-string "New description: "
3782 (cons (planner-task-description info)
3784 '(planner-task-history . 1)
3785 (planner-task-description info)))))
3786 (let ((point (point)))
3787 (with-planner-update-setup
3788 (let ((info (planner-current-task-info))
3789 (live-buffers (and
3790 (equal planner-tasks-file-behavior 'close)
3791 (buffer-list))))
3792 (delete-region (planner-line-beginning-position)
3793 (planner-line-end-position))
3794 (insert (planner-format-task info
3795 nil nil nil
3796 description))
3797 (when (planner-task-link info)
3798 (if (planner-jump-to-linked-task info)
3799 (progn
3800 (setq info (planner-current-task-info))
3801 (delete-region (planner-line-beginning-position)
3802 (planner-line-end-position))
3803 (insert (planner-format-task info
3804 nil nil nil
3805 description)))
3806 (planner-seek-task-creation-point)
3807 (insert
3808 (planner-format-task info nil nil nil description
3809 (planner-make-link (planner-task-page info)))
3810 "\n")))))
3811 (goto-char (point))))
3812 (defalias 'planner-edit-task-description 'planner-edit-task-description-basic)
3815 (defun planner-update-task-basic ()
3816 "Update the current task's priority and status on the linked page.
3817 Tasks are considered the same if they have the same description.
3818 This function allows you to force a task to be recreated if it
3819 disappeared from the associated page.
3821 Note that the text of the task must not change. If you want to be able
3822 to update the task description, see planner-id.el."
3823 (interactive)
3824 (with-planner-update-setup
3825 (beginning-of-line)
3826 (let* ((task-info (planner-current-task-info))
3827 (task-link (and task-info
3828 (if (string-match planner-date-regexp
3829 (planner-page-name))
3830 (planner-task-plan task-info)
3831 (planner-task-date task-info))))
3832 (original (planner-page-name)))
3833 (unless task-info
3834 (error "There is no task on the current line"))
3835 ;; (unless task-link
3836 ;; (error "There is no link for the current task"))
3837 (if (planner-jump-to-linked-task task-info)
3838 ;; Already there, so update only if changed
3839 (unless (planner-tasks-equal-p task-info
3840 (planner-current-task-info))
3841 (delete-region (planner-line-beginning-position)
3842 (min (point-max) (1+ (planner-line-end-position))))
3843 (insert (planner-format-task task-info nil nil nil nil
3844 (planner-make-link
3845 original)) "\n"))
3846 ;; Not yet there, so add it
3847 (when (planner-local-page-p task-link)
3848 (planner-find-file task-link)
3849 (save-excursion
3850 (save-restriction
3851 (planner-seek-task-creation-point)
3852 (insert
3853 (planner-format-task task-info nil nil nil nil
3854 (planner-make-link original))
3855 "\n"))))))))
3857 (defalias 'planner-update-task 'planner-update-task-basic)
3859 ;;;_ + Prioritizing
3861 ;; This really should be called planner-raise/lower-task-priority, but
3862 ;; for some obscure reason, the original planner.el called the task
3863 ;; numbers priorities and "A/B/C" categories. I'm not really sure if I
3864 ;; can change the name right now. I suppose we eventually should.
3866 (defun planner-set-task-priority (priority)
3867 "Set the priority of the current task.
3868 This changes a low-priority task to a medium-priority task
3869 and a medium-priority task to a high-priority task."
3870 (let ((info (planner-current-task-info)))
3871 (when info
3872 (delete-region (planner-line-beginning-position)
3873 (min (point-max) (1+ (planner-line-end-position))))
3874 (save-excursion
3875 (insert (planner-format-task
3876 info
3877 priority) "\n"))
3878 (when (planner-task-link info)
3879 (planner-update-task)))))
3881 (defun planner-raise-task-priority ()
3882 "Raise the priority of the current task.
3883 This changes a low-priority task to a medium-priority task
3884 and a medium-priority task to a high-priority task."
3885 (interactive)
3886 (let ((info (planner-current-task-info)))
3887 (when info
3888 (delete-region (planner-line-beginning-position)
3889 (min (point-max) (1+ (planner-line-end-position))))
3890 (save-excursion
3891 (insert (planner-format-task
3892 info
3893 (cond
3894 ((string= "A" (planner-task-priority info)) "A")
3895 ((string= "B" (planner-task-priority info)) "A")
3896 ((string= "C" (planner-task-priority info)) "B")
3897 (t "C"))) "\n"))
3898 (when (planner-task-link info)
3899 (planner-update-task)))))
3901 (defun planner-lower-task-priority ()
3902 "Lower the priority of the current task.
3903 This changes a medium-priority task to a low-priority task
3904 and a high-priority task to a low-priority task."
3905 (interactive)
3906 (let ((info (planner-current-task-info)))
3907 (when info
3908 (delete-region (planner-line-beginning-position)
3909 (min (point-max) (1+ (planner-line-end-position))))
3910 (save-excursion
3911 (insert (planner-format-task
3912 info
3913 (cond
3914 ((string= "A" (planner-task-priority info)) "B")
3915 ((string= "B" (planner-task-priority info)) "C")
3916 (t "C"))) "\n"))
3917 (when (planner-task-link info)
3918 (planner-update-task)))))
3920 (defun planner-raise-task (&optional arg)
3921 "Raise the number of the current task by ARG steps. (Default: 1)"
3922 (interactive "p")
3923 (beginning-of-line)
3924 (setq arg (or arg 1)) ; ARG defaults to 1 if not specified
3925 (if (< arg 0) (planner-lower-task (- arg)))
3926 (let* ((current-task (planner-current-task-info))
3927 ;; task-seen will be the last task moved over with the same link
3928 task-seen)
3929 (unless current-task
3930 (error "Not on a task line"))
3931 ;; Store the current line in the kill ring, deleting it
3932 (kill-region (point) (1+ (planner-line-end-position)))
3933 ;; If the previous line is not a task, search for the previous block
3934 (while (> arg 0)
3935 (let ((old-point (point)))
3936 (if (= (forward-line -1) 0)
3937 (if (not (planner-current-task-info))
3938 (if (re-search-backward "^#[ABC][0-9]*[ \t]" nil t)
3939 (beginning-of-line)
3940 (setq arg -1) ;; Stop moving, yank back at current place.
3941 (goto-char old-point)))
3942 (setq arg -1)) ;; Stop moving, yank back at current place.
3943 (when (and (> arg 0)
3944 (string= (planner-task-plan current-task)
3945 (planner-task-plan (planner-current-task-info))))
3946 (setq task-seen (planner-current-task-info))))
3947 (setq arg (1- arg)))
3948 ;; Cursor now at right place
3949 (save-excursion (yank))
3950 ;; Update the linked page, if any
3951 (save-window-excursion
3952 (save-excursion
3953 (save-restriction
3954 (when (and task-seen
3955 (planner-task-link current-task)
3956 (planner-jump-to-linked-task current-task))
3957 (let ((old-task
3958 (buffer-substring
3959 (planner-line-beginning-position)
3960 (planner-line-end-position)))
3961 found)
3962 (save-excursion
3963 (save-restriction
3964 (when (planner-find-task task-seen)
3965 ;; Found the new task, so delete the old task and
3966 ;; insert here
3967 (setq found t)
3968 (insert old-task "\n"))))
3969 (when found
3970 (delete-region
3971 (planner-line-beginning-position)
3972 (1+ (planner-line-end-position)))))))))))
3974 (defun planner-lower-task (&optional arg)
3975 "Lower the number of the current task by ARG steps (default 1)."
3976 (interactive "p")
3977 (beginning-of-line)
3978 (setq arg (or arg 1)) ; ARG defaults to 1 if not specified
3979 (if (< arg 0) (planner-raise-task (- arg)))
3980 (let* ((current-task (planner-current-task-info))
3981 ;; task-seen will be the last task moved over with the same link
3982 task-seen)
3983 (unless current-task
3984 (error "Not on a task line"))
3985 ;; Store the current line in the kill ring, deleting it
3986 (kill-region (point) (1+ (planner-line-end-position)))
3987 ;; If the current line is not a task, search for the next block
3988 (while (> arg 0)
3989 (let ((old-point (point)))
3990 (if (not (planner-current-task-info))
3991 (if (re-search-forward "^#[ABC][0-9]*[ \t]" nil t)
3992 (planner-line-beginning-position)
3993 (setq arg -1) ;; Stop moving, yank back at current place.
3994 (goto-char old-point)))
3995 (when (and (> arg 0)
3996 (string= (planner-task-plan current-task)
3997 (planner-task-plan (planner-current-task-info))))
3998 (setq task-seen (planner-current-task-info))))
3999 (unless (and (> arg 0) (= (forward-line 1) 0))
4000 (setq arg -1))
4001 (setq arg (1- arg)))
4002 ;; Cursor now at right place
4003 (save-excursion (yank))
4004 ;; Update the linked page, if any
4005 (save-window-excursion
4006 (save-excursion
4007 (save-restriction
4008 (when (and task-seen
4009 (planner-task-link current-task)
4010 (planner-jump-to-linked-task current-task))
4011 (let ((old-task
4012 (buffer-substring
4013 (planner-line-beginning-position)
4014 (planner-line-end-position)))
4015 found)
4016 (save-excursion
4017 (save-restriction
4018 (when (planner-find-task task-seen)
4019 ;; Found the new task, so delete the old task and
4020 ;; insert here
4021 (setq found t)
4022 (forward-line 1)
4023 (insert old-task "\n"))))
4024 (when found
4025 (delete-region
4026 (planner-line-beginning-position)
4027 (1+ (planner-line-end-position)))))))))))
4029 ;;;_ + Changing the status
4031 (defvar planner-mark-task-hook nil
4032 "Hook called after a task's status has been changed.
4033 Arguments are OLD-STATUS and NEW-STATUS. Functions should leave
4034 the point on the task. If a function returns nil, no other
4035 functions will be processed.")
4037 (defun planner-mark-task (mark &optional this-only)
4038 "Change task status to MARK.
4039 If THIS-ONLY is non-nil, the linked planner page is not
4040 updated."
4041 (let ((case-fold-search nil)
4042 (info (planner-current-task-info)))
4043 (when info
4044 (with-planner-update-setup
4045 (goto-char (planner-line-beginning-position))
4046 (skip-chars-forward "^ \t" (planner-line-end-position))
4047 (skip-chars-forward " \t" (planner-line-end-position))
4048 (delete-char 1)
4049 (insert mark)
4050 (unless this-only
4051 (planner-update-task))
4052 (run-hook-with-args-until-failure
4053 'planner-mark-task-hook
4054 (planner-task-status info)
4055 mark)))))
4057 (defun planner-task-open ()
4058 "Mark the current task as open."
4059 (interactive)
4060 (planner-mark-task "_"))
4062 (defun planner-task-in-progress ()
4063 "Mark the current task as in progress."
4064 (interactive)
4065 (planner-mark-task "o"))
4067 (defun planner-task-done ()
4068 "Mark the current task as done."
4069 (interactive)
4070 (planner-mark-task "X"))
4072 (defun planner-task-cancelled ()
4073 "Mark the current task as cancelled."
4074 (interactive)
4075 (planner-mark-task "C"))
4077 (defun planner-task-delegated ()
4078 "Mark the current task as delegated."
4079 (interactive)
4080 (planner-mark-task "D"))
4082 (defun planner-task-pending ()
4083 "Mark the current task as pending."
4084 (interactive)
4085 (planner-mark-task "P"))
4087 ;;;_ + Extracting
4089 (defun planner-seek-next-unfinished-task ()
4090 "Move point to the next unfinished task on this page.
4091 Return nil if not found."
4092 (interactive)
4093 (re-search-forward "^#[A-C][0-9]*\\s-+[_oDP]\\s-+" nil t))
4095 (defun planner-list-tasks-with-status (status &optional pages)
4096 "Display all tasks that match the STATUS regular expression on all day pages.
4097 If PAGES is:
4098 t: check all pages
4099 nil: check all day pages
4100 regexp: search all pages whose filenames match the regexp
4101 list of page names: limit to those pages
4102 alist of page/filename: limit to those pages
4103 This could take a long time."
4104 (interactive (list (read-string "Status: ")))
4105 (set-buffer (get-buffer-create "*Planner Tasks*"))
4106 (erase-buffer)
4107 (let (tasks)
4108 (setq tasks (planner-extract-tasks
4109 (cond ((eq pages t)
4110 (planner-file-alist))
4111 ((eq pages nil)
4112 (planner-get-day-pages))
4113 ((not (listp pages))
4114 (let ((regexp pages))
4115 (setq pages nil)
4116 (dolist (page (planner-file-alist))
4117 (when (string-match regexp (cdr page))
4118 (setq pages (cons page pages)))))
4119 pages)
4120 (t pages))
4121 (lambda (item)
4122 (string-match status (planner-task-status item)))))
4123 (while tasks
4124 (insert
4125 (format "[[%s]] %s %s %s\n"
4126 (planner-task-page (car tasks))
4127 (planner-task-priority (car tasks))
4128 (planner-task-status (car tasks))
4129 (planner-task-description (car tasks))))
4130 (setq tasks (cdr tasks))))
4131 (planner-mode)
4132 (setq muse-current-project (muse-project planner-project))
4133 (goto-char (point-min))
4134 (pop-to-buffer (current-buffer)))
4136 (defun planner-list-unfinished-tasks (&optional pages)
4137 "Display all unfinished tasks on PAGES.
4138 The PAGES argument limits the pages to be checked in this manner:
4139 t: check all pages
4140 \"regexp\": search all pages whose filenames match \"regexp\"
4141 list of page names: limit to those pages
4142 alist of page/filenames: limit to those pages
4144 Called interactively, this function will search day pages by
4145 default. You can specify the start and end dates or leave them as
4146 nil to search all days. Calling this function with an interactive
4147 prefix will prompt for a regular expression to limit pages.
4148 Specify \".\" or leave this blank to include all pages."
4149 (interactive (list (if current-prefix-arg
4150 (read-string "Regexp: ")
4151 (let ((planner-expand-name-default "nil"))
4152 (planner-get-day-pages
4153 (planner-read-date "nil by default. Start")
4154 (planner-read-date "nil by default. End")
4155 t)))))
4156 (planner-list-tasks-with-status "[^XC\n]" pages))
4158 ;;;_ + Notes
4160 (defvar planner-search-notes-buffer "*Planner Search*"
4161 "Buffer for search results.")
4163 ;;;###autoload
4164 (defun planner-search-notes-with-body (regexp limit)
4165 "Return a buffer with all the notes returned by the query for REGEXP.
4166 If called with a prefix argument, prompt for LIMIT and search days on
4167 or after LIMIT. Display the body of the notes as well."
4168 (interactive (list (read-string "Regexp: ")
4169 (if current-prefix-arg
4170 (let ((planner-expand-name-favor-future-p nil))
4171 (planner-read-date)))))
4172 (planner-search-notes regexp limit t))
4174 ;;;###autoload
4175 (defun planner-search-notes (regexp limit &optional include-body)
4176 "Return a buffer with all the notes returned by the query for REGEXP.
4177 If called with a prefix argument, prompt for LIMIT and search days on
4178 or after LIMIT. If INCLUDE-BODY is non-nil, return the body as well."
4179 (interactive (list (read-string "Regexp: ")
4180 (if current-prefix-arg
4181 (let ((planner-expand-name-favor-future-p nil))
4182 (planner-read-date)))
4183 nil))
4184 (with-planner
4185 (let* ((case-fold-search t)
4186 (results (planner-search-notes-internal regexp limit include-body)))
4187 (if results
4188 (progn
4189 (set-buffer (get-buffer-create planner-search-notes-buffer))
4190 (setq buffer-read-only nil)
4191 (erase-buffer)
4192 (mapcar
4193 (if include-body
4194 (lambda (item)
4195 (insert "** "
4196 (planner-make-link (elt item 0) (elt item 1)) "\t"
4197 (elt item 3) "\n\n"))
4198 (lambda (item)
4199 (insert (planner-make-link (elt item 0) (elt item 1)) "\t"
4200 (elt item 2) "\n")))
4201 results)
4202 (planner-mode)
4203 (goto-char (point-min))
4204 (pop-to-buffer (current-buffer)))
4205 (message "No results found.")))))
4207 ;;;_ + Calendar
4209 (defun planner-calendar-insinuate ()
4210 "Hook Planner into Calendar.
4212 Adds special planner key bindings to `calendar-mode-map'.
4213 After this function is evaluated, you can use the following
4214 planner-related keybindings in `calendar-mode-map':
4216 n jump to the planner page for the current day.
4217 N display the planner page for the current day."
4218 (interactive)
4219 (require 'calendar)
4220 (add-hook 'calendar-move-hook
4221 (lambda ()
4222 (when planner-calendar-show-planner-files
4223 (planner-calendar-show))))
4224 (define-key calendar-mode-map "n" 'planner-calendar-goto)
4225 (define-key calendar-mode-map "N" 'planner-calendar-show))
4226 (defalias 'planner-insinuate-calendar 'planner-calendar-insinuate)
4228 (defvar planner-calendar-buffer-list nil "List of buffers opened by calendar.")
4230 (defun planner-kill-calendar-files ()
4231 "Remove planner files shown from Calendar."
4232 (interactive)
4233 (while planner-calendar-buffer-list
4234 (when (buffer-live-p (car planner-calendar-buffer-list))
4235 (with-current-buffer (car planner-calendar-buffer-list)
4236 (save-buffer)
4237 (planner-maybe-remove-file)))
4238 (setq planner-calendar-buffer-list (cdr planner-calendar-buffer-list))))
4240 ;;;###autoload
4241 (defun planner-calendar-goto ()
4242 "Goto the plan page corresponding to the calendar date."
4243 (interactive)
4244 (let ((planner-use-other-window t))
4245 (planner-goto (planner-date-to-filename (calendar-cursor-to-date)))))
4247 ;;;###autoload
4248 (defun planner-calendar-show ()
4249 "Show the plan page for the calendar date under point in another window."
4250 (interactive)
4251 (save-selected-window
4252 (let ((planner-use-other-window t)
4253 (date (planner-date-to-filename (calendar-cursor-to-date))))
4254 (if (planner-goto date planner-show-only-existing)
4255 (add-to-list 'planner-calendar-buffer-list (current-buffer))
4256 ;; Return nil or a message if there is no day plan page. planner-goto
4257 ;; is not called interactively, so it doesn't send a message.
4258 (when (interactive-p)
4259 (message "No planner file for %s" date))
4260 ;; return nil
4261 nil))))
4263 (defadvice exit-calendar (after planner activate protect)
4264 "Call `planner-kill-calendar-files'."
4265 (planner-kill-calendar-files))
4267 (defun planner-calendar-select ()
4268 "Return to `planner-read-date' with the date currently selected."
4269 (interactive)
4270 (when (calendar-cursor-to-date)
4271 (setq planner-calendar-selected-date
4272 (planner-date-to-filename (calendar-cursor-to-date)))
4273 (if (active-minibuffer-window) (exit-minibuffer))))
4275 ;;;_* Context-sensitive keybindings
4277 (defun planner-jump-to-link ()
4278 "Jump to the item linked to by the current item."
4279 (interactive)
4280 (cond
4281 ((planner-current-task-info) (planner-jump-to-linked-task))
4282 ((planner-current-note-info) (planner-jump-to-linked-note))))
4284 (defun planner-move-up ()
4285 "Move up.
4286 Task: Raise the number of the current task.
4287 Note: Renumbering does not make sense for notes right now, so go to the
4288 previous note.
4289 Headline: Go to previous headline of the same depth."
4290 (interactive)
4291 (cond
4292 ((planner-current-task-info) (planner-raise-task))
4293 ((planner-current-note-info)
4294 (re-search-backward "^\\.#[0-9]+" nil t))
4295 ((and (goto-char (planner-line-beginning-position))
4296 (looking-at "^\\*+"))
4297 (re-search-backward
4298 (concat "^" (regexp-quote (match-string 0)) "\\s-") nil t))))
4301 (defun planner-move-down ()
4302 "Move down.
4303 Task: Lower the number of the current task.
4304 Note: Renumbering does not make sense for notes right now, so go to the
4305 next note.
4306 Headline: Go to the next headline of the same depth."
4307 (interactive)
4308 (cond
4309 ((planner-current-task-info) (planner-lower-task))
4310 ((planner-current-note-info)
4311 (forward-line 1)
4312 (re-search-forward "^\\.#[0-9]+" nil t))
4313 ((and (goto-char (planner-line-beginning-position))
4314 (looking-at "^\\*+"))
4315 (forward-line 1)
4316 (re-search-forward
4317 (concat "^" (regexp-quote (match-string 0)) "\\s-") nil t))))
4319 ;;;_* Initialization
4321 (setq planner-loaded t)
4323 ;; Insinuate with allout mode
4324 (add-hook 'allout-mode-leaders '(planner-mode . "."))
4326 (provide 'planner)
4328 ;;;_* Local emacs vars.
4330 ;; Local variables:
4331 ;; allout-layout: (* 0 : )
4332 ;; End:
4334 ;;; planner.el ends here