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, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
37 (defun calendar-cursor-to-nearest-date ()
38 "Move the cursor to the closest date.
39 The position of the cursor is unchanged if it is already on a date.
40 Returns the list (month day year) giving the cursor position."
41 (or (calendar-cursor-to-date)
42 (let ((column (current-column)))
43 (when (> 3 (count-lines (point-min) (point)))
45 (move-to-column column))
46 (if (not (looking-at "[0-9]"))
47 (if (and (not (looking-at " *$"))
54 (re-search-forward "[0-9]" nil t)
56 (re-search-backward "[0-9]" nil t)))
57 (calendar-cursor-to-date))))
59 (defvar displayed-month) ; from calendar-generate
60 (defvar displayed-year)
63 (defun calendar-cursor-to-visible-date (date)
64 "Move the cursor to DATE that is on the screen."
65 (let ((month (calendar-extract-month date))
66 (day (calendar-extract-day date))
67 (year (calendar-extract-year date)))
71 (- (calendar-day-of-week (list month 1 year))
72 calendar-week-start-day)
77 (1+ (calendar-interval
78 displayed-month displayed-year month year)))
80 (- (calendar-day-of-week date)
81 calendar-week-start-day)
85 (defun calendar-goto-today ()
86 "Reposition the calendar window so the current date is visible."
88 (let ((today (calendar-current-date))) ; the date might have changed
89 (if (not (calendar-date-is-visible-p today))
90 (calendar-generate-window)
91 (calendar-update-mode-line)
92 (calendar-cursor-to-visible-date today)))
93 (run-hooks 'calendar-move-hook))
96 (defun calendar-forward-month (arg)
97 "Move the cursor forward ARG months.
98 Movement is backward if ARG is negative."
100 (calendar-cursor-to-nearest-date)
101 (let* ((cursor-date (calendar-cursor-to-date t))
102 (month (calendar-extract-month cursor-date))
103 (day (calendar-extract-day cursor-date))
104 (year (calendar-extract-year cursor-date))
106 (calendar-increment-month month year arg)
107 (calendar-last-day-of-month month year)))
109 ;; Put the new month on the screen, if needed, and go to the new date.
110 (new-cursor-date (list month day year)))
111 (if (not (calendar-date-is-visible-p new-cursor-date))
112 (calendar-other-month month year))
113 (calendar-cursor-to-visible-date new-cursor-date))
114 (run-hooks 'calendar-move-hook))
117 (defun calendar-forward-year (arg)
118 "Move the cursor forward by ARG years.
119 Movement is backward if ARG is negative."
121 (calendar-forward-month (* 12 arg)))
124 (defun calendar-backward-month (arg)
125 "Move the cursor backward by ARG months.
126 Movement is forward if ARG is negative."
128 (calendar-forward-month (- arg)))
131 (defun calendar-backward-year (arg)
132 "Move the cursor backward ARG years.
133 Movement is forward is ARG is negative."
135 (calendar-forward-month (* -12 arg)))
138 (defun calendar-scroll-left (&optional arg event)
139 "Scroll the displayed calendar left by ARG months.
140 If ARG is negative the calendar is scrolled right. Maintains the relative
141 position of the cursor with respect to the calendar as well as possible.
142 EVENT is an event like `last-nonmenu-event'."
143 (interactive (list (prefix-numeric-value current-prefix-arg)
145 (unless arg (setq arg 1))
146 (save-selected-window
147 ;; Nil if called from menu-bar.
148 (if (setq event (event-start event)) (select-window (posn-window event)))
149 (calendar-cursor-to-nearest-date)
151 (let ((old-date (calendar-cursor-to-date))
152 (today (calendar-current-date))
153 (month displayed-month)
154 (year displayed-year))
155 (calendar-increment-month month year arg)
156 (calendar-generate-window month year)
157 (calendar-cursor-to-visible-date
159 ((calendar-date-is-visible-p old-date) old-date)
160 ((calendar-date-is-visible-p today) today)
161 (t (list month 1 year))))))
162 (run-hooks 'calendar-move-hook)))
164 (define-obsolete-function-alias
165 'scroll-calendar-left 'calendar-scroll-left "23.1")
168 (defun calendar-scroll-right (&optional arg event)
169 "Scroll the displayed calendar window right by ARG months.
170 If ARG is negative the calendar is scrolled left. Maintains the relative
171 position of the cursor with respect to the calendar as well as possible.
172 EVENT is an event like `last-nonmenu-event'."
173 (interactive (list (prefix-numeric-value current-prefix-arg)
175 (calendar-scroll-left (- (or arg 1)) event))
177 (define-obsolete-function-alias
178 'scroll-calendar-right 'calendar-scroll-right "23.1")
181 (defun calendar-scroll-left-three-months (arg)
182 "Scroll the displayed calendar window left by 3*ARG months.
183 If ARG is negative the calendar is scrolled right. Maintains the relative
184 position of the cursor with respect to the calendar as well as possible."
186 (calendar-scroll-left (* 3 arg)))
188 (define-obsolete-function-alias 'scroll-calendar-left-three-months
189 'calendar-scroll-left-three-months "23.1")
192 (defun calendar-scroll-right-three-months (arg)
193 "Scroll the displayed calendar window right by 3*ARG months.
194 If ARG is negative the calendar is scrolled left. Maintains the relative
195 position of the cursor with respect to the calendar as well as possible."
197 (calendar-scroll-left (* -3 arg)))
199 (define-obsolete-function-alias 'scroll-calendar-right-three-months
200 'calendar-scroll-right-three-months "23.1")
203 (defun calendar-forward-day (arg)
204 "Move the cursor forward ARG days.
205 Moves backward if ARG is negative."
208 (let* ((cursor-date (or (calendar-cursor-to-date)
210 (if (> arg 0) (setq arg (1- arg)))
211 (calendar-cursor-to-nearest-date))))
213 (calendar-gregorian-from-absolute
214 (+ (calendar-absolute-from-gregorian cursor-date) arg)))
215 (new-display-month (calendar-extract-month new-cursor-date))
216 (new-display-year (calendar-extract-year new-cursor-date)))
217 ;; Put the new month on the screen, if needed, and go to the new date.
218 (if (not (calendar-date-is-visible-p new-cursor-date))
219 (calendar-other-month new-display-month new-display-year))
220 (calendar-cursor-to-visible-date new-cursor-date)))
221 (run-hooks 'calendar-move-hook))
224 (defun calendar-backward-day (arg)
225 "Move the cursor back ARG days.
226 Moves forward if ARG is negative."
228 (calendar-forward-day (- arg)))
231 (defun calendar-forward-week (arg)
232 "Move the cursor forward ARG weeks.
233 Moves backward if ARG is negative."
235 (calendar-forward-day (* arg 7)))
238 (defun calendar-backward-week (arg)
239 "Move the cursor back ARG weeks.
240 Moves forward if ARG is negative."
242 (calendar-forward-day (* arg -7)))
245 (defun calendar-beginning-of-week (arg)
246 "Move the cursor back ARG calendar-week-start-day's."
248 (calendar-cursor-to-nearest-date)
249 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
250 (calendar-backward-day
251 (if (= day calendar-week-start-day)
253 (+ (mod (- day calendar-week-start-day) 7)
257 (defun calendar-end-of-week (arg)
258 "Move the cursor forward ARG calendar-week-start-day+6's."
260 (calendar-cursor-to-nearest-date)
261 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
262 (calendar-forward-day
263 (if (= day (mod (1- calendar-week-start-day) 7))
265 (+ (- 6 (mod (- day calendar-week-start-day) 7))
269 (defun calendar-beginning-of-month (arg)
270 "Move the cursor backward ARG month beginnings."
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)))
278 (calendar-backward-month arg)
279 (calendar-cursor-to-visible-date (list month 1 year))
280 (calendar-backward-month (1- arg)))))
283 (defun calendar-end-of-month (arg)
284 "Move the cursor forward ARG month ends."
286 (calendar-cursor-to-nearest-date)
287 (let* ((date (calendar-cursor-to-date))
288 (month (calendar-extract-month date))
289 (day (calendar-extract-day date))
290 (year (calendar-extract-year date))
291 (last-day (calendar-last-day-of-month month year))
293 (unless (= day last-day)
294 (calendar-cursor-to-visible-date
295 (list month last-day year))
297 (calendar-increment-month month year arg)
299 (calendar-last-day-of-month month year)
301 (if (not (calendar-date-is-visible-p last-day))
302 (calendar-other-month month year)
303 (calendar-cursor-to-visible-date last-day)))
304 (run-hooks 'calendar-move-hook))
307 (defun calendar-beginning-of-year (arg)
308 "Move the cursor backward ARG year beginnings."
310 (calendar-cursor-to-nearest-date)
311 (let* ((date (calendar-cursor-to-date))
312 (month (calendar-extract-month date))
313 (day (calendar-extract-day date))
314 (year (calendar-extract-year date))
315 (jan-first (list 1 1 year))
316 (calendar-move-hook nil))
317 (if (and (= day 1) (= 1 month))
318 (calendar-backward-month (* 12 arg))
320 (calendar-date-is-visible-p jan-first))
321 (calendar-cursor-to-visible-date jan-first)
322 (calendar-other-month 1 (- year (1- arg)))
323 (calendar-cursor-to-visible-date (list 1 1 displayed-year)))))
324 (run-hooks 'calendar-move-hook))
327 (defun calendar-end-of-year (arg)
328 "Move the cursor forward ARG year beginnings."
330 (calendar-cursor-to-nearest-date)
331 (let* ((date (calendar-cursor-to-date))
332 (month (calendar-extract-month date))
333 (day (calendar-extract-day date))
334 (year (calendar-extract-year date))
335 (dec-31 (list 12 31 year))
336 (calendar-move-hook nil))
337 (if (and (= day 31) (= 12 month))
338 (calendar-forward-month (* 12 arg))
340 (calendar-date-is-visible-p dec-31))
341 (calendar-cursor-to-visible-date dec-31)
342 (calendar-other-month 12 (+ year (1- arg)))
343 (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
344 (run-hooks 'calendar-move-hook))
347 (defun calendar-goto-date (date)
348 "Move cursor to DATE."
349 (interactive (list (calendar-read-date)))
350 (let ((month (calendar-extract-month date))
351 (year (calendar-extract-year date)))
352 (if (not (calendar-date-is-visible-p date))
353 (calendar-other-month
354 (if (and (= month 1) (= year 1))
358 (calendar-cursor-to-visible-date date)
359 (run-hooks 'calendar-move-hook))
362 (defun calendar-goto-day-of-year (year day &optional noecho)
363 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
364 Negative DAY counts backward from end of year."
366 (let* ((year (calendar-read
369 (number-to-string (calendar-extract-year
370 (calendar-current-date)))))
371 (last (if (calendar-leap-year-p year) 366 365))
373 (format "Day number (+/- 1-%d): " last)
374 (lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
377 (calendar-gregorian-from-absolute
379 (+ -1 day (calendar-absolute-from-gregorian (list 1 1 year)))
380 (+ 1 day (calendar-absolute-from-gregorian (list 12 31 year))))))
381 (or noecho (calendar-print-day-of-year)))
385 ;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
386 ;;; cal-move.el ends here