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