PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/calendar/calendar.el

http://github.com/emacsmirror/emacs
Emacs Lisp | 1566 lines | 1135 code | 196 blank | 235 comment | 47 complexity | 800b1c34a19cae89764167592f2e6baf MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, AGPL-3.0
  1. ;;; calendar.el --- calendar functions
  2. ;; Copyright (C) 1988-1995, 1997, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Keywords: calendar
  6. ;; Human-Keywords: calendar, Gregorian calendar, diary, holidays
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This collection of functions implements a calendar window. It
  20. ;; generates a calendar for the current month, together with the
  21. ;; previous and coming months, or for any other three-month period.
  22. ;; The calendar can be scrolled forward and backward in the window to
  23. ;; show months in the past or future; the cursor can move forward and
  24. ;; backward by days, weeks, or months, making it possible, for
  25. ;; instance, to jump to the date a specified number of days, weeks, or
  26. ;; months from the date under the cursor. The user can display a list
  27. ;; of holidays and other notable days for the period shown; the
  28. ;; notable days can be marked on the calendar, if desired. The user
  29. ;; can also specify that dates having corresponding diary entries (in
  30. ;; a file that the user specifies) be marked; the diary entries for
  31. ;; any date can be viewed in a separate window. The diary and the
  32. ;; notable days can be viewed independently of the calendar. Dates
  33. ;; can be translated from the (usual) Gregorian calendar to the day of
  34. ;; the year/days remaining in year, to the ISO commercial calendar, to
  35. ;; the Julian (old style) calendar, to the Hebrew calendar, to the
  36. ;; Islamic calendar, to the Bahá'í calendar, to the French
  37. ;; Revolutionary calendar, to the Mayan calendar, to the Chinese
  38. ;; calendar, to the Coptic calendar, to the Ethiopic calendar, and to
  39. ;; the astronomical (Julian) day number. Times of sunrise/sunset can
  40. ;; be displayed, as can the phases of the moon. Appointment
  41. ;; notification for diary entries is available. Calendar printing via
  42. ;; LaTeX is available.
  43. ;; The following files are part of the calendar/diary code:
  44. ;; appt.el Appointment notification
  45. ;; cal-bahai.el Bahá'í calendar
  46. ;; cal-china.el Chinese calendar
  47. ;; cal-coptic.el Coptic/Ethiopic calendars
  48. ;; cal-dst.el Daylight saving time rules
  49. ;; cal-french.el French revolutionary calendar
  50. ;; cal-hebrew.el Hebrew calendar
  51. ;; cal-html.el Calendars in HTML
  52. ;; cal-islam.el Islamic calendar
  53. ;; cal-iso.el ISO calendar
  54. ;; cal-julian.el Julian/astronomical calendars
  55. ;; cal-mayan.el Mayan calendars
  56. ;; cal-menu.el Menu support
  57. ;; cal-move.el Movement in the calendar
  58. ;; cal-persia.el Persian calendar
  59. ;; cal-tex.el Calendars in LaTeX
  60. ;; cal-x.el Dedicated frame functions
  61. ;; calendar.el This file
  62. ;; diary-lib.el Diary functions
  63. ;; holidays.el Holiday functions
  64. ;; lunar.el Phases of the moon
  65. ;; solar.el Sunrise/sunset, equinoxes/solstices
  66. ;; Technical details of all the calendrical calculations can be found in
  67. ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
  68. ;; and Nachum Dershowitz, Cambridge University Press (2001).
  69. ;; An earlier version of the technical details appeared in
  70. ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
  71. ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
  72. ;; pages 899-928, and in ``Calendrical Calculations, Part II: Three Historical
  73. ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
  74. ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
  75. ;; pages 383-404.
  76. ;; Hard copies of these two papers can be obtained by sending email to
  77. ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
  78. ;; the message BODY containing your mailing address (snail).
  79. ;; A note on free variables:
  80. ;; The calendar passes around a few dynamically bound variables, which
  81. ;; unfortunately have rather common names. They are meant to be
  82. ;; available for external functions, so the names can't be changed.
  83. ;; displayed-month, displayed-year: bound in calendar-generate, the
  84. ;; central month of the 3 month calendar window
  85. ;; original-date, number: bound in diary-list-entries, the arguments
  86. ;; with which that function was called.
  87. ;; date, entry: bound in diary-list-sexp-entries (qv)
  88. ;; Bound in diary-list-entries:
  89. ;; diary-entries-list: use in d-l, appt.el, and by diary-add-to-list
  90. ;; diary-saved-point: only used in diary-lib.el, passed to the display func
  91. ;; date-string: only used in diary-lib.el
  92. ;; list-only: don't modify the diary-buffer, just return a list of entries
  93. ;; file-glob-attrs: yuck
  94. ;;; Code:
  95. (load "cal-loaddefs" nil t)
  96. ;; Avoid recursive load of calendar when loading cal-menu. Yuck.
  97. (provide 'calendar)
  98. (require 'cal-menu)
  99. (defgroup calendar nil
  100. "Calendar and time management support."
  101. :prefix "calendar-"
  102. :group 'applications)
  103. (defgroup calendar-hooks nil
  104. "Calendar hooks."
  105. :prefix "calendar-"
  106. :group 'calendar)
  107. (defgroup calendar-faces nil
  108. "Calendar faces."
  109. :prefix "calendar-"
  110. :group 'calendar)
  111. (defcustom calendar-offset 0
  112. "The offset of the principal month from the center of the calendar window.
  113. 0 means the principal month is in the center (default), -1 means on the left,
  114. +1 means on the right. Larger (or smaller) values push the principal month off
  115. the screen."
  116. :type 'integer
  117. :group 'calendar)
  118. (defcustom calendar-setup nil
  119. "The frame setup of the calendar.
  120. The choices are: `one-frame' (calendar and diary together in one separate,
  121. dedicated frame); `two-frames' (calendar and diary in separate, dedicated
  122. frames); `calendar-only' (calendar in a separate, dedicated frame); with
  123. any other value the current frame is used. Using any of the first
  124. three options overrides the value of `calendar-view-diary-initially-flag'."
  125. :type '(choice
  126. (const :tag "calendar and diary in separate frame" one-frame)
  127. (const :tag "calendar and diary each in own frame" two-frames)
  128. (const :tag "calendar in separate frame" calendar-only)
  129. (const :tag "use current frame" nil))
  130. :group 'calendar)
  131. (defcustom calendar-minimum-window-height 8
  132. "Minimum height `calendar-generate-window' should use for calendar window."
  133. :type 'integer
  134. :version "22.1"
  135. :group 'calendar)
  136. ;; See discussion in bug#1806.
  137. (defcustom calendar-split-width-threshold nil
  138. "Value to use for `split-width-threshold' when creating a calendar.
  139. This only affects frames wider than the default value of
  140. `split-width-threshold'."
  141. :type '(choice (const nil)
  142. (integer))
  143. :version "23.2"
  144. :group 'calendar)
  145. (defcustom calendar-week-start-day 0
  146. "The day of the week on which a week in the calendar begins.
  147. 0 means Sunday (default), 1 means Monday, and so on.
  148. If you change this variable directly (without using customize)
  149. after starting `calendar', you should call `calendar-redraw' to
  150. update the calendar display to reflect the change, otherwise
  151. movement commands will not work correctly."
  152. :type 'integer
  153. ;; Change the initialize so that if you reload calendar.el, it will not
  154. ;; cause a redraw (which may fail, e.g. with "invalid byte-code in
  155. ;; calendar.elc" because of the "byte-compile-dynamic").
  156. :initialize 'custom-initialize-default
  157. :set (lambda (sym val)
  158. (set sym val)
  159. (calendar-redraw))
  160. :group 'calendar)
  161. (define-obsolete-variable-alias 'view-diary-entries-initially
  162. 'calendar-view-diary-initially-flag "23.1")
  163. (defcustom calendar-view-diary-initially-flag nil
  164. "Non-nil means display current date's diary entries on entry to calendar.
  165. The diary is displayed in another window when the calendar is first displayed,
  166. if the current date is visible. The number of days of diary entries displayed
  167. is governed by the variable `diary-number-of-entries'. This variable can
  168. be overridden by the value of `calendar-setup'."
  169. :type 'boolean
  170. :group 'diary)
  171. (define-obsolete-variable-alias 'mark-diary-entries-in-calendar
  172. 'calendar-mark-diary-entries-flag "23.1")
  173. ;; FIXME :set
  174. (defcustom calendar-mark-diary-entries-flag nil
  175. "Non-nil means mark dates with diary entries, in the calendar window.
  176. The marking symbol is specified by the variable `diary-entry-marker'."
  177. :type 'boolean
  178. :group 'diary)
  179. (defcustom calendar-remove-frame-by-deleting t
  180. "Determine how the calendar mode removes a frame no longer needed.
  181. If nil, make an icon of the frame. If non-nil, delete the frame."
  182. :type 'boolean
  183. :version "23.1" ; changed from nil to t
  184. :group 'view
  185. :group 'calendar)
  186. (defface calendar-today
  187. '((t (:underline t)))
  188. "Face for indicating today's date in the calendar.
  189. See the variable `calendar-today-marker'."
  190. :group 'calendar-faces)
  191. (define-obsolete-face-alias 'calendar-today-face 'calendar-today "22.1")
  192. (defface diary
  193. '((((min-colors 88) (class color) (background light))
  194. :foreground "red1")
  195. (((class color) (background light))
  196. :foreground "red")
  197. (((min-colors 88) (class color) (background dark))
  198. :foreground "yellow1")
  199. (((class color) (background dark))
  200. :foreground "yellow")
  201. (t
  202. :weight bold))
  203. "Face for highlighting diary entries.
  204. Used to mark diary entries in the calendar (see `diary-entry-marker'),
  205. and to highlight the date header in the fancy diary."
  206. :group 'calendar-faces)
  207. (define-obsolete-face-alias 'diary-face 'diary "22.1")
  208. (defface holiday
  209. '((((class color) (background light))
  210. :background "pink")
  211. (((class color) (background dark))
  212. :background "chocolate4")
  213. (t
  214. :inverse-video t))
  215. "Face for indicating in the calendar dates that have holidays.
  216. See `calendar-holiday-marker'."
  217. :group 'calendar-faces)
  218. (define-obsolete-face-alias 'holiday-face 'holiday "22.1")
  219. ;; These briefly checked font-lock-mode, but that is broken, since it
  220. ;; is a buffer-local variable, and which buffer happens to be current
  221. ;; when this file is loaded shouldn't make a difference. One could
  222. ;; perhaps check global-font-lock-mode, or font-lock-global-modes; but
  223. ;; this feature doesn't use font-lock, so there's no real reason it
  224. ;; should respect those either. See bug#2199.
  225. ;; They also used to check display-color-p, but that is a problem if
  226. ;; loaded from --daemon. Since BW displays are rare now, this was
  227. ;; also taken out. The way to keep it would be to have nil mean do a
  228. ;; runtime check whenever this variable is used.
  229. (defcustom diary-entry-marker 'diary
  230. "How to mark dates that have diary entries.
  231. The value can be either a single-character string (e.g. \"+\") or a face."
  232. :type '(choice (string :tag "Single character string") face)
  233. :group 'diary
  234. :version "23.1")
  235. (defcustom calendar-today-marker 'calendar-today
  236. "How to mark today's date in the calendar.
  237. The value can be either a single-character string (e.g. \"=\") or a face.
  238. Used by `calendar-mark-today'."
  239. :type '(choice (string :tag "Single character string") face)
  240. :group 'calendar
  241. :version "23.1")
  242. (defcustom calendar-holiday-marker 'holiday
  243. "How to mark notable dates in the calendar.
  244. The value can be either a single-character string (e.g. \"*\") or a face."
  245. :type '(choice (string :tag "Single character string") face)
  246. :group 'holidays
  247. :version "23.1")
  248. (define-obsolete-variable-alias 'view-calendar-holidays-initially
  249. 'calendar-view-holidays-initially-flag "23.1")
  250. (defcustom calendar-view-holidays-initially-flag nil
  251. "Non-nil means display holidays for current three month period on entry.
  252. The holidays are displayed in another window when the calendar is first
  253. displayed."
  254. :type 'boolean
  255. :group 'holidays)
  256. (define-obsolete-variable-alias 'mark-holidays-in-calendar
  257. 'calendar-mark-holidays-flag "23.1")
  258. ;; FIXME :set
  259. (defcustom calendar-mark-holidays-flag nil
  260. "Non-nil means mark dates of holidays in the calendar window.
  261. The marking symbol is specified by the variable `calendar-holiday-marker'."
  262. :type 'boolean
  263. :group 'holidays)
  264. (defcustom calendar-mode-hook nil
  265. "Hook run when entering `calendar-mode'."
  266. :type 'hook
  267. :group 'calendar-hooks)
  268. (defcustom calendar-load-hook nil
  269. "List of functions to be called after the calendar is first loaded.
  270. This is the place to add key bindings to `calendar-mode-map'."
  271. :type 'hook
  272. :group 'calendar-hooks)
  273. (define-obsolete-variable-alias 'initial-calendar-window-hook
  274. 'calendar-initial-window-hook "23.1")
  275. (defcustom calendar-initial-window-hook nil
  276. "List of functions to be called when the calendar window is created.
  277. Quitting the calendar and re-entering it will cause these functions
  278. to be called again."
  279. :type 'hook
  280. :group 'calendar-hooks)
  281. (define-obsolete-variable-alias 'today-visible-calendar-hook
  282. 'calendar-today-visible-hook "23.1")
  283. (defcustom calendar-today-visible-hook nil
  284. "List of functions called whenever the current date is visible.
  285. To mark today's date, add the function `calendar-mark-today'.
  286. To replace the date with asterisks, add the function `calendar-star-date'.
  287. See also `calendar-today-invisible-hook'.
  288. In general, be careful about changing characters in the calendar buffer,
  289. since it may cause the movement commands to fail."
  290. :type 'hook
  291. :options '(calendar-mark-today calendar-star-date)
  292. :group 'calendar-hooks)
  293. (define-obsolete-variable-alias 'today-invisible-calendar-hook
  294. 'calendar-today-invisible-hook "23.1")
  295. (defcustom calendar-today-invisible-hook nil
  296. "List of functions called whenever the current date is not visible.
  297. See also `calendar-today-visible-hook'."
  298. :type 'hook
  299. :group 'calendar-hooks)
  300. (defcustom calendar-move-hook nil
  301. "List of functions called whenever the cursor moves in the calendar.
  302. For example,
  303. (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
  304. redisplays the diary for whatever date the cursor is moved to."
  305. :type 'hook
  306. :options '(calendar-update-mode-line)
  307. :group 'calendar-hooks)
  308. (defcustom calendar-date-echo-text
  309. "mouse-2: general menu\nmouse-3: menu for this date"
  310. "String displayed when the cursor is over a date in the calendar.
  311. Can be either a fixed string, or a lisp expression that returns one.
  312. When this expression is evaluated, DAY, MONTH, and YEAR are
  313. integers appropriate to the relevant date. For example, to
  314. display the ISO date:
  315. (setq calendar-date-echo-text '(format \"ISO date: %s\"
  316. (calendar-iso-date-string
  317. (list month day year))))
  318. Changing this variable without using customize has no effect on
  319. pre-existing calendar windows."
  320. :group 'calendar
  321. :initialize 'custom-initialize-default
  322. :risky t
  323. :set (lambda (sym val)
  324. (set sym val)
  325. (calendar-redraw))
  326. :type '(choice (string :tag "Fixed string")
  327. (sexp :value
  328. (format "ISO date: %s"
  329. (calendar-iso-date-string
  330. (list month day year)))))
  331. :version "23.1")
  332. (defvar calendar-month-digit-width nil
  333. "Width of the region with numbers in each month in the calendar.")
  334. (defvar calendar-month-width nil
  335. "Full width of each month in the calendar.")
  336. (defvar calendar-right-margin nil
  337. "Right margin of the calendar.")
  338. (defvar calendar-month-edges nil
  339. "Alist of month edge columns.
  340. Each element has the form (N LEFT FIRST LAST RIGHT), where
  341. LEFT is the leftmost column associated with month segment N,
  342. FIRST and LAST are the first and last columns with day digits in,
  343. and LAST is the rightmost column.")
  344. (defun calendar-month-edges (segment)
  345. "Compute the month edge columns for month SEGMENT.
  346. Returns a list (LEFT FIRST LAST RIGHT), where LEFT is the
  347. leftmost column associated with a month, FIRST and LAST are the
  348. first and last columns with day digits in, and LAST is the
  349. rightmost column."
  350. ;; The leftmost column with a digit in it in this month segment.
  351. (let* ((first (+ calendar-left-margin
  352. (* segment calendar-month-width)))
  353. ;; The rightmost column with a digit in it in this month segment.
  354. (last (+ first (1- calendar-month-digit-width)))
  355. (left (if (eq segment 0)
  356. 0
  357. (+ calendar-left-margin
  358. (* segment calendar-month-width)
  359. (- (/ calendar-intermonth-spacing 2)))))
  360. ;; The rightmost edge of this month segment, dividing the
  361. ;; space between months in two.
  362. (right (+ calendar-left-margin
  363. (* (1+ segment) calendar-month-width)
  364. (- (/ calendar-intermonth-spacing 2)))))
  365. (list left first last right)))
  366. (defun calendar-recompute-layout-variables ()
  367. "Recompute some layout-related calendar \"constants\"."
  368. (setq calendar-month-digit-width (+ (* 6 calendar-column-width)
  369. calendar-day-digit-width)
  370. calendar-month-width (+ (* 7 calendar-column-width)
  371. calendar-intermonth-spacing)
  372. calendar-right-margin (+ calendar-left-margin
  373. (* 3 (* 7 calendar-column-width))
  374. (* 2 calendar-intermonth-spacing))
  375. calendar-month-edges nil)
  376. (dotimes (i 3)
  377. (push (cons i (calendar-month-edges i)) calendar-month-edges))
  378. (setq calendar-month-edges (reverse calendar-month-edges)))
  379. ;; FIXME add font-lock-keywords.
  380. (defun calendar-set-layout-variable (symbol value &optional minmax)
  381. "Set SYMBOL's value to VALUE, an integer.
  382. A positive/negative MINMAX enforces a minimum/maximum value.
  383. Then redraw the calendar, if necessary."
  384. (let ((oldvalue (symbol-value symbol)))
  385. (custom-set-default symbol (if minmax
  386. (if (< minmax 0)
  387. (min value (- minmax))
  388. (max value minmax))
  389. value))
  390. (unless (equal value oldvalue)
  391. (calendar-recompute-layout-variables)
  392. (calendar-redraw))))
  393. (defcustom calendar-left-margin 5
  394. "Empty space to the left of the first month in the calendar."
  395. :group 'calendar
  396. :initialize 'custom-initialize-default
  397. :set 'calendar-set-layout-variable
  398. :type 'integer
  399. :version "23.1")
  400. ;; Or you can view it as columns of width 2, with 1 space, no space
  401. ;; after the last column, and a 5 space gap between month.
  402. ;; FIXME check things work if this is odd.
  403. (defcustom calendar-intermonth-spacing 4
  404. "Space between months in the calendar. Minimum value is 1."
  405. :group 'calendar
  406. :initialize 'custom-initialize-default
  407. :set (lambda (sym val)
  408. (calendar-set-layout-variable sym val 1))
  409. :type 'integer
  410. :version "23.1")
  411. ;; FIXME calendar-month-column-width?
  412. (defcustom calendar-column-width 3
  413. "Width of each day column in the calendar. Minimum value is 3."
  414. :initialize 'custom-initialize-default
  415. :set (lambda (sym val)
  416. (calendar-set-layout-variable sym val 3))
  417. :type 'integer
  418. :version "23.1")
  419. (defcustom calendar-day-header-width 2
  420. "Width of the day column headers in the calendar.
  421. Must be at least one less than `calendar-column-width'."
  422. :group 'calendar
  423. :initialize 'custom-initialize-default
  424. :set (lambda (sym val)
  425. (calendar-set-layout-variable sym val (- 1 calendar-column-width)))
  426. :type 'integer
  427. :version "23.1")
  428. ;; FIXME a format specifier instead?
  429. (defcustom calendar-day-digit-width 2
  430. "Width of the day digits in the calendar. Minimum value is 2."
  431. :group 'calendar
  432. :initialize 'custom-initialize-default
  433. :set (lambda (sym val)
  434. (calendar-set-layout-variable sym val 2))
  435. :type 'integer
  436. :version "23.1")
  437. (defcustom calendar-intermonth-header nil
  438. "Header text to display in the space to the left of each calendar month.
  439. See `calendar-intermonth-text'."
  440. :group 'calendar
  441. :initialize 'custom-initialize-default
  442. :risky t
  443. :set (lambda (sym val)
  444. (set sym val)
  445. (calendar-redraw))
  446. :type '(choice (const nil :tag "Nothing")
  447. (string :tag "Fixed string")
  448. (sexp :value
  449. (propertize "WK" 'font-lock-face
  450. 'font-lock-function-name-face)))
  451. :version "23.1")
  452. (defcustom calendar-intermonth-text nil
  453. "Text to display in the space to the left of each calendar month.
  454. Can be nil, a fixed string, or a lisp expression that returns a string.
  455. When the expression is evaluated, the variables DAY, MONTH and YEAR
  456. are integers appropriate for the first day in each week.
  457. Will be truncated to the smaller of `calendar-left-margin' and
  458. `calendar-intermonth-spacing'. The last character is forced to be a space.
  459. For example, to display the ISO week numbers:
  460. (setq calendar-week-start-day 1
  461. calendar-intermonth-text
  462. '(propertize
  463. (format \"%2d\"
  464. (car
  465. (calendar-iso-from-absolute
  466. (calendar-absolute-from-gregorian (list month day year)))))
  467. 'font-lock-face 'font-lock-function-name-face))
  468. See also `calendar-intermonth-header'."
  469. :group 'calendar
  470. :initialize 'custom-initialize-default
  471. :risky t
  472. :set (lambda (sym val)
  473. (set sym val)
  474. (calendar-redraw))
  475. :type '(choice (const nil :tag "Nothing")
  476. (string :tag "Fixed string")
  477. (sexp :value
  478. (propertize
  479. (format "%2d"
  480. (car
  481. (calendar-iso-from-absolute
  482. (calendar-absolute-from-gregorian
  483. (list month day year)))))
  484. 'font-lock-face 'font-lock-function-name-face)))
  485. :version "23.1")
  486. (defcustom diary-file "~/diary"
  487. "Name of the file in which one's personal diary of dates is kept.
  488. The file's entries are lines beginning with any of the forms
  489. specified by the variable `diary-date-forms', which by default
  490. uses the forms of `diary-american-date-forms':
  491. MONTH/DAY
  492. MONTH/DAY/YEAR
  493. MONTHNAME DAY
  494. MONTHNAME DAY, YEAR
  495. DAYNAME
  496. with the remainder of the line being the diary entry string for
  497. that date. MONTH and DAY are one or two digit numbers, YEAR is a
  498. number and may be written in full or abbreviated to the final two
  499. digits (if `diary-abbreviated-year-flag' is non-nil). MONTHNAME
  500. and DAYNAME can be spelled in full (as specified by the variables
  501. `calendar-month-name-array' and `calendar-day-name-array'), or
  502. abbreviated (as specified by `calendar-month-abbrev-array' and
  503. `calendar-day-abbrev-array') with or without a period. Case is
  504. ignored. Any of DAY, MONTH, or MONTHNAME, YEAR can be `*' which
  505. matches any day, month, or year, respectively. If the date does
  506. not contain a year, it is generic and applies to any year. A
  507. DAYNAME entry applies to the appropriate day of the week in every week.
  508. You can customize `diary-date-forms' to your preferred format.
  509. Three default styles are provided: `diary-american-date-forms',
  510. `diary-european-date-forms', and `diary-iso-date-forms'.
  511. You can choose between these by setting `calendar-date-style' in your
  512. init file, or by using `calendar-set-date-style' when in the calendar.
  513. A diary entry can be preceded by the character `diary-nonmarking-symbol'
  514. \(ordinarily `&') to make that entry nonmarking--that is, it will not be
  515. marked on dates in the calendar window but will appear in a diary window.
  516. Multiline diary entries are made by indenting lines after the first with
  517. either a TAB or one or more spaces.
  518. Lines not in one the above formats are ignored. Here are some sample diary
  519. entries (in the default American style):
  520. 12/22/1988 Twentieth wedding anniversary!!
  521. &1/1. Happy New Year!
  522. 10/22 Ruth's birthday.
  523. 21: Payday
  524. Tuesday--weekly meeting with grad students at 10am
  525. Supowit, Shen, Bitner, and Kapoor to attend.
  526. 1/13/89 Friday the thirteenth!!
  527. &thu 4pm squash game with Lloyd.
  528. mar 16 Dad's birthday
  529. April 15, 1989 Income tax due.
  530. &* 15 time cards due.
  531. If the first line of a diary entry consists only of the date or day name with
  532. no trailing blanks or punctuation, then that line is not displayed in the
  533. diary window; only the continuation lines is shown. For example, the
  534. single diary entry
  535. 02/11/1989
  536. Bill Blattner visits Princeton today
  537. 2pm Cognitive Studies Committee meeting
  538. 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
  539. 4:00pm Jamie Tappenden
  540. 7:30pm Dinner at George and Ed's for Alan Ryan
  541. 7:30-10:00pm dance at Stewart Country Day School
  542. will appear in the diary window without the date line at the beginning. This
  543. facility allows the diary window to look neater, but can cause confusion if
  544. used with more than one day's entries displayed.
  545. Diary entries can be based on Lisp sexps. For example, the diary entry
  546. %%(diary-block 11 1 1990 11 10 1990) Vacation
  547. causes the diary entry \"Vacation\" to appear from November 1 through
  548. November 10, 1990. See the documentation for the function
  549. `diary-list-sexp-entries' for more details.
  550. Diary entries based on the Hebrew, the Islamic and/or the Bahá
  551. calendar are also possible, but because these are somewhat slow, they
  552. are ignored unless you set the `diary-nongregorian-listing-hook' and
  553. the `diary-nongregorian-marking-hook' appropriately. See the
  554. documentation of these hooks for details.
  555. Diary files can contain directives to include the contents of other files; for
  556. details, see the documentation for the variable `diary-list-entries-hook'."
  557. :type 'file
  558. :group 'diary)
  559. ;; FIXME do these have to be single characters?
  560. (defcustom diary-nonmarking-symbol "&"
  561. "Symbol indicating that a diary entry is not to be marked in the calendar."
  562. :type 'string
  563. :group 'diary)
  564. (define-obsolete-variable-alias 'hebrew-diary-entry-symbol
  565. 'diary-hebrew-entry-symbol "23.1")
  566. (defcustom diary-hebrew-entry-symbol "H"
  567. "Symbol indicating a diary entry according to the Hebrew calendar."
  568. :type 'string
  569. :group 'diary)
  570. (define-obsolete-variable-alias 'islamic-diary-entry-symbol
  571. 'diary-islamic-entry-symbol "23.1")
  572. (defcustom diary-islamic-entry-symbol "I"
  573. "Symbol indicating a diary entry according to the Islamic calendar."
  574. :type 'string
  575. :group 'diary)
  576. (define-obsolete-variable-alias 'bahai-diary-entry-symbol
  577. 'diary-bahai-entry-symbol "23.1")
  578. (defcustom diary-bahai-entry-symbol "B"
  579. "Symbol indicating a diary entry according to the Bahá'í calendar."
  580. :type 'string
  581. :group 'diary)
  582. (defcustom european-calendar-style nil
  583. "Non-nil means use the European style of dates in the diary and display.
  584. In this case, a date like 1/2/1990 would be interpreted as
  585. February 1, 1990. See `diary-european-date-forms' for the
  586. default European diary date styles.
  587. Setting this variable directly does not take effect (if the
  588. calendar package is already loaded). Rather, use either
  589. \\[customize] or the function `calendar-set-date-style'."
  590. :type 'boolean
  591. ;; Without :initialize (require 'calendar) throws an error because
  592. ;; calendar-set-date-style is undefined at this point.
  593. :initialize 'custom-initialize-default
  594. :set (lambda (symbol value)
  595. (if value
  596. (calendar-set-date-style 'european)
  597. (calendar-set-date-style 'american)))
  598. :group 'calendar)
  599. (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
  600. ;; If this is autoloaded, c-d-s gets set before any customization of e-c-s.
  601. (defcustom calendar-date-style (if european-calendar-style 'european
  602. 'american)
  603. "Your preferred style for writing dates.
  604. The options are:
  605. `american' - month/day/year
  606. `european' - day/month/year
  607. `iso' - year/month/day
  608. This affects how dates written in your diary are interpreted.
  609. It also affects date display, as well as those calendar and diary
  610. functions that take a date as an argument, e.g. `diary-date', by
  611. changing the order in which the arguments are interpreted.
  612. Setting this variable directly does not take effect (if the
  613. calendar package is already loaded). Rather, use either
  614. \\[customize] or the function `calendar-set-date-style'."
  615. :version "23.1"
  616. :type '(choice (const american :tag "Month/Day/Year")
  617. (const european :tag "Day/Month/Year")
  618. (const iso :tag "Year/Month/Day"))
  619. :initialize 'custom-initialize-default
  620. :set (lambda (symbol value)
  621. (calendar-set-date-style value))
  622. :group 'calendar)
  623. ;; Next three are provided to aid in setting diary-date-forms.
  624. ;; FIXME move to diary-lib?
  625. (defcustom diary-iso-date-forms
  626. '((month "[-/]" day "[^-/0-9]")
  627. (year "[-/]" month "[-/]" day "[^0-9]")
  628. ;; Cannot allow [-/] as separators here, since it would also match
  629. ;; the first element (bug#7377).
  630. (monthname " *" day "[^-0-9]")
  631. (year " *" monthname " *" day "[^0-9]")
  632. (dayname "\\W"))
  633. "List of pseudo-patterns describing the ISO style of dates.
  634. The defaults are: MONTH[-/]DAY; YEAR[-/]MONTH[-/]DAY; MONTHNAME DAY;
  635. YEAR MONTHNAME DAY; DAYNAME. Normally you should not customize this,
  636. but `diary-date-forms' (which see)."
  637. :version "23.3" ; bug#7377
  638. :type '(repeat (choice (cons :tag "Backup"
  639. :value (backup . nil)
  640. (const backup)
  641. (repeat (list :inline t :format "%v"
  642. (symbol :tag "Keyword")
  643. (choice symbol regexp))))
  644. (repeat (list :inline t :format "%v"
  645. (symbol :tag "Keyword")
  646. (choice symbol regexp)))))
  647. :group 'diary)
  648. (define-obsolete-variable-alias 'american-date-diary-pattern
  649. 'diary-american-date-forms "23.1")
  650. (defcustom diary-american-date-forms
  651. '((month "/" day "[^/0-9]")
  652. (month "/" day "/" year "[^0-9]")
  653. (monthname " *" day "[^,0-9]")
  654. (monthname " *" day ", *" year "[^0-9]")
  655. (dayname "\\W"))
  656. "List of pseudo-patterns describing the American style of dates.
  657. The defaults are: MONTH/DAY; MONTH/DAY/YEAR; MONTHNAME DAY;
  658. MONTHNAME DAY, YEAR; DAYNAME. Normally you should not customize this,
  659. but `diary-date-forms' (which see)."
  660. :type '(repeat (choice (cons :tag "Backup"
  661. :value (backup . nil)
  662. (const backup)
  663. (repeat (list :inline t :format "%v"
  664. (symbol :tag "Keyword")
  665. (choice symbol regexp))))
  666. (repeat (list :inline t :format "%v"
  667. (symbol :tag "Keyword")
  668. (choice symbol regexp)))))
  669. :group 'diary)
  670. (define-obsolete-variable-alias 'european-date-diary-pattern
  671. 'diary-european-date-forms "23.1")
  672. (defcustom diary-european-date-forms
  673. '((day "/" month "[^/0-9]")
  674. (day "/" month "/" year "[^0-9]")
  675. (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|\\([0-9]+[:aApP]\\)\\)")
  676. (day " *" monthname " *" year "[^0-9]")
  677. (dayname "\\W"))
  678. "List of pseudo-patterns describing the European style of dates.
  679. The defaults are: DAY/MONTH; DAY/MONTH/YEAR; DAY MONTHNAME;
  680. DAY MONTHNAME YEAR; DAYNAME. Normally you should not customize this, but
  681. `diary-date-forms' (which see)."
  682. :type '(repeat (choice (cons :tag "Backup"
  683. :value (backup . nil)
  684. (const backup)
  685. (repeat (list :inline t :format "%v"
  686. (symbol :tag "Keyword")
  687. (choice symbol regexp))))
  688. (repeat (list :inline t :format "%v"
  689. (symbol :tag "Keyword")
  690. (choice symbol regexp)))))
  691. :group 'diary)
  692. (defvar diary-font-lock-keywords)
  693. (defcustom diary-date-forms (cond ((eq calendar-date-style 'iso)
  694. diary-iso-date-forms)
  695. ((eq calendar-date-style 'european)
  696. diary-european-date-forms)
  697. (t diary-american-date-forms))
  698. "List of pseudo-patterns describing the forms of date used in the diary.
  699. The patterns on the list must be MUTUALLY EXCLUSIVE and should not match
  700. any portion of the diary entry itself, just the date component.
  701. A pseudo-pattern is a list of regular expressions and the keywords `month',
  702. `day', `year', `monthname', and `dayname'. The keyword `monthname' will
  703. match the name of the month (see `calendar-month-name-array'), capitalized
  704. or not, or its user-specified abbreviation (see `calendar-month-abbrev-array'),
  705. followed by a period or not; it will also match `*'. Similarly, `dayname'
  706. will match the name of the day (see `calendar-day-name-array'), capitalized or
  707. not, or its user-specified abbreviation (see `calendar-day-abbrev-array'),
  708. followed by a period or not. The keywords `month', `day', and `year' will
  709. match those numerical values, preceded by arbitrarily many zeros; they will
  710. also match `*'.
  711. The matching of the diary entries with the date forms is done with the
  712. standard syntax table from Fundamental mode, but with the `*' changed so
  713. that it is a word constituent.
  714. If, to be mutually exclusive, a pseudo-pattern must match a portion of the
  715. diary entry itself, the first element of the pattern MUST be `backup'. This
  716. directive causes the date recognizer to back up to the beginning of the
  717. current word of the diary entry, so in no case can the pattern match more than
  718. a portion of the first word of the diary entry.
  719. For examples of three common styles, see `diary-american-date-forms',
  720. `diary-european-date-forms', and `diary-iso-date-forms'."
  721. :type '(repeat (choice (cons :tag "Backup"
  722. :value (backup . nil)
  723. (const backup)
  724. (repeat (list :inline t :format "%v"
  725. (symbol :tag "Keyword")
  726. (choice symbol regexp))))
  727. (repeat (list :inline t :format "%v"
  728. (symbol :tag "Keyword")
  729. (choice symbol regexp)))))
  730. :set-after '(calendar-date-style diary-iso-date-forms
  731. diary-european-date-forms
  732. diary-american-date-forms)
  733. :initialize 'custom-initialize-default
  734. :set (lambda (symbol value)
  735. (unless (equal value (eval symbol))
  736. (custom-set-default symbol value)
  737. (setq diary-font-lock-keywords (diary-font-lock-keywords))
  738. ;; Need to redraw not just to get new font-locking, but also
  739. ;; to pick up any newly recognized entries.
  740. (and (diary-live-p)
  741. (diary))))
  742. :group 'diary)
  743. ;; Next three are provided to aid in setting calendar-date-display-form.
  744. (defcustom calendar-iso-date-display-form '((format "%s-%.2d-%.2d" year
  745. (string-to-number month)
  746. (string-to-number day)))
  747. "Pseudo-pattern governing the way a date appears in the ISO style.
  748. Normally you should not customize this, but `calendar-date-display-form'
  749. \(which see)."
  750. :type 'sexp
  751. :version "23.1"
  752. :group 'calendar)
  753. (define-obsolete-variable-alias 'european-calendar-display-form
  754. 'calendar-european-date-display-form "23.1")
  755. (defcustom calendar-european-date-display-form
  756. '((if dayname (concat dayname ", ")) day " " monthname " " year)
  757. "Pseudo-pattern governing the way a date appears in the European style.
  758. Normally you should not customize this, but `calendar-date-display-form'
  759. \(which see)."
  760. :type 'sexp
  761. :group 'calendar)
  762. (define-obsolete-variable-alias 'american-calendar-display-form
  763. 'calendar-american-date-display-form "23.1")
  764. (defcustom calendar-american-date-display-form
  765. '((if dayname (concat dayname ", ")) monthname " " day ", " year)
  766. "Pseudo-pattern governing the way a date appears in the American style.
  767. Normally you should not customize this, but `calendar-date-display-form'
  768. \(which see)."
  769. :type 'sexp
  770. :group 'calendar)
  771. (defcustom calendar-date-display-form
  772. (cond ((eq calendar-date-style 'iso)
  773. calendar-iso-date-display-form)
  774. ((eq calendar-date-style 'european)
  775. calendar-european-date-display-form)
  776. (t calendar-american-date-display-form))
  777. "Pseudo-pattern governing the way a calendar date appears.
  778. Used by the function `calendar-date-string' (which see), a pseudo-pattern
  779. is a list of expressions that can involve the keywords `month', `day',
  780. and `year' (all numbers in string form), and `monthname' and `dayname'
  781. \(both alphabetic strings). For example, a typical American form would be
  782. '(month \"/\" day \"/\" (substring year -2))
  783. whereas
  784. '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
  785. would give the usual American style in fixed-length fields. The variables
  786. `calendar-iso-date-display-form', `calendar-european-date-display-form', and
  787. `calendar-american-date-display-form' provide some defaults for three common
  788. styles."
  789. :type 'sexp
  790. :set-after '(calendar-date-style calendar-iso-date-display-form
  791. calendar-european-date-display-form
  792. calendar-american-date-display-form)
  793. :group 'calendar)
  794. (defcustom calendar-american-month-header
  795. '(propertize (format "%s %d" (calendar-month-name month) year)
  796. 'font-lock-face 'font-lock-function-name-face)
  797. "Default format for calendar month headings with the American date style.
  798. Normally you should not customize this, but `calender-month-header'."
  799. :group 'calendar
  800. :risky t
  801. :type 'sexp
  802. :version "24.3")
  803. (defcustom calendar-european-month-header
  804. '(propertize (format "%s %d" (calendar-month-name month) year)
  805. 'font-lock-face 'font-lock-function-name-face)
  806. "Default format for calendar month headings with the European date style.
  807. Normally you should not customize this, but `calender-month-header'."
  808. :group 'calendar
  809. :risky t
  810. :type 'sexp
  811. :version "24.3")
  812. (defcustom calendar-iso-month-header
  813. '(propertize (format "%d %s" year (calendar-month-name month))
  814. 'font-lock-face 'font-lock-function-name-face)
  815. "Default format for calendar month headings with the ISO date style.
  816. Normally you should not customize this, but `calender-month-header'."
  817. :group 'calendar
  818. :risky t
  819. :type 'sexp
  820. :version "24.3")
  821. (defcustom calendar-month-header
  822. (cond ((eq calendar-date-style 'iso)
  823. calendar-iso-month-header)
  824. ((eq calendar-date-style 'european)
  825. calendar-european-month-header)
  826. (t calendar-american-month-header))
  827. "Expression to evaluate to return the calendar month headings.
  828. When this expression is evaluated, the variables MONTH and YEAR are
  829. integers appropriate to the relevant month. The result is padded
  830. to the width of `calendar-month-digit-width'.
  831. For examples of three common styles, see `calendar-american-month-header',
  832. `calendar-european-month-header', and `calendar-iso-month-header'.
  833. Changing this variable without using customize has no effect on
  834. pre-existing calendar windows."
  835. :group 'calendar
  836. :initialize 'custom-initialize-default
  837. :risky t
  838. :set (lambda (sym val)
  839. (set sym val)
  840. (calendar-redraw))
  841. :set-after '(calendar-date-style calendar-american-month-header
  842. calendar-european-month-header
  843. calendar-iso-month-header)
  844. :type 'sexp
  845. :version "24.3")
  846. (defun calendar-set-date-style (style)
  847. "Set the style of calendar and diary dates to STYLE (a symbol).
  848. The valid styles are described in the documentation of `calendar-date-style'."
  849. (interactive (list (intern
  850. (completing-read "Date style: "
  851. '("american" "european" "iso") nil t
  852. nil nil "american"))))
  853. (or (memq style '(american european iso))
  854. (setq style 'american))
  855. (setq calendar-date-style style
  856. calendar-date-display-form
  857. (symbol-value (intern-soft
  858. (format "calendar-%s-date-display-form" style)))
  859. calendar-month-header
  860. (symbol-value (intern-soft (format "calendar-%s-month-header" style)))
  861. diary-date-forms
  862. (symbol-value (intern-soft (format "diary-%s-date-forms" style))))
  863. (calendar-redraw)
  864. (calendar-update-mode-line))
  865. (defun european-calendar ()
  866. "Set the interpretation and display of dates to the European style."
  867. (declare (obsolete calendar-set-date-style "23.1"))
  868. (interactive)
  869. (calendar-set-date-style 'european))
  870. (defun american-calendar ()
  871. "Set the interpretation and display of dates to the American style."
  872. (declare (obsolete calendar-set-date-style "23.1"))
  873. (interactive)
  874. (calendar-set-date-style 'american))
  875. (define-obsolete-variable-alias 'holidays-in-diary-buffer
  876. 'diary-show-holidays-flag "23.1")
  877. (defcustom diary-show-holidays-flag t
  878. "Non-nil means include holidays in the diary display.
  879. The holidays appear in the mode line of the diary buffer, or in the
  880. fancy diary buffer next to the date. This slows down the diary functions
  881. somewhat; setting it to nil makes the diary display faster."
  882. :type 'boolean
  883. :group 'holidays)
  884. (defcustom calendar-debug-sexp nil
  885. "Turn debugging on when evaluating a sexp in the diary or holiday list."
  886. :type 'boolean
  887. :group 'calendar)
  888. (define-obsolete-variable-alias 'all-hebrew-calendar-holidays
  889. 'calendar-hebrew-all-holidays-flag "23.1")
  890. (defcustom calendar-hebrew-all-holidays-flag nil
  891. "If nil, show only major holidays from the Hebrew calendar.
  892. This means only those Jewish holidays that appear on secular calendars.
  893. Otherwise, show all the holidays that would appear in a complete Hebrew
  894. calendar."
  895. :type 'boolean
  896. :group 'holidays)
  897. (define-obsolete-variable-alias 'all-christian-calendar-holidays
  898. 'calendar-christian-all-holidays-flag "23.1")
  899. (defcustom calendar-christian-all-holidays-flag nil
  900. "If nil, show only major holidays from the Christian calendar.
  901. This means only those Christian holidays that appear on secular calendars.
  902. Otherwise, show all the holidays that would appear in a complete Christian
  903. calendar."
  904. :type 'boolean
  905. :group 'holidays)
  906. (define-obsolete-variable-alias 'all-islamic-calendar-holidays
  907. 'calendar-islamic-all-holidays-flag "23.1")
  908. (defcustom calendar-islamic-all-holidays-flag nil
  909. "If nil, show only major holidays from the Islamic calendar.
  910. This means only those Islamic holidays that appear on secular calendars.
  911. Otherwise, show all the holidays that would appear in a complete Islamic
  912. calendar."
  913. :type 'boolean
  914. :group 'holidays)
  915. (define-obsolete-variable-alias 'all-bahai-calendar-holidays
  916. 'calendar-bahai-all-holidays-flag "23.1")
  917. (defcustom calendar-bahai-all-holidays-flag nil
  918. "If nil, show only major holidays from the Bahá'í calendar.
  919. These are the days on which work and school must be suspended.
  920. Otherwise, show all the holidays that would appear in a complete Bahá'í
  921. calendar."
  922. :type 'boolean
  923. :group 'holidays)
  924. (defcustom calendar-chinese-all-holidays-flag nil
  925. "If nil, show only the major holidays from the Chinese calendar."
  926. :version "23.1"
  927. :type 'boolean
  928. :group 'holidays)
  929. ;;; End of user options.
  930. (calendar-recompute-layout-variables)
  931. (defconst calendar-first-date-row 3
  932. "First row in the calendar with actual dates.")
  933. (defconst calendar-buffer "*Calendar*"
  934. "Name of the buffer used for the calendar.")
  935. (defconst holiday-buffer "*Holidays*"
  936. "Name of the buffer used for the displaying the holidays.")
  937. (defconst diary-fancy-buffer "*Fancy Diary Entries*"
  938. "Name of the buffer used for the optional fancy display of the diary.")
  939. (define-obsolete-variable-alias 'fancy-diary-buffer 'diary-fancy-buffer "23.1")
  940. (defconst calendar-other-calendars-buffer "*Other Calendars*"
  941. "Name of the buffer used for the display of date on other calendars.")
  942. (defconst lunar-phases-buffer "*Phases of Moon*"
  943. "Name of the buffer used for the lunar phases.")
  944. (defconst solar-sunrises-buffer "*Sunrise/Sunset Times*"
  945. "Name of buffer used for sunrise/sunset times.")
  946. (defconst calendar-hebrew-yahrzeit-buffer "*Yahrzeits*"
  947. "Name of the buffer used by `list-yahrzeit-dates'.")
  948. (defmacro calendar-increment-month (mon yr n &optional nmonths)
  949. "Increment the variables MON and YR by N months.
  950. Forward if N is positive or backward if N is negative.
  951. A negative YR is interpreted as BC; -1 being 1 BC, and so on.
  952. Optional NMONTHS is the number of months per year (default 12)."
  953. ;; Can view this as a form of base-nmonths arithmetic, in which "a
  954. ;; year" = "ten", and we never bother to use hundreds.
  955. `(let ((nmonths (or ,nmonths 12))
  956. macro-y)
  957. (if (< ,yr 0) (setq ,yr (1+ ,yr))) ; -1 BC -> 0 AD, etc
  958. (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
  959. ,mon (1+ (mod macro-y nmonths))
  960. ,yr (/ macro-y nmonths))
  961. ;; Alternative:
  962. ;;; (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
  963. ;;; ,yr (/ macro-y nmonths)
  964. ;;; ,mon (- macro-y (* ,yr nmonths)))
  965. (and (< macro-y 0) (> ,mon 1) (setq ,yr (1- ,yr)))
  966. (if (< ,yr 1) (setq ,yr (1- ,yr))))) ; 0 AD -> -1 BC, etc
  967. (define-obsolete-function-alias 'increment-calendar-month
  968. 'calendar-increment-month "23.1")
  969. (defvar displayed-month)
  970. (defvar displayed-year)
  971. (defun calendar-increment-month-cons (n &optional mon yr)
  972. "Return the Nth month after MON/YR.
  973. The return value is a pair (MONTH . YEAR).
  974. MON defaults to `displayed-month'. YR defaults to `displayed-year'."
  975. (unless mon (setq mon displayed-month))
  976. (unless yr (setq yr displayed-year))
  977. (calendar-increment-month mon yr n)
  978. (cons mon yr))
  979. (defmacro calendar-for-loop (var from init to final do &rest body)
  980. "Execute a for loop.
  981. Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
  982. inclusive. The standard macro `dotimes' is preferable in most cases."
  983. (declare (obsolete "use `dotimes' or `while' instead." "23.1")
  984. (debug (symbolp "from" form "to" form "do" body))
  985. (indent defun))
  986. `(let ((,var (1- ,init)))
  987. (while (>= ,final (setq ,var (1+ ,var)))
  988. ,@body)))
  989. (defmacro calendar-sum (index initial condition expression)
  990. "For INDEX = INITIAL, +1, ... (as long as CONDITION holds), sum EXPRESSION."
  991. (declare (debug (symbolp form form form)))
  992. `(let ((,index ,initial)
  993. (sum 0))
  994. (while ,condition
  995. (setq sum (+ sum ,expression)
  996. ,index (1+ ,index)))
  997. sum))
  998. (defmacro calendar-in-read-only-buffer (buffer &rest body)
  999. "Switch to BUFFER and execute the forms in BODY.
  1000. First creates or erases BUFFER as needed. Leaves BUFFER read-only,
  1001. with disabled undo. Leaves point at point-min, displays BUFFER."
  1002. (declare (indent 1) (debug t))
  1003. `(progn
  1004. (set-buffer (get-buffer-create ,buffer))
  1005. (or (derived-mode-p 'special-mode) (special-mode))
  1006. (setq buffer-read-only nil
  1007. buffer-undo-list t)
  1008. (erase-buffer)
  1009. ,@body
  1010. (goto-char (point-min))
  1011. (set-buffer-modified-p nil)
  1012. (setq buffer-read-only t)
  1013. (display-buffer ,buffer)))
  1014. ;; The following are in-line for speed; they can be called thousands of times
  1015. ;; when looking up holidays or processing the diary. Here, for example, are
  1016. ;; the numbers of calls to calendar/diary/holiday functions in preparing the
  1017. ;; fancy diary display, for a moderately complex diary file, with functions
  1018. ;; used instead of macros. There were a total of 10000 such calls:
  1019. ;;
  1020. ;; 1934 calendar-extract-month
  1021. ;; 1852 calendar-extract-year
  1022. ;; 1819 calendar-extract-day
  1023. ;; 845 calendar-leap-year-p
  1024. ;; 837 calendar-day-number
  1025. ;; 775 calendar-absolute-from-gregorian
  1026. ;; 346 calendar-last-day-of-month
  1027. ;; 286 calendar-hebrew-last-day-of-month
  1028. ;; 188 calendar-hebrew-leap-year-p
  1029. ;; 180 calendar-hebrew-elapsed-days
  1030. ;; 163 calendar-hebrew-last-month-of-year
  1031. ;; 66 calendar-date-compare
  1032. ;; 65 calendar-hebrew-days-in-year
  1033. ;; 60 calendar-julian-to-absolute
  1034. ;; 50 calendar-hebrew-to-absolute
  1035. ;; 43 calendar-date-equal
  1036. ;; 38 calendar-gregorian-from-absolute
  1037. ;; .
  1038. ;;
  1039. ;; The use of these seven macros eliminates the overhead of 92% of the function
  1040. ;; calls; it's faster this way.
  1041. (defsubst calendar-extract-month (date)
  1042. "Extract the month part of DATE which has the form (month day year)."
  1043. (car date))
  1044. (define-obsolete-function-alias 'extract-calendar-month
  1045. 'calendar-extract-month "23.1")
  1046. ;; Note gives wrong answer for result of (calendar-read-date 'noday),
  1047. ;; but that is only used by `calendar-other-month'.
  1048. (defsubst calendar-extract-day (date)
  1049. "Extract the day part of DATE which has the form (month day year)."
  1050. (cadr date))
  1051. (define-obsolete-function-alias 'extract-calendar-day
  1052. 'calendar-extract-day "23.1")
  1053. (defsubst calendar-extract-year (date)
  1054. "Extract the year part of DATE which has the form (month day year)."
  1055. (nth 2 date))
  1056. (define-obsolete-function-alias 'extract-calendar-year
  1057. 'calendar-extract-year "23.1")
  1058. (defsubst calendar-leap-year-p (year)
  1059. "Return t if YEAR is a Gregorian leap year.
  1060. A negative year is interpreted as BC; -1 being 1 BC, and so on."
  1061. ;; 1 BC = 0 AD, 2 BC acts like 1 AD, etc.
  1062. (if (< year 0) (setq year (1- (abs year))))
  1063. (and (zerop (% year 4))
  1064. (or (not (zerop (% year 100)))
  1065. (zerop (% year 400)))))
  1066. ;; The foregoing is a bit faster, but not as clear as the following:
  1067. ;;
  1068. ;;(defsubst calendar-leap-year-p (year)
  1069. ;; "Return t if YEAR is a Gregorian leap year."
  1070. ;; (or
  1071. ;; (and (zerop (% year 4))
  1072. ;; (not (zerop (% year 100))))
  1073. ;; (zerop (% year 400)))
  1074. (defsubst calendar-last-day-of-month (month year)
  1075. "The last day in MONTH during YEAR."
  1076. (if (and (= month 2) (calendar-leap-year-p year))
  1077. 29
  1078. (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  1079. ;; An explanation of the calculation can be found in PascAlgorithms by
  1080. ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
  1081. (defsubst calendar-day-number (date)
  1082. "Return the day number within the year of the date DATE.
  1083. For example, (calendar-day-number '(1 1 1987)) returns the value 1,
  1084. while (calendar-day-number '(12 31 1980)) returns 366."
  1085. (let* ((month (calendar-extract-month date))
  1086. (day (calendar-extract-day date))
  1087. (year (calendar-extract-year date))
  1088. (day-of-year (+ day (* 31 (1- month)))))
  1089. (when (> month 2)
  1090. (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
  1091. (if (calendar-leap-year-p year)
  1092. (setq day-of-year (1+ day-of-year))))
  1093. day-of-year))
  1094. (defsubst calendar-absolute-from-gregorian (date)
  1095. "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
  1096. The Gregorian date Sunday, December 31, 1 BC is imaginary.
  1097. DATE is a list of the form (month day year). A negative year is
  1098. interpreted as BC; -1 being 1 BC, and so on. Dates before 12/31/1 BC
  1099. return negative results."
  1100. (let ((year (calendar-extract-year date))
  1101. offset-years)
  1102. (cond ((zerop year)
  1103. (error "There was no year zero"))
  1104. ((> year 0)
  1105. (setq offset-years (1- year))
  1106. (+ (calendar-day-number date) ; days this year
  1107. (* 365 offset-years) ; + days in prior years
  1108. (/ offset-years 4) ; + Julian leap years
  1109. (- (/ offset-years 100)) ; - century years
  1110. (/ offset-years 400))) ; + Gregorian leap years
  1111. (t
  1112. ;; Years between date and 1 BC, excluding 1 BC (1 for 2 BC, etc).
  1113. (setq offset-years (abs (1+ year)))
  1114. (- (calendar-day-number date)
  1115. (* 365 offset-years)
  1116. (/ offset-years 4)
  1117. (- (/ offset-years 100))
  1118. (/ offset-years 400)
  1119. (calendar-day-number '(12 31 -1))))))) ; days in year 1 BC
  1120. ;;;###autoload
  1121. (defun calendar (&optional arg)
  1122. "Display a three-month Gregorian calendar.
  1123. The three months appear side by side, with the current month in
  1124. the middle surrounded by the previous and next months. The
  1125. cursor is put on today's date. If optional prefix argument ARG
  1126. is non-nil, prompts for the central month and year.
  1127. Once in the calendar window, future or past months can be moved
  1128. into view. Arbitrary months can be displayed, or the calendar
  1129. can be scrolled forward or backward. The cursor can be moved
  1130. forward or backward by one day, one week, one month, or one year.
  1131. All of these commands take prefix arguments which, when negative,
  1132. cause movement in the opposite direction. For convenience, the
  1133. digit keys and the minus sign are automatically prefixes. Use
  1134. \\[describe-mode] for details of the key bindings in the calendar
  1135. window.
  1136. Displays the calendar in a separate window, or optionally in a
  1137. separate frame, depending on the value of `calendar-setup'.
  1138. If `calendar-view-diary-initially-flag' is non-nil, also displays the
  1139. diary entries for the current date (or however many days
  1140. `diary-number-of-entries' specifies). This variable can be
  1141. overridden by `calendar-setup'. As well as being displayed,
  1142. diary entries can also be marked on the calendar (see
  1143. `calendar-mark-diary-entries-flag').
  1144. Runs the following hooks:
  1145. `calendar-load-hook' - after loading calendar.el
  1146. `calendar-today-visible-hook', `calendar-today-invisible-hook' - after
  1147. generating a calendar, if today's date is visible or not, respectively
  1148. `calendar-initial-window-hook' - after first creating a calendar
  1149. This function is suitable for execution in an init file."
  1150. (interactive "P")
  1151. ;; Avoid loading cal-x unless it will be used.
  1152. (if (and (memq calendar-setup '(one-frame two-frames calendar-only))
  1153. (display-multi-frame-p))
  1154. (calendar-frame-setup calendar-setup arg)
  1155. (calendar-basic-setup arg)))
  1156. (defun calendar-basic-setup (&optional arg nodisplay)
  1157. "Create a three-month calendar.
  1158. If optional prefix argument ARG is non-nil, prompts for the month
  1159. and year, else uses the current date. If NODISPLAY is non-nil, don't
  1160. display the generated calendar."
  1161. (interactive "P")
  1162. (let ((buff (current-buffer)))
  1163. (set-buffer (get-buffer-create calendar-buffer))
  1164. (calendar-mode)
  1165. (let* ((pop-up-windows t)
  1166. ;; Not really needed now, but means we use exactly the same
  1167. ;; behavior as before in the non-wide case (see below).
  1168. (split-height-threshold 1000)
  1169. (split-width-threshold calendar-split-width-threshold)
  1170. (date (if arg (calendar-read-date t)
  1171. (calendar-current-date)))
  1172. (month (calendar-extract-month date))
  1173. (year (calendar-extract-year date)))
  1174. (calendar-increment-month month year (- calendar-offset))
  1175. ;; Display the buffer before calling calendar-generate-window so that it
  1176. ;; can get a chance to adjust the window sizes to the frame size.
  1177. (unless nodisplay
  1178. ;; We want a window configuration that looks something like
  1179. ;; X X | Y
  1180. ;; - -----
  1181. ;; C Z | C
  1182. ;; where C is the calendar, and the LHS is the traditional,
  1183. ;; non-wide frame, and the RHS is the wide frame case.
  1184. ;; We should end up in the same state regardless of whether the
  1185. ;; windows were initially split or not.
  1186. ;; Previously, we only thought about the non-wide case.
  1187. ;; We could just set split-height-threshold to 1000, relying on
  1188. ;; the fact that the window splitting treated a single window as
  1189. ;; a special case and would always split it (vertically). The
  1190. ;; same thing does not work in the wide-frame case, so now we do
  1191. ;; the splitting by hand.
  1192. ;; See discussion in bug#1806.
  1193. ;; Actually, this still does not do quite the right thing in the
  1194. ;; wide frame case if started from a configuration like the LHS.
  1195. ;; Eg if you start with a non-wide frame, call calendar, then
  1196. ;; make the frame wider. This one is problematic because you
  1197. ;; might need to split a totally unrelated window. Oh well, it
  1198. ;; seems unlikely, and perhaps respecting the original layout is
  1199. ;; the right thing in that case.
  1200. ;;
  1201. ;; Is this a wide frame? If so, split it horizontally.
  1202. (if (window-splittable-p t) (split-window-right))
  1203. (pop-to-buffer calendar-buffer)
  1204. ;; Has the window already been split vertically?
  1205. (when (and (not (window-dedicated-p))
  1206. (window-full-height-p))
  1207. (let ((win (split-window-below)))
  1208. ;; In the upper window, show whatever was visible before.
  1209. ;; This looks better than using other-buffer.
  1210. (switch-to-buffer buff)
  1211. ;; Switch to the lower window with the calendar buffer.
  1212. (select-window win))))
  1213. (calendar-generate-window month year)
  1214. (if (and calendar-view-diary-initially-flag
  1215. (calendar-date-is-visible-p date))
  1216. (diary-view-entries))))
  1217. (if calendar-view-holidays-initially-flag
  1218. (let* ((diary-buffer (get-file-buffer diary-file))
  1219. (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
  1220. (split-height-threshold (if diary-window 2 1000)))
  1221. ;; FIXME display buffer?
  1222. (calendar-list-holidays)))
  1223. (run-hooks 'calendar-initial-window-hook))
  1224. (defun calendar-generate-window (&optional mon yr)
  1225. "Generate the calendar window for the current date.
  1226. Optional integers MON and YR are used instead of today's date."
  1227. (let* ((inhibit-read-only t)
  1228. (today (calendar-current-date))
  1229. (month (calendar-extract-month today))
  1230. (day (calendar-extract-day today))
  1231. (year (calendar-extract-year today))
  1232. (today-visible (or (not mon)
  1233. (<= (abs (calendar-interval mon yr month year)) 1)))
  1234. (in-calendar-window (eq (window-buffer (selected-window))
  1235. (get-buffer calendar-buffer))))
  1236. (calendar-generate (or mon month) (or yr year))
  1237. (calendar-update-mode-line)
  1238. (calendar-cursor-to-visible-date
  1239. (if today-visible today (list displayed-month 1 displayed-year)))
  1240. (set-buffer-modified-p nil)
  1241. ;; Don't do any window-related stuff if we weren't called from a
  1242. ;; window displaying the calendar.
  1243. (when in-calendar-window
  1244. (if (window-combined-p)
  1245. ;; Adjust the window to exactly fit the displayed calendar.
  1246. (fit-window-to-buffer nil nil calendar-minimum-window-height)
  1247. ;; For a full height window or a window that is horizontally
  1248. ;; combined don't fit height to that of its buffer.
  1249. (set-window-vscroll nil 0))
  1250. (sit-for 0))
  1251. (and (bound-and-true-p font-lock-mode)
  1252. (font-lock-fontify-buffer))
  1253. (and calendar-mark-holidays-flag
  1254. ;;; (calendar-date-is-valid-p today) ; useful for BC dates
  1255. (calendar-mark-holidays)
  1256. (and in-calendar-window (sit-for 0)))
  1257. (unwind-protect
  1258. (if calendar-mark-diary-entries-flag (diary-mark-entries))
  1259. (if today-visible
  1260. (run-hooks 'calendar-today-visible-hook)
  1261. (run-hooks 'calendar-today-invisible-hook)))))
  1262. (defun calendar-generate (month year)
  1263. "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
  1264. ;; A negative YEAR is interpreted as BC; -1 being 1 BC, and so on.
  1265. ;; Note that while calendars for years BC could be displayed as it
  1266. ;; stands, almost all other calendar functions (eg holidays) would
  1267. ;; at best have unpredictable results for such dates.
  1268. (if (< (+ month (* 12 (1- year))) 2)
  1269. (error "Months before January, 1 AD cannot be displayed"))
  1270. (setq displayed-month month
  1271. displayed-year year)
  1272. (erase-buffer)
  1273. (calendar-increment-month month year -1)
  1274. (dotimes (i 3)
  1275. (calendar-generate-month month year
  1276. (+ calendar-left-margin
  1277. (* calendar-month-width i)))
  1278. (calendar-increment-month month year 1)))
  1279. (defun calendar-move-to-column (indent)
  1280. "Like `move-to-column', but indents if the line is too short."
  1281. (if (< (move-to-column indent) indent)
  1282. (indent-to indent)))
  1283. (defun calendar-ensure-newline ()
  1284. "Move to the next line, adding a newline if necessary."
  1285. (or (zerop (forward-line 1))
  1286. (insert "\n")))
  1287. (defun calendar-insert-at-column (indent string truncate)
  1288. "Move to column INDENT, adding spaces as needed.
  1289. Inserts STRING so that it ends at INDENT. STRING is either a
  1290. literal string, or a sexp to evaluate to return such. Truncates
  1291. STRING to length TRUNCATE, and ensures a trailing space."
  1292. (if (not (ignore-errors (stringp (setq string (eval string)))))
  1293. (calendar-move-to-column indent)
  1294. (if (> (string-width string) truncate)
  1295. (setq string (truncate-string-to-width string truncate)))
  1296. (or (string-match " $" string)
  1297. (setq string (concat (if (= (string-width string) truncate)
  1298. (substring string 0 -1)
  1299. string)
  1300. ;; Avoid inserting text properties unless
  1301. ;; we have to (ie, non-unit-width chars).
  1302. ;; This is by no means essential.
  1303. (if (= (string-width string) (length string))
  1304. " "
  1305. ;; Cribbed from buff-menu.el.
  1306. (propertize
  1307. " " 'display `(space :align-to ,indent))))))
  1308. (calendar-move-to-column (- indent (string-width string)))
  1309. (insert string)))
  1310. (defun calendar-generate-month (month year indent)
  1311. "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
  1312. The calendar is inserted at the top of the buffer in which point is currently
  1313. located, but indented INDENT spaces. The indentation is done from the first
  1314. character on the line and does not disturb the first INDENT characters on the
  1315. line."
  1316. (let ((blank-days ; at start of month
  1317. (mod
  1318. (- (calendar-day-of-week (list month 1 year))
  1319. calendar-week-start-day)
  1320. 7))
  1321. (last (calendar-last-day-of-month month year))
  1322. (trunc (min calendar-intermonth-spacing
  1323. (1- calendar-left-margin)))
  1324. (day 1)
  1325. string)
  1326. (goto-char (point-min))
  1327. (calendar-move-to-column indent)
  1328. (insert
  1329. (calendar-string-spread (list calendar-month-header)
  1330. ?\s calendar-month-digit-width))
  1331. (calendar-ensure-newline)
  1332. (calendar-insert-at-column indent calendar-intermonth-header trunc)
  1333. ;; Use the first two characters of each day to head the columns.
  1334. (dotimes (i 7)
  1335. (insert
  1336. (progn
  1337. (setq string
  1338. (calendar-day-name (mod (+ calendar-week-start-day i) 7) nil t))
  1339. (if enable-multibyte-characters
  1340. (truncate-string-to-width string calendar-day-header-width)
  1341. (substring string 0 calendar-day-header-width)))
  1342. (make-string (- calendar-column-width calendar-day-header-width) ?\s)))
  1343. (calendar-ensure-newline)
  1344. (calendar-insert-at-column indent calendar-intermonth-text trunc)
  1345. ;; Add blank days before the first of the month.
  1346. (insert (make-string (* blank-days calendar-column-width) ?\s))
  1347. ;; Put in the days of the month.
  1348. (dotimes (i last)
  1349. (setq day (1+ i))
  1350. ;; TODO should numbers be left-justified, centered...?
  1351. (insert (format (format "%%%dd%%s" calendar-day-digit-width) day
  1352. (make-string
  1353. (- calendar-column-width calendar-day-digit-width) ?\s)))
  1354. ;; 'date property prevents intermonth text confusing re-searches.
  1355. ;; (Tried intangible, it did not really work.)
  1356. (set-text-properties
  1357. (- (point) (1+ calendar-day-digit-width)) (1- (point))
  1358. `(mouse-face highlight help-echo ,(eval calendar-date-echo-text)
  1359. date t))
  1360. (when (and (zerop (mod (+ day blank-days) 7))
  1361. (/= day last))
  1362. (calendar-ensure-newline)
  1363. (setq day (1+ day)) ; first day of next week
  1364. (calendar-insert-at-column indent calendar-intermonth-text trunc)))))
  1365. (defun calendar-redraw ()
  1366. "Redraw the calendar display, if `calendar-buffer' is live."
  1367. (interactive)
  1368. (if (get-buffer calendar-buffer)
  1369. (with-current-buffer calendar-buffer
  1370. (let ((cursor-date (calendar-cursor-to