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