1 ;;; cal-move.el --- calendar functions for movement in the calendar
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Human-Keywords: calendar
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
35 (defun calendar-cursor-to-nearest-date ()
36 "Move the cursor to the closest date.
37 The position of the cursor is unchanged if it is already on a date.
38 Returns the list (month day year) giving the cursor position."
39 (or (calendar-cursor-to-date)
40 (let ((column (current-column)))
41 (when (> 3 (count-lines (point-min) (point)))
43 (move-to-column column))
44 (if (not (looking-at "[0-9]"))
45 (if (and (not (looking-at " *$"))
52 (re-search-forward "[0-9]" nil t)
54 (re-search-backward "[0-9]" nil t)))
55 (calendar-cursor-to-date))))
57 (defvar displayed-month) ; from calendar-generate
58 (defvar displayed-year)
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)))
69 (- (calendar-day-of-week (list month 1 year))
70 calendar-week-start-day)
75 (1+ (calendar-interval
76 displayed-month displayed-year month year)))
78 (- (calendar-day-of-week date)
79 calendar-week-start-day)
83 (defun calendar-goto-today ()
84 "Reposition the calendar window so the current date is visible."
86 (let ((today (calendar-current-date))) ; the date might have changed
87 (if (not (calendar-date-is-visible-p today))
88 (calendar-generate-window)
89 (calendar-update-mode-line)
90 (calendar-cursor-to-visible-date today)))
91 (run-hooks 'calendar-move-hook))
94 (defun calendar-forward-month (arg)
95 "Move the cursor forward ARG months.
96 Movement is backward if ARG is negative."
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))
104 (calendar-increment-month month year arg)
105 (calendar-last-day-of-month month year)))
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))
115 (defun calendar-forward-year (arg)
116 "Move the cursor forward by ARG years.
117 Movement is backward if ARG is negative."
119 (calendar-forward-month (* 12 arg)))
122 (defun calendar-backward-month (arg)
123 "Move the cursor backward by ARG months.
124 Movement is forward if ARG is negative."
126 (calendar-forward-month (- arg)))
129 (defun calendar-backward-year (arg)
130 "Move the cursor backward ARG years.
131 Movement is forward is ARG is negative."
133 (calendar-forward-month (* -12 arg)))
136 (defun calendar-scroll-left (&optional arg event)
137 "Scroll the displayed calendar left by ARG months.
138 If ARG is negative the calendar is scrolled right. Maintains the relative
139 position of the cursor with respect to the calendar as well as possible.
140 EVENT is an event like `last-nonmenu-event'."
141 (interactive (list (prefix-numeric-value current-prefix-arg)
143 (unless arg (setq arg 1))
144 (save-selected-window
145 ;; Nil if called from menu-bar.
146 (if (setq event (event-start event)) (select-window (posn-window event)))
147 (calendar-cursor-to-nearest-date)
149 (let ((old-date (calendar-cursor-to-date))
150 (today (calendar-current-date))
151 (month displayed-month)
152 (year displayed-year))
153 (calendar-increment-month month year arg)
154 (calendar-generate-window month year)
155 (calendar-cursor-to-visible-date
157 ((calendar-date-is-visible-p old-date) old-date)
158 ((calendar-date-is-visible-p today) today)
159 (t (list month 1 year))))))
160 (run-hooks 'calendar-move-hook)))
162 (define-obsolete-function-alias
163 'scroll-calendar-left 'calendar-scroll-left "23.1")
166 (defun calendar-scroll-right (&optional arg event)
167 "Scroll the displayed calendar window right by ARG months.
168 If ARG is negative the calendar is scrolled left. Maintains the relative
169 position of the cursor with respect to the calendar as well as possible.
170 EVENT is an event like `last-nonmenu-event'."
171 (interactive (list (prefix-numeric-value current-prefix-arg)
173 (calendar-scroll-left (- (or arg 1)) event))
175 (define-obsolete-function-alias
176 'scroll-calendar-right 'calendar-scroll-right "23.1")
179 (defun calendar-scroll-left-three-months (arg)
180 "Scroll the displayed calendar window left by 3*ARG months.
181 If ARG is negative the calendar is scrolled right. Maintains the relative
182 position of the cursor with respect to the calendar as well as possible."
184 (calendar-scroll-left (* 3 arg)))
186 (define-obsolete-function-alias 'scroll-calendar-left-three-months
187 'calendar-scroll-left-three-months "23.1")
190 (defun calendar-scroll-right-three-months (arg)
191 "Scroll the displayed calendar window right by 3*ARG months.
192 If ARG is negative the calendar is scrolled left. Maintains the relative
193 position of the cursor with respect to the calendar as well as possible."
195 (calendar-scroll-left (* -3 arg)))
197 (define-obsolete-function-alias 'scroll-calendar-right-three-months
198 'calendar-scroll-right-three-months "23.1")
201 (defun calendar-forward-day (arg)
202 "Move the cursor forward ARG days.
203 Moves backward if ARG is negative."
206 (let* ((cursor-date (or (calendar-cursor-to-date)
208 (if (> arg 0) (setq arg (1- arg)))
209 (calendar-cursor-to-nearest-date))))
211 (calendar-gregorian-from-absolute
212 (+ (calendar-absolute-from-gregorian cursor-date) arg)))
213 (new-display-month (calendar-extract-month new-cursor-date))
214 (new-display-year (calendar-extract-year new-cursor-date)))
215 ;; Put the new month on the screen, if needed, and go to the new date.
216 (if (not (calendar-date-is-visible-p new-cursor-date))
217 (calendar-other-month new-display-month new-display-year))
218 (calendar-cursor-to-visible-date new-cursor-date)))
219 (run-hooks 'calendar-move-hook))
222 (defun calendar-backward-day (arg)
223 "Move the cursor back ARG days.
224 Moves forward if ARG is negative."
226 (calendar-forward-day (- arg)))
229 (defun calendar-forward-week (arg)
230 "Move the cursor forward ARG weeks.
231 Moves backward if ARG is negative."
233 (calendar-forward-day (* arg 7)))
236 (defun calendar-backward-week (arg)
237 "Move the cursor back ARG weeks.
238 Moves forward if ARG is negative."
240 (calendar-forward-day (* arg -7)))
243 (defun calendar-beginning-of-week (arg)
244 "Move the cursor back ARG calendar-week-start-day's."
246 (calendar-cursor-to-nearest-date)
247 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
248 (calendar-backward-day
249 (if (= day calendar-week-start-day)
251 (+ (mod (- day calendar-week-start-day) 7)
255 (defun calendar-end-of-week (arg)
256 "Move the cursor forward ARG calendar-week-start-day+6's."
258 (calendar-cursor-to-nearest-date)
259 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
260 (calendar-forward-day
261 (if (= day (mod (1- calendar-week-start-day) 7))
263 (+ (- 6 (mod (- day calendar-week-start-day) 7))
267 (defun calendar-beginning-of-month (arg)
268 "Move the cursor backward ARG month beginnings."
270 (calendar-cursor-to-nearest-date)
271 (let* ((date (calendar-cursor-to-date))
272 (month (calendar-extract-month date))
273 (day (calendar-extract-day date))
274 (year (calendar-extract-year date)))
276 (calendar-backward-month arg)
277 (calendar-cursor-to-visible-date (list month 1 year))
278 (calendar-backward-month (1- arg)))))
281 (defun calendar-end-of-month (arg)
282 "Move the cursor forward ARG month ends."
284 (calendar-cursor-to-nearest-date)
285 (let* ((date (calendar-cursor-to-date))
286 (month (calendar-extract-month date))
287 (day (calendar-extract-day date))
288 (year (calendar-extract-year date))
289 (last-day (calendar-last-day-of-month month year))
291 (unless (= day last-day)
292 (calendar-cursor-to-visible-date
293 (list month last-day year))
295 (calendar-increment-month month year arg)
297 (calendar-last-day-of-month month year)
299 (if (not (calendar-date-is-visible-p last-day))
300 (calendar-other-month month year)
301 (calendar-cursor-to-visible-date last-day)))
302 (run-hooks 'calendar-move-hook))
305 (defun calendar-beginning-of-year (arg)
306 "Move the cursor backward ARG year beginnings."
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))
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))
325 (defun calendar-end-of-year (arg)
326 "Move the cursor forward ARG year beginnings."
328 (calendar-cursor-to-nearest-date)
329 (let* ((date (calendar-cursor-to-date))
330 (month (calendar-extract-month date))
331 (day (calendar-extract-day date))
332 (year (calendar-extract-year date))
333 (dec-31 (list 12 31 year))
334 (calendar-move-hook nil))
335 (if (and (= day 31) (= 12 month))
336 (calendar-forward-month (* 12 arg))
338 (calendar-date-is-visible-p dec-31))
339 (calendar-cursor-to-visible-date dec-31)
340 (calendar-other-month 12 (+ year (1- arg)))
341 (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
342 (run-hooks 'calendar-move-hook))
345 (defun calendar-goto-date (date)
346 "Move cursor to DATE."
347 (interactive (list (calendar-read-date)))
348 (let ((month (calendar-extract-month date))
349 (year (calendar-extract-year date)))
350 (if (not (calendar-date-is-visible-p date))
351 (calendar-other-month
352 (if (and (= month 1) (= year 1))
356 (calendar-cursor-to-visible-date date)
357 (run-hooks 'calendar-move-hook))
360 (defun calendar-goto-day-of-year (year day &optional noecho)
361 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
362 Negative DAY counts backward from end of year."
364 (let* ((year (calendar-read
367 (number-to-string (calendar-extract-year
368 (calendar-current-date)))))
369 (last (if (calendar-leap-year-p year) 366 365))
371 (format "Day number (+/- 1-%d): " last)
372 (lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
375 (calendar-gregorian-from-absolute
377 (+ -1 day (calendar-absolute-from-gregorian (list 1 1 year)))
378 (+ 1 day (calendar-absolute-from-gregorian (list 12 31 year))))))
379 (or noecho (calendar-print-day-of-year)))
383 ;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
384 ;;; cal-move.el ends here