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