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