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