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