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