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