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