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