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