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