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