cal-move.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. ;;; cal-move.el --- calendar functions for movement in the calendar
  2. ;; Copyright (C) 1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Keywords: calendar
  6. ;; Human-Keywords: calendar
  7. ;; Package: calendar
  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. ;; See calendar.el.
  21. ;;; Code:
  22. ;; FIXME should calendar just require this?
  23. (require 'calendar)
  24. ;; Note that this is not really the "closest" date.
  25. ;; In most cases, it just searches forwards for the next day.
  26. ;;;###cal-autoload
  27. (defun calendar-cursor-to-nearest-date ()
  28. "Move the cursor to the closest date.
  29. The position of the cursor is unchanged if it is already on a date.
  30. Returns the list (month day year) giving the cursor position."
  31. (or (calendar-cursor-to-date)
  32. (let* ((col (current-column))
  33. (edges (cdr (assoc (calendar-column-to-segment)
  34. calendar-month-edges)))
  35. (last (nth 2 edges))
  36. (right (nth 3 edges)))
  37. (when (< (count-lines (point-min) (point)) calendar-first-date-row)
  38. (goto-char (point-min))
  39. (forward-line (1- calendar-first-date-row))
  40. (move-to-column col))
  41. ;; The date positions are fixed and computable, but searching
  42. ;; is probably more flexible. Need to consider blank days at
  43. ;; start and end of month if computing positions.
  44. ;; 'date text-property is used to exclude intermonth text.
  45. (unless (and (looking-at "[0-9]")
  46. (get-text-property (point) 'date))
  47. ;; We search forwards for a number, except close to the RH
  48. ;; margin of a month, where we search backwards.
  49. ;; Note that the searches can go to other lines.
  50. (if (or (looking-at " *$")
  51. (and (> col last) (< col right)))
  52. (while (and (re-search-backward "[0-9]" nil t)
  53. (not (get-text-property (point) 'date))))
  54. (while (and (re-search-forward "[0-9]" nil t)
  55. (not (get-text-property (1- (point)) 'date))))
  56. (backward-char 1)))
  57. (calendar-cursor-to-date))))
  58. (defvar displayed-month) ; from calendar-generate
  59. (defvar displayed-year)
  60. ;;;###cal-autoload
  61. (defun calendar-cursor-to-visible-date (date)
  62. "Move the cursor to DATE that is on the screen."
  63. (let ((month (calendar-extract-month date))
  64. (day (calendar-extract-day date))
  65. (year (calendar-extract-year date)))
  66. (goto-char (point-min))
  67. (forward-line (+ calendar-first-date-row -1
  68. (/ (+ day -1
  69. (mod
  70. (- (calendar-day-of-week (list month 1 year))
  71. calendar-week-start-day)
  72. 7))
  73. 7)))
  74. (move-to-column (+ calendar-left-margin (1- calendar-day-digit-width)
  75. (* calendar-month-width
  76. (1+ (calendar-interval
  77. displayed-month displayed-year month year)))
  78. (* calendar-column-width
  79. (mod
  80. (- (calendar-day-of-week date)
  81. calendar-week-start-day)
  82. 7))))))
  83. ;;;###cal-autoload
  84. (defun calendar-goto-today ()
  85. "Reposition the calendar window so the current date is visible."
  86. (interactive)
  87. (let ((today (calendar-current-date))) ; the date might have changed
  88. (if (not (calendar-date-is-visible-p today))
  89. (calendar-generate-window)
  90. (calendar-update-mode-line)
  91. (calendar-cursor-to-visible-date today)))
  92. (run-hooks 'calendar-move-hook))
  93. ;;;###cal-autoload
  94. (defun calendar-forward-month (arg)
  95. "Move the cursor forward ARG months.
  96. Movement is backward if ARG is negative."
  97. (interactive "p")
  98. (calendar-cursor-to-nearest-date)
  99. (let* ((cursor-date (calendar-cursor-to-date t))
  100. (month (calendar-extract-month cursor-date))
  101. (day (calendar-extract-day cursor-date))
  102. (year (calendar-extract-year cursor-date))
  103. (last (progn
  104. (calendar-increment-month month year arg)
  105. (calendar-last-day-of-month month year)))
  106. (day (min last day))
  107. ;; Put the new month on the screen, if needed, and go to the new date.
  108. (new-cursor-date (list month day year)))
  109. (if (not (calendar-date-is-visible-p new-cursor-date))
  110. (calendar-other-month month year))
  111. (calendar-cursor-to-visible-date new-cursor-date))
  112. (run-hooks 'calendar-move-hook))
  113. ;;;###cal-autoload
  114. (defun calendar-forward-year (arg)
  115. "Move the cursor forward by ARG years.
  116. Movement is backward if ARG is negative."
  117. (interactive "p")
  118. (calendar-forward-month (* 12 arg)))
  119. ;;;###cal-autoload
  120. (defun calendar-backward-month (arg)
  121. "Move the cursor backward by ARG months.
  122. Movement is forward if ARG is negative."
  123. (interactive "p")
  124. (calendar-forward-month (- arg)))
  125. ;;;###cal-autoload
  126. (defun calendar-backward-year (arg)
  127. "Move the cursor backward ARG years.
  128. Movement is forward is ARG is negative."
  129. (interactive "p")
  130. (calendar-forward-month (* -12 arg)))
  131. ;;;###cal-autoload
  132. (defun calendar-scroll-left (&optional arg event)
  133. "Scroll the displayed calendar left by ARG months.
  134. If ARG is negative the calendar is scrolled right. Maintains the relative
  135. position of the cursor with respect to the calendar as well as possible.
  136. EVENT is an event like `last-nonmenu-event'."
  137. (interactive (list (prefix-numeric-value current-prefix-arg)
  138. last-nonmenu-event))
  139. (unless arg (setq arg 1))
  140. (save-selected-window
  141. ;; Nil if called from menu-bar.
  142. (if (setq event (event-start event)) (select-window (posn-window event)))
  143. (calendar-cursor-to-nearest-date)
  144. (unless (zerop arg)
  145. (let ((old-date (calendar-cursor-to-date))
  146. (today (calendar-current-date))
  147. (month displayed-month)
  148. (year displayed-year))
  149. (calendar-increment-month month year arg)
  150. (calendar-generate-window month year)
  151. (calendar-cursor-to-visible-date
  152. (cond
  153. ((calendar-date-is-visible-p old-date) old-date)
  154. ((calendar-date-is-visible-p today) today)
  155. (t (list month 1 year))))))
  156. (run-hooks 'calendar-move-hook)))
  157. (define-obsolete-function-alias
  158. 'scroll-calendar-left 'calendar-scroll-left "23.1")
  159. ;;;###cal-autoload
  160. (defun calendar-scroll-right (&optional arg event)
  161. "Scroll the displayed calendar window right by ARG months.
  162. If ARG is negative the calendar is scrolled left. Maintains the relative
  163. position of the cursor with respect to the calendar as well as possible.
  164. EVENT is an event like `last-nonmenu-event'."
  165. (interactive (list (prefix-numeric-value current-prefix-arg)
  166. last-nonmenu-event))
  167. (calendar-scroll-left (- (or arg 1)) event))
  168. (define-obsolete-function-alias
  169. 'scroll-calendar-right 'calendar-scroll-right "23.1")
  170. ;;;###cal-autoload
  171. (defun calendar-scroll-left-three-months (arg &optional event)
  172. "Scroll the displayed calendar window left by 3*ARG months.
  173. If ARG is negative the calendar is scrolled right. Maintains the relative
  174. position of the cursor with respect to the calendar as well as possible.
  175. EVENT is an event like `last-nonmenu-event'."
  176. (interactive (list (prefix-numeric-value current-prefix-arg)
  177. last-nonmenu-event))
  178. (calendar-scroll-left (* 3 arg) event))
  179. (define-obsolete-function-alias 'scroll-calendar-left-three-months
  180. 'calendar-scroll-left-three-months "23.1")
  181. ;; cf scroll-bar-toolkit-scroll
  182. ;;;###cal-autoload
  183. (defun calendar-scroll-toolkit-scroll (event)
  184. "Function to scroll the calendar after a toolkit scroll-bar click."
  185. (interactive "e")
  186. (let ((part (nth 4 (event-end event))))
  187. ;; Not bothering with drag events (handle, end-scroll).
  188. (cond ((memq part '(above-handle up top))
  189. (calendar-scroll-right nil event))
  190. ((memq part '(below-handle down bottom))
  191. (calendar-scroll-left nil event)))))
  192. ;;;###cal-autoload
  193. (defun calendar-scroll-right-three-months (arg &optional event)
  194. "Scroll the displayed calendar window right by 3*ARG months.
  195. If ARG is negative the calendar is scrolled left. Maintains the relative
  196. position of the cursor with respect to the calendar as well as possible.
  197. EVENT is an event like `last-nonmenu-event'."
  198. (interactive (list (prefix-numeric-value current-prefix-arg)
  199. last-nonmenu-event))
  200. (calendar-scroll-left (* -3 arg) event))
  201. (define-obsolete-function-alias 'scroll-calendar-right-three-months
  202. 'calendar-scroll-right-three-months "23.1")
  203. ;;;###cal-autoload
  204. (defun calendar-forward-day (arg)
  205. "Move the cursor forward ARG days.
  206. Moves backward if ARG is negative."
  207. (interactive "p")
  208. (unless (zerop arg)
  209. (let* ((cursor-date (or (calendar-cursor-to-date)
  210. (progn
  211. (if (> arg 0) (setq arg (1- arg)))
  212. (calendar-cursor-to-nearest-date))))
  213. (new-cursor-date
  214. (calendar-gregorian-from-absolute
  215. (+ (calendar-absolute-from-gregorian cursor-date) arg)))
  216. (new-display-month (calendar-extract-month new-cursor-date))
  217. (new-display-year (calendar-extract-year new-cursor-date)))
  218. ;; Put the new month on the screen, if needed.
  219. (unless (calendar-date-is-visible-p new-cursor-date)
  220. ;; The next line gives smoother scrolling IMO (one month at a
  221. ;; time rather than two).
  222. (calendar-increment-month new-display-month new-display-year
  223. (if (< arg 0) 1 -1))
  224. (calendar-other-month new-display-month new-display-year))
  225. ;; Go to the new date.
  226. (calendar-cursor-to-visible-date new-cursor-date)))
  227. (run-hooks 'calendar-move-hook))
  228. ;;;###cal-autoload
  229. (defun calendar-backward-day (arg)
  230. "Move the cursor back ARG days.
  231. Moves forward if ARG is negative."
  232. (interactive "p")
  233. (calendar-forward-day (- arg)))
  234. ;;;###cal-autoload
  235. (defun calendar-forward-week (arg)
  236. "Move the cursor forward ARG weeks.
  237. Moves backward if ARG is negative."
  238. (interactive "p")
  239. (calendar-forward-day (* arg 7)))
  240. ;;;###cal-autoload
  241. (defun calendar-backward-week (arg)
  242. "Move the cursor back ARG weeks.
  243. Moves forward if ARG is negative."
  244. (interactive "p")
  245. (calendar-forward-day (* arg -7)))
  246. ;;;###cal-autoload
  247. (defun calendar-beginning-of-week (arg)
  248. "Move the cursor back ARG calendar-week-start-day's."
  249. (interactive "p")
  250. (calendar-cursor-to-nearest-date)
  251. (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
  252. (calendar-backward-day
  253. (if (= day calendar-week-start-day)
  254. (* 7 arg)
  255. (+ (mod (- day calendar-week-start-day) 7)
  256. (* 7 (1- arg)))))))
  257. ;;;###cal-autoload
  258. (defun calendar-end-of-week (arg)
  259. "Move the cursor forward ARG calendar-week-start-day+6's."
  260. (interactive "p")
  261. (calendar-cursor-to-nearest-date)
  262. (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
  263. (calendar-forward-day
  264. (if (= day (mod (1- calendar-week-start-day) 7))
  265. (* 7 arg)
  266. (+ (- 6 (mod (- day calendar-week-start-day) 7))
  267. (* 7 (1- arg)))))))
  268. ;;;###cal-autoload
  269. (defun calendar-beginning-of-month (arg)
  270. "Move the cursor backward ARG month beginnings."
  271. (interactive "p")
  272. (calendar-cursor-to-nearest-date)
  273. (let* ((date (calendar-cursor-to-date))
  274. (month (calendar-extract-month date))
  275. (day (calendar-extract-day date))
  276. (year (calendar-extract-year date)))
  277. (if (= day 1)
  278. (calendar-backward-month arg)
  279. (calendar-cursor-to-visible-date (list month 1 year))
  280. (calendar-backward-month (1- arg)))))
  281. ;;;###cal-autoload
  282. (defun calendar-end-of-month (arg)
  283. "Move the cursor forward ARG month ends."
  284. (interactive "p")
  285. (calendar-cursor-to-nearest-date)
  286. (let* ((date (calendar-cursor-to-date))
  287. (month (calendar-extract-month date))
  288. (day (calendar-extract-day date))
  289. (year (calendar-extract-year date))
  290. (last-day (calendar-last-day-of-month month year))
  291. (last-day (progn
  292. (unless (= day last-day)
  293. (calendar-cursor-to-visible-date
  294. (list month last-day year))
  295. (setq arg (1- arg)))
  296. (calendar-increment-month month year arg)
  297. (list month
  298. (calendar-last-day-of-month month year)
  299. year))))
  300. (if (not (calendar-date-is-visible-p last-day))
  301. (calendar-other-month month year)
  302. (calendar-cursor-to-visible-date last-day)))
  303. (run-hooks 'calendar-move-hook))
  304. ;;;###cal-autoload
  305. (defun calendar-beginning-of-year (arg)
  306. "Move the cursor backward ARG year beginnings."
  307. (interactive "p")
  308. (calendar-cursor-to-nearest-date)
  309. (let* ((date (calendar-cursor-to-date))
  310. (month (calendar-extract-month date))
  311. (day (calendar-extract-day date))
  312. (year (calendar-extract-year date))
  313. (jan-first (list 1 1 year))
  314. (calendar-move-hook nil))
  315. (if (and (= day 1) (= 1 month))
  316. (calendar-backward-month (* 12 arg))
  317. (if (and (= arg 1)
  318. (calendar-date-is-visible-p jan-first))
  319. (calendar-cursor-to-visible-date jan-first)
  320. (calendar-other-month 1 (- year (1- arg)))
  321. (calendar-cursor-to-visible-date (list 1 1 displayed-year)))))
  322. (run-hooks 'calendar-move-hook))
  323. ;;;###cal-autoload
  324. (defun calendar-end-of-year (arg)
  325. "Move the cursor forward ARG year beginnings."
  326. (interactive "p")
  327. (calendar-cursor-to-nearest-date)
  328. (let* ((date (calendar-cursor-to-date))
  329. (month (calendar-extract-month date))
  330. (day (calendar-extract-day date))
  331. (year (calendar-extract-year date))
  332. (dec-31 (list 12 31 year))
  333. (calendar-move-hook nil))
  334. (if (and (= day 31) (= 12 month))
  335. (calendar-forward-month (* 12 arg))
  336. (if (and (= arg 1)
  337. (calendar-date-is-visible-p dec-31))
  338. (calendar-cursor-to-visible-date dec-31)
  339. (calendar-other-month 12 (+ year (1- arg)))
  340. (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
  341. (run-hooks 'calendar-move-hook))
  342. ;;;###cal-autoload
  343. (defun calendar-goto-date (date)
  344. "Move cursor to DATE."
  345. (interactive (list (calendar-read-date)))
  346. (let ((month (calendar-extract-month date))
  347. (year (calendar-extract-year date)))
  348. (if (not (calendar-date-is-visible-p date))
  349. (calendar-other-month
  350. (if (and (= month 1) (= year 1))
  351. 2
  352. month)
  353. year)))
  354. (calendar-cursor-to-visible-date date)
  355. (run-hooks 'calendar-move-hook))
  356. ;;;###cal-autoload
  357. (defun calendar-goto-day-of-year (year day &optional noecho)
  358. "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
  359. Negative DAY counts backward from end of year."
  360. (interactive
  361. (let* ((year (calendar-read
  362. "Year (>0): "
  363. (lambda (x) (> x 0))
  364. (number-to-string (calendar-extract-year
  365. (calendar-current-date)))))
  366. (last (if (calendar-leap-year-p year) 366 365))
  367. (day (calendar-read
  368. (format "Day number (+/- 1-%d): " last)
  369. (lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
  370. (list year day)))
  371. (calendar-goto-date
  372. (calendar-gregorian-from-absolute
  373. (if (< 0 day)
  374. (+ -1 day (calendar-absolute-from-gregorian (list 1 1 year)))
  375. (+ 1 day (calendar-absolute-from-gregorian (list 12 31 year))))))
  376. (or noecho (calendar-print-day-of-year)))
  377. (provide 'cal-move)
  378. ;;; cal-move.el ends here