PageRenderTime 73ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/calendar/calendar.el

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