Fix calendar-mark-1
[emacs.git] / lisp / calendar / diary-lib.el
blob128b62980684e7b103162fa7294ba61185d9cfea
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 (or (get-buffer diary-fancy-buffer)
472 (and diary-file (find-buffer-visiting diary-file))))
474 ;;;###cal-autoload
475 (defun diary-set-maybe-redraw (symbol value)
476 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
477 Redraws the diary if it is being displayed (note this is not the same as
478 just visiting the `diary-file'), and SYMBOL's value is to be changed."
479 (let ((oldvalue (symbol-value symbol)))
480 (custom-set-default symbol value)
481 (and (not (equal value oldvalue))
482 (diary-live-p)
483 ;; Note this assumes diary was called without prefix arg.
484 (diary))))
486 (define-obsolete-variable-alias 'number-of-diary-entries
487 'diary-number-of-entries "23.1")
489 (defcustom diary-number-of-entries 1
490 "Specifies how many days of diary entries are to be displayed initially.
491 This variable affects the diary display when the command \\[diary] is
492 used, or if the value of the variable `calendar-view-diary-initially-flag'
493 is non-nil. For example, if the default value 1 is used, then only the
494 current day's diary entries will be displayed. If the value 2 is used,
495 then both the current day's and the next day's entries will be displayed.
497 The value can also be a vector such as [0 2 2 2 2 4 1]; this value says
498 to display no diary entries on Sunday, the entries for the current date
499 and the day after on Monday through Thursday, Friday through Monday's
500 entries on Friday, and only Saturday's entries on Saturday.
502 This variable does not affect the diary display with the `d' command
503 from the calendar; in that case, the prefix argument controls the number
504 of days of diary entries displayed."
505 :type '(choice (integer :tag "Entries")
506 (vector :value [0 0 0 0 0 0 0]
507 (integer :tag "Sunday")
508 (integer :tag "Monday")
509 (integer :tag "Tuesday")
510 (integer :tag "Wednesday")
511 (integer :tag "Thursday")
512 (integer :tag "Friday")
513 (integer :tag "Saturday")))
514 :initialize 'custom-initialize-default
515 :set 'diary-set-maybe-redraw
516 :group 'diary)
518 ;;; More user options in calendar.el, holidays.el.
521 (defun diary-check-diary-file ()
522 "Check that the file specified by `diary-file' exists and is readable.
523 If so, return the expanded file name, otherwise signal an error."
524 (if (and diary-file (file-exists-p diary-file))
525 (if (file-readable-p diary-file)
526 diary-file
527 (error "Diary file `%s' is not readable" diary-file))
528 (error "Diary file `%s' does not exist" diary-file)))
530 ;;;###autoload
531 (defun diary (&optional arg)
532 "Generate the diary window for ARG days starting with the current date.
533 If no argument is provided, the number of days of diary entries is governed
534 by the variable `diary-number-of-entries'. A value of ARG less than 1
535 does nothing. This function is suitable for execution in an init file."
536 (interactive "P")
537 (diary-check-diary-file)
538 (diary-list-entries (calendar-current-date)
539 (if arg (prefix-numeric-value arg))))
541 ;;;###cal-autoload
542 (defun diary-view-entries (&optional arg)
543 "Prepare and display a buffer with diary entries.
544 Searches the file named in `diary-file' for entries that match
545 ARG days starting with the date indicated by the cursor position
546 in the displayed three-month calendar."
547 (interactive "p")
548 (diary-check-diary-file)
549 (diary-list-entries (calendar-cursor-to-date t) arg))
552 ;;;###cal-autoload
553 (defun diary-view-other-diary-entries (arg dfile)
554 "Prepare and display buffer of diary entries from an alternative diary file.
555 Searches for entries that match ARG days, starting with the date indicated
556 by the cursor position in the displayed three-month calendar.
557 DFILE specifies the file to use as the diary file."
558 (interactive
559 (list (prefix-numeric-value current-prefix-arg)
560 (read-file-name "Enter diary file name: " default-directory nil t)))
561 (let ((diary-file dfile))
562 (diary-view-entries arg)))
564 ;;;###cal-autoload
565 (define-obsolete-function-alias 'view-other-diary-entries
566 'diary-view-other-diary-entries "23.1")
568 (defvar diary-syntax-table
569 (let ((st (copy-syntax-table (standard-syntax-table))))
570 (modify-syntax-entry ?* "w" st)
571 (modify-syntax-entry ?: "w" st)
573 "The syntax table used when parsing dates in the diary file.
574 It is the standard syntax table used in Fundamental mode, but with the
575 syntax of `*' and `:' changed to be word constituents.")
577 (defun diary-attrtype-convert (attrvalue type)
578 "Convert string ATTRVALUE to TYPE appropriate for a face description.
579 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
580 (cond ((eq type 'string) attrvalue)
581 ((eq type 'symbol) (intern-soft attrvalue))
582 ((eq type 'int) (string-to-number attrvalue))
583 ((eq type 'stringtnil)
584 (cond ((string-equal "t" attrvalue) t)
585 ((string-equal "nil" attrvalue) nil)
586 (t attrvalue)))
587 ((eq type 'tnil) (string-equal "t" attrvalue))))
589 (defun diary-pull-attrs (entry fileglobattrs)
590 "Search for matches for regexps from `diary-face-attrs'.
591 If ENTRY is nil, searches from the start of the current buffer, and
592 prepends all regexps with `diary-glob-file-regexp-prefix'.
593 If ENTRY is a string, search for matches in that string, and remove them.
594 Returns a list of ENTRY followed by (ATTRIBUTE VALUE) pairs.
595 When ENTRY is non-nil, FILEGLOBATTRS forms the start of the (ATTRIBUTE VALUE)
596 pairs."
597 (let (regexp regnum attrname attrname attrvalue type ret-attr)
598 (if (null entry)
599 (save-excursion
600 (dolist (attr diary-face-attrs)
601 ;; FIXME inefficient searching.
602 (goto-char (point-min))
603 (setq regexp (concat diary-glob-file-regexp-prefix (car attr))
604 regnum (cadr attr)
605 attrname (nth 2 attr)
606 type (nth 3 attr)
607 attrvalue (if (re-search-forward regexp nil t)
608 (match-string-no-properties regnum)))
609 (and attrvalue
610 (setq attrvalue (diary-attrtype-convert attrvalue type))
611 (setq ret-attr (append ret-attr
612 (list attrname attrvalue))))))
613 (setq ret-attr fileglobattrs)
614 (dolist (attr diary-face-attrs)
615 (setq regexp (car attr)
616 regnum (cadr attr)
617 attrname (nth 2 attr)
618 type (nth 3 attr)
619 attrvalue nil)
620 ;; If multiple matches, replace all, use the last (which may
621 ;; be the first instance in the line, if the regexp is
622 ;; anchored with $).
623 (while (string-match regexp entry)
624 (setq attrvalue (match-string-no-properties regnum entry)
625 entry (replace-match "" t t entry)))
626 (and attrvalue
627 (setq attrvalue (diary-attrtype-convert attrvalue type))
628 (setq ret-attr (append ret-attr (list attrname attrvalue))))))
629 (list entry ret-attr)))
633 (defvar diary-modify-entry-list-string-function nil
634 "Function applied to entry string before putting it into the entries list.
635 Can be used by programs integrating a diary list into other buffers (e.g.
636 org.el and planner.el) to modify the string or add properties to it.
637 The function takes a string argument and must return a string.")
639 (defvar diary-entries-list) ; bound in diary-list-entries
641 (defun diary-add-to-list (date string specifier &optional marker
642 globcolor literal)
643 "Add an entry to `diary-entries-list'.
644 Do nothing if DATE or STRING are nil. DATE is the (MONTH DAY
645 YEAR) for which the entry applies; STRING is the text of the
646 entry as it will appear in the diary (i.e. with any format
647 strings such as \"%d\" expanded); SPECIFIER is the date part of
648 the entry as it appears in the diary-file; LITERAL is the entry
649 as it appears in the diary-file (i.e. before expansion).
650 If LITERAL is nil, it is taken to be the same as STRING.
652 The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
653 GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
654 FILENAME being the file containing the diary entry.
656 Modifies STRING using `diary-modify-entry-list-string-function', if non-nil.
657 Also removes the region between `diary-comment-start' and
658 `diary-comment-end', if the former is non-nil."
659 (when (and date string)
660 ;; b-f-n is nil if we are visiting an include file in a temp-buffer.
661 (let ((dfile (or (buffer-file-name) diary-file))
662 cstart)
663 (if diary-file-name-prefix
664 (let ((prefix (funcall diary-file-name-prefix-function dfile)))
665 (or (string-equal prefix "")
666 (setq string (format "[%s] %s" prefix string)))))
667 (and diary-modify-entry-list-string-function
668 (setq string (funcall diary-modify-entry-list-string-function
669 string)))
670 (when (and diary-comment-start
671 (string-match (setq cstart (regexp-quote diary-comment-start))
672 string))
673 ;; Preserve the value with the comments.
674 (or literal (setq literal string))
675 ;; Handles multiple comments per entry, so long as each is on
676 ;; a single line, and each line has no more than one comment.
677 (setq string (replace-regexp-in-string
678 (format "%s.*%s" cstart (regexp-quote diary-comment-end))
679 "" string)))
680 (setq diary-entries-list
681 (append diary-entries-list
682 (list (list date string specifier
683 (list marker dfile literal)
684 globcolor)))))))
686 (define-obsolete-function-alias 'add-to-diary-list 'diary-add-to-list "23.1")
688 (defun diary-list-entries-2 (date mark globattr list-only
689 &optional months symbol gdate)
690 "Internal subroutine of `diary-list-entries'.
691 Find diary entries applying to DATE, by searching from point-min for
692 each element of `diary-date-forms'. MARK indicates an entry is non-marking.
693 GLOBATTR is the list of global file attributes. If LIST-ONLY is
694 non-nil, don't change the buffer, only return a list of entries.
695 Optional array MONTHS replaces `calendar-month-name-array', and
696 means months cannot be abbreviated. Optional string SYMBOL marks diary
697 entries of the desired type. If DATE is not Gregorian, then the
698 Gregorian equivalent should be provided via GDATE. Returns non-nil if
699 any entries were found."
700 (let* ((month (calendar-extract-month date))
701 (day (calendar-extract-day date))
702 (year (calendar-extract-year date))
703 (dayname (format "%s\\|%s\\.?" (calendar-day-name date)
704 (calendar-day-name date 'abbrev)))
705 (calendar-month-name-array (or months calendar-month-name-array))
706 (monthname (format "\\*\\|%s%s" (calendar-month-name month)
707 (if months ""
708 (format "\\|%s\\.?"
709 (calendar-month-name month 'abbrev)))))
710 (month (format "\\*\\|0*%d" month))
711 (day (format "\\*\\|0*%d" day))
712 (year (format "\\*\\|0*%d%s" year
713 (if diary-abbreviated-year-flag
714 (format "\\|%02d" (% year 100))
715 "")))
716 (case-fold-search t)
717 entry-found)
718 (dolist (date-form diary-date-forms)
719 (let ((backup (when (eq (car date-form) 'backup)
720 (setq date-form (cdr date-form))
722 ;; date-form uses day etc as set above.
723 (regexp (format "^%s?%s\\(%s\\)" (regexp-quote mark)
724 (if symbol (regexp-quote symbol) "")
725 (mapconcat 'eval date-form "\\)\\(?:")))
726 entry-start date-start temp)
727 (goto-char (point-min))
728 (while (re-search-forward regexp nil t)
729 (if backup (re-search-backward "\\<" nil t))
730 ;; regexp moves us past the end of date, onto the next line.
731 ;; Trailing whitespace after date not allowed (see diary-file).
732 (if (and (bolp) (not (looking-at "[ \t]")))
733 ;; Diary entry that consists only of date.
734 (backward-char 1)
735 ;; Found a nonempty diary entry--make it
736 ;; visible and add it to the list.
737 (setq date-start (line-end-position 0))
738 ;; Actual entry starts on the next-line?
739 (if (looking-at "[ \t]*\n[ \t]") (forward-line 1))
740 (setq entry-found t
741 entry-start (point))
742 (forward-line 1)
743 (while (looking-at "[ \t]") ; continued entry
744 (forward-line 1))
745 (unless (and (eobp) (not (bolp)))
746 (backward-char 1))
747 (unless list-only
748 (remove-overlays date-start (point) 'invisible 'diary))
749 (setq temp (diary-pull-attrs
750 (buffer-substring-no-properties
751 entry-start (point)) globattr))
752 (diary-add-to-list
753 (or gdate date) (car temp)
754 (buffer-substring-no-properties (1+ date-start) (1- entry-start))
755 (copy-marker entry-start) (cadr temp))))))
756 entry-found))
758 (defvar original-date) ; from diary-list-entries
759 (defvar file-glob-attrs)
760 (defvar list-only)
761 (defvar number)
763 (defun diary-list-entries-1 (months symbol absfunc)
764 "List diary entries of a certain type.
765 MONTHS is an array of month names. SYMBOL marks diary entries of the type
766 in question. ABSFUNC is a function that converts absolute dates to dates
767 of the appropriate type."
768 (let ((gdate original-date))
769 (dotimes (_idummy number)
770 (diary-list-entries-2
771 (funcall absfunc (calendar-absolute-from-gregorian gdate))
772 diary-nonmarking-symbol file-glob-attrs list-only months symbol gdate)
773 (setq gdate
774 (calendar-gregorian-from-absolute
775 (1+ (calendar-absolute-from-gregorian gdate))))))
776 (goto-char (point-min)))
778 (defvar diary-included-files nil
779 "List of any diary files included in the last call to `diary-list-entries'.
780 Or to `diary-mark-entries'.")
782 (defun diary-list-entries (date number &optional list-only)
783 "Create and display a buffer containing the relevant lines in `diary-file'.
784 Selects entries for NUMBER days starting with date DATE. Hides any
785 other entries using overlays. If NUMBER is less than 1, this function
786 does nothing.
788 Returns a list of all relevant diary entries found.
789 The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
790 \(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
791 SPECIFIER is the applicability. If the variable `diary-list-include-blanks'
792 is non-nil, this list includes a dummy diary entry consisting of the empty
793 string for a date with no diary entries.
795 If producing entries for multiple dates (i.e., NUMBER > 1), then
796 this function normally returns the entries from any given diary
797 file in date order. The entries for any given day are in the
798 order in which they were found in the file, not necessarily in
799 time-of-day order. Note that any functions present on the
800 hooks (see below) may add entries, or change the order. For
801 example, `diary-include-other-diary-files' adds entries from any
802 include files that it finds to the end of the original list. The
803 entries from each file will be in date order, but the overall
804 list will not be. If you want the entire list to be in time
805 order, add `diary-sort-entries' to the end of `diary-list-entries-hook'.
807 After preparing the initial list, hooks run in this order:
809 `diary-nongregorian-listing-hook' runs for the main diary file,
810 and each included file. For example, this is the appropriate hook
811 to process Islamic entries in all diary files.
813 `diary-list-entries-hook' runs once only, for the main diary file.
814 For example, this is appropriate for sorting all the entries.
815 If not using include files, there is no difference from the previous
816 hook.
818 `diary-hook' runs last, after the diary is displayed.
819 This is used e.g. by `appt-check'.
821 Functions called by these hooks may use the variables ORIGINAL-DATE
822 and NUMBER, which are the arguments with which this function was called.
823 Note that hook functions should _not_ use DATE, but ORIGINAL-DATE.
824 \(Sexp diary entries may use DATE - see `diary-list-sexp-entries'.)
826 This function displays the list using `diary-display-function', unless
827 LIST-ONLY is non-nil, in which case it just returns the list."
828 (unless number
829 (setq number (if (vectorp diary-number-of-entries)
830 (aref diary-number-of-entries (calendar-day-of-week date))
831 diary-number-of-entries)))
832 (when (> number 0)
833 (let* ((original-date date) ; save for possible use in the hooks
834 (date-string (calendar-date-string date))
835 (diary-buffer (find-buffer-visiting diary-file))
836 ;; Dynamically bound in diary-include-files.
837 (d-incp (and (boundp 'diary-including) diary-including))
838 diary-entries-list file-glob-attrs temp-buff)
839 (unless d-incp
840 (setq diary-included-files nil)
841 (message "Preparing diary..."))
842 (unwind-protect
843 (with-current-buffer (or diary-buffer
844 (if list-only
845 (setq temp-buff (generate-new-buffer
846 " *diary-temp*"))
847 (find-file-noselect diary-file t)))
848 (if diary-buffer
849 (or (verify-visited-file-modtime diary-buffer)
850 (revert-buffer t t)))
851 (if temp-buff
852 ;; If including, caller has already verified it is readable.
853 (insert-file-contents diary-file)
854 ;; Setup things like the header-line-format and invisibility-spec.
855 (if (eq major-mode (default-value 'major-mode))
856 (diary-mode)
857 ;; This kludge is to make customizations to
858 ;; diary-header-line-flag after diary has been displayed
859 ;; take effect. Unconditionally calling (diary-mode)
860 ;; clobbers file local variables.
861 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
862 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
863 (if (eq major-mode 'diary-mode)
864 (setq header-line-format (and diary-header-line-flag
865 diary-header-line-format)))))
866 ;; d-s-p is passed to the diary display function.
867 (let ((diary-saved-point (point)))
868 (save-excursion
869 (save-restriction
870 (widen) ; bug#5093
871 (setq file-glob-attrs (cadr (diary-pull-attrs nil "")))
872 (with-syntax-table diary-syntax-table
873 (goto-char (point-min))
874 (unless list-only
875 (let ((ol (make-overlay (point-min) (point-max) nil t nil)))
876 (set (make-local-variable 'diary-selective-display) t)
877 (overlay-put ol 'invisible 'diary)
878 (overlay-put ol 'evaporate t)))
879 (dotimes (_idummy number)
880 (let ((sexp-found (diary-list-sexp-entries date))
881 (entry-found (diary-list-entries-2
882 date diary-nonmarking-symbol
883 file-glob-attrs list-only)))
884 (if diary-list-include-blanks
885 (or sexp-found entry-found
886 (diary-add-to-list date "" "" "" "")))
887 (setq date
888 (calendar-gregorian-from-absolute
889 (1+ (calendar-absolute-from-gregorian date)))))))
890 (goto-char (point-min))
891 ;; Although it looks like list-entries-hook runs
892 ;; every time, diary-include-other-diary-files
893 ;; binds it to nil (essentially) when it runs
894 ;; in included files.
895 (run-hooks 'diary-nongregorian-listing-hook
896 'diary-list-entries-hook)
897 ;; We could make this explicit:
898 ;;; (run-hooks 'diary-nongregorian-listing-hook)
899 ;;; (if d-incp
900 ;;; (diary-include-other-diary-files) ; recurse
901 ;;; (run-hooks 'diary-list-entries-hook))
902 (unless list-only
903 ;; Avoid M-x diary; M-x calendar; M-x diary
904 ;; clobbering the calendar window.
905 ;; FIXME this is not the right solution.
906 (let ((display-buffer-fallback-action
907 (list (delq
908 'display-buffer-in-previous-window
909 (copy-sequence
910 (car display-buffer-fallback-action))))))
911 (if (and diary-display-function
912 (listp diary-display-function))
913 ;; Backwards compatibility.
914 (run-hooks 'diary-display-function)
915 (funcall (or diary-display-function
916 'diary-simple-display)))))
917 (run-hooks 'diary-hook)))))
918 (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
919 (or d-incp (message "Preparing diary...done"))
920 diary-entries-list)))
922 (defun diary-unhide-everything ()
923 "Show all invisible text in the diary."
924 (kill-local-variable 'diary-selective-display)
925 (save-restriction ; bug#5477
926 (widen)
927 (remove-overlays (point-min) (point-max) 'invisible 'diary))
928 (kill-local-variable 'mode-line-format))
930 (defvar original-date) ; bound in diary-list-entries
931 ;(defvar number) ; already declared above
933 (defun diary-include-files (&optional mark)
934 "Process diary entries from included diary files.
935 By default, lists included entries, but if optional argument MARK is non-nil
936 marks entries instead.
937 For example, this enables you to share common diary files.
938 Specify include files using lines matching `diary-include-string', e.g.
939 #include \"filename\"
940 This is recursive; that is, included files may include other files."
941 (goto-char (point-min))
942 (while (re-search-forward
943 (format "^%s \"\\([^\"]*\\)\"" (regexp-quote diary-include-string))
944 nil t)
945 (let ((diary-file (match-string-no-properties 1))
946 (diary-mark-entries-hook 'diary-mark-included-diary-files)
947 (diary-list-entries-hook 'diary-include-other-diary-files)
948 (diary-including t)
949 diary-hook diary-list-include-blanks efile)
950 (if (file-exists-p diary-file)
951 (if (file-readable-p diary-file)
952 (if (member (setq efile (expand-file-name diary-file))
953 diary-included-files)
954 (error "Recursive diary include for %s" diary-file)
955 (setq diary-included-files
956 (append diary-included-files (list efile)))
957 (if mark
958 (diary-mark-entries)
959 (setq diary-entries-list
960 (append diary-entries-list
961 (diary-list-entries original-date number t)))))
962 (display-warning
963 :error
964 (format "Can't read included diary file %s\n" diary-file)))
965 (display-warning
966 :error
967 (format "Can't find included diary file %s\n" diary-file)))))
968 (goto-char (point-min)))
970 (defun diary-include-other-diary-files ()
971 "Add diary entries from included diary files to `diary-entries-list'.
972 To use, add this function to `diary-list-entries-hook'.
973 For details, see `diary-include-files'.
974 See also `diary-mark-included-diary-files'."
975 (diary-include-files))
977 (define-obsolete-function-alias 'include-other-diary-files
978 'diary-include-other-diary-files "23.1")
980 (defvar date-string) ; bound in diary-list-entries
982 (defun diary-display-no-entries ()
983 "Common subroutine of `diary-simple-display' and `diary-fancy-display'.
984 Handles the case where there are no diary entries.
985 Returns a cons (NOENTRIES . HOLIDAY-STRING)."
986 (let* ((holiday-list (if diary-show-holidays-flag
987 (calendar-check-holidays original-date)))
988 (hol-string (format "%s%s%s"
989 date-string
990 (if holiday-list ": " "")
991 (mapconcat 'identity holiday-list "; ")))
992 (msg (format "No diary entries for %s" hol-string))
993 ;; Empty list, or single item with no text.
994 ;; FIXME multiple items with no text?
995 (noentries (or (not diary-entries-list)
996 (and (not (cdr diary-entries-list))
997 (string-equal "" (cadr
998 (car diary-entries-list)))))))
999 ;; Inconsistency: whether or not the holidays are displayed in a
1000 ;; separate buffer depends on if there are diary entries.
1001 (when noentries
1002 (if (or (< (length msg) (frame-width))
1003 (not holiday-list))
1004 (message "%s" msg)
1005 ;; holiday-list which is too wide for a message gets a buffer.
1006 (calendar-in-read-only-buffer holiday-buffer
1007 (calendar-set-mode-line (format "Holidays for %s" date-string))
1008 (insert (mapconcat 'identity holiday-list "\n")))
1009 (message "No diary entries for %s" date-string)))
1010 (cons noentries hol-string)))
1013 (defvar diary-saved-point) ; bound in diary-list-entries
1015 (defun diary-simple-display ()
1016 "Display the diary buffer if there are any relevant entries or holidays.
1017 Entries that do not apply are made invisible. Holidays are shown
1018 in the mode line. This is an option for `diary-display-function'."
1019 ;; If selected window is dedicated (to the calendar), need a new one
1020 ;; to display the diary.
1021 (let* ((pop-up-frames (or pop-up-frames (window-dedicated-p)))
1022 (dbuff (find-buffer-visiting diary-file))
1023 (empty (diary-display-no-entries)))
1024 ;; This may be too wide, but when simple diary is used there is
1025 ;; nowhere else for the holidays to go. Also, it is documented in
1026 ;; diary-show-holidays-flag that the holidays go in the mode-line.
1027 ;; FIXME however if there are no diary entries a separate buffer
1028 ;; is displayed - this is inconsistent.
1029 (with-current-buffer dbuff
1030 (calendar-set-mode-line (format "Diary for %s" (cdr empty))))
1031 (unless (car empty) ; no entries
1032 (with-current-buffer dbuff
1033 (let ((window (display-buffer (current-buffer))))
1034 ;; d-s-p is passed from diary-list-entries.
1035 (set-window-point window diary-saved-point)
1036 (set-window-start window (point-min)))))))
1038 (define-obsolete-function-alias 'simple-diary-display
1039 'diary-simple-display "23.1")
1041 (defvar diary-goto-entry-function 'diary-goto-entry
1042 "Function called to jump to a diary entry.
1043 Modes that require special handling of the included file
1044 containing the diary entry can assign a suitable function to this
1045 variable.")
1047 (define-button-type 'diary-entry
1048 'action (lambda (button) (funcall diary-goto-entry-function button))
1049 'face 'diary-button 'help-echo "Find this diary entry"
1050 'follow-link t)
1052 (defun diary-goto-entry (button)
1053 "Jump to the diary entry for the BUTTON at point."
1054 (let* ((locator (button-get button 'locator))
1055 (marker (car locator))
1056 markbuf file)
1057 ;; If marker pointing to diary location is valid, use that.
1058 (if (and marker (setq markbuf (marker-buffer marker)))
1059 (progn
1060 (pop-to-buffer markbuf)
1061 (goto-char (marker-position marker)))
1062 ;; Marker is invalid (eg buffer has been killed).
1063 (or (and (setq file (cadr locator))
1064 (file-exists-p file)
1065 (find-file-other-window file)
1066 (progn
1067 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
1068 (goto-char (point-min))
1069 (if (re-search-forward (format "%s.*\\(%s\\)"
1070 (regexp-quote (nth 2 locator))
1071 (regexp-quote (nth 3 locator)))
1072 nil t)
1073 (goto-char (match-beginning 1)))))
1074 (message "Unable to locate this diary entry")))))
1076 (defun diary-fancy-display ()
1077 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
1078 Holidays are shown unless `diary-show-holidays-flag' is nil.
1079 Days with no diary entries are not shown (even if that day is a
1080 holiday), unless `diary-list-include-blanks' is non-nil.
1082 This is an option for `diary-display-function'."
1083 ;; Turn off selective-display in the diary file's buffer.
1084 (with-current-buffer (find-buffer-visiting diary-file)
1085 (diary-unhide-everything))
1086 (unless (car (diary-display-no-entries)) ; no entries
1087 ;; Prepare the fancy diary buffer.
1088 (calendar-in-read-only-buffer diary-fancy-buffer
1089 (calendar-set-mode-line "Diary Entries")
1090 (let ((holiday-list-last-month 1)
1091 (holiday-list-last-year 1)
1092 (date (list 0 0 0))
1093 holiday-list)
1094 (dolist (entry diary-entries-list)
1095 (unless (calendar-date-equal date (car entry))
1096 (setq date (car entry))
1097 (and diary-show-holidays-flag
1098 (calendar-date-compare
1099 (list (list holiday-list-last-month
1100 (calendar-last-day-of-month
1101 holiday-list-last-month
1102 holiday-list-last-year)
1103 holiday-list-last-year))
1104 (list date))
1105 ;; We need to get the holidays for the next 3 months.
1106 (setq holiday-list-last-month
1107 (calendar-extract-month date)
1108 holiday-list-last-year
1109 (calendar-extract-year date))
1110 (progn
1111 (calendar-increment-month
1112 holiday-list-last-month holiday-list-last-year 1)
1114 (setq holiday-list
1115 (let ((displayed-month holiday-list-last-month)
1116 (displayed-year holiday-list-last-year))
1117 (calendar-holiday-list)))
1118 (calendar-increment-month
1119 holiday-list-last-month holiday-list-last-year 1))
1120 (let ((longest 0)
1121 date-holiday-list cc)
1122 ;; Make a list of all holidays for date.
1123 (dolist (h holiday-list)
1124 (if (calendar-date-equal date (car h))
1125 (setq date-holiday-list (append date-holiday-list
1126 (cdr h)))))
1127 (insert (if (bobp) "" ?\n) (calendar-date-string date))
1128 (if date-holiday-list (insert ": "))
1129 (setq cc (current-column))
1130 (insert (mapconcat (lambda (x)
1131 (setq longest (max longest (length x)))
1133 date-holiday-list
1134 (concat "\n" (make-string cc ?\s))))
1135 (insert ?\n (make-string (+ cc longest) ?=) ?\n)))
1136 (let ((this-entry (cadr entry))
1137 this-loc marks temp-face)
1138 (unless (zerop (length this-entry))
1139 (if (setq this-loc (nth 3 entry))
1140 (insert-button this-entry
1141 ;; (MARKER FILENAME SPECIFIER LITERAL)
1142 'locator (list (car this-loc)
1143 (cadr this-loc)
1144 (nth 2 entry)
1145 (or (nth 2 this-loc)
1146 (nth 1 entry)))
1147 :type 'diary-entry)
1148 (insert this-entry))
1149 (insert ?\n)
1150 ;; Doesn't make sense to check font-lock-mode - see
1151 ;; comments above diary-entry-marker in calendar.el.
1152 (and ; font-lock-mode
1153 (setq marks (nth 4 entry))
1154 (save-excursion
1155 (setq temp-face (calendar-make-temp-face marks))
1156 (search-backward this-entry)
1157 (overlay-put
1158 (make-overlay (match-beginning 0) (match-end 0))
1159 'face temp-face)))))))
1160 ;; FIXME can't remember what this check was for.
1161 ;; To prevent something looping, or a minor optimization?
1162 (if (eq major-mode 'diary-fancy-display-mode)
1163 (run-hooks 'diary-fancy-display-mode-hook)
1164 (diary-fancy-display-mode))
1165 (calendar-set-mode-line date-string))))
1167 (define-obsolete-function-alias 'fancy-diary-display
1168 'diary-fancy-display "23.1")
1170 ;; FIXME modernize?
1171 (defun diary-print-entries ()
1172 "Print a hard copy of the diary display.
1174 If the simple diary display is being used, prepare a temp buffer with the
1175 visible lines of the diary buffer, add a heading line composed from the mode
1176 line, print the temp buffer, and destroy it.
1178 If the fancy diary display is being used, just print the buffer.
1180 The hooks given by the variable `diary-print-entries-hook' are called to do
1181 the actual printing."
1182 (interactive)
1183 (let ((diary-buffer (get-buffer diary-fancy-buffer))
1184 temp-buffer heading start end)
1185 (if diary-buffer
1186 (with-current-buffer diary-buffer
1187 (run-hooks 'diary-print-entries-hook))
1188 (or (setq diary-buffer (find-buffer-visiting diary-file))
1189 (error "You don't have a diary buffer!"))
1190 ;; Name affects printing?
1191 (setq temp-buffer (get-buffer-create " *Printable Diary Entries*"))
1192 (with-current-buffer diary-buffer
1193 (setq heading
1194 (if (not (stringp mode-line-format))
1195 "All Diary Entries"
1196 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
1197 (match-string 1 mode-line-format))
1198 start (point-min))
1199 (while
1200 (progn
1201 (setq end (next-single-char-property-change start 'invisible))
1202 (unless (get-char-property start 'invisible)
1203 (with-current-buffer temp-buffer
1204 (insert-buffer-substring diary-buffer start end)))
1205 (setq start end)
1206 (and end (< end (point-max))))))
1207 (set-buffer temp-buffer)
1208 (goto-char (point-min))
1209 (insert heading "\n"
1210 (make-string (length heading) ?=) "\n")
1211 (run-hooks 'diary-print-entries-hook)
1212 (kill-buffer temp-buffer))))
1214 (define-obsolete-function-alias 'print-diary-entries
1215 'diary-print-entries "23.1")
1217 ;;;###cal-autoload
1218 (defun diary-show-all-entries ()
1219 "Show all of the diary entries in the diary file.
1220 This function gets rid of the selective display of the diary file so that
1221 all entries, not just some, are visible. If there is no diary buffer, one
1222 is created."
1223 (interactive)
1224 (let* ((d-file (diary-check-diary-file))
1225 (pop-up-frames (or pop-up-frames (window-dedicated-p)))
1226 (win (selected-window))
1227 (height (window-height)))
1228 (with-current-buffer (or (find-buffer-visiting d-file)
1229 (find-file-noselect d-file t))
1230 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
1231 (diary-unhide-everything)
1232 (display-buffer (current-buffer))
1233 (when (and (/= height (window-height win))
1234 (with-current-buffer (window-buffer win)
1235 (derived-mode-p 'calendar-mode)))
1236 (fit-window-to-buffer win)))))
1238 ;;;###autoload
1239 (defun diary-mail-entries (&optional ndays)
1240 "Send a mail message showing diary entries for next NDAYS days.
1241 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
1242 Mail is sent to the address specified by `diary-mail-addr'.
1244 Here is an example of a script to call `diary-mail-entries',
1245 suitable for regular scheduling using cron (or at). Note that
1246 since `emacs -script' does not load your init file, you should
1247 ensure that all relevant variables are set.
1249 #!/usr/bin/emacs -script
1250 ;; diary-rem.el - run the Emacs diary-reminder
1252 \(setq diary-mail-days 3
1253 diary-file \"/path/to/diary.file\"
1254 calendar-date-style 'european
1255 diary-mail-addr \"user@host.name\")
1257 \(diary-mail-entries)
1259 # diary-rem.el ends here
1261 (interactive "P")
1262 (if (string-equal diary-mail-addr "")
1263 (error "You must set `diary-mail-addr' to use this command")
1264 (let ((diary-display-function 'diary-fancy-display))
1265 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days)))
1266 (compose-mail diary-mail-addr
1267 (concat "Diary entries generated "
1268 (calendar-date-string (calendar-current-date))))
1269 (insert
1270 (if (get-buffer diary-fancy-buffer)
1271 (with-current-buffer diary-fancy-buffer (buffer-string))
1272 "No entries found"))
1273 (call-interactively (get mail-user-agent 'sendfunc))))
1275 (defun diary-name-pattern (string-array &optional abbrev-array paren)
1276 "Return a regexp matching the strings in the array STRING-ARRAY.
1277 If the optional argument ABBREV-ARRAY is present, the regexp
1278 also matches the supplied abbreviations, with or without final `.'
1279 characters. If the optional argument PAREN is non-nil, surrounds
1280 the regexp with parentheses."
1281 (regexp-opt (append string-array
1282 abbrev-array
1283 (if abbrev-array
1284 (mapcar (lambda (e) (format "%s." e))
1285 abbrev-array))
1286 nil)
1287 paren))
1289 (defvar diary-marking-entries-flag nil
1290 "True during the marking of diary entries, nil otherwise.")
1292 (defvar diary-marking-entry-flag nil
1293 "True during the marking of diary entries, if current entry is marking.")
1295 ;; file-glob-attrs bound in diary-mark-entries.
1296 (defun diary-mark-entries-1 (markfunc &optional months symbol absfunc)
1297 "Mark diary entries of a certain type.
1298 MARKFUNC is a function that marks entries of the appropriate type
1299 matching a given date pattern. MONTHS is an array of month names.
1300 SYMBOL marks diary entries of the type in question. ABSFUNC is a
1301 function that converts absolute dates to dates of the appropriate type. "
1302 (let ((dayname (diary-name-pattern calendar-day-name-array
1303 calendar-day-abbrev-array))
1304 (monthname (format "%s\\|\\*"
1305 (if months
1306 (diary-name-pattern months)
1307 (diary-name-pattern calendar-month-name-array
1308 calendar-month-abbrev-array))))
1309 (month "[0-9]+\\|\\*")
1310 (day "[0-9]+\\|\\*")
1311 (year "[0-9]+\\|\\*")
1312 (case-fold-search t)
1313 marks)
1314 (dolist (date-form diary-date-forms)
1315 (if (eq (car date-form) 'backup) ; ignore 'backup directive
1316 (setq date-form (cdr date-form)))
1317 (let* ((l (length date-form))
1318 (d-name-pos (- l (length (memq 'dayname date-form))))
1319 (d-name-pos (if (/= l d-name-pos) (1+ d-name-pos)))
1320 (m-name-pos (- l (length (memq 'monthname date-form))))
1321 (m-name-pos (if (/= l m-name-pos) (1+ m-name-pos)))
1322 (d-pos (- l (length (memq 'day date-form))))
1323 (d-pos (if (/= l d-pos) (1+ d-pos)))
1324 (m-pos (- l (length (memq 'month date-form))))
1325 (m-pos (if (/= l m-pos) (1+ m-pos)))
1326 (y-pos (- l (length (memq 'year date-form))))
1327 (y-pos (if (/= l y-pos) (1+ y-pos)))
1328 (regexp (format "^%s\\(%s\\)"
1329 (if symbol (regexp-quote symbol) "")
1330 (mapconcat 'eval date-form "\\)\\("))))
1331 (goto-char (point-min))
1332 (while (re-search-forward regexp nil t)
1333 (let* ((dd-name
1334 (if d-name-pos
1335 (match-string-no-properties d-name-pos)))
1336 (mm-name
1337 (if m-name-pos
1338 (match-string-no-properties m-name-pos)))
1339 (mm (string-to-number
1340 (if m-pos
1341 (match-string-no-properties m-pos)
1342 "")))
1343 (dd (string-to-number
1344 (if d-pos
1345 (match-string-no-properties d-pos)
1346 "")))
1347 (y-str (if y-pos
1348 (match-string-no-properties y-pos)))
1349 (yy (if (not y-str)
1351 (if (and (= (length y-str) 2)
1352 diary-abbreviated-year-flag)
1353 (let* ((current-y
1354 (calendar-extract-year
1355 (if absfunc
1356 (funcall
1357 absfunc
1358 (calendar-absolute-from-gregorian
1359 (calendar-current-date)))
1360 (calendar-current-date))))
1361 (y (+ (string-to-number y-str)
1362 ;; Current century, eg 2000.
1363 (* 100 (/ current-y 100))))
1364 (offset (- y current-y)))
1365 ;; Add 2-digit year to current century.
1366 ;; If more than 50 years in the future,
1367 ;; assume last century. If more than 50
1368 ;; years in the past, assume next century.
1369 (if (> offset 50)
1370 (- y 100)
1371 (if (< offset -50)
1372 (+ y 100)
1373 y)))
1374 (string-to-number y-str)))))
1375 (setq marks (cadr (diary-pull-attrs
1376 (buffer-substring-no-properties
1377 (point) (line-end-position))
1378 file-glob-attrs)))
1379 ;; Only mark all days of a given name if the pattern
1380 ;; contains no more specific elements.
1381 (if (and dd-name (not (or d-pos m-pos y-pos)))
1382 (calendar-mark-days-named
1383 (cdr (assoc-string dd-name
1384 (calendar-make-alist
1385 calendar-day-name-array
1386 0 nil calendar-day-abbrev-array
1387 (mapcar (lambda (e)
1388 (format "%s." e))
1389 calendar-day-abbrev-array))
1390 t)) marks)
1391 (if mm-name
1392 (setq mm
1393 (if (string-equal mm-name "*") 0
1394 (cdr (assoc-string
1395 mm-name
1396 (if months (calendar-make-alist months)
1397 (calendar-make-alist
1398 calendar-month-name-array
1399 1 nil calendar-month-abbrev-array
1400 (mapcar (lambda (e)
1401 (format "%s." e))
1402 calendar-month-abbrev-array)))
1403 t)))))
1404 (funcall markfunc mm dd yy marks))))))))
1406 ;;;###cal-autoload
1407 (defun diary-mark-entries (&optional redraw)
1408 "Mark days in the calendar window that have diary entries.
1409 Marks each entry in the diary that is visible in the calendar window.
1411 After marking the entries, runs `diary-nongregorian-marking-hook'
1412 for the main diary file, and each included file. For example,
1413 this is the appropriate hook to process Islamic entries in all
1414 diary files. Next `diary-mark-entries-hook' runs, for the main diary
1415 file only. If not using include files, there is no difference between
1416 these two hooks.
1418 If the optional argument REDRAW is non-nil (which is the case
1419 interactively, for example) then this first removes any existing diary
1420 marks. This is intended to deal with deleted diary entries."
1421 (interactive "p")
1422 ;; To remove any deleted diary entries. Do not redraw when:
1423 ;; i) processing #include diary files (else only get the marks from
1424 ;; the last #include file processed).
1425 ;; ii) called via calendar-redraw (since calendar has already been
1426 ;; erased).
1427 ;; Use of REDRAW handles both of these cases.
1428 (when (and redraw calendar-mark-diary-entries-flag)
1429 (setq calendar-mark-diary-entries-flag nil)
1430 (calendar-redraw))
1431 (let ((diary-marking-entries-flag t)
1432 (diary-buffer (find-buffer-visiting diary-file))
1433 ;; Dynamically bound in diary-include-files.
1434 (d-incp (and (boundp 'diary-including) diary-including))
1435 file-glob-attrs temp-buff)
1436 (unless d-incp
1437 (setq diary-included-files nil)
1438 (message "Marking diary entries..."))
1439 (unwind-protect
1440 (with-current-buffer (or diary-buffer
1441 (if d-incp
1442 (setq temp-buff (generate-new-buffer
1443 " *diary-temp*"))
1444 (find-file-noselect
1445 (diary-check-diary-file) t)))
1446 (if temp-buff
1447 ;; If including, caller has already verified it is readable.
1448 (insert-file-contents diary-file)
1449 (if (eq major-mode (default-value 'major-mode)) (diary-mode)))
1450 (setq calendar-mark-diary-entries-flag t)
1451 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1452 (with-syntax-table diary-syntax-table
1453 (save-excursion
1454 (diary-mark-entries-1 'calendar-mark-date-pattern)
1455 (diary-mark-sexp-entries)
1456 ;; Although it looks like mark-entries-hook runs every time,
1457 ;; diary-mark-included-diary-files binds it to nil
1458 ;; (essentially) when it runs in included files.
1459 (run-hooks 'diary-nongregorian-marking-hook
1460 'diary-mark-entries-hook))))
1461 (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
1462 (or d-incp (message "Marking diary entries...done"))))
1464 ;;;###cal-autoload
1465 (define-obsolete-function-alias 'mark-diary-entries 'diary-mark-entries "23.1")
1467 (defun diary-sexp-entry (sexp entry date)
1468 "Process a SEXP diary ENTRY for DATE."
1469 (let ((result (if calendar-debug-sexp
1470 (let ((debug-on-error t))
1471 (eval (car (read-from-string sexp))))
1472 (let (err)
1473 (condition-case err
1474 (eval (car (read-from-string sexp)))
1475 (error
1476 (display-warning
1477 :error
1478 (format "Bad diary sexp at line %d in %s:\n%s\n\
1479 Error: %s\n"
1480 (count-lines (point-min) (point))
1481 diary-file sexp err))
1482 nil))))))
1483 (cond ((stringp result) result)
1484 ((and (consp result)
1485 (stringp (cdr result))) result)
1486 (result entry)
1487 (t nil))))
1489 (defvar displayed-year) ; bound in calendar-generate
1490 (defvar displayed-month)
1492 (defun diary-mark-sexp-entries ()
1493 "Mark days in the calendar window that have sexp diary entries.
1494 Each entry in the diary file (or included files) visible in the calendar window
1495 is marked. See the documentation for the function `diary-list-sexp-entries'."
1496 (let* ((sexp-mark (regexp-quote diary-sexp-entry-symbol))
1497 (s-entry (format "^\\(%s(\\)\\|\\(%s%s(diary-remind\\)" sexp-mark
1498 (regexp-quote diary-nonmarking-symbol)
1499 sexp-mark))
1500 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1501 m y first-date last-date date mark file-glob-attrs
1502 sexp-start sexp entry entry-start)
1503 (with-current-buffer calendar-buffer
1504 (setq m displayed-month
1505 y displayed-year))
1506 (calendar-increment-month m y -1)
1507 (setq first-date (calendar-absolute-from-gregorian (list m 1 y)))
1508 (calendar-increment-month m y 2)
1509 (setq last-date
1510 (calendar-absolute-from-gregorian
1511 (list m (calendar-last-day-of-month m y) y)))
1512 (goto-char (point-min))
1513 (while (re-search-forward s-entry nil t)
1514 (setq diary-marking-entry-flag (char-equal (preceding-char) ?\())
1515 (re-search-backward "(")
1516 (setq sexp-start (point))
1517 (forward-sexp)
1518 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1519 (forward-char 1)
1520 (if (and (bolp) (not (looking-at "[ \t]")))
1521 ;; Diary entry consists only of the sexp.
1522 (progn
1523 (backward-char 1)
1524 (setq entry ""))
1525 (setq entry-start (point))
1526 ;; Find end of entry.
1527 (forward-line 1)
1528 (while (looking-at "[ \t]")
1529 (forward-line 1))
1530 (if (bolp) (backward-char 1))
1531 (setq entry (buffer-substring-no-properties entry-start (point))))
1532 (setq date (1- first-date))
1533 ;; FIXME this loops over all visible dates.
1534 ;; Could be optimized in many cases. Depends on whether t or * present.
1535 (while (<= (setq date (1+ date)) last-date)
1536 (when (setq mark (diary-sexp-entry
1537 sexp entry
1538 (calendar-gregorian-from-absolute date)))
1539 (calendar-mark-visible-date
1540 (calendar-gregorian-from-absolute date)
1541 (or (cadr (diary-pull-attrs entry file-glob-attrs))
1542 (if (consp mark) (car mark)))))))))
1544 (define-obsolete-function-alias 'mark-sexp-diary-entries
1545 'diary-mark-sexp-entries "23.1")
1547 (defun diary-mark-included-diary-files ()
1548 "Mark diary entries from included diary files.
1549 To use, add this function to `diary-mark-entries-hook'.
1550 For details, see `diary-include-files'.
1551 See also `diary-include-other-diary-files'."
1552 (diary-include-files t))
1554 (define-obsolete-function-alias 'mark-included-diary-files
1555 'diary-mark-included-diary-files "23.1")
1557 (defun calendar-mark-days-named (dayname &optional color)
1558 "Mark all dates in the calendar window that are day DAYNAME of the week.
1559 0 means all Sundays, 1 means all Mondays, and so on.
1560 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1561 (with-current-buffer calendar-buffer
1562 (let ((prev-month displayed-month)
1563 (prev-year displayed-year)
1564 (succ-month displayed-month)
1565 (succ-year displayed-year)
1566 (last-day)
1567 (day))
1568 (calendar-increment-month succ-month succ-year 1)
1569 (calendar-increment-month prev-month prev-year -1)
1570 (setq day (calendar-absolute-from-gregorian
1571 (calendar-nth-named-day 1 dayname prev-month prev-year))
1572 last-day (calendar-absolute-from-gregorian
1573 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1574 (while (<= day last-day)
1575 (calendar-mark-visible-date (calendar-gregorian-from-absolute day)
1576 color)
1577 (setq day (+ day 7))))))
1579 (define-obsolete-function-alias 'mark-calendar-days-named
1580 'calendar-mark-days-named "23.1")
1582 (defun calendar-mark-month (month year p-month p-day p-year &optional color)
1583 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P-DAY/P-YEAR.
1584 A value of 0 in any position of the pattern is a wildcard.
1585 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1586 (if (or (and (= month p-month)
1587 (or (zerop p-year) (= year p-year)))
1588 (and (zerop p-month)
1589 (or (zerop p-year) (= year p-year))))
1590 (if (zerop p-day)
1591 (dotimes (i (calendar-last-day-of-month month year))
1592 (calendar-mark-visible-date (list month (1+ i) year) color))
1593 (calendar-mark-visible-date (list month p-day year) color))))
1595 (define-obsolete-function-alias 'mark-calendar-month
1596 'calendar-mark-month "23.1")
1598 (defun calendar-mark-date-pattern (month day year &optional color)
1599 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1600 A value of 0 in any position is a wildcard. Optional argument COLOR is
1601 passed to `calendar-mark-visible-date' as MARK."
1602 (with-current-buffer calendar-buffer
1603 (let ((m displayed-month)
1604 (y displayed-year))
1605 (calendar-increment-month m y -1)
1606 (dotimes (_idummy 3)
1607 (calendar-mark-month m y month day year color)
1608 (calendar-increment-month m y 1)))))
1610 (define-obsolete-function-alias 'mark-calendar-date-pattern
1611 'calendar-mark-date-pattern "23.1")
1613 ;; Bahai, Hebrew, Islamic.
1614 (defun calendar-mark-complex (month day year fromabs &optional color)
1615 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1616 The function FROMABS converts absolute dates to the appropriate date system.
1617 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1618 ;; Not one of the simple cases--check all visible dates for match.
1619 ;; Actually, the following code takes care of ALL of the cases, but
1620 ;; it's much too slow to be used for the simple (common) cases.
1621 (let* ((m displayed-month)
1622 (y displayed-year)
1623 (first-date (progn
1624 (calendar-increment-month m y -1)
1625 (calendar-absolute-from-gregorian (list m 1 y))))
1626 (last-date (progn
1627 (calendar-increment-month m y 2)
1628 (calendar-absolute-from-gregorian
1629 (list m (calendar-last-day-of-month m y) y))))
1630 (date (1- first-date))
1631 local-date)
1632 (while (<= (setq date (1+ date)) last-date)
1633 (setq local-date (funcall fromabs date))
1634 (and (or (zerop month)
1635 (= month (calendar-extract-month local-date)))
1636 (or (zerop day)
1637 (= day (calendar-extract-day local-date)))
1638 (or (zerop year)
1639 (= year (calendar-extract-year local-date)))
1640 (calendar-mark-visible-date
1641 (calendar-gregorian-from-absolute date) color)))))
1643 ;; Bahai, Islamic.
1644 (defun calendar-mark-1 (month day year fromabs toabs &optional color)
1645 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1646 The function FROMABS converts absolute dates to the appropriate date system.
1647 The function TOABS carries out the inverse operation. Optional argument
1648 COLOR is passed to `calendar-mark-visible-date' as MARK."
1649 (with-current-buffer calendar-buffer
1650 (if (and (not (zerop month)) (not (zerop day)))
1651 (if (not (zerop year))
1652 ;; Fully specified date.
1653 (let ((date (calendar-gregorian-from-absolute
1654 (funcall toabs (list month day year)))))
1655 (if (calendar-date-is-visible-p date)
1656 (calendar-mark-visible-date date color)))
1657 ;; Month and day in any year--this taken from the holiday stuff.
1658 (let* ((i-date (funcall fromabs
1659 (calendar-absolute-from-gregorian
1660 (list displayed-month 15 displayed-year))))
1661 (m (calendar-extract-month i-date))
1662 (y (calendar-extract-year i-date))
1663 date)
1664 (unless (< m 1) ; calendar doesn't apply
1665 (calendar-increment-month m y (- 10 month))
1666 (and (> m 7) ; date might be visible
1667 (calendar-date-is-visible-p
1668 (setq date (calendar-gregorian-from-absolute
1669 (funcall toabs (list month day y)))))
1670 (calendar-mark-visible-date date color)))))
1671 (calendar-mark-complex month day year fromabs color))))
1674 (defun diary-entry-time (s)
1675 "Return time at the beginning of the string S as a military-style integer.
1676 For example, returns 1325 for 1:25pm.
1678 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1679 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1680 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM, XX:XXpm, or XX:XXPM. A period (.) can
1681 be used instead of a colon (:) to separate the hour and minute parts."
1682 (let (case-fold-search)
1683 (cond ((string-match ; military time
1684 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1686 (+ (* 100 (string-to-number (match-string 1 s)))
1687 (string-to-number (match-string 2 s))))
1688 ((string-match ; hour only (XXam or XXpm)
1689 "\\`[ \t\n]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1690 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1691 (if (equal ?a (downcase (aref s (match-beginning 2))))
1692 0 1200)))
1693 ((string-match ; hour and minute (XX:XXam or XX:XXpm)
1694 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1695 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1696 (string-to-number (match-string 2 s))
1697 (if (equal ?a (downcase (aref s (match-beginning 3))))
1698 0 1200)))
1699 (t diary-unknown-time)))) ; unrecognizable
1701 (defun diary-entry-compare (e1 e2)
1702 "Return t if E1 is earlier than E2."
1703 (or (calendar-date-compare e1 e2)
1704 (and (calendar-date-equal (car e1) (car e2))
1705 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1706 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1707 (or (< t1 t2)
1708 (and (= t1 t2)
1709 (string-lessp ts1 ts2)))))))
1711 (defun diary-sort-entries ()
1712 "Sort the list of diary entries by time of day.
1713 If you add this function to `diary-list-entries-hook', it should
1714 be the last item in the hook, in case earlier items add diary
1715 entries, or change the order."
1716 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1718 (define-obsolete-function-alias 'sort-diary-entries 'diary-sort-entries "23.1")
1721 (defun diary-list-sexp-entries (date)
1722 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1723 Also, make them visible in the diary. Returns t if any entries are found.
1725 Sexp diary entries must be prefaced by a `diary-sexp-entry-symbol'
1726 \(normally `%%'). The form of a sexp diary entry is
1728 %%(SEXP) ENTRY
1730 Both ENTRY and DATE are available when the SEXP is evaluated. If
1731 the SEXP returns nil, the diary entry does not apply. If it
1732 returns a non-nil value, ENTRY will be taken to apply to DATE; if
1733 the value is a string, that string will be the diary entry in the
1734 fancy diary display.
1736 For example, the following diary entry will apply to the 21st of
1737 the month if it is a weekday and the Friday before if the 21st is
1738 on a weekend:
1740 &%%(let ((dayname (calendar-day-of-week date))
1741 (day (calendar-extract-day date)))
1743 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1744 (and (memq day '(19 20)) (= dayname 5)))
1745 ) UIUC pay checks deposited
1747 A number of built-in functions are available for this type of
1748 diary entry. In the following, the optional parameter MARK
1749 specifies a face or single-character string to use when
1750 highlighting the day in the calendar. For those functions that
1751 take MONTH, DAY, and YEAR as arguments, the order of the input
1752 parameters changes according to `calendar-date-style' (e.g. to
1753 DAY MONTH YEAR in the European style).
1755 %%(diary-date MONTH DAY YEAR &optional MARK) text
1756 Entry applies if date is MONTH, DAY, YEAR. DAY, MONTH, and YEAR can
1757 be a list of integers, `t' (meaning all values), or an integer.
1759 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1760 Entry will appear on the Nth DAYNAME after/before MONTH DAY.
1761 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
1762 If N>0, use the Nth DAYNAME after MONTH DAY.
1763 If N<0, use the Nth DAYNAME before MONTH DAY.
1764 DAY defaults to 1 if N>0, and MONTH's last day otherwise.
1765 MONTH can be a list of months, a single month, or `t' to
1766 specify all months.
1768 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1769 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1770 inclusive.
1772 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1773 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1774 Text can contain `%d' or `%d%s'; `%d' will be replaced by the
1775 number of years since the MONTH DAY, YEAR, and `%s' by the
1776 ordinal ending of that number (i.e. `st', `nd', `rd' or `th',
1777 as appropriate). The anniversary of February 29 is
1778 considered to be March 1 in a non-leap year.
1780 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1781 Entry will appear every N days, starting MONTH DAY, YEAR.
1782 Text can contain `%d' or `%d%s'; `%d' will be replaced by the
1783 number of repetitions since the MONTH DAY, YEAR and `%s' by
1784 the ordinal ending of that number (i.e. `st', `nd', `rd' or
1785 `th', as appropriate).
1787 %%(diary-remind SEXP DAYS &optional MARKING) text
1788 Entry is a reminder for diary sexp SEXP. DAYS is either a
1789 single number or a list of numbers indicating the number(s)
1790 of days before the event that the warning(s) should occur.
1791 A negative number -DAYS has the same meaning as a list (1 2 ... DAYS).
1792 If the current date is (one of) DAYS before the event indicated
1793 by EXPR, then a suitable message (as specified by
1794 `diary-remind-message') appears. In addition to the
1795 reminders beforehand, the diary entry also appears on the
1796 date itself. If optional MARKING is non-nil then the
1797 *reminders* are marked on the calendar. Marking of reminders
1798 is independent of whether the entry *itself* is a marking or
1799 non-marking one.
1801 %%(diary-hebrew-yahrzeit MONTH DAY YEAR) text
1802 Text is assumed to be the name of the person; the date is the
1803 date of death on the *civil* calendar. The diary entry will
1804 appear on the proper Hebrew-date anniversary and on the day
1805 before.
1807 All the remaining functions do not accept any text, and so only
1808 make sense with `diary-fancy-display'. Most produce output every day.
1810 `diary-day-of-year' - day of year and number of days remaining
1811 `diary-iso-date' - ISO commercial date
1812 `diary-astro-day-number' - astronomical (Julian) day number
1813 `diary-sunrise-sunset' - local times of sunrise and sunset
1815 These functions give the date in alternative calendrical systems:
1817 `diary-bahai-date', `diary-chinese-date', `diary-coptic-date',
1818 `diary-ethiopic-date', `diary-french-date', `diary-hebrew-date',
1819 `diary-islamic-date', `diary-julian-date', `diary-mayan-date',
1820 `diary-persian-date'
1822 Theses functions only produce output on certain dates:
1824 `diary-lunar-phases' - phases of moon (on the appropriate days)
1825 `diary-hebrew-omer' - Omer count, within 50 days after Passover
1826 `diary-hebrew-parasha' - weekly parasha, every Saturday
1827 `diary-hebrew-rosh-hodesh' - Rosh Hodesh, or the day or Saturday before
1828 `diary-hebrew-sabbath-candles' - local time of candle lighting, on Fridays
1831 Marking these entries is *extremely* time consuming, so it is
1832 best if they are non-marking."
1833 (let ((s-entry (format "^%s?%s(" (regexp-quote diary-nonmarking-symbol)
1834 (regexp-quote diary-sexp-entry-symbol)))
1835 entry-found file-glob-attrs marks
1836 sexp-start sexp entry specifier entry-start line-start
1837 diary-entry temp literal)
1838 (goto-char (point-min))
1839 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1840 (while (re-search-forward s-entry nil t)
1841 (backward-char 1)
1842 (setq sexp-start (point))
1843 (forward-sexp)
1844 (setq sexp (buffer-substring-no-properties sexp-start (point))
1845 line-start (line-end-position 0)
1846 specifier
1847 (buffer-substring-no-properties (1+ line-start) (point))
1848 entry-start (1+ line-start))
1849 (forward-char 1)
1850 (if (and (bolp) (not (looking-at "[ \t]")))
1851 ;; Diary entry consists only of the sexp.
1852 (progn
1853 (backward-char 1)
1854 (setq entry ""))
1855 (setq entry-start (point))
1856 (forward-line 1)
1857 (while (looking-at "[ \t]")
1858 (forward-line 1))
1859 (if (bolp) (backward-char 1))
1860 (setq entry (buffer-substring-no-properties entry-start (point))))
1861 (setq diary-entry (diary-sexp-entry sexp entry date)
1862 literal entry ; before evaluation
1863 entry (if (consp diary-entry)
1864 (cdr diary-entry)
1865 diary-entry))
1866 (when diary-entry
1867 (remove-overlays line-start (point) 'invisible 'diary)
1868 (if (< 0 (length entry))
1869 (setq temp (diary-pull-attrs entry file-glob-attrs)
1870 entry (nth 0 temp)
1871 marks (nth 1 temp))))
1872 (diary-add-to-list date entry specifier
1873 (if entry-start (copy-marker entry-start))
1874 marks literal)
1875 (setq entry-found (or entry-found diary-entry)))
1876 entry-found))
1878 (define-obsolete-function-alias 'list-sexp-diary-entries
1879 'diary-list-sexp-entries "23.1")
1881 (defun diary-make-date (a b c)
1882 "Convert A B C into the internal calendar date form.
1883 The expected order of the inputs depends on `calendar-date-style',
1884 e.g. in the European case, A = day, B = month, C = year. Returns
1885 a list (MONTH DAY YEAR), i.e. the American style, which is the
1886 form used internally by the calendar and diary."
1887 (cond ((eq calendar-date-style 'iso) ; YMD
1888 (list b c a))
1889 ((eq calendar-date-style 'european) ; DMY
1890 (list b a c))
1891 (t (list a b c))))
1894 ;;; Sexp diary functions.
1896 (defvar date)
1897 (defvar entry)
1899 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1900 (defun diary-date (month day year &optional mark)
1901 "Specific date(s) diary entry.
1902 Entry applies if date is MONTH, DAY, YEAR. Each parameter can be a
1903 list of integers, `t' (meaning all values), or an integer. The order
1904 of the input parameters changes according to `calendar-date-style'
1905 \(e.g. to DAY MONTH YEAR in the European style).
1907 An optional parameter MARK specifies a face or single-character string
1908 to use when highlighting the day in the calendar."
1909 (let* ((ddate (diary-make-date month day year))
1910 (dd (calendar-extract-day ddate))
1911 (mm (calendar-extract-month ddate))
1912 (yy (calendar-extract-year ddate))
1913 (m (calendar-extract-month date))
1914 (y (calendar-extract-year date))
1915 (d (calendar-extract-day date)))
1916 (and
1917 (or (and (listp dd) (memq d dd))
1918 (equal d dd)
1919 (eq dd t))
1920 (or (and (listp mm) (memq m mm))
1921 (equal m mm)
1922 (eq mm t))
1923 (or (and (listp yy) (memq y yy))
1924 (equal y yy)
1925 (eq yy t))
1926 (cons mark entry))))
1928 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1929 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1930 "Block diary entry.
1931 Entry applies if date is between, or on one of, two dates. The order
1932 of the input parameters changes according to `calendar-date-style'
1933 \(e.g. to D1, M1, Y1, D2, M2, Y2 in the European style).
1935 An optional parameter MARK specifies a face or single-character string
1936 to use when highlighting the day in the calendar."
1937 (let ((date1 (calendar-absolute-from-gregorian
1938 (diary-make-date m1 d1 y1)))
1939 (date2 (calendar-absolute-from-gregorian
1940 (diary-make-date m2 d2 y2)))
1941 (d (calendar-absolute-from-gregorian date)))
1942 (and (<= date1 d) (<= d date2)
1943 (cons mark entry))))
1945 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1946 (defun diary-float (month dayname n &optional day mark)
1947 "Diary entry for the Nth DAYNAME after/before MONTH DAY.
1948 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
1949 If N>0, use the Nth DAYNAME after MONTH DAY.
1950 If N<0, use the Nth DAYNAME before MONTH DAY.
1951 DAY defaults to 1 if N>0, and MONTH's last day otherwise.
1952 MONTH can be a list of months, an integer, or `t' (meaning all months).
1953 Optional MARK specifies a face or single-character string to use when
1954 highlighting the day in the calendar."
1955 ;; This is messy because the diary entry may apply, but the date on which it
1956 ;; is based can be in a different month/year. For example, asking for the
1957 ;; first Monday after December 30. For large values of |n| the problem is
1958 ;; more grotesque.
1959 (and (= dayname (calendar-day-of-week date))
1960 (let* ((m (calendar-extract-month date))
1961 (d (calendar-extract-day date))
1962 (y (calendar-extract-year date))
1963 ;; Last (n>0) or first (n<0) possible base date for entry.
1964 (limit
1965 (calendar-nth-named-absday (- n) dayname m y d))
1966 (last-abs (if (> n 0) limit (+ limit 6)))
1967 (first-abs (if (> n 0) (- limit 6) limit))
1968 (last (calendar-gregorian-from-absolute last-abs))
1969 (first (calendar-gregorian-from-absolute first-abs))
1970 ;; m1, d1 is first possible base date.
1971 (m1 (calendar-extract-month first))
1972 (d1 (calendar-extract-day first))
1973 (y1 (calendar-extract-year first))
1974 ;; m2, d2 is last possible base date.
1975 (m2 (calendar-extract-month last))
1976 (d2 (calendar-extract-day last))
1977 (y2 (calendar-extract-year last)))
1978 (if (or (and (= m1 m2) ; only possible base dates in one month
1979 (or (eq month t)
1980 (if (listp month)
1981 (memq m1 month)
1982 (= m1 month)))
1983 (let ((d (or day (if (> n 0)
1985 (calendar-last-day-of-month m1 y1)))))
1986 (and (<= d1 d) (<= d d2))))
1987 ;; Only possible base dates straddle two months.
1988 (and (or (< y1 y2)
1989 (and (= y1 y2) (< m1 m2)))
1991 ;; m1, d1 works as a base date.
1992 (and
1993 (or (eq month t)
1994 (if (listp month)
1995 (memq m1 month)
1996 (= m1 month)))
1997 (<= d1 (or day (if (> n 0)
1999 (calendar-last-day-of-month m1 y1)))))
2000 ;; m2, d2 works as a base date.
2001 (and (or (eq month t)
2002 (if (listp month)
2003 (memq m2 month)
2004 (= m2 month)))
2005 (<= (or day (if (> n 0)
2007 (calendar-last-day-of-month m2 y2)))
2008 d2)))))
2009 (cons mark entry)))))
2011 (defun diary-ordinal-suffix (n)
2012 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
2013 (if (or (memq (% n 100) '(11 12 13))
2014 (< 3 (% n 10)))
2015 "th"
2016 (aref ["th" "st" "nd" "rd"] (% n 10))))
2018 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
2019 (defun diary-anniversary (month day &optional year mark)
2020 "Anniversary diary entry.
2021 Entry applies if date is the anniversary of MONTH, DAY, YEAR.
2022 The order of the input parameters changes according to
2023 `calendar-date-style' (e.g. to DAY MONTH YEAR in the European style).
2025 The diary entry can contain `%d' or `%d%s'; the %d will be replaced
2026 by the number of years since the MONTH, DAY, YEAR, and the %s will
2027 be replaced by the ordinal ending of that number (that is, `st',
2028 `nd', `rd' or `th', as appropriate). The anniversary of February 29
2029 is considered to be March 1 in non-leap years.
2031 An optional parameter MARK specifies a face or single-character
2032 string to use when highlighting the day in the calendar."
2033 (let* ((ddate (diary-make-date month day year))
2034 (dd (calendar-extract-day ddate))
2035 (mm (calendar-extract-month ddate))
2036 (yy (calendar-extract-year ddate))
2037 (y (calendar-extract-year date))
2038 (diff (if yy (- y yy) 100)))
2039 (and (= mm 2) (= dd 29) (not (calendar-leap-year-p y))
2040 (setq mm 3
2041 dd 1))
2042 (and (> diff 0) (calendar-date-equal (list mm dd y) date)
2043 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
2045 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
2046 (defun diary-cyclic (n month day year &optional mark)
2047 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
2048 The order of the input parameters changes according to
2049 `calendar-date-style' (e.g. to N DAY MONTH YEAR in the European
2050 style). The entry can contain `%d' or `%d%s'; the %d will be
2051 replaced by the number of repetitions since the MONTH DAY YEAR,
2052 and %s by the ordinal ending of that number (that is, `st', `nd',
2053 `rd' or `th', as appropriate).
2055 An optional parameter MARK specifies a face or single-character
2056 string to use when highlighting the day in the calendar."
2057 (or (> n 0)
2058 (error "Day count must be positive"))
2059 (let* ((diff (- (calendar-absolute-from-gregorian date)
2060 (calendar-absolute-from-gregorian
2061 (diary-make-date month day year))))
2062 (cycle (/ diff n)))
2063 (and (>= diff 0) (zerop (% diff n))
2064 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
2066 (defun diary-day-of-year ()
2067 "Day of year and number of days remaining in the year of date diary entry."
2068 (calendar-day-of-year-string date))
2070 (defun diary-remind (sexp days &optional marking)
2071 "Provide a reminder of a diary entry.
2072 SEXP is a diary-sexp. DAYS is either a single number or a list
2073 of numbers indicating the number(s) of days before the event that
2074 the warning(s) should occur on. A negative number -DAYS has the
2075 same meaning as a list (1 2 ... DAYS). If the current date
2076 is (one of) DAYS before the event indicated by SEXP, then this function
2077 returns a suitable message (as specified by `diary-remind-message').
2079 In addition to the reminders beforehand, the diary entry also
2080 appears on the date itself.
2082 A `diary-nonmarking-symbol' at the beginning of the line of the
2083 `diary-remind' entry specifies that the diary entry (not the
2084 reminder) is non-marking. Marking of reminders is independent of
2085 whether the entry itself is a marking or nonmarking; if optional
2086 parameter MARKING is non-nil then the reminders are marked on the
2087 calendar."
2088 ;; `date' has a value at this point, from diary-sexp-entry.
2089 ;; Convert a negative number to a list of days.
2090 (and (integerp days)
2091 (< days 0)
2092 (setq days (number-sequence 1 (- days))))
2093 (let ((diary-entry (eval sexp)))
2094 (cond
2095 ;; Diary entry applies on date.
2096 ((and diary-entry
2097 (or (not diary-marking-entries-flag) diary-marking-entry-flag))
2098 diary-entry)
2099 ;; Diary entry may apply to `days' before date.
2100 ((and (integerp days)
2101 (not diary-entry) ; diary entry does not apply to date
2102 (or (not diary-marking-entries-flag) marking))
2103 ;; Adjust date, and re-evaluate.
2104 (let ((date (calendar-gregorian-from-absolute
2105 (+ (calendar-absolute-from-gregorian date) days))))
2106 (when (setq diary-entry (eval sexp))
2107 ;; Discard any mark portion from diary-anniversary, etc.
2108 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
2109 (mapconcat 'eval diary-remind-message ""))))
2110 ;; Diary entry may apply to one of a list of days before date.
2111 ((and (listp days) days)
2112 (or (diary-remind sexp (car days) marking)
2113 (diary-remind sexp (cdr days) marking))))))
2116 ;;; Diary insertion functions.
2118 ;;;###cal-autoload
2119 (defun diary-make-entry (string &optional nonmarking file)
2120 "Insert a diary entry STRING which may be NONMARKING in FILE.
2121 If omitted, NONMARKING defaults to nil and FILE defaults to
2122 `diary-file'."
2123 (let ((pop-up-frames (or pop-up-frames (window-dedicated-p))))
2124 (find-file-other-window (or file diary-file)))
2125 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
2126 (widen)
2127 (diary-unhide-everything)
2128 (goto-char (point-max))
2129 (when (let ((case-fold-search t))
2130 (search-backward "Local Variables:"
2131 (max (- (point-max) 3000) (point-min))
2133 (beginning-of-line)
2134 (insert "\n")
2135 (forward-line -1))
2136 (insert
2137 (if (bolp) "" "\n")
2138 (if nonmarking diary-nonmarking-symbol "")
2139 string " "))
2141 ;;;###cal-autoload
2142 (define-obsolete-function-alias 'make-diary-entry 'diary-make-entry "23.1")
2144 ;;;###cal-autoload
2145 (defun diary-insert-entry (arg &optional event)
2146 "Insert a diary entry for the date indicated by point.
2147 Prefix argument ARG makes the entry nonmarking."
2148 (interactive
2149 (list current-prefix-arg last-nonmenu-event))
2150 (diary-make-entry (calendar-date-string (calendar-cursor-to-date t event) t t)
2151 arg))
2153 ;;;###cal-autoload
2154 (define-obsolete-function-alias 'insert-diary-entry 'diary-insert-entry "23.1")
2156 ;;;###cal-autoload
2157 (defun diary-insert-weekly-entry (arg)
2158 "Insert a weekly diary entry for the day of the week indicated by point.
2159 Prefix argument ARG makes the entry nonmarking."
2160 (interactive "P")
2161 (diary-make-entry (calendar-day-name (calendar-cursor-to-date t))
2162 arg))
2164 ;;;###cal-autoload
2165 (define-obsolete-function-alias 'insert-weekly-diary-entry
2166 'diary-insert-weekly-entry "23.1")
2168 (defun diary-date-display-form (&optional type)
2169 "Return value for `calendar-date-display-form' using `calendar-date-style'.
2170 Optional symbol TYPE is either `monthly' or `yearly'."
2171 (cond ((eq type 'monthly) (cond ((eq calendar-date-style 'iso)
2172 '((format "*-*-%.2d"
2173 (string-to-number day))))
2174 ((eq calendar-date-style 'european)
2175 '(day " * "))
2176 (t '("* " day ))))
2177 ((eq type 'yearly) (cond ((eq calendar-date-style 'iso)
2178 '((format "*-%.2d-%.2d"
2179 (string-to-number month)
2180 (string-to-number day))))
2181 ((eq calendar-date-style 'european)
2182 '(day " " monthname))
2183 (t '(monthname " " day))))
2184 ;; Iso cannot contain "-", because this form used eg by
2185 ;; diary-insert-anniversary-entry.
2186 (t (cond ((eq calendar-date-style 'iso)
2187 '((format "%s %.2d %.2d" year
2188 (string-to-number month) (string-to-number day))))
2189 ((eq calendar-date-style 'european)
2190 '(day " " month " " year))
2191 (t '(month " " day " " year))))))
2193 (defun diary-insert-entry-1 (&optional type nomark months symbol absfunc)
2194 "Subroutine to insert a diary entry related to the date at point.
2195 TYPE is the type of entry (`monthly' or `yearly'). NOMARK non-nil
2196 means make the entry non-marking. Array MONTHS is used in place
2197 of `calendar-month-name-array'. String SYMBOL marks the type of
2198 diary entry. Function ABSFUNC converts absolute dates to dates of
2199 the appropriate type."
2200 (let ((calendar-date-display-form (if type
2201 (diary-date-display-form type)
2202 calendar-date-display-form))
2203 (calendar-month-name-array (or months calendar-month-name-array))
2204 (date (calendar-cursor-to-date t)))
2205 (diary-make-entry
2206 (format "%s%s" (or symbol "")
2207 (calendar-date-string
2208 (if absfunc
2209 (funcall absfunc (calendar-absolute-from-gregorian date))
2210 date)
2211 (not absfunc)
2212 (not type)))
2213 nomark)))
2215 ;;;###cal-autoload
2216 (defun diary-insert-monthly-entry (arg)
2217 "Insert a monthly diary entry for the day of the month indicated by point.
2218 Prefix argument ARG makes the entry nonmarking."
2219 (interactive "P")
2220 (diary-insert-entry-1 'monthly arg))
2222 ;;;###cal-autoload
2223 (define-obsolete-function-alias 'insert-monthly-diary-entry
2224 'diary-insert-monthly-entry "23.1")
2226 ;;;###cal-autoload
2227 (defun diary-insert-yearly-entry (arg)
2228 "Insert an annual diary entry for the day of the year indicated by point.
2229 Prefix argument ARG makes the entry nonmarking."
2230 (interactive "P")
2231 (diary-insert-entry-1 'yearly arg))
2233 ;;;###cal-autoload
2234 (define-obsolete-function-alias 'insert-yearly-diary-entry
2235 'diary-insert-yearly-entry "23.1")
2237 ;;;###cal-autoload
2238 (defun diary-insert-anniversary-entry (arg)
2239 "Insert an anniversary diary entry for the date given by point.
2240 Prefix argument ARG makes the entry nonmarking."
2241 (interactive "P")
2242 (let ((calendar-date-display-form (diary-date-display-form)))
2243 (diary-make-entry
2244 (format "%s(diary-anniversary %s)"
2245 diary-sexp-entry-symbol
2246 (calendar-date-string (calendar-cursor-to-date t) nil t))
2247 arg)))
2249 ;;;###cal-autoload
2250 (define-obsolete-function-alias 'insert-anniversary-diary-entry
2251 'diary-insert-anniversary-entry "23.1")
2253 ;;;###cal-autoload
2254 (defun diary-insert-block-entry (arg)
2255 "Insert a block diary entry for the days between the point and marked date.
2256 Prefix argument ARG makes the entry nonmarking."
2257 (interactive "P")
2258 (let ((calendar-date-display-form (diary-date-display-form))
2259 (cursor (calendar-cursor-to-date t))
2260 (mark (or (car calendar-mark-ring)
2261 (error "No mark set in this buffer")))
2262 start end)
2263 (if (< (calendar-absolute-from-gregorian mark)
2264 (calendar-absolute-from-gregorian cursor))
2265 (setq start mark
2266 end cursor)
2267 (setq start cursor
2268 end mark))
2269 (diary-make-entry
2270 (format "%s(diary-block %s %s)"
2271 diary-sexp-entry-symbol
2272 (calendar-date-string start nil t)
2273 (calendar-date-string end nil t))
2274 arg)))
2276 ;;;###cal-autoload
2277 (define-obsolete-function-alias 'insert-block-diary-entry
2278 'diary-insert-block-entry "23.1")
2280 ;;;###cal-autoload
2281 (defun diary-insert-cyclic-entry (arg)
2282 "Insert a cyclic diary entry starting at the date given by point.
2283 Prefix argument ARG makes the entry nonmarking."
2284 (interactive "P")
2285 (let ((calendar-date-display-form (diary-date-display-form)))
2286 (diary-make-entry
2287 (format "%s(diary-cyclic %d %s)"
2288 diary-sexp-entry-symbol
2289 (calendar-read "Repeat every how many days: "
2290 (lambda (x) (> x 0)))
2291 (calendar-date-string (calendar-cursor-to-date t) nil t))
2292 arg)))
2294 ;;;###cal-autoload
2295 (define-obsolete-function-alias 'insert-cyclic-diary-entry
2296 'diary-insert-cyclic-entry "23.1")
2298 ;;; Diary mode.
2300 (defun diary-redraw-calendar ()
2301 "If `calendar-buffer' is live and diary entries are marked, redraw it."
2302 (and calendar-mark-diary-entries-flag
2303 (save-excursion
2304 (calendar-redraw)))
2305 ;; Return value suitable for `write-contents-functions'.
2306 nil)
2308 (defvar diary-mode-map
2309 (let ((map (make-sparse-keymap)))
2310 (define-key map "\C-c\C-s" 'diary-show-all-entries)
2311 (define-key map "\C-c\C-q" 'quit-window)
2312 map)
2313 "Keymap for `diary-mode'.")
2315 (defun diary-font-lock-sexps (limit)
2316 "Recognize sexp diary entry up to LIMIT for font-locking."
2317 (if (re-search-forward
2318 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2319 (regexp-quote diary-sexp-entry-symbol))
2320 limit t)
2321 (condition-case nil
2322 (save-restriction
2323 (narrow-to-region (point-min) limit)
2324 (let ((start (point)))
2325 (forward-sexp 1)
2326 (store-match-data (list start (point)))
2328 (error t))))
2330 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
2331 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2332 If given, optional SYMBOL must be a prefix to entries. If
2333 optional ABBREV-ARRAY is present, also matches the abbreviations
2334 from this array (with or without a final `.'), in addition to the
2335 full month names."
2336 (let ((dayname (diary-name-pattern calendar-day-name-array
2337 calendar-day-abbrev-array t))
2338 (monthname (format "\\(%s\\|\\*\\)"
2339 (diary-name-pattern month-array abbrev-array)))
2340 (month "\\([0-9]+\\|\\*\\)")
2341 (day "\\([0-9]+\\|\\*\\)")
2342 (year "-?\\([0-9]+\\|\\*\\)"))
2343 (mapcar (lambda (x)
2344 (cons
2345 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
2346 (if symbol (regexp-quote symbol) "") "\\("
2347 (mapconcat 'eval
2348 ;; If backup, omit first item (backup)
2349 ;; and last item (not part of date).
2350 (if (equal (car x) 'backup)
2351 (nreverse (cdr (reverse (cdr x))))
2354 ;; With backup, last item is not part of date.
2355 (if (equal (car x) 'backup)
2356 (concat "\\)" (eval (car (reverse x))))
2357 "\\)"))
2358 '(1 diary-face)))
2359 diary-date-forms)))
2361 (defmacro diary-font-lock-keywords-1 (markfunc listfunc feature months symbol)
2362 "Subroutine of the function `diary-font-lock-keywords'.
2363 If MARKFUNC is a member of `diary-nongregorian-marking-hook', or
2364 LISTFUNC of `diary-nongregorian-listing-hook', then require FEATURE and
2365 return a font-lock pattern matching array of MONTHS and marking SYMBOL."
2366 `(when (or (memq ',markfunc diary-nongregorian-marking-hook)
2367 (memq ',listfunc diary-nongregorian-listing-hook))
2368 (require ',feature)
2369 (diary-font-lock-date-forms ,months ,symbol)))
2371 (defconst diary-time-regexp
2372 ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
2373 ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
2374 ;; Hence often prefix this with "\\(^\\|\\s-\\)."
2375 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
2376 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
2377 "\\)\\([AaPp][Mm]\\)?\\)")
2378 "Regular expression matching a time of day.")
2380 (defvar calendar-hebrew-month-name-array-leap-year)
2381 (defvar calendar-islamic-month-name-array)
2382 (defvar calendar-bahai-month-name-array)
2384 ;;;###cal-autoload
2385 (defun diary-font-lock-keywords ()
2386 "Return a value for the variable `diary-font-lock-keywords'."
2387 (append
2388 (diary-font-lock-date-forms calendar-month-name-array
2389 nil calendar-month-abbrev-array)
2390 (diary-font-lock-keywords-1 diary-hebrew-mark-entries
2391 diary-hebrew-list-entries
2392 cal-hebrew
2393 calendar-hebrew-month-name-array-leap-year
2394 diary-hebrew-entry-symbol)
2395 (diary-font-lock-keywords-1 diary-islamic-mark-entries
2396 diary-islamic-list-entries
2397 cal-islam
2398 calendar-islamic-month-name-array
2399 diary-islamic-entry-symbol)
2400 (diary-font-lock-keywords-1 diary-bahai-mark-entries
2401 diary-bahai-list-entries
2402 cal-bahai
2403 calendar-bahai-month-name-array
2404 diary-bahai-entry-symbol)
2405 (list
2406 (cons
2407 (format "^%s.*$" (regexp-quote diary-include-string))
2408 'font-lock-keyword-face)
2409 (cons
2410 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2411 (regexp-quote diary-sexp-entry-symbol))
2412 '(1 font-lock-constant-face))
2413 (cons
2414 (format "^%s" (regexp-quote diary-nonmarking-symbol))
2415 'font-lock-constant-face)
2416 (cons
2417 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol)
2418 (regexp-opt (mapcar 'regexp-quote
2419 (list diary-hebrew-entry-symbol
2420 diary-islamic-entry-symbol
2421 diary-bahai-entry-symbol))
2423 '(1 font-lock-constant-face))
2424 '(diary-font-lock-sexps . font-lock-keyword-face)
2425 ;; Don't need to worry about space around "-" because the first
2426 ;; match takes care of that. It does mean the "-" itself may or
2427 ;; may not be fontified though.
2428 ;; diary-date-forms often include a final character that is not
2429 ;; part of the date (eg a non-digit to mark the end of the year).
2430 ;; This can use up the only space char between a date and time (b#7891).
2431 ;; Hence we use OVERRIDE, which can only override whitespace.
2432 ;; FIXME it's probably better to tighten up the diary-time-regexp
2433 ;; and drop the whitespace requirement below.
2434 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2435 diary-time-regexp)
2436 . (0 'diary-time t)))))
2437 ; . 'diary-time))))
2439 (defvar diary-font-lock-keywords (diary-font-lock-keywords)
2440 "Forms to highlight in `diary-mode'.")
2442 ;;;###autoload
2443 (define-derived-mode diary-mode fundamental-mode "Diary"
2444 "Major mode for editing the diary file."
2445 (set (make-local-variable 'font-lock-defaults)
2446 '(diary-font-lock-keywords t))
2447 (set (make-local-variable 'comment-start) diary-comment-start)
2448 (set (make-local-variable 'comment-end) diary-comment-end)
2449 (add-to-invisibility-spec '(diary . nil))
2450 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
2451 ;; In case the file was modified externally, refresh the calendar
2452 ;; after refreshing the diary buffer.
2453 (add-hook 'after-revert-hook 'diary-redraw-calendar nil t)
2454 (if diary-header-line-flag
2455 (setq header-line-format diary-header-line-format)))
2458 ;;; Fancy Diary Mode.
2460 (defun diary-fancy-date-pattern ()
2461 "Return a regexp matching the first line of a fancy diary date header.
2462 This depends on the calendar date style."
2463 (concat
2464 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
2465 (monthname (diary-name-pattern calendar-month-name-array nil t))
2466 (day "1")
2467 (month "2")
2468 ;; FIXME? This used to be "-?[0-9]+" - what was the "-?" for?
2469 (year "3"))
2470 ;; This is ugly. c-d-d-form expects `day' etc to be "numbers in
2471 ;; string form"; eg the iso version calls string-to-number on some.
2472 ;; Therefore we cannot eg just let day = "[0-9]+". (Bug#8583).
2473 ;; Assumes no integers in c-day/month-name-array.
2474 (replace-regexp-in-string "[0-9]+" "[0-9]+"
2475 (mapconcat 'eval calendar-date-display-form "")
2476 nil t))
2477 ;; Optional ": holiday name" after the date.
2478 "\\(: .*\\)?"))
2480 (defun diary-fancy-date-matcher (limit)
2481 "Search for a fancy diary data header, up to LIMIT."
2482 ;; Any number of " other holiday name" lines, followed by "==" line.
2483 (when (re-search-forward
2484 (format "%s\\(\n +.*\\)*\n=+$" (diary-fancy-date-pattern)) limit t)
2485 (put-text-property (match-beginning 0) (match-end 0) 'font-lock-multiline t)
2488 (define-obsolete-variable-alias 'fancy-diary-font-lock-keywords
2489 'diary-fancy-font-lock-keywords "23.1")
2491 (defvar diary-fancy-font-lock-keywords
2492 `((diary-fancy-date-matcher . diary-face)
2493 ("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" . 'diary-anniversary)
2494 ("^.*Yahrzeit.*$" . font-lock-constant-face)
2495 ("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
2496 ("^Day.*omer.*$" . font-lock-builtin-face)
2497 ("^Parashat.*$" . font-lock-comment-face)
2498 (,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2499 diary-time-regexp) . 'diary-time))
2500 "Keywords to highlight in fancy diary display.")
2502 ;; If region looks like it might start or end in the middle of a
2503 ;; multiline pattern, extend the region to encompass the whole pattern.
2504 (defun diary-fancy-font-lock-fontify-region-function (beg end &optional verbose)
2505 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2506 Needed to handle multiline keyword in `diary-fancy-font-lock-keywords'.
2507 Fontify the region between BEG and END, quietly unless VERBOSE is non-nil."
2508 (goto-char beg)
2509 (forward-line 0)
2510 (if (looking-at "=+$") (forward-line -1))
2511 (while (and (looking-at " +[^ ]")
2512 (zerop (forward-line -1))))
2513 ;; This check not essential.
2514 (if (looking-at (diary-fancy-date-pattern))
2515 (setq beg (line-beginning-position)))
2516 (goto-char end)
2517 (forward-line 0)
2518 (while (and (looking-at " +[^ ]")
2519 (zerop (forward-line 1))))
2520 (if (looking-at "=+$")
2521 (setq end (line-beginning-position 2)))
2522 (font-lock-default-fontify-region beg end verbose))
2524 (defvar diary-fancy-overriding-map (make-sparse-keymap)
2525 "Keymap overriding minor-mode maps in `diary-fancy-display-mode'.")
2527 (define-derived-mode diary-fancy-display-mode special-mode
2528 "Diary"
2529 "Major mode used while displaying diary entries using Fancy Display."
2530 (set (make-local-variable 'font-lock-defaults)
2531 '(diary-fancy-font-lock-keywords
2532 t nil nil nil
2533 (font-lock-fontify-region-function
2534 . diary-fancy-font-lock-fontify-region-function)))
2535 (set (make-local-variable 'minor-mode-overriding-map-alist)
2536 (list (cons t diary-fancy-overriding-map)))
2537 (view-mode 1))
2539 (define-obsolete-function-alias 'fancy-diary-display-mode
2540 'diary-fancy-display-mode "23.1")
2542 ;; Following code from Dave Love <fx@gnu.org>.
2543 ;; Import Outlook-format appointments from mail messages in Gnus or
2544 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2545 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2546 ;; could be run from hooks to notice appointments automatically (in
2547 ;; which case they will prompt about adding to the diary). The
2548 ;; message formats recognized are customizable through `diary-outlook-formats'.
2550 (defun diary-from-outlook-internal (subject body &optional test-only)
2551 "Snarf a diary entry from a message assumed to be from MS Outlook.
2552 SUBJECT and BODY are strings giving the message subject and body.
2553 Arg TEST-ONLY non-nil means return non-nil if and only if the
2554 message contains an appointment, don't make a diary entry."
2555 (catch 'finished
2556 (let (format-string)
2557 (dolist (fmt diary-outlook-formats)
2558 (when (eq 0 (string-match (car fmt) body))
2559 (unless test-only
2560 (setq format-string (cdr fmt))
2561 (save-excursion
2562 (save-window-excursion
2563 (diary-make-entry
2564 (format (replace-match (if (functionp format-string)
2565 (funcall format-string body)
2566 format-string)
2567 t nil (match-string 0 body))
2568 subject)))))
2569 (throw 'finished t))))
2570 nil))
2572 (defvar gnus-article-mime-handles)
2573 (defvar gnus-article-buffer)
2575 (autoload 'gnus-fetch-field "gnus-util")
2576 (autoload 'gnus-narrow-to-body "gnus")
2577 (autoload 'mm-get-part "mm-decode")
2579 (defun diary-from-outlook-gnus (&optional noconfirm)
2580 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2581 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2582 this function is called interactively), then if an entry is found the
2583 user is asked to confirm its addition.
2584 Add this function to `gnus-article-prepare-hook' to notice appointments
2585 automatically."
2586 (interactive "p")
2587 (with-current-buffer gnus-article-buffer
2588 (let ((subject (gnus-fetch-field "subject"))
2589 (body (if gnus-article-mime-handles
2590 ;; We're multipart. Don't get confused by part
2591 ;; buttons &c. Assume info is in first part.
2592 (mm-get-part (nth 1 gnus-article-mime-handles))
2593 (save-restriction
2594 (gnus-narrow-to-body)
2595 (buffer-string)))))
2596 (when (diary-from-outlook-internal subject body t)
2597 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2598 (diary-from-outlook-internal subject body)
2599 (message "Diary entry added"))))))
2601 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2603 (defvar rmail-buffer)
2605 (defun diary-from-outlook-rmail (&optional noconfirm)
2606 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2607 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2608 this function is called interactively), then if an entry is found the
2609 user is asked to confirm its addition."
2610 (interactive "p")
2611 ;; FIXME maybe the body needs rmail-mm decoding, in which case
2612 ;; there is no single buffer with both body and subject, sigh.
2613 (with-current-buffer rmail-buffer
2614 (let ((subject (mail-fetch-field "subject"))
2615 (body (buffer-substring (save-excursion
2616 (rfc822-goto-eoh)
2617 (point))
2618 (point-max))))
2619 (when (diary-from-outlook-internal subject body t)
2620 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2621 (diary-from-outlook-internal subject body)
2622 (message "Diary entry added"))))))
2624 (defvar diary-from-outlook-function nil
2625 "If non-nil, a function of one argument for `diary-from-outlook' to call.
2626 If the current buffer contains an Outlook-style appointment message,
2627 this function should extract it into a diary entry. If the argument is
2628 nil, it should ask for confirmation before adding this entry to the diary.
2629 For examples, see `diary-from-outlook-rmail' and `diary-from-outlook-gnus'.")
2631 (defun diary-from-outlook (&optional noconfirm)
2632 "Maybe snarf diary entry from current Outlook-generated message.
2633 Uses `diary-from-outlook-function' if that is non-nil, else
2634 `diary-from-outlook-rmail' for Rmail or `diary-from-outlook-gnus' for Gnus.
2635 Unless the optional argument NOCONFIRM is non-nil (which is the
2636 case when this function is called interactively), then if an
2637 entry is found the user is asked to confirm its addition."
2638 (interactive "p")
2639 (let ((func (cond
2640 (diary-from-outlook-function)
2641 ((eq major-mode 'rmail-mode)
2642 #'diary-from-outlook-rmail)
2643 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2644 #'diary-from-outlook-gnus)
2645 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2646 (funcall func noconfirm)))
2648 (provide 'diary-lib)
2650 ;; Local Variables:
2651 ;; coding: utf-8
2652 ;; End:
2654 ;;; diary-lib.el ends here