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