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