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