PageRenderTime 95ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/lisp/calendar/calendar.el

https://bitbucket.org/danchr/emacs
Emacs Lisp | 2570 lines | 1967 code | 271 blank | 332 comment | 69 complexity | 3c34c363f69052e3c30ea26108059aa2 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, 2010
  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. (load "cal-loaddefs" nil t)
  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. ;; See discussion in bug#1806.
  139. (defcustom calendar-split-width-threshold nil
  140. "Value to use for `split-width-threshold' when creating a calendar.
  141. This only affects frames wider than the default value of
  142. `split-width-threshold'."
  143. :type '(choice (const nil)
  144. (integer))
  145. :version "23.2"
  146. :group 'calendar)
  147. (defcustom calendar-week-start-day 0
  148. "The day of the week on which a week in the calendar begins.
  149. 0 means Sunday (default), 1 means Monday, and so on.
  150. If you change this variable directly (without using customize)
  151. after starting `calendar', you should call `calendar-redraw' to
  152. update the calendar display to reflect the change, otherwise
  153. movement commands will not work correctly."
  154. :type 'integer
  155. ;; Change the initialize so that if you reload calendar.el, it will not
  156. ;; cause a redraw (which may fail, e.g. with "invalid byte-code in
  157. ;; calendar.elc" because of the "byte-compile-dynamic").
  158. :initialize 'custom-initialize-default
  159. :set (lambda (sym val)
  160. (set sym val)
  161. (calendar-redraw))
  162. :group 'calendar)
  163. (define-obsolete-variable-alias 'view-diary-entries-initially
  164. 'calendar-view-diary-initially-flag "23.1")
  165. (defcustom calendar-view-diary-initially-flag nil
  166. "Non-nil means display current date's diary entries on entry to calendar.
  167. The diary is displayed in another window when the calendar is first displayed,
  168. if the current date is visible. The number of days of diary entries displayed
  169. is governed by the variable `diary-number-of-entries'. This variable can
  170. be overridden by the value of `calendar-setup'."
  171. :type 'boolean
  172. :group 'diary)
  173. (define-obsolete-variable-alias 'mark-diary-entries-in-calendar
  174. 'calendar-mark-diary-entries-flag "23.1")
  175. ;; FIXME :set
  176. (defcustom calendar-mark-diary-entries-flag nil
  177. "Non-nil means mark dates with diary entries, in the calendar window.
  178. The marking symbol is specified by the variable `diary-entry-marker'."
  179. :type 'boolean
  180. :group 'diary)
  181. (defcustom calendar-remove-frame-by-deleting t
  182. "Determine how the calendar mode removes a frame no longer needed.
  183. If nil, make an icon of the frame. If non-nil, delete the frame."
  184. :type 'boolean
  185. :version "23.1" ; changed from nil to t
  186. :group 'view
  187. :group 'calendar)
  188. (defface calendar-today
  189. '((t (:underline t)))
  190. "Face for indicating today's date in the calendar.
  191. See the variable `calendar-today-marker'."
  192. :group 'calendar-faces)
  193. (define-obsolete-face-alias 'calendar-today-face 'calendar-today "22.1")
  194. (defface diary
  195. '((((min-colors 88) (class color) (background light))
  196. :foreground "red1")
  197. (((class color) (background light))
  198. :foreground "red")
  199. (((min-colors 88) (class color) (background dark))
  200. :foreground "yellow1")
  201. (((class color) (background dark))
  202. :foreground "yellow")
  203. (t
  204. :weight bold))
  205. "Face for highlighting diary entries.
  206. Used to mark diary entries in the calendar (see `diary-entry-marker'),
  207. and to highlight the date header in the fancy diary."
  208. :group 'calendar-faces)
  209. (define-obsolete-face-alias 'diary-face 'diary "22.1")
  210. (defface holiday
  211. '((((class color) (background light))
  212. :background "pink")
  213. (((class color) (background dark))
  214. :background "chocolate4")
  215. (t
  216. :inverse-video t))
  217. "Face for indicating in the calendar dates that have holidays.
  218. See `calendar-holiday-marker'."
  219. :group 'calendar-faces)
  220. (define-obsolete-face-alias 'holiday-face 'holiday "22.1")
  221. ;; These briefly checked font-lock-mode, but that is broken, since it
  222. ;; is a buffer-local variable, and which buffer happens to be current
  223. ;; when this file is loaded shouldn't make a difference. One could
  224. ;; perhaps check global-font-lock-mode, or font-lock-global-modes; but
  225. ;; this feature doesn't use font-lock, so there's no real reason it
  226. ;; should respect those either. See bug#2199.
  227. ;; They also used to check display-color-p, but that is a problem if
  228. ;; loaded from --daemon. Since BW displays are rare now, this was
  229. ;; also taken out. The way to keep it would be to have nil mean do a
  230. ;; runtime check whenever this variable is used.
  231. (defcustom diary-entry-marker 'diary
  232. "How to mark dates that have diary entries.
  233. The value can be either a single-character string (e.g. \"+\") or a face."
  234. :type '(choice (string :tag "Single character string") face)
  235. :group 'diary
  236. :version "23.1")
  237. (defcustom calendar-today-marker 'calendar-today
  238. "How to mark today's date in the calendar.
  239. The value can be either a single-character string (e.g. \"=\") or a face.
  240. Used by `calendar-mark-today'."
  241. :type '(choice (string :tag "Single character string") face)
  242. :group 'calendar
  243. :version "23.1")
  244. (defcustom calendar-holiday-marker 'holiday
  245. "How to mark notable dates in the calendar.
  246. The value can be either a single-character string (e.g. \"*\") or a face."
  247. :type '(choice (string :tag "Single character string") face)
  248. :group 'holidays
  249. :version "23.1")
  250. (define-obsolete-variable-alias 'view-calendar-holidays-initially
  251. 'calendar-view-holidays-initially-flag "23.1")
  252. (defcustom calendar-view-holidays-initially-flag nil
  253. "Non-nil means display holidays for current three month period on entry.
  254. The holidays are displayed in another window when the calendar is first
  255. displayed."
  256. :type 'boolean
  257. :group 'holidays)
  258. (define-obsolete-variable-alias 'mark-holidays-in-calendar
  259. 'calendar-mark-holidays-flag "23.1")
  260. ;; FIXME :set
  261. (defcustom calendar-mark-holidays-flag nil
  262. "Non-nil means mark dates of holidays in the calendar window.
  263. The marking symbol is specified by the variable `calendar-holiday-marker'."
  264. :type 'boolean
  265. :group 'holidays)
  266. (defcustom calendar-mode-hook nil
  267. "Hook run when entering `calendar-mode'."
  268. :type 'hook
  269. :group 'calendar-hooks)
  270. (defcustom calendar-load-hook nil
  271. "List of functions to be called after the calendar is first loaded.
  272. This is the place to add key bindings to `calendar-mode-map'."
  273. :type 'hook
  274. :group 'calendar-hooks)
  275. (define-obsolete-variable-alias 'initial-calendar-window-hook
  276. 'calendar-initial-window-hook "23.1")
  277. (defcustom calendar-initial-window-hook nil
  278. "List of functions to be called when the calendar window is created.
  279. Quitting the calendar and re-entering it will cause these functions
  280. to be called again."
  281. :type 'hook
  282. :group 'calendar-hooks)
  283. (define-obsolete-variable-alias 'today-visible-calendar-hook
  284. 'calendar-today-visible-hook "23.1")
  285. (defcustom calendar-today-visible-hook nil
  286. "List of functions called whenever the current date is visible.
  287. To mark today's date, add the function `calendar-mark-today'.
  288. To replace the date with asterisks, add the function `calendar-star-date'.
  289. See also `calendar-today-invisible-hook'.
  290. In general, be careful about changing characters in the calendar buffer,
  291. since it may cause the movement commands to fail."
  292. :type 'hook
  293. :options '(calendar-mark-today calendar-star-date)
  294. :group 'calendar-hooks)
  295. (define-obsolete-variable-alias 'today-invisible-calendar-hook
  296. 'calendar-today-invisible-hook "23.1")
  297. (defcustom calendar-today-invisible-hook nil
  298. "List of functions called whenever the current date is not visible.
  299. See also `calendar-today-visible-hook'."
  300. :type 'hook
  301. :group 'calendar-hooks)
  302. (defcustom calendar-move-hook nil
  303. "List of functions called whenever the cursor moves in the calendar.
  304. For example,
  305. (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
  306. redisplays the diary for whatever date the cursor is moved to."
  307. :type 'hook
  308. :options '(calendar-update-mode-line)
  309. :group 'calendar-hooks)
  310. (defcustom calendar-date-echo-text
  311. "mouse-2: general menu\nmouse-3: menu for this date"
  312. "String displayed when the cursor is over a date in the calendar.
  313. Can be either a fixed string, or a lisp expression that returns one.
  314. When this expression is evaluated, DAY, MONTH, and YEAR are
  315. integers appropriate to the relevant date. For example, to
  316. display the ISO date:
  317. (setq calendar-date-echo-text '(format \"ISO date: %s\"
  318. (calendar-iso-date-string
  319. (list month day year))))
  320. Changing this variable without using customize has no effect on
  321. pre-existing calendar windows."
  322. :group 'calendar
  323. :initialize 'custom-initialize-default
  324. :risky t
  325. :set (lambda (sym val)
  326. (set sym val)
  327. (calendar-redraw))
  328. :type '(choice (string :tag "Fixed string")
  329. (sexp :value
  330. (format "ISO date: %s"
  331. (calendar-iso-date-string
  332. (list month day year)))))
  333. :version "23.1")
  334. (defvar calendar-month-digit-width nil
  335. "Width of the region with numbers in each month in the calendar.")
  336. (defvar calendar-month-width nil
  337. "Full width of each month in the calendar.")
  338. (defvar calendar-right-margin nil
  339. "Right margin of the calendar.")
  340. (defvar calendar-month-edges nil
  341. "Alist of month edge columns.
  342. Each element has the form (N LEFT FIRST LAST RIGHT), where
  343. LEFT is the leftmost column associated with month segment N,
  344. FIRST and LAST are the first and last columns with day digits in,
  345. and LAST is the rightmost column.")
  346. (defun calendar-month-edges (segment)
  347. "Compute the month edge columns for month SEGMENT.
  348. Returns a list (LEFT FIRST LAST RIGHT), where LEFT is the
  349. leftmost column associated with a month, FIRST and LAST are the
  350. first and last columns with day digits in, and LAST is the
  351. rightmost column."
  352. ;; The leftmost column with a digit in it in this month segment.
  353. (let* ((first (+ calendar-left-margin
  354. (* segment calendar-month-width)))
  355. ;; The rightmost column with a digit in it in this month segment.
  356. (last (+ first (1- calendar-month-digit-width)))
  357. (left (if (eq segment 0)
  358. 0
  359. (+ calendar-left-margin
  360. (* segment calendar-month-width)
  361. (- (/ calendar-intermonth-spacing 2)))))
  362. ;; The rightmost edge of this month segment, dividing the
  363. ;; space between months in two.
  364. (right (+ calendar-left-margin
  365. (* (1+ segment) calendar-month-width)
  366. (- (/ calendar-intermonth-spacing 2)))))
  367. (list left first last right)))
  368. (defun calendar-recompute-layout-variables ()
  369. "Recompute some layout-related calendar \"constants\"."
  370. (setq calendar-month-digit-width (+ (* 6 calendar-column-width)
  371. calendar-day-digit-width)
  372. calendar-month-width (+ (* 7 calendar-column-width)
  373. calendar-intermonth-spacing)
  374. calendar-right-margin (+ calendar-left-margin
  375. (* 3 (* 7 calendar-column-width))
  376. (* 2 calendar-intermonth-spacing))
  377. calendar-month-edges nil)
  378. (dotimes (i 3)
  379. (push (cons i (calendar-month-edges i)) calendar-month-edges))
  380. (setq calendar-month-edges (reverse calendar-month-edges)))
  381. ;; FIXME add font-lock-keywords.
  382. (defun calendar-set-layout-variable (symbol value &optional minmax)
  383. "Set SYMBOL's value to VALUE, an integer.
  384. A positive/negative MINMAX enforces a minimum/maximum value.
  385. Then redraw the calendar, if necessary."
  386. (let ((oldvalue (symbol-value symbol)))
  387. (custom-set-default symbol (if minmax
  388. (if (< minmax 0)
  389. (min value (- minmax))
  390. (max value minmax))
  391. value))
  392. (unless (equal value oldvalue)
  393. (calendar-recompute-layout-variables)
  394. (calendar-redraw))))
  395. (defcustom calendar-left-margin 5
  396. "Empty space to the left of the first month in the calendar."
  397. :group 'calendar
  398. :initialize 'custom-initialize-default
  399. :set 'calendar-set-layout-variable
  400. :type 'integer
  401. :version "23.1")
  402. ;; Or you can view it as columns of width 2, with 1 space, no space
  403. ;; after the last column, and a 5 space gap between month.
  404. ;; FIXME check things work if this is odd.
  405. (defcustom calendar-intermonth-spacing 4
  406. "Space between months in the calendar. Minimum value is 1."
  407. :group 'calendar
  408. :initialize 'custom-initialize-default
  409. :set (lambda (sym val)
  410. (calendar-set-layout-variable sym val 1))
  411. :type 'integer
  412. :version "23.1")
  413. ;; FIXME calendar-month-column-width?
  414. (defcustom calendar-column-width 3
  415. "Width of each day column in the calendar. Minimum value is 3."
  416. :initialize 'custom-initialize-default
  417. :set (lambda (sym val)
  418. (calendar-set-layout-variable sym val 3))
  419. :type 'integer
  420. :version "23.1")
  421. (defcustom calendar-day-header-width 2
  422. "Width of the day column headers in the calendar.
  423. Must be at least one less than `calendar-column-width'."
  424. :group 'calendar
  425. :initialize 'custom-initialize-default
  426. :set (lambda (sym val)
  427. (calendar-set-layout-variable sym val (- 1 calendar-column-width)))
  428. :type 'integer
  429. :version "23.1")
  430. ;; FIXME a format specifier instead?
  431. (defcustom calendar-day-digit-width 2
  432. "Width of the day digits in the calendar. Minimum value is 2."
  433. :group 'calendar
  434. :initialize 'custom-initialize-default
  435. :set (lambda (sym val)
  436. (calendar-set-layout-variable sym val 2))
  437. :type 'integer
  438. :version "23.1")
  439. (defcustom calendar-intermonth-header nil
  440. "Header text display in the space to the left of each calendar month.
  441. See `calendar-intermonth-text'."
  442. :group 'calendar
  443. :initialize 'custom-initialize-default
  444. :risky t
  445. :set (lambda (sym val)
  446. (set sym val)
  447. (calendar-redraw))
  448. :type '(choice (const nil :tag "Nothing")
  449. (string :tag "Fixed string")
  450. (sexp :value
  451. (propertize "WK" 'font-lock-face
  452. 'font-lock-function-name-face)))
  453. :version "23.1")
  454. (defcustom calendar-intermonth-text nil
  455. "Text to display in the space to the left of each calendar month.
  456. Can be nil, a fixed string, or a lisp expression that returns a string.
  457. When the expression is evaluated, the variables DAY, MONTH and YEAR
  458. are integers appropriate for the first day in each week.
  459. Will be truncated to the smaller of `calendar-left-margin' and
  460. `calendar-intermonth-spacing'. The last character is forced to be a space.
  461. For example, to display the ISO week numbers:
  462. (setq calendar-week-start-day 1
  463. calendar-intermonth-text
  464. '(propertize
  465. (format \"%2d\"
  466. (car
  467. (calendar-iso-from-absolute
  468. (calendar-absolute-from-gregorian (list month day year)))))
  469. 'font-lock-face 'font-lock-function-name-face))
  470. See also `calendar-intermonth-header'."
  471. :group 'calendar
  472. :initialize 'custom-initialize-default
  473. :risky t
  474. :set (lambda (sym val)
  475. (set sym val)
  476. (calendar-redraw))
  477. :type '(choice (const nil :tag "Nothing")
  478. (string :tag "Fixed string")
  479. (sexp :value
  480. (propertize
  481. (format "%2d"
  482. (car
  483. (calendar-iso-from-absolute
  484. (calendar-absolute-from-gregorian
  485. (list month day year)))))
  486. 'font-lock-face 'font-lock-function-name-face)))
  487. :version "23.1")
  488. (defcustom diary-file "~/diary"
  489. "Name of the file in which one's personal diary of dates is kept.
  490. The file's entries are lines beginning with any of the forms
  491. specified by the variable `diary-date-forms', which by default
  492. uses the forms of `diary-american-date-forms':
  493. MONTH/DAY
  494. MONTH/DAY/YEAR
  495. MONTHNAME DAY
  496. MONTHNAME DAY, YEAR
  497. DAYNAME
  498. with the remainder of the line being the diary entry string for
  499. that date. MONTH and DAY are one or two digit numbers, YEAR is a
  500. number and may be written in full or abbreviated to the final two
  501. digits (if `diary-abbreviated-year-flag' is non-nil). MONTHNAME
  502. and DAYNAME can be spelled in full (as specified by the variables
  503. `calendar-month-name-array' and `calendar-day-name-array'), or
  504. abbreviated (as specified by `calendar-month-abbrev-array' and
  505. `calendar-day-abbrev-array') with or without a period. Case is
  506. ignored. Any of DAY, MONTH, or MONTHNAME, YEAR can be `*' which
  507. matches any day, month, or year, respectively. If the date does
  508. not contain a year, it is generic and applies to any year. A
  509. DAYNAME entry applies to the appropriate day of the week in every week.
  510. You can customize `diary-date-forms' to your preferred format.
  511. Three default styles are provided: `diary-american-date-forms',
  512. `diary-european-date-forms', and `diary-iso-date-forms'.
  513. You can choose between these by setting `calendar-date-style' in your
  514. .emacs file, or by using `calendar-set-date-style' when in the calendar.
  515. A diary entry can be preceded by the character `diary-nonmarking-symbol'
  516. \(ordinarily `&') to make that entry nonmarking--that is, it will not be
  517. marked on dates in the calendar window but will appear in a diary window.
  518. Multiline diary entries are made by indenting lines after the first with
  519. either a TAB or one or more spaces.
  520. Lines not in one the above formats are ignored. Here are some sample diary
  521. entries (in the default American style):
  522. 12/22/1988 Twentieth wedding anniversary!!
  523. &1/1. Happy New Year!
  524. 10/22 Ruth's birthday.
  525. 21: Payday
  526. Tuesday--weekly meeting with grad students at 10am
  527. Supowit, Shen, Bitner, and Kapoor to attend.
  528. 1/13/89 Friday the thirteenth!!
  529. &thu 4pm squash game with Lloyd.
  530. mar 16 Dad's birthday
  531. April 15, 1989 Income tax due.
  532. &* 15 time cards due.
  533. If the first line of a diary entry consists only of the date or day name with
  534. no trailing blanks or punctuation, then that line is not displayed in the
  535. diary window; only the continuation lines is shown. For example, the
  536. single diary entry
  537. 02/11/1989
  538. Bill Blattner visits Princeton today
  539. 2pm Cognitive Studies Committee meeting
  540. 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
  541. 4:00pm Jamie Tappenden
  542. 7:30pm Dinner at George and Ed's for Alan Ryan
  543. 7:30-10:00pm dance at Stewart Country Day School
  544. will appear in the diary window without the date line at the beginning. This
  545. facility allows the diary window to look neater, but can cause confusion if
  546. used with more than one day's entries displayed.
  547. Diary entries can be based on Lisp sexps. For example, the diary entry
  548. %%(diary-block 11 1 1990 11 10 1990) Vacation
  549. causes the diary entry \"Vacation\" to appear from November 1 through
  550. November 10, 1990. See the documentation for the function
  551. `diary-list-sexp-entries' for more details.
  552. Diary entries based on the Hebrew, the Islamic and/or the Baha'i
  553. calendar are also possible, but because these are somewhat slow, they
  554. are ignored unless you set the `diary-nongregorian-listing-hook' and
  555. the `diary-nongregorian-marking-hook' appropriately. See the
  556. documentation of these hooks for details.
  557. Diary files can contain directives to include the contents of other files; for
  558. details, see the documentation for the variable `diary-list-entries-hook'."
  559. :type 'file
  560. :group 'diary)
  561. ;; FIXME do these have to be single characters?
  562. (defcustom diary-nonmarking-symbol "&"
  563. "Symbol indicating that a diary entry is not to be marked in the calendar."
  564. :type 'string
  565. :group 'diary)
  566. (define-obsolete-variable-alias 'hebrew-diary-entry-symbol
  567. 'diary-hebrew-entry-symbol "23.1")
  568. (defcustom diary-hebrew-entry-symbol "H"
  569. "Symbol indicating a diary entry according to the Hebrew calendar."
  570. :type 'string
  571. :group 'diary)
  572. (define-obsolete-variable-alias 'islamic-diary-entry-symbol
  573. 'diary-islamic-entry-symbol "23.1")
  574. (defcustom diary-islamic-entry-symbol "I"
  575. "Symbol indicating a diary entry according to the Islamic calendar."
  576. :type 'string
  577. :group 'diary)
  578. (define-obsolete-variable-alias 'bahai-diary-entry-symbol
  579. 'diary-bahai-entry-symbol "23.1")
  580. (defcustom diary-bahai-entry-symbol "B"
  581. "Symbol indicating a diary entry according to the Baha'i calendar."
  582. :type 'string
  583. :group 'diary)
  584. (defcustom european-calendar-style nil
  585. "Non-nil means use the European style of dates in the diary and display.
  586. In this case, a date like 1/2/1990 would be interpreted as
  587. February 1, 1990. See `diary-european-date-forms' for the
  588. default European diary date styles.
  589. Setting this variable directly does not take effect (if the
  590. calendar package is already loaded). Rather, use either
  591. \\[customize] or the function `calendar-set-date-style'."
  592. :type 'boolean
  593. ;; Without :initialize (require 'calendar) throws an error because
  594. ;; calendar-set-date-style is undefined at this point.
  595. :initialize 'custom-initialize-default
  596. :set (lambda (symbol value)
  597. (if value
  598. (calendar-set-date-style 'european)
  599. (calendar-set-date-style 'american)))
  600. :group 'calendar)
  601. (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
  602. ;; If this is autoloaded, c-d-s gets set before any customization of e-c-s.
  603. (defcustom calendar-date-style (if european-calendar-style 'european
  604. 'american)
  605. "Your preferred style for writing dates.
  606. The options are:
  607. `american' - month/day/year
  608. `european' - day/month/year
  609. `iso' - year/month/day
  610. This affects how dates written in your diary are interpreted.
  611. It also affects date display, as well as those calendar and diary
  612. functions that take a date as an argument, e.g. `diary-date', by
  613. changing the order in which the arguments are interpreted.
  614. Setting this variable directly does not take effect (if the
  615. calendar package is already loaded). Rather, use either
  616. \\[customize] or the function `calendar-set-date-style'."
  617. :version "23.1"
  618. :type '(choice (const american :tag "Month/Day/Year")
  619. (const european :tag "Day/Month/Year")
  620. (const iso :tag "Year/Month/Day"))
  621. :initialize 'custom-initialize-default
  622. :set (lambda (symbol value)
  623. (calendar-set-date-style value))
  624. :group 'calendar)
  625. ;; Next three are provided to aid in setting diary-date-forms.
  626. ;; FIXME move to diary-lib?
  627. (defcustom diary-iso-date-forms
  628. '((month "[-/]" day "[^-/0-9]")
  629. (year "[-/]" month "[-/]" day "[^0-9]")
  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.1"
  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. (defun calendar-set-date-style (style)
  795. "Set the style of calendar and diary dates to STYLE (a symbol).
  796. The valid styles are described in the documentation of `calendar-date-style'."
  797. (interactive (list (intern
  798. (completing-read "Date style: "
  799. '("american" "european" "iso") nil t
  800. nil nil "american"))))
  801. (or (memq style '(american european iso))
  802. (setq style 'american))
  803. (setq calendar-date-style style
  804. calendar-date-display-form
  805. (symbol-value (intern-soft
  806. (format "calendar-%s-date-display-form" style)))
  807. diary-date-forms
  808. (symbol-value (intern-soft (format "diary-%s-date-forms" style))))
  809. (calendar-update-mode-line))
  810. (defun european-calendar ()
  811. "Set the interpretation and display of dates to the European style."
  812. (interactive)
  813. (calendar-set-date-style 'european))
  814. (make-obsolete 'european-calendar 'calendar-set-date-style "23.1")
  815. (defun american-calendar ()
  816. "Set the interpretation and display of dates to the American style."
  817. (interactive)
  818. (calendar-set-date-style 'american))
  819. (make-obsolete 'american-calendar 'calendar-set-date-style "23.1")
  820. (define-obsolete-variable-alias 'holidays-in-diary-buffer
  821. 'diary-show-holidays-flag "23.1")
  822. (defcustom diary-show-holidays-flag t
  823. "Non-nil means include holidays in the diary display.
  824. The holidays appear in the mode line of the diary buffer, or in the
  825. fancy diary buffer next to the date. This slows down the diary functions
  826. somewhat; setting it to nil makes the diary display faster."
  827. :type 'boolean
  828. :group 'holidays)
  829. (defcustom calendar-debug-sexp nil
  830. "Turn debugging on when evaluating a sexp in the diary or holiday list."
  831. :type 'boolean
  832. :group 'calendar)
  833. (define-obsolete-variable-alias 'all-hebrew-calendar-holidays
  834. 'calendar-hebrew-all-holidays-flag "23.1")
  835. (defcustom calendar-hebrew-all-holidays-flag nil
  836. "If nil, show only major holidays from the Hebrew calendar.
  837. This means only those Jewish holidays that appear on secular calendars.
  838. Otherwise, show all the holidays that would appear in a complete Hebrew
  839. calendar."
  840. :type 'boolean
  841. :group 'holidays)
  842. (define-obsolete-variable-alias 'all-christian-calendar-holidays
  843. 'calendar-christian-all-holidays-flag "23.1")
  844. (defcustom calendar-christian-all-holidays-flag nil
  845. "If nil, show only major holidays from the Christian calendar.
  846. This means only those Christian holidays that appear on secular calendars.
  847. Otherwise, show all the holidays that would appear in a complete Christian
  848. calendar."
  849. :type 'boolean
  850. :group 'holidays)
  851. (define-obsolete-variable-alias 'all-islamic-calendar-holidays
  852. 'calendar-islamic-all-holidays-flag "23.1")
  853. (defcustom calendar-islamic-all-holidays-flag nil
  854. "If nil, show only major holidays from the Islamic calendar.
  855. This means only those Islamic holidays that appear on secular calendars.
  856. Otherwise, show all the holidays that would appear in a complete Islamic
  857. calendar."
  858. :type 'boolean
  859. :group 'holidays)
  860. (define-obsolete-variable-alias 'all-bahai-calendar-holidays
  861. 'calendar-bahai-all-holidays-flag "23.1")
  862. (defcustom calendar-bahai-all-holidays-flag nil
  863. "If nil, show only major holidays from the Baha'i calendar.
  864. These are the days on which work and school must be suspended.
  865. Otherwise, show all the holidays that would appear in a complete Baha'i
  866. calendar."
  867. :type 'boolean
  868. :group 'holidays)
  869. (defcustom calendar-chinese-all-holidays-flag nil
  870. "If nil, show only the major holidays from the Chinese calendar."
  871. :version "23.1"
  872. :type 'boolean
  873. :group 'holidays)
  874. ;;; End of user options.
  875. (calendar-recompute-layout-variables)
  876. (defconst calendar-first-date-row 3
  877. "First row in the calendar with actual dates.")
  878. (defconst calendar-buffer "*Calendar*"
  879. "Name of the buffer used for the calendar.")
  880. (defconst holiday-buffer "*Holidays*"
  881. "Name of the buffer used for the displaying the holidays.")
  882. (defconst diary-fancy-buffer "*Fancy Diary Entries*"
  883. "Name of the buffer used for the optional fancy display of the diary.")
  884. (define-obsolete-variable-alias 'fancy-diary-buffer 'diary-fancy-buffer "23.1")
  885. (defconst calendar-other-calendars-buffer "*Other Calendars*"
  886. "Name of the buffer used for the display of date on other calendars.")
  887. (defconst lunar-phases-buffer "*Phases of Moon*"
  888. "Name of the buffer used for the lunar phases.")
  889. (defconst solar-sunrises-buffer "*Sunrise/Sunset Times*"
  890. "Name of buffer used for sunrise/sunset times.")
  891. (defconst calendar-hebrew-yahrzeit-buffer "*Yahrzeits*"
  892. "Name of the buffer used by `list-yahrzeit-dates'.")
  893. (defmacro calendar-increment-month (mon yr n &optional nmonths)
  894. "Increment the variables MON and YR by N months.
  895. Forward if N is positive or backward if N is negative.
  896. A negative YR is interpreted as BC; -1 being 1 BC, and so on.
  897. Optional NMONTHS is the number of months per year (default 12)."
  898. ;; Can view this as a form of base-nmonths arithmetic, in which "a
  899. ;; year" = "ten", and we never bother to use hundreds.
  900. `(let ((nmonths (or ,nmonths 12))
  901. macro-y)
  902. (if (< ,yr 0) (setq ,yr (1+ ,yr))) ; -1 BC -> 0 AD, etc
  903. (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
  904. ,mon (1+ (mod macro-y nmonths))
  905. ,yr (/ macro-y nmonths))
  906. ;; Alternative:
  907. ;;; (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
  908. ;;; ,yr (/ macro-y nmonths)
  909. ;;; ,mon (- macro-y (* ,yr nmonths)))
  910. (and (< macro-y 0) (> ,mon 1) (setq ,yr (1- ,yr)))
  911. (if (< ,yr 1) (setq ,yr (1- ,yr))))) ; 0 AD -> -1 BC, etc
  912. (define-obsolete-function-alias 'increment-calendar-month
  913. 'calendar-increment-month "23.1")
  914. (defvar displayed-month)
  915. (defvar displayed-year)
  916. (defun calendar-increment-month-cons (n &optional mon yr)
  917. "Return the Nth month after MON/YR.
  918. The return value is a pair (MONTH . YEAR).
  919. MON defaults to `displayed-month'. YR defaults to `displayed-year'."
  920. (unless mon (setq mon displayed-month))
  921. (unless yr (setq yr displayed-year))
  922. (calendar-increment-month mon yr n)
  923. (cons mon yr))
  924. (defmacro calendar-for-loop (var from init to final do &rest body)
  925. "Execute a for loop.
  926. Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
  927. inclusive. The standard macro `dotimes' is preferable in most cases."
  928. (declare (debug (symbolp "from" form "to" form "do" body))
  929. (indent defun))
  930. `(let ((,var (1- ,init)))
  931. (while (>= ,final (setq ,var (1+ ,var)))
  932. ,@body)))
  933. (make-obsolete 'calendar-for-loop "use `dotimes' or `while' instead." "23.1")
  934. (defmacro calendar-sum (index initial condition expression)
  935. "For INDEX = INITIAL, +1, ... (as long as CONDITION holds), sum EXPRESSION."
  936. (declare (debug (symbolp form form form)))
  937. `(let ((,index ,initial)
  938. (sum 0))
  939. (while ,condition
  940. (setq sum (+ sum ,expression)
  941. ,index (1+ ,index)))
  942. sum))
  943. ;; FIXME bind q to bury-buffer?
  944. (defmacro calendar-in-read-only-buffer (buffer &rest body)
  945. "Switch to BUFFER and executes the forms in BODY.
  946. First creates or erases BUFFER as needed. Leaves BUFFER read-only,
  947. with disabled undo. Leaves point at point-min, displays BUFFER."
  948. (declare (indent 1) (debug t))
  949. `(progn
  950. (set-buffer (get-buffer-create ,buffer))
  951. (setq buffer-read-only nil
  952. buffer-undo-list t)
  953. (erase-buffer)
  954. ,@body
  955. (goto-char (point-min))
  956. (set-buffer-modified-p nil)
  957. (setq buffer-read-only t)
  958. (display-buffer ,buffer)))
  959. ;; The following are in-line for speed; they can be called thousands of times
  960. ;; when looking up holidays or processing the diary. Here, for example, are
  961. ;; the numbers of calls to calendar/diary/holiday functions in preparing the
  962. ;; fancy diary display, for a moderately complex diary file, with functions
  963. ;; used instead of macros. There were a total of 10000 such calls:
  964. ;;
  965. ;; 1934 calendar-extract-month
  966. ;; 1852 calendar-extract-year
  967. ;; 1819 calendar-extract-day
  968. ;; 845 calendar-leap-year-p
  969. ;; 837 calendar-day-number
  970. ;; 775 calendar-absolute-from-gregorian
  971. ;; 346 calendar-last-day-of-month
  972. ;; 286 calendar-hebrew-last-day-of-month
  973. ;; 188 calendar-hebrew-leap-year-p
  974. ;; 180 calendar-hebrew-elapsed-days
  975. ;; 163 calendar-hebrew-last-month-of-year
  976. ;; 66 calendar-date-compare
  977. ;; 65 calendar-hebrew-days-in-year
  978. ;; 60 calendar-julian-to-absolute
  979. ;; 50 calendar-hebrew-to-absolute
  980. ;; 43 calendar-date-equal
  981. ;; 38 calendar-gregorian-from-absolute
  982. ;; .
  983. ;;
  984. ;; The use of these seven macros eliminates the overhead of 92% of the function
  985. ;; calls; it's faster this way.
  986. (defsubst calendar-extract-month (date)
  987. "Extract the month part of DATE which has the form (month day year)."
  988. (car date))
  989. (define-obsolete-function-alias 'extract-calendar-month
  990. 'calendar-extract-month "23.1")
  991. ;; Note gives wrong answer for result of (calendar-read-date 'noday),
  992. ;; but that is only used by `calendar-other-month'.
  993. (defsubst calendar-extract-day (date)
  994. "Extract the day part of DATE which has the form (month day year)."
  995. (cadr date))
  996. (define-obsolete-function-alias 'extract-calendar-day
  997. 'calendar-extract-day "23.1")
  998. (defsubst calendar-extract-year (date)
  999. "Extract the year part of DATE which has the form (month day year)."
  1000. (nth 2 date))
  1001. (define-obsolete-function-alias 'extract-calendar-year
  1002. 'calendar-extract-year "23.1")
  1003. (defsubst calendar-leap-year-p (year)
  1004. "Return t if YEAR is a Gregorian leap year.
  1005. A negative year is interpreted as BC; -1 being 1 BC, and so on."
  1006. ;; 1 BC = 0 AD, 2 BC acts like 1 AD, etc.
  1007. (if (< year 0) (setq year (1- (abs year))))
  1008. (and (zerop (% year 4))
  1009. (or (not (zerop (% year 100)))
  1010. (zerop (% year 400)))))
  1011. ;; The foregoing is a bit faster, but not as clear as the following:
  1012. ;;
  1013. ;;(defsubst calendar-leap-year-p (year)
  1014. ;; "Return t if YEAR is a Gregorian leap year."
  1015. ;; (or
  1016. ;; (and (zerop (% year 4))
  1017. ;; (not (zerop (% year 100))))
  1018. ;; (zerop (% year 400)))
  1019. (defsubst calendar-last-day-of-month (month year)
  1020. "The last day in MONTH during YEAR."
  1021. (if (and (= month 2) (calendar-leap-year-p year))
  1022. 29
  1023. (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  1024. ;; An explanation of the calculation can be found in PascAlgorithms by
  1025. ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
  1026. (defsubst calendar-day-number (date)
  1027. "Return the day number within the year of the date DATE.
  1028. For example, (calendar-day-number '(1 1 1987)) returns the value 1,
  1029. while (calendar-day-number '(12 31 1980)) returns 366."
  1030. (let* ((month (calendar-extract-month date))
  1031. (day (calendar-extract-day date))
  1032. (year (calendar-extract-year date))
  1033. (day-of-year (+ day (* 31 (1- month)))))
  1034. (when (> month 2)
  1035. (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
  1036. (if (calendar-leap-year-p year)
  1037. (setq day-of-year (1+ day-of-year))))
  1038. day-of-year))
  1039. (defsubst calendar-absolute-from-gregorian (date)
  1040. "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
  1041. The Gregorian date Sunday, December 31, 1 BC is imaginary.
  1042. DATE is a list of the form (month day year). A negative year is
  1043. interpreted as BC; -1 being 1 BC, and so on. Dates before 12/31/1 BC
  1044. return negative results."
  1045. (let ((year (calendar-extract-year date))
  1046. offset-years)
  1047. (cond ((zerop year)
  1048. (error "There was no year zero"))
  1049. ((> year 0)
  1050. (setq offset-years (1- year))
  1051. (+ (calendar-day-number date) ; days t

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