PageRenderTime 37ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/ntemacs23.1/lisp/calendar/calendar.el

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