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