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