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