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