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