PageRenderTime 137ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/lisp/calendar/calendar.el

https://bitbucket.org/knzm/emacs
Emacs Lisp | 2628 lines | 2029 code | 272 blank | 327 comment | 72 complexity | ded67db4b52130a5096e2e919b4c499a MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.0
  1. ;;; calendar.el --- calendar functions
  2. ;; Copyright (C) 1988-1995, 1997, 2000-2011 Free Software Foundation, Inc.
  3. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Keywords: calendar
  6. ;; Human-Keywords: calendar, Gregorian calendar, diary, holidays
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This collection of functions implements a calendar window. It
  20. ;; generates a calendar for the current month, together with the
  21. ;; previous and coming months, or for any other three-month period.
  22. ;; The calendar can be scrolled forward and backward in the window to
  23. ;; show months in the past or future; the cursor can move forward and
  24. ;; backward by days, weeks, or months, making it possible, for
  25. ;; instance, to jump to the date a specified number of days, weeks, or
  26. ;; months from the date under the cursor. The user can display a list
  27. ;; of holidays and other notable days for the period shown; the
  28. ;; notable days can be marked on the calendar, if desired. The user
  29. ;; can also specify that dates having corresponding diary entries (in
  30. ;; a file that the user specifies) be marked; the diary entries for
  31. ;; any date can be viewed in a separate window. The diary and the
  32. ;; notable days can be viewed independently of the calendar. Dates
  33. ;; can be translated from the (usual) Gregorian calendar to the day of
  34. ;; the year/days remaining in year, to the ISO commercial calendar, to
  35. ;; the Julian (old style) calendar, to the Hebrew calendar, to the
  36. ;; Islamic calendar, to the Baha'i calendar, to the French
  37. ;; Revolutionary calendar, to the Mayan calendar, to the Chinese
  38. ;; calendar, to the Coptic calendar, to the Ethiopic calendar, and to
  39. ;; the astronomical (Julian) day number. Times of sunrise/sunset can
  40. ;; be displayed, as can the phases of the moon. Appointment
  41. ;; notification for diary entries is available. Calendar printing via
  42. ;; LaTeX is available.
  43. ;; The following files are part of the calendar/diary code:
  44. ;; appt.el Appointment notification
  45. ;; cal-bahai.el Baha'i calendar
  46. ;; cal-china.el Chinese calendar
  47. ;; cal-coptic.el Coptic/Ethiopic calendars
  48. ;; cal-dst.el Daylight saving time rules
  49. ;; cal-french.el French revolutionary calendar
  50. ;; cal-hebrew.el Hebrew calendar
  51. ;; cal-html.el Calendars in HTML
  52. ;; cal-islam.el Islamic calendar
  53. ;; cal-iso.el ISO calendar
  54. ;; cal-julian.el Julian/astronomical calendars
  55. ;; cal-mayan.el Mayan calendars
  56. ;; cal-menu.el Menu support
  57. ;; cal-move.el Movement in the calendar
  58. ;; cal-persia.el Persian calendar
  59. ;; cal-tex.el Calendars in LaTeX
  60. ;; cal-x.el Dedicated frame functions
  61. ;; calendar.el This file
  62. ;; diary-lib.el Diary functions
  63. ;; holidays.el Holiday functions
  64. ;; lunar.el Phases of the moon
  65. ;; solar.el Sunrise/sunset, equinoxes/solstices
  66. ;; Technical details of all the calendrical calculations can be found in
  67. ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
  68. ;; and Nachum Dershowitz, Cambridge University Press (2001).
  69. ;; An earlier version of the technical details appeared in
  70. ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
  71. ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
  72. ;; pages 899-928, and in ``Calendrical Calculations, Part II: Three Historical
  73. ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
  74. ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
  75. ;; pages 383-404.
  76. ;; Hard copies of these two papers can be obtained by sending email to
  77. ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
  78. ;; the message BODY containing your mailing address (snail).
  79. ;; A note on free variables:
  80. ;; The calendar passes around a few dynamically bound variables, which
  81. ;; unfortunately have rather common names. They are meant to be
  82. ;; available for external functions, so the names can't be changed.
  83. ;; displayed-month, displayed-year: bound in calendar-generate, the
  84. ;; central month of the 3 month calendar window
  85. ;; original-date, number: bound in diary-list-entries, the arguments
  86. ;; with which that function was called.
  87. ;; date, entry: bound in diary-list-sexp-entries (qv)
  88. ;; Bound in diary-list-entries:
  89. ;; diary-entries-list: use in d-l, appt.el, and by diary-add-to-list
  90. ;; diary-saved-point: only used in diary-lib.el, passed to the display func
  91. ;; date-string: only used in diary-lib.el
  92. ;; list-only: don't modify the diary-buffer, just return a list of entries
  93. ;; file-glob-attrs: yuck
  94. ;;; Code:
  95. (load "cal-loaddefs" nil t)
  96. ;; Avoid recursive load of calendar when loading cal-menu. Yuck.
  97. (provide 'calendar)
  98. (require 'cal-menu)
  99. (defgroup calendar nil
  100. "Calendar and time management support."
  101. :prefix "calendar-"
  102. :group 'applications)
  103. (defgroup calendar-hooks nil
  104. "Calendar hooks."
  105. :prefix "calendar-"
  106. :group 'calendar)
  107. (defgroup calendar-faces nil
  108. "Calendar faces."
  109. :prefix "calendar-"
  110. :group 'calendar)
  111. (defcustom calendar-offset 0
  112. "The offset of the principal month from the center of the calendar window.
  113. 0 means the principal month is in the center (default), -1 means on the left,
  114. +1 means on the right. Larger (or smaller) values push the principal month off
  115. the screen."
  116. :type 'integer
  117. :group 'calendar)
  118. (defcustom calendar-setup nil
  119. "The frame setup of the calendar.
  120. The choices are: `one-frame' (calendar and diary together in one separate,
  121. dedicated frame); `two-frames' (calendar and diary in separate, dedicated
  122. frames); `calendar-only' (calendar in a separate, dedicated frame); with
  123. any other value the current frame is used. Using any of the first
  124. three options overrides the value of `calendar-view-diary-initially-flag'."
  125. :type '(choice
  126. (const :tag "calendar and diary in separate frame" one-frame)
  127. (const :tag "calendar and diary each in own frame" two-frames)
  128. (const :tag "calendar in separate frame" calendar-only)
  129. (const :tag "use current frame" nil))
  130. :group 'calendar)
  131. (defcustom calendar-minimum-window-height 8
  132. "Minimum height `calendar-generate-window' should use for calendar window."
  133. :type 'integer
  134. :version "22.1"
  135. :group 'calendar)
  136. ;; See discussion in bug#1806.
  137. (defcustom calendar-split-width-threshold nil
  138. "Value to use for `split-width-threshold' when creating a calendar.
  139. This only affects frames wider than the default value of
  140. `split-width-threshold'."
  141. :type '(choice (const nil)
  142. (integer))
  143. :version "23.2"
  144. :group 'calendar)
  145. (defcustom calendar-week-start-day 0
  146. "The day of the week on which a week in the calendar begins.
  147. 0 means Sunday (default), 1 means Monday, and so on.
  148. If you change this variable directly (without using customize)
  149. after starting `calendar', you should call `calendar-redraw' to
  150. update the calendar display to reflect the change, otherwise
  151. movement commands will not work correctly."
  152. :type 'integer
  153. ;; Change the initialize so that if you reload calendar.el, it will not
  154. ;; cause a redraw (which may fail, e.g. with "invalid byte-code in
  155. ;; calendar.elc" because of the "byte-compile-dynamic").
  156. :initialize 'custom-initialize-default
  157. :set (lambda (sym val)
  158. (set sym val)
  159. (calendar-redraw))
  160. :group 'calendar)
  161. (define-obsolete-variable-alias 'view-diary-entries-initially
  162. 'calendar-view-diary-initially-flag "23.1")
  163. (defcustom calendar-view-diary-initially-flag nil
  164. "Non-nil means display current date's diary entries on entry to calendar.
  165. The diary is displayed in another window when the calendar is first displayed,
  166. if the current date is visible. The number of days of diary entries displayed
  167. is governed by the variable `diary-number-of-entries'. This variable can
  168. be overridden by the value of `calendar-setup'."
  169. :type 'boolean
  170. :group 'diary)
  171. (define-obsolete-variable-alias 'mark-diary-entries-in-calendar
  172. 'calendar-mark-diary-entries-flag "23.1")
  173. ;; FIXME :set
  174. (defcustom calendar-mark-diary-entries-flag nil
  175. "Non-nil means mark dates with diary entries, in the calendar window.
  176. The marking symbol is specified by the variable `diary-entry-marker'."
  177. :type 'boolean
  178. :group 'diary)
  179. (defcustom calendar-remove-frame-by-deleting t
  180. "Determine how the calendar mode removes a frame no longer needed.
  181. If nil, make an icon of the frame. If non-nil, delete the frame."
  182. :type 'boolean
  183. :version "23.1" ; changed from nil to t
  184. :group 'view
  185. :group 'calendar)
  186. (defface calendar-today
  187. '((t (:underline t)))
  188. "Face for indicating today's date in the calendar.
  189. See the variable `calendar-today-marker'."
  190. :group 'calendar-faces)
  191. (define-obsolete-face-alias 'calendar-today-face 'calendar-today "22.1")
  192. (defface diary
  193. '((((min-colors 88) (class color) (background light))
  194. :foreground "red1")
  195. (((class color) (background light))
  196. :foreground "red")
  197. (((min-colors 88) (class color) (background dark))
  198. :foreground "yellow1")
  199. (((class color) (background dark))
  200. :foreground "yellow")
  201. (t
  202. :weight bold))
  203. "Face for highlighting diary entries.
  204. Used to mark diary entries in the calendar (see `diary-entry-marker'),
  205. and to highlight the date header in the fancy diary."
  206. :group 'calendar-faces)
  207. (define-obsolete-face-alias 'diary-face 'diary "22.1")
  208. (defface holiday
  209. '((((class color) (background light))
  210. :background "pink")
  211. (((class color) (background dark))
  212. :background "chocolate4")
  213. (t
  214. :inverse-video t))
  215. "Face for indicating in the calendar dates that have holidays.
  216. See `calendar-holiday-marker'."
  217. :group 'calendar-faces)
  218. (define-obsolete-face-alias 'holiday-face 'holiday "22.1")
  219. ;; These briefly checked font-lock-mode, but that is broken, since it
  220. ;; is a buffer-local variable, and which buffer happens to be current
  221. ;; when this file is loaded shouldn't make a difference. One could
  222. ;; perhaps check global-font-lock-mode, or font-lock-global-modes; but
  223. ;; this feature doesn't use font-lock, so there's no real reason it
  224. ;; should respect those either. See bug#2199.
  225. ;; They also used to check display-color-p, but that is a problem if
  226. ;; loaded from --daemon. Since BW displays are rare now, this was
  227. ;; also taken out. The way to keep it would be to have nil mean do a
  228. ;; runtime check whenever this variable is used.
  229. (defcustom diary-entry-marker 'diary
  230. "How to mark dates that have diary entries.
  231. The value can be either a single-character string (e.g. \"+\") or a face."
  232. :type '(choice (string :tag "Single character string") face)
  233. :group 'diary
  234. :version "23.1")
  235. (defcustom calendar-today-marker 'calendar-today
  236. "How to mark today's date in the calendar.
  237. The value can be either a single-character string (e.g. \"=\") or a face.
  238. Used by `calendar-mark-today'."
  239. :type '(choice (string :tag "Single character string") face)
  240. :group 'calendar
  241. :version "23.1")
  242. (defcustom calendar-holiday-marker 'holiday
  243. "How to mark notable dates in the calendar.
  244. The value can be either a single-character string (e.g. \"*\") or a face."
  245. :type '(choice (string :tag "Single character string") face)
  246. :group 'holidays
  247. :version "23.1")
  248. (define-obsolete-variable-alias 'view-calendar-holidays-initially
  249. 'calendar-view-holidays-initially-flag "23.1")
  250. (defcustom calendar-view-holidays-initially-flag nil
  251. "Non-nil means display holidays for current three month period on entry.
  252. The holidays are displayed in another window when the calendar is first
  253. displayed."
  254. :type 'boolean
  255. :group 'holidays)
  256. (define-obsolete-variable-alias 'mark-holidays-in-calendar
  257. 'calendar-mark-holidays-flag "23.1")
  258. ;; FIXME :set
  259. (defcustom calendar-mark-holidays-flag nil
  260. "Non-nil means mark dates of holidays in the calendar window.
  261. The marking symbol is specified by the variable `calendar-holiday-marker'."
  262. :type 'boolean
  263. :group 'holidays)
  264. (defcustom calendar-mode-hook nil
  265. "Hook run when entering `calendar-mode'."
  266. :type 'hook
  267. :group 'calendar-hooks)
  268. (defcustom calendar-load-hook nil
  269. "List of functions to be called after the calendar is first loaded.
  270. This is the place to add key bindings to `calendar-mode-map'."
  271. :type 'hook
  272. :group 'calendar-hooks)
  273. (define-obsolete-variable-alias 'initial-calendar-window-hook
  274. 'calendar-initial-window-hook "23.1")
  275. (defcustom calendar-initial-window-hook nil
  276. "List of functions to be called when the calendar window is created.
  277. Quitting the calendar and re-entering it will cause these functions
  278. to be called again."
  279. :type 'hook
  280. :group 'calendar-hooks)
  281. (define-obsolete-variable-alias 'today-visible-calendar-hook
  282. 'calendar-today-visible-hook "23.1")
  283. (defcustom calendar-today-visible-hook nil
  284. "List of functions called whenever the current date is visible.
  285. To mark today's date, add the function `calendar-mark-today'.
  286. To replace the date with asterisks, add the function `calendar-star-date'.
  287. See also `calendar-today-invisible-hook'.
  288. In general, be careful about changing characters in the calendar buffer,
  289. since it may cause the movement commands to fail."
  290. :type 'hook
  291. :options '(calendar-mark-today calendar-star-date)
  292. :group 'calendar-hooks)
  293. (define-obsolete-variable-alias 'today-invisible-calendar-hook
  294. 'calendar-today-invisible-hook "23.1")
  295. (defcustom calendar-today-invisible-hook nil
  296. "List of functions called whenever the current date is not visible.
  297. See also `calendar-today-visible-hook'."
  298. :type 'hook
  299. :group 'calendar-hooks)
  300. (defcustom calendar-move-hook nil
  301. "List of functions called whenever the cursor moves in the calendar.
  302. For example,
  303. (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
  304. redisplays the diary for whatever date the cursor is moved to."
  305. :type 'hook
  306. :options '(calendar-update-mode-line)
  307. :group 'calendar-hooks)
  308. (defcustom calendar-date-echo-text
  309. "mouse-2: general menu\nmouse-3: menu for this date"
  310. "String displayed when the cursor is over a date in the calendar.
  311. Can be either a fixed string, or a lisp expression that returns one.
  312. When this expression is evaluated, DAY, MONTH, and YEAR are
  313. integers appropriate to the relevant date. For example, to
  314. display the ISO date:
  315. (setq calendar-date-echo-text '(format \"ISO date: %s\"
  316. (calendar-iso-date-string
  317. (list month day year))))
  318. Changing this variable without using customize has no effect on
  319. pre-existing calendar windows."
  320. :group 'calendar
  321. :initialize 'custom-initialize-default
  322. :risky t
  323. :set (lambda (sym val)
  324. (set sym val)
  325. (calendar-redraw))
  326. :type '(choice (string :tag "Fixed string")
  327. (sexp :value
  328. (format "ISO date: %s"
  329. (calendar-iso-date-string
  330. (list month day year)))))
  331. :version "23.1")
  332. (defvar calendar-month-digit-width nil
  333. "Width of the region with numbers in each month in the calendar.")
  334. (defvar calendar-month-width nil
  335. "Full width of each month in the calendar.")
  336. (defvar calendar-right-margin nil
  337. "Right margin of the calendar.")
  338. (defvar calendar-month-edges nil
  339. "Alist of month edge columns.
  340. Each element has the form (N LEFT FIRST LAST RIGHT), where
  341. LEFT is the leftmost column associated with month segment N,
  342. FIRST and LAST are the first and last columns with day digits in,
  343. and LAST is the rightmost column.")
  344. (defun calendar-month-edges (segment)
  345. "Compute the month edge columns for month SEGMENT.
  346. Returns a list (LEFT FIRST LAST RIGHT), where LEFT is the
  347. leftmost column associated with a month, FIRST and LAST are the
  348. first and last columns with day digits in, and LAST is the
  349. rightmost column."
  350. ;; The leftmost column with a digit in it in this month segment.
  351. (let* ((first (+ calendar-left-margin
  352. (* segment calendar-month-width)))
  353. ;; The rightmost column with a digit in it in this month segment.
  354. (last (+ first (1- calendar-month-digit-width)))
  355. (left (if (eq segment 0)
  356. 0
  357. (+ calendar-left-margin
  358. (* segment calendar-month-width)
  359. (- (/ calendar-intermonth-spacing 2)))))
  360. ;; The rightmost edge of this month segment, dividing the
  361. ;; space between months in two.
  362. (right (+ calendar-left-margin
  363. (* (1+ segment) calendar-month-width)
  364. (- (/ calendar-intermonth-spacing 2)))))
  365. (list left first last right)))
  366. (defun calendar-recompute-layout-variables ()
  367. "Recompute some layout-related calendar \"constants\"."
  368. (setq calendar-month-digit-width (+ (* 6 calendar-column-width)
  369. calendar-day-digit-width)
  370. calendar-month-width (+ (* 7 calendar-column-width)
  371. calendar-intermonth-spacing)
  372. calendar-right-margin (+ calendar-left-margin
  373. (* 3 (* 7 calendar-column-width))
  374. (* 2 calendar-intermonth-spacing))
  375. calendar-month-edges nil)
  376. (dotimes (i 3)
  377. (push (cons i (calendar-month-edges i)) calendar-month-edges))
  378. (setq calendar-month-edges (reverse calendar-month-edges)))
  379. ;; FIXME add font-lock-keywords.
  380. (defun calendar-set-layout-variable (symbol value &optional minmax)
  381. "Set SYMBOL's value to VALUE, an integer.
  382. A positive/negative MINMAX enforces a minimum/maximum value.
  383. Then redraw the calendar, if necessary."
  384. (let ((oldvalue (symbol-value symbol)))
  385. (custom-set-default symbol (if minmax
  386. (if (< minmax 0)
  387. (min value (- minmax))
  388. (max value minmax))
  389. value))
  390. (unless (equal value oldvalue)
  391. (calendar-recompute-layout-variables)
  392. (calendar-redraw))))
  393. (defcustom calendar-left-margin 5
  394. "Empty space to the left of the first month in the calendar."
  395. :group 'calendar
  396. :initialize 'custom-initialize-default
  397. :set 'calendar-set-layout-variable
  398. :type 'integer
  399. :version "23.1")
  400. ;; Or you can view it as columns of width 2, with 1 space, no space
  401. ;; after the last column, and a 5 space gap between month.
  402. ;; FIXME check things work if this is odd.
  403. (defcustom calendar-intermonth-spacing 4
  404. "Space between months in the calendar. Minimum value is 1."
  405. :group 'calendar
  406. :initialize 'custom-initialize-default
  407. :set (lambda (sym val)
  408. (calendar-set-layout-variable sym val 1))
  409. :type 'integer
  410. :version "23.1")
  411. ;; FIXME calendar-month-column-width?
  412. (defcustom calendar-column-width 3
  413. "Width of each day column in the calendar. Minimum value is 3."
  414. :initialize 'custom-initialize-default
  415. :set (lambda (sym val)
  416. (calendar-set-layout-variable sym val 3))
  417. :type 'integer
  418. :version "23.1")
  419. (defcustom calendar-day-header-width 2
  420. "Width of the day column headers in the calendar.
  421. Must be at least one less than `calendar-column-width'."
  422. :group 'calendar
  423. :initialize 'custom-initialize-default
  424. :set (lambda (sym val)
  425. (calendar-set-layout-variable sym val (- 1 calendar-column-width)))
  426. :type 'integer
  427. :version "23.1")
  428. ;; FIXME a format specifier instead?
  429. (defcustom calendar-day-digit-width 2
  430. "Width of the day digits in the calendar. Minimum value is 2."
  431. :group 'calendar
  432. :initialize 'custom-initialize-default
  433. :set (lambda (sym val)
  434. (calendar-set-layout-variable sym val 2))
  435. :type 'integer
  436. :version "23.1")
  437. (defcustom calendar-intermonth-header nil
  438. "Header text display in the space to the left of each calendar month.
  439. See `calendar-intermonth-text'."
  440. :group 'calendar
  441. :initialize 'custom-initialize-default
  442. :risky t
  443. :set (lambda (sym val)
  444. (set sym val)
  445. (calendar-redraw))
  446. :type '(choice (const nil :tag "Nothing")
  447. (string :tag "Fixed string")
  448. (sexp :value
  449. (propertize "WK" 'font-lock-face
  450. 'font-lock-function-name-face)))
  451. :version "23.1")
  452. (defcustom calendar-intermonth-text nil
  453. "Text to display in the space to the left of each calendar month.
  454. Can be nil, a fixed string, or a lisp expression that returns a string.
  455. When the expression is evaluated, the variables DAY, MONTH and YEAR
  456. are integers appropriate for the first day in each week.
  457. Will be truncated to the smaller of `calendar-left-margin' and
  458. `calendar-intermonth-spacing'. The last character is forced to be a space.
  459. For example, to display the ISO week numbers:
  460. (setq calendar-week-start-day 1
  461. calendar-intermonth-text
  462. '(propertize
  463. (format \"%2d\"
  464. (car
  465. (calendar-iso-from-absolute
  466. (calendar-absolute-from-gregorian (list month day year)))))
  467. 'font-lock-face 'font-lock-function-name-face))
  468. See also `calendar-intermonth-header'."
  469. :group 'calendar
  470. :initialize 'custom-initialize-default
  471. :risky t
  472. :set (lambda (sym val)
  473. (set sym val)
  474. (calendar-redraw))
  475. :type '(choice (const nil :tag "Nothing")
  476. (string :tag "Fixed string")
  477. (sexp :value
  478. (propertize
  479. (format "%2d"
  480. (car
  481. (calendar-iso-from-absolute
  482. (calendar-absolute-from-gregorian
  483. (list month day year)))))
  484. 'font-lock-face 'font-lock-function-name-face)))
  485. :version "23.1")
  486. (defcustom diary-file "~/diary"
  487. "Name of the file in which one's personal diary of dates is kept.
  488. The file's entries are lines beginning with any of the forms
  489. specified by the variable `diary-date-forms', which by default
  490. uses the forms of `diary-american-date-forms':
  491. MONTH/DAY
  492. MONTH/DAY/YEAR
  493. MONTHNAME DAY
  494. MONTHNAME DAY, YEAR
  495. DAYNAME
  496. with the remainder of the line being the diary entry string for
  497. that date. MONTH and DAY are one or two digit numbers, YEAR is a
  498. number and may be written in full or abbreviated to the final two
  499. digits (if `diary-abbreviated-year-flag' is non-nil). MONTHNAME
  500. and DAYNAME can be spelled in full (as specified by the variables
  501. `calendar-month-name-array' and `calendar-day-name-array'), or
  502. abbreviated (as specified by `calendar-month-abbrev-array' and
  503. `calendar-day-abbrev-array') with or without a period. Case is
  504. ignored. Any of DAY, MONTH, or MONTHNAME, YEAR can be `*' which
  505. matches any day, month, or year, respectively. If the date does
  506. not contain a year, it is generic and applies to any year. A
  507. DAYNAME entry applies to the appropriate day of the week in every week.
  508. You can customize `diary-date-forms' to your preferred format.
  509. Three default styles are provided: `diary-american-date-forms',
  510. `diary-european-date-forms', and `diary-iso-date-forms'.
  511. You can choose between these by setting `calendar-date-style' in your
  512. .emacs file, or by using `calendar-set-date-style' when in the calendar.
  513. A diary entry can be preceded by the character `diary-nonmarking-symbol'
  514. \(ordinarily `&') to make that entry nonmarking--that is, it will not be
  515. marked on dates in the calendar window but will appear in a diary window.
  516. Multiline diary entries are made by indenting lines after the first with
  517. either a TAB or one or more spaces.
  518. Lines not in one the above formats are ignored. Here are some sample diary
  519. entries (in the default American style):
  520. 12/22/1988 Twentieth wedding anniversary!!
  521. &1/1. Happy New Year!
  522. 10/22 Ruth's birthday.
  523. 21: Payday
  524. Tuesday--weekly meeting with grad students at 10am
  525. Supowit, Shen, Bitner, and Kapoor to attend.
  526. 1/13/89 Friday the thirteenth!!
  527. &thu 4pm squash game with Lloyd.
  528. mar 16 Dad's birthday
  529. April 15, 1989 Income tax due.
  530. &* 15 time cards due.
  531. If the first line of a diary entry consists only of the date or day name with
  532. no trailing blanks or punctuation, then that line is not displayed in the
  533. diary window; only the continuation lines is shown. For example, the
  534. single diary entry
  535. 02/11/1989
  536. Bill Blattner visits Princeton today
  537. 2pm Cognitive Studies Committee meeting
  538. 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
  539. 4:00pm Jamie Tappenden
  540. 7:30pm Dinner at George and Ed's for Alan Ryan
  541. 7:30-10:00pm dance at Stewart Country Day School
  542. will appear in the diary window without the date line at the beginning. This
  543. facility allows the diary window to look neater, but can cause confusion if
  544. used with more than one day's entries displayed.
  545. Diary entries can be based on Lisp sexps. For example, the diary entry
  546. %%(diary-block 11 1 1990 11 10 1990) Vacation
  547. causes the diary entry \"Vacation\" to appear from November 1 through
  548. November 10, 1990. See the documentation for the function
  549. `diary-list-sexp-entries' for more details.
  550. Diary entries based on the Hebrew, the Islamic and/or the Baha'i
  551. calendar are also possible, but because these are somewhat slow, they
  552. are ignored unless you set the `diary-nongregorian-listing-hook' and
  553. the `diary-nongregorian-marking-hook' appropriately. See the
  554. documentation of these hooks for details.
  555. Diary files can contain directives to include the contents of other files; for
  556. details, see the documentation for the variable `diary-list-entries-hook'."
  557. :type 'file
  558. :group 'diary)
  559. ;; FIXME do these have to be single characters?
  560. (defcustom diary-nonmarking-symbol "&"
  561. "Symbol indicating that a diary entry is not to be marked in the calendar."
  562. :type 'string
  563. :group 'diary)
  564. (define-obsolete-variable-alias 'hebrew-diary-entry-symbol
  565. 'diary-hebrew-entry-symbol "23.1")
  566. (defcustom diary-hebrew-entry-symbol "H"
  567. "Symbol indicating a diary entry according to the Hebrew calendar."
  568. :type 'string
  569. :group 'diary)
  570. (define-obsolete-variable-alias 'islamic-diary-entry-symbol
  571. 'diary-islamic-entry-symbol "23.1")
  572. (defcustom diary-islamic-entry-symbol "I"
  573. "Symbol indicating a diary entry according to the Islamic calendar."
  574. :type 'string
  575. :group 'diary)
  576. (define-obsolete-variable-alias 'bahai-diary-entry-symbol
  577. 'diary-bahai-entry-symbol "23.1")
  578. (defcustom diary-bahai-entry-symbol "B"
  579. "Symbol indicating a diary entry according to the Baha'i calendar."
  580. :type 'string
  581. :group 'diary)
  582. (defcustom european-calendar-style nil
  583. "Non-nil means use the European style of dates in the diary and display.
  584. In this case, a date like 1/2/1990 would be interpreted as
  585. February 1, 1990. See `diary-european-date-forms' for the
  586. default European diary date styles.
  587. Setting this variable directly does not take effect (if the
  588. calendar package is already loaded). Rather, use either
  589. \\[customize] or the function `calendar-set-date-style'."
  590. :type 'boolean
  591. ;; Without :initialize (require 'calendar) throws an error because
  592. ;; calendar-set-date-style is undefined at this point.
  593. :initialize 'custom-initialize-default
  594. :set (lambda (symbol value)
  595. (if value
  596. (calendar-set-date-style 'european)
  597. (calendar-set-date-style 'american)))
  598. :group 'calendar)
  599. (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
  600. ;; If this is autoloaded, c-d-s gets set before any customization of e-c-s.
  601. (defcustom calendar-date-style (if european-calendar-style 'european
  602. 'american)
  603. "Your preferred style for writing dates.
  604. The options are:
  605. `american' - month/day/year
  606. `european' - day/month/year
  607. `iso' - year/month/day
  608. This affects how dates written in your diary are interpreted.
  609. It also affects date display, as well as those calendar and diary
  610. functions that take a date as an argument, e.g. `diary-date', by
  611. changing the order in which the arguments are interpreted.
  612. Setting this variable directly does not take effect (if the
  613. calendar package is already loaded). Rather, use either
  614. \\[customize] or the function `calendar-set-date-style'."
  615. :version "23.1"
  616. :type '(choice (const american :tag "Month/Day/Year")
  617. (const european :tag "Day/Month/Year")
  618. (const iso :tag "Year/Month/Day"))
  619. :initialize 'custom-initialize-default
  620. :set (lambda (symbol value)
  621. (calendar-set-date-style value))
  622. :group 'calendar)
  623. ;; Next three are provided to aid in setting diary-date-forms.
  624. ;; FIXME move to diary-lib?
  625. (defcustom diary-iso-date-forms
  626. '((month "[-/]" day "[^-/0-9]")
  627. (year "[-/]" month "[-/]" day "[^0-9]")
  628. ;; Cannot allow [-/] as separators here, since it would also match
  629. ;; the first element (bug#7377).
  630. (monthname " *" day "[^-0-9]")
  631. (year " *" monthname " *" day "[^0-9]")
  632. (dayname "\\W"))
  633. "List of pseudo-patterns describing the ISO style of dates.
  634. The defaults are: MONTH[-/]DAY; YEAR[-/]MONTH[-/]DAY; MONTHNAME DAY;
  635. YEAR MONTHNAME DAY; DAYNAME. Normally you should not customize this,
  636. but `diary-date-forms' (which see)."
  637. :version "23.3" ; bug#7377
  638. :type '(repeat (choice (cons :tag "Backup"
  639. :value (backup . nil)
  640. (const backup)
  641. (repeat (list :inline t :format "%v"
  642. (symbol :tag "Keyword")
  643. (choice symbol regexp))))
  644. (repeat (list :inline t :format "%v"
  645. (symbol :tag "Keyword")
  646. (choice symbol regexp)))))
  647. :group 'diary)
  648. (define-obsolete-variable-alias 'american-date-diary-pattern
  649. 'diary-american-date-forms "23.1")
  650. (defcustom diary-american-date-forms
  651. '((month "/" day "[^/0-9]")
  652. (month "/" day "/" year "[^0-9]")
  653. (monthname " *" day "[^,0-9]")
  654. (monthname " *" day ", *" year "[^0-9]")
  655. (dayname "\\W"))
  656. "List of pseudo-patterns describing the American style of dates.
  657. The defaults are: MONTH/DAY; MONTH/DAY/YEAR; MONTHNAME DAY;
  658. MONTHNAME DAY, YEAR; DAYNAME. Normally you should not customize this,
  659. but `diary-date-forms' (which see)."
  660. :type '(repeat (choice (cons :tag "Backup"
  661. :value (backup . nil)
  662. (const backup)
  663. (repeat (list :inline t :format "%v"
  664. (symbol :tag "Keyword")
  665. (choice symbol regexp))))
  666. (repeat (list :inline t :format "%v"
  667. (symbol :tag "Keyword")
  668. (choice symbol regexp)))))
  669. :group 'diary)
  670. (define-obsolete-variable-alias 'european-date-diary-pattern
  671. 'diary-european-date-forms "23.1")
  672. (defcustom diary-european-date-forms
  673. '((day "/" month "[^/0-9]")
  674. (day "/" month "/" year "[^0-9]")
  675. (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|\\([0-9]+[:aApP]\\)\\)")
  676. (day " *" monthname " *" year "[^0-9]")
  677. (dayname "\\W"))
  678. "List of pseudo-patterns describing the European style of dates.
  679. The defaults are: DAY/MONTH; DAY/MONTH/YEAR; DAY MONTHNAME;
  680. DAY MONTHNAME YEAR; DAYNAME. Normally you should not customize this, but
  681. `diary-date-forms' (which see)."
  682. :type '(repeat (choice (cons :tag "Backup"
  683. :value (backup . nil)
  684. (const backup)
  685. (repeat (list :inline t :format "%v"
  686. (symbol :tag "Keyword")
  687. (choice symbol regexp))))
  688. (repeat (list :inline t :format "%v"
  689. (symbol :tag "Keyword")
  690. (choice symbol regexp)))))
  691. :group 'diary)
  692. (defvar diary-font-lock-keywords)
  693. (defcustom diary-date-forms (cond ((eq calendar-date-style 'iso)
  694. diary-iso-date-forms)
  695. ((eq calendar-date-style 'european)
  696. diary-european-date-forms)
  697. (t diary-american-date-forms))
  698. "List of pseudo-patterns describing the forms of date used in the diary.
  699. The patterns on the list must be MUTUALLY EXCLUSIVE and should not match
  700. any portion of the diary entry itself, just the date component.
  701. A pseudo-pattern is a list of regular expressions and the keywords `month',
  702. `day', `year', `monthname', and `dayname'. The keyword `monthname' will
  703. match the name of the month (see `calendar-month-name-array'), capitalized
  704. or not, or its user-specified abbreviation (see `calendar-month-abbrev-array'),
  705. followed by a period or not; it will also match `*'. Similarly, `dayname'
  706. will match the name of the day (see `calendar-day-name-array'), capitalized or
  707. not, or its user-specified abbreviation (see `calendar-day-abbrev-array'),
  708. followed by a period or not. The keywords `month', `day', and `year' will
  709. match those numerical values, preceded by arbitrarily many zeros; they will
  710. also match `*'.
  711. The matching of the diary entries with the date forms is done with the
  712. standard syntax table from Fundamental mode, but with the `*' changed so
  713. that it is a word constituent.
  714. If, to be mutually exclusive, a pseudo-pattern must match a portion of the
  715. diary entry itself, the first element of the pattern MUST be `backup'. This
  716. directive causes the date recognizer to back up to the beginning of the
  717. current word of the diary entry, so in no case can the pattern match more than
  718. a portion of the first word of the diary entry.
  719. For examples of three common styles, see `diary-american-date-forms',
  720. `diary-european-date-forms', and `diary-iso-date-forms'."
  721. :type '(repeat (choice (cons :tag "Backup"
  722. :value (backup . nil)
  723. (const backup)
  724. (repeat (list :inline t :format "%v"
  725. (symbol :tag "Keyword")
  726. (choice symbol regexp))))
  727. (repeat (list :inline t :format "%v"
  728. (symbol :tag "Keyword")
  729. (choice symbol regexp)))))
  730. :set-after '(calendar-date-style diary-iso-date-forms
  731. diary-european-date-forms
  732. diary-american-date-forms)
  733. :initialize 'custom-initialize-default
  734. :set (lambda (symbol value)
  735. (unless (equal value (eval symbol))
  736. (custom-set-default symbol value)
  737. (setq diary-font-lock-keywords (diary-font-lock-keywords))
  738. ;; Need to redraw not just to get new font-locking, but also
  739. ;; to pick up any newly recognized entries.
  740. (and (diary-live-p)
  741. (diary))))
  742. :group 'diary)
  743. ;; Next three are provided to aid in setting calendar-date-display-form.
  744. (defcustom calendar-iso-date-display-form '((format "%s-%.2d-%.2d" year
  745. (string-to-number month)
  746. (string-to-number day)))
  747. "Pseudo-pattern governing the way a date appears in the ISO style.
  748. Normally you should not customize this, but `calendar-date-display-form'
  749. \(which see)."
  750. :type 'sexp
  751. :version "23.1"
  752. :group 'calendar)
  753. (define-obsolete-variable-alias 'european-calendar-display-form
  754. 'calendar-european-date-display-form "23.1")
  755. (defcustom calendar-european-date-display-form
  756. '((if dayname (concat dayname ", ")) day " " monthname " " year)
  757. "Pseudo-pattern governing the way a date appears in the European style.
  758. Normally you should not customize this, but `calendar-date-display-form'
  759. \(which see)."
  760. :type 'sexp
  761. :group 'calendar)
  762. (define-obsolete-variable-alias 'american-calendar-display-form
  763. 'calendar-american-date-display-form "23.1")
  764. (defcustom calendar-american-date-display-form
  765. '((if dayname (concat dayname ", ")) monthname " " day ", " year)
  766. "Pseudo-pattern governing the way a date appears in the American style.
  767. Normally you should not customize this, but `calendar-date-display-form'
  768. \(which see)."
  769. :type 'sexp
  770. :group 'calendar)
  771. (defcustom calendar-date-display-form
  772. (cond ((eq calendar-date-style 'iso)
  773. calendar-iso-date-display-form)
  774. ((eq calendar-date-style 'european)
  775. calendar-european-date-display-form)
  776. (t calendar-american-date-display-form))
  777. "Pseudo-pattern governing the way a calendar date appears.
  778. Used by the function `calendar-date-string' (which see), a pseudo-pattern
  779. is a list of expressions that can involve the keywords `month', `day',
  780. and `year' (all numbers in string form), and `monthname' and `dayname'
  781. \(both alphabetic strings). For example, a typical American form would be
  782. '(month \"/\" day \"/\" (substring year -2))
  783. whereas
  784. '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
  785. would give the usual American style in fixed-length fields. The variables
  786. `calendar-iso-date-display-form', `calendar-european-date-display-form', and
  787. `calendar-american-date-display-form' provide some defaults for three common
  788. styles."
  789. :type 'sexp
  790. :set-after '(calendar-date-style calendar-iso-date-display-form
  791. calendar-european-date-display-form
  792. calendar-american-date-display-form)
  793. :group 'calendar)
  794. (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 this year
  1052. (* 365 offset-years) ; + days in prior years
  1053. (/ offset-years 4) ; + Julian leap years
  1054. (- (/ offset-years 100)) ; - century years
  1055. (/ offset-years 400))) ; + Gregorian leap years
  1056. (t
  1057. ;; Years between date and 1 BC, excluding 1 BC (1 for 2 BC, etc).
  1058. (setq offset-years (abs (1+ year)))
  1059. (- (calendar-day-number date)
  1060. (* 365 offset-years)
  1061. (/ offset-years 4)
  1062. (- (/ offset-years 100))
  1063. (/ offset-years 400)
  1064. (calendar-day-number '(12 31 -1))))))) ; days in year 1 BC
  1065. ;;;###autoload
  1066. (defun calendar (&optional arg)
  1067. "Display a three-month Gregorian calendar.
  1068. The three months appear side by side, with the current month in
  1069. the middle surrounded by the previous and next months. The
  1070. cursor is put on today's date. If optional prefix argument ARG
  1071. is non-nil, prompts for the central month and year.
  1072. Once in the calendar window, future or past months can be moved
  1073. into view. Arbitrary months can be displayed, or the calendar
  1074. can be scrolled forward or backward. The cursor can be moved
  1075. forward or backward by one day, one week, one month, or one year.
  1076. All of these commands take prefix arguments which, when negative,
  1077. cause movement in the opposite direction. For convenience, the
  1078. digit keys and the minus sign are automatically prefixes. Use
  1079. \\[describe-mode] for details of the key bindings in the calendar
  1080. window.
  1081. Displays the calendar in a separate window, or optionally in a
  1082. separate frame, depending on the value of `calendar-setup'.
  1083. If `calendar-view-diary-initially-flag' is non-nil, also displays the
  1084. diary entries for the current date (or however many days
  1085. `diary-number-of-entries' specifies). This variable can be
  1086. overridden by `calendar-setup'. As well as being displayed,
  1087. diary entries can also be marked on the calendar (see
  1088. `calendar-mark-diary-entries-flag').
  1089. Runs the following hooks:
  1090. `calendar-load-hook' - after loading calendar.el
  1091. `calendar-today-visible-hook', `calendar-today-invisible-hook' - after
  1092. generating a calendar, if today's date is visible or not, respectively
  1093. `calendar-initial-window-hook' - after first creating a calendar
  1094. This function is suitable for execution in a .emacs file."
  1095. (interactive "P")
  1096. ;; Avoid loading cal-x unless it will be used.
  1097. (if (and (memq calendar-setup '(one-frame two-frames calendar-only))
  1098. (display-multi-frame-p))
  1099. (calendar-frame-setup calendar-setup arg)
  1100. (calendar-basic-setup arg)))
  1101. (defun calendar-basic-setup (&optional arg nodisplay)
  1102. "Create a three-month calendar.
  1103. If optional prefix argument ARG is non-nil, prompts for the month
  1104. and year, else uses the current date. If NODISPLAY is non-nil, don't
  1105. display the generated calendar."
  1106. (interactive "P")
  1107. (let ((buff (current-buffer)))
  1108. (set-buffer (get-buffer-create calendar-buffer))
  1109. (calendar-mode)
  1110. (let* ((pop-up-windows t)
  1111. ;; Not really needed now, but means we use exactly the same
  1112. ;; behavior as before in the non-wide case (see below).
  1113. (split-height-threshold 1000)
  1114. (split-width-threshold calendar-split-width-threshold)
  1115. (date (if arg (calendar-read-date t)
  1116. (calendar-current-date)))
  1117. (month (calendar-extract-month date))
  1118. (year (calendar-extract-year date)))
  1119. (calendar-increment-month month year (- calendar-offset))
  1120. ;; Display the buffer before calling calendar-generate-window so that it
  1121. ;; can get a chance to adjust the window sizes to the frame size.
  1122. (unless nodisplay
  1123. ;; We want a window configuration that looks something like
  1124. ;; X X | Y
  1125. ;; - -----
  1126. ;; C Z | C
  1127. ;; where C is the calendar, and the LHS is the traditional,
  1128. ;; non-wide frame, and the RHS is the wide frame case.
  1129. ;; We should end up in the same state regardless of whether the
  1130. ;; windows were initially split or not.
  1131. ;; Previously, we only thought about the non-wide case.
  1132. ;; We could just set split-height-threshold to 1000, relying on
  1133. ;; the fact that the window splitting treated a single window as
  1134. ;; a special case and would always split it (vertically). The
  1135. ;; same thing does not work in the wide-frame case, so now we do
  1136. ;; the splitting by hand.
  1137. ;; See discussion in bug#1806.
  1138. ;; Actually, this still does not do quite the right thing in the
  1139. ;; wide frame case if started from a configuration like the LHS.
  1140. ;; Eg if you start with a non-wide frame, call calendar, then
  1141. ;; make the frame wider. This one is problematic because you
  1142. ;; might need to split a totally unrelated window. Oh well, it
  1143. ;; seems unlikely, and perhaps respecting the original layout is
  1144. ;; the right thing in that case.
  1145. ;;
  1146. ;; Is this a wide frame? If so, split it horizontally.
  1147. (if (window-splittable-p t) (split-window-horizontally))
  1148. (pop-to-buffer calendar-buffer)
  1149. ;; Has the window already been split vertically?
  1150. (when (and (not (window-dedicated-p))
  1151. (window-full-height-p))
  1152. (let ((win (split-window-vertically)))
  1153. ;; In the upper window, show whatever was visible before.
  1154. ;; This looks better than using other-buffer.
  1155. (switch-to-buffer buff)
  1156. ;; Switch to the lower window with the calendar buffer.
  1157. (select-window win))))
  1158. (calendar-generate-window month year)
  1159. (if (and calendar-view-diary-initially-flag
  1160. (calendar-date-is-visible-p date))
  1161. (diary-view-entries))))
  1162. (if calendar-view-holidays-initially-flag
  1163. (let* ((diary-buffer (get-file-buffer diary-file))
  1164. (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
  1165. (split-height-threshold (if diary-window 2 1000)))
  1166. ;; FIXME display buffer?
  1167. (calendar-list-holidays)))
  1168. (run-hooks 'calendar-initial-window-hook))
  1169. (defun calendar-generate-window (&optional mon yr)
  1170. "Generate the calendar window for the current date.
  1171. Optional integers MON and YR are used instead of today's date."
  1172. (let* ((inhibit-read-only t)
  1173. (today (calendar-current-date))
  1174. (month (calendar-extract-month today))
  1175. (day (calendar-extract-day today))
  1176. (year (calendar-extract-year today))
  1177. (today-visible (or (not mon)
  1178. (<= (abs (calendar-interval mon yr month year)) 1)))
  1179. (in-calendar-window (eq (window-buffer (selected-window))
  1180. (get-buffer calendar-buffer))))
  1181. (calendar-generate (or mon month) (or yr year))
  1182. (calendar-update-mode-line)
  1183. (calendar-cursor-to-visible-date
  1184. (if today-visible today (list displayed-month 1 displayed-year)))
  1185. (set-buffer-modified-p nil)
  1186. ;; Don't do any window-related stuff if we weren't called from a
  1187. ;; window displaying the calendar.
  1188. (when in-calendar-window
  1189. (if (window-iso-combined-p)
  1190. ;; Adjust the window to exactly fit the displayed calendar.
  1191. (fit-window-to-buffer nil nil calendar-minimum-window-height)
  1192. ;; For a full height window or a window that is horizontally
  1193. ;; combined don't fit height to that of its buffer.
  1194. (set-window-vscroll nil 0))
  1195. (sit-for 0))
  1196. (and (bound-and-true-p font-lock-mode)
  1197. (font-lock-fontify-buffer))
  1198. (and calendar-mark-holidays-flag
  1199. ;;; (calendar-date-is-valid-p today) ; useful for BC dates
  1200. (calendar-mark-holidays)
  1201. (and in-calendar-window (sit-for 0)))
  1202. (unwind-protect
  1203. (if calendar-mark-diary-entries-flag (diary-mark-entries))
  1204. (if today-visible
  1205. (run-hooks 'calendar-today-visible-hook)
  1206. (run-hooks 'calendar-today-invisible-hook)))))
  1207. (defun calendar-generate (month year)
  1208. "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
  1209. ;; A negative YEAR is interpreted as BC; -1 being 1 BC, and so on.
  1210. ;; Note that while calendars for years BC could be displayed as it
  1211. ;; stands, almost all other calendar functions (eg holidays) would
  1212. ;; at best have unpredictable results for such dates.
  1213. (if (< (+ month (* 12 (1- year))) 2)
  1214. (error "Months before January, 1 AD cannot be displayed"))
  1215. (setq displayed-month month
  1216. displayed-year year)
  1217. (erase-buffer)
  1218. (calendar-increment-month month year -1)
  1219. (dotimes (i 3)
  1220. (calendar-generate-month month year
  1221. (+ calendar-left-margin
  1222. (* calendar-month-width i)))
  1223. (calendar-increment-month month year 1)))
  1224. (defun calendar-move-to-column (indent)
  1225. "Like `move-to-column', but indents if the line is too short."
  1226. (if (< (move-to-column indent) indent)
  1227. (indent-to indent)))
  1228. (defun calendar-ensure-newline ()
  1229. "Move to the next line, adding a newline if necessary."
  1230. (or (zerop (forward-line 1))
  1231. (insert "\n")))
  1232. (defun calendar-insert-at-column (indent string truncate)
  1233. "Move to column INDENT, adding spaces as needed.
  1234. Inserts STRING so that it ends at INDENT. STRING is either a
  1235. literal string, or a sexp to evaluate to return such. Truncates
  1236. STRING to length TRUNCATE, ensure a trailing space."
  1237. (if (not (ignore-errors (stringp (setq string (eval string)))))
  1238. (calendar-move-to-column indent)
  1239. (if (> (length string) truncate)
  1240. (setq string (substring string 0 truncate)))
  1241. (or (string-match " $" string)
  1242. (if (= (length string) truncate)
  1243. (aset string (1- truncate) ?\s)
  1244. (setq string (concat string " "))))
  1245. (calendar-move-to-column (- indent (length string)))
  1246. (insert string)))
  1247. (defun calendar-generate-month (month year indent)
  1248. "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
  1249. The calendar is inserted at the top of the buffer in which point is currently
  1250. located, but indented INDENT spaces. The indentation is done from the first
  1251. character on the line and does not disturb the first INDENT characters on the
  1252. line."
  1253. (let ((blank-days ; at start of month
  1254. (mod
  1255. (- (calendar-day-of-week (list month 1 year))
  1256. calendar-week-start-day)
  1257. 7))
  1258. (last (calendar-last-day-of-month month year))
  1259. (trunc (min calendar-intermonth-spacing
  1260. (1- calendar-left-margin)))
  1261. (day 1)
  1262. string)
  1263. (goto-char (point-min))
  1264. (calendar-move-to-column indent)
  1265. (insert
  1266. (calendar-string-spread
  1267. (list (format "%s %d" (calendar-month-name month) year))
  1268. ?\s calendar-month-digit-width))
  1269. (calendar-ensure-newline)
  1270. (calendar-insert-at-column indent calendar-intermonth-header trunc)
  1271. ;; Use the first two characters of each day to head the columns.
  1272. (dotimes (i 7)
  1273. (insert
  1274. (progn
  1275. (setq string
  1276. (calendar-day-name (mod (+ calendar-week-start-day i) 7) nil t))
  1277. (if enable-multibyte-characters
  1278. (truncate-string-to-width string calendar-day-header-width)
  1279. (substring string 0 calendar-day-header-width)))
  1280. (make-string (- calendar-column-width calendar-day-header-width) ?\s)))
  1281. (calendar-ensure-newline)
  1282. (calendar-insert-at-column indent calendar-intermonth-text trunc)
  1283. ;; Add blank days before the first of the month.
  1284. (insert (make-string (* blank-days calendar-column-width) ?\s))
  1285. ;; Put in the days of the month.
  1286. (dotimes (i last)
  1287. (setq day (1+ i))
  1288. ;; TODO should numbers be left-justified, centered...?
  1289. (insert (format (format "%%%dd%%s" calendar-day-digit-width) day
  1290. (make-string
  1291. (- calendar-column-width calendar-day-digit-width) ?\s)))
  1292. ;; 'date property prevents intermonth text confusing re-searches.
  1293. ;; (Tried intangible, it did not really work.)
  1294. (set-text-properties
  1295. (- (point) (1+ calendar-day-digit-width)) (1- (point))
  1296. `(mouse-face highlight help-echo ,(eval calendar-date-echo-text)
  1297. date t))
  1298. (when (and (zerop (mod (+ day blank-days) 7))
  1299. (/= day last))
  1300. (calendar-ensure-newline)
  1301. (setq day (1+ day)) ; first day of next week
  1302. (calendar-insert-at-column indent calendar-intermonth-text trunc)))))
  1303. (defun calendar-redraw ()
  1304. "Redraw the calendar display, if `calendar-buffer' is live."
  1305. (interactive)
  1306. (if (get-buffer calendar-buffer)
  1307. (with-current-buffer calendar-buffer
  1308. (let ((cursor-date (calendar-cursor-to-nearest-date)))
  1309. (calendar-generate-window displayed-month displayed-year)
  1310. (calendar-cursor-to-visible-date cursor-date)))))
  1311. (defvar calendar-mode-map
  1312. (let ((map (make-keymap)))
  1313. (suppress-keymap map)
  1314. (dolist (c '(narrow-to-region mark-word mark-sexp mark-paragraph
  1315. mark-defun mark-whole-buffer mark-page
  1316. downcase-region upcase-region kill-region
  1317. copy-region-as-kill capitalize-region write-region))
  1318. (define-key map (vector 'remap c) 'calendar-not-implemented))
  1319. (define-key map "<" 'calendar-scroll-right)
  1320. (define-key map "\C-x<" 'calendar-scroll-right)
  1321. (define-key map [prior] 'calendar-scroll-right-three-months)
  1322. (define-key map "\ev" 'calendar-scroll-right-three-months)
  1323. (define-key map ">" 'calendar-scroll-left)
  1324. (define-key map "\C-x>" 'calendar-scroll-left)
  1325. (define-key map [next] 'calendar-scroll-left-three-months)
  1326. (define-key map "\C-v" 'calendar-scroll-left-three-months)
  1327. (define-key map "\C-b" 'calendar-backward-day)
  1328. (define-key map "\C-p" 'calendar-backward-week)
  1329. (define-key map "\e{" 'calendar-backward-month)
  1330. (define-key map "\C-x[" 'calendar-backward-year)
  1331. (define-key map "\C-f" 'calendar-forward-day)
  1332. (define-key map "\C-n" 'calendar-forward-week)
  1333. (define-key map [left] 'calendar-backward-day)
  1334. (define-key map [up] 'calendar-backward-week)
  1335. (define-key map [right] 'calendar-forward-day)
  1336. (define-key map [down] 'calendar-forward-week)
  1337. (define-key map "\e}" 'calendar-forward-month)
  1338. (define-key map "\C-x]" 'calendar-forward-year)
  1339. (define-key map "\C-a" 'calendar-beginning-of-week)
  1340. (define-key map "\C-e" 'calendar-end-of-week)
  1341. (define-key map "\ea" 'calendar-beginning-of-month)
  1342. (define-key map "\ee" 'calendar-end-of-month)
  1343. (define-key map "\e<" 'calendar-beginning-of-year)
  1344. (define-key map "\e>" 'calendar-end-of-year)
  1345. (define-key map "\C-@" 'calendar-set-mark)
  1346. ;; Many people are used to typing C-SPC and getting C-@.
  1347. (define-key map [?\C-\s] 'calendar-set-mark)
  1348. (define-key map "\C-x\C-x" 'calendar-exchange-point-and-mark)
  1349. (define-key map "\e=" 'calendar-count-days-region)
  1350. (define-key map "gd" 'calendar-goto-date)
  1351. (define-key map "gD" 'calendar-goto-day-of-year)
  1352. (define-key map "gj" 'calendar-julian-goto-date)
  1353. (define-key map "ga" 'calendar-astro-goto-day-number)
  1354. (define-key map "gh" 'calendar-hebrew-goto-date)
  1355. (define-key map "gi" 'calendar-islamic-goto-date)
  1356. (define-key map "gb" 'calendar-bahai-goto-date)
  1357. (define-key map "gC" 'calendar-chinese-goto-date)
  1358. (define-key map "gk" 'calendar-coptic-goto-date)
  1359. (define-key map "ge" 'calendar-ethiopic-goto-date)
  1360. (define-key map "gp" 'calendar-persian-goto-date)
  1361. (define-key map "gc" 'calendar-iso-goto-date)
  1362. (define-key map "gw" 'calendar-iso-goto-week)
  1363. (define-key map "gf" 'calendar-french-goto-date)
  1364. (define-key map "gml" 'calendar-mayan-goto-long-count-date)
  1365. (define-key map "gmpc" 'calendar-mayan-previous-round-date)
  1366. (define-key map "gmnc" 'calendar-mayan-next-round-date)
  1367. (define-key map "gmph" 'calendar-mayan-previous-haab-date)
  1368. (define-key map "gmnh" 'calendar-mayan-next-haab-date)
  1369. (define-key map "gmpt" 'calendar-mayan-previous-tzolkin-date)
  1370. (define-key map "gmnt" 'calendar-mayan-next-tzolkin-date)
  1371. (define-key map "Aa" 'appt-add)
  1372. (define-key map "Ad" 'appt-delete)
  1373. (define-key map "S" 'calendar-sunrise-sunset)
  1374. (define-key map "M" 'calendar-lunar-phases)
  1375. (define-key map " " 'scroll-other-window)
  1376. (define-key map "\d" 'scroll-other-window-down)
  1377. (define-key map "\C-c\C-l" 'calendar-redraw)
  1378. (define-key map "." 'calendar-goto-today)
  1379. (define-key map "o" 'calendar-other-month)
  1380. (define-key map "q" 'calendar-exit)
  1381. (define-key map "a" 'calendar-list-holidays)
  1382. (define-key map "h" 'calendar-cursor-holidays)
  1383. (define-key map "x" 'calendar-mark-holidays)
  1384. (define-key map "u" 'calendar-unmark)
  1385. (define-key map "m" 'diary-mark-entries)
  1386. (define-key map "d" 'diary-view-entries)
  1387. (define-key map "D" 'diary-view-other-diary-entries)
  1388. (define-key map "s" 'diary-show-all-entries)
  1389. (define-key map "pd" 'calendar-print-day-of-year)
  1390. (define-key map "pC" 'calendar-chinese-print-date)
  1391. (define-key map "pk" 'calendar-coptic-print-date)
  1392. (define-key map "pe" 'calendar-ethiopic-print-date)
  1393. (define-key map "pp" 'calendar-persian-print-date)
  1394. (define-key map "pc" 'calendar-iso-print-date)
  1395. (define-key map "pj" 'calendar-julian-print-date)
  1396. (define-key map "pa" 'calendar-astro-print-day-number)
  1397. (define-key map "ph" 'calendar-hebrew-print-date)
  1398. (define-key map "pi" 'calendar-islamic-print-date)
  1399. (define-key map "pb" 'calendar-bahai-print-date)
  1400. (define-key map "pf" 'calendar-french-print-date)
  1401. (define-key map "pm" 'calendar-mayan-print-date)
  1402. (define-key map "po" 'calendar-print-other-dates)
  1403. (define-key map "id" 'diary-insert-entry)
  1404. (define-key map "iw" 'diary-insert-weekly-entry)
  1405. (define-key map "im" 'diary-insert-monthly-entry)
  1406. (define-key map "iy" 'diary-insert-yearly-entry)
  1407. (define-key map "ia" 'diary-insert-anniversary-entry)
  1408. (define-key map "ib" 'diary-insert-block-entry)
  1409. (define-key map "ic" 'diary-insert-cyclic-entry)
  1410. (define-key map "ihd" 'diary-hebrew-insert-entry)
  1411. (define-key map "ihm" 'diary-hebrew-insert-monthly-entry)
  1412. (define-key map "ihy" 'diary-hebrew-insert-yearly-entry)
  1413. (define-key map "iid" 'diary-islamic-insert-entry)
  1414. (define-key map "iim" 'diary-islamic-insert-monthly-entry)
  1415. (define-key map "iiy" 'diary-islamic-insert-yearly-entry)
  1416. (define-key map "iBd" 'diary-bahai-insert-entry)
  1417. (define-key map "iBm" 'diary-bahai-insert-monthly-entry)
  1418. (define-key map "iBy" 'diary-bahai-insert-yearly-entry)
  1419. (define-key map "?" 'calendar-goto-info-node)
  1420. (define-key map "Hm" 'cal-html-cursor-month)
  1421. (define-key map "Hy" 'cal-html-cursor-year)
  1422. (define-key map "tm" 'cal-tex-cursor-month)
  1423. (define-key map "tM" 'cal-tex-cursor-month-landscape)
  1424. (define-key map "td" 'cal-tex-cursor-day)
  1425. (define-key map "tw1" 'cal-tex-cursor-week)
  1426. (define-key map "tw2" 'cal-tex-cursor-week2)
  1427. (define-key map "tw3" 'cal-tex-cursor-week-iso)
  1428. (define-key map "tw4" 'cal-tex-cursor-week-monday)
  1429. (define-key map "tfd" 'cal-tex-cursor-filofax-daily)
  1430. (define-key map "tfw" 'cal-tex-cursor-filofax-2week)
  1431. (define-key map "tfW" 'cal-tex-cursor-filofax-week)
  1432. (define-key map "tfy" 'cal-tex-cursor-filofax-year)
  1433. (define-key map "ty" 'cal-tex-cursor-year)
  1434. (define-key map "tY" 'cal-tex-cursor-year-landscape)
  1435. (define-key map [menu-bar edit] 'undefined)
  1436. (define-key map [menu-bar search] 'undefined)
  1437. (easy-menu-define nil map nil cal-menu-sunmoon-menu)
  1438. (easy-menu-define nil map nil cal-menu-diary-menu)
  1439. (easy-menu-define nil map nil cal-menu-holidays-menu)
  1440. (easy-menu-define nil map nil cal-menu-goto-menu)
  1441. (easy-menu-define nil map nil cal-menu-scroll-menu)
  1442. ;; These are referenced in the default calendar-date-echo-text.
  1443. (define-key map [down-mouse-3]
  1444. (easy-menu-binding cal-menu-context-mouse-menu))
  1445. (define-key map [down-mouse-2]
  1446. (easy-menu-binding cal-menu-global-mouse-menu))
  1447. ;; cf scroll-bar.el.
  1448. (if (and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
  1449. (define-key map [vertical-scroll-bar mouse-1]
  1450. 'calendar-scroll-toolkit-scroll)
  1451. ;; Left-click moves us forward in time, right-click backwards.
  1452. (define-key map [vertical-scroll-bar mouse-1] 'calendar-scroll-left)
  1453. (define-key map [vertical-scroll-bar drag-mouse-1] 'calendar-scroll-left)
  1454. ;; down-mouse-2 stays as scroll-bar-drag.
  1455. (define-key map [vertical-scroll-bar mouse-3] 'calendar-scroll-right)
  1456. (define-key map [vertical-scroll-bar drag-mouse-3]
  1457. 'calendar-scroll-right))
  1458. map)
  1459. "Keymap for `calendar-mode'.")
  1460. ;; Calendar mode is suitable only for specially formatted data.
  1461. (put 'calendar-mode 'mode-class 'special)
  1462. (defun calendar-mode-line-entry (command echo &optional key string)
  1463. "Return a propertized string for `calendar-mode-line-format'.
  1464. COMMAND is a command to run, ECHO is the help-echo text, KEY
  1465. is COMMAND's keybinding, STRING describes the binding."
  1466. (propertize (or key
  1467. (substitute-command-keys
  1468. (format "\\<calendar-mode-map>\\[%s] %s" command string)))
  1469. 'help-echo (format "mouse-1: %s" echo)
  1470. 'mouse-face 'mode-line-highlight
  1471. 'keymap (make-mode-line-mouse-map 'mouse-1 command)))
  1472. ;; After calendar-mode-map.
  1473. (defcustom calendar-mode-line-format
  1474. (list
  1475. (calendar-mode-line-entry 'calendar-scroll-right "previous month" "<")
  1476. "Calendar"
  1477. (concat
  1478. (calendar-mode-line-entry 'calendar-goto-info-node "read Info on Calendar"
  1479. nil "info")
  1480. " / "
  1481. (calendar-mode-line-entry 'calendar-other-month "choose another month"
  1482. nil "other")
  1483. " / "
  1484. (calendar-mode-line-entry 'calendar-goto-today "go to today's date"
  1485. nil "today"))
  1486. '(calendar-date-string (calendar-current-date) t)
  1487. (calendar-mode-line-entry 'calendar-scroll-left "next month" ">"))
  1488. "The mode line of the calendar buffer.
  1489. This is a list of items that evaluate to strings. The elements
  1490. are evaluated and concatenated, evenly separated by blanks.
  1491. During evaluation, the variable `date' is available as the date
  1492. nearest the cursor (or today's date if that fails). To update
  1493. the mode-line as the cursor moves, add `calendar-update-mode-line'
  1494. to `calendar-move-hook'. Here is an example that has the Hebrew date,
  1495. the day number/days remaining in the year, and the ISO week/year numbers:
  1496. (list
  1497. \"\"
  1498. '(calendar-hebrew-date-string date)
  1499. '(let* ((year (calendar-extract-year date))
  1500. (d (calendar-day-number date))
  1501. (days-remaining
  1502. (- (calendar-day-number (list 12 31 year)) d)))
  1503. (format \"%d/%d\" d days-remaining))
  1504. '(let* ((d (calendar-absolute-from-gregorian date))
  1505. (iso-date (calendar-iso-from-absolute d)))
  1506. (format \"ISO week %d of %d\"
  1507. (calendar-extract-month iso-date)
  1508. (calendar-extract-year iso-date)))
  1509. \"\"))"
  1510. :risky t
  1511. :type 'sexp
  1512. :group 'calendar)
  1513. (defun calendar-goto-info-node ()
  1514. "Go to the info node for the calendar."
  1515. (interactive)
  1516. (info "(emacs)Calendar/Diary")
  1517. (fit-window-to-buffer))
  1518. (defvar calendar-mark-ring nil
  1519. "Used by `calendar-set-mark'.")
  1520. (define-derived-mode calendar-mode nil "Calendar"
  1521. "A major mode for the calendar window.
  1522. For a complete description, see the info node `Calendar/Diary'.
  1523. \\<calendar-mode-map>\\{calendar-mode-map}"
  1524. (setq buffer-read-only t
  1525. buffer-undo-list t
  1526. indent-tabs-mode nil)
  1527. (calendar-update-mode-line)
  1528. (make-local-variable 'calendar-mark-ring)
  1529. (make-local-variable 'displayed-month) ; month in middle of window
  1530. (make-local-variable 'displayed-year) ; year in middle of window
  1531. ;; Most functions only work if displayed-month and displayed-year are set,
  1532. ;; so let's make sure they're always set. Most likely, this will be reset
  1533. ;; soon in calendar-generate, but better safe than sorry.
  1534. (unless (boundp 'displayed-month) (setq displayed-month 1))
  1535. (unless (boundp 'displayed-year) (setq displayed-year 2001))
  1536. (set (make-local-variable 'font-lock-defaults)
  1537. '(calendar-font-lock-keywords t)))
  1538. (defun calendar-string-spread (strings char length)
  1539. "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH.
  1540. The effect is like mapconcat but the separating pieces are as balanced as
  1541. possible. Each item of STRINGS is evaluated before concatenation so it can
  1542. actually be an expression that evaluates to a string. If LENGTH is too short,
  1543. the STRINGS are just concatenated and the result truncated."
  1544. ;; The algorithm is based on equation (3.25) on page 85 of Concrete
  1545. ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
  1546. ;; Addison-Wesley, Reading, MA, 1989.
  1547. (let* ((strings (mapcar 'eval
  1548. (if (< (length strings) 2)
  1549. (append (list "") strings (list ""))
  1550. strings)))
  1551. (n (- length (length (apply 'concat strings))))
  1552. (m (1- (length strings)))
  1553. (s (car strings))
  1554. (strings (cdr strings))
  1555. (i 0))
  1556. (dolist (string strings)
  1557. (setq s (concat s
  1558. (make-string (max 0 (/ (+ n i) m)) char)
  1559. string)
  1560. i (1+ i)))
  1561. (substring s 0 length)))
  1562. (defun calendar-update-mode-line ()
  1563. "Update the calendar mode line with the current date and date style."
  1564. (if (bufferp (get-buffer calendar-buffer))
  1565. (with-current-buffer calendar-buffer
  1566. (let ((start (- calendar-left-margin 2))
  1567. (date (condition-case nil
  1568. (calendar-cursor-to-nearest-date)
  1569. (error (calendar-current-date)))))
  1570. (setq mode-line-format
  1571. (concat (make-string (max 0 (+ start
  1572. (- (car (window-inside-edges))
  1573. (car (window-edges))))) ?\s)
  1574. (calendar-string-spread
  1575. (mapcar 'eval calendar-mode-line-format)
  1576. ?\s (- calendar-right-margin (1- start))))))
  1577. (force-mode-line-update))))
  1578. (defun calendar-window-list ()
  1579. "List of all calendar-related windows."
  1580. (let ((calendar-buffers (calendar-buffer-list))
  1581. list)
  1582. ;; Using 0 rather than t for last argument - see bug#2199.
  1583. ;; This is only used with calendar-hide-window, which ignores
  1584. ;; iconified frames anyway, so could use 'visible rather than 0.
  1585. (walk-windows (lambda (w)
  1586. (if (memq (window-buffer w) calendar-buffers)
  1587. (push w list)))
  1588. nil 0)
  1589. list))
  1590. (defun calendar-buffer-list ()
  1591. "List of all calendar-related buffers (as buffers, not strings)."
  1592. (let (buffs)
  1593. (dolist (b (list calendar-hebrew-yahrzeit-buffer lunar-phases-buffer
  1594. holiday-buffer diary-fancy-buffer solar-sunrises-buffer
  1595. (get-file-buffer diary-file)
  1596. calendar-buffer calendar-other-calendars-buffer))
  1597. (and b (setq b (get-buffer b))
  1598. (push b buffs)))
  1599. buffs))
  1600. (defun calendar-exit ()
  1601. "Get out of the calendar window and hide it and related buffers."
  1602. (interactive)
  1603. (let ((diary-buffer (get-file-buffer diary-file)))
  1604. (if (or (not diary-buffer)
  1605. (not (buffer-modified-p diary-buffer))
  1606. (yes-or-no-p
  1607. "Diary modified; do you really want to exit the calendar? "))
  1608. ;; Need to do this multiple times because one time can replace some
  1609. ;; calendar-related buffers with other calendar-related buffers.
  1610. (mapc (lambda (x)
  1611. (mapc 'calendar-hide-window (calendar-window-list)))
  1612. (calendar-window-list)))))
  1613. (define-obsolete-function-alias 'exit-calendar 'calendar-exit "23.1")
  1614. (defun calendar-hide-window (window)
  1615. "Hide WINDOW if it is calendar-related."
  1616. (let ((buffer (if (window-live-p window) (window-buffer window))))
  1617. (if (memq buffer (calendar-buffer-list))
  1618. (cond
  1619. ((and (display-multi-frame-p)
  1620. (eq 'icon (cdr (assoc 'visibility
  1621. (frame-parameters
  1622. (window-frame window))))))
  1623. nil)
  1624. ((and (display-multi-frame-p) (window-dedicated-p window))
  1625. (if calendar-remove-frame-by-deleting
  1626. (delete-frame (window-frame window))
  1627. (iconify-frame (window-frame window))))
  1628. ((not (and (select-window window) (one-window-p window)))
  1629. (delete-window window))
  1630. (t (set-buffer buffer)
  1631. (bury-buffer))))))
  1632. (defun calendar-current-date (&optional offset)
  1633. "Return the current date in a list (month day year).
  1634. Optional integer OFFSET is a number of days from the current date."
  1635. (let* ((now (decode-time))
  1636. (now (list (nth 4 now) (nth 3 now) (nth 5 now))))
  1637. (if (zerop (or offset 0))
  1638. now
  1639. (calendar-gregorian-from-absolute
  1640. (+ offset (calendar-absolute-from-gregorian now))))))
  1641. (defun calendar-column-to-segment ()
  1642. "Convert current column to calendar month \"segment\".
  1643. The left-most month returns 0, the next right 1, and so on."
  1644. (let ((col (max 0 (+ (current-column)
  1645. (/ calendar-intermonth-spacing 2)
  1646. (- calendar-left-margin)))))
  1647. (/ col (+ (* 7 calendar-column-width) calendar-intermonth-spacing))))
  1648. (defun calendar-cursor-to-date (&optional error event)
  1649. "Return a list (month day year) of current cursor position.
  1650. If cursor is not on a specific date, signals an error if optional parameter
  1651. ERROR is non-nil, otherwise just returns nil.
  1652. If EVENT is non-nil, it's an event indicating the buffer position to
  1653. use instead of point."
  1654. (with-current-buffer
  1655. (if event (window-buffer (posn-window (event-start event)))
  1656. (current-buffer))
  1657. (save-excursion
  1658. (and event (setq event (event-start event))
  1659. (goto-char (posn-point event)))
  1660. (let* ((segment (calendar-column-to-segment))
  1661. (month (% (+ displayed-month (1- segment)) 12)))
  1662. ;; Call with point on either of the two digits in a 2-digit date,
  1663. ;; or on or before the digit of a 1-digit date.
  1664. (if (not (and (looking-at "[ 0-9]?[0-9][^0-9]")
  1665. (get-text-property (point) 'date)))
  1666. (if error (error "Not on a date!"))
  1667. ;; Convert segment to real month and year.
  1668. (if (zerop month) (setq month 12))
  1669. ;; Go back to before the first date digit.
  1670. (or (looking-at " ")
  1671. (re-search-backward "[^0-9]"))
  1672. (list month
  1673. (string-to-number
  1674. (buffer-substring (1+ (point))
  1675. (+ 1 calendar-day-digit-width (point))))
  1676. (cond
  1677. ((and (= 12 month) (zerop segment)) (1- displayed-year))
  1678. ((and (= 1 month) (= segment 2)) (1+ displayed-year))
  1679. (t displayed-year))))))))
  1680. (add-to-list 'debug-ignored-errors "Not on a date!")
  1681. ;; The following version of calendar-gregorian-from-absolute is preferred for
  1682. ;; reasons of clarity, BUT it's much slower than the version that follows it.
  1683. ;;(defun calendar-gregorian-from-absolute (date)
  1684. ;; "Compute the list (month day year) corresponding to the absolute DATE.
  1685. ;;The absolute date is the number of days elapsed since the (imaginary)
  1686. ;;Gregorian date Sunday, December 31, 1 BC."
  1687. ;; (let* ((approx (/ date 366)) ; approximation from below
  1688. ;; (year ; search forward from the approximation
  1689. ;; (+ approx
  1690. ;; (calendar-sum y approx
  1691. ;; (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
  1692. ;; 1)))
  1693. ;; (month ; search forward from January
  1694. ;; (1+ (calendar-sum m 1
  1695. ;; (> date
  1696. ;; (calendar-absolute-from-gregorian
  1697. ;; (list m (calendar-last-day-of-month m year) year)))
  1698. ;; 1)))
  1699. ;; (day ; calculate the day by subtraction
  1700. ;; (- date
  1701. ;; (1- (calendar-absolute-from-gregorian (list month 1 year))))))
  1702. ;; (list month day year)))
  1703. (defun calendar-gregorian-from-absolute (date)
  1704. "Compute the list (month day year) corresponding to the absolute DATE.
  1705. The absolute date is the number of days elapsed since the (imaginary)
  1706. Gregorian date Sunday, December 31, 1 BC. This function does not
  1707. handle dates in years BC."
  1708. ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
  1709. ;; Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
  1710. ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
  1711. ;; (April, 1993), pages 383-404 for an explanation.
  1712. (let* ((d0 (1- date))
  1713. (n400 (/ d0 146097))
  1714. (d1 (% d0 146097))
  1715. (n100 (/ d1 36524))
  1716. (d2 (% d1 36524))
  1717. (n4 (/ d2 1461))
  1718. (d3 (% d2 1461))
  1719. (n1 (/ d3 365))
  1720. (day (1+ (% d3 365)))
  1721. (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1))
  1722. (month 1)
  1723. mdays)
  1724. (if (or (= n100 4) (= n1 4))
  1725. (list 12 31 year)
  1726. (setq year (1+ year))
  1727. (while (< (setq mdays (calendar-last-day-of-month month year)) day)
  1728. (setq day (- day mdays)
  1729. month (1+ month)))
  1730. (list month day year))))
  1731. (defun calendar-other-month (month year &optional event)
  1732. "Display a three-month calendar centered around MONTH and YEAR.
  1733. EVENT is an event like `last-nonmenu-event'."
  1734. (interactive (let ((event (list last-nonmenu-event)))
  1735. (append (calendar-read-date 'noday) event)))
  1736. (save-selected-window
  1737. (and event
  1738. (setq event (event-start event))
  1739. (select-window (posn-window event)))
  1740. (unless (and (= month displayed-month)
  1741. (= year displayed-year))
  1742. (let ((old-date (calendar-cursor-to-date))
  1743. (today (calendar-current-date)))
  1744. (calendar-generate-window month year)
  1745. (calendar-cursor-to-visible-date
  1746. (cond
  1747. ((calendar-date-is-visible-p old-date) old-date)
  1748. ((calendar-date-is-visible-p today) today)
  1749. (t (list month 1 year))))))))
  1750. (defun calendar-set-mark (arg &optional event)
  1751. "Mark the date under the cursor, or jump to marked date.
  1752. With no prefix argument, push current date onto marked date ring.
  1753. With argument ARG, jump to mark, pop it, and put point at end of ring."
  1754. (interactive
  1755. (list current-prefix-arg last-nonmenu-event))
  1756. (let ((date (calendar-cursor-to-date t event)))
  1757. (if arg
  1758. (if (null calendar-mark-ring)
  1759. (error "No mark set in this buffer")
  1760. (calendar-goto-date (car calendar-mark-ring))
  1761. (setq calendar-mark-ring
  1762. (cdr (nconc calendar-mark-ring (list date)))))
  1763. (push date calendar-mark-ring)
  1764. ;; Since the top of the mark ring is the marked date in the
  1765. ;; calendar, the mark ring in the calendar is one longer than
  1766. ;; in other buffers to get the same effect.
  1767. (if (> (length calendar-mark-ring) (1+ mark-ring-max))
  1768. (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
  1769. (message "Mark set"))))
  1770. (defun calendar-exchange-point-and-mark ()
  1771. "Exchange the current cursor position with the marked date."
  1772. (interactive)
  1773. (let ((mark (car calendar-mark-ring))
  1774. (date (calendar-cursor-to-date t)))
  1775. (if (null mark)
  1776. (error "No mark set in this buffer")
  1777. (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
  1778. (calendar-goto-date mark))))
  1779. (defun calendar-count-days-region ()
  1780. "Count the number of days (inclusive) between point and the mark."
  1781. (interactive)
  1782. (let* ((days (- (calendar-absolute-from-gregorian
  1783. (calendar-cursor-to-date t))
  1784. (calendar-absolute-from-gregorian
  1785. (or (car calendar-mark-ring)
  1786. (error "No mark set in this buffer")))))
  1787. (days (1+ (if (> days 0) days (- days)))))
  1788. (message "Region has %d day%s (inclusive)"
  1789. days (if (> days 1) "s" ""))))
  1790. (defun calendar-not-implemented ()
  1791. "Not implemented."
  1792. (interactive)
  1793. (error "%s not available in the calendar"
  1794. (global-key-binding (this-command-keys))))
  1795. (defun calendar-read (prompt acceptable &optional initial-contents)
  1796. "Return an object read from the minibuffer.
  1797. Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
  1798. entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
  1799. is a string to insert in the minibuffer before reading."
  1800. (let ((value (read-minibuffer prompt initial-contents)))
  1801. (while (not (funcall acceptable value))
  1802. (setq value (read-minibuffer prompt initial-contents)))
  1803. value))
  1804. (defun calendar-customized-p (symbol)
  1805. "Return non-nil if SYMBOL has been customized."
  1806. (and (default-boundp symbol)
  1807. (let ((standard (get symbol 'standard-value)))
  1808. (and standard
  1809. (not (equal (eval (car standard)) (default-value symbol)))))))
  1810. (defun calendar-abbrev-construct (full)
  1811. "From sequence FULL, return a vector of abbreviations.
  1812. Each abbreviation is no longer than `calendar-abbrev-length' characters."
  1813. (apply 'vector (mapcar
  1814. (lambda (f)
  1815. (substring f 0 (min calendar-abbrev-length (length f))))
  1816. full)))
  1817. (defcustom calendar-day-name-array
  1818. ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]
  1819. "Array of capitalized strings giving, in order from Sunday, the day names.
  1820. The first two characters of each string will be used to head the
  1821. day columns in the calendar.
  1822. If you change this without using customize after the calendar has loaded,
  1823. then you may also want to change `calendar-day-abbrev-array'."
  1824. :group 'calendar
  1825. :initialize 'custom-initialize-default
  1826. :set (lambda (symbol value)
  1827. (let ((dcustomized (calendar-customized-p 'calendar-day-abbrev-array))
  1828. (hcustomized (calendar-customized-p 'cal-html-day-abbrev-array)))
  1829. (set symbol value)
  1830. (or dcustomized
  1831. (setq calendar-day-abbrev-array
  1832. (calendar-abbrev-construct calendar-day-name-array)))
  1833. (and (not hcustomized)
  1834. (boundp 'cal-html-day-abbrev-array)
  1835. (setq cal-html-day-abbrev-array calendar-day-abbrev-array))))
  1836. :type '(vector (string :tag "Sunday")
  1837. (string :tag "Monday")
  1838. (string :tag "Tuesday")
  1839. (string :tag "Wednesday")
  1840. (string :tag "Thursday")
  1841. (string :tag "Friday")
  1842. (string :tag "Saturday")))
  1843. (defcustom calendar-abbrev-length 3
  1844. "Default length of abbreviations to use for day and month names.
  1845. If you change this without using customize after the calendar has loaded,
  1846. then you may also want to change `calendar-day-abbrev-array' and
  1847. `calendar-month-abbrev-array'."
  1848. :group 'calendar
  1849. :initialize 'custom-initialize-default
  1850. :set (lambda (symbol value)
  1851. (let ((dcustomized (calendar-customized-p 'calendar-day-abbrev-array))
  1852. (mcustomized (calendar-customized-p
  1853. 'calendar-month-abbrev-array))
  1854. (hcustomized (calendar-customized-p 'cal-html-day-abbrev-array)))
  1855. (set symbol value)
  1856. (or dcustomized
  1857. (setq calendar-day-abbrev-array
  1858. (calendar-abbrev-construct calendar-day-name-array)))
  1859. (or mcustomized
  1860. (setq calendar-month-abbrev-array
  1861. (calendar-abbrev-construct calendar-month-name-array)))
  1862. (and (not hcustomized)
  1863. (boundp 'cal-html-day-abbrev-array)
  1864. (setq cal-html-day-abbrev-array calendar-day-abbrev-array))))
  1865. :type 'integer)
  1866. (defcustom calendar-day-abbrev-array
  1867. (calendar-abbrev-construct calendar-day-name-array)
  1868. "Array of capitalized strings giving the abbreviated day names.
  1869. The order should be the same as that of the full names specified
  1870. in `calendar-day-name-array'. These abbreviations may be used
  1871. instead of the full names in the diary file. Do not include a
  1872. trailing `.' in the strings specified in this variable, though
  1873. you may use such in the diary file. By default, each string is
  1874. the first `calendar-abbrev-length' characters of the corresponding
  1875. full name."
  1876. :group 'calendar
  1877. :initialize 'custom-initialize-default
  1878. :set-after '(calendar-abbrev-length calendar-day-name-array)
  1879. :set (lambda (symbol value)
  1880. (let ((hcustomized (calendar-customized-p 'cal-html-day-abbrev-array)))
  1881. (set symbol value)
  1882. (and (not hcustomized)
  1883. (boundp 'cal-html-day-abbrev-array)
  1884. (setq cal-html-day-abbrev-array calendar-day-abbrev-array))))
  1885. :type '(vector (string :tag "Sun")
  1886. (string :tag "Mon")
  1887. (string :tag "Tue")
  1888. (string :tag "Wed")
  1889. (string :tag "Thu")
  1890. (string :tag "Fri")
  1891. (string :tag "Sat"))
  1892. ;; Made defcustom, changed defaults from nil nil...
  1893. :version "24.1")
  1894. (defcustom calendar-month-name-array
  1895. ["January" "February" "March" "April" "May" "June"
  1896. "July" "August" "September" "October" "November" "December"]
  1897. "Array of capitalized strings giving, in order, the month names.
  1898. If you change this without using customize after the calendar has loaded,
  1899. then you may also want to change `calendar-month-abbrev-array'."
  1900. :group 'calendar
  1901. :initialize 'custom-initialize-default
  1902. :set (lambda (symbol value)
  1903. (let ((mcustomized (calendar-customized-p
  1904. 'calendar-month-abbrev-array)))
  1905. (set symbol value)
  1906. (or mcustomized
  1907. (setq calendar-month-abbrev-array
  1908. (calendar-abbrev-construct calendar-month-name-array)))))
  1909. :type '(vector (string :tag "January")
  1910. (string :tag "February")
  1911. (string :tag "March")
  1912. (string :tag "April")
  1913. (string :tag "May")
  1914. (string :tag "June")
  1915. (string :tag "July")
  1916. (string :tag "August")
  1917. (string :tag "September")
  1918. (string :tag "October")
  1919. (string :tag "November")
  1920. (string :tag "December")))
  1921. (defcustom calendar-month-abbrev-array
  1922. (calendar-abbrev-construct calendar-month-name-array)
  1923. "Array of capitalized strings giving the abbreviated month names.
  1924. The order should be the same as that of the full names specified
  1925. in `calendar-month-name-array'. These abbreviations are used in
  1926. the calendar menu entries, and can also be used in the diary
  1927. file. Do not include a trailing `.' in the strings specified in
  1928. this variable, though you may use such in the diary file. By
  1929. default, each string is the first ``calendar-abbrev-length'
  1930. characters of the corresponding full name."
  1931. :group 'calendar
  1932. :set-after '(calendar-abbrev-length calendar-month-name-array)
  1933. :type '(vector (string :tag "Jan")
  1934. (string :tag "Feb")
  1935. (string :tag "Mar")
  1936. (string :tag "Apr")
  1937. (string :tag "May")
  1938. (string :tag "Jun")
  1939. (string :tag "Jul")
  1940. (string :tag "Aug")
  1941. (string :tag "Sep")
  1942. (string :tag "Oct")
  1943. (string :tag "Nov")
  1944. (string :tag "Dec"))
  1945. ;; Made defcustom, changed defaults from nil nil...
  1946. :version "24.1")
  1947. (defun calendar-make-alist (sequence &optional start-index filter
  1948. &rest sequences)
  1949. "Return an association list corresponding to SEQUENCE.
  1950. Associates each element of SEQUENCE with an incremented integer,
  1951. starting from START-INDEX (default 1). Applies the function FILTER,
  1952. if provided, to each key in the alist. Repeats the process, with
  1953. indices starting from START-INDEX each time, for any remaining
  1954. arguments SEQUENCES."
  1955. (or start-index (setq start-index 1))
  1956. (let (index alist)
  1957. (mapc (lambda (seq)
  1958. (setq index start-index)
  1959. (mapc (lambda (elem)
  1960. (setq alist (cons
  1961. (cons (if filter (funcall filter elem) elem)
  1962. index)
  1963. alist)
  1964. index (1+ index)))
  1965. seq))
  1966. (append (list sequence) sequences))
  1967. (reverse alist)))
  1968. (defun calendar-read-date (&optional noday)
  1969. "Prompt for Gregorian date. Return a list (month day year).
  1970. If optional NODAY is t, does not ask for day, but just returns
  1971. \(month 1 year); if NODAY is any other non-nil value the value returned is
  1972. \(month year)"
  1973. (let* ((year (calendar-read
  1974. "Year (>0): "
  1975. (lambda (x) (> x 0))
  1976. (number-to-string (calendar-extract-year
  1977. (calendar-current-date)))))
  1978. (month-array calendar-month-name-array)
  1979. (completion-ignore-case t)
  1980. (month (cdr (assoc-string
  1981. (completing-read
  1982. "Month name: "
  1983. (mapcar 'list (append month-array nil))
  1984. nil t)
  1985. (calendar-make-alist month-array 1) t)))
  1986. (last (calendar-last-day-of-month month year)))
  1987. (if noday
  1988. (if (eq noday t)
  1989. (list month 1 year)
  1990. (list month year))
  1991. (list month
  1992. (calendar-read (format "Day (1-%d): " last)
  1993. (lambda (x) (and (< 0 x) (<= x last))))
  1994. year))))
  1995. (defun calendar-interval (mon1 yr1 mon2 yr2)
  1996. "The number of months difference between MON1, YR1 and MON2, YR2.
  1997. The result is positive if the second date is later than the first.
  1998. Negative years are interpreted as years BC; -1 being 1 BC, and so on."
  1999. (if (< yr1 0) (setq yr1 (1+ yr1))) ; -1 BC -> 0 AD, etc
  2000. (if (< yr2 0) (setq yr2 (1+ yr2)))
  2001. (+ (* 12 (- yr2 yr1))
  2002. (- mon2 mon1)))
  2003. (defvar calendar-font-lock-keywords
  2004. `((,(concat (regexp-opt (mapcar 'identity calendar-month-name-array) t)
  2005. " -?[0-9]+")
  2006. . font-lock-function-name-face) ; month and year
  2007. (,(regexp-opt
  2008. (list (substring (aref calendar-day-name-array 6)
  2009. 0 calendar-day-header-width)
  2010. (substring (aref calendar-day-name-array 0)
  2011. 0 calendar-day-header-width)))
  2012. ;; Saturdays and Sundays are highlighted differently.
  2013. . font-lock-comment-face)
  2014. ;; First two chars of each day are used in the calendar.
  2015. (,(regexp-opt (mapcar (lambda (x) (substring x 0 calendar-day-header-width))
  2016. calendar-day-name-array))
  2017. . font-lock-reference-face))
  2018. "Default keywords to highlight in Calendar mode.")
  2019. (defun calendar-day-name (date &optional abbrev absolute)
  2020. "Return a string with the name of the day of the week of DATE.
  2021. DATE should be a list in the format (MONTH DAY YEAR), unless the
  2022. optional argument ABSOLUTE is non-nil, in which case DATE should
  2023. be an integer in the range 0 to 6 corresponding to the day of the
  2024. week. Day names are taken from the variable `calendar-day-name-array',
  2025. unless the optional argument ABBREV is non-nil, in which case
  2026. the variable `calendar-day-abbrev-array' is used."
  2027. (aref (if abbrev calendar-day-abbrev-array calendar-day-name-array)
  2028. (if absolute date (calendar-day-of-week date))))
  2029. (defun calendar-month-name (month &optional abbrev)
  2030. "Return a string with the name of month number MONTH.
  2031. Months are numbered from one. Month names are taken from the
  2032. variable `calendar-month-name-array', unless the optional
  2033. argument ABBREV is non-nil, in which case
  2034. `calendar-month-abbrev-array' is used."
  2035. (aref (if abbrev calendar-month-abbrev-array calendar-month-name-array)
  2036. (1- month)))
  2037. (defun calendar-day-of-week (date)
  2038. "Return the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc.
  2039. DATE is a list of the form (month day year). A negative year is
  2040. interpreted as BC; -1 being 1 BC, and so on."
  2041. (mod (calendar-absolute-from-gregorian date) 7))
  2042. (defun calendar-week-end-day ()
  2043. "Return the index (0 for Sunday, etc.) of the last day of the week."
  2044. (mod (+ calendar-week-start-day 6) 7))
  2045. (defun calendar-unmark ()
  2046. "Delete all diary/holiday marks/highlighting from the calendar."
  2047. (interactive)
  2048. (setq calendar-mark-holidays-flag nil
  2049. calendar-mark-diary-entries-flag nil)
  2050. (with-current-buffer calendar-buffer
  2051. (mapc 'delete-overlay (overlays-in (point-min) (point-max)))))
  2052. (defun calendar-date-is-visible-p (date)
  2053. "Return non-nil if DATE is valid and is visible in the calendar window."
  2054. (and (calendar-date-is-valid-p date)
  2055. (< (abs (calendar-interval
  2056. displayed-month displayed-year
  2057. (calendar-extract-month date) (calendar-extract-year date)))
  2058. 2)))
  2059. ;; FIXME can this be generalized for holiday-chinese?
  2060. (defun calendar-nongregorian-visible-p (month day toabs fromabs switch)
  2061. "Return non-nil if MONTH, DAY is visible in the calendar window.
  2062. MONTH and DAY are in some non-Gregorian calendar system. The
  2063. functions TOABS and FROMABS convert that system to and from
  2064. absolute, respectively. SWITCH is a function that takes a single
  2065. argument (a local month number). It applies when the local year
  2066. changes across the calendar window, and returns non-nil if the
  2067. specified month should be associated with the higher year.
  2068. Returns the corresponding Gregorian date."
  2069. ;; We need to choose the local year associated with month and day
  2070. ;; that might make them visible.
  2071. (let* ((m1 displayed-month)
  2072. (y1 displayed-year)
  2073. (m2 displayed-month)
  2074. (y2 displayed-year)
  2075. ;; Absolute date of first/last dates in calendar window.
  2076. (start-date (progn
  2077. (calendar-increment-month m1 y1 -1)
  2078. (calendar-absolute-from-gregorian (list m1 1 y1))))
  2079. (end-date (progn
  2080. (calendar-increment-month m2 y2 1)
  2081. (calendar-absolute-from-gregorian
  2082. (list m2 (calendar-last-day-of-month m2 y2) y2))))
  2083. ;; Local date of first/last date in calendar window.
  2084. (local-start (funcall fromabs start-date))
  2085. (local-end (funcall fromabs end-date))
  2086. ;; Local year of first/last dates.
  2087. ;; Can only differ if displayed-month = 12, 1, 2.
  2088. (local-y1 (calendar-extract-year local-start))
  2089. (local-y2 (calendar-extract-year local-end))
  2090. ;; Choose which year might be visible in the window.
  2091. ;; Obviously it only matters when y1 and y2 differ, ie
  2092. ;; when the _local_ new year is visible.
  2093. (year (if (funcall switch month) local-y2 local-y1))
  2094. (date (calendar-gregorian-from-absolute
  2095. (funcall toabs (list month day year)))))
  2096. (if (calendar-date-is-visible-p date)
  2097. date)))
  2098. (defun calendar-date-is-valid-p (date)
  2099. "Return t if DATE is a valid date."
  2100. (let ((month (calendar-extract-month date))
  2101. (day (calendar-extract-day date))
  2102. (year (calendar-extract-year date)))
  2103. (and (<= 1 month) (<= month 12)
  2104. ;; (calendar-read-date t) used to return a date with day = nil.
  2105. ;; Should not be valid (?), since many funcs prob assume integer.
  2106. ;; (calendar-read-date 'noday) returns (month year), which
  2107. ;; currently results in calendar-extract-year returning nil.
  2108. day year (<= 1 day) (<= day (calendar-last-day-of-month month year))
  2109. ;; BC dates left as non-valid, to suppress errors from
  2110. ;; complex holiday algorithms not suitable for years BC.
  2111. ;; Note there are side effects on calendar navigation.
  2112. (<= 1 year))))
  2113. (define-obsolete-function-alias 'calendar-date-is-legal-p
  2114. 'calendar-date-is-valid-p "23.1")
  2115. (defun calendar-date-equal (date1 date2)
  2116. "Return t if the DATE1 and DATE2 are the same."
  2117. (and
  2118. (= (calendar-extract-month date1) (calendar-extract-month date2))
  2119. (= (calendar-extract-day date1) (calendar-extract-day date2))
  2120. (= (calendar-extract-year date1) (calendar-extract-year date2))))
  2121. (defun calendar-make-temp-face (attrlist)
  2122. "Return a temporary face based on the attributes in ATTRLIST.
  2123. ATTRLIST is a list with elements of the form :face face :foreground color."
  2124. (let ((attrs attrlist)
  2125. faceinfo face temp-face)
  2126. ;; Separate :face from the other attributes. Use the last :face
  2127. ;; if there are more than one. FIXME is merging meaningful?
  2128. (while attrs
  2129. (if (eq (car attrs) :face)
  2130. (setq face (intern-soft (cadr attrs))
  2131. attrs (cddr attrs))
  2132. (push (car attrs) faceinfo)
  2133. (setq attrs (cdr attrs))))
  2134. (or (facep face) (setq face 'default))
  2135. (if (not faceinfo)
  2136. ;; No attributes to apply, so just use an existing-face.
  2137. face
  2138. ;; FIXME should we be using numbered temp-faces, re-using where poss?
  2139. (setq temp-face
  2140. (make-symbol
  2141. (concat ":caltemp"
  2142. (mapconcat (lambda (sym)
  2143. (cond
  2144. ((symbolp sym) (symbol-name sym))
  2145. ((numberp sym) (number-to-string sym))
  2146. (t sym)))
  2147. attrlist ""))))
  2148. (make-face temp-face)
  2149. (copy-face face temp-face)
  2150. ;; Apply the font aspects.
  2151. (apply 'set-face-attribute temp-face nil (nreverse faceinfo))
  2152. temp-face)))
  2153. (defun calendar-mark-visible-date (date &optional mark)
  2154. "Mark DATE in the calendar window with MARK.
  2155. MARK is a single-character string, a list of face attributes/values, or a face.
  2156. MARK defaults to `diary-entry-marker'."
  2157. (if (calendar-date-is-valid-p date)
  2158. (with-current-buffer calendar-buffer
  2159. (save-excursion
  2160. (calendar-cursor-to-visible-date date)
  2161. (setq mark
  2162. (or (and (stringp mark) (= (length mark) 1) mark) ; single-char
  2163. ;; The next two use to also check font-lock-mode.
  2164. ;; See comments above diary-entry-marker for why
  2165. ;; this was dropped.
  2166. ;;; (and font-lock-mode
  2167. ;;; (or
  2168. (and (listp mark) (> (length mark) 0) mark) ; attrs
  2169. (and (facep mark) mark) ; )) face-name
  2170. diary-entry-marker))
  2171. (cond
  2172. ;; Face or an attr-list that contained a face.
  2173. ((facep mark)
  2174. (overlay-put
  2175. (make-overlay (1- (point)) (1+ (point))) 'face mark))
  2176. ;; Single-character mark, goes after the date.
  2177. ((and (stringp mark) (= (length mark) 1))
  2178. (overlay-put
  2179. (make-overlay (1+ (point)) (+ 2 (point))) 'display mark))
  2180. (t ; attr list
  2181. (overlay-put
  2182. (make-overlay (1- (point)) (1+ (point))) 'face
  2183. (calendar-make-temp-face mark))))))))
  2184. (define-obsolete-function-alias 'mark-visible-calendar-date
  2185. 'calendar-mark-visible-date "23.1")
  2186. (defun calendar-star-date ()
  2187. "Replace the date under the cursor in the calendar window with asterisks.
  2188. You might want to add this function to `calendar-today-visible-hook'."
  2189. (unless (catch 'found
  2190. (dolist (ol (overlays-at (point)))
  2191. (and (overlay-get ol 'calendar-star)
  2192. (throw 'found t))))
  2193. (let ((ol (make-overlay (1- (point)) (point))))
  2194. (overlay-put ol 'display "*")
  2195. (overlay-put ol 'calendar-star t)
  2196. ;; Use copy-sequence to avoid merging of identical 'display props.
  2197. ;; Use two overlays so as not to mess up
  2198. ;; calendar-cursor-to-nearest-date (and calendar-forward-day).
  2199. (overlay-put (setq ol (make-overlay (point) (1+ (point))))
  2200. 'display (copy-sequence "*"))
  2201. (overlay-put ol 'calendar-star t))))
  2202. (defun calendar-mark-today ()
  2203. "Mark the date under the cursor in the calendar window.
  2204. The date is marked with `calendar-today-marker'. You might want to add
  2205. this function to `calendar-today-visible-hook'."
  2206. (calendar-mark-visible-date (calendar-cursor-to-date) calendar-today-marker))
  2207. ;; FIXME why the car? Almost every usage calls list on the args.
  2208. (defun calendar-date-compare (date1 date2)
  2209. "Return t if DATE1 is before DATE2, nil otherwise.
  2210. The actual dates are in the car of DATE1 and DATE2."
  2211. (< (calendar-absolute-from-gregorian (car date1))
  2212. (calendar-absolute-from-gregorian (car date2))))
  2213. (defun calendar-date-string (date &optional abbreviate nodayname)
  2214. "A string form of DATE, driven by the variable `calendar-date-display-form'.
  2215. An optional parameter ABBREVIATE, when non-nil, causes the month
  2216. and day names to be abbreviated as specified by
  2217. `calendar-month-abbrev-array' and `calendar-day-abbrev-array',
  2218. respectively. An optional parameter NODAYNAME, when t, omits the
  2219. name of the day of the week."
  2220. (let* ((dayname (unless nodayname (calendar-day-name date abbreviate)))
  2221. (month (calendar-extract-month date))
  2222. (monthname (calendar-month-name month abbreviate))
  2223. (day (number-to-string (calendar-extract-day date)))
  2224. (month (number-to-string month))
  2225. (year (number-to-string (calendar-extract-year date))))
  2226. (mapconcat 'eval calendar-date-display-form "")))
  2227. (defun calendar-dayname-on-or-before (dayname date)
  2228. "Return the absolute date of the DAYNAME on or before absolute DATE.
  2229. DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
  2230. Note: Applying this function to d+6 gives us the DAYNAME on or after an
  2231. absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
  2232. absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
  2233. date d, and applying it to d+7 gives the DAYNAME following absolute date d."
  2234. (- date (% (- date dayname) 7)))
  2235. (defun calendar-nth-named-absday (n dayname month year &optional day)
  2236. "Absolute date of the Nth DAYNAME after/before MONTH YEAR DAY.
  2237. A DAYNAME of 0 means Sunday, 1 means Monday, and so on.
  2238. If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
  2239. If N<0, return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
  2240. DAY defaults to 1 if N>0, and MONTH's last day otherwise."
  2241. (if (> n 0)
  2242. (+ (* 7 (1- n))
  2243. (calendar-dayname-on-or-before
  2244. dayname
  2245. (+ 6 (calendar-absolute-from-gregorian
  2246. (list month (or day 1) year)))))
  2247. (+ (* 7 (1+ n))
  2248. (calendar-dayname-on-or-before
  2249. dayname
  2250. (calendar-absolute-from-gregorian
  2251. (list month
  2252. (or day (calendar-last-day-of-month month year))
  2253. year))))))
  2254. (defun calendar-nth-named-day (n dayname month year &optional day)
  2255. "Date of the Nth DAYNAME after/before MONTH YEAR DAY.
  2256. Like `calendar-nth-named-absday', but returns a Gregorian date."
  2257. (calendar-gregorian-from-absolute
  2258. (calendar-nth-named-absday n dayname month year day)))
  2259. (defun calendar-day-of-year-string (&optional date)
  2260. "String of day number of year of Gregorian DATE.
  2261. Defaults to today's date if DATE is not given."
  2262. (let* ((d (or date (calendar-current-date)))
  2263. (year (calendar-extract-year d))
  2264. (day (calendar-day-number d))
  2265. (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
  2266. (format "Day %d of %d; %d day%s remaining in the year"
  2267. day year days-remaining (if (= days-remaining 1) "" "s"))))
  2268. (defun calendar-other-dates (date)
  2269. "Return a list of strings giving Gregorian DATE in other calendars.
  2270. DATE is (month day year). Calendars that do not apply are omitted."
  2271. (let (odate)
  2272. (delq nil
  2273. (list
  2274. (calendar-day-of-year-string date)
  2275. (format "ISO date: %s" (calendar-iso-date-string date))
  2276. (format "Julian date: %s"
  2277. (calendar-julian-date-string date))
  2278. (format "Astronomical (Julian) day number (at noon UTC): %s.0"
  2279. (calendar-astro-date-string date))
  2280. (format "Fixed (RD) date: %s"
  2281. (calendar-absolute-from-gregorian date))
  2282. (format "Hebrew date (before sunset): %s"
  2283. (calendar-hebrew-date-string date))
  2284. (format "Persian date: %s"
  2285. (calendar-persian-date-string date))
  2286. (unless (string-equal
  2287. (setq odate (calendar-islamic-date-string date))
  2288. "")
  2289. (format "Islamic date (before sunset): %s" odate))
  2290. (unless (string-equal
  2291. (setq odate (calendar-bahai-date-string date))
  2292. "")
  2293. (format "Baha'i date: %s" odate))
  2294. (format "Chinese date: %s"
  2295. (calendar-chinese-date-string date))
  2296. (unless (string-equal
  2297. (setq odate (calendar-coptic-date-string date))
  2298. "")
  2299. (format "Coptic date: %s" odate))
  2300. (unless (string-equal
  2301. (setq odate (calendar-ethiopic-date-string date))
  2302. "")
  2303. (format "Ethiopic date: %s" odate))
  2304. (unless (string-equal
  2305. (setq odate (calendar-french-date-string date))
  2306. "")
  2307. (format "French Revolutionary date: %s" odate))
  2308. (format "Mayan date: %s"
  2309. (calendar-mayan-date-string date))))))
  2310. (declare-function x-popup-menu "menu.c" (position menu))
  2311. (defun calendar-print-other-dates (&optional event)
  2312. "Show dates on other calendars for date under the cursor.
  2313. If called by a mouse-event, pops up a menu with the result."
  2314. (interactive (list last-nonmenu-event))
  2315. (let* ((date (calendar-cursor-to-date t event))
  2316. (title (format "%s (Gregorian)" (calendar-date-string date)))
  2317. (others (calendar-other-dates date))
  2318. selection)
  2319. (if (mouse-event-p event)
  2320. (and (setq selection (cal-menu-x-popup-menu event title
  2321. (mapcar 'list others)))
  2322. (call-interactively selection))
  2323. (calendar-in-read-only-buffer calendar-other-calendars-buffer
  2324. (calendar-set-mode-line title)
  2325. (insert (mapconcat 'identity others "\n"))))))
  2326. (defun calendar-print-day-of-year ()
  2327. "Show day number in year/days remaining in year for date under the cursor."
  2328. (interactive)
  2329. (message "%s" (calendar-day-of-year-string (calendar-cursor-to-date t))))
  2330. (defun calendar-set-mode-line (str)
  2331. "Set mode line to STR, centered, surrounded by dashes."
  2332. (let* ((edges (window-edges))
  2333. ;; As per doc of window-width, total visible mode-line length.
  2334. (width (- (nth 2 edges) (car edges))))
  2335. ;; Hack for --daemon. See bug #2199.
  2336. ;; If no frame exists yet, we have no idea what width to use.
  2337. (and (= width 10)
  2338. (not window-system)
  2339. (setq width (or (getenv "COLUMNS") 80)))
  2340. (setq mode-line-format
  2341. (if buffer-file-name
  2342. `("-" mode-line-modified
  2343. ,(calendar-string-spread (list str) ?- (- width 6))
  2344. "---")
  2345. (calendar-string-spread (list str) ?- width)))))
  2346. (defun calendar-version ()
  2347. "Display the Calendar version."
  2348. (interactive)
  2349. (message "GNU Emacs %s" emacs-version))
  2350. (make-obsolete 'calendar-version 'emacs-version "23.1")
  2351. (run-hooks 'calendar-load-hook)
  2352. (provide 'calendar)
  2353. ;; Local variables:
  2354. ;; byte-compile-dynamic: t
  2355. ;; End:
  2356. ;;; calendar.el ends here