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