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