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