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