PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/lisp/calendar/calendar.el

http://github.com/jave/emacs
Emacs Lisp | 1551 lines | 1156 code | 190 blank | 205 comment | 43 complexity | 99b4e74ef736a6b5540900bde496d307 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. ;;; calendar.el --- calendar functions
  2. ;; Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1997,
  3. ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  6. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  7. ;; Keywords: calendar
  8. ;; Human-Keywords: calendar, Gregorian calendar, diary, holidays
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; This collection of functions implements a calendar window. It
  22. ;; generates a calendar for the current month, together with the
  23. ;; previous and coming months, or for any other three-month period.
  24. ;; The calendar can be scrolled forward and backward in the window to
  25. ;; show months in the past or future; the cursor can move forward and
  26. ;; backward by days, weeks, or months, making it possible, for
  27. ;; instance, to jump to the date a specified number of days, weeks, or
  28. ;; months from the date under the cursor. The user can display a list
  29. ;; of holidays and other notable days for the period shown; the
  30. ;; notable days can be marked on the calendar, if desired. The user
  31. ;; can also specify that dates having corresponding diary entries (in
  32. ;; a file that the user specifies) be marked; the diary entries for
  33. ;; any date can be viewed in a separate window. The diary and the
  34. ;; notable days can be viewed independently of the calendar. Dates
  35. ;; can be translated from the (usual) Gregorian calendar to the day of
  36. ;; the year/days remaining in year, to the ISO commercial calendar, to
  37. ;; the Julian (old style) calendar, to the Hebrew calendar, to the
  38. ;; Islamic calendar, to the Baha'i calendar, to the French
  39. ;; Revolutionary calendar, to the Mayan calendar, to the Chinese
  40. ;; calendar, to the Coptic calendar, to the Ethiopic calendar, and to
  41. ;; the astronomical (Julian) day number. Times of sunrise/sunset can
  42. ;; be displayed, as can the phases of the moon. Appointment
  43. ;; notification for diary entries is available. Calendar printing via
  44. ;; LaTeX is available.
  45. ;; The following files are part of the calendar/diary code:
  46. ;; appt.el Appointment notification
  47. ;; cal-bahai.el Baha'i calendar
  48. ;; cal-china.el Chinese calendar
  49. ;; cal-coptic.el Coptic/Ethiopic calendars
  50. ;; cal-dst.el Daylight saving time rules
  51. ;; cal-french.el French revolutionary calendar
  52. ;; cal-hebrew.el Hebrew calendar
  53. ;; cal-html.el Calendars in HTML
  54. ;; cal-islam.el Islamic calendar
  55. ;; cal-iso.el ISO calendar
  56. ;; cal-julian.el Julian/astronomical calendars
  57. ;; cal-mayan.el Mayan calendars
  58. ;; cal-menu.el Menu support
  59. ;; cal-move.el Movement in the calendar
  60. ;; cal-persia.el Persian calendar
  61. ;; cal-tex.el Calendars in LaTeX
  62. ;; cal-x.el Dedicated frame functions
  63. ;; calendar.el This file
  64. ;; diary-lib.el Diary functions
  65. ;; holidays.el Holiday functions
  66. ;; lunar.el Phases of the moon
  67. ;; solar.el Sunrise/sunset, equinoxes/solstices
  68. ;; Technical details of all the calendrical calculations can be found in
  69. ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
  70. ;; and Nachum Dershowitz, Cambridge University Press (2001).
  71. ;; An earlier version of the technical details appeared in
  72. ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
  73. ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
  74. ;; pages 899-928, and in ``Calendrical Calculations, Part II: Three Historical
  75. ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
  76. ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
  77. ;; pages 383-404.
  78. ;; Hard copies of these two papers can be obtained by sending email to
  79. ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
  80. ;; the message BODY containing your mailing address (snail).
  81. ;; A note on free variables:
  82. ;; The calendar passes around a few dynamically bound variables, which
  83. ;; unfortunately have rather common names. They are meant to be
  84. ;; available for external functions, so the names can't be changed.
  85. ;; displayed-month, displayed-year: bound in calendar-generate, the
  86. ;; central month of the 3 month calendar window
  87. ;; original-date, number: bound in diary-list-entries, the arguments
  88. ;; with which that function was called.
  89. ;; date, entry: bound in diary-list-sexp-entries (qv)
  90. ;; Bound in diary-list-entries:
  91. ;; diary-entries-list: use in d-l, appt.el, and by diary-add-to-list
  92. ;; diary-saved-point: only used in diary-lib.el, passed to the display func
  93. ;; date-string: only used in diary-lib.el
  94. ;; list-only: don't modify the diary-buffer, just return a list of entries
  95. ;; file-glob-attrs: yuck
  96. ;;; Code:
  97. (require 'cal-loaddefs)
  98. ;; Avoid recursive load of calendar when loading cal-menu. Yuck.
  99. (provide 'calendar)
  100. (require 'cal-menu)
  101. (defgroup calendar nil
  102. "Calendar and time management support."
  103. :prefix "calendar-"
  104. :group 'applications)
  105. (defgroup calendar-hooks nil
  106. "Calendar hooks."
  107. :prefix "calendar-"
  108. :group 'calendar)
  109. (defgroup calendar-faces nil
  110. "Calendar faces."
  111. :prefix "calendar-"
  112. :group 'calendar)
  113. (defcustom calendar-offset 0
  114. "The offset of the principal month from the center of the calendar window.
  115. 0 means the principal month is in the center (default), -1 means on the left,
  116. +1 means on the right. Larger (or smaller) values push the principal month off
  117. the screen."
  118. :type 'integer
  119. :group 'calendar)
  120. (defcustom calendar-setup nil
  121. "The frame setup of the calendar.
  122. The choices are: `one-frame' (calendar and diary together in one separate,
  123. dedicated frame); `two-frames' (calendar and diary in separate, dedicated
  124. frames); `calendar-only' (calendar in a separate, dedicated frame); with
  125. any other value the current frame is used. Using any of the first
  126. three options overrides the value of `calendar-view-diary-initially-flag'."
  127. :type '(choice
  128. (const :tag "calendar and diary in separate frame" one-frame)
  129. (const :tag "calendar and diary each in own frame" two-frames)
  130. (const :tag "calendar in separate frame" calendar-only)
  131. (const :tag "use current frame" nil))
  132. :group 'calendar)
  133. (defcustom calendar-minimum-window-height 8
  134. "Minimum height `calendar-generate-window' should use for calendar window."
  135. :type 'integer
  136. :version "22.1"
  137. :group 'calendar)
  138. (defcustom calendar-week-start-day 0
  139. "The day of the week on which a week in the calendar begins.
  140. 0 means Sunday (default), 1 means Monday, and so on.
  141. If you change this variable directly (without using customize)
  142. after starting `calendar', you should call `calendar-redraw' to
  143. update the calendar display to reflect the change, otherwise
  144. movement commands will not work correctly."
  145. :type 'integer
  146. ;; Change the initialize so that if you reload calendar.el, it will not
  147. ;; cause a redraw (which may fail, e.g. with "invalid byte-code in
  148. ;; calendar.elc" because of the "byte-compile-dynamic").
  149. :initialize 'custom-initialize-default
  150. :set (lambda (sym val)
  151. (set sym val)
  152. (calendar-redraw))
  153. :group 'calendar)
  154. (define-obsolete-variable-alias 'view-diary-entries-initially
  155. 'calendar-view-diary-initially-flag "23.1")
  156. (defcustom calendar-view-diary-initially-flag nil
  157. "Non-nil means display current date's diary entries on entry to calendar.
  158. The diary is displayed in another window when the calendar is first displayed,
  159. if the current date is visible. The number of days of diary entries displayed
  160. is governed by the variable `diary-number-of-entries'. This variable can
  161. be overridden by the value of `calendar-setup'."
  162. :type 'boolean
  163. :group 'diary)
  164. (define-obsolete-variable-alias 'mark-diary-entries-in-calendar
  165. 'calendar-mark-diary-entries-flag "23.1")
  166. ;; FIXME :set
  167. (defcustom calendar-mark-diary-entries-flag nil
  168. "Non-nil means mark dates with diary entries, in the calendar window.
  169. The marking symbol is specified by the variable `diary-entry-marker'."
  170. :type 'boolean
  171. :group 'diary)
  172. (defcustom calendar-remove-frame-by-deleting t
  173. "Determine how the calendar mode removes a frame no longer needed.
  174. If nil, make an icon of the frame. If non-nil, delete the frame."
  175. :type 'boolean
  176. :version "23.1" ; changed from nil to t
  177. :group 'view
  178. :group 'calendar)
  179. (defface calendar-today
  180. '((t (:underline t)))
  181. "Face for indicating today's date in the calendar.
  182. See `calendar-today-marker'."
  183. :group 'calendar-faces)
  184. ;; Backward-compatibility alias. FIXME make obsolete.
  185. (put 'calendar-today-face 'face-alias 'calendar-today)
  186. (defface diary
  187. '((((min-colors 88) (class color) (background light))
  188. :foreground "red1")
  189. (((class color) (background light))
  190. :foreground "red")
  191. (((min-colors 88) (class color) (background dark))
  192. :foreground "yellow1")
  193. (((class color) (background dark))
  194. :foreground "yellow")
  195. (t
  196. :weight bold))
  197. "Face for highlighting diary entries.
  198. Used to mark diary entries in the calendar (see `diary-entry-marker'),
  199. and to highlight the date header in the fancy diary."
  200. :group 'calendar-faces)
  201. ;; Backward-compatibility alias. FIXME make obsolete.
  202. (put 'diary-face 'face-alias 'diary)
  203. (defface holiday
  204. '((((class color) (background light))
  205. :background "pink")
  206. (((class color) (background dark))
  207. :background "chocolate4")
  208. (t
  209. :inverse-video t))
  210. "Face for indicating in the calendar dates that have holidays.
  211. See `calendar-holiday-marker'."
  212. :group 'calendar-faces)
  213. ;; Backward-compatibility alias. FIXME make obsolete.
  214. (put 'holiday-face 'face-alias 'holiday)
  215. ;; These briefly checked font-lock-mode, but that is broken, since it
  216. ;; is a buffer-local variable, and which buffer happens to be current
  217. ;; when this file is loaded shouldn't make a difference. One could
  218. ;; perhaps check global-font-lock-mode, or font-lock-global-modes; but
  219. ;; this feature doesn't use font-lock, so there's no real reason it
  220. ;; should respect those either. See bug#2199.
  221. ;; They also used to check display-color-p, but that is a problem if
  222. ;; loaded from --daemon. Since BW displays are rare now, this was
  223. ;; also taken out. The way to keep it would be to have nil mean do a
  224. ;; runtime check whenever this variable is used.
  225. (defcustom diary-entry-marker 'diary
  226. "How to mark dates that have diary entries.
  227. The value can be either a single-character string (e.g. \"+\") or a face."
  228. :type '(choice (string :tag "Single character string") face)
  229. :group 'diary
  230. :version "23.1")
  231. (defcustom calendar-today-marker 'calendar-today
  232. "How to mark today's date in the calendar.
  233. The value can be either a single-character string (e.g. \"=\") or a face.
  234. Used by `calendar-mark-today'."
  235. :type '(choice (string :tag "Single character string") face)
  236. :group 'calendar
  237. :version "23.1")
  238. (defcustom calendar-holiday-marker 'holiday
  239. "How to mark notable dates in the calendar.
  240. The value can be either a single-character string (e.g. \"*\") or a face."
  241. :type '(choice (string :tag "Single character string") face)
  242. :group 'holidays
  243. :version "23.1")
  244. (define-obsolete-variable-alias 'view-calendar-holidays-initially
  245. 'calendar-view-holidays-initially-flag "23.1")
  246. (defcustom calendar-view-holidays-initially-flag nil
  247. "Non-nil means display holidays for current three month period on entry.
  248. The holidays are displayed in another window when the calendar is first
  249. displayed."
  250. :type 'boolean
  251. :group 'holidays)
  252. (define-obsolete-variable-alias 'mark-holidays-in-calendar
  253. 'calendar-mark-holidays-flag "23.1")
  254. ;; FIXME :set
  255. (defcustom calendar-mark-holidays-flag nil
  256. "Non-nil means mark dates of holidays in the calendar window.
  257. The marking symbol is specified by the variable `calendar-holiday-marker'."
  258. :type 'boolean
  259. :group 'holidays)
  260. (defcustom calendar-mode-hook nil
  261. "Hook run when entering `calendar-mode'."
  262. :type 'hook
  263. :group 'calendar-hooks)
  264. (defcustom calendar-load-hook nil
  265. "List of functions to be called after the calendar is first loaded.
  266. This is the place to add key bindings to `calendar-mode-map'."
  267. :type 'hook
  268. :group 'calendar-hooks)
  269. (define-obsolete-variable-alias 'initial-calendar-window-hook
  270. 'calendar-initial-window-hook "23.1")
  271. (defcustom calendar-initial-window-hook nil
  272. "List of functions to be called when the calendar window is created.
  273. Quitting the calendar and re-entering it will cause these functions
  274. to be called again."
  275. :type 'hook
  276. :group 'calendar-hooks)
  277. (define-obsolete-variable-alias 'today-visible-calendar-hook
  278. 'calendar-today-visible-hook "23.1")
  279. (defcustom calendar-today-visible-hook nil
  280. "List of functions called whenever the current date is visible.
  281. To mark today's date, add the function `calendar-mark-today'.
  282. To replace the date with asterisks, add the function `calendar-star-date'.
  283. See also `calendar-today-invisible-hook'.
  284. In general, be careful about changing characters in the calendar buffer,
  285. since it may cause the movement commands to fail."
  286. :type 'hook
  287. :options '(calendar-mark-today calendar-star-date)
  288. :group 'calendar-hooks)
  289. (define-obsolete-variable-alias 'today-invisible-calendar-hook
  290. 'calendar-today-invisible-hook "23.1")
  291. (defcustom calendar-today-invisible-hook nil
  292. "List of functions called whenever the current date is not visible.
  293. See also `calendar-today-visible-hook'."
  294. :type 'hook
  295. :group 'calendar-hooks)
  296. (defcustom calendar-move-hook nil
  297. "List of functions called whenever the cursor moves in the calendar.
  298. For example,
  299. (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
  300. redisplays the diary for whatever date the cursor is moved to."
  301. :type 'hook
  302. :options '(calendar-update-mode-line)
  303. :group 'calendar-hooks)
  304. (defcustom calendar-date-echo-text
  305. "mouse-2: general menu\nmouse-3: menu for this date"
  306. "String displayed when the cursor is over a date in the calendar.
  307. Can be either a fixed string, or a lisp expression that returns one.
  308. When this expression is evaluated, DAY, MONTH, and YEAR are
  309. integers appropriate to the relevant date. For example, to
  310. display the ISO date:
  311. (setq calendar-date-echo-text '(format \"ISO date: %s\"
  312. (calendar-iso-date-string
  313. (list month day year))))
  314. Changing this variable without using customize has no effect on
  315. pre-existing calendar windows."
  316. :group 'calendar
  317. :initialize 'custom-initialize-default
  318. :risky t
  319. :set (lambda (sym val)
  320. (set sym val)
  321. (calendar-redraw))
  322. :type '(choice (string :tag "Fixed string")
  323. (sexp :value
  324. (format "ISO date: %s"
  325. (calendar-iso-date-string
  326. (list month day year)))))
  327. :version "23.1")
  328. (defvar calendar-month-digit-width nil
  329. "Width of the region with numbers in each month in the calendar.")
  330. (defvar calendar-month-width nil
  331. "Full width of each month in the calendar.")
  332. (defvar calendar-right-margin nil
  333. "Right margin of the calendar.")
  334. (defvar calendar-month-edges nil
  335. "Alist of month edge columns.
  336. Each element has the form (N LEFT FIRST LAST RIGHT), where
  337. LEFT is the leftmost column associated with month segment N,
  338. FIRST and LAST are the first and last columns with day digits in,
  339. and LAST is the rightmost column.")
  340. (defun calendar-month-edges (segment)
  341. "Compute the month edge columns for month SEGMENT.
  342. Returns a list (LEFT FIRST LAST RIGHT), where LEFT is the
  343. leftmost column associated with a month, FIRST and LAST are the
  344. first and last columns with day digits in, and LAST is the
  345. rightmost column."
  346. ;; The leftmost column with a digit in it in this month segment.
  347. (let* ((first (+ calendar-left-margin
  348. (* segment calendar-month-width)))
  349. ;; The rightmost column with a digit in it in this month segment.
  350. (last (+ first (1- calendar-month-digit-width)))
  351. (left (if (eq segment 0)
  352. 0
  353. (+ calendar-left-margin
  354. (* segment calendar-month-width)
  355. (- (/ calendar-intermonth-spacing 2)))))
  356. ;; The rightmost edge of this month segment, dividing the
  357. ;; space between months in two.
  358. (right (+ calendar-left-margin
  359. (* (1+ segment) calendar-month-width)
  360. (- (/ calendar-intermonth-spacing 2)))))
  361. (list left first last right)))
  362. (defun calendar-recompute-layout-variables ()
  363. "Recompute some layout-related calendar \"constants\"."
  364. (setq calendar-month-digit-width (+ (* 6 calendar-column-width)
  365. calendar-day-digit-width)
  366. calendar-month-width (+ (* 7 calendar-column-width)
  367. calendar-intermonth-spacing)
  368. calendar-right-margin (+ calendar-left-margin
  369. (* 3 (* 7 calendar-column-width))
  370. (* 2 calendar-intermonth-spacing))
  371. calendar-month-edges nil)
  372. (dotimes (i 3)
  373. (push (cons i (calendar-month-edges i)) calendar-month-edges))
  374. (setq calendar-month-edges (reverse calendar-month-edges)))
  375. ;; FIXME add font-lock-keywords.
  376. (defun calendar-set-layout-variable (symbol value &optional minmax)
  377. "Set SYMBOL's value to VALUE, an integer.
  378. A positive/negative MINMAX enforces a minimum/maximum value.
  379. Then redraw the calendar, if necessary."
  380. (let ((oldvalue (symbol-value symbol)))
  381. (custom-set-default symbol (if minmax
  382. (if (< minmax 0)
  383. (min value (- minmax))
  384. (max value minmax))
  385. value))
  386. (unless (equal value oldvalue)
  387. (calendar-recompute-layout-variables)
  388. (calendar-redraw))))
  389. (defcustom calendar-left-margin 5
  390. "Empty space to the left of the first month in the calendar."
  391. :group 'calendar
  392. :initialize 'custom-initialize-default
  393. :set 'calendar-set-layout-variable
  394. :type 'integer
  395. :version "23.1")
  396. ;; Or you can view it as columns of width 2, with 1 space, no space
  397. ;; after the last column, and a 5 space gap between month.
  398. ;; FIXME check things work if this is odd.
  399. (defcustom calendar-intermonth-spacing 4
  400. "Space between months in the calendar. Minimum value is 1."
  401. :group 'calendar
  402. :initialize 'custom-initialize-default
  403. :set (lambda (sym val)
  404. (calendar-set-layout-variable sym val 1))
  405. :type 'integer
  406. :version "23.1")
  407. ;; FIXME calendar-month-column-width?
  408. (defcustom calendar-column-width 3
  409. "Width of each day column in the calendar. Minimum value is 3."
  410. :initialize 'custom-initialize-default
  411. :set (lambda (sym val)
  412. (calendar-set-layout-variable sym val 3))
  413. :type 'integer
  414. :version "23.1")
  415. (defcustom calendar-day-header-width 2
  416. "Width of the day column headers in the calendar.
  417. Must be at least one less than `calendar-column-width'."
  418. :group 'calendar
  419. :initialize 'custom-initialize-default
  420. :set (lambda (sym val)
  421. (calendar-set-layout-variable sym val (- 1 calendar-column-width)))
  422. :type 'integer
  423. :version "23.1")
  424. ;; FIXME a format specifier instead?
  425. (defcustom calendar-day-digit-width 2
  426. "Width of the day digits in the calendar. Minimum value is 2."
  427. :group 'calendar
  428. :initialize 'custom-initialize-default
  429. :set (lambda (sym val)
  430. (calendar-set-layout-variable sym val 2))
  431. :type 'integer
  432. :version "23.1")
  433. (defcustom calendar-intermonth-header nil
  434. "Header text display in the space to the left of each calendar month.
  435. See `calendar-intermonth-text'."
  436. :group 'calendar
  437. :initialize 'custom-initialize-default
  438. :risky t
  439. :set (lambda (sym val)
  440. (set sym val)
  441. (calendar-redraw))
  442. :type '(choice (const nil :tag "Nothing")
  443. (string :tag "Fixed string")
  444. (sexp :value
  445. (propertize "WK" 'font-lock-face
  446. 'font-lock-function-name-face)))
  447. :version "23.1")
  448. (defcustom calendar-intermonth-text nil
  449. "Text to display in the space to the left of each calendar month.
  450. Can be nil, a fixed string, or a lisp expression that returns a string.
  451. When the expression is evaluated, the variables DAY, MONTH and YEAR
  452. are integers appropriate for the first day in each week.
  453. Will be truncated to the smaller of `calendar-left-margin' and
  454. `calendar-intermonth-spacing'. The last character is forced to be a space.
  455. For example, to display the ISO week numbers:
  456. (setq calendar-week-start-day 1
  457. calendar-intermonth-text
  458. '(propertize
  459. (format \"%2d\"
  460. (car
  461. (calendar-iso-from-absolute
  462. (calendar-absolute-from-gregorian (list month day year)))))
  463. 'font-lock-face 'font-lock-function-name-face))
  464. See also `calendar-intermonth-header'."
  465. :group 'calendar
  466. :initialize 'custom-initialize-default
  467. :risky t
  468. :set (lambda (sym val)
  469. (set sym val)
  470. (calendar-redraw))
  471. :type '(choice (const nil :tag "Nothing")
  472. (string :tag "Fixed string")
  473. (sexp :value
  474. (propertize
  475. (format "%2d"
  476. (car
  477. (calendar-iso-from-absolute
  478. (calendar-absolute-from-gregorian
  479. (list month day year)))))
  480. 'font-lock-face 'font-lock-function-name-face)))
  481. :version "23.1")
  482. (defcustom diary-file "~/diary"
  483. "Name of the file in which one's personal diary of dates is kept.
  484. The file's entries are lines beginning with any of the forms
  485. specified by the variable `diary-date-forms', which by default
  486. uses the forms of `diary-american-date-forms':
  487. MONTH/DAY
  488. MONTH/DAY/YEAR
  489. MONTHNAME DAY
  490. MONTHNAME DAY, YEAR
  491. DAYNAME
  492. with the remainder of the line being the diary entry string for
  493. that date. MONTH and DAY are one or two digit numbers, YEAR is a
  494. number and may be written in full or abbreviated to the final two
  495. digits (if `diary-abbreviated-year-flag' is non-nil). MONTHNAME
  496. and DAYNAME can be spelled in full (as specified by the variables
  497. `calendar-month-name-array' and `calendar-day-name-array'), or
  498. abbreviated (as specified by `calendar-month-abbrev-array' and
  499. `calendar-day-abbrev-array') with or without a period. Case is
  500. ignored. Any of DAY, MONTH, or MONTHNAME, YEAR can be `*' which
  501. matches any day, month, or year, respectively. If the date does
  502. not contain a year, it is generic and applies to any year. A
  503. DAYNAME entry applies to the appropriate day of the week in every week.
  504. You can customize `diary-date-forms' to your preferred format.
  505. Three default styles are provided: `diary-american-date-forms',
  506. `diary-european-date-forms', and `diary-iso-date-forms'.
  507. You can choose between these by setting `calendar-date-style' in your
  508. .emacs file, or by using `calendar-set-date-style' when in the calendar.
  509. A diary entry can be preceded by the character `diary-nonmarking-symbol'
  510. \(ordinarily `&') to make that entry nonmarking--that is, it will not be
  511. marked on dates in the calendar window but will appear in a diary window.
  512. Multiline diary entries are made by indenting lines after the first with
  513. either a TAB or one or more spaces.
  514. Lines not in one the above formats are ignored. Here are some sample diary
  515. entries (in the default American style):
  516. 12/22/1988 Twentieth wedding anniversary!!
  517. &1/1. Happy New Year!
  518. 10/22 Ruth's birthday.
  519. 21: Payday
  520. Tuesday--weekly meeting with grad students at 10am
  521. Supowit, Shen, Bitner, and Kapoor to attend.
  522. 1/13/89 Friday the thirteenth!!
  523. &thu 4pm squash game with Lloyd.
  524. mar 16 Dad's birthday
  525. April 15, 1989 Income tax due.
  526. &* 15 time cards due.
  527. If the first line of a diary entry consists only of the date or day name with
  528. no trailing blanks or punctuation, then that line is not displayed in the
  529. diary window; only the continuation lines is shown. For example, the
  530. single diary entry
  531. 02/11/1989
  532. Bill Blattner visits Princeton today
  533. 2pm Cognitive Studies Committee meeting
  534. 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
  535. 4:00pm Jamie Tappenden
  536. 7:30pm Dinner at George and Ed's for Alan Ryan
  537. 7:30-10:00pm dance at Stewart Country Day School
  538. will appear in the diary window without the date line at the beginning. This
  539. facility allows the diary window to look neater, but can cause confusion if
  540. used with more than one day's entries displayed.
  541. Diary entries can be based on Lisp sexps. For example, the diary entry
  542. %%(diary-block 11 1 1990 11 10 1990) Vacation
  543. causes the diary entry \"Vacation\" to appear from November 1 through
  544. November 10, 1990. See the documentation for the function
  545. `diary-list-sexp-entries' for more details.
  546. Diary entries based on the Hebrew, the Islamic and/or the Baha'i
  547. calendar are also possible, but because these are somewhat slow, they
  548. are ignored unless you set the `diary-nongregorian-listing-hook' and
  549. the `diary-nongregorian-marking-hook' appropriately. See the
  550. documentation of these hooks for details.
  551. Diary files can contain directives to include the contents of other files; for
  552. details, see the documentation for the variable `diary-list-entries-hook'."
  553. :type 'file
  554. :group 'diary)
  555. ;; FIXME do these have to be single characters?
  556. (defcustom diary-nonmarking-symbol "&"
  557. "Symbol indicating that a diary entry is not to be marked in the calendar."
  558. :type 'string
  559. :group 'diary)
  560. (define-obsolete-variable-alias 'hebrew-diary-entry-symbol
  561. 'diary-hebrew-entry-symbol "23.1")
  562. (defcustom diary-hebrew-entry-symbol "H"
  563. "Symbol indicating a diary entry according to the Hebrew calendar."
  564. :type 'string
  565. :group 'diary)
  566. (define-obsolete-variable-alias 'islamic-diary-entry-symbol
  567. 'diary-islamic-entry-symbol "23.1")
  568. (defcustom diary-islamic-entry-symbol "I"
  569. "Symbol indicating a diary entry according to the Islamic calendar."
  570. :type 'string
  571. :group 'diary)
  572. (define-obsolete-variable-alias 'bahai-diary-entry-symbol
  573. 'diary-bahai-entry-symbol "23.1")
  574. (defcustom diary-bahai-entry-symbol "B"
  575. "Symbol indicating a diary entry according to the Baha'i calendar."
  576. :type 'string
  577. :group 'diary)
  578. (defcustom european-calendar-style nil
  579. "Non-nil means use the European style of dates in the diary and display.
  580. In this case, a date like 1/2/1990 would be interpreted as
  581. February 1, 1990. See `diary-european-date-forms' for the
  582. default European diary date styles.
  583. Setting this variable directly does not take effect (if the
  584. calendar package is already loaded). Rather, use either
  585. \\[customize] or the function `calendar-set-date-style'."
  586. :type 'boolean
  587. ;; Without :initialize (require 'calendar) throws an error because
  588. ;; calendar-set-date-style is undefined at this point.
  589. :initialize 'custom-initialize-default
  590. :set (lambda (symbol value)
  591. (if value
  592. (calendar-set-date-style 'european)
  593. (calendar-set-date-style 'american)))
  594. :group 'calendar)
  595. (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
  596. ;; If this is autoloaded, c-d-s gets set before any customization of e-c-s.
  597. (defcustom calendar-date-style (if european-calendar-style 'european
  598. 'american)
  599. "Your preferred style for writing dates.
  600. The options are:
  601. `american' - month/day/year
  602. `european' - day/month/year
  603. `iso' - year/month/day
  604. This affects how dates written in your diary are interpreted.
  605. It also affects date display, as well as those calendar and diary
  606. functions that take a date as an argument, e.g. `diary-date', by
  607. changing the order in which the arguments are interpreted.
  608. Setting this variable directly does not take effect (if the
  609. calendar package is already loaded). Rather, use either
  610. \\[customize] or the function `calendar-set-date-style'."
  611. :version "23.1"
  612. :type '(choice (const american :tag "Month/Day/Year")
  613. (const european :tag "Day/Month/Year")
  614. (const iso :tag "Year/Month/Day"))
  615. :initialize 'custom-initialize-default
  616. :set (lambda (symbol value)
  617. (calendar-set-date-style value))
  618. :group 'calendar)
  619. ;; Next three are provided to aid in setting diary-date-forms.
  620. ;; FIXME move to diary-lib?
  621. (defcustom diary-iso-date-forms
  622. '((month "[-/]" day "[^-/0-9]")
  623. (year "[-/]" month "[-/]" day "[^0-9]")
  624. (monthname "-" day "[^-0-9]")
  625. (year "-" monthname "-" day "[^0-9]")
  626. (dayname "\\W"))
  627. "List of pseudo-patterns describing the ISO style of dates.
  628. The defaults are: MONTH[-/]DAY; YEAR[-/]MONTH[-/]DAY; MONTHNAME-DAY;
  629. YEAR-MONTHNAME-DAY; DAYNAME. Normally you should not customize this,
  630. but `diary-date-forms' (which see)."
  631. :version "23.1"
  632. :type '(repeat (choice (cons :tag "Backup"
  633. :value (backup . nil)
  634. (const backup)
  635. (repeat (list :inline t :format "%v"
  636. (symbol :tag "Keyword")
  637. (choice symbol regexp))))
  638. (repeat (list :inline t :format "%v"
  639. (symbol :tag "Keyword")
  640. (choice symbol regexp)))))
  641. :group 'diary)
  642. (define-obsolete-variable-alias 'american-date-diary-pattern
  643. 'diary-american-date-forms "23.1")
  644. (defcustom diary-american-date-forms
  645. '((month "/" day "[^/0-9]")
  646. (month "/" day "/" year "[^0-9]")
  647. (monthname " *" day "[^,0-9]")
  648. (monthname " *" day ", *" year "[^0-9]")
  649. (dayname "\\W"))
  650. "List of pseudo-patterns describing the American style of dates.
  651. The defaults are: MONTH/DAY; MONTH/DAY/YEAR; MONTHNAME DAY;
  652. MONTHNAME DAY, YEAR; DAYNAME. Normally you should not customize this,
  653. but `diary-date-forms' (which see)."
  654. :type '(repeat (choice (cons :tag "Backup"
  655. :value (backup . nil)
  656. (const backup)
  657. (repeat (list :inline t :format "%v"
  658. (symbol :tag "Keyword")
  659. (choice symbol regexp))))
  660. (repeat (list :inline t :format "%v"
  661. (symbol :tag "Keyword")
  662. (choice symbol regexp)))))
  663. :group 'diary)
  664. (define-obsolete-variable-alias 'european-date-diary-pattern
  665. 'diary-european-date-forms "23.1")
  666. (defcustom diary-european-date-forms
  667. '((day "/" month "[^/0-9]")
  668. (day "/" month "/" year "[^0-9]")
  669. (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|\\([0-9]+[:aApP]\\)\\)")
  670. (day " *" monthname " *" year "[^0-9]")
  671. (dayname "\\W"))
  672. "List of pseudo-patterns describing the European style of dates.
  673. The defaults are: DAY/MONTH; DAY/MONTH/YEAR; DAY MONTHNAME;
  674. DAY MONTHNAME YEAR; DAYNAME. Normally you should not customize this, but
  675. `diary-date-forms' (which see)."
  676. :type '(repeat (choice (cons :tag "Backup"
  677. :value (backup . nil)
  678. (const backup)
  679. (repeat (list :inline t :format "%v"
  680. (symbol :tag "Keyword")
  681. (choice symbol regexp))))
  682. (repeat (list :inline t :format "%v"
  683. (symbol :tag "Keyword")
  684. (choice symbol regexp)))))
  685. :group 'diary)
  686. (defvar diary-font-lock-keywords)
  687. (defcustom diary-date-forms (cond ((eq calendar-date-style 'iso)
  688. diary-iso-date-forms)
  689. ((eq calendar-date-style 'european)
  690. diary-european-date-forms)
  691. (t diary-american-date-forms))
  692. "List of pseudo-patterns describing the forms of date used in the diary.
  693. The patterns on the list must be MUTUALLY EXCLUSIVE and should not match
  694. any portion of the diary entry itself, just the date component.
  695. A pseudo-pattern is a list of regular expressions and the keywords `month',
  696. `day', `year', `monthname', and `dayname'. The keyword `monthname' will
  697. match the name of the month (see `calendar-month-name-array'), capitalized
  698. or not, or its user-specified abbreviation (see `calendar-month-abbrev-array'),
  699. followed by a period or not; it will also match `*'. Similarly, `dayname'
  700. will match the name of the day (see `calendar-day-name-array'), capitalized or
  701. not, or its user-specified abbreviation (see `calendar-day-abbrev-array'),
  702. followed by a period or not. The keywords `month', `day', and `year' will
  703. match those numerical values, preceded by arbitrarily many zeros; they will
  704. also match `*'.
  705. The matching of the diary entries with the date forms is done with the
  706. standard syntax table from Fundamental mode, but with the `*' changed so
  707. that it is a word constituent.
  708. If, to be mutually exclusive, a pseudo-pattern must match a portion of the
  709. diary entry itself, the first element of the pattern MUST be `backup'. This
  710. directive causes the date recognizer to back up to the beginning of the
  711. current word of the diary entry, so in no case can the pattern match more than
  712. a portion of the first word of the diary entry.
  713. For examples of three common styles, see `diary-american-date-forms',
  714. `diary-european-date-forms', and `diary-iso-date-forms'."
  715. :type '(repeat (choice (cons :tag "Backup"
  716. :value (backup . nil)
  717. (const backup)
  718. (repeat (list :inline t :format "%v"
  719. (symbol :tag "Keyword")
  720. (choice symbol regexp))))
  721. (repeat (list :inline t :format "%v"
  722. (symbol :tag "Keyword")
  723. (choice symbol regexp)))))
  724. :set-after '(calendar-date-style diary-iso-date-forms
  725. diary-european-date-forms
  726. diary-american-date-forms)
  727. :initialize 'custom-initialize-default
  728. :set (lambda (symbol value)
  729. (unless (equal value (eval symbol))
  730. (custom-set-default symbol value)
  731. (setq diary-font-lock-keywords (diary-font-lock-keywords))
  732. ;; Need to redraw not just to get new font-locking, but also
  733. ;; to pick up any newly recognized entries.
  734. (and (diary-live-p)
  735. (diary))))
  736. :group 'diary)
  737. ;; Next three are provided to aid in setting calendar-date-display-form.
  738. (defcustom calendar-iso-date-display-form '((format "%s-%.2d-%.2d" year
  739. (string-to-number month)
  740. (string-to-number day)))
  741. "Pseudo-pattern governing the way a date appears in the ISO style.
  742. Normally you should not customize this, but `calendar-date-display-form'
  743. \(which see)."
  744. :type 'sexp
  745. :version "23.1"
  746. :group 'calendar)
  747. (define-obsolete-variable-alias 'european-calendar-display-form
  748. 'calendar-european-date-display-form "23.1")
  749. (defcustom calendar-european-date-display-form
  750. '((if dayname (concat dayname ", ")) day " " monthname " " year)
  751. "Pseudo-pattern governing the way a date appears in the European style.
  752. Normally you should not customize this, but `calendar-date-display-form'
  753. \(which see)."
  754. :type 'sexp
  755. :group 'calendar)
  756. (define-obsolete-variable-alias 'american-calendar-display-form
  757. 'calendar-american-date-display-form "23.1")
  758. (defcustom calendar-american-date-display-form
  759. '((if dayname (concat dayname ", ")) monthname " " day ", " year)
  760. "Pseudo-pattern governing the way a date appears in the American style.
  761. Normally you should not customize this, but `calendar-date-display-form'
  762. \(which see)."
  763. :type 'sexp
  764. :group 'calendar)
  765. (defcustom calendar-date-display-form
  766. (cond ((eq calendar-date-style 'iso)
  767. calendar-iso-date-display-form)
  768. ((eq calendar-date-style 'european)
  769. calendar-european-date-display-form)
  770. (t calendar-american-date-display-form))
  771. "Pseudo-pattern governing the way a calendar date appears.
  772. Used by the function `calendar-date-string' (which see), a pseudo-pattern
  773. is a list of expressions that can involve the keywords `month', `day',
  774. and `year' (all numbers in string form), and `monthname' and `dayname'
  775. \(both alphabetic strings). For example, a typical American form would be
  776. '(month \"/\" day \"/\" (substring year -2))
  777. whereas
  778. '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
  779. would give the usual American style in fixed-length fields. The variables
  780. `calendar-iso-date-display-form', `calendar-european-date-display-form', and
  781. `calendar-american-date-display-form' provide some defaults for three common
  782. styles."
  783. :type 'sexp
  784. :set-after '(calendar-date-style calendar-iso-date-display-form
  785. calendar-european-date-display-form
  786. calendar-american-date-display-form)
  787. :group 'calendar)
  788. (defun calendar-set-date-style (style)
  789. "Set the style of calendar and diary dates to STYLE (a symbol).
  790. The valid styles are described in the documentation of `calendar-date-style'."
  791. (interactive (list (intern
  792. (completing-read "Date style: "
  793. '("american" "european" "iso") nil t
  794. nil nil "american"))))
  795. (or (memq style '(american european iso))
  796. (setq style 'american))
  797. (setq calendar-date-style style
  798. calendar-date-display-form
  799. (symbol-value (intern-soft
  800. (format "calendar-%s-date-display-form" style)))
  801. diary-date-forms
  802. (symbol-value (intern-soft (format "diary-%s-date-forms" style))))
  803. (calendar-update-mode-line))
  804. (defun european-calendar ()
  805. "Set the interpretation and display of dates to the European style."
  806. (interactive)
  807. (calendar-set-date-style 'european))
  808. (make-obsolete 'european-calendar 'calendar-set-date-style "23.1")
  809. (defun american-calendar ()
  810. "Set the interpretation and display of dates to the American style."
  811. (interactive)
  812. (calendar-set-date-style 'american))
  813. (make-obsolete 'american-calendar 'calendar-set-date-style "23.1")
  814. (define-obsolete-variable-alias 'holidays-in-diary-buffer
  815. 'diary-show-holidays-flag "23.1")
  816. (defcustom diary-show-holidays-flag t
  817. "Non-nil means include holidays in the diary display.
  818. The holidays appear in the mode line of the diary buffer, or in the
  819. fancy diary buffer next to the date. This slows down the diary functions
  820. somewhat; setting it to nil makes the diary display faster."
  821. :type 'boolean
  822. :group 'holidays)
  823. (defcustom calendar-debug-sexp nil
  824. "Turn debugging on when evaluating a sexp in the diary or holiday list."
  825. :type 'boolean
  826. :group 'calendar)
  827. (define-obsolete-variable-alias 'all-hebrew-calendar-holidays
  828. 'calendar-hebrew-all-holidays-flag "23.1")
  829. (defcustom calendar-hebrew-all-holidays-flag nil
  830. "If nil, show only major holidays from the Hebrew calendar.
  831. This means only those Jewish holidays that appear on secular calendars.
  832. Otherwise, show all the holidays that would appear in a complete Hebrew
  833. calendar."
  834. :type 'boolean
  835. :group 'holidays)
  836. (define-obsolete-variable-alias 'all-christian-calendar-holidays
  837. 'calendar-christian-all-holidays-flag "23.1")
  838. (defcustom calendar-christian-all-holidays-flag nil
  839. "If nil, show only major holidays from the Christian calendar.
  840. This means only those Christian holidays that appear on secular calendars.
  841. Otherwise, show all the holidays that would appear in a complete Christian
  842. calendar."
  843. :type 'boolean
  844. :group 'holidays)
  845. (define-obsolete-variable-alias 'all-islamic-calendar-holidays
  846. 'calendar-islamic-all-holidays-flag "23.1")
  847. (defcustom calendar-islamic-all-holidays-flag nil
  848. "If nil, show only major holidays from the Islamic calendar.
  849. This means only those Islamic holidays that appear on secular calendars.
  850. Otherwise, show all the holidays that would appear in a complete Islamic
  851. calendar."
  852. :type 'boolean
  853. :group 'holidays)
  854. (define-obsolete-variable-alias 'all-bahai-calendar-holidays
  855. 'calendar-bahai-all-holidays-flag "23.1")
  856. (defcustom calendar-bahai-all-holidays-flag nil
  857. "If nil, show only major holidays from the Baha'i calendar.
  858. These are the days on which work and school must be suspended.
  859. Otherwise, show all the holidays that would appear in a complete Baha'i
  860. calendar."
  861. :type 'boolean
  862. :group 'holidays)
  863. (defcustom calendar-chinese-all-holidays-flag nil
  864. "If nil, show only the major holidays from the Chinese calendar."
  865. :version "23.1"
  866. :type 'boolean
  867. :group 'holidays)
  868. ;;; End of user options.
  869. (calendar-recompute-layout-variables)
  870. (defconst calendar-first-date-row 3
  871. "First row in the calendar with actual dates.")
  872. (defconst calendar-buffer "*Calendar*"
  873. "Name of the buffer used for the calendar.")
  874. (defconst holiday-buffer "*Holidays*"
  875. "Name of the buffer used for the displaying the holidays.")
  876. (defconst diary-fancy-buffer "*Fancy Diary Entries*"
  877. "Name of the buffer used for the optional fancy display of the diary.")
  878. (define-obsolete-variable-alias 'fancy-diary-buffer 'diary-fancy-buffer "23.1")
  879. (defconst calendar-other-calendars-buffer "*Other Calendars*"
  880. "Name of the buffer used for the display of date on other calendars.")
  881. (defconst lunar-phases-buffer "*Phases of Moon*"
  882. "Name of the buffer used for the lunar phases.")
  883. (defconst solar-sunrises-buffer "*Sunrise/Sunset Times*"
  884. "Name of buffer used for sunrise/sunset times.")
  885. (defconst calendar-hebrew-yahrzeit-buffer "*Yahrzeits*"
  886. "Name of the buffer used by `list-yahrzeit-dates'.")
  887. (defmacro calendar-increment-month (mon yr n &optional nmonths)
  888. "Increment the variables MON and YR by N months.
  889. Forward if N is positive or backward if N is negative.
  890. A negative YR is interpreted as BC; -1 being 1 BC, and so on.
  891. Optional NMONTHS is the number of months per year (default 12)."
  892. ;; Can view this as a form of base-nmonths arithmetic, in which "a
  893. ;; year" = "ten", and we never bother to use hundreds.
  894. `(let ((nmonths (or ,nmonths 12))
  895. macro-y)
  896. (if (< ,yr 0) (setq ,yr (1+ ,yr))) ; -1 BC -> 0 AD, etc
  897. (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
  898. ,mon (1+ (mod macro-y nmonths))
  899. ,yr (/ macro-y nmonths))
  900. ;; Alternative:
  901. ;;; (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
  902. ;;; ,yr (/ macro-y nmonths)
  903. ;;; ,mon (- macro-y (* ,yr nmonths)))
  904. (and (< macro-y 0) (> ,mon 1) (setq ,yr (1- ,yr)))
  905. (if (< ,yr 1) (setq ,yr (1- ,yr))))) ; 0 AD -> -1 BC, etc
  906. (define-obsolete-function-alias 'increment-calendar-month
  907. 'calendar-increment-month "23.1")
  908. (defvar displayed-month)
  909. (defvar displayed-year)
  910. (defun calendar-increment-month-cons (n &optional mon yr)
  911. "Return the Nth month after MON/YR.
  912. The return value is a pair (MONTH . YEAR).
  913. MON defaults to `displayed-month'. YR defaults to `displayed-year'."
  914. (unless mon (setq mon displayed-month))
  915. (unless yr (setq yr displayed-year))
  916. (calendar-increment-month mon yr n)
  917. (cons mon yr))
  918. (defmacro calendar-for-loop (var from init to final do &rest body)
  919. "Execute a for loop.
  920. Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
  921. inclusive. The standard macro `dotimes' is preferable in most cases."
  922. (declare (debug (symbolp "from" form "to" form "do" body))
  923. (indent defun))
  924. `(let ((,var (1- ,init)))
  925. (while (>= ,final (setq ,var (1+ ,var)))
  926. ,@body)))
  927. (make-obsolete 'calendar-for-loop "use `dotimes' or `while' instead." "23.1")
  928. (defmacro calendar-sum (index initial condition expression)
  929. "For INDEX = INITIAL, +1, ... (as long as CONDITION holds), sum EXPRESSION."
  930. (declare (debug (symbolp form form form)))
  931. `(let ((,index ,initial)
  932. (sum 0))
  933. (while ,condition
  934. (setq sum (+ sum ,expression)
  935. ,index (1+ ,index)))
  936. sum))
  937. ;; FIXME bind q to bury-buffer?
  938. (defmacro calendar-in-read-only-buffer (buffer &rest body)
  939. "Switch to BUFFER and executes the forms in BODY.
  940. First creates or erases BUFFER as needed. Leaves BUFFER read-only,
  941. with disabled undo. Leaves point at point-min, displays BUFFER."
  942. (declare (indent 1) (debug t))
  943. `(progn
  944. (set-buffer (get-buffer-create ,buffer))
  945. (setq buffer-read-only nil
  946. buffer-undo-list t)
  947. (erase-buffer)
  948. ,@body
  949. (goto-char (point-min))
  950. (set-buffer-modified-p nil)
  951. (setq buffer-read-only t)
  952. (display-buffer ,buffer)))
  953. ;; The following are in-line for speed; they can be called thousands of times
  954. ;; when looking up holidays or processing the diary. Here, for example, are
  955. ;; the numbers of calls to calendar/diary/holiday functions in preparing the
  956. ;; fancy diary display, for a moderately complex diary file, with functions
  957. ;; used instead of macros. There were a total of 10000 such calls:
  958. ;;
  959. ;; 1934 calendar-extract-month
  960. ;; 1852 calendar-extract-year
  961. ;; 1819 calendar-extract-day
  962. ;; 845 calendar-leap-year-p
  963. ;; 837 calendar-day-number
  964. ;; 775 calendar-absolute-from-gregorian
  965. ;; 346 calendar-last-day-of-month
  966. ;; 286 calendar-hebrew-last-day-of-month
  967. ;; 188 calendar-hebrew-leap-year-p
  968. ;; 180 calendar-hebrew-elapsed-days
  969. ;; 163 calendar-hebrew-last-month-of-year
  970. ;; 66 calendar-date-compare
  971. ;; 65 calendar-hebrew-days-in-year
  972. ;; 60 calendar-julian-to-absolute
  973. ;; 50 calendar-hebrew-to-absolute
  974. ;; 43 calendar-date-equal
  975. ;; 38 calendar-gregorian-from-absolute
  976. ;; .
  977. ;;
  978. ;; The use of these seven macros eliminates the overhead of 92% of the function
  979. ;; calls; it's faster this way.
  980. (defsubst calendar-extract-month (date)
  981. "Extract the month part of DATE which has the form (month day year)."
  982. (car date))
  983. (define-obsolete-function-alias 'extract-calendar-month
  984. 'calendar-extract-month "23.1")
  985. ;; Note gives wrong answer for result of (calendar-read-date 'noday),
  986. ;; but that is only used by `calendar-other-month'.
  987. (defsubst calendar-extract-day (date)
  988. "Extract the day part of DATE which has the form (month day year)."
  989. (cadr date))
  990. (define-obsolete-function-alias 'extract-calendar-day
  991. 'calendar-extract-day "23.1")
  992. (defsubst calendar-extract-year (date)
  993. "Extract the year part of DATE which has the form (month day year)."
  994. (nth 2 date))
  995. (define-obsolete-function-alias 'extract-calendar-year
  996. 'calendar-extract-year "23.1")
  997. (defsubst calendar-leap-year-p (year)
  998. "Return t if YEAR is a Gregorian leap year.
  999. A negative year is interpreted as BC; -1 being 1 BC, and so on."
  1000. ;; 1 BC = 0 AD, 2 BC acts like 1 AD, etc.
  1001. (if (< year 0) (setq year (1- (abs year))))
  1002. (and (zerop (% year 4))
  1003. (or (not (zerop (% year 100)))
  1004. (zerop (% year 400)))))
  1005. ;; The foregoing is a bit faster, but not as clear as the following:
  1006. ;;
  1007. ;;(defsubst calendar-leap-year-p (year)
  1008. ;; "Return t if YEAR is a Gregorian leap year."
  1009. ;; (or
  1010. ;; (and (zerop (% year 4))
  1011. ;; (not (zerop (% year 100))))
  1012. ;; (zerop (% year 400)))
  1013. (defsubst calendar-last-day-of-month (month year)
  1014. "The last day in MONTH during YEAR."
  1015. (if (and (= month 2) (calendar-leap-year-p year))
  1016. 29
  1017. (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  1018. ;; An explanation of the calculation can be found in PascAlgorithms by
  1019. ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
  1020. (defsubst calendar-day-number (date)
  1021. "Return the day number within the year of the date DATE.
  1022. For example, (calendar-day-number '(1 1 1987)) returns the value 1,
  1023. while (calendar-day-number '(12 31 1980)) returns 366."
  1024. (let* ((month (calendar-extract-month date))
  1025. (day (calendar-extract-day date))
  1026. (year (calendar-extract-year date))
  1027. (day-of-year (+ day (* 31 (1- month)))))
  1028. (when (> month 2)
  1029. (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
  1030. (if (calendar-leap-year-p year)
  1031. (setq day-of-year (1+ day-of-year))))
  1032. day-of-year))
  1033. (defsubst calendar-absolute-from-gregorian (date)
  1034. "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
  1035. The Gregorian date Sunday, December 31, 1 BC is imaginary.
  1036. DATE is a list of the form (month day year). A negative year is
  1037. interpreted as BC; -1 being 1 BC, and so on. Dates before 12/31/1 BC
  1038. return negative results."
  1039. (let ((year (calendar-extract-year date))
  1040. offset-years)
  1041. (cond ((zerop year)
  1042. (error "There was no year zero"))
  1043. ((> year 0)
  1044. (setq offset-years (1- year))
  1045. (+ (calendar-day-number date) ; days this year
  1046. (* 365 offset-years) ; + days in prior years
  1047. (/ offset-years 4) ; + Julian leap years
  1048. (- (/ offset-years 100)) ; - century years
  1049. (/ offset-years 400))) ; +

Large files files are truncated, but you can click here to view the full file