]> code.delx.au - gnu-emacs/blob - lisp/calendar/calendar.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / calendar / calendar.el
1 ;;; calendar.el --- calendar functions
2
3 ;; Copyright (C) 1988-1995, 1997, 2000-2011
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: calendar, Gregorian calendar, diary, holidays
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This collection of functions implements a calendar window. It
29 ;; generates a calendar for the current month, together with the
30 ;; previous and coming months, or for any other three-month period.
31 ;; The calendar can be scrolled forward and backward in the window to
32 ;; show months in the past or future; the cursor can move forward and
33 ;; backward by days, weeks, or months, making it possible, for
34 ;; instance, to jump to the date a specified number of days, weeks, or
35 ;; months from the date under the cursor. The user can display a list
36 ;; of holidays and other notable days for the period shown; the
37 ;; notable days can be marked on the calendar, if desired. The user
38 ;; can also specify that dates having corresponding diary entries (in
39 ;; a file that the user specifies) be marked; the diary entries for
40 ;; any date can be viewed in a separate window. The diary and the
41 ;; notable days can be viewed independently of the calendar. Dates
42 ;; can be translated from the (usual) Gregorian calendar to the day of
43 ;; the year/days remaining in year, to the ISO commercial calendar, to
44 ;; the Julian (old style) calendar, to the Hebrew calendar, to the
45 ;; Islamic calendar, to the Baha'i calendar, to the French
46 ;; Revolutionary calendar, to the Mayan calendar, to the Chinese
47 ;; calendar, to the Coptic calendar, to the Ethiopic calendar, and to
48 ;; the astronomical (Julian) day number. Times of sunrise/sunset can
49 ;; be displayed, as can the phases of the moon. Appointment
50 ;; notification for diary entries is available. Calendar printing via
51 ;; LaTeX is available.
52
53 ;; The following files are part of the calendar/diary code:
54
55 ;; appt.el Appointment notification
56 ;; cal-bahai.el Baha'i calendar
57 ;; cal-china.el Chinese calendar
58 ;; cal-coptic.el Coptic/Ethiopic calendars
59 ;; cal-dst.el Daylight saving time rules
60 ;; cal-french.el French revolutionary calendar
61 ;; cal-hebrew.el Hebrew calendar
62 ;; cal-html.el Calendars in HTML
63 ;; cal-islam.el Islamic calendar
64 ;; cal-iso.el ISO calendar
65 ;; cal-julian.el Julian/astronomical calendars
66 ;; cal-mayan.el Mayan calendars
67 ;; cal-menu.el Menu support
68 ;; cal-move.el Movement in the calendar
69 ;; cal-persia.el Persian calendar
70 ;; cal-tex.el Calendars in LaTeX
71 ;; cal-x.el Dedicated frame functions
72 ;; calendar.el This file
73 ;; diary-lib.el Diary functions
74 ;; holidays.el Holiday functions
75 ;; lunar.el Phases of the moon
76 ;; solar.el Sunrise/sunset, equinoxes/solstices
77
78 ;; Technical details of all the calendrical calculations can be found in
79 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
80 ;; and Nachum Dershowitz, Cambridge University Press (2001).
81
82 ;; An earlier version of the technical details appeared in
83 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
84 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
85 ;; pages 899-928, and in ``Calendrical Calculations, Part II: Three Historical
86 ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
87 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
88 ;; pages 383-404.
89
90 ;; Hard copies of these two papers can be obtained by sending email to
91 ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
92 ;; the message BODY containing your mailing address (snail).
93
94
95 ;; A note on free variables:
96
97 ;; The calendar passes around a few dynamically bound variables, which
98 ;; unfortunately have rather common names. They are meant to be
99 ;; available for external functions, so the names can't be changed.
100
101 ;; displayed-month, displayed-year: bound in calendar-generate, the
102 ;; central month of the 3 month calendar window
103 ;; original-date, number: bound in diary-list-entries, the arguments
104 ;; with which that function was called.
105 ;; date, entry: bound in diary-list-sexp-entries (qv)
106
107 ;; Bound in diary-list-entries:
108 ;; diary-entries-list: use in d-l, appt.el, and by diary-add-to-list
109 ;; diary-saved-point: only used in diary-lib.el, passed to the display func
110 ;; date-string: only used in diary-lib.el
111 ;; list-only: don't modify the diary-buffer, just return a list of entries
112 ;; file-glob-attrs: yuck
113
114 ;;; Code:
115
116 (load "cal-loaddefs" nil t)
117
118 ;; Avoid recursive load of calendar when loading cal-menu. Yuck.
119 (provide 'calendar)
120 (require 'cal-menu)
121
122 (defgroup calendar nil
123 "Calendar and time management support."
124 :prefix "calendar-"
125 :group 'applications)
126
127 (defgroup calendar-hooks nil
128 "Calendar hooks."
129 :prefix "calendar-"
130 :group 'calendar)
131
132 (defgroup calendar-faces nil
133 "Calendar faces."
134 :prefix "calendar-"
135 :group 'calendar)
136
137 (defcustom calendar-offset 0
138 "The offset of the principal month from the center of the calendar window.
139 0 means the principal month is in the center (default), -1 means on the left,
140 +1 means on the right. Larger (or smaller) values push the principal month off
141 the screen."
142 :type 'integer
143 :group 'calendar)
144
145 (defcustom calendar-setup nil
146 "The frame setup of the calendar.
147 The choices are: `one-frame' (calendar and diary together in one separate,
148 dedicated frame); `two-frames' (calendar and diary in separate, dedicated
149 frames); `calendar-only' (calendar in a separate, dedicated frame); with
150 any other value the current frame is used. Using any of the first
151 three options overrides the value of `calendar-view-diary-initially-flag'."
152 :type '(choice
153 (const :tag "calendar and diary in separate frame" one-frame)
154 (const :tag "calendar and diary each in own frame" two-frames)
155 (const :tag "calendar in separate frame" calendar-only)
156 (const :tag "use current frame" nil))
157 :group 'calendar)
158
159 (defcustom calendar-minimum-window-height 8
160 "Minimum height `calendar-generate-window' should use for calendar window."
161 :type 'integer
162 :version "22.1"
163 :group 'calendar)
164
165 ;; See discussion in bug#1806.
166 (defcustom calendar-split-width-threshold nil
167 "Value to use for `split-width-threshold' when creating a calendar.
168 This only affects frames wider than the default value of
169 `split-width-threshold'."
170 :type '(choice (const nil)
171 (integer))
172 :version "23.2"
173 :group 'calendar)
174
175 (defcustom calendar-week-start-day 0
176 "The day of the week on which a week in the calendar begins.
177 0 means Sunday (default), 1 means Monday, and so on.
178
179 If you change this variable directly (without using customize)
180 after starting `calendar', you should call `calendar-redraw' to
181 update the calendar display to reflect the change, otherwise
182 movement commands will not work correctly."
183 :type 'integer
184 ;; Change the initialize so that if you reload calendar.el, it will not
185 ;; cause a redraw (which may fail, e.g. with "invalid byte-code in
186 ;; calendar.elc" because of the "byte-compile-dynamic").
187 :initialize 'custom-initialize-default
188 :set (lambda (sym val)
189 (set sym val)
190 (calendar-redraw))
191 :group 'calendar)
192
193 (define-obsolete-variable-alias 'view-diary-entries-initially
194 'calendar-view-diary-initially-flag "23.1")
195
196 (defcustom calendar-view-diary-initially-flag nil
197 "Non-nil means display current date's diary entries on entry to calendar.
198 The diary is displayed in another window when the calendar is first displayed,
199 if the current date is visible. The number of days of diary entries displayed
200 is governed by the variable `diary-number-of-entries'. This variable can
201 be overridden by the value of `calendar-setup'."
202 :type 'boolean
203 :group 'diary)
204
205 (define-obsolete-variable-alias 'mark-diary-entries-in-calendar
206 'calendar-mark-diary-entries-flag "23.1")
207
208 ;; FIXME :set
209 (defcustom calendar-mark-diary-entries-flag nil
210 "Non-nil means mark dates with diary entries, in the calendar window.
211 The marking symbol is specified by the variable `diary-entry-marker'."
212 :type 'boolean
213 :group 'diary)
214
215 (defcustom calendar-remove-frame-by-deleting t
216 "Determine how the calendar mode removes a frame no longer needed.
217 If nil, make an icon of the frame. If non-nil, delete the frame."
218 :type 'boolean
219 :version "23.1" ; changed from nil to t
220 :group 'view
221 :group 'calendar)
222
223 (defface calendar-today
224 '((t (:underline t)))
225 "Face for indicating today's date in the calendar.
226 See the variable `calendar-today-marker'."
227 :group 'calendar-faces)
228
229 (define-obsolete-face-alias 'calendar-today-face 'calendar-today "22.1")
230
231 (defface diary
232 '((((min-colors 88) (class color) (background light))
233 :foreground "red1")
234 (((class color) (background light))
235 :foreground "red")
236 (((min-colors 88) (class color) (background dark))
237 :foreground "yellow1")
238 (((class color) (background dark))
239 :foreground "yellow")
240 (t
241 :weight bold))
242 "Face for highlighting diary entries.
243 Used to mark diary entries in the calendar (see `diary-entry-marker'),
244 and to highlight the date header in the fancy diary."
245 :group 'calendar-faces)
246
247 (define-obsolete-face-alias 'diary-face 'diary "22.1")
248
249 (defface holiday
250 '((((class color) (background light))
251 :background "pink")
252 (((class color) (background dark))
253 :background "chocolate4")
254 (t
255 :inverse-video t))
256 "Face for indicating in the calendar dates that have holidays.
257 See `calendar-holiday-marker'."
258 :group 'calendar-faces)
259
260 (define-obsolete-face-alias 'holiday-face 'holiday "22.1")
261
262 ;; These briefly checked font-lock-mode, but that is broken, since it
263 ;; is a buffer-local variable, and which buffer happens to be current
264 ;; when this file is loaded shouldn't make a difference. One could
265 ;; perhaps check global-font-lock-mode, or font-lock-global-modes; but
266 ;; this feature doesn't use font-lock, so there's no real reason it
267 ;; should respect those either. See bug#2199.
268 ;; They also used to check display-color-p, but that is a problem if
269 ;; loaded from --daemon. Since BW displays are rare now, this was
270 ;; also taken out. The way to keep it would be to have nil mean do a
271 ;; runtime check whenever this variable is used.
272 (defcustom diary-entry-marker 'diary
273 "How to mark dates that have diary entries.
274 The value can be either a single-character string (e.g. \"+\") or a face."
275 :type '(choice (string :tag "Single character string") face)
276 :group 'diary
277 :version "23.1")
278
279 (defcustom calendar-today-marker 'calendar-today
280 "How to mark today's date in the calendar.
281 The value can be either a single-character string (e.g. \"=\") or a face.
282 Used by `calendar-mark-today'."
283 :type '(choice (string :tag "Single character string") face)
284 :group 'calendar
285 :version "23.1")
286
287 (defcustom calendar-holiday-marker 'holiday
288 "How to mark notable dates in the calendar.
289 The value can be either a single-character string (e.g. \"*\") or a face."
290 :type '(choice (string :tag "Single character string") face)
291 :group 'holidays
292 :version "23.1")
293
294 (define-obsolete-variable-alias 'view-calendar-holidays-initially
295 'calendar-view-holidays-initially-flag "23.1")
296
297 (defcustom calendar-view-holidays-initially-flag nil
298 "Non-nil means display holidays for current three month period on entry.
299 The holidays are displayed in another window when the calendar is first
300 displayed."
301 :type 'boolean
302 :group 'holidays)
303
304 (define-obsolete-variable-alias 'mark-holidays-in-calendar
305 'calendar-mark-holidays-flag "23.1")
306
307 ;; FIXME :set
308 (defcustom calendar-mark-holidays-flag nil
309 "Non-nil means mark dates of holidays in the calendar window.
310 The marking symbol is specified by the variable `calendar-holiday-marker'."
311 :type 'boolean
312 :group 'holidays)
313
314 (defcustom calendar-mode-hook nil
315 "Hook run when entering `calendar-mode'."
316 :type 'hook
317 :group 'calendar-hooks)
318
319 (defcustom calendar-load-hook nil
320 "List of functions to be called after the calendar is first loaded.
321 This is the place to add key bindings to `calendar-mode-map'."
322 :type 'hook
323 :group 'calendar-hooks)
324
325 (define-obsolete-variable-alias 'initial-calendar-window-hook
326 'calendar-initial-window-hook "23.1")
327
328 (defcustom calendar-initial-window-hook nil
329 "List of functions to be called when the calendar window is created.
330 Quitting the calendar and re-entering it will cause these functions
331 to be called again."
332 :type 'hook
333 :group 'calendar-hooks)
334
335 (define-obsolete-variable-alias 'today-visible-calendar-hook
336 'calendar-today-visible-hook "23.1")
337
338 (defcustom calendar-today-visible-hook nil
339 "List of functions called whenever the current date is visible.
340 To mark today's date, add the function `calendar-mark-today'.
341 To replace the date with asterisks, add the function `calendar-star-date'.
342 See also `calendar-today-invisible-hook'.
343
344 In general, be careful about changing characters in the calendar buffer,
345 since it may cause the movement commands to fail."
346 :type 'hook
347 :options '(calendar-mark-today calendar-star-date)
348 :group 'calendar-hooks)
349
350 (define-obsolete-variable-alias 'today-invisible-calendar-hook
351 'calendar-today-invisible-hook "23.1")
352
353 (defcustom calendar-today-invisible-hook nil
354 "List of functions called whenever the current date is not visible.
355 See also `calendar-today-visible-hook'."
356 :type 'hook
357 :group 'calendar-hooks)
358
359 (defcustom calendar-move-hook nil
360 "List of functions called whenever the cursor moves in the calendar.
361 For example,
362
363 (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
364
365 redisplays the diary for whatever date the cursor is moved to."
366 :type 'hook
367 :options '(calendar-update-mode-line)
368 :group 'calendar-hooks)
369
370 (defcustom calendar-date-echo-text
371 "mouse-2: general menu\nmouse-3: menu for this date"
372 "String displayed when the cursor is over a date in the calendar.
373 Can be either a fixed string, or a lisp expression that returns one.
374 When this expression is evaluated, DAY, MONTH, and YEAR are
375 integers appropriate to the relevant date. For example, to
376 display the ISO date:
377
378 (setq calendar-date-echo-text '(format \"ISO date: %s\"
379 (calendar-iso-date-string
380 (list month day year))))
381 Changing this variable without using customize has no effect on
382 pre-existing calendar windows."
383 :group 'calendar
384 :initialize 'custom-initialize-default
385 :risky t
386 :set (lambda (sym val)
387 (set sym val)
388 (calendar-redraw))
389 :type '(choice (string :tag "Fixed string")
390 (sexp :value
391 (format "ISO date: %s"
392 (calendar-iso-date-string
393 (list month day year)))))
394 :version "23.1")
395
396
397 (defvar calendar-month-digit-width nil
398 "Width of the region with numbers in each month in the calendar.")
399
400 (defvar calendar-month-width nil
401 "Full width of each month in the calendar.")
402
403 (defvar calendar-right-margin nil
404 "Right margin of the calendar.")
405
406 (defvar calendar-month-edges nil
407 "Alist of month edge columns.
408 Each element has the form (N LEFT FIRST LAST RIGHT), where
409 LEFT is the leftmost column associated with month segment N,
410 FIRST and LAST are the first and last columns with day digits in,
411 and LAST is the rightmost column.")
412
413 (defun calendar-month-edges (segment)
414 "Compute the month edge columns for month SEGMENT.
415 Returns a list (LEFT FIRST LAST RIGHT), where LEFT is the
416 leftmost column associated with a month, FIRST and LAST are the
417 first and last columns with day digits in, and LAST is the
418 rightmost column."
419 ;; The leftmost column with a digit in it in this month segment.
420 (let* ((first (+ calendar-left-margin
421 (* segment calendar-month-width)))
422 ;; The rightmost column with a digit in it in this month segment.
423 (last (+ first (1- calendar-month-digit-width)))
424 (left (if (eq segment 0)
425 0
426 (+ calendar-left-margin
427 (* segment calendar-month-width)
428 (- (/ calendar-intermonth-spacing 2)))))
429 ;; The rightmost edge of this month segment, dividing the
430 ;; space between months in two.
431 (right (+ calendar-left-margin
432 (* (1+ segment) calendar-month-width)
433 (- (/ calendar-intermonth-spacing 2)))))
434 (list left first last right)))
435
436 (defun calendar-recompute-layout-variables ()
437 "Recompute some layout-related calendar \"constants\"."
438 (setq calendar-month-digit-width (+ (* 6 calendar-column-width)
439 calendar-day-digit-width)
440 calendar-month-width (+ (* 7 calendar-column-width)
441 calendar-intermonth-spacing)
442 calendar-right-margin (+ calendar-left-margin
443 (* 3 (* 7 calendar-column-width))
444 (* 2 calendar-intermonth-spacing))
445 calendar-month-edges nil)
446 (dotimes (i 3)
447 (push (cons i (calendar-month-edges i)) calendar-month-edges))
448 (setq calendar-month-edges (reverse calendar-month-edges)))
449
450 ;; FIXME add font-lock-keywords.
451 (defun calendar-set-layout-variable (symbol value &optional minmax)
452 "Set SYMBOL's value to VALUE, an integer.
453 A positive/negative MINMAX enforces a minimum/maximum value.
454 Then redraw the calendar, if necessary."
455 (let ((oldvalue (symbol-value symbol)))
456 (custom-set-default symbol (if minmax
457 (if (< minmax 0)
458 (min value (- minmax))
459 (max value minmax))
460 value))
461 (unless (equal value oldvalue)
462 (calendar-recompute-layout-variables)
463 (calendar-redraw))))
464
465 (defcustom calendar-left-margin 5
466 "Empty space to the left of the first month in the calendar."
467 :group 'calendar
468 :initialize 'custom-initialize-default
469 :set 'calendar-set-layout-variable
470 :type 'integer
471 :version "23.1")
472
473 ;; Or you can view it as columns of width 2, with 1 space, no space
474 ;; after the last column, and a 5 space gap between month.
475 ;; FIXME check things work if this is odd.
476 (defcustom calendar-intermonth-spacing 4
477 "Space between months in the calendar. Minimum value is 1."
478 :group 'calendar
479 :initialize 'custom-initialize-default
480 :set (lambda (sym val)
481 (calendar-set-layout-variable sym val 1))
482 :type 'integer
483 :version "23.1")
484
485 ;; FIXME calendar-month-column-width?
486 (defcustom calendar-column-width 3
487 "Width of each day column in the calendar. Minimum value is 3."
488 :initialize 'custom-initialize-default
489 :set (lambda (sym val)
490 (calendar-set-layout-variable sym val 3))
491 :type 'integer
492 :version "23.1")
493
494 (defcustom calendar-day-header-width 2
495 "Width of the day column headers in the calendar.
496 Must be at least one less than `calendar-column-width'."
497 :group 'calendar
498 :initialize 'custom-initialize-default
499 :set (lambda (sym val)
500 (calendar-set-layout-variable sym val (- 1 calendar-column-width)))
501 :type 'integer
502 :version "23.1")
503
504 ;; FIXME a format specifier instead?
505 (defcustom calendar-day-digit-width 2
506 "Width of the day digits in the calendar. Minimum value is 2."
507 :group 'calendar
508 :initialize 'custom-initialize-default
509 :set (lambda (sym val)
510 (calendar-set-layout-variable sym val 2))
511 :type 'integer
512 :version "23.1")
513
514 (defcustom calendar-intermonth-header nil
515 "Header text display in the space to the left of each calendar month.
516 See `calendar-intermonth-text'."
517 :group 'calendar
518 :initialize 'custom-initialize-default
519 :risky t
520 :set (lambda (sym val)
521 (set sym val)
522 (calendar-redraw))
523 :type '(choice (const nil :tag "Nothing")
524 (string :tag "Fixed string")
525 (sexp :value
526 (propertize "WK" 'font-lock-face
527 'font-lock-function-name-face)))
528 :version "23.1")
529
530 (defcustom calendar-intermonth-text nil
531 "Text to display in the space to the left of each calendar month.
532 Can be nil, a fixed string, or a lisp expression that returns a string.
533 When the expression is evaluated, the variables DAY, MONTH and YEAR
534 are integers appropriate for the first day in each week.
535 Will be truncated to the smaller of `calendar-left-margin' and
536 `calendar-intermonth-spacing'. The last character is forced to be a space.
537 For example, to display the ISO week numbers:
538
539 (setq calendar-week-start-day 1
540 calendar-intermonth-text
541 '(propertize
542 (format \"%2d\"
543 (car
544 (calendar-iso-from-absolute
545 (calendar-absolute-from-gregorian (list month day year)))))
546 'font-lock-face 'font-lock-function-name-face))
547
548 See also `calendar-intermonth-header'."
549 :group 'calendar
550 :initialize 'custom-initialize-default
551 :risky t
552 :set (lambda (sym val)
553 (set sym val)
554 (calendar-redraw))
555 :type '(choice (const nil :tag "Nothing")
556 (string :tag "Fixed string")
557 (sexp :value
558 (propertize
559 (format "%2d"
560 (car
561 (calendar-iso-from-absolute
562 (calendar-absolute-from-gregorian
563 (list month day year)))))
564 'font-lock-face 'font-lock-function-name-face)))
565 :version "23.1")
566
567 (defcustom diary-file "~/diary"
568 "Name of the file in which one's personal diary of dates is kept.
569
570 The file's entries are lines beginning with any of the forms
571 specified by the variable `diary-date-forms', which by default
572 uses the forms of `diary-american-date-forms':
573
574 MONTH/DAY
575 MONTH/DAY/YEAR
576 MONTHNAME DAY
577 MONTHNAME DAY, YEAR
578 DAYNAME
579
580 with the remainder of the line being the diary entry string for
581 that date. MONTH and DAY are one or two digit numbers, YEAR is a
582 number and may be written in full or abbreviated to the final two
583 digits (if `diary-abbreviated-year-flag' is non-nil). MONTHNAME
584 and DAYNAME can be spelled in full (as specified by the variables
585 `calendar-month-name-array' and `calendar-day-name-array'), or
586 abbreviated (as specified by `calendar-month-abbrev-array' and
587 `calendar-day-abbrev-array') with or without a period. Case is
588 ignored. Any of DAY, MONTH, or MONTHNAME, YEAR can be `*' which
589 matches any day, month, or year, respectively. If the date does
590 not contain a year, it is generic and applies to any year. A
591 DAYNAME entry applies to the appropriate day of the week in every week.
592
593 You can customize `diary-date-forms' to your preferred format.
594 Three default styles are provided: `diary-american-date-forms',
595 `diary-european-date-forms', and `diary-iso-date-forms'.
596 You can choose between these by setting `calendar-date-style' in your
597 .emacs file, or by using `calendar-set-date-style' when in the calendar.
598
599 A diary entry can be preceded by the character `diary-nonmarking-symbol'
600 \(ordinarily `&') to make that entry nonmarking--that is, it will not be
601 marked on dates in the calendar window but will appear in a diary window.
602
603 Multiline diary entries are made by indenting lines after the first with
604 either a TAB or one or more spaces.
605
606 Lines not in one the above formats are ignored. Here are some sample diary
607 entries (in the default American style):
608
609 12/22/1988 Twentieth wedding anniversary!!
610 &1/1. Happy New Year!
611 10/22 Ruth's birthday.
612 21: Payday
613 Tuesday--weekly meeting with grad students at 10am
614 Supowit, Shen, Bitner, and Kapoor to attend.
615 1/13/89 Friday the thirteenth!!
616 &thu 4pm squash game with Lloyd.
617 mar 16 Dad's birthday
618 April 15, 1989 Income tax due.
619 &* 15 time cards due.
620
621 If the first line of a diary entry consists only of the date or day name with
622 no trailing blanks or punctuation, then that line is not displayed in the
623 diary window; only the continuation lines is shown. For example, the
624 single diary entry
625
626 02/11/1989
627 Bill Blattner visits Princeton today
628 2pm Cognitive Studies Committee meeting
629 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
630 4:00pm Jamie Tappenden
631 7:30pm Dinner at George and Ed's for Alan Ryan
632 7:30-10:00pm dance at Stewart Country Day School
633
634 will appear in the diary window without the date line at the beginning. This
635 facility allows the diary window to look neater, but can cause confusion if
636 used with more than one day's entries displayed.
637
638 Diary entries can be based on Lisp sexps. For example, the diary entry
639
640 %%(diary-block 11 1 1990 11 10 1990) Vacation
641
642 causes the diary entry \"Vacation\" to appear from November 1 through
643 November 10, 1990. See the documentation for the function
644 `diary-list-sexp-entries' for more details.
645
646 Diary entries based on the Hebrew, the Islamic and/or the Baha'i
647 calendar are also possible, but because these are somewhat slow, they
648 are ignored unless you set the `diary-nongregorian-listing-hook' and
649 the `diary-nongregorian-marking-hook' appropriately. See the
650 documentation of these hooks for details.
651
652 Diary files can contain directives to include the contents of other files; for
653 details, see the documentation for the variable `diary-list-entries-hook'."
654 :type 'file
655 :group 'diary)
656
657 ;; FIXME do these have to be single characters?
658 (defcustom diary-nonmarking-symbol "&"
659 "Symbol indicating that a diary entry is not to be marked in the calendar."
660 :type 'string
661 :group 'diary)
662
663 (define-obsolete-variable-alias 'hebrew-diary-entry-symbol
664 'diary-hebrew-entry-symbol "23.1")
665
666 (defcustom diary-hebrew-entry-symbol "H"
667 "Symbol indicating a diary entry according to the Hebrew calendar."
668 :type 'string
669 :group 'diary)
670
671 (define-obsolete-variable-alias 'islamic-diary-entry-symbol
672 'diary-islamic-entry-symbol "23.1")
673
674 (defcustom diary-islamic-entry-symbol "I"
675 "Symbol indicating a diary entry according to the Islamic calendar."
676 :type 'string
677 :group 'diary)
678
679 (define-obsolete-variable-alias 'bahai-diary-entry-symbol
680 'diary-bahai-entry-symbol "23.1")
681
682 (defcustom diary-bahai-entry-symbol "B"
683 "Symbol indicating a diary entry according to the Baha'i calendar."
684 :type 'string
685 :group 'diary)
686
687 (defcustom european-calendar-style nil
688 "Non-nil means use the European style of dates in the diary and display.
689 In this case, a date like 1/2/1990 would be interpreted as
690 February 1, 1990. See `diary-european-date-forms' for the
691 default European diary date styles.
692
693 Setting this variable directly does not take effect (if the
694 calendar package is already loaded). Rather, use either
695 \\[customize] or the function `calendar-set-date-style'."
696 :type 'boolean
697 ;; Without :initialize (require 'calendar) throws an error because
698 ;; calendar-set-date-style is undefined at this point.
699 :initialize 'custom-initialize-default
700 :set (lambda (symbol value)
701 (if value
702 (calendar-set-date-style 'european)
703 (calendar-set-date-style 'american)))
704 :group 'calendar)
705
706 (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
707
708 ;; If this is autoloaded, c-d-s gets set before any customization of e-c-s.
709 (defcustom calendar-date-style (if european-calendar-style 'european
710 'american)
711 "Your preferred style for writing dates.
712 The options are:
713 `american' - month/day/year
714 `european' - day/month/year
715 `iso' - year/month/day
716 This affects how dates written in your diary are interpreted.
717 It also affects date display, as well as those calendar and diary
718 functions that take a date as an argument, e.g. `diary-date', by
719 changing the order in which the arguments are interpreted.
720
721 Setting this variable directly does not take effect (if the
722 calendar package is already loaded). Rather, use either
723 \\[customize] or the function `calendar-set-date-style'."
724 :version "23.1"
725 :type '(choice (const american :tag "Month/Day/Year")
726 (const european :tag "Day/Month/Year")
727 (const iso :tag "Year/Month/Day"))
728 :initialize 'custom-initialize-default
729 :set (lambda (symbol value)
730 (calendar-set-date-style value))
731 :group 'calendar)
732
733 ;; Next three are provided to aid in setting diary-date-forms.
734 ;; FIXME move to diary-lib?
735 (defcustom diary-iso-date-forms
736 '((month "[-/]" day "[^-/0-9]")
737 (year "[-/]" month "[-/]" day "[^0-9]")
738 ;; Cannot allow [-/] as separators here, since it would also match
739 ;; the first element (bug#7377).
740 (monthname " *" day "[^-0-9]")
741 (year " *" monthname " *" day "[^0-9]")
742 (dayname "\\W"))
743 "List of pseudo-patterns describing the ISO style of dates.
744 The defaults are: MONTH[-/]DAY; YEAR[-/]MONTH[-/]DAY; MONTHNAME DAY;
745 YEAR MONTHNAME DAY; DAYNAME. Normally you should not customize this,
746 but `diary-date-forms' (which see)."
747 :version "23.3" ; bug#7377
748 :type '(repeat (choice (cons :tag "Backup"
749 :value (backup . nil)
750 (const backup)
751 (repeat (list :inline t :format "%v"
752 (symbol :tag "Keyword")
753 (choice symbol regexp))))
754 (repeat (list :inline t :format "%v"
755 (symbol :tag "Keyword")
756 (choice symbol regexp)))))
757 :group 'diary)
758
759 (define-obsolete-variable-alias 'american-date-diary-pattern
760 'diary-american-date-forms "23.1")
761
762 (defcustom diary-american-date-forms
763 '((month "/" day "[^/0-9]")
764 (month "/" day "/" year "[^0-9]")
765 (monthname " *" day "[^,0-9]")
766 (monthname " *" day ", *" year "[^0-9]")
767 (dayname "\\W"))
768 "List of pseudo-patterns describing the American style of dates.
769 The defaults are: MONTH/DAY; MONTH/DAY/YEAR; MONTHNAME DAY;
770 MONTHNAME DAY, YEAR; DAYNAME. Normally you should not customize this,
771 but `diary-date-forms' (which see)."
772 :type '(repeat (choice (cons :tag "Backup"
773 :value (backup . nil)
774 (const backup)
775 (repeat (list :inline t :format "%v"
776 (symbol :tag "Keyword")
777 (choice symbol regexp))))
778 (repeat (list :inline t :format "%v"
779 (symbol :tag "Keyword")
780 (choice symbol regexp)))))
781 :group 'diary)
782
783 (define-obsolete-variable-alias 'european-date-diary-pattern
784 'diary-european-date-forms "23.1")
785
786 (defcustom diary-european-date-forms
787 '((day "/" month "[^/0-9]")
788 (day "/" month "/" year "[^0-9]")
789 (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|\\([0-9]+[:aApP]\\)\\)")
790 (day " *" monthname " *" year "[^0-9]")
791 (dayname "\\W"))
792 "List of pseudo-patterns describing the European style of dates.
793 The defaults are: DAY/MONTH; DAY/MONTH/YEAR; DAY MONTHNAME;
794 DAY MONTHNAME YEAR; DAYNAME. Normally you should not customize this, but
795 `diary-date-forms' (which see)."
796 :type '(repeat (choice (cons :tag "Backup"
797 :value (backup . nil)
798 (const backup)
799 (repeat (list :inline t :format "%v"
800 (symbol :tag "Keyword")
801 (choice symbol regexp))))
802 (repeat (list :inline t :format "%v"
803 (symbol :tag "Keyword")
804 (choice symbol regexp)))))
805 :group 'diary)
806
807 (defvar diary-font-lock-keywords)
808
809 (defcustom diary-date-forms (cond ((eq calendar-date-style 'iso)
810 diary-iso-date-forms)
811 ((eq calendar-date-style 'european)
812 diary-european-date-forms)
813 (t diary-american-date-forms))
814 "List of pseudo-patterns describing the forms of date used in the diary.
815 The patterns on the list must be MUTUALLY EXCLUSIVE and should not match
816 any portion of the diary entry itself, just the date component.
817
818 A pseudo-pattern is a list of regular expressions and the keywords `month',
819 `day', `year', `monthname', and `dayname'. The keyword `monthname' will
820 match the name of the month (see `calendar-month-name-array'), capitalized
821 or not, or its user-specified abbreviation (see `calendar-month-abbrev-array'),
822 followed by a period or not; it will also match `*'. Similarly, `dayname'
823 will match the name of the day (see `calendar-day-name-array'), capitalized or
824 not, or its user-specified abbreviation (see `calendar-day-abbrev-array'),
825 followed by a period or not. The keywords `month', `day', and `year' will
826 match those numerical values, preceded by arbitrarily many zeros; they will
827 also match `*'.
828
829 The matching of the diary entries with the date forms is done with the
830 standard syntax table from Fundamental mode, but with the `*' changed so
831 that it is a word constituent.
832
833 If, to be mutually exclusive, a pseudo-pattern must match a portion of the
834 diary entry itself, the first element of the pattern MUST be `backup'. This
835 directive causes the date recognizer to back up to the beginning of the
836 current word of the diary entry, so in no case can the pattern match more than
837 a portion of the first word of the diary entry.
838
839 For examples of three common styles, see `diary-american-date-forms',
840 `diary-european-date-forms', and `diary-iso-date-forms'."
841 :type '(repeat (choice (cons :tag "Backup"
842 :value (backup . nil)
843 (const backup)
844 (repeat (list :inline t :format "%v"
845 (symbol :tag "Keyword")
846 (choice symbol regexp))))
847 (repeat (list :inline t :format "%v"
848 (symbol :tag "Keyword")
849 (choice symbol regexp)))))
850 :set-after '(calendar-date-style diary-iso-date-forms
851 diary-european-date-forms
852 diary-american-date-forms)
853 :initialize 'custom-initialize-default
854 :set (lambda (symbol value)
855 (unless (equal value (eval symbol))
856 (custom-set-default symbol value)
857 (setq diary-font-lock-keywords (diary-font-lock-keywords))
858 ;; Need to redraw not just to get new font-locking, but also
859 ;; to pick up any newly recognized entries.
860 (and (diary-live-p)
861 (diary))))
862 :group 'diary)
863
864 ;; Next three are provided to aid in setting calendar-date-display-form.
865 (defcustom calendar-iso-date-display-form '((format "%s-%.2d-%.2d" year
866 (string-to-number month)
867 (string-to-number day)))
868 "Pseudo-pattern governing the way a date appears in the ISO style.
869 Normally you should not customize this, but `calendar-date-display-form'
870 \(which see)."
871 :type 'sexp
872 :version "23.1"
873 :group 'calendar)
874
875 (define-obsolete-variable-alias 'european-calendar-display-form
876 'calendar-european-date-display-form "23.1")
877
878 (defcustom calendar-european-date-display-form
879 '((if dayname (concat dayname ", ")) day " " monthname " " year)
880 "Pseudo-pattern governing the way a date appears in the European style.
881 Normally you should not customize this, but `calendar-date-display-form'
882 \(which see)."
883 :type 'sexp
884 :group 'calendar)
885
886 (define-obsolete-variable-alias 'american-calendar-display-form
887 'calendar-american-date-display-form "23.1")
888
889 (defcustom calendar-american-date-display-form
890 '((if dayname (concat dayname ", ")) monthname " " day ", " year)
891 "Pseudo-pattern governing the way a date appears in the American style.
892 Normally you should not customize this, but `calendar-date-display-form'
893 \(which see)."
894 :type 'sexp
895 :group 'calendar)
896
897 (defcustom calendar-date-display-form
898 (cond ((eq calendar-date-style 'iso)
899 calendar-iso-date-display-form)
900 ((eq calendar-date-style 'european)
901 calendar-european-date-display-form)
902 (t calendar-american-date-display-form))
903 "Pseudo-pattern governing the way a calendar date appears.
904 Used by the function `calendar-date-string' (which see), a pseudo-pattern
905 is a list of expressions that can involve the keywords `month', `day',
906 and `year' (all numbers in string form), and `monthname' and `dayname'
907 \(both alphabetic strings). For example, a typical American form would be
908
909 '(month \"/\" day \"/\" (substring year -2))
910
911 whereas
912
913 '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
914
915 would give the usual American style in fixed-length fields. The variables
916 `calendar-iso-date-display-form', `calendar-european-date-display-form', and
917 `calendar-american-date-display-form' provide some defaults for three common
918 styles."
919 :type 'sexp
920 :set-after '(calendar-date-style calendar-iso-date-display-form
921 calendar-european-date-display-form
922 calendar-american-date-display-form)
923 :group 'calendar)
924
925 (defun calendar-set-date-style (style)
926 "Set the style of calendar and diary dates to STYLE (a symbol).
927 The valid styles are described in the documentation of `calendar-date-style'."
928 (interactive (list (intern
929 (completing-read "Date style: "
930 '("american" "european" "iso") nil t
931 nil nil "american"))))
932 (or (memq style '(american european iso))
933 (setq style 'american))
934 (setq calendar-date-style style
935 calendar-date-display-form
936 (symbol-value (intern-soft
937 (format "calendar-%s-date-display-form" style)))
938 diary-date-forms
939 (symbol-value (intern-soft (format "diary-%s-date-forms" style))))
940 (calendar-update-mode-line))
941
942 (defun european-calendar ()
943 "Set the interpretation and display of dates to the European style."
944 (interactive)
945 (calendar-set-date-style 'european))
946
947 (make-obsolete 'european-calendar 'calendar-set-date-style "23.1")
948
949 (defun american-calendar ()
950 "Set the interpretation and display of dates to the American style."
951 (interactive)
952 (calendar-set-date-style 'american))
953
954 (make-obsolete 'american-calendar 'calendar-set-date-style "23.1")
955
956 (define-obsolete-variable-alias 'holidays-in-diary-buffer
957 'diary-show-holidays-flag "23.1")
958
959 (defcustom diary-show-holidays-flag t
960 "Non-nil means include holidays in the diary display.
961 The holidays appear in the mode line of the diary buffer, or in the
962 fancy diary buffer next to the date. This slows down the diary functions
963 somewhat; setting it to nil makes the diary display faster."
964 :type 'boolean
965 :group 'holidays)
966
967 (defcustom calendar-debug-sexp nil
968 "Turn debugging on when evaluating a sexp in the diary or holiday list."
969 :type 'boolean
970 :group 'calendar)
971
972 (define-obsolete-variable-alias 'all-hebrew-calendar-holidays
973 'calendar-hebrew-all-holidays-flag "23.1")
974
975 (defcustom calendar-hebrew-all-holidays-flag nil
976 "If nil, show only major holidays from the Hebrew calendar.
977 This means only those Jewish holidays that appear on secular calendars.
978 Otherwise, show all the holidays that would appear in a complete Hebrew
979 calendar."
980 :type 'boolean
981 :group 'holidays)
982
983 (define-obsolete-variable-alias 'all-christian-calendar-holidays
984 'calendar-christian-all-holidays-flag "23.1")
985
986 (defcustom calendar-christian-all-holidays-flag nil
987 "If nil, show only major holidays from the Christian calendar.
988 This means only those Christian holidays that appear on secular calendars.
989 Otherwise, show all the holidays that would appear in a complete Christian
990 calendar."
991 :type 'boolean
992 :group 'holidays)
993
994 (define-obsolete-variable-alias 'all-islamic-calendar-holidays
995 'calendar-islamic-all-holidays-flag "23.1")
996
997 (defcustom calendar-islamic-all-holidays-flag nil
998 "If nil, show only major holidays from the Islamic calendar.
999 This means only those Islamic holidays that appear on secular calendars.
1000 Otherwise, show all the holidays that would appear in a complete Islamic
1001 calendar."
1002 :type 'boolean
1003 :group 'holidays)
1004
1005 (define-obsolete-variable-alias 'all-bahai-calendar-holidays
1006 'calendar-bahai-all-holidays-flag "23.1")
1007
1008 (defcustom calendar-bahai-all-holidays-flag nil
1009 "If nil, show only major holidays from the Baha'i calendar.
1010 These are the days on which work and school must be suspended.
1011 Otherwise, show all the holidays that would appear in a complete Baha'i
1012 calendar."
1013 :type 'boolean
1014 :group 'holidays)
1015
1016 (defcustom calendar-chinese-all-holidays-flag nil
1017 "If nil, show only the major holidays from the Chinese calendar."
1018 :version "23.1"
1019 :type 'boolean
1020 :group 'holidays)
1021
1022 ;;; End of user options.
1023
1024 (calendar-recompute-layout-variables)
1025
1026 (defconst calendar-first-date-row 3
1027 "First row in the calendar with actual dates.")
1028
1029 (defconst calendar-buffer "*Calendar*"
1030 "Name of the buffer used for the calendar.")
1031
1032 (defconst holiday-buffer "*Holidays*"
1033 "Name of the buffer used for the displaying the holidays.")
1034
1035 (defconst diary-fancy-buffer "*Fancy Diary Entries*"
1036 "Name of the buffer used for the optional fancy display of the diary.")
1037
1038 (define-obsolete-variable-alias 'fancy-diary-buffer 'diary-fancy-buffer "23.1")
1039
1040 (defconst calendar-other-calendars-buffer "*Other Calendars*"
1041 "Name of the buffer used for the display of date on other calendars.")
1042
1043 (defconst lunar-phases-buffer "*Phases of Moon*"
1044 "Name of the buffer used for the lunar phases.")
1045
1046 (defconst solar-sunrises-buffer "*Sunrise/Sunset Times*"
1047 "Name of buffer used for sunrise/sunset times.")
1048
1049 (defconst calendar-hebrew-yahrzeit-buffer "*Yahrzeits*"
1050 "Name of the buffer used by `list-yahrzeit-dates'.")
1051
1052 (defmacro calendar-increment-month (mon yr n &optional nmonths)
1053 "Increment the variables MON and YR by N months.
1054 Forward if N is positive or backward if N is negative.
1055 A negative YR is interpreted as BC; -1 being 1 BC, and so on.
1056 Optional NMONTHS is the number of months per year (default 12)."
1057 ;; Can view this as a form of base-nmonths arithmetic, in which "a
1058 ;; year" = "ten", and we never bother to use hundreds.
1059 `(let ((nmonths (or ,nmonths 12))
1060 macro-y)
1061 (if (< ,yr 0) (setq ,yr (1+ ,yr))) ; -1 BC -> 0 AD, etc
1062 (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1063 ,mon (1+ (mod macro-y nmonths))
1064 ,yr (/ macro-y nmonths))
1065 ;; Alternative:
1066 ;;; (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1067 ;;; ,yr (/ macro-y nmonths)
1068 ;;; ,mon (- macro-y (* ,yr nmonths)))
1069 (and (< macro-y 0) (> ,mon 1) (setq ,yr (1- ,yr)))
1070 (if (< ,yr 1) (setq ,yr (1- ,yr))))) ; 0 AD -> -1 BC, etc
1071
1072 (define-obsolete-function-alias 'increment-calendar-month
1073 'calendar-increment-month "23.1")
1074
1075 (defvar displayed-month)
1076 (defvar displayed-year)
1077
1078 (defun calendar-increment-month-cons (n &optional mon yr)
1079 "Return the Nth month after MON/YR.
1080 The return value is a pair (MONTH . YEAR).
1081 MON defaults to `displayed-month'. YR defaults to `displayed-year'."
1082 (unless mon (setq mon displayed-month))
1083 (unless yr (setq yr displayed-year))
1084 (calendar-increment-month mon yr n)
1085 (cons mon yr))
1086
1087 (defmacro calendar-for-loop (var from init to final do &rest body)
1088 "Execute a for loop.
1089 Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
1090 inclusive. The standard macro `dotimes' is preferable in most cases."
1091 (declare (debug (symbolp "from" form "to" form "do" body))
1092 (indent defun))
1093 `(let ((,var (1- ,init)))
1094 (while (>= ,final (setq ,var (1+ ,var)))
1095 ,@body)))
1096
1097 (make-obsolete 'calendar-for-loop "use `dotimes' or `while' instead." "23.1")
1098
1099 (defmacro calendar-sum (index initial condition expression)
1100 "For INDEX = INITIAL, +1, ... (as long as CONDITION holds), sum EXPRESSION."
1101 (declare (debug (symbolp form form form)))
1102 `(let ((,index ,initial)
1103 (sum 0))
1104 (while ,condition
1105 (setq sum (+ sum ,expression)
1106 ,index (1+ ,index)))
1107 sum))
1108
1109 ;; FIXME bind q to bury-buffer?
1110 (defmacro calendar-in-read-only-buffer (buffer &rest body)
1111 "Switch to BUFFER and executes the forms in BODY.
1112 First creates or erases BUFFER as needed. Leaves BUFFER read-only,
1113 with disabled undo. Leaves point at point-min, displays BUFFER."
1114 (declare (indent 1) (debug t))
1115 `(progn
1116 (set-buffer (get-buffer-create ,buffer))
1117 (setq buffer-read-only nil
1118 buffer-undo-list t)
1119 (erase-buffer)
1120 ,@body
1121 (goto-char (point-min))
1122 (set-buffer-modified-p nil)
1123 (setq buffer-read-only t)
1124 (display-buffer ,buffer)))
1125
1126 ;; The following are in-line for speed; they can be called thousands of times
1127 ;; when looking up holidays or processing the diary. Here, for example, are
1128 ;; the numbers of calls to calendar/diary/holiday functions in preparing the
1129 ;; fancy diary display, for a moderately complex diary file, with functions
1130 ;; used instead of macros. There were a total of 10000 such calls:
1131 ;;
1132 ;; 1934 calendar-extract-month
1133 ;; 1852 calendar-extract-year
1134 ;; 1819 calendar-extract-day
1135 ;; 845 calendar-leap-year-p
1136 ;; 837 calendar-day-number
1137 ;; 775 calendar-absolute-from-gregorian
1138 ;; 346 calendar-last-day-of-month
1139 ;; 286 calendar-hebrew-last-day-of-month
1140 ;; 188 calendar-hebrew-leap-year-p
1141 ;; 180 calendar-hebrew-elapsed-days
1142 ;; 163 calendar-hebrew-last-month-of-year
1143 ;; 66 calendar-date-compare
1144 ;; 65 calendar-hebrew-days-in-year
1145 ;; 60 calendar-julian-to-absolute
1146 ;; 50 calendar-hebrew-to-absolute
1147 ;; 43 calendar-date-equal
1148 ;; 38 calendar-gregorian-from-absolute
1149 ;; .
1150 ;;
1151 ;; The use of these seven macros eliminates the overhead of 92% of the function
1152 ;; calls; it's faster this way.
1153
1154 (defsubst calendar-extract-month (date)
1155 "Extract the month part of DATE which has the form (month day year)."
1156 (car date))
1157
1158 (define-obsolete-function-alias 'extract-calendar-month
1159 'calendar-extract-month "23.1")
1160
1161 ;; Note gives wrong answer for result of (calendar-read-date 'noday),
1162 ;; but that is only used by `calendar-other-month'.
1163 (defsubst calendar-extract-day (date)
1164 "Extract the day part of DATE which has the form (month day year)."
1165 (cadr date))
1166
1167 (define-obsolete-function-alias 'extract-calendar-day
1168 'calendar-extract-day "23.1")
1169
1170 (defsubst calendar-extract-year (date)
1171 "Extract the year part of DATE which has the form (month day year)."
1172 (nth 2 date))
1173
1174 (define-obsolete-function-alias 'extract-calendar-year
1175 'calendar-extract-year "23.1")
1176
1177 (defsubst calendar-leap-year-p (year)
1178 "Return t if YEAR is a Gregorian leap year.
1179 A negative year is interpreted as BC; -1 being 1 BC, and so on."
1180 ;; 1 BC = 0 AD, 2 BC acts like 1 AD, etc.
1181 (if (< year 0) (setq year (1- (abs year))))
1182 (and (zerop (% year 4))
1183 (or (not (zerop (% year 100)))
1184 (zerop (% year 400)))))
1185
1186 ;; The foregoing is a bit faster, but not as clear as the following:
1187 ;;
1188 ;;(defsubst calendar-leap-year-p (year)
1189 ;; "Return t if YEAR is a Gregorian leap year."
1190 ;; (or
1191 ;; (and (zerop (% year 4))
1192 ;; (not (zerop (% year 100))))
1193 ;; (zerop (% year 400)))
1194
1195 (defsubst calendar-last-day-of-month (month year)
1196 "The last day in MONTH during YEAR."
1197 (if (and (= month 2) (calendar-leap-year-p year))
1198 29
1199 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
1200
1201 ;; An explanation of the calculation can be found in PascAlgorithms by
1202 ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
1203
1204 (defsubst calendar-day-number (date)
1205 "Return the day number within the year of the date DATE.
1206 For example, (calendar-day-number '(1 1 1987)) returns the value 1,
1207 while (calendar-day-number '(12 31 1980)) returns 366."
1208 (let* ((month (calendar-extract-month date))
1209 (day (calendar-extract-day date))
1210 (year (calendar-extract-year date))
1211 (day-of-year (+ day (* 31 (1- month)))))
1212 (when (> month 2)
1213 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
1214 (if (calendar-leap-year-p year)
1215 (setq day-of-year (1+ day-of-year))))
1216 day-of-year))
1217
1218 (defsubst calendar-absolute-from-gregorian (date)
1219 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
1220 The Gregorian date Sunday, December 31, 1 BC is imaginary.
1221 DATE is a list of the form (month day year). A negative year is
1222 interpreted as BC; -1 being 1 BC, and so on. Dates before 12/31/1 BC
1223 return negative results."
1224 (let ((year (calendar-extract-year date))
1225 offset-years)
1226 (cond ((zerop year)
1227 (error "There was no year zero"))
1228 ((> year 0)
1229 (setq offset-years (1- year))
1230 (+ (calendar-day-number date) ; days this year
1231 (* 365 offset-years) ; + days in prior years
1232 (/ offset-years 4) ; + Julian leap years
1233 (- (/ offset-years 100)) ; - century years
1234 (/ offset-years 400))) ; + Gregorian leap years
1235 (t
1236 ;; Years between date and 1 BC, excluding 1 BC (1 for 2 BC, etc).
1237 (setq offset-years (abs (1+ year)))
1238 (- (calendar-day-number date)
1239 (* 365 offset-years)
1240 (/ offset-years 4)
1241 (- (/ offset-years 100))
1242 (/ offset-years 400)
1243 (calendar-day-number '(12 31 -1))))))) ; days in year 1 BC
1244
1245 ;;;###autoload
1246 (defun calendar (&optional arg)
1247 "Display a three-month Gregorian calendar.
1248 The three months appear side by side, with the current month in
1249 the middle surrounded by the previous and next months. The
1250 cursor is put on today's date. If optional prefix argument ARG
1251 is non-nil, prompts for the central month and year.
1252
1253 Once in the calendar window, future or past months can be moved
1254 into view. Arbitrary months can be displayed, or the calendar
1255 can be scrolled forward or backward. The cursor can be moved
1256 forward or backward by one day, one week, one month, or one year.
1257 All of these commands take prefix arguments which, when negative,
1258 cause movement in the opposite direction. For convenience, the
1259 digit keys and the minus sign are automatically prefixes. Use
1260 \\[describe-mode] for details of the key bindings in the calendar
1261 window.
1262
1263 Displays the calendar in a separate window, or optionally in a
1264 separate frame, depending on the value of `calendar-setup'.
1265
1266 If `calendar-view-diary-initially-flag' is non-nil, also displays the
1267 diary entries for the current date (or however many days
1268 `diary-number-of-entries' specifies). This variable can be
1269 overridden by `calendar-setup'. As well as being displayed,
1270 diary entries can also be marked on the calendar (see
1271 `calendar-mark-diary-entries-flag').
1272
1273 Runs the following hooks:
1274
1275 `calendar-load-hook' - after loading calendar.el
1276 `calendar-today-visible-hook', `calendar-today-invisible-hook' - after
1277 generating a calendar, if today's date is visible or not, respectively
1278 `calendar-initial-window-hook' - after first creating a calendar
1279
1280 This function is suitable for execution in a .emacs file."
1281 (interactive "P")
1282 ;; Avoid loading cal-x unless it will be used.
1283 (if (and (memq calendar-setup '(one-frame two-frames calendar-only))
1284 (display-multi-frame-p))
1285 (calendar-frame-setup calendar-setup arg)
1286 (calendar-basic-setup arg)))
1287
1288 (defun calendar-basic-setup (&optional arg nodisplay)
1289 "Create a three-month calendar.
1290 If optional prefix argument ARG is non-nil, prompts for the month
1291 and year, else uses the current date. If NODISPLAY is non-nil, don't
1292 display the generated calendar."
1293 (interactive "P")
1294 (let ((buff (current-buffer)))
1295 (set-buffer (get-buffer-create calendar-buffer))
1296 (calendar-mode)
1297 (let* ((pop-up-windows t)
1298 ;; Not really needed now, but means we use exactly the same
1299 ;; behavior as before in the non-wide case (see below).
1300 (split-height-threshold 1000)
1301 (split-width-threshold calendar-split-width-threshold)
1302 (date (if arg (calendar-read-date t)
1303 (calendar-current-date)))
1304 (month (calendar-extract-month date))
1305 (year (calendar-extract-year date)))
1306 (calendar-increment-month month year (- calendar-offset))
1307 ;; Display the buffer before calling calendar-generate-window so that it
1308 ;; can get a chance to adjust the window sizes to the frame size.
1309 (unless nodisplay
1310 ;; We want a window configuration that looks something like
1311 ;; X X | Y
1312 ;; - -----
1313 ;; C Z | C
1314 ;; where C is the calendar, and the LHS is the traditional,
1315 ;; non-wide frame, and the RHS is the wide frame case.
1316 ;; We should end up in the same state regardless of whether the
1317 ;; windows were initially split or not.
1318 ;; Previously, we only thought about the non-wide case.
1319 ;; We could just set split-height-threshold to 1000, relying on
1320 ;; the fact that the window splitting treated a single window as
1321 ;; a special case and would always split it (vertically). The
1322 ;; same thing does not work in the wide-frame case, so now we do
1323 ;; the splitting by hand.
1324 ;; See discussion in bug#1806.
1325 ;; Actually, this still does not do quite the right thing in the
1326 ;; wide frame case if started from a configuration like the LHS.
1327 ;; Eg if you start with a non-wide frame, call calendar, then
1328 ;; make the frame wider. This one is problematic because you
1329 ;; might need to split a totally unrelated window. Oh well, it
1330 ;; seems unlikely, and perhaps respecting the original layout is
1331 ;; the right thing in that case.
1332 ;;
1333 ;; Is this a wide frame? If so, split it horizontally.
1334 (if (window-splittable-p t) (split-window-horizontally))
1335 (pop-to-buffer calendar-buffer)
1336 ;; Has the window already been split vertically?
1337 (when (and (not (window-dedicated-p))
1338 (window-full-height-p))
1339 (let ((win (split-window-vertically)))
1340 ;; In the upper window, show whatever was visible before.
1341 ;; This looks better than using other-buffer.
1342 (switch-to-buffer buff)
1343 ;; Switch to the lower window with the calendar buffer.
1344 (select-window win))))
1345 (calendar-generate-window month year)
1346 (if (and calendar-view-diary-initially-flag
1347 (calendar-date-is-visible-p date))
1348 (diary-view-entries))))
1349 (if calendar-view-holidays-initially-flag
1350 (let* ((diary-buffer (get-file-buffer diary-file))
1351 (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
1352 (split-height-threshold (if diary-window 2 1000)))
1353 ;; FIXME display buffer?
1354 (calendar-list-holidays)))
1355 (run-hooks 'calendar-initial-window-hook))
1356
1357 (defun calendar-generate-window (&optional mon yr)
1358 "Generate the calendar window for the current date.
1359 Optional integers MON and YR are used instead of today's date."
1360 (let* ((inhibit-read-only t)
1361 (today (calendar-current-date))
1362 (month (calendar-extract-month today))
1363 (day (calendar-extract-day today))
1364 (year (calendar-extract-year today))
1365 (today-visible (or (not mon)
1366 (<= (abs (calendar-interval mon yr month year)) 1)))
1367 (day-in-week (calendar-day-of-week today))
1368 (in-calendar-window (eq (window-buffer (selected-window))
1369 (get-buffer calendar-buffer))))
1370 (calendar-generate (or mon month) (or yr year))
1371 (calendar-update-mode-line)
1372 (calendar-cursor-to-visible-date
1373 (if today-visible today (list displayed-month 1 displayed-year)))
1374 (set-buffer-modified-p nil)
1375 ;; Don't do any window-related stuff if we weren't called from a
1376 ;; window displaying the calendar.
1377 (when in-calendar-window
1378 ;; The second test used to be window-full-width-p.
1379 ;; Not sure what it was/is for, except perhaps some way of saying
1380 ;; "try not to mess with existing configurations".
1381 ;; If did the wrong thing on wide frames, where we have done a
1382 ;; horizontal split in calendar-basic-setup.
1383 (if (or (one-window-p t) (not (window-safely-shrinkable-p)))
1384 ;; Don't mess with the window size, but ensure that the first
1385 ;; line is fully visible.
1386 (set-window-vscroll nil 0)
1387 ;; Adjust the window to exactly fit the displayed calendar.
1388 (fit-window-to-buffer nil nil calendar-minimum-window-height))
1389 (sit-for 0))
1390 (and (bound-and-true-p font-lock-mode)
1391 (font-lock-fontify-buffer))
1392 (and calendar-mark-holidays-flag
1393 ;;; (calendar-date-is-valid-p today) ; useful for BC dates
1394 (calendar-mark-holidays)
1395 (and in-calendar-window (sit-for 0)))
1396 (unwind-protect
1397 (if calendar-mark-diary-entries-flag (diary-mark-entries))
1398 (if today-visible
1399 (run-hooks 'calendar-today-visible-hook)
1400 (run-hooks 'calendar-today-invisible-hook)))))
1401
1402 (defun calendar-generate (month year)
1403 "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
1404 ;; A negative YEAR is interpreted as BC; -1 being 1 BC, and so on.
1405 ;; Note that while calendars for years BC could be displayed as it
1406 ;; stands, almost all other calendar functions (eg holidays) would
1407 ;; at best have unpredictable results for such dates.
1408 (if (< (+ month (* 12 (1- year))) 2)
1409 (error "Months before January, 1 AD cannot be displayed"))
1410 (setq displayed-month month
1411 displayed-year year)
1412 (erase-buffer)
1413 (calendar-increment-month month year -1)
1414 (dotimes (i 3)
1415 (calendar-generate-month month year
1416 (+ calendar-left-margin
1417 (* calendar-month-width i)))
1418 (calendar-increment-month month year 1)))
1419
1420 (defun calendar-move-to-column (indent)
1421 "Like `move-to-column', but indents if the line is too short."
1422 (if (< (move-to-column indent) indent)
1423 (indent-to indent)))
1424
1425 (defun calendar-ensure-newline ()
1426 "Move to the next line, adding a newline if necessary."
1427 (or (zerop (forward-line 1))
1428 (insert "\n")))
1429
1430 (defun calendar-insert-at-column (indent string truncate)
1431 "Move to column INDENT, adding spaces as needed.
1432 Inserts STRING so that it ends at INDENT. STRING is either a
1433 literal string, or a sexp to evaluate to return such. Truncates
1434 STRING to length TRUNCATE, ensure a trailing space."
1435 (if (not (ignore-errors (stringp (setq string (eval string)))))
1436 (calendar-move-to-column indent)
1437 (if (> (length string) truncate)
1438 (setq string (substring string 0 truncate)))
1439 (or (string-match " $" string)
1440 (if (= (length string) truncate)
1441 (aset string (1- truncate) ?\s)
1442 (setq string (concat string " "))))
1443 (calendar-move-to-column (- indent (length string)))
1444 (insert string)))
1445
1446 (defun calendar-generate-month (month year indent)
1447 "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
1448 The calendar is inserted at the top of the buffer in which point is currently
1449 located, but indented INDENT spaces. The indentation is done from the first
1450 character on the line and does not disturb the first INDENT characters on the
1451 line."
1452 (let ((blank-days ; at start of month
1453 (mod
1454 (- (calendar-day-of-week (list month 1 year))
1455 calendar-week-start-day)
1456 7))
1457 (last (calendar-last-day-of-month month year))
1458 (trunc (min calendar-intermonth-spacing
1459 (1- calendar-left-margin)))
1460 (day 1)
1461 string)
1462 (goto-char (point-min))
1463 (calendar-move-to-column indent)
1464 (insert
1465 (calendar-string-spread
1466 (list (format "%s %d" (calendar-month-name month) year))
1467 ?\s calendar-month-digit-width))
1468 (calendar-ensure-newline)
1469 (calendar-insert-at-column indent calendar-intermonth-header trunc)
1470 ;; Use the first two characters of each day to head the columns.
1471 (dotimes (i 7)
1472 (insert
1473 (progn
1474 (setq string
1475 (calendar-day-name (mod (+ calendar-week-start-day i) 7) nil t))
1476 (if enable-multibyte-characters
1477 (truncate-string-to-width string calendar-day-header-width)
1478 (substring string 0 calendar-day-header-width)))
1479 (make-string (- calendar-column-width calendar-day-header-width) ?\s)))
1480 (calendar-ensure-newline)
1481 (calendar-insert-at-column indent calendar-intermonth-text trunc)
1482 ;; Add blank days before the first of the month.
1483 (insert (make-string (* blank-days calendar-column-width) ?\s))
1484 ;; Put in the days of the month.
1485 (dotimes (i last)
1486 (setq day (1+ i))
1487 ;; TODO should numbers be left-justified, centered...?
1488 (insert (format (format "%%%dd%%s" calendar-day-digit-width) day
1489 (make-string
1490 (- calendar-column-width calendar-day-digit-width) ?\s)))
1491 ;; 'date property prevents intermonth text confusing re-searches.
1492 ;; (Tried intangible, it did not really work.)
1493 (set-text-properties
1494 (- (point) (1+ calendar-day-digit-width)) (1- (point))
1495 `(mouse-face highlight help-echo ,(eval calendar-date-echo-text)
1496 date t))
1497 (when (and (zerop (mod (+ day blank-days) 7))
1498 (/= day last))
1499 (calendar-ensure-newline)
1500 (setq day (1+ day)) ; first day of next week
1501 (calendar-insert-at-column indent calendar-intermonth-text trunc)))))
1502
1503 (defun calendar-redraw ()
1504 "Redraw the calendar display, if `calendar-buffer' is live."
1505 (interactive)
1506 (if (get-buffer calendar-buffer)
1507 (with-current-buffer calendar-buffer
1508 (let ((cursor-date (calendar-cursor-to-nearest-date)))
1509 (calendar-generate-window displayed-month displayed-year)
1510 (calendar-cursor-to-visible-date cursor-date)))))
1511
1512 (defvar calendar-mode-map
1513 (let ((map (make-keymap)))
1514 (suppress-keymap map)
1515 (dolist (c '(narrow-to-region mark-word mark-sexp mark-paragraph
1516 mark-defun mark-whole-buffer mark-page
1517 downcase-region upcase-region kill-region
1518 copy-region-as-kill capitalize-region write-region))
1519 (define-key map (vector 'remap c) 'calendar-not-implemented))
1520 (define-key map "<" 'calendar-scroll-right)
1521 (define-key map "\C-x<" 'calendar-scroll-right)
1522 (define-key map [prior] 'calendar-scroll-right-three-months)
1523 (define-key map "\ev" 'calendar-scroll-right-three-months)
1524 (define-key map ">" 'calendar-scroll-left)
1525 (define-key map "\C-x>" 'calendar-scroll-left)
1526 (define-key map [next] 'calendar-scroll-left-three-months)
1527 (define-key map "\C-v" 'calendar-scroll-left-three-months)
1528 (define-key map "\C-b" 'calendar-backward-day)
1529 (define-key map "\C-p" 'calendar-backward-week)
1530 (define-key map "\e{" 'calendar-backward-month)
1531 (define-key map "\C-x[" 'calendar-backward-year)
1532 (define-key map "\C-f" 'calendar-forward-day)
1533 (define-key map "\C-n" 'calendar-forward-week)
1534 (define-key map [left] 'calendar-backward-day)
1535 (define-key map [up] 'calendar-backward-week)
1536 (define-key map [right] 'calendar-forward-day)
1537 (define-key map [down] 'calendar-forward-week)
1538 (define-key map "\e}" 'calendar-forward-month)
1539 (define-key map "\C-x]" 'calendar-forward-year)
1540 (define-key map "\C-a" 'calendar-beginning-of-week)
1541 (define-key map "\C-e" 'calendar-end-of-week)
1542 (define-key map "\ea" 'calendar-beginning-of-month)
1543 (define-key map "\ee" 'calendar-end-of-month)
1544 (define-key map "\e<" 'calendar-beginning-of-year)
1545 (define-key map "\e>" 'calendar-end-of-year)
1546 (define-key map "\C-@" 'calendar-set-mark)
1547 ;; Many people are used to typing C-SPC and getting C-@.
1548 (define-key map [?\C-\s] 'calendar-set-mark)
1549 (define-key map "\C-x\C-x" 'calendar-exchange-point-and-mark)
1550 (define-key map "\e=" 'calendar-count-days-region)
1551 (define-key map "gd" 'calendar-goto-date)
1552 (define-key map "gD" 'calendar-goto-day-of-year)
1553 (define-key map "gj" 'calendar-julian-goto-date)
1554 (define-key map "ga" 'calendar-astro-goto-day-number)
1555 (define-key map "gh" 'calendar-hebrew-goto-date)
1556 (define-key map "gi" 'calendar-islamic-goto-date)
1557 (define-key map "gb" 'calendar-bahai-goto-date)
1558 (define-key map "gC" 'calendar-chinese-goto-date)
1559 (define-key map "gk" 'calendar-coptic-goto-date)
1560 (define-key map "ge" 'calendar-ethiopic-goto-date)
1561 (define-key map "gp" 'calendar-persian-goto-date)
1562 (define-key map "gc" 'calendar-iso-goto-date)
1563 (define-key map "gw" 'calendar-iso-goto-week)
1564 (define-key map "gf" 'calendar-french-goto-date)
1565 (define-key map "gml" 'calendar-mayan-goto-long-count-date)
1566 (define-key map "gmpc" 'calendar-mayan-previous-round-date)
1567 (define-key map "gmnc" 'calendar-mayan-next-round-date)
1568 (define-key map "gmph" 'calendar-mayan-previous-haab-date)
1569 (define-key map "gmnh" 'calendar-mayan-next-haab-date)
1570 (define-key map "gmpt" 'calendar-mayan-previous-tzolkin-date)
1571 (define-key map "gmnt" 'calendar-mayan-next-tzolkin-date)
1572 (define-key map "Aa" 'appt-add)
1573 (define-key map "Ad" 'appt-delete)
1574 (define-key map "S" 'calendar-sunrise-sunset)
1575 (define-key map "M" 'calendar-lunar-phases)
1576 (define-key map " " 'scroll-other-window)
1577 (define-key map "\d" 'scroll-other-window-down)
1578 (define-key map "\C-c\C-l" 'calendar-redraw)
1579 (define-key map "." 'calendar-goto-today)
1580 (define-key map "o" 'calendar-other-month)
1581 (define-key map "q" 'calendar-exit)
1582 (define-key map "a" 'calendar-list-holidays)
1583 (define-key map "h" 'calendar-cursor-holidays)
1584 (define-key map "x" 'calendar-mark-holidays)
1585 (define-key map "u" 'calendar-unmark)
1586 (define-key map "m" 'diary-mark-entries)
1587 (define-key map "d" 'diary-view-entries)
1588 (define-key map "D" 'diary-view-other-diary-entries)
1589 (define-key map "s" 'diary-show-all-entries)
1590 (define-key map "pd" 'calendar-print-day-of-year)
1591 (define-key map "pC" 'calendar-chinese-print-date)
1592 (define-key map "pk" 'calendar-coptic-print-date)
1593 (define-key map "pe" 'calendar-ethiopic-print-date)
1594 (define-key map "pp" 'calendar-persian-print-date)
1595 (define-key map "pc" 'calendar-iso-print-date)
1596 (define-key map "pj" 'calendar-julian-print-date)
1597 (define-key map "pa" 'calendar-astro-print-day-number)
1598 (define-key map "ph" 'calendar-hebrew-print-date)
1599 (define-key map "pi" 'calendar-islamic-print-date)
1600 (define-key map "pb" 'calendar-bahai-print-date)
1601 (define-key map "pf" 'calendar-french-print-date)
1602 (define-key map "pm" 'calendar-mayan-print-date)
1603 (define-key map "po" 'calendar-print-other-dates)
1604 (define-key map "id" 'diary-insert-entry)
1605 (define-key map "iw" 'diary-insert-weekly-entry)
1606 (define-key map "im" 'diary-insert-monthly-entry)
1607 (define-key map "iy" 'diary-insert-yearly-entry)
1608 (define-key map "ia" 'diary-insert-anniversary-entry)
1609 (define-key map "ib" 'diary-insert-block-entry)
1610 (define-key map "ic" 'diary-insert-cyclic-entry)
1611 (define-key map "ihd" 'diary-hebrew-insert-entry)
1612 (define-key map "ihm" 'diary-hebrew-insert-monthly-entry)
1613 (define-key map "ihy" 'diary-hebrew-insert-yearly-entry)
1614 (define-key map "iid" 'diary-islamic-insert-entry)
1615 (define-key map "iim" 'diary-islamic-insert-monthly-entry)
1616 (define-key map "iiy" 'diary-islamic-insert-yearly-entry)
1617 (define-key map "iBd" 'diary-bahai-insert-entry)
1618 (define-key map "iBm" 'diary-bahai-insert-monthly-entry)
1619 (define-key map "iBy" 'diary-bahai-insert-yearly-entry)
1620 (define-key map "?" 'calendar-goto-info-node)
1621 (define-key map "Hm" 'cal-html-cursor-month)
1622 (define-key map "Hy" 'cal-html-cursor-year)
1623 (define-key map "tm" 'cal-tex-cursor-month)
1624 (define-key map "tM" 'cal-tex-cursor-month-landscape)
1625 (define-key map "td" 'cal-tex-cursor-day)
1626 (define-key map "tw1" 'cal-tex-cursor-week)
1627 (define-key map "tw2" 'cal-tex-cursor-week2)
1628 (define-key map "tw3" 'cal-tex-cursor-week-iso)
1629 (define-key map "tw4" 'cal-tex-cursor-week-monday)
1630 (define-key map "tfd" 'cal-tex-cursor-filofax-daily)
1631 (define-key map "tfw" 'cal-tex-cursor-filofax-2week)
1632 (define-key map "tfW" 'cal-tex-cursor-filofax-week)
1633 (define-key map "tfy" 'cal-tex-cursor-filofax-year)
1634 (define-key map "ty" 'cal-tex-cursor-year)
1635 (define-key map "tY" 'cal-tex-cursor-year-landscape)
1636
1637 (define-key map [menu-bar edit] 'undefined)
1638 (define-key map [menu-bar search] 'undefined)
1639
1640 (easy-menu-define nil map nil cal-menu-sunmoon-menu)
1641 (easy-menu-define nil map nil cal-menu-diary-menu)
1642 (easy-menu-define nil map nil cal-menu-holidays-menu)
1643 (easy-menu-define nil map nil cal-menu-goto-menu)
1644 (easy-menu-define nil map nil cal-menu-scroll-menu)
1645
1646 ;; These are referenced in the default calendar-date-echo-text.
1647 (define-key map [down-mouse-3]
1648 (easy-menu-binding cal-menu-context-mouse-menu))
1649 (define-key map [down-mouse-2]
1650 (easy-menu-binding cal-menu-global-mouse-menu))
1651
1652 ;; Left-click moves us forward in time, right-click backwards.
1653 ;; cf scroll-bar.el.
1654 (define-key map [vertical-scroll-bar mouse-1] 'calendar-scroll-left)
1655 (define-key map [vertical-scroll-bar drag-mouse-1] 'calendar-scroll-left)
1656 ;; down-mouse-2 stays as scroll-bar-drag.
1657 (define-key map [vertical-scroll-bar mouse-3] 'calendar-scroll-right)
1658 (define-key map [vertical-scroll-bar drag-mouse-3] 'calendar-scroll-right)
1659
1660 map)
1661 "Keymap for `calendar-mode'.")
1662
1663 ;; Calendar mode is suitable only for specially formatted data.
1664 (put 'calendar-mode 'mode-class 'special)
1665
1666 (defun calendar-mode-line-entry (command echo &optional key string)
1667 "Return a propertized string for `calendar-mode-line-format'.
1668 COMMAND is a command to run, ECHO is the help-echo text, KEY
1669 is COMMAND's keybinding, STRING describes the binding."
1670 (propertize (or key
1671 (substitute-command-keys
1672 (format "\\<calendar-mode-map>\\[%s] %s" command string)))
1673 'help-echo (format "mouse-1: %s" echo)
1674 'mouse-face 'mode-line-highlight
1675 'keymap (make-mode-line-mouse-map 'mouse-1 command)))
1676
1677 ;; After calendar-mode-map.
1678 (defcustom calendar-mode-line-format
1679 (list
1680 (calendar-mode-line-entry 'calendar-scroll-right "previous month" "<")
1681 "Calendar"
1682 (concat
1683 (calendar-mode-line-entry 'calendar-goto-info-node "read Info on Calendar"
1684 nil "info")
1685 " / "
1686 (calendar-mode-line-entry 'calendar-other-month "choose another month"
1687 nil "other")
1688 " / "
1689 (calendar-mode-line-entry 'calendar-goto-today "go to today's date"
1690 nil "today"))
1691 '(calendar-date-string (calendar-current-date) t)
1692 (calendar-mode-line-entry 'calendar-scroll-left "next month" ">"))
1693 "The mode line of the calendar buffer.
1694 This is a list of items that evaluate to strings. The elements
1695 are evaluated and concatenated, evenly separated by blanks.
1696 During evaluation, the variable `date' is available as the date
1697 nearest the cursor (or today's date if that fails). To update
1698 the mode-line as the cursor moves, add `calendar-update-mode-line'
1699 to `calendar-move-hook'. Here is an example that has the Hebrew date,
1700 the day number/days remaining in the year, and the ISO week/year numbers:
1701
1702 (list
1703 \"\"
1704 '(calendar-hebrew-date-string date)
1705 '(let* ((year (calendar-extract-year date))
1706 (d (calendar-day-number date))
1707 (days-remaining
1708 (- (calendar-day-number (list 12 31 year)) d)))
1709 (format \"%d/%d\" d days-remaining))
1710 '(let* ((d (calendar-absolute-from-gregorian date))
1711 (iso-date (calendar-iso-from-absolute d)))
1712 (format \"ISO week %d of %d\"
1713 (calendar-extract-month iso-date)
1714 (calendar-extract-year iso-date)))
1715 \"\"))"
1716 :risky t
1717 :type 'sexp
1718 :group 'calendar)
1719
1720 (defun calendar-goto-info-node ()
1721 "Go to the info node for the calendar."
1722 (interactive)
1723 (info "(emacs)Calendar/Diary")
1724 (fit-window-to-buffer))
1725
1726 (defvar calendar-mark-ring nil
1727 "Used by `calendar-set-mark'.")
1728
1729 (define-derived-mode calendar-mode nil "Calendar"
1730 "A major mode for the calendar window.
1731 For a complete description, see the info node `Calendar/Diary'.
1732
1733 \\<calendar-mode-map>\\{calendar-mode-map}"
1734 (setq buffer-read-only t
1735 buffer-undo-list t
1736 indent-tabs-mode nil)
1737 (calendar-update-mode-line)
1738 (make-local-variable 'calendar-mark-ring)
1739 (make-local-variable 'displayed-month) ; month in middle of window
1740 (make-local-variable 'displayed-year) ; year in middle of window
1741 ;; Most functions only work if displayed-month and displayed-year are set,
1742 ;; so let's make sure they're always set. Most likely, this will be reset
1743 ;; soon in calendar-generate, but better safe than sorry.
1744 (unless (boundp 'displayed-month) (setq displayed-month 1))
1745 (unless (boundp 'displayed-year) (setq displayed-year 2001))
1746 (set (make-local-variable 'font-lock-defaults)
1747 '(calendar-font-lock-keywords t)))
1748
1749 (defun calendar-string-spread (strings char length)
1750 "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH.
1751 The effect is like mapconcat but the separating pieces are as balanced as
1752 possible. Each item of STRINGS is evaluated before concatenation so it can
1753 actually be an expression that evaluates to a string. If LENGTH is too short,
1754 the STRINGS are just concatenated and the result truncated."
1755 ;; The algorithm is based on equation (3.25) on page 85 of Concrete
1756 ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
1757 ;; Addison-Wesley, Reading, MA, 1989.
1758 (let* ((strings (mapcar 'eval
1759 (if (< (length strings) 2)
1760 (append (list "") strings (list ""))
1761 strings)))
1762 (n (- length (length (apply 'concat strings))))
1763 (m (1- (length strings)))
1764 (s (car strings))
1765 (strings (cdr strings))
1766 (i 0))
1767 (dolist (string strings)
1768 (setq s (concat s
1769 (make-string (max 0 (/ (+ n i) m)) char)
1770 string)
1771 i (1+ i)))
1772 (substring s 0 length)))
1773
1774 (defun calendar-update-mode-line ()
1775 "Update the calendar mode line with the current date and date style."
1776 (if (bufferp (get-buffer calendar-buffer))
1777 (with-current-buffer calendar-buffer
1778 (let ((start (- calendar-left-margin 2))
1779 (date (condition-case nil
1780 (calendar-cursor-to-nearest-date)
1781 (error (calendar-current-date)))))
1782 (setq mode-line-format
1783 (concat (make-string (max 0 (+ start
1784 (- (car (window-inside-edges))
1785 (car (window-edges))))) ?\s)
1786 (calendar-string-spread
1787 (mapcar 'eval calendar-mode-line-format)
1788 ?\s (- calendar-right-margin (1- start))))))
1789 (force-mode-line-update))))
1790
1791 (defun calendar-window-list ()
1792 "List of all calendar-related windows."
1793 (let ((calendar-buffers (calendar-buffer-list))
1794 list)
1795 ;; Using 0 rather than t for last argument - see bug#2199.
1796 ;; This is only used with calendar-hide-window, which ignores
1797 ;; iconified frames anyway, so could use 'visible rather than 0.
1798 (walk-windows (lambda (w)
1799 (if (memq (window-buffer w) calendar-buffers)
1800 (push w list)))
1801 nil 0)
1802 list))
1803
1804 (defun calendar-buffer-list ()
1805 "List of all calendar-related buffers (as buffers, not strings)."
1806 (let (buffs)
1807 (dolist (b (list calendar-hebrew-yahrzeit-buffer lunar-phases-buffer
1808 holiday-buffer diary-fancy-buffer solar-sunrises-buffer
1809 (get-file-buffer diary-file)
1810 calendar-buffer calendar-other-calendars-buffer))
1811 (and b (setq b (get-buffer b))
1812 (push b buffs)))
1813 buffs))
1814
1815 (defun calendar-exit ()
1816 "Get out of the calendar window and hide it and related buffers."
1817 (interactive)
1818 (let ((diary-buffer (get-file-buffer diary-file)))
1819 (if (or (not diary-buffer)
1820 (not (buffer-modified-p diary-buffer))
1821 (yes-or-no-p
1822 "Diary modified; do you really want to exit the calendar? "))
1823 ;; Need to do this multiple times because one time can replace some
1824 ;; calendar-related buffers with other calendar-related buffers.
1825 (mapc (lambda (x)
1826 (mapc 'calendar-hide-window (calendar-window-list)))
1827 (calendar-window-list)))))
1828
1829 (define-obsolete-function-alias 'exit-calendar 'calendar-exit "23.1")
1830
1831 (defun calendar-hide-window (window)
1832 "Hide WINDOW if it is calendar-related."
1833 (let ((buffer (if (window-live-p window) (window-buffer window))))
1834 (if (memq buffer (calendar-buffer-list))
1835 (cond
1836 ((and (display-multi-frame-p)
1837 (eq 'icon (cdr (assoc 'visibility
1838 (frame-parameters
1839 (window-frame window))))))
1840 nil)
1841 ((and (display-multi-frame-p) (window-dedicated-p window))
1842 (if calendar-remove-frame-by-deleting
1843 (delete-frame (window-frame window))
1844 (iconify-frame (window-frame window))))
1845 ((not (and (select-window window) (one-window-p window)))
1846 (delete-window window))
1847 (t (set-buffer buffer)
1848 (bury-buffer))))))
1849
1850 (defun calendar-current-date (&optional offset)
1851 "Return the current date in a list (month day year).
1852 Optional integer OFFSET is a number of days from the current date."
1853 (let* ((now (decode-time))
1854 (now (list (nth 4 now) (nth 3 now) (nth 5 now))))
1855 (if (zerop (or offset 0))
1856 now
1857 (calendar-gregorian-from-absolute
1858 (+ offset (calendar-absolute-from-gregorian now))))))
1859
1860 (defun calendar-column-to-segment ()
1861 "Convert current column to calendar month \"segment\".
1862 The left-most month returns 0, the next right 1, and so on."
1863 (let ((col (max 0 (+ (current-column)
1864 (/ calendar-intermonth-spacing 2)
1865 (- calendar-left-margin)))))
1866 (/ col (+ (* 7 calendar-column-width) calendar-intermonth-spacing))))
1867
1868 (defun calendar-cursor-to-date (&optional error event)
1869 "Return a list (month day year) of current cursor position.
1870 If cursor is not on a specific date, signals an error if optional parameter
1871 ERROR is non-nil, otherwise just returns nil.
1872 If EVENT is non-nil, it's an event indicating the buffer position to
1873 use instead of point."
1874 (with-current-buffer
1875 (if event (window-buffer (posn-window (event-start event)))
1876 (current-buffer))
1877 (save-excursion
1878 (and event (setq event (event-start event))
1879 (goto-char (posn-point event)))
1880 (let* ((segment (calendar-column-to-segment))
1881 (month (% (+ displayed-month (1- segment)) 12)))
1882 ;; Call with point on either of the two digits in a 2-digit date,
1883 ;; or on or before the digit of a 1-digit date.
1884 (if (not (and (looking-at "[ 0-9]?[0-9][^0-9]")
1885 (get-text-property (point) 'date)))
1886 (if error (error "Not on a date!"))
1887 ;; Convert segment to real month and year.
1888 (if (zerop month) (setq month 12))
1889 ;; Go back to before the first date digit.
1890 (or (looking-at " ")
1891 (re-search-backward "[^0-9]"))
1892 (list month
1893 (string-to-number
1894 (buffer-substring (1+ (point))
1895 (+ 1 calendar-day-digit-width (point))))
1896 (cond
1897 ((and (= 12 month) (zerop segment)) (1- displayed-year))
1898 ((and (= 1 month) (= segment 2)) (1+ displayed-year))
1899 (t displayed-year))))))))
1900
1901 (add-to-list 'debug-ignored-errors "Not on a date!")
1902
1903 ;; The following version of calendar-gregorian-from-absolute is preferred for
1904 ;; reasons of clarity, BUT it's much slower than the version that follows it.
1905
1906 ;;(defun calendar-gregorian-from-absolute (date)
1907 ;; "Compute the list (month day year) corresponding to the absolute DATE.
1908 ;;The absolute date is the number of days elapsed since the (imaginary)
1909 ;;Gregorian date Sunday, December 31, 1 BC."
1910 ;; (let* ((approx (/ date 366)) ; approximation from below
1911 ;; (year ; search forward from the approximation
1912 ;; (+ approx
1913 ;; (calendar-sum y approx
1914 ;; (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
1915 ;; 1)))
1916 ;; (month ; search forward from January
1917 ;; (1+ (calendar-sum m 1
1918 ;; (> date
1919 ;; (calendar-absolute-from-gregorian
1920 ;; (list m (calendar-last-day-of-month m year) year)))
1921 ;; 1)))
1922 ;; (day ; calculate the day by subtraction
1923 ;; (- date
1924 ;; (1- (calendar-absolute-from-gregorian (list month 1 year))))))
1925 ;; (list month day year)))
1926
1927 (defun calendar-gregorian-from-absolute (date)
1928 "Compute the list (month day year) corresponding to the absolute DATE.
1929 The absolute date is the number of days elapsed since the (imaginary)
1930 Gregorian date Sunday, December 31, 1 BC. This function does not
1931 handle dates in years BC."
1932 ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
1933 ;; Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
1934 ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
1935 ;; (April, 1993), pages 383-404 for an explanation.
1936 (let* ((d0 (1- date))
1937 (n400 (/ d0 146097))
1938 (d1 (% d0 146097))
1939 (n100 (/ d1 36524))
1940 (d2 (% d1 36524))
1941 (n4 (/ d2 1461))
1942 (d3 (% d2 1461))
1943 (n1 (/ d3 365))
1944 (day (1+ (% d3 365)))
1945 (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1))
1946 (month 1)
1947 mdays)
1948 (if (or (= n100 4) (= n1 4))
1949 (list 12 31 year)
1950 (setq year (1+ year))
1951 (while (< (setq mdays (calendar-last-day-of-month month year)) day)
1952 (setq day (- day mdays)
1953 month (1+ month)))
1954 (list month day year))))
1955
1956 (defun calendar-other-month (month year &optional event)
1957 "Display a three-month calendar centered around MONTH and YEAR.
1958 EVENT is an event like `last-nonmenu-event'."
1959 (interactive (let ((event (list last-nonmenu-event)))
1960 (append (calendar-read-date 'noday) event)))
1961 (save-selected-window
1962 (and event
1963 (setq event (event-start event))
1964 (select-window (posn-window event)))
1965 (unless (and (= month displayed-month)
1966 (= year displayed-year))
1967 (let ((old-date (calendar-cursor-to-date))
1968 (today (calendar-current-date)))
1969 (calendar-generate-window month year)
1970 (calendar-cursor-to-visible-date
1971 (cond
1972 ((calendar-date-is-visible-p old-date) old-date)
1973 ((calendar-date-is-visible-p today) today)
1974 (t (list month 1 year))))))))
1975
1976 (defun calendar-set-mark (arg &optional event)
1977 "Mark the date under the cursor, or jump to marked date.
1978 With no prefix argument, push current date onto marked date ring.
1979 With argument ARG, jump to mark, pop it, and put point at end of ring."
1980 (interactive
1981 (list current-prefix-arg last-nonmenu-event))
1982 (let ((date (calendar-cursor-to-date t event)))
1983 (if arg
1984 (if (null calendar-mark-ring)
1985 (error "No mark set in this buffer")
1986 (calendar-goto-date (car calendar-mark-ring))
1987 (setq calendar-mark-ring
1988 (cdr (nconc calendar-mark-ring (list date)))))
1989 (push date calendar-mark-ring)
1990 ;; Since the top of the mark ring is the marked date in the
1991 ;; calendar, the mark ring in the calendar is one longer than
1992 ;; in other buffers to get the same effect.
1993 (if (> (length calendar-mark-ring) (1+ mark-ring-max))
1994 (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
1995 (message "Mark set"))))
1996
1997 (defun calendar-exchange-point-and-mark ()
1998 "Exchange the current cursor position with the marked date."
1999 (interactive)
2000 (let ((mark (car calendar-mark-ring))
2001 (date (calendar-cursor-to-date t)))
2002 (if (null mark)
2003 (error "No mark set in this buffer")
2004 (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
2005 (calendar-goto-date mark))))
2006
2007 (defun calendar-count-days-region ()
2008 "Count the number of days (inclusive) between point and the mark."
2009 (interactive)
2010 (let* ((days (- (calendar-absolute-from-gregorian
2011 (calendar-cursor-to-date t))
2012 (calendar-absolute-from-gregorian
2013 (or (car calendar-mark-ring)
2014 (error "No mark set in this buffer")))))
2015 (days (1+ (if (> days 0) days (- days)))))
2016 (message "Region has %d day%s (inclusive)"
2017 days (if (> days 1) "s" ""))))
2018
2019 (defun calendar-not-implemented ()
2020 "Not implemented."
2021 (interactive)
2022 (error "%s not available in the calendar"
2023 (global-key-binding (this-command-keys))))
2024
2025 (defun calendar-read (prompt acceptable &optional initial-contents)
2026 "Return an object read from the minibuffer.
2027 Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
2028 entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
2029 is a string to insert in the minibuffer before reading."
2030 (let ((value (read-minibuffer prompt initial-contents)))
2031 (while (not (funcall acceptable value))
2032 (setq value (read-minibuffer prompt initial-contents)))
2033 value))
2034
2035
2036 (defvar calendar-abbrev-length 3
2037 "*Length of abbreviations to be used for day and month names.
2038 See also `calendar-day-abbrev-array' and `calendar-month-abbrev-array'.")
2039
2040 ;; FIXME does it have to start from Sunday?
2041 (defcustom calendar-day-name-array
2042 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]
2043 "Array of capitalized strings giving, in order, the day names.
2044 The first two characters of each string will be used to head the
2045 day columns in the calendar. See also the variable
2046 `calendar-day-abbrev-array'."
2047 :group 'calendar
2048 :type '(vector (string :tag "Sunday")
2049 (string :tag "Monday")
2050 (string :tag "Tuesday")
2051 (string :tag "Wednesday")
2052 (string :tag "Thursday")
2053 (string :tag "Friday")
2054 (string :tag "Saturday")))
2055
2056 (defvar calendar-day-abbrev-array
2057 [nil nil nil nil nil nil nil]
2058 "*Array of capitalized strings giving the abbreviated day names.
2059 The order should be the same as that of the full names specified
2060 in `calendar-day-name-array'. These abbreviations may be used
2061 instead of the full names in the diary file. Do not include a
2062 trailing `.' in the strings specified in this variable, though
2063 you may use such in the diary file. If any element of this array
2064 is nil, then the abbreviation will be constructed as the first
2065 `calendar-abbrev-length' characters of the corresponding full name.")
2066
2067 (defcustom calendar-month-name-array
2068 ["January" "February" "March" "April" "May" "June"
2069 "July" "August" "September" "October" "November" "December"]
2070 "Array of capitalized strings giving, in order, the month names.
2071 See also the variable `calendar-month-abbrev-array'."
2072 :group 'calendar
2073 :type '(vector (string :tag "January")
2074 (string :tag "February")
2075 (string :tag "March")
2076 (string :tag "April")
2077 (string :tag "May")
2078 (string :tag "June")
2079 (string :tag "July")
2080 (string :tag "August")
2081 (string :tag "September")
2082 (string :tag "October")
2083 (string :tag "November")
2084 (string :tag "December")))
2085
2086 (defvar calendar-month-abbrev-array
2087 [nil nil nil nil nil nil nil nil nil nil nil nil]
2088 "*Array of capitalized strings giving the abbreviated month names.
2089 The order should be the same as that of the full names specified
2090 in `calendar-month-name-array'. These abbreviations are used in
2091 the calendar menu entries, and can also be used in the diary
2092 file. Do not include a trailing `.' in the strings specified in
2093 this variable, though you may use such in the diary file. If any
2094 element of this array is nil, then the abbreviation will be
2095 constructed as the first `calendar-abbrev-length' characters of the
2096 corresponding full name.")
2097
2098 (defun calendar-make-alist (sequence &optional start-index filter abbrevs)
2099 "Make an assoc list corresponding to SEQUENCE.
2100 Each element of sequence will be associated with an integer, starting
2101 from 1, or from START-INDEX if that is non-nil. If a sequence ABBREVS
2102 is supplied, the function `calendar-abbrev-construct' is used to
2103 construct abbreviations corresponding to the elements in SEQUENCE.
2104 Each abbreviation is entered into the alist with the same
2105 association index as the full name it represents.
2106 If FILTER is provided, apply it to each key in the alist."
2107 (let ((index 0)
2108 (offset (or start-index 1))
2109 (aseq (if abbrevs (calendar-abbrev-construct abbrevs sequence)))
2110 (aseqp (if abbrevs (calendar-abbrev-construct abbrevs sequence
2111 'period)))
2112 alist elem)
2113 (dotimes (i (length sequence) (reverse alist))
2114 (setq index (+ i offset)
2115 elem (elt sequence i)
2116 alist
2117 (cons (cons (if filter (funcall filter elem) elem) index) alist))
2118 (if aseq
2119 (setq elem (elt aseq i)
2120 alist (cons (cons (if filter (funcall filter elem) elem)
2121 index) alist)))
2122 (if aseqp
2123 (setq elem (elt aseqp i)
2124 alist (cons (cons (if filter (funcall filter elem) elem)
2125 index) alist))))))
2126
2127 (defun calendar-read-date (&optional noday)
2128 "Prompt for Gregorian date. Return a list (month day year).
2129 If optional NODAY is t, does not ask for day, but just returns
2130 \(month 1 year); if NODAY is any other non-nil value the value returned is
2131 \(month year)"
2132 (let* ((year (calendar-read
2133 "Year (>0): "
2134 (lambda (x) (> x 0))
2135 (number-to-string (calendar-extract-year
2136 (calendar-current-date)))))
2137 (month-array calendar-month-name-array)
2138 (completion-ignore-case t)
2139 (month (cdr (assoc-string
2140 (completing-read
2141 "Month name: "
2142 (mapcar 'list (append month-array nil))
2143 nil t)
2144 (calendar-make-alist month-array 1) t)))
2145 (last (calendar-last-day-of-month month year)))
2146 (if noday
2147 (if (eq noday t)
2148 (list month 1 year)
2149 (list month year))
2150 (list month
2151 (calendar-read (format "Day (1-%d): " last)
2152 (lambda (x) (and (< 0 x) (<= x last))))
2153 year))))
2154
2155 (defun calendar-interval (mon1 yr1 mon2 yr2)
2156 "The number of months difference between MON1, YR1 and MON2, YR2.
2157 The result is positive if the second date is later than the first.
2158 Negative years are interpreted as years BC; -1 being 1 BC, and so on."
2159 (if (< yr1 0) (setq yr1 (1+ yr1))) ; -1 BC -> 0 AD, etc
2160 (if (< yr2 0) (setq yr2 (1+ yr2)))
2161 (+ (* 12 (- yr2 yr1))
2162 (- mon2 mon1)))
2163
2164 (defun calendar-abbrev-construct (abbrev full &optional period)
2165 "Internal calendar function to return a complete abbreviation array.
2166 ABBREV is an array of abbreviations, FULL the corresponding array
2167 of full names. The return value is the ABBREV array, with any nil
2168 elements replaced by the first three characters taken from the
2169 corresponding element of FULL. If optional argument PERIOD is non-nil,
2170 each element returned has a final `.' character."
2171 (let (elem array name)
2172 (dotimes (i (length full))
2173 (setq name (aref full i)
2174 elem (or (aref abbrev i)
2175 (substring name 0
2176 (min calendar-abbrev-length (length name))))
2177 elem (format "%s%s" elem (if period "." ""))
2178 array (append array (list elem))))
2179 (vconcat array)))
2180
2181 (defvar calendar-font-lock-keywords
2182 `((,(concat (regexp-opt (mapcar 'identity calendar-month-name-array) t)
2183 " -?[0-9]+")
2184 . font-lock-function-name-face) ; month and year
2185 (,(regexp-opt
2186 (list (substring (aref calendar-day-name-array 6)
2187 0 calendar-day-header-width)
2188 (substring (aref calendar-day-name-array 0)
2189 0 calendar-day-header-width)))
2190 ;; Saturdays and Sundays are highlighted differently.
2191 . font-lock-comment-face)
2192 ;; First two chars of each day are used in the calendar.
2193 (,(regexp-opt (mapcar (lambda (x) (substring x 0 calendar-day-header-width))
2194 calendar-day-name-array))
2195 . font-lock-reference-face))
2196 "Default keywords to highlight in Calendar mode.")
2197
2198 (defun calendar-day-name (date &optional abbrev absolute)
2199 "Return a string with the name of the day of the week of DATE.
2200 DATE should be a list in the format (MONTH DAY YEAR), unless the
2201 optional argument ABSOLUTE is non-nil, in which case DATE should
2202 be an integer in the range 0 to 6 corresponding to the day of the
2203 week. Day names are taken from the variable `calendar-day-name-array',
2204 unless the optional argument ABBREV is non-nil, in which case
2205 the variable `calendar-day-abbrev-array' is used."
2206 (aref (if abbrev
2207 (calendar-abbrev-construct calendar-day-abbrev-array
2208 calendar-day-name-array)
2209 calendar-day-name-array)
2210 (if absolute date (calendar-day-of-week date))))
2211
2212 (defun calendar-month-name (month &optional abbrev)
2213 "Return a string with the name of month number MONTH.
2214 Months are numbered from one. Month names are taken from the
2215 variable `calendar-month-name-array', unless the optional
2216 argument ABBREV is non-nil, in which case
2217 `calendar-month-abbrev-array' is used."
2218 (aref (if abbrev
2219 (calendar-abbrev-construct calendar-month-abbrev-array
2220 calendar-month-name-array)
2221 calendar-month-name-array)
2222 (1- month)))
2223
2224 (defun calendar-day-of-week (date)
2225 "Return the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc.
2226 DATE is a list of the form (month day year). A negative year is
2227 interpreted as BC; -1 being 1 BC, and so on."
2228 (mod (calendar-absolute-from-gregorian date) 7))
2229
2230 (defun calendar-week-end-day ()
2231 "Return the index (0 for Sunday, etc.) of the last day of the week."
2232 (mod (+ calendar-week-start-day 6) 7))
2233
2234 (defun calendar-unmark ()
2235 "Delete all diary/holiday marks/highlighting from the calendar."
2236 (interactive)
2237 (setq calendar-mark-holidays-flag nil
2238 calendar-mark-diary-entries-flag nil)
2239 (with-current-buffer calendar-buffer
2240 (mapc 'delete-overlay (overlays-in (point-min) (point-max)))))
2241
2242 (defun calendar-date-is-visible-p (date)
2243 "Return non-nil if DATE is valid and is visible in the calendar window."
2244 (and (calendar-date-is-valid-p date)
2245 (< (abs (calendar-interval
2246 displayed-month displayed-year
2247 (calendar-extract-month date) (calendar-extract-year date)))
2248 2)))
2249
2250 ;; FIXME can this be generalized for holiday-chinese?
2251 (defun calendar-nongregorian-visible-p (month day toabs fromabs switch)
2252 "Return non-nil if MONTH, DAY is visible in the calendar window.
2253 MONTH and DAY are in some non-Gregorian calendar system. The
2254 functions TOABS and FROMABS convert that system to and from
2255 absolute, respectively. SWITCH is a function that takes a single
2256 argument (a local month number). It applies when the local year
2257 changes across the calendar window, and returns non-nil if the
2258 specified month should be associated with the higher year.
2259 Returns the corresponding Gregorian date."
2260 ;; We need to choose the local year associated with month and day
2261 ;; that might make them visible.
2262 (let* ((m1 displayed-month)
2263 (y1 displayed-year)
2264 (m2 displayed-month)
2265 (y2 displayed-year)
2266 ;; Absolute date of first/last dates in calendar window.
2267 (start-date (progn
2268 (calendar-increment-month m1 y1 -1)
2269 (calendar-absolute-from-gregorian (list m1 1 y1))))
2270 (end-date (progn
2271 (calendar-increment-month m2 y2 1)
2272 (calendar-absolute-from-gregorian
2273 (list m2 (calendar-last-day-of-month m2 y2) y2))))
2274 ;; Local date of first/last date in calendar window.
2275 (local-start (funcall fromabs start-date))
2276 (local-end (funcall fromabs end-date))
2277 ;; Local year of first/last dates.
2278 ;; Can only differ if displayed-month = 12, 1, 2.
2279 (local-y1 (calendar-extract-year local-start))
2280 (local-y2 (calendar-extract-year local-end))
2281 ;; Choose which year might be visible in the window.
2282 ;; Obviously it only matters when y1 and y2 differ, ie
2283 ;; when the _local_ new year is visible.
2284 (year (if (funcall switch month) local-y2 local-y1))
2285 (date (calendar-gregorian-from-absolute
2286 (funcall toabs (list month day year)))))
2287 (if (calendar-date-is-visible-p date)
2288 date)))
2289
2290 (defun calendar-date-is-valid-p (date)
2291 "Return t if DATE is a valid date."
2292 (let ((month (calendar-extract-month date))
2293 (day (calendar-extract-day date))
2294 (year (calendar-extract-year date)))
2295 (and (<= 1 month) (<= month 12)
2296 ;; (calendar-read-date t) used to return a date with day = nil.
2297 ;; Should not be valid (?), since many funcs prob assume integer.
2298 ;; (calendar-read-date 'noday) returns (month year), which
2299 ;; currently results in calendar-extract-year returning nil.
2300 day year (<= 1 day) (<= day (calendar-last-day-of-month month year))
2301 ;; BC dates left as non-valid, to suppress errors from
2302 ;; complex holiday algorithms not suitable for years BC.
2303 ;; Note there are side effects on calendar navigation.
2304 (<= 1 year))))
2305
2306 (define-obsolete-function-alias 'calendar-date-is-legal-p
2307 'calendar-date-is-valid-p "23.1")
2308
2309 (defun calendar-date-equal (date1 date2)
2310 "Return t if the DATE1 and DATE2 are the same."
2311 (and
2312 (= (calendar-extract-month date1) (calendar-extract-month date2))
2313 (= (calendar-extract-day date1) (calendar-extract-day date2))
2314 (= (calendar-extract-year date1) (calendar-extract-year date2))))
2315
2316 (defun calendar-make-temp-face (attrlist)
2317 "Return a temporary face based on the attributes in ATTRLIST.
2318 ATTRLIST is a list with elements of the form :face face :foreground color."
2319 (let ((attrs attrlist)
2320 faceinfo face temp-face)
2321 ;; Separate :face from the other attributes. Use the last :face
2322 ;; if there are more than one. FIXME is merging meaningful?
2323 (while attrs
2324 (if (eq (car attrs) :face)
2325 (setq face (intern-soft (cadr attrs))
2326 attrs (cddr attrs))
2327 (push (car attrs) faceinfo)
2328 (setq attrs (cdr attrs))))
2329 (or (facep face) (setq face 'default))
2330 (if (not faceinfo)
2331 ;; No attributes to apply, so just use an existing-face.
2332 face
2333 ;; FIXME should we be using numbered temp-faces, re-using where poss?
2334 (setq temp-face
2335 (make-symbol
2336 (concat ":caltemp"
2337 (mapconcat (lambda (sym)
2338 (cond
2339 ((symbolp sym) (symbol-name sym))
2340 ((numberp sym) (number-to-string sym))
2341 (t sym)))
2342 attrlist ""))))
2343 (make-face temp-face)
2344 (copy-face face temp-face)
2345 ;; Apply the font aspects.
2346 (apply 'set-face-attribute temp-face nil (nreverse faceinfo))
2347 temp-face)))
2348
2349 (defun calendar-mark-visible-date (date &optional mark)
2350 "Mark DATE in the calendar window with MARK.
2351 MARK is a single-character string, a list of face attributes/values, or a face.
2352 MARK defaults to `diary-entry-marker'."
2353 (if (calendar-date-is-valid-p date)
2354 (with-current-buffer calendar-buffer
2355 (save-excursion
2356 (calendar-cursor-to-visible-date date)
2357 (setq mark
2358 (or (and (stringp mark) (= (length mark) 1) mark) ; single-char
2359 ;; The next two use to also check font-lock-mode.
2360 ;; See comments above diary-entry-marker for why
2361 ;; this was dropped.
2362 ;;; (and font-lock-mode
2363 ;;; (or
2364 (and (listp mark) (> (length mark) 0) mark) ; attrs
2365 (and (facep mark) mark) ; )) face-name
2366 diary-entry-marker))
2367 (cond
2368 ;; Face or an attr-list that contained a face.
2369 ((facep mark)
2370 (overlay-put
2371 (make-overlay (1- (point)) (1+ (point))) 'face mark))
2372 ;; Single-character mark, goes after the date.
2373 ((and (stringp mark) (= (length mark) 1))
2374 (overlay-put
2375 (make-overlay (1+ (point)) (+ 2 (point))) 'display mark))
2376 (t ; attr list
2377 (overlay-put
2378 (make-overlay (1- (point)) (1+ (point))) 'face
2379 (calendar-make-temp-face mark))))))))
2380
2381 (define-obsolete-function-alias 'mark-visible-calendar-date
2382 'calendar-mark-visible-date "23.1")
2383
2384 (defun calendar-star-date ()
2385 "Replace the date under the cursor in the calendar window with asterisks.
2386 You might want to add this function to `calendar-today-visible-hook'."
2387 (unless (catch 'found
2388 (dolist (ol (overlays-at (point)))
2389 (and (overlay-get ol 'calendar-star)
2390 (throw 'found t))))
2391 (let ((ol (make-overlay (1- (point)) (point))))
2392 (overlay-put ol 'display "*")
2393 (overlay-put ol 'calendar-star t)
2394 ;; Use copy-sequence to avoid merging of identical 'display props.
2395 ;; Use two overlays so as not to mess up
2396 ;; calendar-cursor-to-nearest-date (and calendar-forward-day).
2397 (overlay-put (setq ol (make-overlay (point) (1+ (point))))
2398 'display (copy-sequence "*"))
2399 (overlay-put ol 'calendar-star t))))
2400
2401 (defun calendar-mark-today ()
2402 "Mark the date under the cursor in the calendar window.
2403 The date is marked with `calendar-today-marker'. You might want to add
2404 this function to `calendar-today-visible-hook'."
2405 (calendar-mark-visible-date (calendar-cursor-to-date) calendar-today-marker))
2406
2407 ;; FIXME why the car? Almost every usage calls list on the args.
2408 (defun calendar-date-compare (date1 date2)
2409 "Return t if DATE1 is before DATE2, nil otherwise.
2410 The actual dates are in the car of DATE1 and DATE2."
2411 (< (calendar-absolute-from-gregorian (car date1))
2412 (calendar-absolute-from-gregorian (car date2))))
2413
2414 (defun calendar-date-string (date &optional abbreviate nodayname)
2415 "A string form of DATE, driven by the variable `calendar-date-display-form'.
2416 An optional parameter ABBREVIATE, when non-nil, causes the month
2417 and day names to be abbreviated as specified by
2418 `calendar-month-abbrev-array' and `calendar-day-abbrev-array',
2419 respectively. An optional parameter NODAYNAME, when t, omits the
2420 name of the day of the week."
2421 (let* ((dayname (unless nodayname (calendar-day-name date abbreviate)))
2422 (month (calendar-extract-month date))
2423 (monthname (calendar-month-name month abbreviate))
2424 (day (number-to-string (calendar-extract-day date)))
2425 (month (number-to-string month))
2426 (year (number-to-string (calendar-extract-year date))))
2427 (mapconcat 'eval calendar-date-display-form "")))
2428
2429 (defun calendar-dayname-on-or-before (dayname date)
2430 "Return the absolute date of the DAYNAME on or before absolute DATE.
2431 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
2432
2433 Note: Applying this function to d+6 gives us the DAYNAME on or after an
2434 absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
2435 absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
2436 date d, and applying it to d+7 gives the DAYNAME following absolute date d."
2437 (- date (% (- date dayname) 7)))
2438
2439 (defun calendar-nth-named-absday (n dayname month year &optional day)
2440 "Absolute date of the Nth DAYNAME after/before MONTH YEAR DAY.
2441 A DAYNAME of 0 means Sunday, 1 means Monday, and so on.
2442 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2443 If N<0, return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2444 DAY defaults to 1 if N>0, and MONTH's last day otherwise."
2445 (if (> n 0)
2446 (+ (* 7 (1- n))
2447 (calendar-dayname-on-or-before
2448 dayname
2449 (+ 6 (calendar-absolute-from-gregorian
2450 (list month (or day 1) year)))))
2451 (+ (* 7 (1+ n))
2452 (calendar-dayname-on-or-before
2453 dayname
2454 (calendar-absolute-from-gregorian
2455 (list month
2456 (or day (calendar-last-day-of-month month year))
2457 year))))))
2458
2459 (defun calendar-nth-named-day (n dayname month year &optional day)
2460 "Date of the Nth DAYNAME after/before MONTH YEAR DAY.
2461 Like `calendar-nth-named-absday', but returns a Gregorian date."
2462 (calendar-gregorian-from-absolute
2463 (calendar-nth-named-absday n dayname month year day)))
2464
2465 (defun calendar-day-of-year-string (&optional date)
2466 "String of day number of year of Gregorian DATE.
2467 Defaults to today's date if DATE is not given."
2468 (let* ((d (or date (calendar-current-date)))
2469 (year (calendar-extract-year d))
2470 (day (calendar-day-number d))
2471 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
2472 (format "Day %d of %d; %d day%s remaining in the year"
2473 day year days-remaining (if (= days-remaining 1) "" "s"))))
2474
2475 (defun calendar-other-dates (date)
2476 "Return a list of strings giving Gregorian DATE in other calendars.
2477 DATE is (month day year). Calendars that do not apply are omitted."
2478 (let (odate)
2479 (delq nil
2480 (list
2481 (calendar-day-of-year-string date)
2482 (format "ISO date: %s" (calendar-iso-date-string date))
2483 (format "Julian date: %s"
2484 (calendar-julian-date-string date))
2485 (format "Astronomical (Julian) day number (at noon UTC): %s.0"
2486 (calendar-astro-date-string date))
2487 (format "Fixed (RD) date: %s"
2488 (calendar-absolute-from-gregorian date))
2489 (format "Hebrew date (before sunset): %s"
2490 (calendar-hebrew-date-string date))
2491 (format "Persian date: %s"
2492 (calendar-persian-date-string date))
2493 (unless (string-equal
2494 (setq odate (calendar-islamic-date-string date))
2495 "")
2496 (format "Islamic date (before sunset): %s" odate))
2497 (unless (string-equal
2498 (setq odate (calendar-bahai-date-string date))
2499 "")
2500 (format "Baha'i date: %s" odate))
2501 (format "Chinese date: %s"
2502 (calendar-chinese-date-string date))
2503 (unless (string-equal
2504 (setq odate (calendar-coptic-date-string date))
2505 "")
2506 (format "Coptic date: %s" odate))
2507 (unless (string-equal
2508 (setq odate (calendar-ethiopic-date-string date))
2509 "")
2510 (format "Ethiopic date: %s" odate))
2511 (unless (string-equal
2512 (setq odate (calendar-french-date-string date))
2513 "")
2514 (format "French Revolutionary date: %s" odate))
2515 (format "Mayan date: %s"
2516 (calendar-mayan-date-string date))))))
2517
2518 (declare-function x-popup-menu "menu.c" (position menu))
2519
2520 (defun calendar-print-other-dates (&optional event)
2521 "Show dates on other calendars for date under the cursor.
2522 If called by a mouse-event, pops up a menu with the result."
2523 (interactive (list last-nonmenu-event))
2524 (let* ((date (calendar-cursor-to-date t event))
2525 (title (format "%s (Gregorian)" (calendar-date-string date)))
2526 (others (calendar-other-dates date))
2527 selection)
2528 (if (mouse-event-p event)
2529 (and (setq selection (cal-menu-x-popup-menu event title
2530 (mapcar 'list others)))
2531 (call-interactively selection))
2532 (calendar-in-read-only-buffer calendar-other-calendars-buffer
2533 (calendar-set-mode-line title)
2534 (insert (mapconcat 'identity others "\n"))))))
2535
2536 (defun calendar-print-day-of-year ()
2537 "Show day number in year/days remaining in year for date under the cursor."
2538 (interactive)
2539 (message "%s" (calendar-day-of-year-string (calendar-cursor-to-date t))))
2540
2541 (defun calendar-set-mode-line (str)
2542 "Set mode line to STR, centered, surrounded by dashes."
2543 (let* ((edges (window-edges))
2544 ;; As per doc of window-width, total visible mode-line length.
2545 (width (- (nth 2 edges) (car edges))))
2546 ;; Hack for --daemon. See bug #2199.
2547 ;; If no frame exists yet, we have no idea what width to use.
2548 (and (= width 10)
2549 (not window-system)
2550 (setq width (or (getenv "COLUMNS") 80)))
2551 (setq mode-line-format
2552 (if buffer-file-name
2553 `("-" mode-line-modified
2554 ,(calendar-string-spread (list str) ?- (- width 6))
2555 "---")
2556 (calendar-string-spread (list str) ?- width)))))
2557
2558 (defun calendar-version ()
2559 "Display the Calendar version."
2560 (interactive)
2561 (message "GNU Emacs %s" emacs-version))
2562
2563 (make-obsolete 'calendar-version 'emacs-version "23.1")
2564
2565
2566 (run-hooks 'calendar-load-hook)
2567
2568 (provide 'calendar)
2569
2570 ;; Local variables:
2571 ;; byte-compile-dynamic: t
2572 ;; End:
2573
2574 ;;; calendar.el ends here