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