Fix typo, closes #12843.
[planner-el.git] / planner.el
blobbe04ded00508940ea05d55cc7c959656f58d64f6
1 ;;; planner.el --- The Emacs Planner
3 ;;; Commentary:
5 ;;;_* Commentary
7 ;;;_ + Package description
9 ;; Copyright (C) 2001, 2003, 2004, 2005,
10 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
11 ;; Parts copyright (C) 2004, 2008 David D. Smith (davidsmith AT acm DOT org)
12 ;; Parts copyright (C) 2004, 2008 Yvonne Thomson (yvonne AT netbrains DOT com DOT au)
13 ;; Parts copyright (C) 2004, 2008 Maciej Kalisak (mac AT cs DOT toronto DOT edu)
14 ;; Parts copyright (C) 2004, 2008 Chris Parsons (chris.p AT rsons.org)
15 ;; Parts copyright (C) 2004, 2008 Dirk Bernhardt (nospam AT krid.de)
16 ;; Parts copyright (C) 2005, 2008 Dryice Dong Liu
17 ;; Parts copyright (C) 2005, 2008 Angus Lees (gus AT debian.org)
18 ;; Parts copyright (C) 2005, 2008 Sergey Vlasov (vsu AT altlinux.ru)
19 ;; Parts copyright (C) 2005, 2008 Yann Hodique (hodique AT lifl DOT fr)
20 ;; Parts copyright (C) 2005, 2008 Peter K. Lee
22 ;; Emacs Lisp Archive Entry
23 ;; Filename: planner.el
24 ;; Version: 3.42
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 3, 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.42"
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, unique 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 (unless (rassoc (cdr item) files)
1247 (setq files (cons item files)))))
1248 (planner-file-alist))
1249 (unless exclude-temp
1250 (while buffers
1251 (with-current-buffer (car buffers)
1252 (when (and (equal 'planner-mode major-mode)
1253 (planner-page-name)
1254 (string-match planner-date-regexp (planner-page-name)))
1255 (unless (assoc (planner-page-name) files)
1256 (add-to-list 'files (cons (planner-page-name)
1257 (buffer-file-name))))))
1258 (setq buffers (cdr buffers))))
1259 files))
1261 (defun planner-get-day-pages (&optional from to exclude-temp)
1262 "Return a descending list of day pages from FROM to TO (inclusive).
1263 If EXCLUDE-TEMP is non-nil, ignore unsaved pages."
1264 (sort (delq nil
1265 (mapcar
1266 (lambda (item)
1267 (and
1268 (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 (or (not (stringp link-name)) (string= link-name ""))
1663 (setq link (planner-make-link link link-name t)))
1664 (if (not (stringp link))
1665 (message "No annotation can be made")
1666 (message "Copied '%s' to the kill-ring" link)
1667 (kill-new link))))
1668 (custom-add-option 'planner-annotation-functions
1669 'planner-annotation-as-kill)
1671 (defun planner-annotation-from-planner-note ()
1672 "Return a link to the current page.
1673 Call when the point is on the first line of the note."
1674 (when (and (planner-derived-mode-p 'planner-mode)
1675 (planner-page-name))
1676 (save-excursion
1677 (goto-char (planner-line-beginning-position))
1678 (when (looking-at ".\\(#[0-9]+\\)")
1679 (planner-make-link
1680 (concat (planner-page-name)
1681 (planner-match-string-no-properties 1))
1682 (concat (planner-page-name)
1683 (planner-match-string-no-properties 1))
1684 t)))))
1685 (custom-add-option 'planner-annotation-functions
1686 'planner-annotation-from-planner-note)
1688 (defun planner-annotation-from-planner ()
1689 "Return a wiki link to the current wiki page.
1690 Date pages are not linked."
1691 (when (and (planner-derived-mode-p 'planner-mode)
1692 (planner-page-name))
1693 (cond
1694 ((string-match planner-date-regexp (planner-page-name))
1695 "") ; None for date pages
1696 (t (planner-make-link (planner-page-name) nil t)))))
1697 (custom-add-option 'planner-annotation-functions
1698 'planner-annotation-from-planner)
1700 (defun planner-annotation-from-wiki ()
1701 "Return the interwiki link to the current wiki page."
1702 (when (and (planner-derived-mode-p 'muse-mode)
1703 muse-current-project
1704 (muse-page-name))
1705 (concat "[[" (car muse-current-project) "::" (muse-page-name) "]]")))
1706 (custom-add-option 'planner-annotation-functions
1707 'planner-annotation-from-wiki)
1709 (defun planner-annotation-from-dired ()
1710 "Return the `default-directory' of the current Dired buffer."
1711 (when (eq major-mode 'dired-mode)
1712 (planner-make-link default-directory)))
1714 (defun planner-annotation-from-file-relative ()
1715 "Return the filename of the current buffer relative to `planner-directory'."
1716 (when buffer-file-name
1717 (planner-make-link (file-relative-name buffer-file-name
1718 (planner-directory))
1719 nil t)))
1720 (custom-add-option 'planner-annotation-functions
1721 'planner-annotation-from-dired)
1723 (defcustom planner-annotation-use-relative-file nil
1724 "If t, use relative file links always.
1725 If a function, it is called with the file name. Return value of t
1726 means use relative file links."
1727 :group 'planner
1728 :type '(choice (const :tag "Always use relative file links" t)
1729 (const :tag "Never use relative file links" nil)
1730 function))
1732 (defcustom planner-annotation-strip-directory nil
1733 "If non-nil, strip the directory part of the filename from link text."
1734 :group 'planner
1735 :type 'boolean)
1737 (defcustom planner-annotation-format-local-file-name nil
1738 "If non-nil, use the result of `planner-annotation-format-local-file-name'."
1739 :group 'planner
1740 :type '(choice (const :tag "Use filename as is" nil)
1741 function))
1743 (defun planner-annotation-from-file ()
1744 "Return the filename of the current buffer.
1745 If `planner-annotation-use-relative-file' is t or a function that
1746 returns non-nil, a relative link is used instead. If
1747 `planner-annotation-strip-directory' is non-nil, the directory is
1748 stripped from the link description."
1749 (when buffer-file-name
1750 (planner-make-link
1751 (if (or (and (functionp planner-annotation-use-relative-file)
1752 (funcall planner-annotation-use-relative-file
1753 (buffer-file-name)))
1754 (equal planner-annotation-use-relative-file t))
1755 (file-relative-name (buffer-file-name) (planner-directory))
1756 (if (functionp planner-annotation-format-local-file-name)
1757 (funcall planner-annotation-format-local-file-name buffer-file-name)
1758 buffer-file-name))
1759 (when planner-annotation-strip-directory
1760 (file-name-nondirectory buffer-file-name))
1761 t)))
1762 (custom-add-option 'planner-annotation-functions
1763 'planner-annotation-from-file)
1765 ;;;###autoload
1766 (defun planner-annotation-from-file-with-position ()
1767 "Return the filename and cursor position of the current buffer.
1768 If `planner-annotation-use-relative-file' is t or a function that
1769 returns non-nil, a relative link is used instead. If
1770 `planner-annotation-strip-directory' is non-nil, the directory is
1771 stripped from the link description."
1772 (when buffer-file-name
1773 (planner-make-link
1774 (concat
1775 "pos://"
1776 (if (or (and (functionp planner-annotation-use-relative-file)
1777 (funcall planner-annotation-use-relative-file
1778 (buffer-file-name)))
1779 (equal planner-annotation-use-relative-file t))
1780 (file-relative-name (buffer-file-name) (planner-directory))
1781 buffer-file-name)
1782 "#" (number-to-string (point)))
1783 (if planner-annotation-strip-directory
1784 (file-name-nondirectory buffer-file-name)
1785 buffer-file-name)
1786 t)))
1788 ;;;###autoload
1789 (defun planner-browse-position-url (url)
1790 "If this is a position URL, jump to it."
1791 (when (string-match "^pos://\\(.+\\)#\\([0-9]+\\)$" url)
1792 (let ((file (match-string 1 url))
1793 (pos (string-to-number (match-string 2 url))))
1794 (find-file file)
1795 (goto-char pos)
1796 t)))
1798 ;;;###autoload
1799 (defun planner-resolve-position-url (id)
1800 "Replace ID with the blog, web or e-mail address of the BBDB record."
1801 (save-match-data
1802 (when (string-match "\\`pos://\\(.+\\)#\\([0-9]+\\)" id)
1803 (match-string 1 id))))
1805 (planner-add-protocol "pos://" 'planner-browse-position-url
1806 'planner-resolve-position-url)
1807 (custom-add-option 'planner-annotation-functions
1808 'planner-annotation-from-file-with-position)
1810 ;;;_ + Tasks
1812 (defcustom planner-create-task-hook nil
1813 "Functions to run after a task has been created.
1814 Point will be on the same line as the task."
1815 :type 'hook
1816 :group 'planner-tasks)
1818 (defcustom planner-create-task-from-buffer-hook nil
1819 "Functions to run after a task has been created from a buffer.
1820 This will be run before `planner-create-task-hook'.
1821 Point will be on the same line as the task."
1822 :type 'hook
1823 :group 'planner-tasks)
1825 (defcustom planner-task-dates-favor-future-p nil
1826 "*If this is non-nil, favor future dates for task creation or movement."
1827 :type 'boolean
1828 :group 'planner-tasks)
1830 (defcustom planner-default-page "TaskPool"
1831 "Default page for tasks.
1832 This is set to the current planner page, or the last page used
1833 if not on a plan page."
1834 :type 'string
1835 :group 'planner-tasks)
1837 (defcustom planner-tasks-file-behavior 'close
1838 "Controls behavior of task creation and updates.
1839 If 'close, newly-opened files are saved and closed.
1840 If 'save, newly-opened files are saved and left open.
1841 If nil, no actions will be taken."
1842 :group 'planner-tasks
1843 :type '(choice (const :tag "Save and close opened files" 'close)
1844 (const :tag "Save opened files" 'save)
1845 (const :tag "Do nothing" nil)))
1847 (defcustom planner-tasks-never-suppress-fixing-flag t
1848 "Non-nil means always sort, renumber and align tasks whenever
1849 files are saved."
1850 :group 'planner-tasks
1851 :type 'boolean)
1853 (defcustom planner-sort-undated-tasks-equivalent "9999.99.99"
1854 "Date considered for undated tasks.
1855 This option controls task sorting on plan pages. By default,
1856 undated tasks are sorted after dated tasks."
1857 :group 'planner-tasks
1858 :type
1859 '(choice
1860 (const :tag "Sort undated tasks after dated tasks" "9999.99.99")
1861 (const :tag "Sort undated tasks before dated tasks" "")
1862 string))
1864 (defcustom planner-sort-tasks-key-function 'planner-sort-tasks-default-key
1865 "Function called to determine the sorting key for the current line."
1866 :group 'planner-tasks
1867 :type 'function)
1869 (defcustom planner-use-task-numbers nil
1870 "Non-nil means number tasks.
1871 This allows you to refer to past tasks if your tasks are numbered
1872 appropriately. If you set this to nil, you can save space in your
1873 plan files."
1874 :type 'boolean
1875 :group 'planner-tasks)
1877 ;;;_ + Information
1879 (defun planner-task-info-from-string (page-name string)
1880 "On the planner page PAGE-NAME, parse STRING and return the task as a list.
1881 Argument PAGE-NAME is used to determine whether this is a link
1882 from a plan page or a date page."
1883 (with-planner
1884 (when (string-match "#\\([A-C]\\)\\([0-9]*\\)\\s-+\\(.\\)\\s-+\\(.+\\)"
1885 string)
1886 (let ((priority (planner-match-string-no-properties 1 string))
1887 (number (planner-match-string-no-properties 2 string))
1888 (status (planner-match-string-no-properties 3 string))
1889 (description (planner-match-string-no-properties 4 string))
1890 (case-fold-search nil)
1891 link-text link plan date)
1892 (when (= (length number) 0)
1893 (setq number nil))
1894 (cond
1895 ((string-match
1896 "\\s-+(\\(\\[\\[\\([^])]+\\)\\]\\[\\([^])]+\\)\\]\\]\\))\\s-*$"
1897 description)
1898 (setq link-text (match-string 1 description))
1899 (setq link (match-string 2 description))
1900 (setq description (replace-match "" t t description)))
1901 ((string-match
1902 "\\s-+(\\(\\[\\[\\([^])]+\\)\\]\\]\\))\\s-*$" description)
1903 (setq link-text (match-string 1 description))
1904 (setq link (match-string 2 description))
1905 (setq description (replace-match "" t t description)))
1906 ((string-match "\\s-+(\\([^)]+\\))\\s-*$" description)
1907 (setq link-text (match-string 1 description))
1908 (setq link (match-string 1 description))
1909 (setq description (replace-match "" t t description)))
1910 ((string-match "\\s-+$" description)
1911 (setq description (replace-match "" t t description))))
1912 (when link
1913 (setq link (planner-link-base link-text)))
1914 (unless (planner-local-page-p link) (setq link nil))
1915 (if (string-match planner-date-regexp page-name)
1916 ;; We're on a date page, so the link page (if any) should be the
1917 ;; planner page.
1918 (progn
1919 (setq date page-name)
1920 (setq plan (and link
1921 (unless (string-match planner-date-regexp link)
1922 link))))
1923 ;; We're on a planner page, so the link page (if any) will be the plan
1924 (setq plan (and page-name (unless (string-match planner-date-regexp
1925 page-name)
1926 page-name)))
1927 (when (and link (string-match planner-date-regexp link))
1928 (setq date (match-string 0 link))))
1929 (list page-name
1930 priority number status description link link-text plan date)))))
1932 (defun planner-task-info-override (task-info properties)
1933 "Replace fields in TASK-INFO with PROPERTIES.
1934 Acceptable properties are: page-name priority number status
1935 description link link-text plan date."
1936 (let ((fields '(page-name priority number status description
1937 link link-text plan date))
1938 result)
1939 (while task-info
1940 (setq result
1941 (cons
1942 (car (let ((search (memq (car fields) properties)))
1943 (if search (cdr search) task-info)))
1944 result))
1945 (setq fields (cdr fields))
1946 (setq task-info (cdr task-info)))
1947 (nreverse result)))
1949 (defun planner-current-task-info ()
1950 "Parse the current line and return the task information as a list."
1951 (planner-task-info-from-string (planner-page-name)
1952 (buffer-substring
1953 (planner-line-beginning-position)
1954 (planner-line-end-position))))
1956 (defun planner-task-page (info)
1957 "Return the page of a task given INFO." (nth 0 info))
1958 (defun planner-task-priority (info)
1959 "Return the priority of a task given INFO." (nth 1 info))
1960 (defun planner-task-number (info)
1961 "Return the number of a task given INFO." (nth 2 info))
1962 (defun planner-task-status (info)
1963 "Return the status of a task given INFO." (nth 3 info))
1964 (defun planner-task-description (info)
1965 "Return the description of a task given INFO." (nth 4 info))
1966 (defun planner-task-link (info)
1967 "Return the page linked to by a task given INFO." (nth 5 info))
1968 (defun planner-task-link-text (info)
1969 "Return the link text of a task given INFO." (nth 6 info))
1970 (defun planner-task-plan (info)
1971 "Return the planner page of a task given INFO." (nth 7 info))
1972 (defun planner-task-date (info)
1973 "Return the planner date of a task given INFO." (nth 8 info))
1974 (defun planner-task-link-as-list (info)
1975 "Return a list of all the pages this task is on."
1976 (delq nil (list (nth 7 info) (nth 8 info))))
1978 ;;;_ + Creation
1980 (defvar planner-create-task-from-info-function
1981 'planner-create-task-from-info-basic
1982 "Function for creating tasks.
1983 Should accept the same arguments as `planner-create-task-from-info-basic'.")
1985 (defun planner-create-task-from-info (info &optional priority number status description link-text date plan)
1986 "Create a task in the date and plan pages based on INFO.
1987 Optional arguments PRIORITY, NUMBER, STATUS, DESCRIPTION,
1988 LINK-TEXT, DATE, and PLAN override those in INFO."
1989 (funcall planner-create-task-from-info-function info priority
1990 number status description link-text date plan))
1992 (defun planner-create-task-from-info-basic
1993 (info &optional priority number status description link-text date plan)
1994 "Create a task in the date and plan pages based on INFO.
1995 Optional arguments PRIORITY, NUMBER, STATUS, DESCRIPTION,
1996 LINK-TEXT, DATE, and PLAN override those in INFO."
1997 (save-window-excursion
1998 (save-excursion
1999 ;; page-name priority number status description
2000 ;; link link-text plan date
2001 ;; Create the task in the plan page
2002 (let ((plan-page (or plan (planner-task-plan info)))
2003 (date-page (or date (planner-task-date info)))
2004 (live-buffers
2005 (and (equal planner-tasks-file-behavior 'close)
2006 (buffer-list))))
2007 (when plan-page
2008 (if (string-match planner-date-regexp
2009 plan-page)
2010 (setq plan-page nil)))
2011 (when (and plan-page (not (string= plan-page "")))
2012 (planner-find-file plan-page)
2013 (planner-seek-task-creation-point)
2014 (insert (planner-format-task info priority number
2015 status description
2016 (planner-make-link date-page)
2017 (planner-make-link date-page))
2018 "\n"))
2019 ;; Create the task in the date page
2020 (when (and date-page (not (string= date-page "")))
2021 (planner-goto date-page)
2022 (planner-seek-task-creation-point)
2023 (insert (planner-format-task info priority number
2024 status description
2026 link-text
2027 (planner-task-link-text info))
2028 plan-page) "\n"))
2029 (forward-line -1)
2030 (run-hooks 'planner-create-task-hook)
2031 (when planner-tasks-file-behavior
2032 (planner-save-buffers live-buffers t))))))
2034 (defvar planner-task-format "#%s%s %s %s%s"
2035 "Format used by `planner-format-task' when inserting new tasks.")
2037 (defun planner-format-task
2038 (task-info &optional priority number status description link-text link)
2039 "Return a string containing TASK-INFO ready to be inserted into a page.
2040 Non-nil values of PRIORITY, NUMBER, STATUS, DESCRIPTION, LINK-TEXT,
2041 and LINK override TASK-INFO."
2042 (format planner-task-format
2043 (or priority (planner-task-priority task-info))
2044 (if planner-use-task-numbers
2045 (format "%-2s" (or number (planner-task-number task-info) ""))
2047 (or status (planner-task-status task-info))
2048 (or description (planner-task-description task-info))
2049 (let ((text (or link-text
2050 (and link (planner-make-link link))
2051 (planner-task-link-text task-info))))
2052 (if (and text (not (equal text "")))
2053 (concat " ("
2054 text
2055 ")")
2056 ""))))
2058 ;;;_ + Scheduling
2060 (defun planner-copy-or-move-region (beg end &optional date muffle-errors)
2061 "Move all tasks from BEG to END to DATE.
2062 If this is the original task, it copies it instead of moving.
2063 Most of the time, the original should be kept in a planning file,
2064 but this is not required. If BEG is at the end of a line, the
2065 first task moved will be the one on the following line. If END is
2066 at the beginning of a line, the last task moved will be the one
2067 on the previous line. If MUFFLE-ERRORS is non-nil, no errors will
2068 be reported."
2069 (interactive "r")
2070 (unless date (setq date
2071 (let ((planner-expand-name-favor-future-p
2072 (or planner-expand-name-favor-future-p
2073 planner-task-dates-favor-future-p)))
2074 (planner-read-date))))
2075 (let ((start (if (< beg end) beg end))
2076 (finish (if (< beg end) end beg))
2077 (buffer (current-buffer))
2078 (error-count 0)
2079 (count 0)
2080 (live-buffers (when (equal planner-tasks-file-behavior
2081 'close)
2082 (buffer-list))))
2083 ;; Invoke planner-copy-or-move-task on each line in reverse
2084 (let ((planner-tasks-file-behavior nil)
2085 (task-info)
2086 prev-point)
2087 (save-excursion
2088 (save-restriction
2089 (narrow-to-region
2090 (and (goto-char (1+ start)) (planner-line-beginning-position))
2091 (and (goto-char (1- finish))
2092 (min (point-max)
2093 (1+ (planner-line-end-position)))))
2094 (when planner-add-task-at-end-flag
2095 (reverse-region (point-min) (point-max)))
2096 (goto-char (point-max))
2097 (setq prev-point (point))
2098 (while (not (and (bobp)
2099 (= (point) prev-point)))
2100 (forward-line 0)
2101 (setq prev-point (point))
2102 (setq task-info (planner-current-task-info))
2103 ;; Don't move completed or cancelled tasks
2104 (if (and task-info
2105 (not (member (planner-task-status task-info) '("C" "X"))))
2106 (condition-case err
2107 (when (planner-copy-or-move-task date)
2108 (setq count (1+ count)))
2109 (error
2110 (unless (or muffle-errors (not (interactive-p)))
2111 (message
2112 "Error with %s: %s"
2113 (planner-task-description (planner-current-task-info)) err)
2114 (setq error-count (1+ error-count)))
2115 (forward-line -1)
2116 nil))
2117 (forward-line -1)))
2118 (when planner-add-task-at-end-flag
2119 (reverse-region (point-min) (point-max)))
2120 (when (and (not muffle-errors)
2121 (not error-count)
2122 (> error-count 0)
2123 (interactive-p))
2124 (message (if (> error-count 1) "%d errors." "%d error.")
2125 error-count)))))
2126 (when planner-tasks-file-behavior
2127 (planner-save-buffers live-buffers))
2128 (set-buffer buffer)
2129 count)) ; Return the number of tasks moved.
2131 ;;;_ + Navigation
2133 (defvar planner-jump-to-linked-task-function 'planner-jump-to-linked-task-basic
2134 "Function to jump to a linked task. Function should have one
2135 optional parameter: TASK-INFO.")
2137 (defun planner-jump-to-linked-task (&optional task-info)
2138 "Display the task page linked to by the current task or TASK-INFO."
2139 (funcall planner-jump-to-linked-task-function task-info))
2141 (defun planner-jump-to-linked-task-basic (&optional task-info)
2142 "Display the task page linked to by the current task or TASK-INFO."
2143 (interactive)
2144 (let* ((task-info (or task-info (planner-current-task-info)))
2145 (link (and task-info (planner-task-link task-info))))
2146 (when (planner-local-page-p link)
2147 (planner-find-file (planner-task-link task-info))
2148 (widen)
2149 (goto-char (point-min))
2150 (when (search-forward (planner-task-description task-info) nil t)
2151 (beginning-of-line)
2152 t))))
2154 ;;;_ + Convenience
2156 (defvar planner-history-list nil "History list for pages.")
2158 (defvar planner-read-name-function 'planner-read-name-single
2159 "Function to call in order to read the names of pages.")
2161 (defun planner-read-name (file-alist &optional prompt initial)
2162 "Read the name of a valid Wiki page from minibuffer.
2163 FILE-ALIST is a list of (page-name . filename) entries. If PROMPT
2164 is non-nil, it is used as the prompt string. If INITIAL is specified,
2165 it is used as a reasonable default."
2166 (funcall planner-read-name-function file-alist prompt initial))
2168 (defun planner-read-name-single (file-alist &optional prompt initial)
2169 "Read the name of a valid Wiki page from minibuffer with completion.
2170 FILE-ALIST is a list of (page-name . filename) entries. If PROMPT
2171 is non-nil, it is used as the prompt string. If INITIAL is specified,
2172 it is used as a reasonable default."
2173 (let* ((default planner-default-page)
2174 (str (completing-read
2175 (format "%s(default: %s) " (or prompt "Page: ") default)
2176 file-alist nil nil initial 'planner-history-list)))
2177 (cond
2178 ((or (null str) (= (length str) 0)) default)
2179 ((string= str "nil") nil)
2180 (t str))))
2182 (defun planner-read-name-no-completion (names &optional prompt initial)
2183 "Read the name of a valid Wiki page from minibuffer without completion.
2184 FILE-ALIST is a list of (page-name . filename) entries. If PROMPT
2185 is non-nil, it is used as the prompt string. If INITIAL is specified,
2186 it is used as a reasonable default."
2187 (let* ((default planner-default-page)
2188 (str (read-string
2189 (format "%s(default: %s) " (or prompt "Page: ") default)
2190 initial 'planner-history-list default)))
2191 (cond
2192 ((or (null str) (= (length str) 0)) default)
2193 ((string= str "nil") nil)
2194 (t str))))
2196 (defun planner-read-non-date-page (file-alist &optional prompt initial)
2197 "Prompt for a page name that does not match `planner-date-regexp'.
2198 Base completion on FILE-ALIST. If PROMPT is non-nil, use that as
2199 the prompt. If INITIAL is non-nil, use that as the initial contents
2200 of the minibuffer."
2201 (planner-read-name
2202 (delq nil
2203 (mapcar
2204 (lambda (item)
2205 (unless (string-match
2206 (concat "^\\(?:" planner-date-regexp "\\)$")
2207 (car item))
2208 item))
2209 (copy-alist file-alist)))
2210 prompt initial))
2212 (defvar planner-find-task-function 'planner-find-task-basic
2213 "Function to find a task based on INFO and POINT.")
2215 (defun planner-find-task (info &optional point)
2216 "Move point to the character before the task described by INFO.
2217 If POINT is specified, start search from that point."
2218 (funcall planner-find-task-function info point))
2220 (defun planner-find-task-basic (info &optional point)
2221 "Move point to the character before the task described by INFO.
2222 If POINT is specified, start search from that point."
2223 (goto-char (or point (point-min)))
2224 (when (re-search-forward
2225 (concat
2226 "^#[A-C][0-9]*\\s-+.\\s-+"
2227 (regexp-quote (planner-task-description info))) nil t)
2228 (goto-char (planner-line-beginning-position))))
2230 (defun planner-tasks-equal-p (task-a task-b)
2231 "Return t if TASK-A and TASK-B are equivalent.
2232 This is true if they have the same value for priority, status,
2233 description, plan and date."
2234 (and (string= (or (planner-task-priority task-a) "")
2235 (or (planner-task-priority task-b) ""))
2236 (string= (or (planner-task-status task-a) "")
2237 (or (planner-task-status task-b) ""))
2238 (string= (or (planner-task-description task-a) "")
2239 (or (planner-task-description task-b) ""))
2240 (string= (or (planner-task-plan task-a) "")
2241 (or (planner-task-plan task-b) ""))
2242 (string= (or (planner-task-date task-a) "")
2243 (or (planner-task-date task-b) ""))))
2245 (defun planner-save-buffers (&optional buffer-list suppress-fixing skip-buffer)
2246 "Save all planner buffers.
2247 If BUFFER-LIST is a list of buffers, close all buffers not found
2248 in that list. If SUPPRESS-FIXING is non-nil, do not perform any
2249 planner-related modifications such as task sorting. If
2250 SKIP-BUFFER is non-nil, do not save that buffer."
2251 (interactive)
2252 (setq suppress-fixing (and (not planner-tasks-never-suppress-fixing-flag)
2253 suppress-fixing))
2254 (mapcar
2255 (lambda (buffer)
2256 (unless (eq buffer skip-buffer)
2257 (with-current-buffer buffer
2258 ;; Save all planner buffers
2259 (when (and (planner-derived-mode-p 'planner-mode)
2260 buffer-file-name
2261 (planner-page-name)
2262 (not (string= "" (planner-page-name))))
2263 ;; SUPPRESS-FIXING is negated in the following forms because
2264 ;; it makes more sense to let planner-save-buffers do the
2265 ;; usual actions when the parameter is omitted.
2266 (let ((planner-sort-tasks-automatically
2267 (and planner-sort-tasks-automatically
2268 (not suppress-fixing)))
2269 (planner-renumber-tasks-automatically
2270 (and planner-renumber-tasks-automatically
2271 (not suppress-fixing)))
2272 (planner-align-tasks-automatically
2273 (and planner-align-tasks-automatically
2274 (not suppress-fixing)))
2275 (planner-renumber-notes-automatically
2276 (and planner-renumber-notes-automatically
2277 (not suppress-fixing)))
2278 (planner-tasks-save-behavior nil)
2279 (planner-id-update-automatically nil))
2280 (when (buffer-modified-p)
2281 (save-buffer)))
2282 (when (and buffer-list
2283 (not (memq buffer buffer-list)))
2284 (kill-buffer nil))))))
2285 (buffer-list)))
2287 ;;;_ + Extraction
2289 (defvar planner-task-regexp (concat "^#[A-C][0-9]*\\s-+.\\s-+")
2290 "Regexp used to match tasks.")
2292 (defvar planner-live-task-regexp "^#[ABC][0-9]*\\s-+[_oDP]\\s-+"
2293 "Regular expression matching \"live\" tasks.
2294 A task is live if it is not finished and it is not cancelled.")
2296 (defun planner-extract-tasks (pages &optional condition)
2297 "Parse PAGES and extract all tasks.
2298 If CONDITION is non-nil, it should be a function that
2299 accepts the task info as an argument and returns t if
2300 the task should be added to the list."
2301 (with-temp-buffer
2302 (unless (consp (car pages))
2303 (let ((list (planner-file-alist)))
2304 (setq pages (mapcar '(lambda (page)
2305 (cons page (cdr (assoc page list))))
2306 pages))))
2307 (let (result)
2308 (while pages
2309 (erase-buffer)
2310 (insert-file-contents (cdar pages))
2311 (goto-char (point-max))
2312 (while (re-search-backward "^#[A-C]" nil t)
2313 (let ((info
2314 (planner-task-info-from-string
2315 (caar pages)
2316 (buffer-substring
2317 (planner-line-beginning-position)
2318 (planner-line-end-position)))))
2319 (when (and info
2320 (if condition
2321 (funcall condition info)
2323 (setq result (append (list info) result)))))
2324 (setq pages (cdr pages)))
2325 result)))
2327 (defun planner-extract-tasks-with-status (pages status)
2328 "Return all tasks on PAGES with the specified STATUS."
2329 (planner-extract-tasks pages
2330 (lambda (item)
2331 (equal (planner-task-status item)
2332 status))))
2334 (defun planner-tasks-tag (beg end attrs)
2335 "Replace the region BEG to END with a report of tasks.
2336 If status is specified in ATTRS, list tasks matching that status only.
2337 To negate the sense of a match, use a regexp."
2338 (delete-region beg end)
2339 (let* ((status (cdr (assoc "status" attrs)))
2340 (tasks (planner-extract-tasks
2341 (planner-get-day-pages nil nil t)
2342 (if status
2343 (lambda (item)
2344 (string-match status (planner-task-status item)))
2345 nil))))
2346 (while tasks
2347 (insert
2348 (planner-make-link (planner-task-page (car tasks)) nil t)
2349 " | "
2350 (planner-task-priority (car tasks))
2351 " | "
2352 (planner-task-status (car tasks))
2353 " | "
2354 (planner-task-description (car tasks))
2355 "\n")
2356 (setq tasks (cdr tasks)))))
2358 (defvar planner-on-date-page nil
2359 "Internal variable used in `planner-sort-tasks-default-key'.")
2361 (defun planner-sort-tasks-default-key ()
2362 "Provide old sorting behavior.
2363 Day pages sort by status and priority. Plan pages sort by date,
2364 status and priority."
2365 (if planner-on-date-page
2366 (planner-sort-tasks-basic)
2367 (planner-sort-tasks-by-date)))
2369 (defun planner-sort-tasks-basic ()
2370 "Sort tasks by status (_PDXC) and priority (ABC)."
2371 (skip-chars-forward "#ABC")
2372 (let ((case-fold-search t)
2373 (ch (char-before))
2374 status)
2375 (skip-chars-forward "0123456789 ")
2376 (setq status (char-after))
2377 (+ ;(read (current-buffer))
2378 (cond
2379 ((eq status ?P) 1000)
2380 ((eq status ?D) 2000)
2381 ((eq status ?X) 3000)
2382 ((eq status ?C) 4000)
2383 (t 0))
2384 (cond ((eq ch ?A) 100)
2385 ((eq ch ?B) 200)
2386 ((eq ch ?C) 300)
2387 (t 0)))))
2389 (defun planner-sort-tasks-by-date ()
2390 "Sort tasks by date, status and priority."
2391 (skip-chars-forward "#ABC")
2392 (let ((ch (char-before))
2393 status)
2394 (skip-chars-forward "0123456789 ")
2395 (setq status (char-after))
2396 (goto-char (planner-line-end-position))
2397 (skip-chars-backward "]) ")
2398 (format "%1c%1c%10s"
2399 (if (or (= status ?X)
2400 (= status ?C))
2401 status ?\ )
2403 (if (= (skip-chars-backward "0123456789.")
2404 -10)
2405 (buffer-substring (point)
2406 (+ 10 (point)))
2407 planner-sort-undated-tasks-equivalent))))
2409 (defun planner-sort-tasks-by-link ()
2410 "Sort tasks by status, priority and link."
2411 (let ((info (planner-current-task-info)))
2412 (concat ;(read (current-buffer))
2413 (cond
2414 ((string= (planner-task-status info) "P") "1")
2415 ((string= (planner-task-status info) "D") "2")
2416 ((string= (planner-task-status info) "X") "3")
2417 ((string= (planner-task-status info) "C") "4")
2418 (t "0"))
2419 (planner-task-priority info)
2420 (or (planner-task-link info) ""))))
2422 (defun planner-sort-tasks ()
2423 "Sort the tasks.
2424 On day pages, sort according to priority and position. On plan
2425 pages, sort according to status, priority, date, and position."
2426 (interactive)
2427 (let ((case-fold-search nil)
2428 (planner-on-date-page (string-match planner-date-regexp
2429 (planner-page-name)))
2430 (old-task (planner-current-task-info))
2431 (line-offset (- (point) (planner-line-beginning-position)))
2432 (old-point (point)))
2433 (goto-char (point-min))
2434 (while (re-search-forward "^#[A-C][0-9]*" nil t)
2435 (goto-char (match-beginning 0))
2436 (let ((here (point)))
2437 (while (and (char-after) (= (char-after) ?#))
2438 (forward-line 1))
2439 (save-restriction
2440 (narrow-to-region here (point))
2441 (goto-char here)
2442 (condition-case err
2443 (sort-subr nil
2444 'forward-line 'end-of-line
2445 planner-sort-tasks-key-function nil
2446 nil)
2447 (wrong-number-of-arguments ; OLD EMACS, 5 args
2448 (sort-subr nil
2449 'forward-line 'end-of-line
2450 planner-sort-tasks-key-function nil)))
2451 (goto-char (point-max)))))
2452 (if old-task
2453 (progn
2454 (planner-find-task old-task)
2455 (forward-char line-offset))
2456 (goto-char old-point))
2457 nil)) ; Must return nil because of write-file-functions
2459 (defun planner-sort-tasks-maybe ()
2460 "Sort tasks depending on `planner-sort-tasks-automatically'."
2461 (when planner-sort-tasks-automatically
2462 (planner-sort-tasks)))
2464 (defun planner-renumber-tasks ()
2465 "Update task numbering to be in sequence once again."
2466 (interactive)
2467 (let ((old-point (point)))
2468 (goto-char (point-min))
2469 (let ((counters (list (cons "A" 1) (cons "B" 1) (cons "C" 1))))
2470 (while (re-search-forward "^#\\([A-C]\\)\\([0-9]+\\)" nil t)
2471 (let ((counter (assoc (match-string 1) counters)))
2472 (replace-match (number-to-string (cdr counter)) t t nil 2)
2473 (setcdr counter (1+ (cdr counter))))))
2474 (goto-char old-point))
2475 nil) ; Must return nil because of write-file-functions
2477 (defun planner-renumber-tasks-maybe ()
2478 "Renumber tasks depending on `planner-renumber-tasks-automatically'."
2479 (when planner-renumber-tasks-automatically
2480 (planner-renumber-tasks)))
2482 (defun planner-fix-tasks ()
2483 "Sort, renumber and align tasks."
2484 (interactive)
2485 (planner-sort-tasks)
2486 (planner-renumber-tasks)
2487 (planner-align-tasks))
2489 ;;;_ + Notes
2491 ;;;###autoload
2492 (defun planner-create-note (&optional page)
2493 "Create a note to be remembered in PAGE (today if PAGE is nil).
2494 If `planner-reverse-chronological-notes' is non-nil, create the
2495 note at the beginning of the notes section; otherwise, add it to
2496 the end. Position point after the anchor."
2497 (interactive (list (and (planner-derived-mode-p 'planner-mode)
2498 (planner-page-name))))
2499 (planner-goto (or page
2500 (and (planner-derived-mode-p 'planner-mode)
2501 (planner-page-name))
2502 (planner-today)))
2503 (planner-seek-to-first 'notes)
2504 (save-restriction
2505 (when (planner-narrow-to-section 'notes)
2506 (let ((total 0))
2507 (goto-char (point-min))
2508 (while (re-search-forward "^\.#\\([0-9]+\\)\\s-+" nil t)
2509 (setq total (max total (string-to-number (match-string 1)))))
2510 (if planner-reverse-chronological-notes
2511 (progn (goto-char (point-min))
2512 (forward-line 1)
2513 (skip-chars-forward "\n"))
2514 (goto-char (point-max))
2515 (skip-chars-backward "\n")
2516 (when (= (forward-line 1) 1) (insert "\n"))
2517 (when (= (forward-line 1) 1) (insert "\n")))
2518 (insert ".#" (number-to-string (1+ total)) " ")
2519 (unless (eobp) (save-excursion (insert "\n\n")))
2520 (1+ total)))))
2522 (defun planner-delete-note ()
2523 "Delete the current note."
2524 (interactive)
2525 (save-window-excursion
2526 (let ((info (planner-current-note-info)))
2527 (when info
2528 (save-window-excursion
2529 (when (planner-jump-to-linked-note info)
2530 (save-restriction
2531 (planner-narrow-to-note)
2532 (delete-region (point-min) (point-max)))))
2533 (save-restriction
2534 (planner-narrow-to-note)
2535 (delete-region (point-min) (point-max)))))))
2537 (defun planner-format-note (info &optional anchor title timestamp link body)
2538 "Return the string representation of INFO.
2539 ANCHOR, TITLE, TIMESTAMP, LINK and BODY override INFO if present."
2540 (unless anchor (setq anchor (planner-note-anchor info)))
2541 (unless title (setq title (planner-note-title info)))
2542 (unless timestamp (setq timestamp (planner-note-timestamp info)))
2543 (unless link (setq link (planner-note-link info)))
2544 (unless body (setq body (planner-note-body info)))
2545 (concat (if (and anchor (not (string= "" anchor)))
2546 (concat ".#" anchor " ")
2548 title
2549 (if (and timestamp (not (string= "" timestamp)))
2550 (concat " " timestamp)
2552 (if (and link (not (string= "" link))) (concat " (" link ")") "")
2553 (if (and body (not (string= "" body))) body "")))
2555 (defun planner-update-note ()
2556 "Copy the text from this note to the linked note, if any."
2557 (interactive)
2558 (save-window-excursion
2559 (let ((info (planner-current-note-info))
2560 text)
2561 (save-restriction
2562 (when (planner-narrow-to-note)
2563 (setq text (buffer-substring-no-properties (point-min) (point-max)))
2564 ;; Get rid of the anchor.
2565 (when (string-match "^\\.#[0-9]+\\s-+" text)
2566 (setq text (replace-match "" nil t text)))
2567 ;; Get rid of the link
2568 (when (string-match "\\s-+(\\[\\[.+?\\]\\])" text)
2569 (setq text (replace-match "" nil t text)))))
2570 ;; Insert the new body
2571 (when (planner-jump-to-linked-note)
2572 (save-restriction
2573 (when (planner-narrow-to-note)
2574 (goto-char (point-min))
2575 (skip-chars-forward ".#0-9")
2576 (delete-region (point) (point-max))
2577 (insert " " text)
2578 (goto-char (point-min))
2579 (goto-char (planner-line-end-position))
2580 (insert " ("
2581 (planner-make-link
2582 (concat (planner-note-page info) "#"
2583 (planner-note-anchor info)))
2584 ")")))))))
2586 ;; Case 1a: Date and plan page exist, new plan page wanted
2587 ;; Case 1b: Date page exists, no plan yet, plan page wanted
2588 ;; Case 2: Date and plan page exist, no plan page wanted
2589 ;; Case 3: No date, just plan page
2590 (defun planner-replan-note (page)
2591 "Change or assign the plan page for the current note.
2592 PAGE-NAME is the new plan page for the note."
2593 (interactive
2594 (list (planner-read-non-date-page
2595 (planner-file-alist) nil
2596 (planner-note-link-text (planner-current-note-info)))))
2597 (let ((info (planner-current-note-info t)))
2598 (when (and page
2599 (or (string= page (planner-note-plan info))
2600 (string= page (planner-note-date info))))
2601 (error "Same plan page"))
2602 (when (null (or page (planner-note-date info)))
2603 (error "Cannot unplan note without day page"))
2604 (save-window-excursion
2605 ;; Delete the old plan note
2606 (when (planner-note-plan info)
2607 (when (string-match planner-date-regexp (planner-note-page info))
2608 (planner-jump-to-linked-note info))
2609 (save-restriction
2610 (planner-narrow-to-note)
2611 (delete-region (point-min) (point-max))))
2612 (let (new)
2613 (when page
2614 ;; Create note on plan page
2615 (setq new (planner-create-note page))
2616 (insert (planner-format-note
2617 info "" nil nil
2618 (if (planner-note-date info)
2619 (planner-make-link
2620 (concat (planner-note-date info)
2622 (planner-note-anchor info)))
2623 ""))))
2624 ;; Update note on date page, if any
2625 (forward-line -1)
2626 (when (planner-note-date info)
2627 (if (string-match planner-date-regexp (planner-note-page info))
2628 (progn
2629 (planner-find-file (planner-note-date info))
2630 (goto-char (point-min))
2631 (re-search-forward (concat "^\\.#" (planner-note-anchor info)
2632 "\\s-")
2633 nil t))
2634 (planner-jump-to-linked-note info))
2635 (save-restriction
2636 (planner-narrow-to-note)
2637 (delete-region (point-min) (point-max))
2638 (insert (planner-format-note
2639 info nil nil nil
2640 (if new
2641 (planner-make-link
2642 (concat (planner-link-base page) "#"
2643 (number-to-string new)))
2644 "")))))))))
2646 ;; Improvements:
2648 ;; - Link back to the task? If we can figure out how to stably link to
2649 ;; a task in the first place...
2651 ;; - Should plan-page-p default to t? be a customizable variable? What
2652 ;; should it be called? I have the urge to write
2653 ;; planner-create-note-from-task-behavior which can have the
2654 ;; following values: 'day, 'plan, 'copy, 'xref ...
2656 (defun planner-create-note-from-task (&optional plan-page-p)
2657 "Create a note based on the current task.
2658 Argument PLAN-PAGE-P is used to determine whether we put the new
2659 note on the task's plan page or on the current page."
2660 (interactive "P")
2661 (let ((task-info (planner-current-task-info))
2662 note-num)
2663 (when task-info
2664 ;; If PLAN-PAGE-P and the task has a plan page, create a note on
2665 ;; the plan page. If not, create it on the current page.
2666 (when (and plan-page-p
2667 (string= (planner-task-date task-info)
2668 (planner-task-page task-info)))
2669 (planner-jump-to-linked-task task-info))
2670 (setq note-num (planner-create-note (planner-page-name)))
2671 (save-excursion
2672 (save-window-excursion
2673 (when (planner-find-task task-info)
2674 (planner-edit-task-description
2675 (concat (planner-task-description task-info) " "
2676 (planner-make-link
2677 (concat (planner-page-name) "#"
2678 (number-to-string note-num))
2679 (format "(%d)" note-num)))))))
2680 (insert " " (planner-task-description task-info) "\n\n"))))
2682 (defun planner-create-note-from-context (&optional plan-page-p)
2683 "Create a note based on the current context.
2684 If on a task item, call `planner-create-note-from-task'.
2685 Anywhere else, call `Footnote-add-footnote' if footnote has been
2686 loaded, else, call `planner-create-note'."
2689 (defun planner-narrow-to-note (&optional page note-number)
2690 "Narrow to the specified note. Widen and return nil if note is not found.
2691 If PAGE is nil, use current page.
2692 If NOTE-NUMBER is nil, use current note.
2693 Undefined behavior if PAGE is (non-nil and not today) and NOTE-NUMBER is nil."
2694 (when page (planner-goto page))
2695 (save-excursion
2696 (let (beginning)
2697 (if note-number
2698 (progn
2699 (goto-char (point-min))
2700 (when (re-search-forward (concat "^\\.#" note-number) nil t)
2701 (setq beginning (match-beginning 0))))
2702 (goto-char (planner-line-end-position))
2703 (when (re-search-backward "^\\.#[0-9]+" nil t)
2704 (setq beginning (planner-line-beginning-position))))
2705 (when beginning
2706 ;; Search for the end
2707 (forward-line 1)
2708 (narrow-to-region
2709 beginning
2710 (or (save-excursion
2711 (and (re-search-forward "^\\(\\.#\\|* \\)" nil t)
2712 (match-beginning 0)))
2713 (point-max)))
2714 t))))
2716 (defun planner-note-page (note-info)
2717 "Return the page specified by NOTE-INFO."
2718 (elt note-info 0))
2719 (defun planner-note-anchor (note-info)
2720 "Return the anchor specified by NOTE-INFO."
2721 (elt note-info 1))
2722 (defun planner-note-title (note-info)
2723 "Return the title specified by NOTE-INFO."
2724 (elt note-info 2))
2725 (defun planner-note-timestamp (note-info)
2726 "Return the timestamp specified by NOTE-INFO."
2727 (elt note-info 3))
2728 (defun planner-note-link (note-info)
2729 "Return the link specified by NOTE-INFO."
2730 (elt note-info 4))
2731 (defun planner-note-link-text (note-info)
2732 "Return the link text specified by NOTE-INFO."
2733 (elt note-info 4))
2734 (defun planner-note-body (note-info)
2735 "Return the timestamp specified by NOTE-INFO."
2736 (elt note-info 5))
2738 (defun planner-note-date (note-info)
2739 "Return the date for NOTE-INFO."
2740 (cond
2741 ((string-match planner-date-regexp (planner-note-page note-info))
2742 (planner-note-page note-info))
2743 ((and (planner-note-link note-info)
2744 (string-match planner-date-regexp (planner-note-link note-info)))
2745 (planner-link-base (planner-note-link note-info)))))
2747 (defun planner-note-plan (note-info)
2748 "Return the date for NOTE-INFO."
2749 (cond
2750 ((null (string-match planner-date-regexp (planner-note-page note-info)))
2751 (planner-note-page note-info))
2752 ((and (planner-note-link note-info)
2753 (null (string-match planner-date-regexp
2754 (planner-note-link note-info))))
2755 (planner-link-base (planner-note-link note-info)))))
2757 (defun planner-current-note-info (&optional include-body)
2758 "Parse the current note and return the note information as a list.
2759 The list is of the form (PAGE ANCHOR TITLE TIMESTAMP LINK BODY).
2760 If INCLUDE-BODY is non-nil, the list will include the body of the
2761 note."
2762 (save-excursion
2763 (save-restriction
2764 (when (planner-narrow-to-note)
2765 (goto-char (point-min))
2766 (when (looking-at "^\\.#\\([0-9]+\\)\\s-+\\(.+\\)")
2767 (let ((anchor (planner-match-string-no-properties 1))
2768 (title (planner-match-string-no-properties 2))
2769 timestamp link)
2770 (when (string-match
2771 (concat
2772 "\\s-+(\\("
2773 (if (featurep 'planner-multi)
2774 (concat "\\(" muse-explicit-link-regexp "\\)"
2775 "\\("
2776 (regexp-quote planner-multi-separator)
2777 muse-explicit-link-regexp
2778 "\\)*")
2779 muse-explicit-link-regexp)
2780 "\\))\\s-*$")
2781 title)
2782 (setq link (planner-match-string-no-properties 1 title))
2783 (setq title (replace-match "" nil t title)))
2784 (when (string-match "\\s-*\\([0-9]+:[0-9][0-9]\\)" title)
2785 (setq timestamp (planner-match-string-no-properties 1 title))
2786 (setq title (replace-match "" nil t title)))
2787 (list (planner-page-name) anchor title timestamp link
2788 (and include-body (buffer-substring-no-properties
2789 (planner-line-end-position)
2790 (point-max))))))))))
2792 (defun planner-search-notes-internal (regexp &optional limit include-body)
2793 "Return an alist of all notes in daily plan pages containing REGEXP.
2794 The alist is of the form ((REFERENCE TITLE BODY) (REFERENCE TITLE BODY)
2795 ...). If LIMIT is non-nil, do not search days before LIMIT. If
2796 INCLUDE-BODY is non-nil, return the body text, else return nil."
2797 (let ((pages (planner-get-day-pages limit t))
2798 filename
2799 page start anchor text results title page-results)
2800 (while pages
2801 (setq page (caar pages)
2802 filename (cdar pages))
2803 (with-temp-buffer
2804 (when (file-readable-p filename)
2805 (insert-file-contents filename)
2806 (setq start nil)
2807 (setq page-results nil)
2808 ;; Find the first note
2809 (when (re-search-forward "\\.\\(#[0-9]+\\)\\s-+\\(.*\\)" nil t)
2810 (setq start (match-beginning 2))
2811 (setq anchor (match-string 1))
2812 (setq title (match-string 2)))
2813 (while (re-search-forward "\\.\\(#[0-9]+\\)\\s-+\\(.*\\)" nil t)
2814 ;; The text between start and (1- (match-beginning 0))
2815 ;; is the note body.
2816 (when (save-excursion
2817 (goto-char (planner-line-beginning-position))
2818 (save-match-data (re-search-backward regexp start t)))
2819 (add-to-list 'page-results
2820 (list (concat filename anchor)
2821 (concat page anchor)
2822 title
2823 (if include-body
2824 (buffer-substring-no-properties
2825 start
2826 (planner-line-beginning-position))))))
2827 (setq start (match-beginning 2))
2828 (setq anchor (match-string 1))
2829 (setq title (match-string 2)))
2830 (when start
2831 (goto-char (point-max))
2832 (when (save-excursion (re-search-backward regexp start t))
2833 (add-to-list 'page-results
2834 (list (concat filename anchor)
2835 (concat page anchor)
2836 title
2837 (if include-body
2838 (buffer-substring-no-properties
2839 start
2840 (point)))))))
2841 (when planner-reverse-chronological-notes
2842 (setq page-results (nreverse page-results))))
2843 (setq results (append page-results results)))
2844 (setq pages (cdr pages)))
2845 results))
2847 (defun planner-jump-to-linked-note (&optional note-info)
2848 "Display the note linked to by the current note or NOTE-INFO if non-nil."
2849 (interactive)
2850 (setq note-info (or note-info (planner-current-note-info)))
2851 (when (and (planner-note-link note-info)
2852 (save-window-excursion
2853 (planner-visit-link (planner-note-link note-info))))
2854 (planner-visit-link (planner-note-link note-info))
2855 (widen)
2858 (defun planner-renumber-notes ()
2859 "Update note numbering."
2860 (interactive)
2861 (let ((old-point (point))
2862 (counter 1))
2863 (goto-char
2864 (if planner-reverse-chronological-notes (point-max) (point-min)))
2865 (while (if planner-reverse-chronological-notes
2866 (re-search-backward "^\\.#\\([0-9]+\\)" nil t)
2867 (re-search-forward "^\\.#\\([0-9]+\\)" nil t))
2868 (replace-match (number-to-string counter) t t nil 1)
2869 (when planner-reverse-chronological-notes
2870 (goto-char (planner-line-beginning-position)))
2871 (setq counter (1+ counter)))
2872 (goto-char old-point))
2873 nil) ; Must return nil because of write-file-functions
2875 (defun planner-renumber-notes-maybe ()
2876 "Renumber notes depending on `planner-renumber-notes-automatically'."
2877 (when planner-renumber-notes-automatically
2878 (planner-renumber-notes)))
2880 ;;;_ + Highlighting
2882 (defgroup planner-fontlock nil
2883 "Font-locking for planner.el pages."
2884 :prefix "planner-"
2885 :group 'planner)
2887 (defface planner-completed-task-face
2888 (if (featurep 'xemacs)
2889 '((t (:strikethru t :foreground "gray")))
2890 '((t (:strike-through t :foreground "gray"))))
2891 "Face for completed tasks."
2892 :group 'planner-fontlock)
2894 (defface planner-cancelled-task-face
2895 (if (featurep 'xemacs)
2896 '((t (:strikethru t :foreground "gray")))
2897 '((t (:strike-through t :foreground "gray"))))
2898 "Face for cancelled tasks."
2899 :group 'planner-fontlock)
2901 (defface planner-delegated-task-face
2902 '((t (:underline t)))
2903 "Face for delegated tasks."
2904 :group 'planner-fontlock)
2906 (defface planner-in-progress-task-face
2907 (if (featurep 'xemacs)
2908 '((t (:bold t)))
2909 '((t (:slant oblique))))
2910 "Face for tasks in progress."
2911 :group 'planner-fontlock)
2912 (defface planner-high-priority-task-face '((t (:foreground "red")))
2913 "Face for high-priority tasks."
2914 :group 'planner-fontlock)
2915 (defface planner-medium-priority-task-face '((t (:foreground "green")))
2916 "Face for medium-priority tasks."
2917 :group 'planner-fontlock)
2918 (defface planner-low-priority-task-face '((t (:foreground "blue")))
2919 "Face for low-priority tasks."
2920 :group 'planner-fontlock)
2922 (defface planner-note-headline-face
2923 '((((class color) (background light))
2924 (:foreground "dark slate blue" :bold t))
2925 (((class color) (background dark))
2926 (:foreground "pale turquoise" :bold t))
2927 (t (:bold t)))
2928 "Face for note headlines."
2929 :group 'planner-fontlock)
2931 ;; Thanks to Oliver (oik AT gmx DOT net)
2932 (defun planner-align-tasks ()
2933 "Align tasks neatly.
2934 You can add this to `write-file-functions'to have the tasks
2935 automatically lined up whenever you save. For best results,
2936 ensure `planner-align-tasks' is run after
2937 `planner-renumber-tasks'."
2938 (interactive)
2939 (save-excursion
2940 (goto-char (point-min))
2941 (while (re-search-forward "^#\\([A-C]\\)\\([0-9]*\\)\\(\\s-+\\)" nil t)
2942 (replace-match
2943 ;; Ugly hack!
2944 ;; (make-string (max (- (length (match-string 2))) 0) ?\s)
2945 ;; is better, but relies on a CVSism.
2946 (let ((length (length (match-string 2))))
2947 (cond
2948 ((and (= length 0) planner-use-task-numbers) " ")
2949 ((= length 1) " ")
2950 (t " ")))
2951 t t nil 3)))
2952 nil) ; Return nil so that we can add this to write-file-functions
2954 (defun planner-align-tasks-maybe ()
2955 "Align tasks depending on `planner-align-tasks-automatically'."
2956 (when planner-align-tasks-automatically
2957 (planner-align-tasks)))
2959 (defun planner-align-table ()
2960 "Align table neatly. Take account of links which hides characters when
2961 displayed.
2962 Perhaps, guts of this should really be inside muse..."
2963 (interactive)
2964 (save-excursion
2965 (when (fboundp 'align-regexp)
2966 (align-regexp (point-min) (point-max) "\\(\\s-*\\)|" 1 1 t))
2967 (goto-char (point-min))
2968 (while (re-search-forward (concat "^" muse-explicit-link-regexp) nil t)
2969 (let ((link (match-string 1))
2970 (desc (match-string 2)))
2971 (insert " "
2972 (make-string (if desc
2973 (+ 2 (length link))
2974 0) (aref " " 0)))))))
2976 ;; FIXME: Is there a better way to do this?
2978 (defun planner-highlight-region (beg end identifier priority properties)
2979 "Add the specified text properties to the overlay or region.
2980 BEG and END are the start and end of the region. IDENTIFIER is a
2981 symbol that identifies this particular overlay. PRIORITY controls
2982 how important this overlay is. PROPERTIES is a list of properties
2983 or attributes to apply."
2984 (if (featurep 'xemacs)
2985 (let ((extent (make-extent beg end)))
2986 (set-extent-properties extent properties)
2987 (set-extent-property extent 'priority priority))
2988 (if (functionp 'overlay-put)
2989 (progn
2990 (let ((overlay (make-overlay beg end)))
2991 (overlay-put overlay identifier t)
2992 (overlay-put overlay 'planner t)
2993 (overlay-put overlay 'priority priority)
2994 (while properties
2995 (overlay-put overlay (car properties) (cadr properties))
2996 (setq properties (cddr properties)))))
2997 (add-text-properties beg end properties))))
2999 (defcustom planner-hide-task-status-when-highlighting nil
3000 "*If non-nil, hide task status when font-locking."
3001 :type 'boolean
3002 :group 'planner-fontlock)
3004 (defun planner-highlight-tasks (beg end &optional verbose)
3005 "Highlight tasks from BEG to END. VERBOSE is ignored."
3006 (goto-char beg)
3007 (while (re-search-forward (concat "^#\\([A-C]\\)\\([0-9]*\\)\\s-+\\("
3008 planner-marks-regexp
3009 "\\)\\s-") end t)
3010 (let ((mark (match-string 3))
3011 (priority (match-string 1))
3012 faces)
3013 (setq faces
3014 (append
3015 (cond
3016 ((string= priority "A") '(planner-high-priority-task-face))
3017 ((string= priority "B") '(planner-medium-priority-task-face))
3018 ((string= priority "C") '(planner-low-priority-task-face)))
3019 (cond
3020 ((string= mark "X") '(planner-completed-task-face))
3021 ((string= mark "D") '(planner-delegated-task-face))
3022 ((string= mark "C") '(planner-cancelled-task-face))
3023 ((string= mark "o") '(planner-in-progress-task-face)))))
3024 (if (featurep 'xemacs)
3025 (mapcar (lambda (face)
3026 (when face
3027 (planner-highlight-region
3028 (match-beginning 0) (match-end 3) 'planner-task 50
3029 (list 'face face)))) faces)
3030 (planner-highlight-region
3031 (match-beginning 0) (match-end 3) 'planner-task 50
3032 (list 'face (mapcar 'face-attr-construct faces))))
3033 (planner-highlight-region
3034 (match-end 3) (planner-line-end-position)
3035 'planner-task
3037 (list 'face
3038 (cond
3039 ((string= mark "X") 'planner-completed-task-face)
3040 ((string= mark "D") 'planner-delegated-task-face)
3041 ((string= mark "C") 'planner-cancelled-task-face)
3042 ((string= mark "o") 'planner-in-progress-task-face))))
3043 (when planner-hide-task-status-when-highlighting
3044 (planner-highlight-region
3045 (match-beginning 3) (1+ (match-end 3))
3046 'planner-task
3048 (list 'invisible t))))))
3050 (defun planner-highlight-notes (beg end &optional verbose)
3051 "Highlight notes as second-level headers from BEG to END.
3052 VERBOSE is ignored."
3053 (goto-char beg)
3054 (while (re-search-forward "^\\.#\\([0-9]+\\) [^(\n]+" end t)
3055 (add-text-properties
3056 (match-beginning 0) (match-end 0)
3057 '(face planner-note-headline-face))))
3059 (defun planner-notes-get-headlines (&optional limit)
3060 "Return note headlines on the current page.
3061 If LIMIT is non-nil, return only that many from the top."
3062 (when (stringp limit) (setq limit (string-to-number limit)))
3063 (let (headlines)
3064 (save-excursion
3065 (save-restriction
3066 (widen)
3067 (goto-char (point-min))
3068 (while (and
3069 (re-search-forward "^.\\(#[0-9]+\\)\\s-+\\(.+\\)" nil t)
3070 (if limit
3071 (> limit 0)
3073 (add-to-list
3074 'headlines
3075 (cons
3076 (planner-match-string-no-properties 1)
3077 (planner-match-string-no-properties 2))
3079 (if limit (setq limit (1- limit))))))
3080 headlines))
3082 ;;;_* Indexing
3084 ;; I want to compress the list of day pages. Arranging them by month
3085 ;; may be a good strategy, although a calendar would be optimal.
3087 (defun planner-index ()
3088 "Display an index of all known Wiki pages."
3089 (interactive)
3090 (let ((muse-current-project (muse-project planner-project)))
3091 (message "Generating Muse index...")
3092 (pop-to-buffer (planner-generate-index))
3093 (goto-char (point-min))
3094 (planner-mode)
3095 (message "Generating Muse index...done")))
3097 (defun planner-generate-index (&optional as-list exclude-private)
3098 "Generate an index of all Wiki pages.
3099 List planner pages separately. If AS-LIST is non-nil, format it
3100 as a list. If EXCLUDE-PRIVATE is non-nil, exclude anything for
3101 which `muse-project-private-p' returns non-nil."
3102 (let ((index (planner-index-as-string as-list exclude-private)))
3103 (with-current-buffer (get-buffer-create "*Planner Index*")
3104 (erase-buffer)
3105 (insert index)
3106 (current-buffer))))
3108 (defun planner-index-as-string (&optional as-list exclude-private
3109 exclude-daypages)
3110 "Generate an index of all Wiki pages.
3111 Day pages are listed separately.
3113 If AS-LIST is non-nil, format it as a list.
3115 If EXCLUDE-PRIVATE is non-nil, exclude anything for which
3116 `muse-project-private-p' returns non-nil.
3118 If EXCLUDE-DAYPAGES is non-nil, exclude day pages from the list."
3119 (let ((index (muse-index-as-string as-list exclude-private)))
3120 (with-temp-buffer
3121 (insert index)
3122 (goto-char (point-min))
3123 (delete-matching-lines
3124 "\\[\\[[0-9][0-9][0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9]\\]\\]")
3125 (unless exclude-daypages
3126 (goto-char (point-max))
3127 (unless (bolp) (insert "\n"))
3128 (if planner-publish-dates-first-p
3129 (progn
3130 (goto-char (point-min))
3131 (insert "\n")
3132 (goto-char (point-min)))
3133 (insert "\n"))
3134 (let ((dates (mapcar 'car (planner-list-daily-files)))
3135 month last-month)
3136 (while dates
3137 (setq month (substring (car dates) 0 7))
3138 (unless (string= month last-month)
3139 (setq last-month month)
3140 (insert "\n" month " |"))
3141 (insert " [[" (car dates) "][."
3142 (substring (car dates) 8)
3143 " ]]")
3144 (setq dates (cdr dates)))
3145 (when planner-publish-dates-first-p
3146 (insert "\n"))))
3147 (buffer-string))))
3149 ;;;_ + Info
3151 (defun planner-annotation-from-info ()
3152 "If called from an info node, return an annotation.
3153 Suitable for use in `planner-annotation-functions'."
3154 (when (eq major-mode 'Info-mode)
3155 (planner-make-link
3156 (concat "info://" Info-current-file "#" Info-current-node)
3157 (if (and (not (equal Info-current-file "dir"))
3158 (equal Info-current-node "Top"))
3159 (file-name-nondirectory Info-current-file)
3160 Info-current-node)
3161 t)))
3163 (custom-add-option 'planner-annotation-functions 'planner-annotation-from-info)
3165 ;;;_ + Man
3167 (defun planner-annotation-from-man ()
3168 "If called from a man page, return an annotation.
3169 Suitable for use in `planner-annotation-functions'."
3170 (when (eq major-mode 'Man-mode)
3171 ;; There may be a better way to get the name of the current Man page.
3172 (let ((page (downcase (cadr (cdar Man-page-list)))))
3173 (planner-make-link
3174 (concat "man://" page)
3175 (concat "Man: " page) t))))
3177 (custom-add-option 'planner-annotation-functions 'planner-annotation-from-man)
3179 ;;;_ + WoMan
3181 (defun planner-annotation-from-woman ()
3182 "If called from a woman page, return an annotation.
3183 Suitable for use in `planner-annotation-functions'."
3184 (when (eq major-mode 'woman-mode)
3185 ;; You'd think there'd be a better way to get the page name.
3186 (let* ((buf (split-string (buffer-name)))
3187 (page (substring (nth 2 buf) 0 -1))
3188 (sec (nth 1 buf)))
3189 (planner-make-link
3190 (concat "woman://" page)
3191 (concat "WoMan: " page "(" sec ")") t))))
3193 (custom-add-option 'planner-annotation-functions 'planner-annotation-from-woman)
3195 ;;;_ + Common mail functions
3197 (defun planner-get-name-from-address (address)
3198 "Return the name for ADDRESS to be used in links."
3199 (let ((addr (mail-extract-address-components address)))
3200 (or (car addr) (cadr addr))))
3202 ;;;_* User functions
3204 ;;;_ + Navigation
3206 (defun planner-page-file (page &optional no-check-p)
3207 "Return a filename if PAGE exists within `planner-project'.
3208 If NO-CHECK-P is non-nil, the planner project files are always updated."
3209 (muse-project-page-file page planner-project))
3211 ;;;###autoload
3212 (defun plan (&optional force-days)
3213 "Start your planning for the day, carrying unfinished tasks forward.
3215 If FORCE-DAYS is a positive integer, search that many days in the past
3216 for unfinished tasks.
3217 If FORCE-DAYS is 0 or t, scan all days.
3218 If FORCE-DAYS is nil, use the value of `planner-carry-tasks-forward'
3219 instead, except t means scan only yesterday."
3220 ;; Special treatment of t for planner-carry-tasks-forward is for
3221 ;; backward compatibility.
3222 (interactive "P")
3223 (unless muse-project-alist
3224 (planner-display-warning
3225 (concat "The variable `muse-project-alist' has not defined.\n"
3226 "\nSee the \"Creating Your Planner\" chapter in the Planner"
3227 " manual\nfor details on how to set this up."))
3228 (error "The variable `muse-project-alist' has not been defined"))
3229 (if planner-use-day-pages
3230 (progn
3231 (unless force-days
3232 (setq force-days
3233 (if (equal planner-carry-tasks-forward t)
3235 planner-carry-tasks-forward)))
3236 (when (and (integerp force-days)
3237 (= force-days 0))
3238 (setq force-days t))
3239 (planner-goto-today)
3240 (let* ((today (planner-today))
3241 (names (planner-get-day-pages nil (planner-yesterday)))
3242 (today-buffer (current-buffer))
3243 (planner-tasks-file-behavior
3244 ;; Force saving so that the file list can be updated
3245 (or planner-tasks-file-behavior
3246 'save))
3247 (planner-use-other-window nil)
3248 (live-buffers (and
3249 (equal planner-tasks-file-behavior
3250 'close)
3251 (buffer-list))))
3252 ;; Limit the list for force-days
3253 (when (and (integerp force-days)
3254 (> (length names) force-days))
3255 (setcdr (nthcdr (1- force-days) names) nil))
3256 (when force-days
3257 (while names
3258 (find-file (cdar names))
3259 ;; Attempt to copy all the tasks
3260 (when (not (equal today (planner-page-name)))
3261 (let ((planner-tasks-file-behavior nil))
3262 (planner-copy-or-move-region (point-min) (point-max)
3263 (planner-today) t))
3264 (unless (buffer-modified-p)
3265 (kill-buffer (current-buffer))))
3266 (setq names (cdr names))))
3267 ;; Jump to the most recent daily page
3268 (if (or planner-carry-tasks-forward
3269 (planner-page-file today)
3270 (null names))
3271 (planner-goto-today)
3272 (planner-goto (caar names)))
3273 ;; Save/kill files if configured to do so
3274 (when planner-tasks-file-behavior
3275 (planner-save-buffers live-buffers))))
3276 (planner-find-file (or planner-default-page
3277 planner-initial-page))))
3279 (defvar planner-goto-hook '(planner-seek-to-first)
3280 "Functions called after a planner day page is opened.")
3282 ;;;###autoload
3283 (defun planner-goto (date &optional just-show)
3284 "Jump to the planning page for DATE.
3285 If no page for DATE exists and JUST-SHOW is non-nil, don't create
3286 a new page - simply return nil."
3287 (interactive (list (or
3288 (planner-read-date)
3289 (planner-read-non-date-page (planner-file-alist)))))
3290 (if (or (not just-show) (planner-page-exists-p date))
3291 (progn
3292 (planner-find-file date
3293 (if planner-use-other-window
3294 'find-file-other-window
3295 'find-file))
3296 (widen)
3297 (goto-char (point-min))
3298 (run-hooks 'planner-goto-hook)
3299 ;; planner-goto-hook returns nil
3301 ;; File not found, and not supposed to be created.
3302 (when (interactive-p)
3303 (message "No planner file for %s." date))
3304 ;; return nil
3305 nil))
3307 ;;;###autoload
3308 (defun planner-goto-plan-page (page)
3309 "Opens PAGE in the the `planner-project' wiki.
3310 Use `planner-goto' if you want fancy calendar completion."
3311 (interactive (list (planner-read-name (planner-file-alist))))
3312 (planner-find-file page))
3314 ;;;###autoload
3315 (defun planner-show (date)
3316 "Show the plan page for DATE in another window, but don't select it.
3317 If no page for DATE exists, return nil."
3318 (interactive (list (planner-read-date)))
3319 (save-selected-window
3320 (let ((planner-use-other-window t))
3321 (planner-goto date planner-show-only-existing))))
3323 ;;;###autoload
3324 (defun planner-goto-today ()
3325 "Jump to the planning page for today."
3326 (interactive)
3327 (planner-goto (planner-today)))
3329 ;;;###autoload
3330 (defun planner-goto-most-recent ()
3331 "Go to the most recent day with planning info."
3332 (interactive)
3333 (planner-goto
3334 (planner-get-previous-existing-day
3335 (planner-calculate-date-from-day-offset
3336 (planner-get-current-date-filename) 1))))
3338 ;;;###autoload
3339 (defun planner-goto-yesterday (&optional days)
3340 "Goto the planner page DAYS before the currently displayed date.
3341 If DAYS is nil, goes to the day immediately before the currently
3342 displayed date. If the current buffer is not a daily planner
3343 page, calculates date based on today."
3344 (interactive "p")
3345 (let ((planner-use-other-window nil))
3346 (planner-goto (planner-calculate-date-from-day-offset
3347 (planner-get-current-date-filename) (or (- days) -1)))))
3349 ;;;###autoload
3350 (defun planner-goto-tomorrow (&optional days)
3351 "Goto the planner page DAYS after the currently displayed date.
3352 If DAYS is nil, goes to the day immediately after the currently
3353 displayed date. If the current buffer is not a daily planner
3354 page, calculates date based on today."
3355 (interactive "p")
3356 (let ((planner-use-other-window nil))
3357 (planner-goto (planner-calculate-date-from-day-offset
3358 (planner-get-current-date-filename) (or days 1)))))
3360 ;;;###autoload
3361 (defun planner-goto-previous-daily-page ()
3362 "Goto the last plan page before the current date.
3363 The current date is taken from the day page in the current
3364 buffer, or today if the current buffer is not a planner page.
3365 Does not create pages if they do not yet exist."
3366 (interactive)
3367 (let ((planner-use-other-window nil))
3368 (planner-goto (planner-get-previous-existing-day
3369 (planner-get-current-date-filename)))))
3371 ;;;###autoload
3372 (defun planner-goto-next-daily-page ()
3373 "Goto the first plan page after the current date.
3374 The current date is taken from the day page in the current
3375 buffer, or today if the current buffer is not a planner page.
3376 Does not create pages if they do not yet exist."
3377 (interactive)
3378 (let ((planner-use-other-window nil))
3379 (planner-goto (planner-get-next-existing-day
3380 (planner-get-current-date-filename)))))
3382 ;;;_ + Tasks
3384 ;;;_ + Creating
3386 ;;;###autoload
3387 (defun planner-create-high-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 "A"))
3394 (call-interactively 'planner-create-task-from-buffer)))
3396 ;;;###autoload
3397 (defun planner-create-medium-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 "B"))
3404 (call-interactively 'planner-create-task-from-buffer)))
3406 ;;;###autoload
3407 (defun planner-create-low-priority-task-from-buffer ()
3408 "Create a high-priority task based on this buffer.
3409 Do not use this in LISP programs. Instead, set the value of
3410 `planner-default-task-priority' and call `planner-create-task' or
3411 `planner-create-task-from-buffer'."
3412 (interactive)
3413 (let ((planner-default-task-priority "C"))
3414 (call-interactively 'planner-create-task-from-buffer)))
3416 (defun planner-read-task ()
3417 "Return a list of information for a task."
3418 (list
3419 (read-string "Describe task: ")
3420 (when planner-use-day-pages
3421 (cond
3422 ;; Universal prefix means pick up from current page
3423 ((and current-prefix-arg
3424 (planner-derived-mode-p 'planner-mode)
3425 (string-match planner-date-regexp (planner-page-name)))
3426 (planner-page-name))
3427 ;; Date selected in calendar
3428 ((condition-case nil (calendar-cursor-to-date) (error nil))
3429 (planner-date-to-filename (calendar-cursor-to-date)))
3430 ;; Prompt for date
3431 (t (let ((planner-expand-name-favor-future-p
3432 (or planner-expand-name-favor-future-p
3433 planner-task-dates-favor-future-p)))
3434 (planner-read-date)))))
3435 (when planner-use-plan-pages
3436 (if (and current-prefix-arg (planner-derived-mode-p 'planner-mode)
3437 (not (string-match planner-date-regexp (planner-page-name))))
3438 ;; Universal prefix means pick up from current page
3439 (planner-page-name)
3440 (let ((planner-default-page
3441 (if (and (planner-derived-mode-p 'planner-mode)
3442 (planner-page-name)
3443 (not (string-match planner-date-regexp
3444 (planner-page-name))))
3445 (planner-page-name)
3446 planner-default-page)))
3447 (planner-read-non-date-page
3448 (planner-file-alist)))))
3449 planner-default-task-status))
3451 ;; NOTE: Prefix arg changed to prompt for PLAN-PAGE instead of
3452 ;; set to today
3453 ;;;###autoload
3454 (defun planner-create-task-from-buffer (title date &optional plan-page status)
3455 "Create a new task named TITLE on DATE based on the current buffer.
3456 With a prefix, do not prompt for PLAN-PAGE. The task is
3457 associated with PLAN-PAGE if non-nil. If STATUS is non-nil, use
3458 that as the status for the task. Otherwise, use
3459 `planner-default-task-status'. See `planner-create-task' for more
3460 information."
3461 (interactive (planner-read-task))
3462 (setq planner-default-page plan-page)
3463 (let ((planner-create-task-hook (append planner-create-task-from-buffer-hook
3464 planner-create-task-hook))
3465 (annotation (run-hook-with-args-until-success
3466 'planner-annotation-functions)))
3467 (when (and planner-annotation-symbol-string
3468 (string-match planner-annotation-symbol-string title))
3469 (setq title (replace-match (or annotation "") t t title)
3470 annotation nil))
3471 (planner-create-task title
3472 (when (and date
3473 (string-match planner-date-regexp date))
3474 date)
3475 annotation
3476 plan-page
3477 status)))
3479 (defun planner-create-task (title date &optional annotation plan-page status)
3480 "Create a new task named TITLE based on the current Wiki page.
3481 If DATE is non-nil, makes a daily entry on DATE, else makes an
3482 entry in today's planner page. It's assumed that the current Wiki
3483 page is the page you're using to plan an activity. Any time
3484 accrued to this task will be applied to that page's name in the
3485 timelog file, assuming you use timeclock. If ANNOTATION is
3486 non-nil, it will be used for the page annotation. If PLAN-PAGE is
3487 non-nil, the task is associated with the given page. If STATUS is
3488 non-nil, use that as the status for the task. Otherwise, use
3489 `planner-default-task-status'.
3491 If called with an interactive prefix argument, do not prompt for
3492 PLAN-PAGE.
3494 You probably want to call `planner-create-task-from-buffer' instead."
3495 (interactive
3496 (list
3497 (read-string "Describe task: ")
3498 (when planner-use-day-pages
3499 (cond
3500 ;; Universal prefix means pick up from current page
3501 ((and current-prefix-arg
3502 (planner-derived-mode-p 'planner-mode)
3503 (string-match planner-date-regexp (planner-page-name)))
3504 (planner-page-name))
3505 ;; Date selected in calendar
3506 ((condition-case nil (calendar-cursor-to-date) (error nil))
3507 (planner-date-to-filename (calendar-cursor-to-date)))
3508 ;; Prompt for date
3509 (t (let ((planner-expand-name-favor-future-p
3510 (or planner-expand-name-favor-future-p
3511 planner-task-dates-favor-future-p)))
3512 (planner-read-date)))))
3513 nil ;; No annotation, interactively
3514 (when planner-use-plan-pages
3515 (if (and current-prefix-arg (planner-derived-mode-p 'planner-mode)
3516 (not (string-match planner-date-regexp (planner-page-name))))
3517 ;; Universal prefix means pick up from current page
3518 (planner-page-name)
3519 (let ((planner-default-page
3520 (if (and (planner-derived-mode-p 'planner-mode)
3521 (planner-page-name)
3522 (not (string-match planner-date-regexp
3523 (planner-page-name))))
3524 (planner-page-name)
3525 planner-default-page)))
3526 (planner-read-non-date-page
3527 (planner-file-alist)))))
3528 planner-default-task-status))
3529 (setq planner-default-page plan-page)
3530 (planner-create-task-from-info
3531 nil ; info
3532 planner-default-task-priority ; priority
3533 "0" ; number
3534 (or status planner-default-task-status) ; status
3535 (if (and annotation
3536 (not (string= annotation ""))
3537 (or (not plan-page)
3538 (and
3539 (not (string= plan-page annotation))
3540 (not (string= (concat "[[" plan-page "]]") annotation)))))
3541 ;; Used C-u to make a plan-page annotation, so preserve
3542 ;; the context
3543 (concat title " : " annotation)
3544 title) ; description
3545 ;; link: If creating this from a planner plan page, use the
3546 ;; current page name
3547 (and plan-page (planner-make-link plan-page)) ; link text
3548 date
3549 plan-page))
3551 (defun planner-create-task-from-note (title date &optional plan-page status)
3552 "Create a task based on the current note with TITLE, DATE, PLAN-PAGE and
3553 STATUS.
3555 A more do-what-I-mean way to do this is to position point on the first
3556 line of a note (.#1 ...) and call `planner-create-task-from-buffer'."
3557 (interactive (let* ((info (planner-current-note-info))
3558 (planner-default-page (and info
3559 (planner-note-plan info))))
3560 (if info
3561 (planner-read-task)
3562 (error "Not in a planner note"))))
3563 (let* ((info (planner-current-note-info))
3564 (annotation (planner-make-link (concat (planner-note-page info)
3566 (planner-note-anchor info)))))
3567 (when (and planner-annotation-symbol-string
3568 (string-match planner-annotation-symbol-string title))
3569 (setq title (replace-match (or annotation "") t t title)
3570 annotation nil))
3571 (planner-create-task title
3572 (when (and date (string-match planner-date-regexp
3573 date))
3574 date)
3575 annotation
3576 plan-page
3577 status)))
3580 ;;;_ + Rescheduling
3582 (defvar planner-copy-or-move-task-suppress-duplicates t
3583 "*If non-nil, do not create duplicate tasks.")
3585 (defun planner-replan-task-basic (page-name)
3586 "Change or assign the plan page for the current task.
3587 PAGE-NAME is the new plan page for the task. Use
3588 `planner-copy-or-move-task' if you want to change the date.
3589 With a prefix, provide the current link text for editing."
3590 (interactive (list
3591 (planner-read-name
3592 (planner-file-alist) nil
3593 (when current-prefix-arg
3594 (planner-task-plan (planner-current-task-info))))))
3595 (let ((info (planner-current-task-info)))
3596 (when (and info (not (equal page-name (planner-task-plan info))))
3597 (with-planner-update-setup
3598 ;; Delete from old plan page
3599 (when (planner-task-plan info)
3600 (planner-find-file (planner-task-plan info))
3601 (when (planner-find-task info)
3602 (delete-region (planner-line-beginning-position)
3603 (1+ (planner-line-end-position)))))
3604 ;; Add to new plan page, if any, and update date
3605 (if page-name
3606 (progn
3607 (planner-find-file page-name)
3608 (planner-seek-task-creation-point)
3609 (insert (planner-format-task info nil nil nil nil
3610 (or (planner-task-date info)
3612 (or (planner-task-date info)
3613 ""))
3614 "\n")
3615 (forward-line -1)
3616 (planner-update-task))
3617 ;; Else, go to day page and update line
3618 (planner-find-file (planner-task-date info))
3619 (if (planner-find-task info)
3620 (delete-region (planner-line-beginning-position)
3621 (1+ (planner-line-end-position)))
3622 (planner-seek-task-creation-point))
3623 (insert (planner-format-task info nil nil nil nil
3624 (or (planner-make-link page-name) "")
3625 (or page-name ""))
3626 "\n"))))))
3627 (defalias 'planner-replan-task 'planner-replan-task-basic)
3629 (defun planner-seek-task-creation-point ()
3630 "Jump to point where task would be created."
3631 (planner-seek-to-first (cdr (assoc 'tasks planner-sections)))
3632 (when planner-add-task-at-end-flag
3633 (while (looking-at "^#")
3634 (forward-line))
3635 (unless (bolp) (insert "\n"))))
3637 (defun planner-copy-or-move-task-basic (&optional date force)
3638 "Move the current task to DATE.
3639 If this is the original task, it copies it instead of moving.
3640 Most of the time, the original should be kept in a planning file,
3641 but this is not required. If FORCE is non-nil, the task is moved
3642 regardless of status. It also works for creating tasks from a
3643 Note. Use `planner-replan-task' if you want to change the plan
3644 page in order to get better completion.
3645 This function is the most complex aspect of planner.el."
3646 (interactive (list (let ((planner-expand-name-favor-future-p
3647 (or planner-expand-name-favor-future-p
3648 planner-task-dates-favor-future-p)))
3649 (planner-read-date))
3650 current-prefix-arg))
3651 (if (or (null date)
3652 (string-match planner-date-regexp date))
3653 (let ((live-buffers (when (equal planner-tasks-file-behavior 'close)
3654 (buffer-list))))
3655 (when (equal date (planner-page-name))
3656 (error "Cannot move a task back to the same day"))
3657 (save-excursion
3658 (save-window-excursion
3659 (save-restriction
3660 (beginning-of-line)
3661 (let* ((task-info (planner-current-task-info))
3662 (plan-page (planner-task-plan task-info))
3663 (date-page (planner-task-date task-info)))
3664 (unless task-info
3665 (error "There is no task on the current line"))
3666 (unless force
3667 (when (equal date-page date)
3668 (error "Cannot move a task back to the same day"))
3669 (when (equal (planner-task-status task-info) "X")
3670 (error "Cannot reschedule a completed task"))
3671 (when (equal (planner-task-status task-info) "C")
3672 (error "Cannot reschedule a cancelled task")))
3673 (when (and (or (null date) (string= date "nil"))
3674 (not plan-page))
3675 (error "Cannot unset date in task not associated with plan"))
3676 ;; Delete it from the old date page
3677 (when date-page
3678 (planner-goto date-page)
3679 (goto-char (point-min))
3680 (when (planner-find-task task-info)
3681 (beginning-of-line)
3682 (delete-region (point)
3683 (min (point-max)
3684 (1+ (planner-line-end-position))))))
3685 ;; Update the new date page
3686 (unless (null date)
3687 (planner-goto date)
3688 (when (or (not planner-copy-or-move-task-suppress-duplicates)
3689 (and (not (planner-find-task task-info))))
3690 (planner-seek-task-creation-point)
3691 (insert
3692 (planner-format-task task-info
3693 nil nil nil nil
3694 (when plan-page
3695 (planner-make-link plan-page)))
3696 "\n")))
3697 ;; Update planner page
3698 (when (and plan-page
3699 (not (string-match planner-date-regexp plan-page)))
3700 (planner-find-file plan-page)
3701 (goto-char (point-min))
3702 (if (planner-find-task task-info)
3703 (progn
3704 (beginning-of-line)
3705 (delete-region (point)
3706 (min (point-max)
3707 (1+ (planner-line-end-position)))))
3708 (planner-seek-task-creation-point))
3709 (insert (planner-format-task
3710 task-info
3711 nil nil nil nil
3712 (planner-make-link date)) "\n"))
3713 t))))
3714 ;; Operation successful.
3715 (when planner-tasks-file-behavior
3716 (planner-save-buffers live-buffers t)))
3717 (when (planner-replan-task date) t)))
3718 (defalias 'planner-copy-or-move-task 'planner-copy-or-move-task-basic)
3720 ;;;_ + Deleting
3722 (defcustom planner-delete-task-hook nil
3723 "Hook called after a task has been deleted.
3724 Argument is a task information list. If a function returns nil,
3725 no other functions will be processed."
3726 :type 'hook
3727 :group 'planner-tasks)
3729 (defun planner-delete-task ()
3730 "Deletes this task from the current page and the linked page."
3731 (interactive)
3732 (save-excursion
3733 (save-window-excursion
3734 (beginning-of-line)
3735 (let* ((task-info (planner-current-task-info))
3736 (task-link (and task-info (planner-task-link task-info)))
3737 (live-buffers
3738 (and (equal planner-tasks-file-behavior 'close)
3739 (buffer-list))))
3740 (unless task-info
3741 (error "There is no task on the current line"))
3742 (beginning-of-line)
3743 (delete-region (point) (min (point-max)
3744 (1+ (planner-line-end-position))))
3745 (when (and task-link (assoc task-link (planner-file-alist))
3746 (planner-jump-to-linked-task task-info))
3747 (delete-region (planner-line-beginning-position)
3748 (min (point-max) (1+ (planner-line-end-position)))))
3749 (when planner-tasks-file-behavior
3750 (planner-save-buffers live-buffers t))
3751 (run-hook-with-args-until-failure
3752 'planner-delete-task-hook task-info)))))
3754 ;;;_ + Updating
3756 (defun planner-edit-task-description-basic (description)
3757 "Change the current task to use DESCRIPTION."
3758 (interactive (list
3759 (let* ((info (planner-current-task-info))
3760 (planner-task-history
3761 (list
3762 (planner-task-description info))))
3763 (unless info (error "No task on current line"))
3764 (read-string "New description: "
3765 (cons (planner-task-description info)
3767 '(planner-task-history . 1)
3768 (planner-task-description info)))))
3769 (let ((point (point)))
3770 (with-planner-update-setup
3771 (let ((info (planner-current-task-info))
3772 (live-buffers (and
3773 (equal planner-tasks-file-behavior 'close)
3774 (buffer-list))))
3775 (delete-region (planner-line-beginning-position)
3776 (planner-line-end-position))
3777 (insert (planner-format-task info
3778 nil nil nil
3779 description))
3780 (when (planner-task-link info)
3781 (if (planner-jump-to-linked-task info)
3782 (progn
3783 (setq info (planner-current-task-info))
3784 (delete-region (planner-line-beginning-position)
3785 (planner-line-end-position))
3786 (insert (planner-format-task info
3787 nil nil nil
3788 description)))
3789 (planner-seek-task-creation-point)
3790 (insert
3791 (planner-format-task info nil nil nil description
3792 (planner-make-link (planner-task-page info)))
3793 "\n")))))
3794 (goto-char (point))))
3795 (defalias 'planner-edit-task-description 'planner-edit-task-description-basic)
3798 (defun planner-update-task-basic ()
3799 "Update the current task's priority and status on the linked page.
3800 Tasks are considered the same if they have the same description.
3801 This function allows you to force a task to be recreated if it
3802 disappeared from the associated page.
3804 Note that the text of the task must not change. If you want to be able
3805 to update the task description, see planner-id.el."
3806 (interactive)
3807 (with-planner-update-setup
3808 (beginning-of-line)
3809 (let* ((task-info (planner-current-task-info))
3810 (task-link (and task-info
3811 (if (string-match planner-date-regexp
3812 (planner-page-name))
3813 (planner-task-plan task-info)
3814 (planner-task-date task-info))))
3815 (original (planner-page-name)))
3816 (unless task-info
3817 (error "There is no task on the current line"))
3818 ;; (unless task-link
3819 ;; (error "There is no link for the current task"))
3820 (if (planner-jump-to-linked-task task-info)
3821 ;; Already there, so update only if changed
3822 (unless (planner-tasks-equal-p task-info
3823 (planner-current-task-info))
3824 (delete-region (planner-line-beginning-position)
3825 (min (point-max) (1+ (planner-line-end-position))))
3826 (insert (planner-format-task task-info nil nil nil nil
3827 (planner-make-link
3828 original)) "\n"))
3829 ;; Not yet there, so add it
3830 (when (planner-local-page-p task-link)
3831 (planner-find-file task-link)
3832 (save-excursion
3833 (save-restriction
3834 (planner-seek-task-creation-point)
3835 (insert
3836 (planner-format-task task-info nil nil nil nil
3837 (planner-make-link original))
3838 "\n"))))))))
3840 (defalias 'planner-update-task 'planner-update-task-basic)
3842 ;;;_ + Prioritizing
3844 ;; This really should be called planner-raise/lower-task-priority, but
3845 ;; for some obscure reason, the original planner.el called the task
3846 ;; numbers priorities and "A/B/C" categories. I'm not really sure if I
3847 ;; can change the name right now. I suppose we eventually should.
3849 (defun planner-set-task-priority (priority)
3850 "Set the priority of the current task.
3851 This changes a low-priority task to a medium-priority task
3852 and a medium-priority task to a high-priority task."
3853 (let ((info (planner-current-task-info)))
3854 (when info
3855 (delete-region (planner-line-beginning-position)
3856 (min (point-max) (1+ (planner-line-end-position))))
3857 (save-excursion
3858 (insert (planner-format-task
3859 info
3860 priority) "\n"))
3861 (when (planner-task-link info)
3862 (planner-update-task)))))
3864 (defun planner-raise-task-priority ()
3865 "Raise the priority of the current task.
3866 This changes a low-priority task to a medium-priority task
3867 and a medium-priority task to a high-priority task."
3868 (interactive)
3869 (let ((info (planner-current-task-info)))
3870 (when info
3871 (delete-region (planner-line-beginning-position)
3872 (min (point-max) (1+ (planner-line-end-position))))
3873 (save-excursion
3874 (insert (planner-format-task
3875 info
3876 (cond
3877 ((string= "A" (planner-task-priority info)) "A")
3878 ((string= "B" (planner-task-priority info)) "A")
3879 ((string= "C" (planner-task-priority info)) "B")
3880 (t "C"))) "\n"))
3881 (when (planner-task-link info)
3882 (planner-update-task)))))
3884 (defun planner-lower-task-priority ()
3885 "Lower the priority of the current task.
3886 This changes a medium-priority task to a low-priority task
3887 and a high-priority task to a low-priority task."
3888 (interactive)
3889 (let ((info (planner-current-task-info)))
3890 (when info
3891 (delete-region (planner-line-beginning-position)
3892 (min (point-max) (1+ (planner-line-end-position))))
3893 (save-excursion
3894 (insert (planner-format-task
3895 info
3896 (cond
3897 ((string= "A" (planner-task-priority info)) "B")
3898 ((string= "B" (planner-task-priority info)) "C")
3899 (t "C"))) "\n"))
3900 (when (planner-task-link info)
3901 (planner-update-task)))))
3903 (defun planner-raise-task (&optional arg)
3904 "Raise the number of the current task by ARG steps. (Default: 1)"
3905 (interactive "p")
3906 (beginning-of-line)
3907 (setq arg (or arg 1)) ; ARG defaults to 1 if not specified
3908 (if (< arg 0) (planner-lower-task (- arg)))
3909 (let* ((current-task (planner-current-task-info))
3910 ;; task-seen will be the last task moved over with the same link
3911 task-seen)
3912 (unless current-task
3913 (error "Not on a task line"))
3914 ;; Store the current line in the kill ring, deleting it
3915 (kill-region (point) (1+ (planner-line-end-position)))
3916 ;; If the previous line is not a task, search for the previous block
3917 (while (> arg 0)
3918 (let ((old-point (point)))
3919 (if (= (forward-line -1) 0)
3920 (if (not (planner-current-task-info))
3921 (if (re-search-backward "^#[ABC][0-9]*[ \t]" nil t)
3922 (beginning-of-line)
3923 (setq arg -1) ;; Stop moving, yank back at current place.
3924 (goto-char old-point)))
3925 (setq arg -1)) ;; Stop moving, yank back at current place.
3926 (when (and (> arg 0)
3927 (string= (planner-task-plan current-task)
3928 (planner-task-plan (planner-current-task-info))))
3929 (setq task-seen (planner-current-task-info))))
3930 (setq arg (1- arg)))
3931 ;; Cursor now at right place
3932 (save-excursion (yank))
3933 ;; Update the linked page, if any
3934 (save-window-excursion
3935 (save-excursion
3936 (save-restriction
3937 (when (and task-seen
3938 (planner-task-link current-task)
3939 (planner-jump-to-linked-task current-task))
3940 (let ((old-task
3941 (buffer-substring
3942 (planner-line-beginning-position)
3943 (planner-line-end-position)))
3944 found)
3945 (save-excursion
3946 (save-restriction
3947 (when (planner-find-task task-seen)
3948 ;; Found the new task, so delete the old task and
3949 ;; insert here
3950 (setq found t)
3951 (insert old-task "\n"))))
3952 (when found
3953 (delete-region
3954 (planner-line-beginning-position)
3955 (1+ (planner-line-end-position)))))))))))
3957 (defun planner-lower-task (&optional arg)
3958 "Lower the number of the current task by ARG steps (default 1)."
3959 (interactive "p")
3960 (beginning-of-line)
3961 (setq arg (or arg 1)) ; ARG defaults to 1 if not specified
3962 (if (< arg 0) (planner-raise-task (- arg)))
3963 (let* ((current-task (planner-current-task-info))
3964 ;; task-seen will be the last task moved over with the same link
3965 task-seen)
3966 (unless current-task
3967 (error "Not on a task line"))
3968 ;; Store the current line in the kill ring, deleting it
3969 (kill-region (point) (1+ (planner-line-end-position)))
3970 ;; If the current line is not a task, search for the next block
3971 (while (> arg 0)
3972 (let ((old-point (point)))
3973 (if (not (planner-current-task-info))
3974 (if (re-search-forward "^#[ABC][0-9]*[ \t]" nil t)
3975 (planner-line-beginning-position)
3976 (setq arg -1) ;; Stop moving, yank back at current place.
3977 (goto-char old-point)))
3978 (when (and (> arg 0)
3979 (string= (planner-task-plan current-task)
3980 (planner-task-plan (planner-current-task-info))))
3981 (setq task-seen (planner-current-task-info))))
3982 (unless (and (> arg 0) (= (forward-line 1) 0))
3983 (setq arg -1))
3984 (setq arg (1- arg)))
3985 ;; Cursor now at right place
3986 (save-excursion (yank))
3987 ;; Update the linked page, if any
3988 (save-window-excursion
3989 (save-excursion
3990 (save-restriction
3991 (when (and task-seen
3992 (planner-task-link current-task)
3993 (planner-jump-to-linked-task current-task))
3994 (let ((old-task
3995 (buffer-substring
3996 (planner-line-beginning-position)
3997 (planner-line-end-position)))
3998 found)
3999 (save-excursion
4000 (save-restriction
4001 (when (planner-find-task task-seen)
4002 ;; Found the new task, so delete the old task and
4003 ;; insert here
4004 (setq found t)
4005 (forward-line 1)
4006 (insert old-task "\n"))))
4007 (when found
4008 (delete-region
4009 (planner-line-beginning-position)
4010 (1+ (planner-line-end-position)))))))))))
4012 ;;;_ + Changing the status
4014 (defvar planner-mark-task-hook nil
4015 "Hook called after a task's status has been changed.
4016 Arguments are OLD-STATUS and NEW-STATUS. Functions should leave
4017 the point on the task. If a function returns nil, no other
4018 functions will be processed.")
4020 (defun planner-mark-task (mark &optional this-only)
4021 "Change task status to MARK.
4022 If THIS-ONLY is non-nil, the linked planner page is not
4023 updated."
4024 (let ((case-fold-search nil)
4025 (info (planner-current-task-info)))
4026 (when info
4027 (with-planner-update-setup
4028 (goto-char (planner-line-beginning-position))
4029 (skip-chars-forward "^ \t" (planner-line-end-position))
4030 (skip-chars-forward " \t" (planner-line-end-position))
4031 (delete-char 1)
4032 (insert mark)
4033 (unless this-only
4034 (planner-update-task))
4035 (run-hook-with-args-until-failure
4036 'planner-mark-task-hook
4037 (planner-task-status info)
4038 mark)))))
4040 (defun planner-task-open ()
4041 "Mark the current task as open."
4042 (interactive)
4043 (planner-mark-task "_"))
4045 (defun planner-task-in-progress ()
4046 "Mark the current task as in progress."
4047 (interactive)
4048 (planner-mark-task "o"))
4050 (defun planner-task-done ()
4051 "Mark the current task as done."
4052 (interactive)
4053 (planner-mark-task "X"))
4055 (defun planner-task-cancelled ()
4056 "Mark the current task as cancelled."
4057 (interactive)
4058 (planner-mark-task "C"))
4060 (defun planner-task-delegated ()
4061 "Mark the current task as delegated."
4062 (interactive)
4063 (planner-mark-task "D"))
4065 (defun planner-task-pending ()
4066 "Mark the current task as pending."
4067 (interactive)
4068 (planner-mark-task "P"))
4070 ;;;_ + Extracting
4072 (defun planner-seek-next-unfinished-task ()
4073 "Move point to the next unfinished task on this page.
4074 Return nil if not found."
4075 (interactive)
4076 (re-search-forward "^#[A-C][0-9]*\\s-+[_oDP]\\s-+" nil t))
4078 (defun planner-list-tasks-with-status (status &optional pages)
4079 "Display all tasks that match the STATUS regular expression on all day pages.
4080 If PAGES is:
4081 t: check all pages
4082 nil: check all day pages
4083 regexp: search all pages whose filenames match the regexp
4084 list of page names: limit to those pages
4085 alist of page/filename: limit to those pages
4086 This could take a long time."
4087 (interactive (list (read-string "Status: ")))
4088 (set-buffer (get-buffer-create "*Planner Tasks*"))
4089 (erase-buffer)
4090 (let (tasks)
4091 (setq tasks (planner-extract-tasks
4092 (cond ((eq pages t)
4093 (planner-file-alist))
4094 ((eq pages nil)
4095 (planner-get-day-pages))
4096 ((not (listp pages))
4097 (let ((regexp pages))
4098 (setq pages nil)
4099 (dolist (page (planner-file-alist))
4100 (when (string-match regexp (cdr page))
4101 (setq pages (cons page pages)))))
4102 pages)
4103 (t pages))
4104 (lambda (item)
4105 (string-match status (planner-task-status item)))))
4106 (while tasks
4107 (insert
4108 (format "[[%s]] %s %s %s\n"
4109 (planner-task-page (car tasks))
4110 (planner-task-priority (car tasks))
4111 (planner-task-status (car tasks))
4112 (planner-task-description (car tasks))))
4113 (setq tasks (cdr tasks))))
4114 (planner-mode)
4115 (setq muse-current-project (muse-project planner-project))
4116 (goto-char (point-min))
4117 (pop-to-buffer (current-buffer)))
4119 (defun planner-list-unfinished-tasks (&optional pages)
4120 "Display all unfinished tasks on PAGES.
4121 The PAGES argument limits the pages to be checked in this manner:
4122 t: check all pages
4123 \"regexp\": search all pages whose filenames match \"regexp\"
4124 list of page names: limit to those pages
4125 alist of page/filenames: limit to those pages
4127 Called interactively, this function will search day pages by
4128 default. You can specify the start and end dates or leave them as
4129 nil to search all days. Calling this function with an interactive
4130 prefix will prompt for a regular expression to limit pages.
4131 Specify \".\" or leave this blank to include all pages."
4132 (interactive (list (if current-prefix-arg
4133 (read-string "Regexp: ")
4134 (let ((planner-expand-name-default "nil"))
4135 (planner-get-day-pages
4136 (planner-read-date "nil by default. Start")
4137 (planner-read-date "nil by default. End")
4138 t)))))
4139 (planner-list-tasks-with-status "[^XC\n]" pages))
4141 ;;;_ + Notes
4143 (defvar planner-search-notes-buffer "*Planner Search*"
4144 "Buffer for search results.")
4146 ;;;###autoload
4147 (defun planner-search-notes-with-body (regexp limit)
4148 "Return a buffer with all the notes returned by the query for REGEXP.
4149 If called with a prefix argument, prompt for LIMIT and search days on
4150 or after LIMIT. Display the body of the notes as well."
4151 (interactive (list (read-string "Regexp: ")
4152 (if current-prefix-arg
4153 (let ((planner-expand-name-favor-future-p nil))
4154 (planner-read-date)))))
4155 (planner-search-notes regexp limit t))
4157 ;;;###autoload
4158 (defun planner-search-notes (regexp limit &optional include-body)
4159 "Return a buffer with all the notes returned by the query for REGEXP.
4160 If called with a prefix argument, prompt for LIMIT and search days on
4161 or after LIMIT. If INCLUDE-BODY is non-nil, return the body as well."
4162 (interactive (list (read-string "Regexp: ")
4163 (if current-prefix-arg
4164 (let ((planner-expand-name-favor-future-p nil))
4165 (planner-read-date)))
4166 nil))
4167 (with-planner
4168 (let* ((case-fold-search t)
4169 (results (planner-search-notes-internal regexp limit include-body)))
4170 (if results
4171 (progn
4172 (set-buffer (get-buffer-create planner-search-notes-buffer))
4173 (setq buffer-read-only nil)
4174 (erase-buffer)
4175 (mapcar
4176 (if include-body
4177 (lambda (item)
4178 (insert "** "
4179 (planner-make-link (elt item 0) (elt item 1)) "\t"
4180 (elt item 3) "\n\n"))
4181 (lambda (item)
4182 (insert (planner-make-link (elt item 0) (elt item 1)) "\t"
4183 (elt item 2) "\n")))
4184 results)
4185 (planner-mode)
4186 (goto-char (point-min))
4187 (pop-to-buffer (current-buffer)))
4188 (message "No results found.")))))
4190 ;;;_ + Calendar
4192 (defun planner-calendar-insinuate ()
4193 "Hook Planner into Calendar.
4195 Adds special planner key bindings to `calendar-mode-map'.
4196 After this function is evaluated, you can use the following
4197 planner-related keybindings in `calendar-mode-map':
4199 n jump to the planner page for the current day.
4200 N display the planner page for the current day."
4201 (interactive)
4202 (require 'calendar)
4203 (add-hook 'calendar-move-hook
4204 (lambda ()
4205 (when planner-calendar-show-planner-files
4206 (planner-calendar-show))))
4207 (define-key calendar-mode-map "n" 'planner-calendar-goto)
4208 (define-key calendar-mode-map "N" 'planner-calendar-show))
4209 (defalias 'planner-insinuate-calendar 'planner-calendar-insinuate)
4211 (defvar planner-calendar-buffer-list nil "List of buffers opened by calendar.")
4213 (defun planner-kill-calendar-files ()
4214 "Remove planner files shown from Calendar."
4215 (interactive)
4216 (while planner-calendar-buffer-list
4217 (when (buffer-live-p (car planner-calendar-buffer-list))
4218 (with-current-buffer (car planner-calendar-buffer-list)
4219 (save-buffer)
4220 (planner-maybe-remove-file)))
4221 (setq planner-calendar-buffer-list (cdr planner-calendar-buffer-list))))
4223 ;;;###autoload
4224 (defun planner-calendar-goto ()
4225 "Goto the plan page corresponding to the calendar date."
4226 (interactive)
4227 (let ((planner-use-other-window t))
4228 (planner-goto (planner-date-to-filename (calendar-cursor-to-date)))))
4230 ;;;###autoload
4231 (defun planner-calendar-show ()
4232 "Show the plan page for the calendar date under point in another window."
4233 (interactive)
4234 (save-selected-window
4235 (let ((planner-use-other-window t)
4236 (date (planner-date-to-filename (calendar-cursor-to-date))))
4237 (if (planner-goto date planner-show-only-existing)
4238 (add-to-list 'planner-calendar-buffer-list (current-buffer))
4239 ;; Return nil or a message if there is no day plan page. planner-goto
4240 ;; is not called interactively, so it doesn't send a message.
4241 (when (interactive-p)
4242 (message "No planner file for %s" date))
4243 ;; return nil
4244 nil))))
4246 (defadvice exit-calendar (after planner activate protect)
4247 "Call `planner-kill-calendar-files'."
4248 (planner-kill-calendar-files))
4250 (defun planner-calendar-select ()
4251 "Return to `planner-read-date' with the date currently selected."
4252 (interactive)
4253 (when (calendar-cursor-to-date)
4254 (setq planner-calendar-selected-date
4255 (planner-date-to-filename (calendar-cursor-to-date)))
4256 (if (active-minibuffer-window) (exit-minibuffer))))
4258 ;;;_* Context-sensitive keybindings
4260 (defun planner-jump-to-link ()
4261 "Jump to the item linked to by the current item."
4262 (interactive)
4263 (cond
4264 ((planner-current-task-info) (planner-jump-to-linked-task))
4265 ((planner-current-note-info) (planner-jump-to-linked-note))))
4267 (defun planner-move-up ()
4268 "Move up.
4269 Task: Raise the number of the current task.
4270 Note: Renumbering does not make sense for notes right now, so go to the
4271 previous note.
4272 Headline: Go to previous headline of the same depth."
4273 (interactive)
4274 (cond
4275 ((planner-current-task-info) (planner-raise-task))
4276 ((planner-current-note-info)
4277 (re-search-backward "^\\.#[0-9]+" nil t))
4278 ((and (goto-char (planner-line-beginning-position))
4279 (looking-at "^\\*+"))
4280 (re-search-backward
4281 (concat "^" (regexp-quote (match-string 0)) "\\s-") nil t))))
4284 (defun planner-move-down ()
4285 "Move down.
4286 Task: Lower the number of the current task.
4287 Note: Renumbering does not make sense for notes right now, so go to the
4288 next note.
4289 Headline: Go to the next headline of the same depth."
4290 (interactive)
4291 (cond
4292 ((planner-current-task-info) (planner-lower-task))
4293 ((planner-current-note-info)
4294 (forward-line 1)
4295 (re-search-forward "^\\.#[0-9]+" nil t))
4296 ((and (goto-char (planner-line-beginning-position))
4297 (looking-at "^\\*+"))
4298 (forward-line 1)
4299 (re-search-forward
4300 (concat "^" (regexp-quote (match-string 0)) "\\s-") nil t))))
4302 ;;;_* Initialization
4304 (setq planner-loaded t)
4306 ;; Insinuate with allout mode
4307 (add-hook 'allout-mode-leaders '(planner-mode . "."))
4309 (provide 'planner)
4311 ;;;_* Local emacs vars.
4313 ;; Local variables:
4314 ;; allout-layout: (* 0 : )
4315 ;; End:
4317 ;;; planner.el ends here