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