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