(diary-face-attrs): Fix type of `width'.
[emacs.git] / lisp / calendar / diary-lib.el
blob8786ea95ad8984ba0ec38a288be2f2f771af5e17
1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This collection of functions implements the diary features as described
30 ;; in calendar.el.
32 ;;; Code:
34 (require 'calendar)
36 (eval-and-compile
37 (load "diary-loaddefs" nil 'quiet))
39 (defcustom diary-include-string "#include"
40 "The string indicating inclusion of another file of diary entries.
41 See the documentation for the function `include-other-diary-files'."
42 :type 'string
43 :group 'diary)
45 (defcustom diary-list-include-blanks nil
46 "If nil, do not include days with no diary entry in the list of diary entries.
47 Such days will then not be shown in the fancy diary buffer, even if they
48 are holidays."
49 :type 'boolean
50 :group 'diary)
52 (defcustom diary-face 'diary
53 "Face name to use for diary entries."
54 :type 'face
55 :group 'diary)
56 (make-obsolete-variable 'diary-face "customize the face `diary' instead."
57 "23.1")
59 (defface diary-anniversary '((t :inherit font-lock-keyword-face))
60 "Face used for anniversaries in the fancy diary display."
61 :version "22.1"
62 :group 'diary)
64 (defface diary-time '((t :inherit font-lock-variable-name-face))
65 "Face used for times of day in the diary."
66 :version "22.1"
67 :group 'diary)
69 (defface diary-button '((((type pc) (class color))
70 (:foreground "lightblue")))
71 "Default face used for buttons."
72 :version "22.1"
73 :group 'diary)
74 ;; Backward-compatibility alias. FIXME make obsolete.
75 (put 'diary-button-face 'face-alias 'diary-button)
77 ;; Face markup of calendar and diary displays: Any entry line that
78 ;; ends with [foo:value] where foo is a face attribute (except :box
79 ;; :stipple) or with [face:blah] tags, will have these values applied
80 ;; to the calendar and fancy diary displays. These attributes "stack"
81 ;; on calendar displays. File-wide attributes can be defined as
82 ;; follows: the first line matching "^# [tag:value]" defines the value
83 ;; for that particular tag.
84 (defcustom diary-face-attrs
85 '((" *\\[foreground:\\([-a-z]+\\)\\]$" 1 :foreground string)
86 (" *\\[background:\\([-a-z]+\\)\\]$" 1 :background string)
87 (" *\\[width:\\([-a-z]+\\)\\]$" 1 :width int)
88 (" *\\[height:\\([-0-9a-z]+\\)\\]$" 1 :height int)
89 (" *\\[weight:\\([-a-z]+\\)\\]$" 1 :weight symbol)
90 (" *\\[slant:\\([-a-z]+\\)\\]$" 1 :slant symbol)
91 (" *\\[underline:\\([-a-z]+\\)\\]$" 1 :underline stringtnil)
92 (" *\\[overline:\\([-a-z]+\\)\\]$" 1 :overline stringtnil)
93 (" *\\[strike-through:\\([-a-z]+\\)\\]$" 1 :strike-through stringtnil)
94 (" *\\[inverse-video:\\([-a-z]+\\)\\]$" 1 :inverse-video tnil)
95 (" *\\[face:\\([-0-9a-z]+\\)\\]$" 1 :face string)
96 (" *\\[font:\\([-a-z0-9]+\\)\\]$" 1 :font string)
97 ;; Unsupported.
98 ;;; (" *\\[box:\\([-a-z]+\\)\\]$" 1 :box)
99 ;;; (" *\\[stipple:\\([-a-z]+\\)\\]$" 1 :stipple)
101 "Alist of (REGEXP SUBEXP ATTRIBUTE TYPE) elements.
102 This is used by `diary-pull-attrs' to fontify certain diary
103 elements. REGEXP is a regular expression to for, and SUBEXP is
104 the numbered sub-expression to extract. `diary-glob-file-regexp-prefix'
105 is pre-pended to REGEXP for file-wide specifiers. ATTRIBUTE
106 specifies which face attribute (e.g. `:foreground') to modify, or
107 that this is a face (`:face') to apply. TYPE is the type of
108 attribute being applied. Available TYPES (see `diary-attrtype-convert')
109 are: `string', `symbol', `int', `tnil',`stringtnil.'"
110 :type '(repeat (list (string :tag "Regular expression")
111 (integer :tag "Sub-expression")
112 (symbol :tag "Attribute (e.g. :foreground)")
113 (choice (const string :tag "A string")
114 (const symbol :tag "A symbol")
115 (const int :tag "An integer")
116 (const tnil :tag "`t' or `nil'")
117 (const stringtnil
118 :tag "A string, `t', or `nil'"))))
119 :group 'diary)
121 (defcustom diary-glob-file-regexp-prefix "^\\#"
122 "Regular expression pre-pended to `diary-face-attrs' for file-wide specifiers."
123 :type 'regexp
124 :group 'diary)
126 (defcustom diary-file-name-prefix nil
127 "Non-nil means prefix each diary entry with the name of the file defining it."
128 :type 'boolean
129 :group 'diary)
131 (defcustom diary-file-name-prefix-function 'identity
132 "The function that will take a diary file name and return the desired prefix."
133 :type 'function
134 :group 'diary)
136 (defcustom sexp-diary-entry-symbol "%%"
137 "The string used to indicate a sexp diary entry in `diary-file'.
138 See the documentation for the function `list-sexp-diary-entries'."
139 :type 'string
140 :group 'diary)
142 (defcustom list-diary-entries-hook nil
143 "List of functions called after diary file is culled for relevant entries.
144 You might wish to add `include-other-diary-files', in which case
145 you will probably also want to add `mark-included-diary-files' to
146 `mark-diary-entries-hook'. For example, you could use
148 (add-hook 'list-diary-entries-hook 'include-other-diary-files)
149 (add-hook 'list-diary-entries-hook 'sort-diary-entries)
150 (add-hook 'diary-display-hook 'fancy-diary-display)
152 in your `.emacs' file to cause the fancy diary buffer to be displayed with
153 diary entries from various included files, each day's entries sorted into
154 lexicographic order."
155 :type 'hook
156 :options '(include-other-diary-files sort-diary-entries)
157 :group 'diary)
159 (defcustom mark-diary-entries-hook nil
160 "List of functions called after marking diary entries in the calendar.
161 You might wish to add `mark-included-diary-files', in which case
162 you will probably also want to add `include-other-diary-files' to
163 `list-diary-entries-hook'."
164 :type 'hook
165 :options '(mark-included-diary-files)
166 :group 'diary)
168 (defcustom nongregorian-diary-listing-hook nil
169 "List of functions called for listing diary file and included files.
170 As the files are processed for diary entries, these functions are used
171 to cull relevant entries. You can use any or all of
172 `list-hebrew-diary-entries', `list-islamic-diary-entries' and
173 `diary-bahai-list-entries'. The documentation for these functions
174 describes the style of such diary entries."
175 :type 'hook
176 :options '(list-hebrew-diary-entries
177 list-islamic-diary-entries
178 diary-bahai-list-entries)
179 :group 'diary)
181 (defcustom nongregorian-diary-marking-hook nil
182 "List of functions called for marking diary file and included files.
183 As the files are processed for diary entries, these functions are used
184 to cull relevant entries. You can use any or all of
185 `mark-hebrew-diary-entries', `mark-islamic-diary-entries' and
186 `bahai-mark-diary-entries'. The documentation for these functions
187 describes the style of such diary entries."
188 :type 'hook
189 :options '(mark-hebrew-diary-entries
190 mark-islamic-diary-entries
191 diary-bahai-mark-entries)
192 :group 'diary)
194 (defcustom print-diary-entries-hook 'lpr-buffer
195 "List of functions called after a temporary diary buffer is prepared.
196 The buffer shows only the diary entries currently visible in the diary
197 buffer. The default just does the printing. Other uses might include, for
198 example, rearranging the lines into order by day and time, saving the buffer
199 instead of deleting it, or changing the function used to do the printing."
200 :type 'hook
201 :group 'diary)
203 (defcustom diary-unknown-time -9999
204 "Value returned by `diary-entry-time' when no time is found.
205 The default value -9999 causes entries with no recognizable time to be placed
206 before those with times; 9999 would place entries with no recognizable time
207 after those with times."
208 :type 'integer
209 :group 'diary
210 :version "20.3")
212 (defcustom diary-mail-addr
213 (if (boundp 'user-mail-address) user-mail-address "")
214 "Email address that `diary-mail-entries' will send email to."
215 :group 'diary
216 :type 'string
217 :version "20.3")
219 (defcustom diary-mail-days 7
220 "Default number of days for `diary-mail-entries' to check."
221 :group 'diary
222 :type 'integer
223 :version "20.3")
225 (defcustom diary-remind-message
226 '("Reminder: Only "
227 (if (zerop (% days 7))
228 (format "%d week%s" (/ days 7) (if (= 7 days) "" "s"))
229 (format "%d day%s" days (if (= 1 days) "" "s")))
230 " until "
231 diary-entry)
232 "Pseudo-pattern giving form of reminder messages in the fancy diary display.
234 Used by the function `diary-remind', a pseudo-pattern is a list of
235 expressions that can involve the keywords `days' (a number), `date' (a list of
236 month, day, year), and `diary-entry' (a string)."
237 :type 'sexp
238 :group 'diary)
240 (defcustom diary-outlook-formats
242 ;; When: 11 October 2001 12:00-14:00 (GMT) Greenwich Mean Time : Dublin, ...
243 ;; [Current UK format? The timezone is meaningless. Sometimes the
244 ;; Where is missing.]
245 ("When: \\([0-9]+ [[:alpha:]]+ [0-9]+\\) \
246 \\([^ ]+\\) [^\n]+
247 \[^\n]+
248 \\(?:Where: \\([^\n]+\\)\n+\\)?
249 \\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*"
250 . "\\1\n \\2 %s, \\3")
251 ;; When: Tuesday, April 30, 2002 03:00 PM-03:30 PM (GMT) Greenwich Mean ...
252 ;; [Old UK format?]
253 ("^When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \\([0-9]\\{4\\}\\) \
254 \\([^ ]+\\) [^\n]+
255 \[^\n]+
256 \\(?:Where: \\([^\n]+\\)\\)?\n+"
257 . "\\2 \\1 \\3\n \\4 %s, \\5")
259 ;; German format, apparently.
260 "^Zeit: [^ ]+, +\\([0-9]+\\)\. +\\([[:upper:]][[:lower:]][[:lower:]]\\)[^ ]* +\\([0-9]+\\) +\\([^ ]+\\).*$"
261 . "\\1 \\2 \\3\n \\4 %s"))
262 "Alist of regexps matching message text and replacement text.
264 The regexp must match the start of the message text containing an
265 appointment, but need not include a leading `^'. If it matches the
266 current message, a diary entry is made from the corresponding
267 template. If the template is a string, it should be suitable for
268 passing to `replace-match', and so will have occurrences of `\\D' to
269 substitute the match for the Dth subexpression. It must also contain
270 a single `%s' which will be replaced with the text of the message's
271 Subject field. Any other `%' characters must be doubled, so that the
272 template can be passed to `format'.
274 If the template is actually a function, it is called with the message
275 body text as argument, and may use `match-string' etc. to make a
276 template following the rules above."
277 :type '(alist :key-type (regexp :tag "Regexp matching time/place")
278 :value-type (choice
279 (string :tag "Template for entry")
280 (function :tag
281 "Unary function providing template")))
282 :version "22.1"
283 :group 'diary)
286 (defun diary-set-header (symbol value)
287 "Set SYMBOL's value to VALUE, and redraw the diary header if necessary."
288 (let ((oldvalue (symbol-value symbol))
289 (dbuff (and diary-file
290 (find-buffer-visiting
291 (substitute-in-file-name diary-file)))))
292 (custom-set-default symbol value)
293 (and dbuff
294 (not (equal value oldvalue))
295 (with-current-buffer dbuff
296 (if (eq major-mode 'diary-mode)
297 (setq header-line-format (and diary-header-line-flag
298 diary-header-line-format)))))))
300 ;; This can be removed once the kill/yank treatment of invisible text
301 ;; (see etc/TODO) is fixed. -- gm
302 (defcustom diary-header-line-flag t
303 "Non-nil means `simple-diary-display' will show a header line.
304 The format of the header is specified by `diary-header-line-format'."
305 :group 'diary
306 :type 'boolean
307 :initialize 'custom-initialize-default
308 :set 'diary-set-header
309 :version "22.1")
311 (defvar diary-selective-display nil
312 "Internal diary variable; non-nil if some diary text is hidden.")
314 (defcustom diary-header-line-format
315 '(:eval (calendar-string-spread
316 (list (if diary-selective-display
317 "Some text is hidden - press \"s\" in calendar \
318 before edit/copy"
319 "Diary"))
320 ?\s (frame-width)))
321 "Format of the header line displayed by `simple-diary-display'.
322 Only used if `diary-header-line-flag' is non-nil."
323 :group 'diary
324 :type 'sexp
325 :initialize 'custom-initialize-default
326 :set 'diary-set-header
327 :version "22.1")
329 ;; The first version of this also checked for diary-selective-display
330 ;; in the non-fancy case. This was an attempt to distinguish between
331 ;; displaying the diary and just visiting the diary file. However,
332 ;; when using fancy diary, calling diary when there are no entries to
333 ;; display does not create the fancy buffer, nor does it set
334 ;; diary-selective-display in the diary buffer. This means some
335 ;; customizations will not take effect, eg:
336 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
337 ;; So the check for diary-selective-display was dropped. This means the
338 ;; diary will be displayed if one customizes a diary variable while
339 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
340 ;;;###cal-autoload
341 (defun diary-live-p ()
342 "Return non-nil if the diary is being displayed."
343 (or (get-buffer fancy-diary-buffer)
344 (and diary-file
345 (find-buffer-visiting (substitute-in-file-name diary-file)))))
347 ;;;###cal-autoload
348 (defun diary-set-maybe-redraw (symbol value)
349 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
350 Redraws the diary if it is being displayed (note this is not the same as
351 just visiting the `diary-file'), and SYMBOL's value is to be changed."
352 (let ((oldvalue (symbol-value symbol)))
353 (custom-set-default symbol value)
354 (and (not (equal value oldvalue))
355 (diary-live-p)
356 ;; Note this assumes diary was called without prefix arg.
357 (diary))))
359 (defcustom number-of-diary-entries 1
360 "Specifies how many days of diary entries are to be displayed initially.
361 This variable affects the diary display when the command \\[diary] is used,
362 or if the value of the variable `view-diary-entries-initially' is non-nil.
363 For example, if the default value 1 is used, then only the current day's diary
364 entries will be displayed. If the value 2 is used, then both the current
365 day's and the next day's entries will be displayed.
367 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
368 says to display no diary entries on Sunday, the entries for
369 the current date and the day after on Monday through Thursday,
370 Friday through Monday's entries on Friday, and only Saturday's
371 entries on Saturday.
373 This variable does not affect the diary display with the `d' command
374 from the calendar; in that case, the prefix argument controls the
375 number of days of diary entries displayed."
376 :type '(choice (integer :tag "Entries")
377 (vector :value [0 0 0 0 0 0 0]
378 (integer :tag "Sunday")
379 (integer :tag "Monday")
380 (integer :tag "Tuesday")
381 (integer :tag "Wednesday")
382 (integer :tag "Thursday")
383 (integer :tag "Friday")
384 (integer :tag "Saturday")))
385 :initialize 'custom-initialize-default
386 :set 'diary-set-maybe-redraw
387 :group 'diary)
389 ;;; More user options in calendar.el.
392 (defun diary-check-diary-file ()
393 "Check that the file specified by `diary-file' exists and is readable.
394 If so, return the expanded file name, otherwise signal an error."
395 (let ((d-file (substitute-in-file-name diary-file)))
396 (if (and d-file (file-exists-p d-file))
397 (if (file-readable-p d-file)
398 d-file
399 (error "Diary file `%s' is not readable" diary-file))
400 (error "Diary file `%s' does not exist" diary-file))))
402 ;;;###autoload
403 (defun diary (&optional arg)
404 "Generate the diary window for ARG days starting with the current date.
405 If no argument is provided, the number of days of diary entries is governed
406 by the variable `number-of-diary-entries'. A value of ARG less than 1
407 does nothing. This function is suitable for execution in a `.emacs' file."
408 (interactive "P")
409 (diary-check-diary-file)
410 (diary-list-entries (calendar-current-date)
411 (if arg (prefix-numeric-value arg))))
413 ;;;###cal-autoload
414 (defun diary-view-entries (&optional arg)
415 "Prepare and display a buffer with diary entries.
416 Searches the file named in `diary-file' for entries that
417 match ARG days starting with the date indicated by the cursor position
418 in the displayed three-month calendar."
419 (interactive "p")
420 (diary-check-diary-file)
421 (diary-list-entries (calendar-cursor-to-date t) arg))
423 (define-obsolete-function-alias 'view-diary-entries 'diary-view-entries)
426 ;;;###cal-autoload
427 (defun view-other-diary-entries (arg dfile)
428 "Prepare and display buffer of diary entries from an alternative diary file.
429 Searches for entries that match ARG days, starting with the date indicated
430 by the cursor position in the displayed three-month calendar.
431 DFILE specifies the file to use as the diary file."
432 (interactive
433 (list (prefix-numeric-value current-prefix-arg)
434 (read-file-name "Enter diary file name: " default-directory nil t)))
435 (let ((diary-file dfile))
436 (diary-view-entries arg)))
438 (defvar diary-syntax-table
439 (let ((st (copy-syntax-table (standard-syntax-table))))
440 (modify-syntax-entry ?* "w" st)
441 (modify-syntax-entry ?: "w" st)
443 "The syntax table used when parsing dates in the diary file.
444 It is the standard syntax table used in Fundamental mode, but with the
445 syntax of `*' and `:' changed to be word constituents.")
447 (defun diary-attrtype-convert (attrvalue type)
448 "Convert string ATTRVALUE to TYPE appropriate for a face description.
449 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
450 (cond ((eq type 'string) attrvalue)
451 ((eq type 'symbol) (intern-soft attrvalue))
452 ((eq type 'int) (string-to-number attrvalue))
453 ((eq type 'stringtnil)
454 (cond ((string-equal "t" attrvalue) t)
455 ((string-equal "nil" attrvalue) nil)
456 (t attrvalue)))
457 ((eq type 'tnil) (string-equal "t" attrvalue))))
459 (defun diary-pull-attrs (entry fileglobattrs)
460 "Search for matches for regexps from `diary-face-attrs'.
461 If ENTRY is nil, searches from the start of the current buffer, and
462 prepends all regexps with `diary-glob-file-regexp-prefix'.
463 If ENTRY is a string, search for matches in that string, and remove them.
464 Returns a list of ENTRY followed by (ATTRIBUTE VALUE) pairs.
465 When ENTRY is non-nil, FILEGLOBATTRS forms the start of the (ATTRIBUTE VALUE)
466 pairs."
467 (let (regexp regnum attrname attrname attrvalue type ret-attr)
468 (if (null entry)
469 (save-excursion
470 (dolist (attr diary-face-attrs)
471 ;; FIXME inefficient searching.
472 (goto-char (point-min))
473 (setq regexp (concat diary-glob-file-regexp-prefix (car attr))
474 regnum (cadr attr)
475 attrname (nth 2 attr)
476 type (nth 3 attr)
477 attrvalue (if (re-search-forward regexp nil t)
478 (match-string-no-properties regnum)))
479 (and attrvalue
480 (setq attrvalue (diary-attrtype-convert attrvalue type))
481 (setq ret-attr (append ret-attr
482 (list attrname attrvalue))))))
483 (setq ret-attr fileglobattrs)
484 (dolist (attr diary-face-attrs)
485 (setq regexp (car attr)
486 regnum (cadr attr)
487 attrname (nth 2 attr)
488 type (nth 3 attr)
489 attrvalue nil)
490 ;; FIXME multiple matches?
491 (if (string-match regexp entry)
492 (setq attrvalue (match-string-no-properties regnum entry)
493 entry (replace-match "" t t entry)))
494 (and attrvalue
495 (setq attrvalue (diary-attrtype-convert attrvalue type))
496 (setq ret-attr (append ret-attr (list attrname attrvalue))))))
497 (list entry ret-attr)))
501 (defvar diary-modify-entry-list-string-function nil
502 "Function applied to entry string before putting it into the entries list.
503 Can be used by programs integrating a diary list into other buffers (e.g.
504 org.el and planner.el) to modify the string or add properties to it.
505 The function takes a string argument and must return a string.")
507 (defvar diary-entries-list) ; bound in diary-list-entries
509 (defun add-to-diary-list (date string specifier &optional marker
510 globcolor literal)
511 "Add an entry to `diary-entries-list'.
512 Do nothing if DATE or STRING is nil. DATE is the (MONTH DAY
513 YEAR) for which the entry applies; STRING is the text of the
514 entry as it will appear in the diary (i.e. with any format
515 strings such as \"%d\" expanded); SPECIFIER is the date part of
516 the entry as it appears in the diary-file; LITERAL is the entry
517 as it appears in the diary-file (i.e. before expansion). If
518 LITERAL is nil, it is taken to be the same as STRING.
520 The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
521 GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
522 FILENAME being the file containing the diary entry."
523 (when (and date string)
524 (if diary-file-name-prefix
525 (let ((prefix (funcall diary-file-name-prefix-function
526 (buffer-file-name))))
527 (or (string-equal prefix "")
528 (setq string (format "[%s] %s" prefix string)))))
529 (and diary-modify-entry-list-string-function
530 (setq string (funcall diary-modify-entry-list-string-function
531 string)))
532 (setq diary-entries-list
533 (append diary-entries-list
534 (list (list date string specifier
535 (list marker (buffer-file-name) literal)
536 globcolor))))))
538 (defvar number) ; not clear this should use number
540 (defun diary-list-entries-2 (date mark globattr list-only
541 &optional months symbol)
542 "Internal subroutine of `diary-list-entries'.
543 Find diary entries applying to DATE, by searching from point-min for
544 each element of `diary-date-forms'. MARK indicates an entry is non-marking.
545 GLOBATTR is the list of global file attributes. If LIST-ONLY is
546 non-nil, don't change the buffer, only return a list of entries.
547 Optional array MONTHS replaces `calendar-month-name-array', and
548 means months cannot be abbreviated. Optional string SYMBOL marks diary
549 entries of the desired type. Returns non-nil if any entries were found."
550 (let* ((month (extract-calendar-month date))
551 (day (extract-calendar-day date))
552 (year (extract-calendar-year date))
553 (dayname (format "%s\\|%s\\.?" (calendar-day-name date)
554 (calendar-day-name date 'abbrev)))
555 (calendar-month-name-array (or months calendar-month-name-array))
556 (monthname (format "\\*\\|%s%s" (calendar-month-name month)
557 (if months ""
558 (format "\\|%s\\.?"
559 (calendar-month-name month 'abbrev)))))
560 (month (format "\\*\\|0*%d" month))
561 (day (format "\\*\\|0*%d" day))
562 (year (format "\\*\\|0*%d%s" year
563 (if abbreviated-calendar-year
564 ;; FIXME was %d in non-greg case.
565 (format "\\|%02d" (% year 100))
566 "")))
567 (case-fold-search t)
568 entry-found)
569 (dolist (date-form diary-date-forms)
570 (let ((backup (when (eq (car date-form) 'backup)
571 (setq date-form (cdr date-form))
573 ;; date-form uses day etc as set above.
574 (regexp (format "^%s?%s\\(%s\\)" (regexp-quote mark)
575 (if symbol (regexp-quote symbol) "")
576 (mapconcat 'eval date-form "\\)\\(?:")))
577 entry-start date-start temp)
578 (goto-char (point-min))
579 (while (re-search-forward regexp nil t)
580 (if backup (re-search-backward "\\<" nil t))
581 (if (and (bolp) (not (looking-at "[ \t]")))
582 ;; Diary entry that consists only of date.
583 (backward-char 1)
584 ;; Found a nonempty diary entry--make it
585 ;; visible and add it to the list.
586 ;; Actual entry starts on the next-line?
587 (if (looking-at "[ \t]*\n[ \t]") (forward-line 1))
588 (setq entry-found t
589 entry-start (point)
590 ;; If bolp, must have done (forward-line 1).
591 ;; FIXME Why number > 1?
592 date-start (line-end-position (if (and (bolp) (> number 1))
593 -1 0)))
594 (forward-line 1)
595 (while (looking-at "[ \t]") ; continued entry
596 (forward-line 1))
597 (unless (and (eobp) (not (bolp)))
598 (backward-char 1))
599 (unless list-only
600 (remove-overlays date-start (point) 'invisible 'diary))
601 (setq temp (diary-pull-attrs
602 (buffer-substring-no-properties
603 entry-start (point)) globattr))
604 (add-to-diary-list
605 date (car temp)
606 (buffer-substring-no-properties (1+ date-start) (1- entry-start))
607 (copy-marker entry-start) (cadr temp))))))
608 entry-found))
610 (defvar original-date) ; from diary-list-entries
611 (defvar file-glob-attrs)
612 (defvar list-only)
614 (defun diary-list-entries-1 (months symbol absfunc)
615 "List diary entries of a certain type.
616 MONTHS is an array of month names. SYMBOL marks diary entries of the type
617 in question. ABSFUNC is a function that converts absolute dates to dates
618 of the appropriate type."
619 (let ((gdate original-date))
620 (dotimes (idummy number)
621 (diary-list-entries-2
622 (funcall absfunc (calendar-absolute-from-gregorian gdate))
623 diary-nonmarking-symbol file-glob-attrs list-only months symbol)
624 (setq gdate
625 (calendar-gregorian-from-absolute
626 (1+ (calendar-absolute-from-gregorian gdate))))))
627 (goto-char (point-min)))
629 ;; FIXME non-greg and list hooks run same number of times?
630 (define-obsolete-function-alias 'list-diary-entries 'diary-list-entries)
631 (defun diary-list-entries (date number &optional list-only)
632 "Create and display a buffer containing the relevant lines in `diary-file'.
633 The arguments are DATE and NUMBER; the entries selected are those
634 for NUMBER days starting with date DATE. The other entries are hidden
635 using overlays. If NUMBER is less than 1, this function does nothing.
637 Returns a list of all relevant diary entries found, if any, in order by date.
638 The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
639 \(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
640 SPECIFIER is the applicability. If the variable `diary-list-include-blanks'
641 is non-nil, this list includes a dummy diary entry consisting of the empty
642 string for a date with no diary entries.
644 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
645 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
646 These hooks have the following distinct roles:
648 `nongregorian-diary-listing-hook' can cull dates from the diary
649 and each included file, for example to process Islamic diary
650 entries. Applied to *each* file.
652 `list-diary-entries-hook' adds or manipulates diary entries from
653 external sources. Used, for example, to include diary entries
654 from other files or to sort the diary entries. Invoked *once* only,
655 before the display hook is run.
657 `diary-display-hook' does the actual display of information. If this is
658 nil, `simple-diary-display' will be used. Use `add-hook' to use
659 `fancy-diary-display', if desired. If you want no diary display, use
660 add-hook to set this to `ignore'.
662 `diary-hook' is run last. This can be used for an appointment
663 notification function.
665 Functions called by these hooks may use the variables ORIGINAL-DATE
666 and NUMBER, which are the arguments with which this function was called.
667 Note that hook functions should _not_ use DATE, but ORIGINAL-DATE.
668 \(Sexp diary entries may use DATE - see `list-sexp-diary-entries'.)
670 If LIST-ONLY is non-nil don't modify or display the buffer, only return a list."
671 (unless number
672 (setq number (if (vectorp number-of-diary-entries)
673 (aref number-of-diary-entries (calendar-day-of-week date))
674 number-of-diary-entries)))
675 (when (> number 0)
676 (let ((original-date date) ; save for possible use in the hooks
677 diary-entries-list
678 file-glob-attrs
679 (date-string (calendar-date-string date))
680 (d-file (substitute-in-file-name diary-file)))
681 (message "Preparing diary...")
682 (save-excursion
683 (let ((diary-buffer (find-buffer-visiting d-file)))
684 (if (not diary-buffer)
685 (set-buffer (find-file-noselect d-file t))
686 (set-buffer diary-buffer)
687 (or (verify-visited-file-modtime diary-buffer)
688 (revert-buffer t t))))
689 ;; Setup things like the header-line-format and invisibility-spec.
690 (if (eq major-mode default-major-mode)
691 (diary-mode)
692 ;; This kludge is to make customizations to
693 ;; diary-header-line-flag after diary has been displayed
694 ;; take effect. Unconditionally calling (diary-mode)
695 ;; clobbers file local variables.
696 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
697 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
698 (if (eq major-mode 'diary-mode)
699 (setq header-line-format (and diary-header-line-flag
700 diary-header-line-format))))
701 ;; d-s-p is passed to the diary display function.
702 (let ((diary-saved-point (point)))
703 (save-excursion
704 ;; FIXME move after goto? Syntax?
705 (setq file-glob-attrs (cadr (diary-pull-attrs nil "")))
706 (with-syntax-table diary-syntax-table
707 (goto-char (point-min))
708 (unless list-only
709 (let ((ol (make-overlay (point-min) (point-max) nil t nil)))
710 (set (make-local-variable 'diary-selective-display) t)
711 (overlay-put ol 'invisible 'diary)
712 (overlay-put ol 'evaporate t)))
713 (dotimes (idummy number)
714 (let ((sexp-found (list-sexp-diary-entries date))
715 (entry-found (diary-list-entries-2
716 date diary-nonmarking-symbol
717 file-glob-attrs list-only)))
718 (if diary-list-include-blanks
719 (or sexp-found entry-found
720 (add-to-diary-list date "" "" "" "")))
721 (setq date
722 (calendar-gregorian-from-absolute
723 (1+ (calendar-absolute-from-gregorian date)))))))
724 (goto-char (point-min))
725 (run-hooks 'nongregorian-diary-listing-hook
726 'list-diary-entries-hook)
727 (unless list-only
728 (if diary-display-hook
729 (run-hooks 'diary-display-hook)
730 (simple-diary-display)))
731 (run-hooks 'diary-hook)
732 diary-entries-list))))))
734 (defun diary-unhide-everything ()
735 "Show all invisible text in the diary."
736 (kill-local-variable 'diary-selective-display)
737 (remove-overlays (point-min) (point-max) 'invisible 'diary)
738 (kill-local-variable 'mode-line-format))
740 (defvar original-date) ; bound in diary-list-entries
741 (defvar number)
743 (defun include-other-diary-files ()
744 "Include the diary entries from other diary files with those of `diary-file'.
745 This function is suitable for use with `list-diary-entries-hook';
746 it enables you to use shared diary files together with your own.
747 The files included are specified in the `diary-file' by lines of this form:
748 #include \"filename\"
749 This is recursive; that is, #include directives in diary files thus included
750 are obeyed. You can change the `#include' to some other string by
751 changing the variable `diary-include-string'."
752 (goto-char (point-min))
753 (while (re-search-forward
754 (format "^%s \"\\([^\"]*\\)\"" (regexp-quote diary-include-string))
755 nil t)
756 (let ((diary-file (substitute-in-file-name
757 (match-string-no-properties 1)))
758 (diary-list-include-blanks nil)
759 (list-diary-entries-hook 'include-other-diary-files)
760 (diary-display-hook 'ignore)
761 (diary-hook nil))
762 (if (file-exists-p diary-file)
763 (if (file-readable-p diary-file)
764 (unwind-protect
765 (setq diary-entries-list
766 (append diary-entries-list
767 (diary-list-entries original-date number)))
768 (with-current-buffer (find-buffer-visiting diary-file)
769 (diary-unhide-everything)))
770 (beep)
771 (message "Can't read included diary file %s" diary-file)
772 (sleep-for 2))
773 (beep)
774 (message "Can't find included diary file %s" diary-file)
775 (sleep-for 2))))
776 (goto-char (point-min)))
778 (defvar date-string) ; bound in diary-list-entries
780 (defun diary-display-no-entries ()
781 "Common subroutine of `simple-diary-display' and `fancy-diary-display'.
782 Handles the case where there are no diary entries.
783 Returns a cons (NOENTRIES . HOLIDAY-STRING)."
784 (let* ((holiday-list (if holidays-in-diary-buffer
785 (calendar-check-holidays original-date)))
786 (hol-string (format "%s%s%s"
787 date-string
788 (if holiday-list ": " "")
789 (mapconcat 'identity holiday-list "; ")))
790 (msg (format "No diary entries for %s" hol-string))
791 ;; Empty list, or single item with no text.
792 ;; FIXME multiple items with no text?
793 (noentries (or (not diary-entries-list)
794 (and (not (cdr diary-entries-list))
795 (string-equal "" (cadr
796 (car diary-entries-list)))))))
797 ;; Inconsistency: whether or not the holidays are displayed in a
798 ;; separate buffer depends on if there are diary entries.
799 (when noentries
800 (if (or (< (length msg) (frame-width))
801 (not holiday-list))
802 (message "%s" msg)
803 ;; holiday-list which is too wide for a message gets a buffer.
804 (calendar-in-read-only-buffer holiday-buffer
805 (calendar-set-mode-line (format "Holidays for %s" date-string))
806 (insert (mapconcat 'identity holiday-list "\n")))
807 (message "No diary entries for %s" date-string)))
808 (cons noentries hol-string)))
811 (defvar diary-saved-point) ; bound in diary-list-entries
813 (defun simple-diary-display ()
814 "Display the diary buffer if there are any relevant entries or holidays."
815 ;; If selected window is dedicated (to the calendar), need a new one
816 ;; to display the diary.
817 (let* ((pop-up-frames (or pop-up-frames
818 (window-dedicated-p (selected-window))))
819 (dbuff (find-buffer-visiting (substitute-in-file-name diary-file)))
820 (empty (diary-display-no-entries)))
821 ;; This may be too wide, but when simple diary is used there is
822 ;; nowhere else for the holidays to go. Also, it is documented in
823 ;; holidays-in-diary-buffer that the holidays go in the mode-line.
824 ;; FIXME however if there are no diary entries a separate buffer
825 ;; is displayed - this is inconsistent.
826 (with-current-buffer dbuff
827 (calendar-set-mode-line (format "Diary for %s" (cdr empty))))
828 (unless (car empty) ; no entries
829 (with-current-buffer dbuff
830 (let ((window (display-buffer (current-buffer))))
831 ;; d-s-p is passed from diary-list-entries.
832 (set-window-point window diary-saved-point)
833 (set-window-start window (point-min))))
834 (message "Preparing diary...done"))))
836 (define-button-type 'diary-entry
837 'action #'diary-goto-entry
838 'face 'diary-button)
840 (defun diary-goto-entry (button)
841 "Jump to the diary entry for the BUTTON at point."
842 (let* ((locator (button-get button 'locator))
843 (marker (car locator))
844 markbuf file)
845 ;; If marker pointing to diary location is valid, use that.
846 (if (and marker (setq markbuf (marker-buffer marker)))
847 (progn
848 (pop-to-buffer markbuf)
849 (goto-char (marker-position marker)))
850 ;; Marker is invalid (eg buffer has been killed).
851 (or (and (setq file (cadr locator))
852 (file-exists-p file)
853 (find-file-other-window file)
854 (progn
855 (when (eq major-mode default-major-mode) (diary-mode))
856 (goto-char (point-min))
857 (if (re-search-forward (format "%s.*\\(%s\\)"
858 (regexp-quote (nth 2 locator))
859 (regexp-quote (nth 3 locator)))
860 nil t)
861 (goto-char (match-beginning 1)))))
862 (message "Unable to locate this diary entry")))))
864 (defun fancy-diary-display ()
865 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
866 To use this function, add it to `diary-display-hook'."
867 ;; Turn off selective-display in the diary file's buffer.
868 (with-current-buffer
869 (find-buffer-visiting (substitute-in-file-name diary-file))
870 (diary-unhide-everything))
871 (unless (car (diary-display-no-entries)) ; no entries
872 ;; Prepare the fancy diary buffer.
873 (calendar-in-read-only-buffer fancy-diary-buffer
874 (calendar-set-mode-line "Diary Entries")
875 (let ((holiday-list-last-month 1)
876 (holiday-list-last-year 1)
877 (date (list 0 0 0))
878 holiday-list)
879 (dolist (entry diary-entries-list)
880 (unless (calendar-date-equal date (car entry))
881 (setq date (car entry))
882 (and holidays-in-diary-buffer
883 (calendar-date-compare
884 (list (list holiday-list-last-month
885 (calendar-last-day-of-month
886 holiday-list-last-month
887 holiday-list-last-year)
888 holiday-list-last-year))
889 (list date))
890 ;; We need to get the holidays for the next 3 months.
891 (setq holiday-list-last-month
892 (extract-calendar-month date)
893 holiday-list-last-year
894 (extract-calendar-year date))
895 (progn
896 (increment-calendar-month
897 holiday-list-last-month holiday-list-last-year 1)
899 (setq holiday-list
900 (let ((displayed-month holiday-list-last-month)
901 (displayed-year holiday-list-last-year))
902 (calendar-holiday-list)))
903 (increment-calendar-month
904 holiday-list-last-month holiday-list-last-year 1))
905 (let (date-holiday-list)
906 ;; Make a list of all holidays for date.
907 (dolist (h holiday-list)
908 (if (calendar-date-equal date (car h))
909 (setq date-holiday-list (append date-holiday-list
910 (cdr h)))))
911 (insert (if (bobp) "" ?\n) (calendar-date-string date))
912 (if date-holiday-list (insert ": "))
913 (let ((l (current-column))
914 (longest 0))
915 (insert (mapconcat (lambda (x)
916 (if (< longest (length x))
917 (setq longest (length x)))
919 date-holiday-list
920 (concat "\n" (make-string l ? ))))
921 (insert ?\n (make-string (+ l longest) ?=) ?\n))))
922 (let ((this-entry (cadr entry))
923 this-loc)
924 (unless (zerop (length this-entry))
925 (if (setq this-loc (nth 3 entry))
926 (insert-button (concat this-entry "\n")
927 ;; (MARKER FILENAME SPECIFIER LITERAL)
928 'locator (list (car this-loc)
929 (cadr this-loc)
930 (nth 2 entry)
931 (or (nth 2 this-loc)
932 (nth 1 entry)))
933 :type 'diary-entry)
934 (insert this-entry ?\n))
935 (save-excursion
936 (let* ((marks (nth 4 entry))
937 (faceinfo marks)
938 temp-face)
939 (when marks
940 (setq temp-face (make-symbol
941 (apply
942 'concat "temp-face-"
943 (mapcar (lambda (sym)
944 (if (stringp sym)
946 (symbol-name sym)))
947 marks))))
948 (make-face temp-face)
949 ;; Remove :face info from the marks,
950 ;; copy the face info into temp-face
951 (while (setq faceinfo (memq :face faceinfo))
952 (copy-face (read (nth 1 faceinfo)) temp-face)
953 (setcar faceinfo nil)
954 (setcar (cdr faceinfo) nil))
955 (setq marks (delq nil marks))
956 ;; Apply the font aspects.
957 (apply 'set-face-attribute temp-face nil marks)
958 (search-backward this-entry)
959 (overlay-put
960 (make-overlay (match-beginning 0) (match-end 0))
961 'face temp-face))))))))
962 (fancy-diary-display-mode)
963 (calendar-set-mode-line date-string)
964 (message "Preparing diary...done"))))
966 ;; FIXME modernize?
967 (defun print-diary-entries ()
968 "Print a hard copy of the diary display.
970 If the simple diary display is being used, prepare a temp buffer with the
971 visible lines of the diary buffer, add a heading line composed from the mode
972 line, print the temp buffer, and destroy it.
974 If the fancy diary display is being used, just print the buffer.
976 The hooks given by the variable `print-diary-entries-hook' are called to do
977 the actual printing."
978 (interactive)
979 (if (bufferp (get-buffer fancy-diary-buffer))
980 (with-current-buffer (get-buffer fancy-diary-buffer)
981 (run-hooks 'print-diary-entries-hook))
982 (let ((diary-buffer
983 (find-buffer-visiting (substitute-in-file-name diary-file))))
984 (if diary-buffer
985 ;; Name affects printing?
986 (let ((temp-buffer (get-buffer-create " *Printable Diary Entries*"))
987 heading)
988 (with-current-buffer diary-buffer
989 (setq heading
990 (if (not (stringp mode-line-format))
991 "All Diary Entries"
992 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
993 (match-string 1 mode-line-format)))
994 (let ((start (point-min))
995 end)
996 (while
997 (progn
998 (setq end (next-single-char-property-change
999 start 'invisible))
1000 (unless (get-char-property start 'invisible)
1001 (with-current-buffer temp-buffer
1002 (insert-buffer-substring diary-buffer
1003 start (or end (point-max)))))
1004 (setq start end)
1005 (and end (< end (point-max))))))
1006 (set-buffer temp-buffer)
1007 (goto-char (point-min))
1008 (insert heading "\n"
1009 (make-string (length heading) ?=) "\n")
1010 (run-hooks 'print-diary-entries-hook)
1011 (kill-buffer temp-buffer)))
1012 (error "You don't have a diary buffer!")))))
1014 (define-obsolete-function-alias 'show-all-diary-entries 'diary-show-all-entries)
1015 ;;;###cal-autoload
1016 (defun diary-show-all-entries ()
1017 "Show all of the diary entries in the diary file.
1018 This function gets rid of the selective display of the diary file so that
1019 all entries, not just some, are visible. If there is no diary buffer, one
1020 is created."
1021 (interactive)
1022 (let ((d-file (diary-check-diary-file))
1023 (pop-up-frames (window-dedicated-p (selected-window))))
1024 (with-current-buffer (or (find-buffer-visiting d-file)
1025 (find-file-noselect d-file t))
1026 (when (eq major-mode default-major-mode) (diary-mode))
1027 (diary-unhide-everything)
1028 (display-buffer (current-buffer)))))
1030 ;;;###autoload
1031 (defun diary-mail-entries (&optional ndays)
1032 "Send a mail message showing diary entries for next NDAYS days.
1033 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
1034 Mail is sent to the address specified by `diary-mail-addr'.
1036 You can call `diary-mail-entries' every night using an at/cron job.
1037 For example, this script will run the program at 2am daily. Since
1038 `emacs -batch' does not load your `.emacs' file, you must ensure that
1039 all relevant variables are set, as done here.
1041 #!/bin/sh
1042 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
1043 emacs -batch \\
1044 -eval \"(setq diary-mail-days 3 \\
1045 diary-file \\\"/path/to/diary.file\\\" \\
1046 european-calendar-style t \\
1047 diary-mail-addr \\\"user@host.name\\\" )\" \\
1048 -l diary-lib -f diary-mail-entries
1049 at -f diary-rem.sh 0200 tomorrow
1051 You may have to tweak the syntax of the `at' command to suit your
1052 system. Alternatively, you can specify a cron entry:
1053 0 1 * * * diary-rem.sh
1054 to run it every morning at 1am."
1055 (interactive "P")
1056 (if (string-equal diary-mail-addr "")
1057 (error "You must set `diary-mail-addr' to use this command")
1058 (let ((diary-display-hook 'fancy-diary-display))
1059 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days)))
1060 (compose-mail diary-mail-addr
1061 (concat "Diary entries generated "
1062 (calendar-date-string (calendar-current-date))))
1063 (insert
1064 (if (get-buffer fancy-diary-buffer)
1065 (with-current-buffer fancy-diary-buffer (buffer-string))
1066 "No entries found"))
1067 (call-interactively (get mail-user-agent 'sendfunc))))
1069 (defun diary-name-pattern (string-array &optional abbrev-array paren)
1070 "Return a regexp matching the strings in the array STRING-ARRAY.
1071 If the optional argument ABBREV-ARRAY is present, then the function
1072 `calendar-abbrev-construct' is used to construct abbreviations from the
1073 two supplied arrays. The returned regexp will then also match these
1074 abbreviations, with or without final `.' characters. If the optional
1075 argument PAREN is non-nil, the regexp is surrounded by parentheses."
1076 (regexp-opt (append string-array
1077 (if abbrev-array
1078 (calendar-abbrev-construct abbrev-array
1079 string-array))
1080 (if abbrev-array
1081 (calendar-abbrev-construct abbrev-array
1082 string-array
1083 'period))
1084 nil)
1085 paren))
1087 (defvar marking-diary-entries nil
1088 "True during the marking of diary entries, nil otherwise.")
1090 (defvar marking-diary-entry nil
1091 "True during the marking of diary entries, if current entry is marking.")
1093 ;; file-glob-attrs bound in mark-diary-entries.
1094 (defun diary-mark-entries-1 (markfunc &optional months symbol absfunc)
1095 "Mark diary entries of a certain type.
1096 MARKFUNC is a function that marks entries of the appropriate type
1097 matching a given date pattern. MONTHS is an array of month names.
1098 SYMBOL marks diary entries of the type in question. ABSFUNC is a
1099 function that converts absolute dates to dates of the appropriate type. "
1100 (let ((dayname (diary-name-pattern calendar-day-name-array
1101 calendar-day-abbrev-array))
1102 (monthname (format "%s\\|\\*"
1103 (if months
1104 (diary-name-pattern months)
1105 (diary-name-pattern calendar-month-name-array
1106 calendar-month-abbrev-array))))
1107 (month "[0-9]+\\|\\*")
1108 (day "[0-9]+\\|\\*")
1109 (year "[0-9]+\\|\\*")
1110 (case-fold-search t)
1111 ;; FIXME is this the right reason for 1 versus 2?
1112 ;; Should docs of symbols say must be single character?
1113 (inc (if symbol 2 1))
1114 marks)
1115 (dolist (date-form diary-date-forms)
1116 (if (eq (car date-form) 'backup) ; ignore 'backup directive
1117 (setq date-form (cdr date-form)))
1118 (let* ((l (length date-form))
1119 (d-name-pos (- l (length (memq 'dayname date-form))))
1120 (d-name-pos (if (/= l d-name-pos) (+ inc d-name-pos)))
1121 (m-name-pos (- l (length (memq 'monthname date-form))))
1122 (m-name-pos (if (/= l m-name-pos) (+ inc m-name-pos)))
1123 (d-pos (- l (length (memq 'day date-form))))
1124 (d-pos (if (/= l d-pos) (+ inc d-pos)))
1125 (m-pos (- l (length (memq 'month date-form))))
1126 (m-pos (if (/= l m-pos) (+ inc m-pos)))
1127 (y-pos (- l (length (memq 'year date-form))))
1128 (y-pos (if (/= l y-pos) (+ inc y-pos)))
1129 (regexp (format "^%s\\(%s\\)"
1130 (if symbol (regexp-quote symbol) "")
1131 (mapconcat 'eval date-form "\\)\\("))))
1132 (goto-char (point-min))
1133 (while (re-search-forward regexp nil t)
1134 (let* ((dd-name
1135 (if d-name-pos
1136 (match-string-no-properties d-name-pos)))
1137 (mm-name
1138 (if m-name-pos
1139 (match-string-no-properties m-name-pos)))
1140 (mm (string-to-number
1141 (if m-pos
1142 (match-string-no-properties m-pos)
1143 "")))
1144 (dd (string-to-number
1145 (if d-pos
1146 (match-string-no-properties d-pos)
1147 "")))
1148 (y-str (if y-pos
1149 (match-string-no-properties y-pos)))
1150 (yy (if (not y-str)
1152 (if (and (= (length y-str) 2)
1153 abbreviated-calendar-year)
1154 (let* ((current-y
1155 (extract-calendar-year
1156 (if absfunc
1157 (funcall
1158 absfunc
1159 (calendar-absolute-from-gregorian
1160 (calendar-current-date)))
1161 (calendar-current-date))))
1162 (y (+ (string-to-number y-str)
1163 (* 100 (/ current-y 100)))))
1164 (if (> (- y current-y) 50)
1165 (- y 100)
1166 (if (> (- current-y y) 50)
1167 (+ y 100)
1168 y)))
1169 (string-to-number y-str)))))
1170 (setq marks (cadr (diary-pull-attrs
1171 (buffer-substring-no-properties
1172 (point) (line-end-position))
1173 file-glob-attrs)))
1174 (if dd-name
1175 (mark-calendar-days-named
1176 (cdr (assoc-string dd-name
1177 (calendar-make-alist
1178 calendar-day-name-array
1179 0 nil calendar-day-abbrev-array) t)) marks)
1180 (if mm-name
1181 (setq mm
1182 (if (string-equal mm-name "*") 0
1183 (cdr (assoc-string
1184 mm-name
1185 (if months (calendar-make-alist months)
1186 (calendar-make-alist
1187 calendar-month-name-array
1188 1 nil calendar-month-abbrev-array)) t)))))
1189 (funcall markfunc mm dd yy marks))))))))
1191 ;;;###cal-autoload
1192 (defun mark-diary-entries (&optional redraw)
1193 "Mark days in the calendar window that have diary entries.
1194 Each entry in the diary file visible in the calendar window is
1195 marked. After the entries are marked, the hooks
1196 `nongregorian-diary-marking-hook' and `mark-diary-entries-hook'
1197 are run. If the optional argument REDRAW is non-nil (which is
1198 the case interactively, for example) then any existing diary
1199 marks are first removed. This is intended to deal with deleted
1200 diary entries."
1201 (interactive "p")
1202 ;; To remove any deleted diary entries. Do not redraw when:
1203 ;; i) processing #include diary files (else only get the marks from
1204 ;; the last #include file processed).
1205 ;; ii) called via calendar-redraw (since calendar has already been
1206 ;; erased).
1207 ;; Use of REDRAW handles both of these cases.
1208 (when (and redraw mark-diary-entries-in-calendar)
1209 (setq mark-diary-entries-in-calendar nil)
1210 (redraw-calendar))
1211 (let ((marking-diary-entries t)
1212 file-glob-attrs)
1213 (with-current-buffer (find-file-noselect (diary-check-diary-file) t)
1214 (save-excursion
1215 (when (eq major-mode default-major-mode) (diary-mode))
1216 (setq mark-diary-entries-in-calendar t)
1217 (message "Marking diary entries...")
1218 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1219 (with-syntax-table diary-syntax-table
1220 (diary-mark-entries-1 'mark-calendar-date-pattern)
1221 (mark-sexp-diary-entries)
1222 (run-hooks 'nongregorian-diary-marking-hook
1223 'mark-diary-entries-hook))
1224 (message "Marking diary entries...done")))))
1227 (defun diary-sexp-entry (sexp entry date)
1228 "Process a SEXP diary ENTRY for DATE."
1229 (let ((result (if calendar-debug-sexp
1230 (let ((stack-trace-on-error t))
1231 (eval (car (read-from-string sexp))))
1232 (condition-case nil
1233 (eval (car (read-from-string sexp)))
1234 (error
1235 (beep)
1236 (message "Bad sexp at line %d in %s: %s"
1237 (count-lines (point-min) (point))
1238 diary-file sexp)
1239 (sleep-for 2))))))
1240 (cond ((stringp result) result)
1241 ((and (consp result)
1242 (stringp (cdr result))) result)
1243 (result entry)
1244 (t nil))))
1246 (defvar displayed-year) ; bound in generate-calendar
1247 (defvar displayed-month)
1249 (defun mark-sexp-diary-entries ()
1250 "Mark days in the calendar window that have sexp diary entries.
1251 Each entry in the diary file (or included files) visible in the calendar window
1252 is marked. See the documentation for the function `list-sexp-diary-entries'."
1253 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
1254 (s-entry (format "^\\(%s(\\)\\|\\(%s%s(diary-remind\\)" sexp-mark
1255 (regexp-quote diary-nonmarking-symbol)
1256 sexp-mark))
1257 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1258 m y first-date last-date mark file-glob-attrs)
1259 (with-current-buffer calendar-buffer
1260 (setq m displayed-month
1261 y displayed-year))
1262 (increment-calendar-month m y -1)
1263 (setq first-date
1264 (calendar-absolute-from-gregorian (list m 1 y)))
1265 (increment-calendar-month m y 2)
1266 (setq last-date
1267 (calendar-absolute-from-gregorian
1268 (list m (calendar-last-day-of-month m y) y)))
1269 (goto-char (point-min))
1270 (while (re-search-forward s-entry nil t)
1271 (setq marking-diary-entry (char-equal (preceding-char) ?\())
1272 (re-search-backward "(")
1273 (let ((sexp-start (point))
1274 sexp entry entry-start marks)
1275 (forward-sexp)
1276 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1277 (forward-char 1)
1278 (if (and (bolp) (not (looking-at "[ \t]")))
1279 ;; Diary entry consists only of the sexp.
1280 (progn
1281 (backward-char 1)
1282 (setq entry ""))
1283 (setq entry-start (point))
1284 ;; Find end of entry.
1285 (forward-line 1)
1286 (while (looking-at "[ \t]")
1287 (forward-line 1))
1288 (if (bolp) (backward-char 1))
1289 (setq entry (buffer-substring-no-properties entry-start (point))))
1290 (calendar-for-loop date from first-date to last-date do
1291 (when (setq mark (diary-sexp-entry
1292 sexp entry
1293 (calendar-gregorian-from-absolute date)))
1294 ;; FIXME does this make sense?
1295 (setq marks (diary-pull-attrs entry file-glob-attrs)
1296 marks (nth 1 (diary-pull-attrs entry file-glob-attrs)))
1297 (mark-visible-calendar-date
1298 (calendar-gregorian-from-absolute date)
1299 (if (< 0 (length marks))
1300 marks
1301 (if (consp mark) (car mark))))))))))
1303 (defun mark-included-diary-files ()
1304 "Mark the diary entries from other diary files with those of the diary file.
1305 This function is suitable for use with `mark-diary-entries-hook'; it enables
1306 you to use shared diary files together with your own. The files included are
1307 specified in the `diary-file' by lines of this form:
1308 #include \"filename\"
1309 This is recursive; that is, #include directives in diary files thus included
1310 are obeyed. You can change the `#include' to some other string by
1311 changing the variable `diary-include-string'."
1312 (goto-char (point-min))
1313 (while (re-search-forward
1314 (format "^%s \"\\([^\"]*\\)\"" (regexp-quote diary-include-string))
1315 nil t)
1316 (let* ((diary-file (substitute-in-file-name
1317 (match-string-no-properties 1)))
1318 (mark-diary-entries-hook 'mark-included-diary-files)
1319 (dbuff (find-buffer-visiting diary-file)))
1320 (if (file-exists-p diary-file)
1321 (if (file-readable-p diary-file)
1322 (progn
1323 (mark-diary-entries)
1324 (unless dbuff
1325 (kill-buffer (find-buffer-visiting diary-file))))
1326 (beep)
1327 (message "Can't read included diary file %s" diary-file)
1328 (sleep-for 2))
1329 (beep)
1330 (message "Can't find included diary file %s" diary-file)
1331 (sleep-for 2))))
1332 (goto-char (point-min)))
1334 (defun mark-calendar-days-named (dayname &optional color)
1335 "Mark all dates in the calendar window that are day DAYNAME of the week.
1336 0 means all Sundays, 1 means all Mondays, and so on.
1337 Optional argument COLOR is passed to `mark-visible-calendar-date' as MARK."
1338 (with-current-buffer calendar-buffer
1339 (let ((prev-month displayed-month)
1340 (prev-year displayed-year)
1341 (succ-month displayed-month)
1342 (succ-year displayed-year)
1343 (last-day)
1344 (day))
1345 (increment-calendar-month succ-month succ-year 1)
1346 (increment-calendar-month prev-month prev-year -1)
1347 (setq day (calendar-absolute-from-gregorian
1348 (calendar-nth-named-day 1 dayname prev-month prev-year))
1349 last-day (calendar-absolute-from-gregorian
1350 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1351 (while (<= day last-day)
1352 (mark-visible-calendar-date (calendar-gregorian-from-absolute day)
1353 color)
1354 (setq day (+ day 7))))))
1356 (defun mark-calendar-month (month year p-month p-day p-year &optional color)
1357 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P-DAY/P-YEAR.
1358 A value of 0 in any position of the pattern is a wildcard.
1359 Optional argument COLOR is passed to `mark-visible-calendar-date' as MARK."
1360 (if (or (and (= month p-month)
1361 (or (zerop p-year) (= year p-year)))
1362 (and (zerop p-month)
1363 (or (zerop p-year) (= year p-year))))
1364 (if (zerop p-day)
1365 (dotimes (i (calendar-last-day-of-month month year))
1366 (mark-visible-calendar-date (list month (1+ i) year) color))
1367 (mark-visible-calendar-date (list month p-day year) color))))
1369 (defun mark-calendar-date-pattern (month day year &optional color)
1370 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1371 A value of 0 in any position is a wildcard. Optional argument COLOR is
1372 passed to `mark-visible-calendar-date' as MARK."
1373 (with-current-buffer calendar-buffer
1374 (let ((m displayed-month)
1375 (y displayed-year))
1376 (increment-calendar-month m y -1)
1377 (dotimes (idummy 3)
1378 (mark-calendar-month m y month day year color)
1379 (increment-calendar-month m y 1)))))
1382 ;; Bahai, Hebrew, Islamic.
1383 (defun calendar-mark-complex (month day year fromabs &optional color)
1384 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1385 The function FROMABS converts absolute dates to the appropriate date system.
1386 Optional argument COLOR is passed to `mark-visible-calendar-date' as MARK."
1387 ;; Not one of the simple cases--check all visible dates for match.
1388 ;; Actually, the following code takes care of ALL of the cases, but
1389 ;; it's much too slow to be used for the simple (common) cases.
1390 (let ((m displayed-month)
1391 (y displayed-year)
1392 first-date last-date)
1393 (increment-calendar-month m y -1)
1394 (setq first-date (calendar-absolute-from-gregorian (list m 1 y)))
1395 (increment-calendar-month m y 2)
1396 (setq last-date (calendar-absolute-from-gregorian
1397 (list m (calendar-last-day-of-month m y) y)))
1398 (calendar-for-loop date from first-date to last-date do
1399 (let* ((i-date (funcall fromabs date))
1400 (i-month (extract-calendar-month i-date))
1401 (i-day (extract-calendar-day i-date))
1402 (i-year (extract-calendar-year i-date)))
1403 (and (or (zerop month)
1404 (= month i-month))
1405 (or (zerop day)
1406 (= day i-day))
1407 (or (zerop year)
1408 (= year i-year))
1409 (mark-visible-calendar-date
1410 (calendar-gregorian-from-absolute date) color))))))
1412 ;; Bahai, Islamic.
1413 (defun calendar-mark-1 (month day year fromabs toabs &optional color)
1414 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1415 The function FROMABS converts absolute dates to the appropriate date system.
1416 The function TOABDS carries out the inverse operation. Optional argument
1417 COLOR is passed to `mark-visible-calendar-date' as MARK."
1418 (save-excursion
1419 (set-buffer calendar-buffer)
1420 (if (and (not (zerop month)) (not (zerop day)))
1421 (if (not (zerop year))
1422 ;; Fully specified date.
1423 (let ((date (calendar-gregorian-from-absolute
1424 (funcall toabs (list month day year)))))
1425 (if (calendar-date-is-visible-p date)
1426 (mark-visible-calendar-date date color)))
1427 ;; Month and day in any year--this taken from the holiday stuff.
1428 (let* ((i-date (funcall fromabs
1429 (calendar-absolute-from-gregorian
1430 (list displayed-month 15 displayed-year))))
1431 (m (extract-calendar-month i-date))
1432 (y (extract-calendar-year i-date))
1433 date)
1434 (unless (< m 1) ; calendar doesn't apply
1435 (increment-calendar-month m y (- 10 month))
1436 (if (> m 7) ; date might be visible
1437 (let ((date (calendar-gregorian-from-absolute
1438 (funcall toabs (list month day y)))))
1439 (if (calendar-date-is-visible-p date)
1440 (mark-visible-calendar-date date color)))))))
1441 (calendar-mark-complex month day year
1442 'calendar-bahai-from-absolute color))))
1445 (defun diary-entry-time (s)
1446 "Return time at the beginning of the string S as a military-style integer.
1447 For example, returns 1325 for 1:25pm.
1449 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1450 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1451 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm, or XX:XXPM. A period (.) can
1452 be used instead of a colon (:) to separate the hour and minute parts."
1453 (let ((case-fold-search nil))
1454 (cond ((string-match ; military time
1455 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1457 (+ (* 100 (string-to-number (match-string 1 s)))
1458 (string-to-number (match-string 2 s))))
1459 ((string-match ; hour only (XXam or XXpm)
1460 "\\`[ \t\n]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1461 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1462 (if (equal ?a (downcase (aref s (match-beginning 2))))
1463 0 1200)))
1464 ((string-match ; hour and minute (XX:XXam or XX:XXpm)
1465 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1466 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1467 (string-to-number (match-string 2 s))
1468 (if (equal ?a (downcase (aref s (match-beginning 3))))
1469 0 1200)))
1470 (t diary-unknown-time)))) ; unrecognizable
1472 (defun diary-entry-compare (e1 e2)
1473 "Return t if E1 is earlier than E2."
1474 (or (calendar-date-compare e1 e2)
1475 (and (calendar-date-equal (car e1) (car e2))
1476 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1477 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1478 (or (< t1 t2)
1479 (and (= t1 t2)
1480 (string-lessp ts1 ts2)))))))
1482 (defun sort-diary-entries ()
1483 "Sort the list of diary entries by time of day."
1484 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1487 (defun list-sexp-diary-entries (date)
1488 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1489 Also, make them visible in the diary file. Returns t if any entries were
1490 found.
1492 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1493 `%%'). The form of a sexp diary entry is
1495 %%(SEXP) ENTRY
1497 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1498 SEXP yields the value nil, the diary entry does not apply. If it yields a
1499 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1500 string, that string will be the diary entry in the fancy diary display.
1502 For example, the following diary entry will apply to the 21st of the month
1503 if it is a weekday and the Friday before if the 21st is on a weekend:
1505 &%%(let ((dayname (calendar-day-of-week date))
1506 (day (extract-calendar-day date)))
1508 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1509 (and (memq day '(19 20)) (= dayname 5)))
1510 ) UIUC pay checks deposited
1512 A number of built-in functions are available for this type of diary entry:
1514 %%(diary-date MONTH DAY YEAR &optional MARK) text
1515 Entry applies if date is MONTH, DAY, YEAR if
1516 `european-calendar-style' is nil (otherwise DAY, MONTH,
1517 YEAR). DAY, MONTH, and YEAR can be lists of integers,
1518 `t' (meaning all values), or an integer. An optional
1519 parameter MARK specifies a face or single-character string
1520 to use when highlighting the day in the calendar.
1522 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1523 Entry will appear on the Nth DAYNAME of MONTH.
1524 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1525 if N is negative it counts backward from the end of
1526 the month. MONTH can be a list of months, a single
1527 month, or t to specify all months. Optional DAY means
1528 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1529 to 1 if N>0 and the last day of the month if N<0. An
1530 optional parameter MARK specifies a face or single-character
1531 string to use when highlighting the day in the calendar.
1533 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1534 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1535 inclusive. (If `european-calendar-style' is non-nil, the
1536 order of the parameters should be changed to D1, M1, Y1,
1537 D2, M2, Y2.) An optional parameter MARK specifies a face
1538 or single-character string to use when highlighting the
1539 day in the calendar.
1541 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1542 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1543 (If `european-calendar-style' is non-nil, the order of the
1544 parameters should be changed to DAY, MONTH, YEAR.) Text
1545 can contain %d or %d%s; %d will be replaced by the number
1546 of years since the MONTH DAY, YEAR and %s will be replaced
1547 by the ordinal ending of that number (that is, `st', `nd',
1548 `rd' or `th', as appropriate. The anniversary of February
1549 29 is considered to be March 1 in a non-leap year. An
1550 optional parameter MARK specifies a face or single-character
1551 string to use when highlighting the day in the calendar.
1553 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1554 Entry will appear every N days, starting MONTH DAY, YEAR.
1555 (If `european-calendar-style' is non-nil, the order of the
1556 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1557 can contain %d or %d%s; %d will be replaced by the number
1558 of repetitions since the MONTH DAY, YEAR and %s will
1559 be replaced by the ordinal ending of that number (that is,
1560 `st', `nd', `rd' or `th', as appropriate. An optional
1561 parameter MARK specifies a face or single-character string
1562 to use when highlighting the day in the calendar.
1564 %%(diary-remind SEXP DAYS &optional MARKING) text
1565 Entry is a reminder for diary sexp SEXP. DAYS is either a
1566 single number or a list of numbers indicating the number(s)
1567 of days before the event that the warning(s) should occur.
1568 If the current date is (one of) DAYS before the event
1569 indicated by EXPR, then a suitable message (as specified
1570 by `diary-remind-message') appears. In addition to the
1571 reminders beforehand, the diary entry also appears on
1572 the date itself. If optional MARKING is non-nil then the
1573 *reminders* are marked on the calendar. Marking of
1574 reminders is independent of whether the entry *itself* is
1575 a marking or nonmarking one.
1577 %%(diary-day-of-year)
1578 Diary entries giving the day of the year and the number of
1579 days remaining in the year will be made every day. Note
1580 that since there is no text, it makes sense only if the
1581 fancy diary display is used.
1583 %%(diary-iso-date)
1584 Diary entries giving the corresponding ISO commercial date
1585 will be made every day. Note that since there is no text,
1586 it makes sense only if the fancy diary display is used.
1588 %%(diary-french-date)
1589 Diary entries giving the corresponding French Revolutionary
1590 date will be made every day. Note that since there is no
1591 text, it makes sense only if the fancy diary display is used.
1593 %%(diary-islamic-date)
1594 Diary entries giving the corresponding Islamic date will be
1595 made every day. Note that since there is no text, it
1596 makes sense only if the fancy diary display is used.
1598 %%(diary-hebrew-date)
1599 Diary entries giving the corresponding Hebrew date will be
1600 made every day. Note that since there is no text, it
1601 makes sense only if the fancy diary display is used.
1603 %%(diary-astro-day-number) Diary entries giving the corresponding
1604 astronomical (Julian) day number will be made every day.
1605 Note that since there is no text, it makes sense only if the
1606 fancy diary display is used.
1608 %%(diary-julian-date) Diary entries giving the corresponding
1609 Julian date will be made every day. Note that since
1610 there is no text, it makes sense only if the fancy diary
1611 display is used.
1613 %%(diary-sunrise-sunset)
1614 Diary entries giving the local times of sunrise and sunset
1615 will be made every day. Note that since there is no text,
1616 it makes sense only if the fancy diary display is used.
1617 Floating point required.
1619 %%(diary-phases-of-moon)
1620 Diary entries giving the times of the phases of the moon
1621 will be when appropriate. Note that since there is no text,
1622 it makes sense only if the fancy diary display is used.
1623 Floating point required.
1625 %%(diary-yahrzeit MONTH DAY YEAR) text
1626 Text is assumed to be the name of the person; the date is
1627 the date of death on the *civil* calendar. The diary entry
1628 will appear on the proper Hebrew-date anniversary and on the
1629 day before. (If `european-calendar-style' is non-nil, the
1630 parameter order should be changed to DAY, MONTH, YEAR.)
1632 %%(diary-rosh-hodesh)
1633 Diary entries will be made on the dates of Rosh Hodesh on
1634 the Hebrew calendar. Note that since there is no text, it
1635 makes sense only if the fancy diary display is used.
1637 %%(diary-parasha)
1638 Diary entries giving the weekly parasha will be made on
1639 every Saturday. Note that since there is no text, it
1640 makes sense only if the fancy diary display is used.
1642 %%(diary-omer)
1643 Diary entries giving the omer count will be made every day
1644 from Passover to Shavuot. Note that since there is no text,
1645 it makes sense only if the fancy diary display is used.
1647 Marking these entries is *extremely* time consuming, so these entries are
1648 best if they are nonmarking."
1649 (let ((s-entry (format "^%s?%s(" (regexp-quote diary-nonmarking-symbol)
1650 (regexp-quote sexp-diary-entry-symbol)))
1651 entry-found file-glob-attrs marks)
1652 (goto-char (point-min))
1653 (save-excursion
1654 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '()))))
1655 (while (re-search-forward s-entry nil t)
1656 (backward-char 1)
1657 (let ((sexp-start (point))
1658 sexp entry specifier entry-start line-start)
1659 (forward-sexp)
1660 (setq sexp (buffer-substring-no-properties sexp-start (point))
1661 line-start (line-end-position 0)
1662 specifier
1663 (buffer-substring-no-properties (1+ line-start) (point))
1664 entry-start (1+ line-start))
1665 (forward-char 1)
1666 (if (and (bolp) (not (looking-at "[ \t]")))
1667 ;; Diary entry consists only of the sexp.
1668 (progn
1669 (backward-char 1)
1670 (setq entry ""))
1671 (setq entry-start (point))
1672 (forward-line 1)
1673 (while (looking-at "[ \t]")
1674 (forward-line 1))
1675 (backward-char 1)
1676 (setq entry (buffer-substring-no-properties entry-start (point))))
1677 (let ((diary-entry (diary-sexp-entry sexp entry date))
1678 temp literal)
1679 (setq literal entry ; before evaluation
1680 entry (if (consp diary-entry)
1681 (cdr diary-entry)
1682 diary-entry))
1683 (when diary-entry
1684 (remove-overlays line-start (point) 'invisible 'diary)
1685 (if (< 0 (length entry))
1686 (setq temp (diary-pull-attrs entry file-glob-attrs)
1687 entry (nth 0 temp)
1688 marks (nth 1 temp))))
1689 (add-to-diary-list date
1690 entry
1691 specifier
1692 (if entry-start (copy-marker entry-start))
1693 marks
1694 literal)
1695 (setq entry-found (or entry-found diary-entry)))))
1696 entry-found))
1699 ;;; Sexp diary functions.
1701 (defvar date)
1702 (defvar entry)
1704 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1705 (defun diary-date (month day year &optional mark)
1706 "Specific date(s) diary entry.
1707 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1708 and DAY, MONTH, YEAR otherwise. DAY, MONTH, and YEAR can be lists of
1709 integers, `t' (meaning all values), or an integer.
1711 An optional parameter MARK specifies a face or single-character string to
1712 use when highlighting the day in the calendar."
1713 (let ((dd (if european-calendar-style
1714 month
1715 day))
1716 (mm (if european-calendar-style
1718 month))
1719 (m (extract-calendar-month date))
1720 (y (extract-calendar-year date))
1721 (d (extract-calendar-day date)))
1722 (if (and
1723 (or (and (listp dd) (memq d dd))
1724 (equal d dd)
1725 (eq dd t))
1726 (or (and (listp mm) (memq m mm))
1727 (equal m mm)
1728 (eq mm t))
1729 (or (and (listp year) (memq y year))
1730 (equal y year)
1731 (eq year t)))
1732 (cons mark entry))))
1734 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1735 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1736 "Block diary entry.
1737 Entry applies if date is between, or on one of, two dates.
1738 The order of the parameters is M1, D1, Y1, M2, D2, Y2 if
1739 `european-calendar-style' is nil, and D1, M1, Y1, D2, M2, Y2 otherwise.
1741 An optional parameter MARK specifies a face or single-character string to
1742 use when highlighting the day in the calendar."
1744 (let ((date1 (calendar-absolute-from-gregorian
1745 (if european-calendar-style
1746 (list d1 m1 y1)
1747 (list m1 d1 y1))))
1748 (date2 (calendar-absolute-from-gregorian
1749 (if european-calendar-style
1750 (list d2 m2 y2)
1751 (list m2 d2 y2))))
1752 (d (calendar-absolute-from-gregorian date)))
1753 (if (and (<= date1 d) (<= d date2))
1754 (cons mark entry))))
1756 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1757 (defun diary-float (month dayname n &optional day mark)
1758 "Floating diary entry--entry applies if date is the nth dayname of month.
1759 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, an integer,
1760 or `t' (meaning all months). If N is negative, count backward from the end
1761 of the month.
1763 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1764 Optional MARK specifies a face or single-character string to use when
1765 highlighting the day in the calendar."
1766 ;; This is messy because the diary entry may apply, but the date on which it
1767 ;; is based can be in a different month/year. For example, asking for the
1768 ;; first Monday after December 30. For large values of |n| the problem is
1769 ;; more grotesque.
1770 (and (= dayname (calendar-day-of-week date))
1771 (let* ((m (extract-calendar-month date))
1772 (d (extract-calendar-day date))
1773 (y (extract-calendar-year date))
1774 ;; Last (n>0) or first (n<0) possible base date for entry.
1775 (limit
1776 (calendar-nth-named-absday (- n) dayname m y d))
1777 (last-abs (if (> n 0) limit (+ limit 6)))
1778 (first-abs (if (> n 0) (- limit 6) limit))
1779 (last (calendar-gregorian-from-absolute last-abs))
1780 (first (calendar-gregorian-from-absolute first-abs))
1781 ;; m1, d1 is first possible base date.
1782 (m1 (extract-calendar-month first))
1783 (d1 (extract-calendar-day first))
1784 (y1 (extract-calendar-year first))
1785 ;; m2, d2 is last possible base date.
1786 (m2 (extract-calendar-month last))
1787 (d2 (extract-calendar-day last))
1788 (y2 (extract-calendar-year last)))
1789 (if (or (and (= m1 m2) ; only possible base dates in one month
1790 (or (eq month t)
1791 (if (listp month)
1792 (memq m1 month)
1793 (= m1 month)))
1794 (let ((d (or day (if (> n 0)
1796 (calendar-last-day-of-month m1 y1)))))
1797 (and (<= d1 d) (<= d d2))))
1798 ;; Only possible base dates straddle two months.
1799 (and (or (< y1 y2)
1800 (and (= y1 y2) (< m1 m2)))
1802 ;; m1, d1 works as a base date.
1803 (and
1804 (or (eq month t)
1805 (if (listp month)
1806 (memq m1 month)
1807 (= m1 month)))
1808 (<= d1 (or day (if (> n 0)
1810 (calendar-last-day-of-month m1 y1)))))
1811 ;; m2, d2 works as a base date.
1812 (and (or (eq month t)
1813 (if (listp month)
1814 (memq m2 month)
1815 (= m2 month)))
1816 (<= (or day (if (> n 0)
1818 (calendar-last-day-of-month m2 y2)))
1819 d2)))))
1820 (cons mark entry)))))
1822 (defun diary-ordinal-suffix (n)
1823 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1824 (if (or (memq (% n 100) '(11 12 13))
1825 (< 3 (% n 10)))
1826 "th"
1827 (aref ["th" "st" "nd" "rd"] (% n 10))))
1829 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1830 (defun diary-anniversary (month day &optional year mark)
1831 "Anniversary diary entry.
1832 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1833 `european-calendar-style' is nil, and DAY, MONTH, YEAR otherwise. The
1834 diary entry can contain `%d' or `%d%s'; the %d will be replaced by the
1835 number of years since the MONTH DAY, YEAR and the %s will be replaced by
1836 the ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1837 appropriate. The anniversary of February 29 is considered to be March 1
1838 in non-leap years.
1840 An optional parameter MARK specifies a face or single-character string to
1841 use when highlighting the day in the calendar."
1842 (let* ((d (if european-calendar-style
1843 month
1844 day))
1845 (m (if european-calendar-style
1847 month))
1848 (y (extract-calendar-year date))
1849 (diff (if year (- y year) 100)))
1850 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1851 (setq m 3
1852 d 1))
1853 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1854 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
1856 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1857 (defun diary-cyclic (n month day year &optional mark)
1858 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1859 If `european-calendar-style' is non-nil, parameters are N, DAY, MONTH, YEAR.
1860 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1861 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1862 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1863 appropriate.
1865 An optional parameter MARK specifies a face or single-character string to
1866 use when highlighting the day in the calendar."
1867 (let* ((d (if european-calendar-style
1868 month
1869 day))
1870 (m (if european-calendar-style
1872 month))
1873 (diff (- (calendar-absolute-from-gregorian date)
1874 (calendar-absolute-from-gregorian
1875 (list m d year))))
1876 (cycle (/ diff n)))
1877 (if (and (>= diff 0) (zerop (% diff n)))
1878 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
1880 (defun diary-day-of-year ()
1881 "Day of year and number of days remaining in the year of date diary entry."
1882 (calendar-day-of-year-string date))
1884 (defun diary-remind (sexp days &optional marking)
1885 "Provide a reminder of a diary entry.
1886 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1887 indicating the number(s) of days before the event that the warning(s) should
1888 occur on. If the current date is (one of) DAYS before the event indicated by
1889 SEXP, then a suitable message (as specified by `diary-remind-message' is
1890 returned.
1892 In addition to the reminders beforehand, the diary entry also appears on the
1893 date itself.
1895 A `diary-nonmarking-symbol' at the beginning of the line of the `diary-remind'
1896 entry specifies that the diary entry (not the reminder) is non-marking.
1897 Marking of reminders is independent of whether the entry itself is a marking
1898 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1899 marked on the calendar."
1900 (let ((diary-entry (eval sexp)))
1901 (cond
1902 ;; Diary entry applies on date.
1903 ((and diary-entry
1904 (or (not marking-diary-entries) marking-diary-entry))
1905 diary-entry)
1906 ;; Diary entry may apply to `days' before date.
1907 ((and (integerp days)
1908 (not diary-entry) ; diary entry does not apply to date
1909 (or (not marking-diary-entries) marking))
1910 (let ((date (calendar-gregorian-from-absolute
1911 (+ (calendar-absolute-from-gregorian date) days))))
1912 (when (setq diary-entry (eval sexp)) ; re-evaluate with adjusted date
1913 ;; Discard any mark portion from diary-anniversary, etc.
1914 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
1915 (mapconcat 'eval diary-remind-message ""))))
1916 ;; Diary entry may apply to one of a list of days before date.
1917 ((and (listp days) days)
1918 (or (diary-remind sexp (car days) marking)
1919 (diary-remind sexp (cdr days) marking))))))
1922 ;;; Diary insertion functions.
1924 ;;;###cal-autoload
1925 (defun make-diary-entry (string &optional nonmarking file)
1926 "Insert a diary entry STRING which may be NONMARKING in FILE.
1927 If omitted, NONMARKING defaults to nil and FILE defaults to
1928 `diary-file'."
1929 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1930 (find-file-other-window (substitute-in-file-name (or file diary-file))))
1931 (when (eq major-mode default-major-mode) (diary-mode))
1932 (widen)
1933 (diary-unhide-everything)
1934 (goto-char (point-max))
1935 ;; FIXME cf hack-local-variables.
1936 (when (let ((case-fold-search t))
1937 (search-backward "Local Variables:"
1938 (max (- (point-max) 3000) (point-min))
1940 (beginning-of-line)
1941 (insert "\n")
1942 (forward-line -1))
1943 (insert
1944 (if (bolp) "" "\n")
1945 (if nonmarking diary-nonmarking-symbol "")
1946 string " "))
1948 ;;;###cal-autoload
1949 (defun insert-diary-entry (arg)
1950 "Insert a diary entry for the date indicated by point.
1951 Prefix argument ARG makes the entry nonmarking."
1952 (interactive "P")
1953 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1954 arg))
1956 ;;;###cal-autoload
1957 (defun insert-weekly-diary-entry (arg)
1958 "Insert a weekly diary entry for the day of the week indicated by point.
1959 Prefix argument ARG makes the entry nonmarking."
1960 (interactive "P")
1961 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1962 arg))
1964 ;;;###cal-autoload
1965 (defun insert-monthly-diary-entry (arg)
1966 "Insert a monthly diary entry for the day of the month indicated by point.
1967 Prefix argument ARG makes the entry nonmarking."
1968 (interactive "P")
1969 (let ((calendar-date-display-form
1970 (if european-calendar-style
1971 '(day " * ")
1972 '("* " day))))
1973 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1974 arg)))
1976 ;;;###cal-autoload
1977 (defun insert-yearly-diary-entry (arg)
1978 "Insert an annual diary entry for the day of the year indicated by point.
1979 Prefix argument ARG makes the entry nonmarking."
1980 (interactive "P")
1981 (let ((calendar-date-display-form
1982 (if european-calendar-style
1983 '(day " " monthname)
1984 '(monthname " " day))))
1985 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1986 arg)))
1988 ;;;###cal-autoload
1989 (defun insert-anniversary-diary-entry (arg)
1990 "Insert an anniversary diary entry for the date given by point.
1991 Prefix argument ARG makes the entry nonmarking."
1992 (interactive "P")
1993 (let ((calendar-date-display-form
1994 (if european-calendar-style
1995 '(day " " month " " year)
1996 '(month " " day " " year))))
1997 (make-diary-entry
1998 (format "%s(diary-anniversary %s)"
1999 sexp-diary-entry-symbol
2000 (calendar-date-string (calendar-cursor-to-date t) nil t))
2001 arg)))
2003 ;;;###cal-autoload
2004 (defun insert-block-diary-entry (arg)
2005 "Insert a block diary entry for the days between the point and marked date.
2006 Prefix argument ARG makes the entry nonmarking."
2007 (interactive "P")
2008 (let ((calendar-date-display-form
2009 (if european-calendar-style
2010 '(day " " month " " year)
2011 '(month " " day " " year)))
2012 (cursor (calendar-cursor-to-date t))
2013 (mark (or (car calendar-mark-ring)
2014 (error "No mark set in this buffer")))
2015 start end)
2016 (if (< (calendar-absolute-from-gregorian mark)
2017 (calendar-absolute-from-gregorian cursor))
2018 (setq start mark
2019 end cursor)
2020 (setq start cursor
2021 end mark))
2022 (make-diary-entry
2023 (format "%s(diary-block %s %s)"
2024 sexp-diary-entry-symbol
2025 (calendar-date-string start nil t)
2026 (calendar-date-string end nil t))
2027 arg)))
2029 ;;;###cal-autoload
2030 (defun insert-cyclic-diary-entry (arg)
2031 "Insert a cyclic diary entry starting at the date given by point.
2032 Prefix argument ARG makes the entry nonmarking."
2033 (interactive "P")
2034 (let ((calendar-date-display-form
2035 (if european-calendar-style
2036 '(day " " month " " year)
2037 '(month " " day " " year))))
2038 (make-diary-entry
2039 (format "%s(diary-cyclic %d %s)"
2040 sexp-diary-entry-symbol
2041 (calendar-read "Repeat every how many days: "
2042 (lambda (x) (> x 0)))
2043 (calendar-date-string (calendar-cursor-to-date t) nil t))
2044 arg)))
2046 ;;; Diary mode.
2048 (defun diary-redraw-calendar ()
2049 "If `calendar-buffer' is live and diary entries are marked, redraw it."
2050 (and mark-diary-entries-in-calendar
2051 (save-excursion
2052 (redraw-calendar)))
2053 ;; Return value suitable for `write-contents-functions'.
2054 nil)
2056 (defvar diary-mode-map
2057 (let ((map (make-sparse-keymap)))
2058 (define-key map "\C-c\C-s" 'diary-show-all-entries)
2059 (define-key map "\C-c\C-q" 'quit-window)
2060 map)
2061 "Keymap for `diary-mode'.")
2063 (defun diary-font-lock-sexps (limit)
2064 "Recognize sexp diary entry up to LIMIT for font-locking."
2065 (if (re-search-forward
2066 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2067 (regexp-quote sexp-diary-entry-symbol))
2068 limit t)
2069 (condition-case nil
2070 (save-restriction
2071 (narrow-to-region (point-min) limit)
2072 (let ((start (point)))
2073 (forward-sexp 1)
2074 (store-match-data (list start (point)))
2076 (error t))))
2078 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
2079 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2080 If given, optional SYMBOL must be a prefix to entries.
2081 If optional ABBREV-ARRAY is present, the abbreviations constructed
2082 from this array by the function `calendar-abbrev-construct' are
2083 matched (with or without a final `.'), in addition to the full month
2084 names."
2085 (let ((dayname (diary-name-pattern calendar-day-name-array
2086 calendar-day-abbrev-array t))
2087 (monthname (format "\\(%s\\|\\*\\)"
2088 (diary-name-pattern month-array abbrev-array)))
2089 (month "\\([0-9]+\\|\\*\\)")
2090 (day "\\([0-9]+\\|\\*\\)")
2091 (year "-?\\([0-9]+\\|\\*\\)"))
2092 (mapcar (lambda (x)
2093 (cons
2094 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
2095 (if symbol (regexp-quote symbol) "") "\\("
2096 (mapconcat 'eval
2097 ;; If backup, omit first item (backup)
2098 ;; and last item (not part of date).
2099 (if (equal (car x) 'backup)
2100 (nreverse (cdr (reverse (cdr x))))
2103 ;; With backup, last item is not part of date.
2104 (if (equal (car x) 'backup)
2105 (concat "\\)" (eval (car (reverse x))))
2106 "\\)"))
2107 '(1 diary-face)))
2108 diary-date-forms)))
2110 (defmacro diary-font-lock-keywords-1 (markfunc listfunc feature months symbol)
2111 "Subroutine of the function `diary-font-lock-keywords'.
2112 If MARKFUNC is a member of `nongregorian-diary-marking-hook', or
2113 LISTFUNC of `nongregorian-diary-listing-hook', then require FEATURE
2114 and return a font-lock pattern matching array of MONTHS and marking SYMBOL."
2115 `(when (or (memq ',markfunc nongregorian-diary-marking-hook)
2116 (memq ',listfunc nongregorian-diary-listing-hook))
2117 (require ',feature)
2118 (diary-font-lock-date-forms ,months ,symbol)))
2120 (defconst diary-time-regexp
2121 ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
2122 ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
2123 ;; Hence often prefix this with "\\(^\\|\\s-\\)."
2124 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
2125 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
2126 "\\)\\([AaPp][Mm]\\)?\\)")
2127 "Regular expression matching a time of day.")
2129 (defvar calendar-hebrew-month-name-array-leap-year)
2130 (defvar calendar-islamic-month-name-array)
2131 (defvar calendar-bahai-month-name-array)
2133 ;;;###cal-autoload
2134 (defun diary-font-lock-keywords ()
2135 "Return a value for the variable `diary-font-lock-keywords'."
2136 (append
2137 (diary-font-lock-date-forms calendar-month-name-array
2138 nil calendar-month-abbrev-array)
2139 (diary-font-lock-keywords-1 mark-hebrew-diary-entries
2140 list-hebrew-diary-entries
2141 cal-hebrew
2142 calendar-hebrew-month-name-array-leap-year
2143 hebrew-diary-entry-symbol)
2144 (diary-font-lock-keywords-1 mark-islamic-diary-entries
2145 list-islamic-diary-entries
2146 cal-islam
2147 calendar-islamic-month-name-array
2148 islamic-diary-entry-symbol)
2149 (diary-font-lock-keywords-1 diary-bahai-mark-entries
2150 diary-bahai-list-entries
2151 cal-bahai
2152 calendar-bahai-month-name-array
2153 bahai-diary-entry-symbol)
2154 (list
2155 (cons
2156 (format "^%s.*$" (regexp-quote diary-include-string))
2157 'font-lock-keyword-face)
2158 (cons
2159 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2160 (regexp-quote sexp-diary-entry-symbol))
2161 '(1 font-lock-reference-face))
2162 (cons
2163 (format "^%s" (regexp-quote diary-nonmarking-symbol))
2164 'font-lock-reference-face)
2165 (cons
2166 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol)
2167 (regexp-opt (mapcar 'regexp-quote
2168 (list hebrew-diary-entry-symbol
2169 islamic-diary-entry-symbol
2170 bahai-diary-entry-symbol))
2172 '(1 font-lock-reference-face))
2173 '(diary-font-lock-sexps . font-lock-keyword-face)
2174 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2175 diary-time-regexp)
2176 . 'diary-time))))
2178 (defvar diary-font-lock-keywords (diary-font-lock-keywords)
2179 "Forms to highlight in `diary-mode'.")
2181 ;;;###autoload
2182 (define-derived-mode diary-mode fundamental-mode "Diary"
2183 "Major mode for editing the diary file."
2184 (set (make-local-variable 'font-lock-defaults)
2185 '(diary-font-lock-keywords t))
2186 (add-to-invisibility-spec '(diary . nil))
2187 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
2188 (if diary-header-line-flag
2189 (setq header-line-format diary-header-line-format)))
2192 ;;; Fancy Diary Mode.
2194 (defvar diary-fancy-date-pattern
2195 (concat
2196 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
2197 (monthname (diary-name-pattern calendar-month-name-array nil t))
2198 (day "[0-9]+")
2199 (month "[0-9]+")
2200 (year "-?[0-9]+"))
2201 (mapconcat 'eval calendar-date-display-form ""))
2202 ;; Optional ": holiday name" after the date.
2203 "\\(: .*\\)?")
2204 "Regular expression matching a date header in Fancy Diary.")
2206 (defvar fancy-diary-font-lock-keywords
2207 (list
2208 (list
2209 ;; Any number of " other holiday name" lines, followed by "==" line.
2210 (concat diary-fancy-date-pattern "\\(\n +.*\\)*\n=+$")
2211 '(0 (progn (put-text-property (match-beginning 0) (match-end 0)
2212 'font-lock-multiline t)
2213 diary-face)))
2214 '("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" . 'diary-anniversary)
2215 '("^.*Yahrzeit.*$" . font-lock-reference-face)
2216 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
2217 '("^Day.*omer.*$" . font-lock-builtin-face)
2218 '("^Parashat.*$" . font-lock-comment-face)
2219 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2220 diary-time-regexp) . 'diary-time))
2221 "Keywords to highlight in fancy diary display.")
2223 ;; If region looks like it might start or end in the middle of a
2224 ;; multiline pattern, extend the region to encompass the whole pattern.
2225 (defun diary-fancy-font-lock-fontify-region-function (beg end &optional verbose)
2226 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2227 Needed to handle multiline keyword in `fancy-diary-font-lock-keywords'.
2228 Fontify the region between BEG and END, quietly unless VERBOSE is non-nil."
2229 (goto-char beg)
2230 (forward-line 0)
2231 (if (looking-at "=+$") (forward-line -1))
2232 (while (and (looking-at " +[^ ]")
2233 (zerop (forward-line -1))))
2234 ;; This check not essential.
2235 (if (looking-at diary-fancy-date-pattern)
2236 (setq beg (line-beginning-position)))
2237 (goto-char end)
2238 (forward-line 0)
2239 (while (and (looking-at " +[^ ]")
2240 (zerop (forward-line 1))))
2241 (if (looking-at "=+$")
2242 (setq end (line-beginning-position 2)))
2243 (font-lock-default-fontify-region beg end verbose))
2245 (define-derived-mode fancy-diary-display-mode fundamental-mode
2246 "Diary"
2247 "Major mode used while displaying diary entries using Fancy Display."
2248 (set (make-local-variable 'font-lock-defaults)
2249 '(fancy-diary-font-lock-keywords
2250 t nil nil nil
2251 (font-lock-fontify-region-function
2252 . diary-fancy-font-lock-fontify-region-function)))
2253 (local-set-key "q" 'quit-window))
2256 ;; Following code from Dave Love <fx@gnu.org>.
2257 ;; Import Outlook-format appointments from mail messages in Gnus or
2258 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2259 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2260 ;; could be run from hooks to notice appointments automatically (in
2261 ;; which case they will prompt about adding to the diary). The
2262 ;; message formats recognized are customizable through
2263 ;; `diary-outlook-formats'.
2265 (defvar subject) ; bound in diary-from-outlook-gnus
2267 (defun diary-from-outlook-internal (&optional test-only)
2268 "Snarf a diary entry from a message assumed to be from MS Outlook.
2269 Assumes `body' is bound to a string comprising the body of the message and
2270 `subject' is bound to a string comprising its subject.
2271 Arg TEST-ONLY non-nil means return non-nil if and only if the
2272 message contains an appointment, don't make a diary entry."
2273 (catch 'finished
2274 (let (format-string)
2275 (dotimes (i (length diary-outlook-formats))
2276 (when (eq 0 (string-match (car (nth i diary-outlook-formats))
2277 body))
2278 (unless test-only
2279 (setq format-string (cdr (nth i diary-outlook-formats)))
2280 (save-excursion
2281 (save-window-excursion
2282 ;; Fixme: References to optional fields in the format
2283 ;; are treated literally, not replaced by the empty
2284 ;; string. I think this is an Emacs bug.
2285 (make-diary-entry
2286 (format (replace-match (if (functionp format-string)
2287 (funcall format-string body)
2288 format-string)
2289 t nil (match-string 0 body))
2290 subject))
2291 (save-buffer))))
2292 (throw 'finished t))))
2293 nil))
2295 (defvar gnus-article-mime-handles)
2296 (defvar gnus-article-buffer)
2298 (autoload 'gnus-fetch-field "gnus-util")
2299 (autoload 'gnus-narrow-to-body "gnus")
2300 (autoload 'mm-get-part "mm-decode")
2302 (defun diary-from-outlook-gnus (&optional noconfirm)
2303 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2304 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2305 this function is called interactively), then if an entry is found the
2306 user is asked to confirm its addition.
2307 Add this function to `gnus-article-prepare-hook' to notice appointments
2308 automatically."
2309 (interactive "p")
2310 (with-current-buffer gnus-article-buffer
2311 (let ((subject (gnus-fetch-field "subject"))
2312 (body (if gnus-article-mime-handles
2313 ;; We're multipart. Don't get confused by part
2314 ;; buttons &c. Assume info is in first part.
2315 (mm-get-part (nth 1 gnus-article-mime-handles))
2316 (save-restriction
2317 (gnus-narrow-to-body)
2318 (buffer-string)))))
2319 (when (diary-from-outlook-internal t)
2320 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2321 (diary-from-outlook-internal)
2322 (message "Diary entry added"))))))
2324 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2326 (defvar rmail-buffer)
2328 (defun diary-from-outlook-rmail (&optional noconfirm)
2329 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2330 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2331 this function is called interactively), then if an entry is found the
2332 user is asked to confirm its addition."
2333 (interactive "p")
2334 (with-current-buffer rmail-buffer
2335 (let ((subject (mail-fetch-field "subject"))
2336 (body (buffer-substring (save-excursion
2337 (rfc822-goto-eoh)
2338 (point))
2339 (point-max))))
2340 (when (diary-from-outlook-internal t)
2341 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2342 (diary-from-outlook-internal)
2343 (message "Diary entry added"))))))
2345 (defun diary-from-outlook (&optional noconfirm)
2346 "Maybe snarf diary entry from current Outlook-generated message.
2347 Currently knows about Gnus and Rmail modes. Unless the optional
2348 argument NOCONFIRM is non-nil (which is the case when this
2349 function is called interactively), then if an entry is found the
2350 user is asked to confirm its addition."
2351 (interactive "p")
2352 (let ((func (cond
2353 ((eq major-mode 'rmail-mode)
2354 #'diary-from-outlook-rmail)
2355 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2356 #'diary-from-outlook-gnus)
2357 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2358 (funcall func noconfirm)))
2360 (provide 'diary-lib)
2362 ;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
2363 ;;; diary-lib.el ends here