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