1 ;;; planner-appt.el --- appointment alerts from planner
4 ;; Copyright (C) 2005 Jim Ottaway <j.ottaway@lse.ac.uk>
5 ;; Copyright (C) 2005 Henrik S. Hansen <hsh@freecode.dk>
6 ;; Parts copyright (C) 2005, 2006 Free Software Foundation, Inc.
7 ;; Parts copyright (C) 2005 Seth Falcon <sethfalcon AT gmail.com>
10 ;; Keywords: hypermedia
13 ;; This file is part of Planner. It is not part of GNU Emacs.
15 ;; Planner is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; Planner is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with Planner; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
31 ;; Please report any bugs that you come across to the authors at the
32 ;; addresses given above.
36 ;; Add "(planner-appt-insinuate)" to your configuration to make
37 ;; Planner work with appt.
39 ;; See the Appointments section of the Planner manual for further
44 ;; * Seth Falcon supplied the idea and the code that is the basis of
45 ;; the forthcoming appointments display functionality.
49 ;; * Correct sorting of task appointments
51 ;; * Consider changing "insinuate" into "install". I don't like the
52 ;; word "insinuate" very much! Or a minor mode perhaps:
53 ;; planner-appt-minor-mode
55 ;; * A lot of the code properly belongs elsewhere: schedule sorting,
56 ;; schedule cyclical entries, calendar marking...
68 (defgroup planner-appt nil
69 "Appointment integration for planner.el."
70 :prefix
"planner-appt-"
73 (defcustom planner-appt-schedule-section
"Schedule"
74 "The name of the section where the schedule is to be found."
78 (defcustom planner-appt-font-lock-appointments-flag t
79 "Non-nil means use font-locking for appointments."
81 :type
'(choice (const :tag
"Use font-locking" t
)
82 (const :tag
"Don't use font-locking" nil
)))
84 (defcustom planner-appt-update-appts-on-save-flag nil
85 "Non-nil means update appointment alerts on saving today's plan."
87 :type
'(choice (const :tag
"Update on save" t
)
88 (const :tag
"Don't update on save" nil
)))
90 (defcustom planner-appt-sort-schedule-on-update-flag nil
91 "Non-nil means sort the schedule when updating appointments."
93 :type
'(choice (const :tag
"Sort on update" t
)
94 (const :tag
"Don't sort on update" nil
)))
97 (defcustom planner-appt-update-hook
'()
98 "Hook run after appointments have been updated."
102 (defcustom planner-appt-schedule-cyclic-behaviour
'today
103 "Determines the behaviour of cyclical schedule insertion.
104 Used after `planner-appt-schedule-cyclic-insinuate' has been called.
105 'today means only add cylical schedule entries for today
106 'future means add cyclical entries for all future day pages visited."
108 :type
'(choice (const :tag
"For today only" today
)
109 (const :tag
"For all future pages." future
)))
111 (defcustom planner-appt-alert-buffer
"*Alerts*"
112 "Name of the buffer for displaying active alerts.
113 Used by `planner-appt-show-alerts'."
118 (defcustom planner-appt-task-use-appointments-section-flag nil
119 "When non-nil, task appointments will be copied to an appoinments section.
120 The section name is supplied by
121 `planner-appt-task-appointments-section'."
125 (defcustom planner-appt-task-appointments-section
"Schedule"
126 "Name of the section where task appointments are copied.
127 The copying is contingent upon
128 `planner-appt-task-use-appointments-section-flag'."
132 (defcustom planner-appt-format-appt-section-line-function
133 #'planner-appt-format-appt-section-line
134 "The function used when formatting an appointment section line.
136 This function should take one argument: an appointment description.
137 The description is in the form used when an appointment alert is
138 signalled: a string with the time of the appointment and some text
139 such as \"12:00 Do something\". Look at the default function
140 `planner-appt-format-appt-section-line' for inspiration if you want to
141 make a different format."
145 (defcustom planner-appt-limit-highlighting-flag t
146 "When non-nil, only highlight appointment times in tasks and the schedule.
147 When nil, all appointment times are highlighted, wherever they may be
152 (defcustom planner-appt-forthcoming-days
7
153 "Number of days to look ahead for appointments."
157 (defcustom planner-appt-forthcoming-appt-section
"Forthcoming Appointments"
158 "Title of the section for forthcoming appointments."
162 (defcustom planner-appt-forthcoming-show-day-names-flag t
163 "When non nil, show day names in forthcoming appointments."
167 (defcustom planner-appt-forthcoming-repeat-date-string
" "
168 "String to insert for repeated dates.
169 When there are multiple appointments for a date, the date is inserted
170 in the first appointment and the others have this string in their date
173 If the string consists of anything other than whitespace, then a link
174 to the day page for the appoinment is created."
178 (defcustom planner-appt-forthcoming-look-at-cyclic-flag nil
179 "When non nil, add cyclic entries to the forthcoming appointments section."
184 ;; Regular Expressions
186 ;; TODO: Should these really be customizable anyway?
188 ;; TODO: Dynamically changing dependent customizations; i.e., if this
189 ;; is changed, all the other time-based regexps should change too [I
190 ;; don't understand customize well enough to do this].
192 (defcustom planner-appt-time-regexp
193 "[0-9]?[0-9]:[0-5][0-9]\\(?:am\\|pm\\)?"
194 "Regular expression matching times."
198 (defcustom planner-appt-task-regexp
199 (concat "[@!][ \t]*\\(" planner-appt-time-regexp
"\\)[ \t]*")
200 "If a task description matches this regexp, it's an appointment.
201 Match group 1 is the time of the appointment.
202 Used with the task-based method. If you use schedules, look at
203 `planner-appt-schedule-appt-regexp'."
207 (defcustom planner-appt-task-nagging-regexp
208 (concat "![ \t]*\\(" planner-appt-time-regexp
"\\)[ \t]*")
209 "If a task description matches this regexp, it's a nagging
210 appointment. Used with the task-based method. If you use schedules,
211 look at `planner-appt-schedule-appt-regexp'."
215 (defcustom planner-appt-schedule-basic-regexp
218 ;; the appointment time (match group 1)
219 planner-appt-time-regexp
221 ;; possibly some space, possibly a |, and any amount of space
223 ;; perhaps another time [the end time] (match group 2)
225 planner-appt-time-regexp
227 ;; possibly some space or some ?' chars, possibly a |, and any
230 ;; the appointment text (match group 3)
232 "Basic regular expression to match a schedule.
233 Match group 1 should yield the start time, match group 2 the stop
234 time, and match group 3 the schedule text."
235 :group
'planner-appt
)
237 ;; NB: The groups are shifted in this regexp.
238 (defcustom planner-appt-schedule-regexp
240 ;; any amount of whitespace possibly followed by @ and any amount
242 "^[ \t]*\\(@?\\)[ \t]*"
243 ;; followed by the basic regexp
244 planner-appt-schedule-basic-regexp
)
245 "Regexp matching schedule entries.
246 Match group 1 should match at most one leading instance of the
247 appointment marker, Match group 2 should yield the start time, match
248 group 3 the stop time, and match group 4 the schedule text."
252 (defcustom planner-appt-schedule-appt-regexp
254 ;; any amount of whitespace followed by @ and any amount of
257 ;; followed by the basic regexp
258 planner-appt-schedule-basic-regexp
)
259 "Regexp matching appointments in the schedule requiring alerts.
260 Used with the schedule-based method. If you use tasks for appointments,
261 look at `planner-appt-task-regexp.'
262 Match group 1 should yield the start time, match group 2 the stop
263 time, and match group 3 the alert text."
268 ;;; Planner Miscellany
270 ;; Could be useful elsewhere in planner?
272 (defun planner-appt-todays-page-p ()
273 "Return t if the current page is today's, otherwise nil."
274 (string= (planner-page-name) (planner-today)))
276 (defun planner-appt-seek-to-end-of-current-section ()
277 "Go to the end of the current section."
279 (or (and (re-search-forward "^\\*[^*\n]" nil t
)
280 (1- (planner-line-beginning-position)))
283 (defvar planner-appt-write-file-hook
284 (if (and (boundp 'write-file-functions
)
285 (not (featurep 'xemacs
)))
286 'write-file-functions
288 "The write file hook to use.")
290 ;;; Planner-Appt Miscellany
292 (defvar planner-appt-debug-buffer
"*planner-appt debug messages*"
293 "The buffer to put debugging messages from planner-appt.")
295 (defvar planner-appt-debug-flag nil
296 "Non-nil means turn on planner-appt debugging.")
298 (defmacro planner-appt-debug
(form &rest body
)
299 "Evaluate FORM if `planner-appt-debug-flag' is non-nil.
300 Optional BODY is evaluated otherwise."
301 `(if planner-appt-debug-flag
305 (defun planner-appt-debug-message (&rest args
)
306 "Insert ARGS into `planner-appt-debug-buffer'.
307 This code runs only if `planner-appt-debug-flag' is non-nil."
310 (get-buffer-create planner-appt-debug-buffer
)
311 (goto-char (point-max))
312 (apply #'insert args
)
315 (defun planner-appt-earlier-than-now-p (time)
316 "Return t if TIME is earlier than the current time.
317 Time formats are those used by the appt appointment system."
319 (let* ((now (decode-time))
320 (cur-hour (nth 2 now
))
321 (cur-min (nth 1 now
))
322 (cur-time (+ (* cur-hour
60) cur-min
)))
323 (> cur-time
(appt-convert-time time
))))
325 ;; Not used in this file, but added for completeness.
326 (defun planner-appt-later-than-now-p (time)
327 "Return t if TIME is later than the current time.
328 Time formats are those used by the appt appointment system."
330 (let* ((now (decode-time))
331 (cur-hour (nth 2 now
))
332 (cur-min (nth 1 now
))
333 (cur-time (+ (* cur-hour
60) cur-min
)))
334 (< cur-time
(appt-convert-time time
))))
337 (defvar --planner-appt-tasks-added-appts
'()
338 "Internal variable: Tracks added task-based appointment alerts.")
340 (defvar --planner-appt-tasks-earlier-appts
'()
342 Tracks appointments ignored because they were too early.")
344 (defun planner-appt-clear-appts (appt-list)
346 (setq appt-time-msg-list
347 (delete (pop appt-list
) appt-time-msg-list
))))
349 (defun planner-appt-format-time-and-description (time description
)
350 "Format TIME [a string] and DESCRIPTION as an appointment."
351 (concat time
" " description
))
354 (if (> emacs-major-version
21)
355 (defun planner-appt-make-appt-element (time text
)
357 (list (appt-convert-time time
))
358 (planner-appt-format-time-and-description time text
)
360 (defun planner-appt-make-appt-element (time text
)
362 (list (appt-convert-time time
))
363 (planner-appt-format-time-and-description time text
)))))
365 (defun planner-appt-remember-appt (time text list
)
366 "Store details of an appointment with TIME and TEXT in LIST.
367 Return the new list."
368 (push (planner-appt-make-appt-element time text
) list
))
370 (defun planner-appt-forget-appt (appt appt-list
)
371 "Remove APPT from APPT-LIST and return the new list.
372 APPT is in the appt format."
373 (delete (car (member appt appt-list
)) appt-list
))
375 (defun planner-appt-add-hook (hook function
&optional append global
)
376 "Add to the value of HOOK the function FUNCTION.
377 This is `add-hook' with local and global switched.
378 FUNCTION is not added if already present.
379 FUNCTION is added (if necessary) at the beginning of the hook list
380 unless the optional argument APPEND is non-nil, in which case
381 FUNCTION is added at the end.
382 The optional fourth argument, GLOBAL, if non-nil, says to modify
383 the hook's global value rather than its local value."
384 (add-hook hook function append
(not global
)))
387 (defun planner-appt-remove-task-id (description)
390 (if (boundp 'planner-id-regexp
)
393 (replace-match "" t t description
)
397 (defun planner-appt-format-description (description)
398 (planner-appt-remove-task-id
399 (planner-remove-links description
)))
402 ;;; Showing Appointments In Various Ways
404 (defvar planner-appt-methods
'()
405 "Methods used for appointment alerts.
406 Internal variable: to set up appointment methods use one of:
407 `planner-appt-use-tasks'
408 `planner-appt-use-schedule'
409 `planner-appt-use-tasks-and-schedule'.")
411 ;; Copying task appts over to an "Appointments" section.
413 (defun planner-appt-format-appt-section-line (desc)
414 "Format DESC as a line for the appointments section."
415 (let* ((td (planner-appt-task-parse-task
416 ;; Trick the function into parsing:
420 (end-time (if (string-match
421 (format "\\s-*\\(%s\\)\\s-*"
422 planner-appt-time-regexp
)
425 (match-string 1 text
)
426 (setq text
(replace-match "" t t text
)))
428 ;; Format in the style of a tabular schedule.
429 (format "%6s | %5s | %s"
430 ;; Using an @ means the time gets fontified for free.
433 (if (string= planner-appt-task-appointments-section
434 planner-appt-schedule-section
)
435 ;; To avoid confusion, add an indication that this
436 ;; item came from a task.
440 (defvar --planner-appt-lines-added-to-section
'()
442 Remembers lines added by `planner-appt-update-appt-section' the last
443 time it was called.")
445 (defun planner-appt-task-schedule-item-p (string)
446 "Return t if STRING is a schedule item derived from a task."
447 (member string --planner-appt-lines-added-to-section
))
448 ;; ;; Look for any property in the string since STRING will usually be
449 ;; ;; derived from a buffer substring which may have been edited.
450 ;; (text-property-any 0 (length string) 'appt-task t string))
452 (defun planner-appt-update-appt-section ()
454 (planner-seek-to-first planner-appt-task-appointments-section
)
455 (let ((bound (make-marker))
457 (copy-sequence --planner-appt-lines-added-to-section
))
460 (planner-appt-seek-to-end-of-current-section)
461 (set-marker bound
(point)))
462 (dolist (appt (append --planner-appt-tasks-added-appts
463 --planner-appt-tasks-earlier-appts
))
464 (setq line
(funcall planner-appt-format-appt-section-line-function
466 (setq lines-to-delete
(delete line lines-to-delete
))
468 (unless (search-forward line bound t
)
470 ;; Remember the line even if it was already there
471 (push line --planner-appt-lines-added-to-section
))
472 ;; Remove lines of deleted tasks
473 (dolist (del-line lines-to-delete
)
474 (setq --planner-appt-lines-added-to-section
475 (delete del-line --planner-appt-lines-added-to-section
))
477 (when (search-forward del-line bound t
)
479 (when (eq (char-after) ?
\n)
481 (set-marker bound nil
))
482 ;; Use schedule sorting with some changes
483 (let ((planner-appt-schedule-section
484 planner-appt-task-appointments-section
)
485 (planner-appt-schedule-regexp
486 (concat "\\(.*?\\)" ; to shift the match groups
487 planner-appt-schedule-basic-regexp
)))
488 (planner-appt-schedule-sort))))
490 (defun planner-appt-update-appt-section-maybe ()
492 ;; The appointment section is only relevant if the task
494 (memq 'tasks planner-appt-methods
)
495 planner-appt-task-use-appointments-section-flag
)
496 (with-planner-update-setup
499 (planner-appt-update-appt-section)))))
501 (defmacro with-planner-appt-update-section-disabled
(&rest body
)
502 `(let ((planner-appt-task-use-appointments-section-flag nil
))
505 (put 'with-planner-appt-update-section-disabled
'lisp-indent-function
0)
506 (put 'with-planner-appt-update-section-disabled
'edebug-form-spec
'(body))
508 ;; Compatibility fix for Xemacs [and for Emacs <21?]
509 (if (fboundp 'fit-window-to-buffer
)
510 (defalias 'planner-fit-window-to-buffer
'fit-window-to-buffer
)
511 (defalias 'planner-fit-window-to-buffer
'shrink-window-if-larger-than-buffer
))
513 ;; Display Current Appointments
514 (defun planner-appt-show-alerts ()
515 "Display a list of currently active alerts in another window."
517 (let ((buf (get-buffer-create planner-appt-alert-buffer
)))
518 (with-current-buffer buf
520 (insert "Current alerts\n==============")
521 (if appt-time-msg-list
522 (dolist (appt appt-time-msg-list
)
523 (insert "\n" (cadr appt
)))
525 (goto-char (point-min)))
526 (planner-fit-window-to-buffer (display-buffer buf
))))
529 ;; Display/Insert Forthcoming Appointments
531 (defvar planner-appt-forthcoming-regexp
533 planner-appt-schedule-appt-regexp
535 planner-live-task-regexp
536 planner-appt-task-regexp
539 (defvar planner-appt-forthcoming-task-regexp
540 (concat planner-live-task-regexp
541 planner-appt-task-regexp
))
543 (defun planner-appt-forthcoming-format-appt-description (time description
)
545 planner-appt-format-appt-section-line-function
546 (planner-appt-format-time-and-description
548 (planner-appt-format-description description
))))
550 (defun planner-appt-forthcoming-task-data (info)
552 (planner-appt-task-parse-task
553 (planner-task-description info
))))
555 (cons (appt-convert-time (nth 1 task-appt
))
556 (planner-appt-forthcoming-format-appt-description
558 (nth 0 task-appt
))))))
560 (defun planner-appt-forthcoming-get-appts (n &optional include-today
)
561 (planner-save-buffers)
563 (pages (planner-get-day-pages
566 (planner-calculate-date-from-day-offset
568 (planner-calculate-date-from-day-offset
569 (planner-today) (if include-today n
(1+ n
)))))
570 cyclic-data cyclic-task-descriptions
571 line task-info task-data
572 date-absolute date time text
)
573 ;; After scanning pages and [conditionally] cyclic entries, each
574 ;; element of appts has:
577 ;; <time in appt format [minutes from midnight]>
578 ;; <date in planner format>
581 ;; The first two elements are used for sorting/merging; they are
582 ;; removed from the returned list.
583 (when (and (featurep 'planner-cyclic
)
584 planner-appt-forthcoming-look-at-cyclic-flag
)
585 ;; Returns (<appts> . <list of planner-cyclic-ly formatted tasks>)
586 (setq cyclic-data
(planner-appt-forthcoming-get-cyclic n
))
587 (setq appts
(car cyclic-data
)
588 cyclic-task-descriptions
(cdr cyclic-data
)))
592 (when (file-exists-p (cdr page
))
593 (setq date
(car page
))
594 (setq date-absolute
(calendar-absolute-from-gregorian
595 (planner-filename-to-calendar-date
597 (insert-file-contents (cdr page
))
598 (goto-char (point-min))
599 (while (re-search-forward planner-appt-forthcoming-regexp nil t
)
600 (setq line
(match-string 0))
601 (if (string-match planner-appt-schedule-appt-regexp line
)
602 (unless (planner-appt-task-schedule-item-p line
)
603 (setq time
(save-match-data
604 (appt-convert-time (match-string 1 line
)))
605 text
(match-string 0 line
)))
606 (setq task-info
(planner-current-task-info))
607 (setq task-data
(planner-appt-forthcoming-task-data task-info
))
609 ;; Check for a cyclic task already added.
610 ;; This is a bit messy, since a task id
611 ;; won't have been added [and there might
612 ;; be other special case that I haven't
616 "\\s-+{{Tasks:[0-9]+}}\\s-*"
617 (planner-task-description task-info
))
620 (planner-task-description task-info
))
621 (planner-task-description task-info
))
622 cyclic-task-descriptions
)))
623 (setq time
(car task-data
)
624 text
(cdr task-data
))))
625 (when (and time text
)
626 ;; Add if it is not there already [there may be a
627 ;; duplicate if this is a schedule item derived from a
629 (add-to-list 'appts
(list date-absolute time date text
))
630 (setq time nil text nil
)))
636 (or (< (car a
) (car b
))
637 (and (= (car a
) (car b
))
638 (< (cadr a
) (cadr b
))))))))))
641 (defun planner-appt-forthcoming-get-cyclic (n)
643 (cyclic-task-descriptions '())
644 date line time text task-info task-data
)
645 (dolist (entry (planner-list-diary-entries
646 planner-cyclic-diary-file
647 (planner-filename-to-calendar-date
648 (planner-calculate-date-from-day-offset
651 (setq date
(planner-date-to-filename (car entry
))
653 (if (string-match planner-appt-schedule-appt-regexp line
)
654 (setq time
(save-match-data
655 (appt-convert-time (match-string 1 line
)))
656 text
(match-string 0 line
))
657 (when (string-match planner-appt-forthcoming-task-regexp line
)
658 (setq task-info
(planner-task-info-from-string date line
))
659 (setq task-data
(planner-appt-forthcoming-task-data task-info
))
661 ;; For duplicate checking: remember the description as
662 ;; it would be transformed by planner-cyclic.
663 (push (format planner-cyclic-task-description-format
664 (planner-task-description task-info
) date
)
665 cyclic-task-descriptions
)
666 (setq time
(car task-data
)
667 text
(cdr task-data
)))))
668 (when (and time text
)
671 (list (calendar-absolute-from-gregorian (car entry
))
673 (setq time nil text nil
)))
674 (cons appts cyclic-task-descriptions
)))
676 ;; Format the data into a big string to make it easy either to put
677 ;; into a display buffer or into the day page.
678 (defun planner-appt-forthcoming-format (appt-data)
679 (let* ((last-date "")
680 (empty-cell-p (string-match
681 (format "\\`[%s]+\\'" muse-regexp-blank
)
682 planner-appt-forthcoming-repeat-date-string
))
683 (calendar-abbrev-length
684 ;; compatibility fix for Gnu Emacs < 22 and XEmacs
685 (if (boundp 'calendar-abbrev-length
)
686 calendar-abbrev-length
688 (day-padding (make-string (1+ calendar-abbrev-length
) ?\
)))
689 (mapconcat #'(lambda (a)
692 (if (string= (car a
) last-date
)
694 (when planner-appt-forthcoming-show-day-names-flag
697 planner-appt-forthcoming-repeat-date-string
700 planner-appt-forthcoming-repeat-date-string
)))
702 (when planner-appt-forthcoming-show-day-names-flag
705 (planner-filename-to-calendar-date (car a
))
707 calendar-abbrev-length
)
709 (planner-make-link (car a
))))
711 ;; Remove @s from times to avoid spurious
713 (muse-replace-regexp-in-string
717 (setq last-date
(car a
))))
721 (defvar planner-appt-forthcoming-display-buffer
722 "*Forthcoming Appointments*"
723 "Buffer to display forthcoming appointments.")
725 (defun planner-appt-forthcoming-display (&optional days
)
727 ;; TODO: I wanted to use (interactive "p"), but that defaults to
728 ;; 1. Is this really the best way of getting nil as the default
729 ;; for a command that takes an optional integer prefix?:
730 (list (cond ((consp current-prefix-arg
)
731 (car current-prefix-arg
))
732 ((integerp current-prefix-arg
)
735 (unless days
(setq days planner-appt-forthcoming-days
))
737 (get-buffer-create planner-appt-forthcoming-display-buffer
)
738 (unless (planner-derived-mode-p 'planner-mode
)
739 (setq muse-current-project
(muse-project planner-project
))
741 (cd (planner-directory)))
742 (delete-region (point-min) (point-max))
743 (insert "* Appointments in the next "
744 (number-to-string days
)
745 (if (= days
1) " day" " days")
747 (planner-appt-forthcoming-format
748 (planner-appt-forthcoming-get-appts
749 (or days planner-appt-forthcoming-days
) t
)))
750 (goto-char (point-min)))
751 (display-buffer planner-appt-forthcoming-display-buffer
)
752 (planner-fit-window-to-buffer
753 (get-buffer-window planner-appt-forthcoming-display-buffer
)))
756 (defun planner-appt-forthcoming-update-section (&optional days
)
758 (list (cond ((consp current-prefix-arg
)
759 (car current-prefix-arg
))
760 ((integerp current-prefix-arg
)
763 (with-planner-update-setup
766 (planner-seek-to-first planner-appt-forthcoming-appt-section
)
767 (delete-region (point)
768 (planner-appt-seek-to-end-of-current-section))
769 (insert (planner-appt-forthcoming-format
770 (planner-appt-forthcoming-get-appts
771 (or days planner-appt-forthcoming-days
)))
775 ;; A function suitable for planner-mode-hook
776 (defun planner-appt-forthcoming-update-section-maybe ()
777 (when (planner-appt-todays-page-p)
778 (planner-appt-forthcoming-update-section)))
781 ;;; Alerts From Task Descriptions
783 ;; Add a bit of [premature] optimization, mainly for updating
784 ;; functions where the same task gets examined often.
785 (defvar --planner-appt-task-cache
(make-hash-table :test
'equal
))
787 (defun planner-appt-task-parse-task (description)
788 "Extract appointment time and text from the DESCRIPTION.
789 Return a list (text time). If the task is not an
790 appointment, time defaults to nil."
791 (or (gethash description --planner-appt-task-cache
)
794 (if (string-match planner-appt-task-regexp description
)
795 (list (substring description
(match-end 0))
796 (substring description
(match-beginning 1) (match-end 1)))
797 (list description nil
))
798 --planner-appt-task-cache
)))
800 (defun planner-appt-task-nagging-p (description)
801 "Return non-nil if task DESCRIPTION is a nagging appointment."
802 (string-match planner-appt-task-nagging-regexp description
))
804 (defun planner-appt-task-member (description time appt-list
)
805 "Return non-nil if DESCRIPTION at TIME in APPT-LIST has been scheduled."
806 (member (planner-appt-make-appt-element time description
) appt-list
))
809 ;; Integration with planner-schedule.el
811 (defvar planner-appt-schedule-task-estimate-regexp
812 (concat "[!@][ \t]*\\(?:" ; "shy" form of planner-appt-task-regexp
813 planner-appt-time-regexp
815 "\\s-*\\([0-9]+[smhdw]\\)")
816 "Regular expression matching a task time estimate.")
818 ;; NB: The following advice could be avoided if the regexp were not
819 ;; hard-coded into the original function.
821 ;; NNB: This is not well tested!
823 (defadvice planner-schedule-task-estimate
(around planner-appt-task disable
)
824 "Modify the regexp matched to take appointments into account."
825 (when (string-match planner-appt-schedule-task-estimate-regexp
826 (planner-task-description info
))
827 (schedule-duration-to-seconds
828 (match-string 2 (planner-task-description info
)))))
831 (defun planner-appt-task-add (&optional info
)
832 "Create an appointment from the current task if this is today's plan.
833 Return t if an appointment was added.
834 If the task is an appointment, it is not cancelled, it is scheduled for
835 later today, and is not already added.
836 Optional argument: use INFO instead of the current task info."
838 (let* ((info (or info
839 ;; Unfortunately, in emacs-lisp there are no
840 ;; defaults for optional arguments, so one can't
841 ;; distinguish between null info in an arg and null
842 ;; info from planner-current-task-info; so the
843 ;; error message might be uninformative here.
844 (planner-current-task-info)
845 (error "There is no task on the current line")))
846 (appt (planner-appt-task-parse-task
847 (planner-task-description info
)))
849 (desc (and time
(planner-appt-format-description (nth 0 appt
)))))
851 (not (string= (planner-task-status info
) "C"))
852 (string= (planner-task-date info
)
853 (planner-date-to-filename
854 (decode-time (current-time))))
855 (not (planner-appt-task-member desc time
856 appt-time-msg-list
)))
857 (if (planner-appt-earlier-than-now-p time
)
859 ;; Remember earlier appts separately [principally for
860 ;; their addition in an appointment section].
861 (unless (planner-appt-task-member
862 desc time --planner-appt-tasks-earlier-appts
)
863 (setq --planner-appt-tasks-earlier-appts
864 (planner-appt-remember-appt
866 --planner-appt-tasks-earlier-appts
)))
867 (planner-appt-update-appt-section-maybe)
868 ;; Make sure nil is returned.
871 ;; Keep track of tasks added by this function.
872 (setq --planner-appt-tasks-added-appts
873 (planner-appt-remember-appt
875 --planner-appt-tasks-added-appts
))
876 (planner-appt-update-appt-section-maybe)
879 (defun planner-appt-task-delete (&optional info
)
880 "Delete the appointment from the current task if this is today's plan.
881 Do not remove the time string.
882 Return any deleted appointments.
883 Optional argument: use INFO instead of the current task info."
885 (let* ((info (or info
886 ;; See planner-appt-task-add for comments about
887 ;; the possibly uninformative error message.
888 (planner-current-task-info)
889 (error "There is no task on the current line")))
890 (appt (planner-appt-task-parse-task
891 (planner-task-description info
)))
893 (desc (and time
(planner-appt-format-description (nth 0 appt
))))
894 (tmp-msg-list appt-time-msg-list
))
896 ;; Method from appt-delete
897 (let ((deleted-appts '())
899 (car (planner-appt-task-member
900 desc time --planner-appt-tasks-earlier-appts
)))
902 ;; NB: Mustn't concat time onto description until earlier-appt
903 ;; has been determined [since planner-appt-task-member does
904 ;; the concat itself [this could be improved somehow]]
905 (setq desc
(concat time
" " desc
))
907 (setq element
(car tmp-msg-list
))
908 (when (string= (car (cdr element
)) desc
)
909 (push element deleted-appts
)
910 (setq appt-time-msg-list
(delq element appt-time-msg-list
))
911 (setq --planner-appt-tasks-added-appts
912 (planner-appt-forget-appt
913 element --planner-appt-tasks-added-appts
)))
914 (setq tmp-msg-list
(cdr tmp-msg-list
)))
915 (when (or deleted-appts earlier-appt
)
916 ;; Forget a deleted appt that was earlier than now.
918 (setq --planner-appt-tasks-earlier-appts
919 (planner-appt-forget-appt
920 earlier-appt --planner-appt-tasks-earlier-appts
)))
921 (planner-appt-update-appt-section-maybe))
924 (defun planner-appt-add-appts-from-tasks ()
925 "Parse all of today's tasks and add appointments automatically."
927 (when (planner-appt-todays-page-p)
928 ;; Clear old appts added by this function.
929 (planner-appt-clear-appts --planner-appt-tasks-added-appts
)
930 (setq --planner-appt-tasks-added-appts
'()
931 --planner-appt-tasks-earlier-appts
'())
932 (let ((case-fold-search nil
))
934 (goto-char (point-min))
935 (with-planner-appt-update-section-disabled
936 (while (re-search-forward
937 (concat planner-live-task-regexp
938 planner-appt-task-regexp
)
940 (planner-appt-task-add))))
941 (when (or --planner-appt-tasks-added-appts
942 --planner-appt-tasks-earlier-appts
)
943 (planner-appt-update-appt-section-maybe)))))
947 ;; for speedy enabling and disabling of advice:
949 (defvar --planner-appt-advice
'()
950 "Internal variable: List of advices added by `planner-appt-defadvice'.
951 Each element is a list of args for `ad-enable-advice' and
952 `ad-disable-advice'.")
955 (defvar planner-appt-advice-common-flags
956 '(preactivate disable
)
957 "Advice flags common to all planner-appt advice."))
959 (defmacro planner-appt-defadvice
(function args doc
&rest body
)
960 "Advise FUNCTION with ARGS, DOC and BODY.
961 Remembers the advice function and args in `--planner-appt-advice'."
964 (,@args
,@planner-appt-advice-common-flags
) ,doc
,@body
)
965 (let ((info '(,function
,(car args
) ,(cadr args
))))
966 (unless (member info --planner-appt-advice
)
967 (push info --planner-appt-advice
)))))
969 (put 'planner-appt-defadvice
972 (name name
&rest sexp
)
975 ("interactive" interactive
)]
978 (put 'planner-appt-defadvice
'lisp-indent-function
'defun
)
980 ;; See what happened with the preactivation.
984 (trace-function-background
985 'ad-cache-id-verification-code
986 "*planner-appt advice trace*")))
988 (defun planner-appt-disable-all-advice ()
989 "Disable all advice added with `planner-appt-defadvice'."
990 (mapcar #'(lambda (args)
991 (apply #'ad-disable-advice args
)
992 (ad-activate (car args
)))
993 --planner-appt-advice
))
995 (defun planner-appt-enable-all-advice ()
996 "Enable all advice added with `planner-appt-defadvice'."
997 (mapcar #'(lambda (args)
998 (apply #'ad-enable-advice args
)
999 (ad-activate (car args
)))
1000 --planner-appt-advice
))
1003 (defmacro with-planner-appt-task-advice-disabled
(&rest body
)
1004 "Evaluate BODY forms with all advice matching \"planner-appt-task\" disabled."
1007 (planner-appt-disable-all-advice)
1008 (planner-appt-debug-message "all advice disabled")
1010 (planner-appt-enable-all-advice)
1011 (planner-appt-debug-message "all advice enabled")))
1013 (put 'with-planner-appt-task-advice-disabled
'lisp-indent-function
0)
1014 (put 'with-planner-appt-task-advice-disabled
'edebug-form-spec
'(body))
1016 (planner-appt-defadvice planner-task-cancelled
1017 (before planner-appt-task
)
1018 "Delete the appointment as well."
1019 (planner-appt-debug-message
1020 "*** called advice on planner-task-cancelled")
1021 (planner-appt-task-delete))
1023 (planner-appt-defadvice planner-task-done
1024 (before planner-appt-task
)
1025 "Delete the appointment as well."
1026 (planner-appt-debug-message
1027 "*** called advice on planner-task-done")
1028 (planner-appt-task-delete))
1030 (planner-appt-defadvice planner-delete-task
1031 (before planner-appt-task
)
1032 "Delete the appointment as well."
1033 (planner-appt-debug-message
1034 "*** called advice on planner-delete-task")
1035 (planner-appt-task-delete))
1037 ;; The advice for planner-update-task is quite tricky. A task will
1038 ;; need updating [for appointments] if the task is dated today and the
1039 ;; description differs from other task lines linked to by the current
1040 ;; task. If this is true, we have to examine all the other links,
1041 ;; delete any appointments, and then add the task after planner-update
1042 ;; has been called. Note that it is only possible for this to happen
1043 ;; if planner-id is loaded since otherwise the "same" task line can't
1044 ;; have different descriptions.
1046 (defun planner-appt-get-diff-links (info)
1047 "Given INFO, return a list of tasks linked to it whose info differs."
1050 ;; Todo: with-planner-update-setup really ought to return the
1051 ;; value of the body.
1052 (with-planner-update-setup
1053 ;; Preserve point as well.
1056 (if (featurep 'planner-multi
)
1057 (planner-multi-link-delete
1058 (planner-task-page info
)
1059 (planner-multi-task-link-as-list info
))
1060 (list (planner-task-link info
))))
1061 (when (and (planner-find-file (planner-link-base link
))
1062 (planner-find-task info
)
1063 (setq linked-info
(planner-current-task-info))
1064 (not (planner-tasks-equal-p info linked-info
)))
1065 (push linked-info diffs
)))))
1068 (planner-appt-defadvice planner-update-task
1069 (around planner-appt-task
)
1070 "Update the appointment as well."
1071 (planner-appt-debug-message
1072 "*** called advice on planner-update-task")
1073 (let* ((info (planner-current-task-info))
1075 (and (featurep 'planner-id
)
1076 (string= (planner-task-date info
) (planner-today))
1077 (planner-appt-get-diff-links info
))))
1078 (with-planner-appt-task-advice-disabled ad-do-it
)
1080 (dolist (i diff-linked
)
1081 (planner-appt-task-delete i
))
1082 (planner-appt-task-add))))
1084 ;; For planner-id-update-tasks-on-page, it is actually much faster to
1085 ;; update today's page after it has done its work rather than using
1086 ;; the update advice above.
1088 (planner-appt-defadvice planner-id-update-tasks-on-page
1089 (around planner-appt-task
)
1090 "Update today's appointments as well."
1091 (planner-appt-debug-message
1092 "*** called advice on planner-id-update-tasks-on-page")
1093 (with-planner-appt-task-advice-disabled ad-do-it
)
1094 (with-planner-update-setup
1096 (planner-goto-today)
1097 ;; Update the appointments section afterwards for efficiency.
1098 (with-planner-appt-update-section-disabled
1099 (planner-appt-update))
1100 (planner-appt-update-appt-section-maybe))))
1102 (defvar --planner-appt-planning nil
1104 Lets planner-appt advice know that it has been called within a call to
1107 (planner-appt-defadvice plan
(around planner-appt-task
)
1108 "Note that plan is in progress."
1109 (planner-appt-debug-message
1110 "*** called advice on plan")
1111 (let ((--planner-appt-planning t
))
1114 (planner-appt-defadvice planner-copy-or-move-task
1115 (around planner-appt-task
)
1116 "Update the appointment as well."
1117 (planner-appt-debug-message
1118 "*** called advice on planner-copy-or-move-task; "
1119 (if --planner-appt-planning
1122 (cond ((not --planner-appt-planning
)
1124 ;; Save the appt information for error cleanup and
1125 ;; appointment adding.
1126 ((deleted-appts (planner-appt-task-delete))
1127 (old-info (planner-current-task-info)))
1130 (with-planner-appt-task-advice-disabled ad-do-it
)
1131 ;; If the task was moved to today, add it.
1132 (when (and date
; Bound in the advised function.
1133 (string= date
(planner-today)))
1134 ;; Fiddle the old info so it looks like
1135 ;; today's. NB: would need changing should
1136 ;; the task-info format ever change.
1138 ;; planner-multi uses the date in the link for
1139 ;; planner-task-date [why?], so that has to be
1140 ;; modified too. This has to be done before the
1141 ;; date element is changed, of course.
1142 (when (and (featurep 'planner-multi
)
1143 (consp (nth 5 old-info
))) ; the link element
1145 (member (nth 8 old-info
)
1147 ;; Silly way of avoiding an error if the
1148 ;; date is not in the list: can the date
1149 ;; not be in the list?
1152 ;; Set the date element.
1153 (setcar (nthcdr 8 old-info
) date
)
1154 (planner-appt-task-add old-info
)))
1156 ;; Catch errors in planner-copy-or-move-task: restore
1158 (dolist (d deleted-appts
)
1159 (push d appt-time-msg-list
))
1160 (error (error-message-string err
))))))
1162 ;; `plan' in progress: only move the task if it is not a
1163 ;; regular (non-nagging) appointment. If it's a nagging
1164 ;; appointment, remove the appointment and then move the
1166 (let* ((info (planner-current-task-info))
1167 (appt (planner-appt-task-parse-task
1168 (planner-task-description info
)))
1169 (time (nth 1 appt
)))
1170 (with-planner-appt-task-advice-disabled
1171 (if (planner-appt-task-nagging-p (planner-task-description info
))
1172 (progn (planner-edit-task-description (nth 0 appt
))
1175 ;; Fri Nov 25 13:59:02 GMT 2005: Fix for new [?]
1176 ;; behaviour of planner-copy-or-move-region.
1179 ;; Return nil, otherwise
1180 ;; planner-copy-or-move-region's count will be
1182 (setq ad-return-value nil
))
1185 ;; NB: this advice leads to two updates of the task appointment
1186 ;; section [when updating it is enabled, of course]: it is hard to see
1187 ;; how to avoid this unless there is yet another global variable
1188 ;; tracking deleted appts.
1189 (planner-appt-defadvice planner-edit-task-description
1190 (around planner-appt-task
)
1191 "Update the appointment as well."
1192 (planner-appt-debug-message
1193 "*** called advice on planner-edit-task-description")
1194 (planner-appt-task-delete)
1195 (with-planner-appt-task-advice-disabled ad-do-it
)
1196 (planner-appt-task-add))
1198 ;; planner-create-task-from-info: The appointment adding needs doing
1199 ;; after all hooks to planner-create-task-from-info have been run so
1200 ;; that planner-appt-task-add has the correct task line; planner-id,
1201 ;; for example, adds a task id so if the planner-appt hook is run
1202 ;; first it won't have the right task description.
1204 ;; Hence these shenanigans:
1206 (defvar --planner-appt-created-task-marker
(make-marker))
1207 (defvar --planner-appt-close-the-buffer-flag nil
)
1210 ;; Hide warning about live-buffers [q.v.].
1211 (defvar live-buffers
))
1213 (defun planner-appt-create-task-hook-func ()
1214 "One half of planner-appt create task handling.
1215 Remembers the position of the added task. The other half of the
1216 handling is in advice to `planner-create-task-from-info'."
1217 (set-marker --planner-appt-created-task-marker
(point))
1218 ;; If planner-tasks-file-behavior is 'close, it won't be possible to
1219 ;; recover the position from a marker, so temporarily defeat closing
1220 ;; this file, and close it if necessary in the
1221 ;; planner-create-task-from-info advice.
1222 (setq --planner-appt-close-the-buffer-flag
1223 (if (and (eq planner-tasks-file-behavior
'close
)
1224 ;; `live-buffers' is defined in
1225 ;; planner-create-task-from-info, but it is not
1226 ;; defined in the planner-multi advice [hmm...].
1227 (boundp 'live-buffers
)
1228 (not (memq (current-buffer) live-buffers
)))
1229 ;; Adding the buffer to `live-buffers' means it won't be
1230 ;; closed automatically.
1231 (progn (push (current-buffer) live-buffers
) t
)
1235 (planner-appt-defadvice planner-create-task-from-info
1236 (around planner-appt-task
)
1237 "Add an appointment alert for the new task if necessary."
1238 (planner-appt-debug-message
1239 "*** called advice on planner-create-task-from-info")
1240 (with-planner-appt-task-advice-disabled ad-do-it
)
1241 (let ((buf (marker-buffer --planner-appt-created-task-marker
))
1242 (pos (marker-position --planner-appt-created-task-marker
)))
1244 (with-current-buffer buf
1247 (planner-appt-task-add)))
1248 (set-marker --planner-appt-created-task-marker nil
)
1249 (when --planner-appt-close-the-buffer-flag
1250 ;; Use planner-save-buffers for consistency; remove the buffer
1251 ;; from buffer-list so that it gets closed.
1252 (planner-save-buffers (delq buf
(buffer-list)))))))
1254 (defun planner-appt-task-insinuate ()
1255 "Do task-specific insinuation into `planner-mode'."
1256 ;; Nothing at the moment!
1259 ;; In case of breakage while developing
1260 ;; NB: remember to remove hooks locally where relevant
1261 (defun planner-appt-task-de-insinuate ()
1262 "Remove task-specific hooks."
1263 ;; Nothing at the moment!
1267 ;;; Alerts From The Schedule
1269 (defvar --planner-appt-schedule-added-appts
'()
1270 "Internal variable: Tracks added schedule-based appointment alerts.")
1272 (defun planner-appt-add-appts-from-schedule ()
1273 "Add appointment reminders from the schedule if this is today's plan."
1275 (when (planner-appt-todays-page-p)
1276 ;; Delete old appts created by this function.
1277 (planner-appt-clear-appts --planner-appt-schedule-added-appts
)
1278 (setq --planner-appt-schedule-added-appts
'())
1280 (planner-seek-to-first planner-appt-schedule-section
)
1281 (let ((bound (save-excursion
1282 (planner-appt-seek-to-end-of-current-section)))
1283 line time text task-item-p
)
1284 ;; There are no entries in the schedule unless bound is
1285 ;; greater than point.
1286 (when (> bound
(point))
1287 (while (re-search-forward
1288 planner-appt-schedule-appt-regexp bound t
)
1289 (setq line
(planner-match-string-no-properties 0)
1290 time
(planner-match-string-no-properties 1)
1291 text
(planner-appt-format-description
1292 (planner-match-string-no-properties 3))
1294 (planner-appt-task-schedule-item-p (match-string 0)))
1295 ;; (text-property-any (match-beginning 0) (match-end 0)
1297 (unless (or task-item-p
1298 (planner-appt-earlier-than-now-p time
))
1299 (appt-add time text
)
1300 ;; Remember tasks added here.
1301 (setq --planner-appt-schedule-added-appts
1302 (planner-appt-remember-appt
1303 time text --planner-appt-schedule-added-appts
)))))))))
1305 (defun planner-appt-schedule-insinuate ()
1306 "Do schedule specific insinuation into `planner-mode'."
1307 ;; Nothing at the moment!
1310 (defun planner-appt-schedule-de-insinuate ()
1311 "Remove schedule-based hooks."
1312 ;; Nothing at the moment!
1315 ;; Make appt-make-list behave.
1317 (defadvice appt-make-list
(after planner-appt activate
)
1318 "Restore appointments added by planner-appt."
1319 (dolist (appt (append --planner-appt-tasks-added-appts
1320 --planner-appt-schedule-added-appts
))
1321 (unless (member appt appt-time-msg-list
)
1322 (push appt appt-time-msg-list
)))
1323 (setq appt-time-msg-list
(appt-sort-list appt-time-msg-list
)))
1325 ;;; Sorting the Schedule
1327 (defun planner-appt-schedule-sort ()
1328 "Sort the schedule in the current page."
1332 (planner-seek-to-first planner-appt-schedule-section
)
1333 (narrow-to-region (point)
1335 (planner-appt-seek-to-end-of-current-section)
1337 (sort-subr nil
'forward-line
'end-of-line
1339 (goto-char (planner-line-beginning-position))
1340 (if (looking-at planner-appt-schedule-regexp
)
1343 ;; 1+ max number of minutes from midnight
1347 ;;; Cyclical Schedule Entries
1349 (require 'diary-lib
)
1352 (defvar planner-cyclic-diary-file
))
1354 ;; Purloined from planner-cyclic.el.
1355 (defun planner-appt-schedule-get-cyclic-tasks (date &optional no-of-days
)
1356 "For DATE, get the cyclic tasks.
1357 Optional argument get tasks for NO-OF-DAYS from DATE, the default is 1
1358 day [i.e., only for DATE]."
1359 (let ((date (if (stringp date
)
1360 (planner-filename-to-calendar-date date
)
1363 (mapcar #'(lambda (item)
1364 (when (string-match planner-appt-schedule-regexp
1366 (match-string 0 (elt item
1))))
1367 (planner-list-diary-entries planner-cyclic-diary-file
1368 date no-of-days
)))))
1370 (defun planner-appt-schedule-add-cyclic ()
1371 "Add cylical tasks to the schedule if the current buffer is a day page."
1372 (when (string-match planner-date-regexp
(planner-page-name))
1374 (planner-appt-schedule-get-cyclic-tasks (planner-page-name))))
1376 (planner-seek-to-first planner-appt-schedule-section
)
1377 (let ((start (point)))
1378 (dolist (entry entries
)
1379 ;; Only insert if the entry is not already there.
1380 (unless (save-excursion
1382 (search-forward entry nil t
))
1383 (insert entry ?
\n))))
1384 ;; Lazy way of putting them in the right place.
1385 (when planner-appt-sort-schedule-on-update-flag
1386 (planner-appt-schedule-sort))))))
1388 (defun planner-appt-schedule-add-cyclic-maybe ()
1389 "Add cylical tasks to the schedule.
1390 Behaviour depends upon `planner-appt-schedule-cyclic-behaviour'."
1391 (when (and (not (string< (planner-page-name) (planner-today)))
1392 (or (eq planner-appt-schedule-cyclic-behaviour
'future
)
1394 (eq planner-appt-schedule-cyclic-behaviour
'today
)
1395 (planner-appt-todays-page-p))))
1396 (planner-appt-schedule-add-cyclic)))
1398 (defun planner-appt-schedule-cyclic-insinuate ()
1399 "Insinuate the adding of cyclical schedule entries."
1400 ;; TODO: Add locally?
1401 (planner-appt-add-hook 'planner-mode-hook
1402 'planner-appt-schedule-add-cyclic-maybe nil t
))
1404 (defun planner-appt-schedule-cyclic-de-insinuate ()
1405 "Remove cyclic schedule adding functionality."
1406 (remove-hook 'planner-mode-hook
1407 'planner-appt-schedule-add-cyclic-maybe
))
1410 ;;; Common Functions
1413 ;; So that one doesn't have to use two separate commands when using
1416 (defvar --planner-appt-updated nil
)
1419 (defun planner-appt-update ()
1420 "Update the appointments on the current page."
1422 ;; NB: Task adding has to be done before the schedule to avoid
1423 ;; duplicates if task appointments are copied to the schedule.
1424 (when (memq 'tasks planner-appt-methods
)
1425 (planner-appt-add-appts-from-tasks))
1426 (when (memq 'schedule planner-appt-methods
)
1427 (planner-appt-add-appts-from-schedule))
1428 (when (and planner-appt-sort-schedule-on-update-flag
1429 (planner-appt-todays-page-p))
1430 (planner-appt-schedule-sort))
1431 (run-hooks 'planner-appt-update-hook
)
1432 ;; TODO: Use a belt and some braces: see comments in
1433 ;; `planner-appt-insinuate-if-today'.
1434 (setq --planner-appt-updated t
))
1436 (defun planner-appt-update-for-write ()
1437 (when planner-appt-update-appts-on-save-flag
1438 (planner-appt-update)
1439 ;; Return nil for local-write-file-hooks.
1442 ;; NB: Something like the above could be done for other hooks as
1446 ;;; General Insinuation
1448 ;; This indirect method is used rather than some variable so that the
1449 ;; function advice can be avoided if it is not necessary [when using
1450 ;; schedule appointments exclusively].
1453 (defun planner-appt-use (source)
1454 "Use SOURCE to derive appointments from plan pages.
1455 Possible values for SOURCE are:
1456 'tasks [use today's task descriptions]
1457 'schedule [use today's schedule]
1458 '(tasks schedule) [use both tasks and schedule]."
1459 (dolist (s (if (listp source
)
1462 (cond ((eq s
'tasks
)
1463 ;; Add task-specific non-insinuating code here.
1464 ;; ;; Only activate advice when necessary.
1465 ;; (ad-enable-regexp "planner-appt-task")
1466 ;; (ad-activate-regexp "planner-appt-task")
1467 ;; An outstanding global hook [planner-appt-task-add tests
1468 ;; for today-ness anyway].
1469 (planner-appt-add-hook 'planner-create-task-hook
1470 'planner-appt-create-task-hook-func nil t
)
1471 (add-to-list 'planner-appt-methods s
))
1473 ;; Add schedule task-specific non-insinuating code here.
1474 (add-to-list 'planner-appt-methods s
))
1476 (error "Invalid appointment source %s" s
))))
1477 ;; Add here any non method-specific code that should be executed
1478 ;; even if planner-appt-insinuate is not called.
1479 (if (memq 'tasks planner-appt-methods
)
1480 (planner-appt-enable-all-advice)
1481 (planner-appt-disable-all-advice))
1482 (planner-appt-add-hook 'planner-mode-hook
1483 'planner-appt-font-setup nil t
)
1484 ;; Might as well return something interesting.
1485 planner-appt-methods
)
1488 (defun planner-appt-insinuate-if-today ()
1489 (when (planner-appt-todays-page-p)
1490 (planner-appt-add-hook 'planner-mode-hook
1491 'planner-appt-update t
)
1492 ;; Add method specific things
1493 (when (memq 'tasks planner-appt-methods
)
1494 (planner-appt-task-insinuate))
1495 (when (memq 'schedule planner-appt-methods
)
1496 (planner-appt-schedule-insinuate))
1497 (planner-appt-add-hook planner-appt-write-file-hook
1498 'planner-appt-update-for-write t
)
1499 ;; TODO: Under some conditions, as yet undetermined, an update is
1500 ;; not done when `plan' is called for the first time. So do an
1501 ;; update, but only if there hasn't been one already.
1503 ;; Fri 15 Apr 2005 16:26:04 BST: this has probably been sorted out
1504 ;; now, but it doesn't do any harm to leave this in for now, just
1506 (unless --planner-appt-updated
1507 (planner-appt-update))))
1510 (defun planner-appt-insinuate ()
1511 "Insinuate appointment alerting into planner mode.
1512 Appointment methods should be set up first using one of:
1513 `planner-appt-use-tasks'
1514 `planner-appt-use-schedule'
1515 `planner-appt-use-tasks-and-schedule'."
1516 (unless planner-appt-methods
1518 "No appointment source methods. Use one of:
1519 `planner-appt-use-tasks'
1520 `planner-appt-use-schedule'
1521 `planner-appt-use-tasks-and-schedule'
1522 before you call this function"))
1523 ;; Initialize the appt system according to emacs type and version.
1524 (cond ((fboundp 'appt-activate
) ; Gnu Emacs >= 22
1526 ((fboundp 'appt-initialize
) ; Xemacs
1527 ;; appt-initialize needs a diary file, so create one if it
1528 ;; doesn't already exist
1529 (unless (file-exists-p diary-file
)
1532 (write-region (point-min) (point-max) diary-file
)))
1535 (t (display-time) ; Gnu Emacs < 22
1536 (add-hook 'diary-hook
'appt-make-list
)))
1537 (planner-appt-add-hook 'planner-mode-hook
1538 'planner-appt-insinuate-if-today t t
))
1540 ;; Remove hooks if there is something not working during testing.
1541 (defun planner-appt-de-insinuate ()
1542 "Remove all hooks associated with planner-appt.
1543 Use in an emergency if breakage in planner-appt interferes with your planning."
1545 (remove-hook 'planner-mode-hook
'planner-appt-insinuate-if-today
)
1546 (remove-hook 'mark-diary-entries-hook
'planner-appt-mark-calendar-maybe
)
1547 (when (get-buffer (planner-today))
1548 (with-current-buffer (get-buffer (planner-today))
1549 ;; NB: Remember to remove locally where appropriate.
1550 (remove-hook 'planner-goto-hook
1551 'planner-appt-update t
)
1552 (remove-hook 'planner-mode-hook
1553 'planner-appt-update t
)
1554 (remove-hook planner-appt-write-file-hook
1555 'planner-appt-update-for-write t
)
1556 (remove-hook 'planner-create-task-hook
1557 'planner-appt-create-task-hook-func
)
1558 (planner-appt-task-de-insinuate)
1559 (planner-appt-schedule-de-insinuate)
1560 (planner-appt-schedule-cyclic-de-insinuate)))
1561 (planner-appt-disable-all-advice)
1562 (message "Planner-appt de-insinuated."))
1565 ;;; Convenient Functions For Users' Configuration.
1568 (defun planner-appt-use-tasks ()
1569 "Use tasks to derive appointment alerts."
1570 (planner-appt-use 'tasks
))
1573 (defun planner-appt-use-schedule ()
1574 "Use the schedule to derive appointment alerts."
1575 (planner-appt-use 'schedule
))
1578 (defun planner-appt-use-tasks-and-schedule ()
1579 "Use both tasks and the schedule to derive appointment alerts."
1580 (planner-appt-use '(tasks schedule
)))
1582 ;;; Font Highlighting
1584 (defface planner-appt-face
'((t (:foreground
"green")))
1585 "Face for scheduled time."
1586 :group
'planner-appt
)
1588 (defface planner-appt-overdue-face
'((t (:foreground
"red")))
1589 "Face for scheduled, but overdue time."
1590 :group
'planner-appt
)
1592 (defun planner-appt-task-highlight-face (time)
1593 "Return appropriate face, depending on TIME.
1594 If TIME is earlier than now, return `planner-appt-face', else
1595 return `planner-appt-overdue-face'."
1596 (if (planner-appt-earlier-than-now-p time
)
1597 'planner-appt-overdue-face
1598 'planner-appt-face
))
1600 (defun planner-appt-task-highlight (beg end
&optional verbose
)
1601 "Highlight appointment times in tasks from BEG to END.
1602 VERBOSE is ignored."
1603 (when planner-appt-font-lock-appointments-flag
1605 (while (re-search-forward planner-appt-task-regexp end t
)
1606 (when (or (not planner-appt-limit-highlighting-flag
)
1608 (goto-char (planner-line-beginning-position))
1610 (or (and (looking-at planner-live-task-regexp
)
1611 (string= (planner-task-date
1612 (planner-current-task-info))
1615 (planner-appt-todays-page-p)
1616 (looking-at planner-appt-schedule-appt-regexp
))))))
1617 (planner-highlight-region
1622 (planner-appt-task-highlight-face
1623 (match-string 1))))))))
1625 (defun planner-appt-font-setup ()
1626 "Hook into `planner-mode'."
1627 (planner-appt-add-hook 'muse-colors-buffer-hook
1628 'planner-appt-task-highlight t
))
1630 ;;; Calendar Marking
1632 ;; This is not strictly part of appointment handling, but if the
1633 ;; diary is to be by-passed for appointments, it makes sense to mark
1634 ;; the calendar using day pages.
1637 ;; Use a different face from the diary-entry-marker so we can
1638 ;; see where the different marks come from.
1639 (defface planner-appt-entry-marker
1640 '((t (:foreground
"indianred")))
1641 "Face for planner day page mark in the calendar."
1642 :group
'planner-appt
)
1645 (defun planner-appt-mark-calendar (&optional from to
)
1646 "Mark dates in the calendar that have day pages.
1647 Optional args: mark dates from FROM to TO.
1648 FROM and to are lists: (month day year)."
1649 (with-current-buffer calendar-buffer
; Needed to get displayed date
1651 (let* ((displayed-month-last
1652 (1+ (cdr (assq 'displayed-month
1653 (buffer-local-variables)))))
1655 (cdr (assq 'displayed-year
1656 (buffer-local-variables))))
1657 (today-filename (planner-today))
1658 (today (planner-filename-to-calendar-date today-filename
)))
1659 ;; Do nothing if the calendar is currently showing all months
1660 ;; earlier than today.
1661 (when (and (>= (elt today
2) ; year
1663 (>= displayed-month-last
1664 (elt today
0))) ; month
1665 (let ((diary-entry-marker 'planner-appt-entry-marker
)
1667 (planner-get-day-pages
1669 (planner-date-to-filename from
)
1672 (planner-date-to-filename to
)
1673 ;; The default is last day visible
1675 (planner-date-to-filename
1676 (list displayed-month-last
31 displayed-year
))))))
1677 (dolist (day-page day-pages
)
1678 (apply #'mark-calendar-date-pattern
1679 (planner-filename-to-calendar-date (car day-page
)))))))))
1681 (defun planner-appt-calendar-insinuate ()
1682 (add-hook 'mark-diary-entries-hook
'planner-appt-mark-calendar
))
1684 (provide 'planner-appt
)
1685 ;;; planner-appt.el ends here
1688 ;; indent-tabs-mode: t