PageRenderTime 36ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/calendar/calendar.el

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