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