ChangeLog fixes from M-x authors
[emacs.git] / lisp / calendar / diary-lib.el
blobf4aa26a086499020306ecd2ea97148acf8fd405e
1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989-1990, 1992-1995, 2001-2013 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)))
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 (if (and diary-display-function
905 (listp diary-display-function))
906 ;; Backwards compatibility.
907 (run-hooks 'diary-display-function)
908 (funcall (or diary-display-function
909 'diary-simple-display))))
910 (run-hooks 'diary-hook)))))
911 (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
912 (or d-incp (message "Preparing diary...done"))
913 diary-entries-list)))
915 (defun diary-unhide-everything ()
916 "Show all invisible text in the diary."
917 (kill-local-variable 'diary-selective-display)
918 (save-restriction ; bug#5477
919 (widen)
920 (remove-overlays (point-min) (point-max) 'invisible 'diary))
921 (kill-local-variable 'mode-line-format))
923 (defvar original-date) ; bound in diary-list-entries
924 ;(defvar number) ; already declared above
926 (defun diary-include-files (&optional mark)
927 "Process diary entries from included diary files.
928 By default, lists included entries, but if optional argument MARK is non-nil
929 marks entries instead.
930 For example, this enables you to share common diary files.
931 Specify include files using lines matching `diary-include-string', e.g.
932 #include \"filename\"
933 This is recursive; that is, included files may include other files."
934 (goto-char (point-min))
935 (while (re-search-forward
936 (format "^%s \"\\([^\"]*\\)\"" (regexp-quote diary-include-string))
937 nil t)
938 (let ((diary-file (match-string-no-properties 1))
939 (diary-mark-entries-hook 'diary-mark-included-diary-files)
940 (diary-list-entries-hook 'diary-include-other-diary-files)
941 (diary-including t)
942 diary-hook diary-list-include-blanks efile)
943 (if (file-exists-p diary-file)
944 (if (file-readable-p diary-file)
945 (if (member (setq efile (expand-file-name diary-file))
946 diary-included-files)
947 (error "Recursive diary include for %s" diary-file)
948 (setq diary-included-files
949 (append diary-included-files (list efile)))
950 (if mark
951 (diary-mark-entries)
952 (setq diary-entries-list
953 (append diary-entries-list
954 (diary-list-entries original-date number t)))))
955 (display-warning
956 :error
957 (format "Can't read included diary file %s\n" diary-file)))
958 (display-warning
959 :error
960 (format "Can't find included diary file %s\n" diary-file)))))
961 (goto-char (point-min)))
963 (defun diary-include-other-diary-files ()
964 "Add diary entries from included diary files to `diary-entries-list'.
965 To use, add this function to `diary-list-entries-hook'.
966 For details, see `diary-include-files'.
967 See also `diary-mark-included-diary-files'."
968 (diary-include-files))
970 (define-obsolete-function-alias 'include-other-diary-files
971 'diary-include-other-diary-files "23.1")
973 (defvar date-string) ; bound in diary-list-entries
975 (defun diary-display-no-entries ()
976 "Common subroutine of `diary-simple-display' and `diary-fancy-display'.
977 Handles the case where there are no diary entries.
978 Returns a cons (NOENTRIES . HOLIDAY-STRING)."
979 (let* ((holiday-list (if diary-show-holidays-flag
980 (calendar-check-holidays original-date)))
981 (hol-string (format "%s%s%s"
982 date-string
983 (if holiday-list ": " "")
984 (mapconcat 'identity holiday-list "; ")))
985 (msg (format "No diary entries for %s" hol-string))
986 ;; Empty list, or single item with no text.
987 ;; FIXME multiple items with no text?
988 (noentries (or (not diary-entries-list)
989 (and (not (cdr diary-entries-list))
990 (string-equal "" (cadr
991 (car diary-entries-list)))))))
992 ;; Inconsistency: whether or not the holidays are displayed in a
993 ;; separate buffer depends on if there are diary entries.
994 (when noentries
995 (if (or (< (length msg) (frame-width))
996 (not holiday-list))
997 (message "%s" msg)
998 ;; holiday-list which is too wide for a message gets a buffer.
999 (calendar-in-read-only-buffer holiday-buffer
1000 (calendar-set-mode-line (format "Holidays for %s" date-string))
1001 (insert (mapconcat 'identity holiday-list "\n")))
1002 (message "No diary entries for %s" date-string)))
1003 (cons noentries hol-string)))
1006 (defvar diary-saved-point) ; bound in diary-list-entries
1008 (defun diary-simple-display ()
1009 "Display the diary buffer if there are any relevant entries or holidays.
1010 Entries that do not apply are made invisible. Holidays are shown
1011 in the mode line. This is an option for `diary-display-function'."
1012 ;; If selected window is dedicated (to the calendar), need a new one
1013 ;; to display the diary.
1014 (let* ((pop-up-frames (or pop-up-frames
1015 (window-dedicated-p (selected-window))))
1016 (dbuff (find-buffer-visiting diary-file))
1017 (empty (diary-display-no-entries)))
1018 ;; This may be too wide, but when simple diary is used there is
1019 ;; nowhere else for the holidays to go. Also, it is documented in
1020 ;; diary-show-holidays-flag that the holidays go in the mode-line.
1021 ;; FIXME however if there are no diary entries a separate buffer
1022 ;; is displayed - this is inconsistent.
1023 (with-current-buffer dbuff
1024 (calendar-set-mode-line (format "Diary for %s" (cdr empty))))
1025 (unless (car empty) ; no entries
1026 (with-current-buffer dbuff
1027 (let ((window (display-buffer (current-buffer))))
1028 ;; d-s-p is passed from diary-list-entries.
1029 (set-window-point window diary-saved-point)
1030 (set-window-start window (point-min)))))))
1032 (define-obsolete-function-alias 'simple-diary-display
1033 'diary-simple-display "23.1")
1035 (define-button-type 'diary-entry 'action #'diary-goto-entry
1036 'face 'diary-button 'help-echo "Find this diary entry"
1037 'follow-link t)
1039 (defun diary-goto-entry (button)
1040 "Jump to the diary entry for the BUTTON at point."
1041 (let* ((locator (button-get button 'locator))
1042 (marker (car locator))
1043 markbuf file)
1044 ;; If marker pointing to diary location is valid, use that.
1045 (if (and marker (setq markbuf (marker-buffer marker)))
1046 (progn
1047 (pop-to-buffer markbuf)
1048 (goto-char (marker-position marker)))
1049 ;; Marker is invalid (eg buffer has been killed).
1050 (or (and (setq file (cadr locator))
1051 (file-exists-p file)
1052 (find-file-other-window file)
1053 (progn
1054 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
1055 (goto-char (point-min))
1056 (if (re-search-forward (format "%s.*\\(%s\\)"
1057 (regexp-quote (nth 2 locator))
1058 (regexp-quote (nth 3 locator)))
1059 nil t)
1060 (goto-char (match-beginning 1)))))
1061 (message "Unable to locate this diary entry")))))
1063 (defun diary-fancy-display ()
1064 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
1065 Holidays are shown unless `diary-show-holidays-flag' is nil.
1066 Days with no diary entries are not shown (even if that day is a
1067 holiday), unless `diary-list-include-blanks' is non-nil.
1069 This is an option for `diary-display-function'."
1070 ;; Turn off selective-display in the diary file's buffer.
1071 (with-current-buffer (find-buffer-visiting diary-file)
1072 (diary-unhide-everything))
1073 (unless (car (diary-display-no-entries)) ; no entries
1074 ;; Prepare the fancy diary buffer.
1075 (calendar-in-read-only-buffer diary-fancy-buffer
1076 (calendar-set-mode-line "Diary Entries")
1077 (let ((holiday-list-last-month 1)
1078 (holiday-list-last-year 1)
1079 (date (list 0 0 0))
1080 holiday-list)
1081 (dolist (entry diary-entries-list)
1082 (unless (calendar-date-equal date (car entry))
1083 (setq date (car entry))
1084 (and diary-show-holidays-flag
1085 (calendar-date-compare
1086 (list (list holiday-list-last-month
1087 (calendar-last-day-of-month
1088 holiday-list-last-month
1089 holiday-list-last-year)
1090 holiday-list-last-year))
1091 (list date))
1092 ;; We need to get the holidays for the next 3 months.
1093 (setq holiday-list-last-month
1094 (calendar-extract-month date)
1095 holiday-list-last-year
1096 (calendar-extract-year date))
1097 (progn
1098 (calendar-increment-month
1099 holiday-list-last-month holiday-list-last-year 1)
1101 (setq holiday-list
1102 (let ((displayed-month holiday-list-last-month)
1103 (displayed-year holiday-list-last-year))
1104 (calendar-holiday-list)))
1105 (calendar-increment-month
1106 holiday-list-last-month holiday-list-last-year 1))
1107 (let ((longest 0)
1108 date-holiday-list cc)
1109 ;; Make a list of all holidays for date.
1110 (dolist (h holiday-list)
1111 (if (calendar-date-equal date (car h))
1112 (setq date-holiday-list (append date-holiday-list
1113 (cdr h)))))
1114 (insert (if (bobp) "" ?\n) (calendar-date-string date))
1115 (if date-holiday-list (insert ": "))
1116 (setq cc (current-column))
1117 (insert (mapconcat (lambda (x)
1118 (setq longest (max longest (length x)))
1120 date-holiday-list
1121 (concat "\n" (make-string cc ?\s))))
1122 (insert ?\n (make-string (+ cc longest) ?=) ?\n)))
1123 (let ((this-entry (cadr entry))
1124 this-loc marks temp-face)
1125 (unless (zerop (length this-entry))
1126 (if (setq this-loc (nth 3 entry))
1127 (insert-button this-entry
1128 ;; (MARKER FILENAME SPECIFIER LITERAL)
1129 'locator (list (car this-loc)
1130 (cadr this-loc)
1131 (nth 2 entry)
1132 (or (nth 2 this-loc)
1133 (nth 1 entry)))
1134 :type 'diary-entry)
1135 (insert this-entry))
1136 (insert ?\n)
1137 ;; Doesn't make sense to check font-lock-mode - see
1138 ;; comments above diary-entry-marker in calendar.el.
1139 (and ; font-lock-mode
1140 (setq marks (nth 4 entry))
1141 (save-excursion
1142 (setq temp-face (calendar-make-temp-face marks))
1143 (search-backward this-entry)
1144 (overlay-put
1145 (make-overlay (match-beginning 0) (match-end 0))
1146 'face temp-face)))))))
1147 ;; FIXME can't remember what this check was for.
1148 ;; To prevent something looping, or a minor optimization?
1149 (if (eq major-mode 'diary-fancy-display-mode)
1150 (run-hooks 'diary-fancy-display-mode-hook)
1151 (diary-fancy-display-mode))
1152 (calendar-set-mode-line date-string))))
1154 (define-obsolete-function-alias 'fancy-diary-display
1155 'diary-fancy-display "23.1")
1157 ;; FIXME modernize?
1158 (defun diary-print-entries ()
1159 "Print a hard copy of the diary display.
1161 If the simple diary display is being used, prepare a temp buffer with the
1162 visible lines of the diary buffer, add a heading line composed from the mode
1163 line, print the temp buffer, and destroy it.
1165 If the fancy diary display is being used, just print the buffer.
1167 The hooks given by the variable `diary-print-entries-hook' are called to do
1168 the actual printing."
1169 (interactive)
1170 (let ((diary-buffer (get-buffer diary-fancy-buffer))
1171 temp-buffer heading start end)
1172 (if diary-buffer
1173 (with-current-buffer diary-buffer
1174 (run-hooks 'diary-print-entries-hook))
1175 (or (setq diary-buffer (find-buffer-visiting diary-file))
1176 (error "You don't have a diary buffer!"))
1177 ;; Name affects printing?
1178 (setq temp-buffer (get-buffer-create " *Printable Diary Entries*"))
1179 (with-current-buffer diary-buffer
1180 (setq heading
1181 (if (not (stringp mode-line-format))
1182 "All Diary Entries"
1183 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
1184 (match-string 1 mode-line-format))
1185 start (point-min))
1186 (while
1187 (progn
1188 (setq end (next-single-char-property-change start 'invisible))
1189 (unless (get-char-property start 'invisible)
1190 (with-current-buffer temp-buffer
1191 (insert-buffer-substring diary-buffer start end)))
1192 (setq start end)
1193 (and end (< end (point-max))))))
1194 (set-buffer temp-buffer)
1195 (goto-char (point-min))
1196 (insert heading "\n"
1197 (make-string (length heading) ?=) "\n")
1198 (run-hooks 'diary-print-entries-hook)
1199 (kill-buffer temp-buffer))))
1201 (define-obsolete-function-alias 'print-diary-entries
1202 'diary-print-entries "23.1")
1204 ;;;###cal-autoload
1205 (defun diary-show-all-entries ()
1206 "Show all of the diary entries in the diary file.
1207 This function gets rid of the selective display of the diary file so that
1208 all entries, not just some, are visible. If there is no diary buffer, one
1209 is created."
1210 (interactive)
1211 (let* ((d-file (diary-check-diary-file))
1212 (pop-up-frames (or pop-up-frames
1213 (window-dedicated-p (selected-window))))
1214 (win (selected-window))
1215 (height (window-height)))
1216 (with-current-buffer (or (find-buffer-visiting d-file)
1217 (find-file-noselect d-file t))
1218 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
1219 (diary-unhide-everything)
1220 (display-buffer (current-buffer))
1221 (when (and (/= height (window-height win))
1222 (with-current-buffer (window-buffer win)
1223 (derived-mode-p 'calendar-mode)))
1224 (fit-window-to-buffer win)))))
1226 ;;;###autoload
1227 (defun diary-mail-entries (&optional ndays)
1228 "Send a mail message showing diary entries for next NDAYS days.
1229 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
1230 Mail is sent to the address specified by `diary-mail-addr'.
1232 Here is an example of a script to call `diary-mail-entries',
1233 suitable for regular scheduling using cron (or at). Note that
1234 since `emacs -script' does not load your init file, you should
1235 ensure that all relevant variables are set.
1237 #!/usr/bin/emacs -script
1238 ;; diary-rem.el - run the Emacs diary-reminder
1240 \(setq diary-mail-days 3
1241 diary-file \"/path/to/diary.file\"
1242 calendar-date-style 'european
1243 diary-mail-addr \"user@host.name\")
1245 \(diary-mail-entries)
1247 # diary-rem.el ends here
1249 (interactive "P")
1250 (if (string-equal diary-mail-addr "")
1251 (error "You must set `diary-mail-addr' to use this command")
1252 (let ((diary-display-function 'diary-fancy-display))
1253 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days)))
1254 (compose-mail diary-mail-addr
1255 (concat "Diary entries generated "
1256 (calendar-date-string (calendar-current-date))))
1257 (insert
1258 (if (get-buffer diary-fancy-buffer)
1259 (with-current-buffer diary-fancy-buffer (buffer-string))
1260 "No entries found"))
1261 (call-interactively (get mail-user-agent 'sendfunc))))
1263 (defun diary-name-pattern (string-array &optional abbrev-array paren)
1264 "Return a regexp matching the strings in the array STRING-ARRAY.
1265 If the optional argument ABBREV-ARRAY is present, the regexp
1266 also matches the supplied abbreviations, with or without final `.'
1267 characters. If the optional argument PAREN is non-nil, surrounds
1268 the regexp with parentheses."
1269 (regexp-opt (append string-array
1270 abbrev-array
1271 (if abbrev-array
1272 (mapcar (lambda (e) (format "%s." e))
1273 abbrev-array))
1274 nil)
1275 paren))
1277 (defvar diary-marking-entries-flag nil
1278 "True during the marking of diary entries, nil otherwise.")
1280 (defvar diary-marking-entry-flag nil
1281 "True during the marking of diary entries, if current entry is marking.")
1283 ;; file-glob-attrs bound in diary-mark-entries.
1284 (defun diary-mark-entries-1 (markfunc &optional months symbol absfunc)
1285 "Mark diary entries of a certain type.
1286 MARKFUNC is a function that marks entries of the appropriate type
1287 matching a given date pattern. MONTHS is an array of month names.
1288 SYMBOL marks diary entries of the type in question. ABSFUNC is a
1289 function that converts absolute dates to dates of the appropriate type. "
1290 (let ((dayname (diary-name-pattern calendar-day-name-array
1291 calendar-day-abbrev-array))
1292 (monthname (format "%s\\|\\*"
1293 (if months
1294 (diary-name-pattern months)
1295 (diary-name-pattern calendar-month-name-array
1296 calendar-month-abbrev-array))))
1297 (month "[0-9]+\\|\\*")
1298 (day "[0-9]+\\|\\*")
1299 (year "[0-9]+\\|\\*")
1300 (case-fold-search t)
1301 marks)
1302 (dolist (date-form diary-date-forms)
1303 (if (eq (car date-form) 'backup) ; ignore 'backup directive
1304 (setq date-form (cdr date-form)))
1305 (let* ((l (length date-form))
1306 (d-name-pos (- l (length (memq 'dayname date-form))))
1307 (d-name-pos (if (/= l d-name-pos) (1+ d-name-pos)))
1308 (m-name-pos (- l (length (memq 'monthname date-form))))
1309 (m-name-pos (if (/= l m-name-pos) (1+ m-name-pos)))
1310 (d-pos (- l (length (memq 'day date-form))))
1311 (d-pos (if (/= l d-pos) (1+ d-pos)))
1312 (m-pos (- l (length (memq 'month date-form))))
1313 (m-pos (if (/= l m-pos) (1+ m-pos)))
1314 (y-pos (- l (length (memq 'year date-form))))
1315 (y-pos (if (/= l y-pos) (1+ y-pos)))
1316 (regexp (format "^%s\\(%s\\)"
1317 (if symbol (regexp-quote symbol) "")
1318 (mapconcat 'eval date-form "\\)\\("))))
1319 (goto-char (point-min))
1320 (while (re-search-forward regexp nil t)
1321 (let* ((dd-name
1322 (if d-name-pos
1323 (match-string-no-properties d-name-pos)))
1324 (mm-name
1325 (if m-name-pos
1326 (match-string-no-properties m-name-pos)))
1327 (mm (string-to-number
1328 (if m-pos
1329 (match-string-no-properties m-pos)
1330 "")))
1331 (dd (string-to-number
1332 (if d-pos
1333 (match-string-no-properties d-pos)
1334 "")))
1335 (y-str (if y-pos
1336 (match-string-no-properties y-pos)))
1337 (yy (if (not y-str)
1339 (if (and (= (length y-str) 2)
1340 diary-abbreviated-year-flag)
1341 (let* ((current-y
1342 (calendar-extract-year
1343 (if absfunc
1344 (funcall
1345 absfunc
1346 (calendar-absolute-from-gregorian
1347 (calendar-current-date)))
1348 (calendar-current-date))))
1349 (y (+ (string-to-number y-str)
1350 ;; Current century, eg 2000.
1351 (* 100 (/ current-y 100))))
1352 (offset (- y current-y)))
1353 ;; Add 2-digit year to current century.
1354 ;; If more than 50 years in the future,
1355 ;; assume last century. If more than 50
1356 ;; years in the past, assume next century.
1357 (if (> offset 50)
1358 (- y 100)
1359 (if (< offset -50)
1360 (+ y 100)
1361 y)))
1362 (string-to-number y-str)))))
1363 (setq marks (cadr (diary-pull-attrs
1364 (buffer-substring-no-properties
1365 (point) (line-end-position))
1366 file-glob-attrs)))
1367 ;; Only mark all days of a given name if the pattern
1368 ;; contains no more specific elements.
1369 (if (and dd-name (not (or d-pos m-pos y-pos)))
1370 (calendar-mark-days-named
1371 (cdr (assoc-string dd-name
1372 (calendar-make-alist
1373 calendar-day-name-array
1374 0 nil calendar-day-abbrev-array
1375 (mapcar (lambda (e)
1376 (format "%s." e))
1377 calendar-day-abbrev-array))
1378 t)) marks)
1379 (if mm-name
1380 (setq mm
1381 (if (string-equal mm-name "*") 0
1382 (cdr (assoc-string
1383 mm-name
1384 (if months (calendar-make-alist months)
1385 (calendar-make-alist
1386 calendar-month-name-array
1387 1 nil calendar-month-abbrev-array
1388 (mapcar (lambda (e)
1389 (format "%s." e))
1390 calendar-month-abbrev-array)))
1391 t)))))
1392 (funcall markfunc mm dd yy marks))))))))
1394 ;;;###cal-autoload
1395 (defun diary-mark-entries (&optional redraw)
1396 "Mark days in the calendar window that have diary entries.
1397 Marks each entry in the diary that is visible in the calendar window.
1399 After marking the entries, runs `diary-nongregorian-marking-hook'
1400 for the main diary file, and each included file. For example,
1401 this is the appropriate hook to process Islamic entries in all
1402 diary files. Next `diary-mark-entries-hook' runs, for the main diary
1403 file only. If not using include files, there is no difference between
1404 these two hooks.
1406 If the optional argument REDRAW is non-nil (which is the case
1407 interactively, for example) then this first removes any existing diary
1408 marks. This is intended to deal with deleted diary entries."
1409 (interactive "p")
1410 ;; To remove any deleted diary entries. Do not redraw when:
1411 ;; i) processing #include diary files (else only get the marks from
1412 ;; the last #include file processed).
1413 ;; ii) called via calendar-redraw (since calendar has already been
1414 ;; erased).
1415 ;; Use of REDRAW handles both of these cases.
1416 (when (and redraw calendar-mark-diary-entries-flag)
1417 (setq calendar-mark-diary-entries-flag nil)
1418 (calendar-redraw))
1419 (let ((diary-marking-entries-flag t)
1420 (diary-buffer (find-buffer-visiting diary-file))
1421 ;; Dynamically bound in diary-include-files.
1422 (d-incp (and (boundp 'diary-including) diary-including))
1423 file-glob-attrs temp-buff)
1424 (unless d-incp
1425 (setq diary-included-files nil)
1426 (message "Marking diary entries..."))
1427 (unwind-protect
1428 (with-current-buffer (or diary-buffer
1429 (if d-incp
1430 (setq temp-buff (generate-new-buffer
1431 " *diary-temp*"))
1432 (find-file-noselect
1433 (diary-check-diary-file) t)))
1434 (if temp-buff
1435 ;; If including, caller has already verified it is readable.
1436 (insert-file-contents diary-file)
1437 (if (eq major-mode (default-value 'major-mode)) (diary-mode)))
1438 (setq calendar-mark-diary-entries-flag t)
1439 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1440 (with-syntax-table diary-syntax-table
1441 (save-excursion
1442 (diary-mark-entries-1 'calendar-mark-date-pattern)
1443 (diary-mark-sexp-entries)
1444 ;; Although it looks like mark-entries-hook runs every time,
1445 ;; diary-mark-included-diary-files binds it to nil
1446 ;; (essentially) when it runs in included files.
1447 (run-hooks 'diary-nongregorian-marking-hook
1448 'diary-mark-entries-hook))))
1449 (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
1450 (or d-incp (message "Marking diary entries...done"))))
1452 ;;;###cal-autoload
1453 (define-obsolete-function-alias 'mark-diary-entries 'diary-mark-entries "23.1")
1455 (defun diary-sexp-entry (sexp entry date)
1456 "Process a SEXP diary ENTRY for DATE."
1457 (let ((result (if calendar-debug-sexp
1458 (let ((debug-on-error t))
1459 (eval (car (read-from-string sexp))))
1460 (let (err)
1461 (condition-case err
1462 (eval (car (read-from-string sexp)))
1463 (error
1464 (display-warning
1465 :error
1466 (format "Bad diary sexp at line %d in %s:\n%s\n\
1467 Error: %s\n"
1468 (count-lines (point-min) (point))
1469 diary-file sexp err))
1470 nil))))))
1471 (cond ((stringp result) result)
1472 ((and (consp result)
1473 (stringp (cdr result))) result)
1474 (result entry)
1475 (t nil))))
1477 (defvar displayed-year) ; bound in calendar-generate
1478 (defvar displayed-month)
1480 (defun diary-mark-sexp-entries ()
1481 "Mark days in the calendar window that have sexp diary entries.
1482 Each entry in the diary file (or included files) visible in the calendar window
1483 is marked. See the documentation for the function `diary-list-sexp-entries'."
1484 (let* ((sexp-mark (regexp-quote diary-sexp-entry-symbol))
1485 (s-entry (format "^\\(%s(\\)\\|\\(%s%s(diary-remind\\)" sexp-mark
1486 (regexp-quote diary-nonmarking-symbol)
1487 sexp-mark))
1488 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1489 m y first-date last-date date mark file-glob-attrs
1490 sexp-start sexp entry entry-start)
1491 (with-current-buffer calendar-buffer
1492 (setq m displayed-month
1493 y displayed-year))
1494 (calendar-increment-month m y -1)
1495 (setq first-date (calendar-absolute-from-gregorian (list m 1 y)))
1496 (calendar-increment-month m y 2)
1497 (setq last-date
1498 (calendar-absolute-from-gregorian
1499 (list m (calendar-last-day-of-month m y) y)))
1500 (goto-char (point-min))
1501 (while (re-search-forward s-entry nil t)
1502 (setq diary-marking-entry-flag (char-equal (preceding-char) ?\())
1503 (re-search-backward "(")
1504 (setq sexp-start (point))
1505 (forward-sexp)
1506 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1507 (forward-char 1)
1508 (if (and (bolp) (not (looking-at "[ \t]")))
1509 ;; Diary entry consists only of the sexp.
1510 (progn
1511 (backward-char 1)
1512 (setq entry ""))
1513 (setq entry-start (point))
1514 ;; Find end of entry.
1515 (forward-line 1)
1516 (while (looking-at "[ \t]")
1517 (forward-line 1))
1518 (if (bolp) (backward-char 1))
1519 (setq entry (buffer-substring-no-properties entry-start (point))))
1520 (setq date (1- first-date))
1521 ;; FIXME this loops over all visible dates.
1522 ;; Could be optimized in many cases. Depends on whether t or * present.
1523 (while (<= (setq date (1+ date)) last-date)
1524 (when (setq mark (diary-sexp-entry
1525 sexp entry
1526 (calendar-gregorian-from-absolute date)))
1527 (calendar-mark-visible-date
1528 (calendar-gregorian-from-absolute date)
1529 (or (cadr (diary-pull-attrs entry file-glob-attrs))
1530 (if (consp mark) (car mark)))))))))
1532 (define-obsolete-function-alias 'mark-sexp-diary-entries
1533 'diary-mark-sexp-entries "23.1")
1535 (defun diary-mark-included-diary-files ()
1536 "Mark diary entries from included diary files.
1537 To use, add this function to `diary-mark-entries-hook'.
1538 For details, see `diary-include-files'.
1539 See also `diary-include-other-diary-files'."
1540 (diary-include-files t))
1542 (define-obsolete-function-alias 'mark-included-diary-files
1543 'diary-mark-included-diary-files "23.1")
1545 (defun calendar-mark-days-named (dayname &optional color)
1546 "Mark all dates in the calendar window that are day DAYNAME of the week.
1547 0 means all Sundays, 1 means all Mondays, and so on.
1548 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1549 (with-current-buffer calendar-buffer
1550 (let ((prev-month displayed-month)
1551 (prev-year displayed-year)
1552 (succ-month displayed-month)
1553 (succ-year displayed-year)
1554 (last-day)
1555 (day))
1556 (calendar-increment-month succ-month succ-year 1)
1557 (calendar-increment-month prev-month prev-year -1)
1558 (setq day (calendar-absolute-from-gregorian
1559 (calendar-nth-named-day 1 dayname prev-month prev-year))
1560 last-day (calendar-absolute-from-gregorian
1561 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1562 (while (<= day last-day)
1563 (calendar-mark-visible-date (calendar-gregorian-from-absolute day)
1564 color)
1565 (setq day (+ day 7))))))
1567 (define-obsolete-function-alias 'mark-calendar-days-named
1568 'calendar-mark-days-named "23.1")
1570 (defun calendar-mark-month (month year p-month p-day p-year &optional color)
1571 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P-DAY/P-YEAR.
1572 A value of 0 in any position of the pattern is a wildcard.
1573 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1574 (if (or (and (= month p-month)
1575 (or (zerop p-year) (= year p-year)))
1576 (and (zerop p-month)
1577 (or (zerop p-year) (= year p-year))))
1578 (if (zerop p-day)
1579 (dotimes (i (calendar-last-day-of-month month year))
1580 (calendar-mark-visible-date (list month (1+ i) year) color))
1581 (calendar-mark-visible-date (list month p-day year) color))))
1583 (define-obsolete-function-alias 'mark-calendar-month
1584 'calendar-mark-month "23.1")
1586 (defun calendar-mark-date-pattern (month day year &optional color)
1587 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1588 A value of 0 in any position is a wildcard. Optional argument COLOR is
1589 passed to `calendar-mark-visible-date' as MARK."
1590 (with-current-buffer calendar-buffer
1591 (let ((m displayed-month)
1592 (y displayed-year))
1593 (calendar-increment-month m y -1)
1594 (dotimes (_idummy 3)
1595 (calendar-mark-month m y month day year color)
1596 (calendar-increment-month m y 1)))))
1598 (define-obsolete-function-alias 'mark-calendar-date-pattern
1599 'calendar-mark-date-pattern "23.1")
1601 ;; Bahai, Hebrew, Islamic.
1602 (defun calendar-mark-complex (month day year fromabs &optional color)
1603 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1604 The function FROMABS converts absolute dates to the appropriate date system.
1605 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1606 ;; Not one of the simple cases--check all visible dates for match.
1607 ;; Actually, the following code takes care of ALL of the cases, but
1608 ;; it's much too slow to be used for the simple (common) cases.
1609 (let* ((m displayed-month)
1610 (y displayed-year)
1611 (first-date (progn
1612 (calendar-increment-month m y -1)
1613 (calendar-absolute-from-gregorian (list m 1 y))))
1614 (last-date (progn
1615 (calendar-increment-month m y 2)
1616 (calendar-absolute-from-gregorian
1617 (list m (calendar-last-day-of-month m y) y))))
1618 (date (1- first-date))
1619 local-date)
1620 (while (<= (setq date (1+ date)) last-date)
1621 (setq local-date (funcall fromabs date))
1622 (and (or (zerop month)
1623 (= month (calendar-extract-month local-date)))
1624 (or (zerop day)
1625 (= day (calendar-extract-day local-date)))
1626 (or (zerop year)
1627 (= year (calendar-extract-year local-date)))
1628 (calendar-mark-visible-date
1629 (calendar-gregorian-from-absolute date) color)))))
1631 ;; Bahai, Islamic.
1632 (defun calendar-mark-1 (month day year fromabs toabs &optional color)
1633 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1634 The function FROMABS converts absolute dates to the appropriate date system.
1635 The function TOABS carries out the inverse operation. Optional argument
1636 COLOR is passed to `calendar-mark-visible-date' as MARK."
1637 (with-current-buffer calendar-buffer
1638 (if (and (not (zerop month)) (not (zerop day)))
1639 (if (not (zerop year))
1640 ;; Fully specified date.
1641 (let ((date (calendar-gregorian-from-absolute
1642 (funcall toabs (list month day year)))))
1643 (if (calendar-date-is-visible-p date)
1644 (calendar-mark-visible-date date color)))
1645 ;; Month and day in any year--this taken from the holiday stuff.
1646 (let* ((i-date (funcall fromabs
1647 (calendar-absolute-from-gregorian
1648 (list displayed-month 15 displayed-year))))
1649 (m (calendar-extract-month i-date))
1650 (y (calendar-extract-year i-date))
1651 date)
1652 (unless (< m 1) ; calendar doesn't apply
1653 (calendar-increment-month m y (- 10 month))
1654 (and (> m 7) ; date might be visible
1655 (calendar-date-is-visible-p
1656 (setq date (calendar-gregorian-from-absolute
1657 (funcall toabs (list month day y)))))
1658 (calendar-mark-visible-date date color)))))
1659 (calendar-mark-complex month day year
1660 'calendar-bahai-from-absolute color))))
1663 (defun diary-entry-time (s)
1664 "Return time at the beginning of the string S as a military-style integer.
1665 For example, returns 1325 for 1:25pm.
1667 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1668 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1669 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM, XX:XXpm, or XX:XXPM. A period (.) can
1670 be used instead of a colon (:) to separate the hour and minute parts."
1671 (let (case-fold-search)
1672 (cond ((string-match ; military time
1673 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1675 (+ (* 100 (string-to-number (match-string 1 s)))
1676 (string-to-number (match-string 2 s))))
1677 ((string-match ; hour only (XXam or XXpm)
1678 "\\`[ \t\n]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1679 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1680 (if (equal ?a (downcase (aref s (match-beginning 2))))
1681 0 1200)))
1682 ((string-match ; hour and minute (XX:XXam or XX:XXpm)
1683 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1684 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1685 (string-to-number (match-string 2 s))
1686 (if (equal ?a (downcase (aref s (match-beginning 3))))
1687 0 1200)))
1688 (t diary-unknown-time)))) ; unrecognizable
1690 (defun diary-entry-compare (e1 e2)
1691 "Return t if E1 is earlier than E2."
1692 (or (calendar-date-compare e1 e2)
1693 (and (calendar-date-equal (car e1) (car e2))
1694 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1695 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1696 (or (< t1 t2)
1697 (and (= t1 t2)
1698 (string-lessp ts1 ts2)))))))
1700 (defun diary-sort-entries ()
1701 "Sort the list of diary entries by time of day.
1702 If you add this function to `diary-list-entries-hook', it should
1703 be the last item in the hook, in case earlier items add diary
1704 entries, or change the order."
1705 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1707 (define-obsolete-function-alias 'sort-diary-entries 'diary-sort-entries "23.1")
1710 (defun diary-list-sexp-entries (date)
1711 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1712 Also, make them visible in the diary. Returns t if any entries are found.
1714 Sexp diary entries must be prefaced by a `diary-sexp-entry-symbol'
1715 \(normally `%%'). The form of a sexp diary entry is
1717 %%(SEXP) ENTRY
1719 Both ENTRY and DATE are available when the SEXP is evaluated. If
1720 the SEXP returns nil, the diary entry does not apply. If it
1721 returns a non-nil value, ENTRY will be taken to apply to DATE; if
1722 the value is a string, that string will be the diary entry in the
1723 fancy diary display.
1725 For example, the following diary entry will apply to the 21st of
1726 the month if it is a weekday and the Friday before if the 21st is
1727 on a weekend:
1729 &%%(let ((dayname (calendar-day-of-week date))
1730 (day (calendar-extract-day date)))
1732 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1733 (and (memq day '(19 20)) (= dayname 5)))
1734 ) UIUC pay checks deposited
1736 A number of built-in functions are available for this type of
1737 diary entry. In the following, the optional parameter MARK
1738 specifies a face or single-character string to use when
1739 highlighting the day in the calendar. For those functions that
1740 take MONTH, DAY, and YEAR as arguments, the order of the input
1741 parameters changes according to `calendar-date-style' (e.g. to
1742 DAY MONTH YEAR in the European style).
1744 %%(diary-date MONTH DAY YEAR &optional MARK) text
1745 Entry applies if date is MONTH, DAY, YEAR. DAY, MONTH, and YEAR can
1746 be a list of integers, `t' (meaning all values), or an integer.
1748 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1749 Entry will appear on the Nth DAYNAME after/before MONTH DAY.
1750 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
1751 If N>0, use the Nth DAYNAME after MONTH DAY.
1752 If N<0, use the Nth DAYNAME before MONTH DAY.
1753 DAY defaults to 1 if N>0, and MONTH's last day otherwise.
1754 MONTH can be a list of months, a single month, or `t' to
1755 specify all months.
1757 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1758 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1759 inclusive.
1761 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1762 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1763 Text can contain `%d' or `%d%s'; `%d' will be replaced by the
1764 number of years since the MONTH DAY, YEAR, and `%s' by the
1765 ordinal ending of that number (i.e. `st', `nd', `rd' or `th',
1766 as appropriate). The anniversary of February 29 is
1767 considered to be March 1 in a non-leap year.
1769 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1770 Entry will appear every N days, starting MONTH DAY, YEAR.
1771 Text can contain `%d' or `%d%s'; `%d' will be replaced by the
1772 number of repetitions since the MONTH DAY, YEAR and `%s' by
1773 the ordinal ending of that number (i.e. `st', `nd', `rd' or
1774 `th', as appropriate).
1776 %%(diary-remind SEXP DAYS &optional MARKING) text
1777 Entry is a reminder for diary sexp SEXP. DAYS is either a
1778 single number or a list of numbers indicating the number(s)
1779 of days before the event that the warning(s) should occur.
1780 A negative number -DAYS has the same meaning as a list (1 2 ... DAYS).
1781 If the current date is (one of) DAYS before the event indicated
1782 by EXPR, then a suitable message (as specified by
1783 `diary-remind-message') appears. In addition to the
1784 reminders beforehand, the diary entry also appears on the
1785 date itself. If optional MARKING is non-nil then the
1786 *reminders* are marked on the calendar. Marking of reminders
1787 is independent of whether the entry *itself* is a marking or
1788 non-marking one.
1790 %%(diary-hebrew-yahrzeit MONTH DAY YEAR) text
1791 Text is assumed to be the name of the person; the date is the
1792 date of death on the *civil* calendar. The diary entry will
1793 appear on the proper Hebrew-date anniversary and on the day
1794 before.
1796 All the remaining functions do not accept any text, and so only
1797 make sense with `diary-fancy-display'. Most produce output every day.
1799 `diary-day-of-year' - day of year and number of days remaining
1800 `diary-iso-date' - ISO commercial date
1801 `diary-astro-day-number' - astronomical (Julian) day number
1802 `diary-sunrise-sunset' - local times of sunrise and sunset
1804 These functions give the date in alternative calendrical systems:
1806 `diary-bahai-date', `diary-chinese-date', `diary-coptic-date',
1807 `diary-ethiopic-date', `diary-french-date', `diary-hebrew-date',
1808 `diary-islamic-date', `diary-julian-date', `diary-mayan-date',
1809 `diary-persian-date'
1811 Theses functions only produce output on certain dates:
1813 `diary-lunar-phases' - phases of moon (on the appropriate days)
1814 `diary-hebrew-omer' - Omer count, within 50 days after Passover
1815 `diary-hebrew-parasha' - weekly parasha, every Saturday
1816 `diary-hebrew-rosh-hodesh' - Rosh Hodesh, or the day or Saturday before
1817 `diary-hebrew-sabbath-candles' - local time of candle lighting, on Fridays
1820 Marking these entries is *extremely* time consuming, so it is
1821 best if they are non-marking."
1822 (let ((s-entry (format "^%s?%s(" (regexp-quote diary-nonmarking-symbol)
1823 (regexp-quote diary-sexp-entry-symbol)))
1824 entry-found file-glob-attrs marks
1825 sexp-start sexp entry specifier entry-start line-start
1826 diary-entry temp literal)
1827 (goto-char (point-min))
1828 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1829 (while (re-search-forward s-entry nil t)
1830 (backward-char 1)
1831 (setq sexp-start (point))
1832 (forward-sexp)
1833 (setq sexp (buffer-substring-no-properties sexp-start (point))
1834 line-start (line-end-position 0)
1835 specifier
1836 (buffer-substring-no-properties (1+ line-start) (point))
1837 entry-start (1+ line-start))
1838 (forward-char 1)
1839 (if (and (bolp) (not (looking-at "[ \t]")))
1840 ;; Diary entry consists only of the sexp.
1841 (progn
1842 (backward-char 1)
1843 (setq entry ""))
1844 (setq entry-start (point))
1845 (forward-line 1)
1846 (while (looking-at "[ \t]")
1847 (forward-line 1))
1848 (if (bolp) (backward-char 1))
1849 (setq entry (buffer-substring-no-properties entry-start (point))))
1850 (setq diary-entry (diary-sexp-entry sexp entry date)
1851 literal entry ; before evaluation
1852 entry (if (consp diary-entry)
1853 (cdr diary-entry)
1854 diary-entry))
1855 (when diary-entry
1856 (remove-overlays line-start (point) 'invisible 'diary)
1857 (if (< 0 (length entry))
1858 (setq temp (diary-pull-attrs entry file-glob-attrs)
1859 entry (nth 0 temp)
1860 marks (nth 1 temp))))
1861 (diary-add-to-list date entry specifier
1862 (if entry-start (copy-marker entry-start))
1863 marks literal)
1864 (setq entry-found (or entry-found diary-entry)))
1865 entry-found))
1867 (define-obsolete-function-alias 'list-sexp-diary-entries
1868 'diary-list-sexp-entries "23.1")
1870 (defun diary-make-date (a b c)
1871 "Convert A B C into the internal calendar date form.
1872 The expected order of the inputs depends on `calendar-date-style',
1873 e.g. in the European case, A = day, B = month, C = year. Returns
1874 a list (MONTH DAY YEAR), i.e. the American style, which is the
1875 form used internally by the calendar and diary."
1876 (cond ((eq calendar-date-style 'iso) ; YMD
1877 (list b c a))
1878 ((eq calendar-date-style 'european) ; DMY
1879 (list b a c))
1880 (t (list a b c))))
1883 ;;; Sexp diary functions.
1885 (defvar date)
1886 (defvar entry)
1888 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1889 (defun diary-date (month day year &optional mark)
1890 "Specific date(s) diary entry.
1891 Entry applies if date is MONTH, DAY, YEAR. Each parameter can be a
1892 list of integers, `t' (meaning all values), or an integer. The order
1893 of the input parameters changes according to `calendar-date-style'
1894 \(e.g. to DAY MONTH YEAR in the European style).
1896 An optional parameter MARK specifies a face or single-character string
1897 to use when highlighting the day in the calendar."
1898 (let* ((ddate (diary-make-date month day year))
1899 (dd (calendar-extract-day ddate))
1900 (mm (calendar-extract-month ddate))
1901 (yy (calendar-extract-year ddate))
1902 (m (calendar-extract-month date))
1903 (y (calendar-extract-year date))
1904 (d (calendar-extract-day date)))
1905 (and
1906 (or (and (listp dd) (memq d dd))
1907 (equal d dd)
1908 (eq dd t))
1909 (or (and (listp mm) (memq m mm))
1910 (equal m mm)
1911 (eq mm t))
1912 (or (and (listp yy) (memq y yy))
1913 (equal y yy)
1914 (eq yy t))
1915 (cons mark entry))))
1917 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1918 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1919 "Block diary entry.
1920 Entry applies if date is between, or on one of, two dates. The order
1921 of the input parameters changes according to `calendar-date-style'
1922 \(e.g. to D1, M1, Y1, D2, M2, Y2 in the European style).
1924 An optional parameter MARK specifies a face or single-character string
1925 to use when highlighting the day in the calendar."
1926 (let ((date1 (calendar-absolute-from-gregorian
1927 (diary-make-date m1 d1 y1)))
1928 (date2 (calendar-absolute-from-gregorian
1929 (diary-make-date m2 d2 y2)))
1930 (d (calendar-absolute-from-gregorian date)))
1931 (and (<= date1 d) (<= d date2)
1932 (cons mark entry))))
1934 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1935 (defun diary-float (month dayname n &optional day mark)
1936 "Diary entry for the Nth DAYNAME after/before MONTH DAY.
1937 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
1938 If N>0, use the Nth DAYNAME after MONTH DAY.
1939 If N<0, use the Nth DAYNAME before MONTH DAY.
1940 DAY defaults to 1 if N>0, and MONTH's last day otherwise.
1941 MONTH can be a list of months, an integer, or `t' (meaning all months).
1942 Optional MARK specifies a face or single-character string to use when
1943 highlighting the day in the calendar."
1944 ;; This is messy because the diary entry may apply, but the date on which it
1945 ;; is based can be in a different month/year. For example, asking for the
1946 ;; first Monday after December 30. For large values of |n| the problem is
1947 ;; more grotesque.
1948 (and (= dayname (calendar-day-of-week date))
1949 (let* ((m (calendar-extract-month date))
1950 (d (calendar-extract-day date))
1951 (y (calendar-extract-year date))
1952 ;; Last (n>0) or first (n<0) possible base date for entry.
1953 (limit
1954 (calendar-nth-named-absday (- n) dayname m y d))
1955 (last-abs (if (> n 0) limit (+ limit 6)))
1956 (first-abs (if (> n 0) (- limit 6) limit))
1957 (last (calendar-gregorian-from-absolute last-abs))
1958 (first (calendar-gregorian-from-absolute first-abs))
1959 ;; m1, d1 is first possible base date.
1960 (m1 (calendar-extract-month first))
1961 (d1 (calendar-extract-day first))
1962 (y1 (calendar-extract-year first))
1963 ;; m2, d2 is last possible base date.
1964 (m2 (calendar-extract-month last))
1965 (d2 (calendar-extract-day last))
1966 (y2 (calendar-extract-year last)))
1967 (if (or (and (= m1 m2) ; only possible base dates in one month
1968 (or (eq month t)
1969 (if (listp month)
1970 (memq m1 month)
1971 (= m1 month)))
1972 (let ((d (or day (if (> n 0)
1974 (calendar-last-day-of-month m1 y1)))))
1975 (and (<= d1 d) (<= d d2))))
1976 ;; Only possible base dates straddle two months.
1977 (and (or (< y1 y2)
1978 (and (= y1 y2) (< m1 m2)))
1980 ;; m1, d1 works as a base date.
1981 (and
1982 (or (eq month t)
1983 (if (listp month)
1984 (memq m1 month)
1985 (= m1 month)))
1986 (<= d1 (or day (if (> n 0)
1988 (calendar-last-day-of-month m1 y1)))))
1989 ;; m2, d2 works as a base date.
1990 (and (or (eq month t)
1991 (if (listp month)
1992 (memq m2 month)
1993 (= m2 month)))
1994 (<= (or day (if (> n 0)
1996 (calendar-last-day-of-month m2 y2)))
1997 d2)))))
1998 (cons mark entry)))))
2000 (defun diary-ordinal-suffix (n)
2001 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
2002 (if (or (memq (% n 100) '(11 12 13))
2003 (< 3 (% n 10)))
2004 "th"
2005 (aref ["th" "st" "nd" "rd"] (% n 10))))
2007 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
2008 (defun diary-anniversary (month day &optional year mark)
2009 "Anniversary diary entry.
2010 Entry applies if date is the anniversary of MONTH, DAY, YEAR.
2011 The order of the input parameters changes according to
2012 `calendar-date-style' (e.g. to DAY MONTH YEAR in the European style).
2014 The diary entry can contain `%d' or `%d%s'; the %d will be replaced
2015 by the number of years since the MONTH, DAY, YEAR, and the %s will
2016 be replaced by the ordinal ending of that number (that is, `st',
2017 `nd', `rd' or `th', as appropriate). The anniversary of February 29
2018 is considered to be March 1 in non-leap years.
2020 An optional parameter MARK specifies a face or single-character
2021 string to use when highlighting the day in the calendar."
2022 (let* ((ddate (diary-make-date month day year))
2023 (dd (calendar-extract-day ddate))
2024 (mm (calendar-extract-month ddate))
2025 (yy (calendar-extract-year ddate))
2026 (y (calendar-extract-year date))
2027 (diff (if yy (- y yy) 100)))
2028 (and (= mm 2) (= dd 29) (not (calendar-leap-year-p y))
2029 (setq mm 3
2030 dd 1))
2031 (and (> diff 0) (calendar-date-equal (list mm dd y) date)
2032 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
2034 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
2035 (defun diary-cyclic (n month day year &optional mark)
2036 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
2037 The order of the input parameters changes according to
2038 `calendar-date-style' (e.g. to N DAY MONTH YEAR in the European
2039 style). The entry can contain `%d' or `%d%s'; the %d will be
2040 replaced by the number of repetitions since the MONTH DAY YEAR,
2041 and %s by the ordinal ending of that number (that is, `st', `nd',
2042 `rd' or `th', as appropriate).
2044 An optional parameter MARK specifies a face or single-character
2045 string to use when highlighting the day in the calendar."
2046 (or (> n 0)
2047 (error "Day count must be positive"))
2048 (let* ((diff (- (calendar-absolute-from-gregorian date)
2049 (calendar-absolute-from-gregorian
2050 (diary-make-date month day year))))
2051 (cycle (/ diff n)))
2052 (and (>= diff 0) (zerop (% diff n))
2053 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
2055 (defun diary-day-of-year ()
2056 "Day of year and number of days remaining in the year of date diary entry."
2057 (calendar-day-of-year-string date))
2059 (defun diary-remind (sexp days &optional marking)
2060 "Provide a reminder of a diary entry.
2061 SEXP is a diary-sexp. DAYS is either a single number or a list
2062 of numbers indicating the number(s) of days before the event that
2063 the warning(s) should occur on. A negative number -DAYS has the
2064 same meaning as a list (1 2 ... DAYS). If the current date
2065 is (one of) DAYS before the event indicated by SEXP, then this function
2066 returns a suitable message (as specified by `diary-remind-message').
2068 In addition to the reminders beforehand, the diary entry also
2069 appears on the date itself.
2071 A `diary-nonmarking-symbol' at the beginning of the line of the
2072 `diary-remind' entry specifies that the diary entry (not the
2073 reminder) is non-marking. Marking of reminders is independent of
2074 whether the entry itself is a marking or nonmarking; if optional
2075 parameter MARKING is non-nil then the reminders are marked on the
2076 calendar."
2077 ;; `date' has a value at this point, from diary-sexp-entry.
2078 ;; Convert a negative number to a list of days.
2079 (and (integerp days)
2080 (< days 0)
2081 (setq days (number-sequence 1 (- days))))
2082 (let ((diary-entry (eval sexp)))
2083 (cond
2084 ;; Diary entry applies on date.
2085 ((and diary-entry
2086 (or (not diary-marking-entries-flag) diary-marking-entry-flag))
2087 diary-entry)
2088 ;; Diary entry may apply to `days' before date.
2089 ((and (integerp days)
2090 (not diary-entry) ; diary entry does not apply to date
2091 (or (not diary-marking-entries-flag) marking))
2092 ;; Adjust date, and re-evaluate.
2093 (let ((date (calendar-gregorian-from-absolute
2094 (+ (calendar-absolute-from-gregorian date) days))))
2095 (when (setq diary-entry (eval sexp))
2096 ;; Discard any mark portion from diary-anniversary, etc.
2097 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
2098 (mapconcat 'eval diary-remind-message ""))))
2099 ;; Diary entry may apply to one of a list of days before date.
2100 ((and (listp days) days)
2101 (or (diary-remind sexp (car days) marking)
2102 (diary-remind sexp (cdr days) marking))))))
2105 ;;; Diary insertion functions.
2107 ;;;###cal-autoload
2108 (defun diary-make-entry (string &optional nonmarking file)
2109 "Insert a diary entry STRING which may be NONMARKING in FILE.
2110 If omitted, NONMARKING defaults to nil and FILE defaults to
2111 `diary-file'."
2112 (let ((pop-up-frames (or pop-up-frames
2113 (window-dedicated-p (selected-window)))))
2114 (find-file-other-window (or file diary-file)))
2115 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
2116 (widen)
2117 (diary-unhide-everything)
2118 (goto-char (point-max))
2119 (when (let ((case-fold-search t))
2120 (search-backward "Local Variables:"
2121 (max (- (point-max) 3000) (point-min))
2123 (beginning-of-line)
2124 (insert "\n")
2125 (forward-line -1))
2126 (insert
2127 (if (bolp) "" "\n")
2128 (if nonmarking diary-nonmarking-symbol "")
2129 string " "))
2131 ;;;###cal-autoload
2132 (define-obsolete-function-alias 'make-diary-entry 'diary-make-entry "23.1")
2134 ;;;###cal-autoload
2135 (defun diary-insert-entry (arg &optional event)
2136 "Insert a diary entry for the date indicated by point.
2137 Prefix argument ARG makes the entry nonmarking."
2138 (interactive
2139 (list current-prefix-arg last-nonmenu-event))
2140 (diary-make-entry (calendar-date-string (calendar-cursor-to-date t event) t t)
2141 arg))
2143 ;;;###cal-autoload
2144 (define-obsolete-function-alias 'insert-diary-entry 'diary-insert-entry "23.1")
2146 ;;;###cal-autoload
2147 (defun diary-insert-weekly-entry (arg)
2148 "Insert a weekly diary entry for the day of the week indicated by point.
2149 Prefix argument ARG makes the entry nonmarking."
2150 (interactive "P")
2151 (diary-make-entry (calendar-day-name (calendar-cursor-to-date t))
2152 arg))
2154 ;;;###cal-autoload
2155 (define-obsolete-function-alias 'insert-weekly-diary-entry
2156 'diary-insert-weekly-entry "23.1")
2158 (defun diary-date-display-form (&optional type)
2159 "Return value for `calendar-date-display-form' using `calendar-date-style'.
2160 Optional symbol TYPE is either `monthly' or `yearly'."
2161 (cond ((eq type 'monthly) (cond ((eq calendar-date-style 'iso)
2162 '((format "*-*-%.2d"
2163 (string-to-number day))))
2164 ((eq calendar-date-style 'european)
2165 '(day " * "))
2166 (t '("* " day ))))
2167 ((eq type 'yearly) (cond ((eq calendar-date-style 'iso)
2168 '((format "*-%.2d-%.2d"
2169 (string-to-number month)
2170 (string-to-number day))))
2171 ((eq calendar-date-style 'european)
2172 '(day " " monthname))
2173 (t '(monthname " " day))))
2174 ;; Iso cannot contain "-", because this form used eg by
2175 ;; diary-insert-anniversary-entry.
2176 (t (cond ((eq calendar-date-style 'iso)
2177 '((format "%s %.2d %.2d" year
2178 (string-to-number month) (string-to-number day))))
2179 ((eq calendar-date-style 'european)
2180 '(day " " month " " year))
2181 (t '(month " " day " " year))))))
2183 (defun diary-insert-entry-1 (&optional type nomark months symbol absfunc)
2184 "Subroutine to insert a diary entry related to the date at point.
2185 TYPE is the type of entry (`monthly' or `yearly'). NOMARK non-nil
2186 means make the entry non-marking. Array MONTHS is used in place
2187 of `calendar-month-name-array'. String SYMBOL marks the type of
2188 diary entry. Function ABSFUNC converts absolute dates to dates of
2189 the appropriate type."
2190 (let ((calendar-date-display-form (if type
2191 (diary-date-display-form type)
2192 calendar-date-display-form))
2193 (calendar-month-name-array (or months calendar-month-name-array))
2194 (date (calendar-cursor-to-date t)))
2195 (diary-make-entry
2196 (format "%s%s" (or symbol "")
2197 (calendar-date-string
2198 (if absfunc
2199 (funcall absfunc (calendar-absolute-from-gregorian date))
2200 date)
2201 (not absfunc)
2202 (not type)))
2203 nomark)))
2205 ;;;###cal-autoload
2206 (defun diary-insert-monthly-entry (arg)
2207 "Insert a monthly diary entry for the day of the month indicated by point.
2208 Prefix argument ARG makes the entry nonmarking."
2209 (interactive "P")
2210 (diary-insert-entry-1 'monthly arg))
2212 ;;;###cal-autoload
2213 (define-obsolete-function-alias 'insert-monthly-diary-entry
2214 'diary-insert-monthly-entry "23.1")
2216 ;;;###cal-autoload
2217 (defun diary-insert-yearly-entry (arg)
2218 "Insert an annual diary entry for the day of the year indicated by point.
2219 Prefix argument ARG makes the entry nonmarking."
2220 (interactive "P")
2221 (diary-insert-entry-1 'yearly arg))
2223 ;;;###cal-autoload
2224 (define-obsolete-function-alias 'insert-yearly-diary-entry
2225 'diary-insert-yearly-entry "23.1")
2227 ;;;###cal-autoload
2228 (defun diary-insert-anniversary-entry (arg)
2229 "Insert an anniversary diary entry for the date given by point.
2230 Prefix argument ARG makes the entry nonmarking."
2231 (interactive "P")
2232 (let ((calendar-date-display-form (diary-date-display-form)))
2233 (diary-make-entry
2234 (format "%s(diary-anniversary %s)"
2235 diary-sexp-entry-symbol
2236 (calendar-date-string (calendar-cursor-to-date t) nil t))
2237 arg)))
2239 ;;;###cal-autoload
2240 (define-obsolete-function-alias 'insert-anniversary-diary-entry
2241 'diary-insert-anniversary-entry "23.1")
2243 ;;;###cal-autoload
2244 (defun diary-insert-block-entry (arg)
2245 "Insert a block diary entry for the days between the point and marked date.
2246 Prefix argument ARG makes the entry nonmarking."
2247 (interactive "P")
2248 (let ((calendar-date-display-form (diary-date-display-form))
2249 (cursor (calendar-cursor-to-date t))
2250 (mark (or (car calendar-mark-ring)
2251 (error "No mark set in this buffer")))
2252 start end)
2253 (if (< (calendar-absolute-from-gregorian mark)
2254 (calendar-absolute-from-gregorian cursor))
2255 (setq start mark
2256 end cursor)
2257 (setq start cursor
2258 end mark))
2259 (diary-make-entry
2260 (format "%s(diary-block %s %s)"
2261 diary-sexp-entry-symbol
2262 (calendar-date-string start nil t)
2263 (calendar-date-string end nil t))
2264 arg)))
2266 ;;;###cal-autoload
2267 (define-obsolete-function-alias 'insert-block-diary-entry
2268 'diary-insert-block-entry "23.1")
2270 ;;;###cal-autoload
2271 (defun diary-insert-cyclic-entry (arg)
2272 "Insert a cyclic diary entry starting at the date given by point.
2273 Prefix argument ARG makes the entry nonmarking."
2274 (interactive "P")
2275 (let ((calendar-date-display-form (diary-date-display-form)))
2276 (diary-make-entry
2277 (format "%s(diary-cyclic %d %s)"
2278 diary-sexp-entry-symbol
2279 (calendar-read "Repeat every how many days: "
2280 (lambda (x) (> x 0)))
2281 (calendar-date-string (calendar-cursor-to-date t) nil t))
2282 arg)))
2284 ;;;###cal-autoload
2285 (define-obsolete-function-alias 'insert-cyclic-diary-entry
2286 'diary-insert-cyclic-entry "23.1")
2288 ;;; Diary mode.
2290 (defun diary-redraw-calendar ()
2291 "If `calendar-buffer' is live and diary entries are marked, redraw it."
2292 (and calendar-mark-diary-entries-flag
2293 (save-excursion
2294 (calendar-redraw)))
2295 ;; Return value suitable for `write-contents-functions'.
2296 nil)
2298 (defvar diary-mode-map
2299 (let ((map (make-sparse-keymap)))
2300 (define-key map "\C-c\C-s" 'diary-show-all-entries)
2301 (define-key map "\C-c\C-q" 'quit-window)
2302 map)
2303 "Keymap for `diary-mode'.")
2305 (defun diary-font-lock-sexps (limit)
2306 "Recognize sexp diary entry up to LIMIT for font-locking."
2307 (if (re-search-forward
2308 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2309 (regexp-quote diary-sexp-entry-symbol))
2310 limit t)
2311 (condition-case nil
2312 (save-restriction
2313 (narrow-to-region (point-min) limit)
2314 (let ((start (point)))
2315 (forward-sexp 1)
2316 (store-match-data (list start (point)))
2318 (error t))))
2320 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
2321 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2322 If given, optional SYMBOL must be a prefix to entries. If
2323 optional ABBREV-ARRAY is present, also matches the abbreviations
2324 from this array (with or without a final `.'), in addition to the
2325 full month names."
2326 (let ((dayname (diary-name-pattern calendar-day-name-array
2327 calendar-day-abbrev-array t))
2328 (monthname (format "\\(%s\\|\\*\\)"
2329 (diary-name-pattern month-array abbrev-array)))
2330 (month "\\([0-9]+\\|\\*\\)")
2331 (day "\\([0-9]+\\|\\*\\)")
2332 (year "-?\\([0-9]+\\|\\*\\)"))
2333 (mapcar (lambda (x)
2334 (cons
2335 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
2336 (if symbol (regexp-quote symbol) "") "\\("
2337 (mapconcat 'eval
2338 ;; If backup, omit first item (backup)
2339 ;; and last item (not part of date).
2340 (if (equal (car x) 'backup)
2341 (nreverse (cdr (reverse (cdr x))))
2344 ;; With backup, last item is not part of date.
2345 (if (equal (car x) 'backup)
2346 (concat "\\)" (eval (car (reverse x))))
2347 "\\)"))
2348 '(1 diary-face)))
2349 diary-date-forms)))
2351 (defmacro diary-font-lock-keywords-1 (markfunc listfunc feature months symbol)
2352 "Subroutine of the function `diary-font-lock-keywords'.
2353 If MARKFUNC is a member of `diary-nongregorian-marking-hook', or
2354 LISTFUNC of `diary-nongregorian-listing-hook', then require FEATURE and
2355 return a font-lock pattern matching array of MONTHS and marking SYMBOL."
2356 `(when (or (memq ',markfunc diary-nongregorian-marking-hook)
2357 (memq ',listfunc diary-nongregorian-listing-hook))
2358 (require ',feature)
2359 (diary-font-lock-date-forms ,months ,symbol)))
2361 (defconst diary-time-regexp
2362 ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
2363 ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
2364 ;; Hence often prefix this with "\\(^\\|\\s-\\)."
2365 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
2366 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
2367 "\\)\\([AaPp][Mm]\\)?\\)")
2368 "Regular expression matching a time of day.")
2370 (defvar calendar-hebrew-month-name-array-leap-year)
2371 (defvar calendar-islamic-month-name-array)
2372 (defvar calendar-bahai-month-name-array)
2374 ;;;###cal-autoload
2375 (defun diary-font-lock-keywords ()
2376 "Return a value for the variable `diary-font-lock-keywords'."
2377 (append
2378 (diary-font-lock-date-forms calendar-month-name-array
2379 nil calendar-month-abbrev-array)
2380 (diary-font-lock-keywords-1 diary-hebrew-mark-entries
2381 diary-hebrew-list-entries
2382 cal-hebrew
2383 calendar-hebrew-month-name-array-leap-year
2384 diary-hebrew-entry-symbol)
2385 (diary-font-lock-keywords-1 diary-islamic-mark-entries
2386 diary-islamic-list-entries
2387 cal-islam
2388 calendar-islamic-month-name-array
2389 diary-islamic-entry-symbol)
2390 (diary-font-lock-keywords-1 diary-bahai-mark-entries
2391 diary-bahai-list-entries
2392 cal-bahai
2393 calendar-bahai-month-name-array
2394 diary-bahai-entry-symbol)
2395 (list
2396 (cons
2397 (format "^%s.*$" (regexp-quote diary-include-string))
2398 'font-lock-keyword-face)
2399 (cons
2400 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2401 (regexp-quote diary-sexp-entry-symbol))
2402 '(1 font-lock-constant-face))
2403 (cons
2404 (format "^%s" (regexp-quote diary-nonmarking-symbol))
2405 'font-lock-constant-face)
2406 (cons
2407 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol)
2408 (regexp-opt (mapcar 'regexp-quote
2409 (list diary-hebrew-entry-symbol
2410 diary-islamic-entry-symbol
2411 diary-bahai-entry-symbol))
2413 '(1 font-lock-constant-face))
2414 '(diary-font-lock-sexps . font-lock-keyword-face)
2415 ;; Don't need to worry about space around "-" because the first
2416 ;; match takes care of that. It does mean the "-" itself may or
2417 ;; may not be fontified though.
2418 ;; diary-date-forms often include a final character that is not
2419 ;; part of the date (eg a non-digit to mark the end of the year).
2420 ;; This can use up the only space char between a date and time (b#7891).
2421 ;; Hence we use OVERRIDE, which can only override whitespace.
2422 ;; FIXME it's probably better to tighten up the diary-time-regexp
2423 ;; and drop the whitespace requirement below.
2424 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2425 diary-time-regexp)
2426 . (0 'diary-time t)))))
2427 ; . 'diary-time))))
2429 (defvar diary-font-lock-keywords (diary-font-lock-keywords)
2430 "Forms to highlight in `diary-mode'.")
2432 ;;;###autoload
2433 (define-derived-mode diary-mode fundamental-mode "Diary"
2434 "Major mode for editing the diary file."
2435 (set (make-local-variable 'font-lock-defaults)
2436 '(diary-font-lock-keywords t))
2437 (set (make-local-variable 'comment-start) diary-comment-start)
2438 (set (make-local-variable 'comment-end) diary-comment-end)
2439 (add-to-invisibility-spec '(diary . nil))
2440 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
2441 ;; In case the file was modified externally, refresh the calendar
2442 ;; after refreshing the diary buffer.
2443 (add-hook 'after-revert-hook 'diary-redraw-calendar nil t)
2444 (if diary-header-line-flag
2445 (setq header-line-format diary-header-line-format)))
2448 ;;; Fancy Diary Mode.
2450 (defun diary-fancy-date-pattern ()
2451 "Return a regexp matching the first line of a fancy diary date header.
2452 This depends on the calendar date style."
2453 (concat
2454 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
2455 (monthname (diary-name-pattern calendar-month-name-array nil t))
2456 (day "1")
2457 (month "2")
2458 ;; FIXME? This used to be "-?[0-9]+" - what was the "-?" for?
2459 (year "3"))
2460 ;; This is ugly. c-d-d-form expects `day' etc to be "numbers in
2461 ;; string form"; eg the iso version calls string-to-number on some.
2462 ;; Therefore we cannot eg just let day = "[0-9]+". (Bug#8583).
2463 ;; Assumes no integers in c-day/month-name-array.
2464 (replace-regexp-in-string "[0-9]+" "[0-9]+"
2465 (mapconcat 'eval calendar-date-display-form "")
2466 nil t))
2467 ;; Optional ": holiday name" after the date.
2468 "\\(: .*\\)?"))
2470 (defun diary-fancy-date-matcher (limit)
2471 "Search for a fancy diary data header, up to LIMIT."
2472 ;; Any number of " other holiday name" lines, followed by "==" line.
2473 (when (re-search-forward
2474 (format "%s\\(\n +.*\\)*\n=+$" (diary-fancy-date-pattern)) limit t)
2475 (put-text-property (match-beginning 0) (match-end 0) 'font-lock-multiline t)
2478 (define-obsolete-variable-alias 'fancy-diary-font-lock-keywords
2479 'diary-fancy-font-lock-keywords "23.1")
2481 (defvar diary-fancy-font-lock-keywords
2482 `((diary-fancy-date-matcher . diary-face)
2483 ("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" . 'diary-anniversary)
2484 ("^.*Yahrzeit.*$" . font-lock-constant-face)
2485 ("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
2486 ("^Day.*omer.*$" . font-lock-builtin-face)
2487 ("^Parashat.*$" . font-lock-comment-face)
2488 (,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2489 diary-time-regexp) . 'diary-time))
2490 "Keywords to highlight in fancy diary display.")
2492 ;; If region looks like it might start or end in the middle of a
2493 ;; multiline pattern, extend the region to encompass the whole pattern.
2494 (defun diary-fancy-font-lock-fontify-region-function (beg end &optional verbose)
2495 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2496 Needed to handle multiline keyword in `diary-fancy-font-lock-keywords'.
2497 Fontify the region between BEG and END, quietly unless VERBOSE is non-nil."
2498 (goto-char beg)
2499 (forward-line 0)
2500 (if (looking-at "=+$") (forward-line -1))
2501 (while (and (looking-at " +[^ ]")
2502 (zerop (forward-line -1))))
2503 ;; This check not essential.
2504 (if (looking-at (diary-fancy-date-pattern))
2505 (setq beg (line-beginning-position)))
2506 (goto-char end)
2507 (forward-line 0)
2508 (while (and (looking-at " +[^ ]")
2509 (zerop (forward-line 1))))
2510 (if (looking-at "=+$")
2511 (setq end (line-beginning-position 2)))
2512 (font-lock-default-fontify-region beg end verbose))
2514 (defvar diary-fancy-overriding-map (make-sparse-keymap)
2515 "Keymap overriding minor-mode maps in `diary-fancy-display-mode'.")
2517 (define-derived-mode diary-fancy-display-mode special-mode
2518 "Diary"
2519 "Major mode used while displaying diary entries using Fancy Display."
2520 (set (make-local-variable 'font-lock-defaults)
2521 '(diary-fancy-font-lock-keywords
2522 t nil nil nil
2523 (font-lock-fontify-region-function
2524 . diary-fancy-font-lock-fontify-region-function)))
2525 (set (make-local-variable 'minor-mode-overriding-map-alist)
2526 (list (cons t diary-fancy-overriding-map)))
2527 (view-mode 1))
2529 (define-obsolete-function-alias 'fancy-diary-display-mode
2530 'diary-fancy-display-mode "23.1")
2532 ;; Following code from Dave Love <fx@gnu.org>.
2533 ;; Import Outlook-format appointments from mail messages in Gnus or
2534 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2535 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2536 ;; could be run from hooks to notice appointments automatically (in
2537 ;; which case they will prompt about adding to the diary). The
2538 ;; message formats recognized are customizable through `diary-outlook-formats'.
2540 (defun diary-from-outlook-internal (subject body &optional test-only)
2541 "Snarf a diary entry from a message assumed to be from MS Outlook.
2542 SUBJECT and BODY are strings giving the message subject and body.
2543 Arg TEST-ONLY non-nil means return non-nil if and only if the
2544 message contains an appointment, don't make a diary entry."
2545 (catch 'finished
2546 (let (format-string)
2547 (dolist (fmt diary-outlook-formats)
2548 (when (eq 0 (string-match (car fmt) body))
2549 (unless test-only
2550 (setq format-string (cdr fmt))
2551 (save-excursion
2552 (save-window-excursion
2553 (diary-make-entry
2554 (format (replace-match (if (functionp format-string)
2555 (funcall format-string body)
2556 format-string)
2557 t nil (match-string 0 body))
2558 subject)))))
2559 (throw 'finished t))))
2560 nil))
2562 (defvar gnus-article-mime-handles)
2563 (defvar gnus-article-buffer)
2565 (autoload 'gnus-fetch-field "gnus-util")
2566 (autoload 'gnus-narrow-to-body "gnus")
2567 (autoload 'mm-get-part "mm-decode")
2569 (defun diary-from-outlook-gnus (&optional noconfirm)
2570 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2571 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2572 this function is called interactively), then if an entry is found the
2573 user is asked to confirm its addition.
2574 Add this function to `gnus-article-prepare-hook' to notice appointments
2575 automatically."
2576 (interactive "p")
2577 (with-current-buffer gnus-article-buffer
2578 (let ((subject (gnus-fetch-field "subject"))
2579 (body (if gnus-article-mime-handles
2580 ;; We're multipart. Don't get confused by part
2581 ;; buttons &c. Assume info is in first part.
2582 (mm-get-part (nth 1 gnus-article-mime-handles))
2583 (save-restriction
2584 (gnus-narrow-to-body)
2585 (buffer-string)))))
2586 (when (diary-from-outlook-internal subject body t)
2587 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2588 (diary-from-outlook-internal subject body)
2589 (message "Diary entry added"))))))
2591 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2593 (defvar rmail-buffer)
2595 (defun diary-from-outlook-rmail (&optional noconfirm)
2596 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2597 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2598 this function is called interactively), then if an entry is found the
2599 user is asked to confirm its addition."
2600 (interactive "p")
2601 ;; FIXME maybe the body needs rmail-mm decoding, in which case
2602 ;; there is no single buffer with both body and subject, sigh.
2603 (with-current-buffer rmail-buffer
2604 (let ((subject (mail-fetch-field "subject"))
2605 (body (buffer-substring (save-excursion
2606 (rfc822-goto-eoh)
2607 (point))
2608 (point-max))))
2609 (when (diary-from-outlook-internal subject body t)
2610 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2611 (diary-from-outlook-internal subject body)
2612 (message "Diary entry added"))))))
2614 (defun diary-from-outlook (&optional noconfirm)
2615 "Maybe snarf diary entry from current Outlook-generated message.
2616 Currently knows about Gnus and Rmail modes. Unless the optional
2617 argument NOCONFIRM is non-nil (which is the case when this
2618 function is called interactively), then if an entry is found the
2619 user is asked to confirm its addition."
2620 (interactive "p")
2621 (let ((func (cond
2622 ((eq major-mode 'rmail-mode)
2623 #'diary-from-outlook-rmail)
2624 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2625 #'diary-from-outlook-gnus)
2626 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2627 (funcall func noconfirm)))
2629 (provide 'diary-lib)
2631 ;; Local Variables:
2632 ;; coding: utf-8
2633 ;; End:
2635 ;;; diary-lib.el ends here