(calendar-cursor-to-nearest-date): Use or, when. Move definition before use.
[emacs.git] / lisp / calendar / cal-move.el
blob2aef750db684deab4829c2658f4166c48c16fc23
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>
8 ;; Keywords: calendar
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)
16 ;; 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; 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.
28 ;;; Commentary:
30 ;; This collection of functions implements movement in the calendar for
31 ;; calendar.el.
33 ;;; Code:
35 (require 'calendar)
37 ;;;###cal-autoload
38 (defun calendar-cursor-to-nearest-date ()
39 "Move the cursor to the closest date.
40 The position of the cursor is unchanged if it is already on a date.
41 Returns the list (month day year) giving the cursor position."
42 (let ((date (calendar-cursor-to-date))
43 (column (current-column)))
44 (or date
45 (when (> 3 (count-lines (point-min) (point)))
46 (goto-line 3)
47 (move-to-column column))
48 (if (not (looking-at "[0-9]"))
49 (if (and (not (looking-at " *$"))
50 (or (< column 25)
51 (and (> column 27)
52 (< column 50))
53 (and (> column 52)
54 (< column 75))))
55 (progn
56 (re-search-forward "[0-9]" nil t)
57 (backward-char 1))
58 (re-search-backward "[0-9]" nil t)))
59 (calendar-cursor-to-date))))
61 (defvar displayed-month) ; from generate-calendar
62 (defvar displayed-year)
64 ;;;###cal-autoload
65 (defun calendar-cursor-to-visible-date (date)
66 "Move the cursor to DATE that is on the screen."
67 (let* ((month (extract-calendar-month date))
68 (day (extract-calendar-day date))
69 (year (extract-calendar-year date))
70 (first-of-month-weekday (calendar-day-of-week (list month 1 year))))
71 (goto-line (+ 3
72 (/ (+ day -1
73 (mod
74 (- (calendar-day-of-week (list month 1 year))
75 calendar-week-start-day)
76 7))
77 7)))
78 (move-to-column (+ 6
79 (* 25
80 (1+ (calendar-interval
81 displayed-month displayed-year month year)))
82 (* 3 (mod
83 (- (calendar-day-of-week date)
84 calendar-week-start-day)
85 7))))))
87 ;;;###cal-autoload
88 (defun calendar-goto-today ()
89 "Reposition the calendar window so the current date is visible."
90 (interactive)
91 (let ((today (calendar-current-date))) ; the date might have changed
92 (if (not (calendar-date-is-visible-p today))
93 (generate-calendar-window)
94 (update-calendar-mode-line)
95 (calendar-cursor-to-visible-date today)))
96 (run-hooks 'calendar-move-hook))
98 ;;;###cal-autoload
99 (defun calendar-forward-month (arg)
100 "Move the cursor forward ARG months.
101 Movement is backward if ARG is negative."
102 (interactive "p")
103 (calendar-cursor-to-nearest-date)
104 (let* ((cursor-date (calendar-cursor-to-date t))
105 (month (extract-calendar-month cursor-date))
106 (day (extract-calendar-day cursor-date))
107 (year (extract-calendar-year cursor-date)))
108 (increment-calendar-month month year arg)
109 (let ((last (calendar-last-day-of-month month year)))
110 (if (< last day)
111 (setq day last)))
112 ;; Put the new month on the screen, if needed, and go to the new date.
113 (let ((new-cursor-date (list month day year)))
114 (if (not (calendar-date-is-visible-p new-cursor-date))
115 (calendar-other-month month year))
116 (calendar-cursor-to-visible-date new-cursor-date)))
117 (run-hooks 'calendar-move-hook))
119 ;;;###cal-autoload
120 (defun calendar-forward-year (arg)
121 "Move the cursor forward by ARG years.
122 Movement is backward if ARG is negative."
123 (interactive "p")
124 (calendar-forward-month (* 12 arg)))
126 ;;;###cal-autoload
127 (defun calendar-backward-month (arg)
128 "Move the cursor backward by ARG months.
129 Movement is forward if ARG is negative."
130 (interactive "p")
131 (calendar-forward-month (- arg)))
133 ;;;###cal-autoload
134 (defun calendar-backward-year (arg)
135 "Move the cursor backward ARG years.
136 Movement is forward is ARG is negative."
137 (interactive "p")
138 (calendar-forward-month (* -12 arg)))
140 ;;;###cal-autoload
141 (defun calendar-scroll-left (&optional arg event)
142 "Scroll the displayed calendar left by ARG months.
143 If ARG is negative the calendar is scrolled right. Maintains the relative
144 position of the cursor with respect to the calendar as well as possible.
145 EVENT is an event like `last-nonmenu-event'."
146 (interactive (list (prefix-numeric-value current-prefix-arg)
147 last-nonmenu-event))
148 (unless arg (setq arg 1))
149 (save-selected-window
150 (select-window (posn-window (event-start event)))
151 (calendar-cursor-to-nearest-date)
152 (unless (zerop arg)
153 (let ((old-date (calendar-cursor-to-date))
154 (today (calendar-current-date))
155 (month displayed-month)
156 (year displayed-year))
157 (increment-calendar-month month year arg)
158 (generate-calendar-window month year)
159 (calendar-cursor-to-visible-date
160 (cond
161 ((calendar-date-is-visible-p old-date) old-date)
162 ((calendar-date-is-visible-p today) today)
163 (t (list month 1 year))))))
164 (run-hooks 'calendar-move-hook)))
166 (define-obsolete-function-alias
167 'scroll-calendar-left 'calendar-scroll-left "23.1")
169 ;;;###cal-autoload
170 (defun calendar-scroll-right (&optional arg event)
171 "Scroll the displayed calendar window right by ARG months.
172 If ARG is negative the calendar is scrolled left. Maintains the relative
173 position of the cursor with respect to the calendar as well as possible.
174 EVENT is an event like `last-nonmenu-event'."
175 (interactive (list (prefix-numeric-value current-prefix-arg)
176 last-nonmenu-event))
177 (calendar-scroll-left (- (or arg 1)) event))
179 (define-obsolete-function-alias
180 'scroll-calendar-right 'calendar-scroll-right "23.1")
182 ;;;###cal-autoload
183 (defun calendar-scroll-left-three-months (arg)
184 "Scroll the displayed calendar window left by 3*ARG months.
185 If ARG is negative the calendar is scrolled right. Maintains the relative
186 position of the cursor with respect to the calendar as well as possible."
187 (interactive "p")
188 (calendar-scroll-left (* 3 arg)))
190 (define-obsolete-function-alias 'scroll-calendar-left-three-months
191 'calendar-scroll-left-three-months "23.1")
193 ;;;###cal-autoload
194 (defun calendar-scroll-right-three-months (arg)
195 "Scroll the displayed calendar window right by 3*ARG months.
196 If ARG is negative the calendar is scrolled left. Maintains the relative
197 position of the cursor with respect to the calendar as well as possible."
198 (interactive "p")
199 (calendar-scroll-left (* -3 arg)))
201 (define-obsolete-function-alias 'scroll-calendar-right-three-months
202 'calendar-scroll-right-three-months "23.1")
204 ;;;###cal-autoload
205 (defun calendar-forward-day (arg)
206 "Move the cursor forward ARG days.
207 Moves backward if ARG is negative."
208 (interactive "p")
209 (unless (zerop arg)
210 (let* ((cursor-date (or (calendar-cursor-to-date)
211 (progn
212 (if (> arg 0) (setq arg (1- arg)))
213 (calendar-cursor-to-nearest-date))))
214 (new-cursor-date
215 (calendar-gregorian-from-absolute
216 (+ (calendar-absolute-from-gregorian cursor-date) arg)))
217 (new-display-month (extract-calendar-month new-cursor-date))
218 (new-display-year (extract-calendar-year new-cursor-date)))
219 ;; Put the new month on the screen, if needed, and go to the new date.
220 (if (not (calendar-date-is-visible-p new-cursor-date))
221 (calendar-other-month new-display-month new-display-year))
222 (calendar-cursor-to-visible-date new-cursor-date)))
223 (run-hooks 'calendar-move-hook))
225 ;;;###cal-autoload
226 (defun calendar-backward-day (arg)
227 "Move the cursor back ARG days.
228 Moves forward if ARG is negative."
229 (interactive "p")
230 (calendar-forward-day (- arg)))
232 ;;;###cal-autoload
233 (defun calendar-forward-week (arg)
234 "Move the cursor forward ARG weeks.
235 Moves backward if ARG is negative."
236 (interactive "p")
237 (calendar-forward-day (* arg 7)))
239 ;;;###cal-autoload
240 (defun calendar-backward-week (arg)
241 "Move the cursor back ARG weeks.
242 Moves forward if ARG is negative."
243 (interactive "p")
244 (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)))))))
258 ;;;###cal-autoload
259 (defun calendar-end-of-week (arg)
260 "Move the cursor forward ARG calendar-week-start-day+6's."
261 (interactive "p")
262 (calendar-cursor-to-nearest-date)
263 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
264 (calendar-forward-day
265 (if (= day (mod (1- calendar-week-start-day) 7))
266 (* 7 arg)
267 (+ (- 6 (mod (- day calendar-week-start-day) 7))
268 (* 7 (1- arg)))))))
270 ;;;###cal-autoload
271 (defun calendar-beginning-of-month (arg)
272 "Move the cursor backward ARG month beginnings."
273 (interactive "p")
274 (calendar-cursor-to-nearest-date)
275 (let* ((date (calendar-cursor-to-date))
276 (month (extract-calendar-month date))
277 (day (extract-calendar-day date))
278 (year (extract-calendar-year date)))
279 (if (= day 1)
280 (calendar-backward-month arg)
281 (calendar-cursor-to-visible-date (list month 1 year))
282 (calendar-backward-month (1- arg)))))
284 ;;;###cal-autoload
285 (defun calendar-end-of-month (arg)
286 "Move the cursor forward ARG month ends."
287 (interactive "p")
288 (calendar-cursor-to-nearest-date)
289 (let* ((date (calendar-cursor-to-date))
290 (month (extract-calendar-month date))
291 (day (extract-calendar-day date))
292 (year (extract-calendar-year date))
293 (last-day (calendar-last-day-of-month month year)))
294 (unless (= day last-day)
295 (calendar-cursor-to-visible-date (list month last-day year))
296 (setq arg (1- arg)))
297 (increment-calendar-month month year arg)
298 (let ((last-day (list
299 month
300 (calendar-last-day-of-month month year)
301 year)))
302 (if (not (calendar-date-is-visible-p last-day))
303 (calendar-other-month month year)
304 (calendar-cursor-to-visible-date last-day))))
305 (run-hooks 'calendar-move-hook))
307 ;;;###cal-autoload
308 (defun calendar-beginning-of-year (arg)
309 "Move the cursor backward ARG year beginnings."
310 (interactive "p")
311 (calendar-cursor-to-nearest-date)
312 (let* ((date (calendar-cursor-to-date))
313 (month (extract-calendar-month date))
314 (day (extract-calendar-day date))
315 (year (extract-calendar-year date))
316 (jan-first (list 1 1 year))
317 (calendar-move-hook nil))
318 (if (and (= day 1) (= 1 month))
319 (calendar-backward-month (* 12 arg))
320 (if (and (= arg 1)
321 (calendar-date-is-visible-p jan-first))
322 (calendar-cursor-to-visible-date jan-first)
323 (calendar-other-month 1 (- year (1- arg)))
324 (calendar-cursor-to-visible-date (list 1 1 displayed-year)))))
325 (run-hooks 'calendar-move-hook))
327 ;;;###cal-autoload
328 (defun calendar-end-of-year (arg)
329 "Move the cursor forward ARG year beginnings."
330 (interactive "p")
331 (calendar-cursor-to-nearest-date)
332 (let* ((date (calendar-cursor-to-date))
333 (month (extract-calendar-month date))
334 (day (extract-calendar-day date))
335 (year (extract-calendar-year date))
336 (dec-31 (list 12 31 year))
337 (calendar-move-hook nil))
338 (if (and (= day 31) (= 12 month))
339 (calendar-forward-month (* 12 arg))
340 (if (and (= arg 1)
341 (calendar-date-is-visible-p dec-31))
342 (calendar-cursor-to-visible-date dec-31)
343 (calendar-other-month 12 (+ year (1- arg)))
344 (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
345 (run-hooks 'calendar-move-hook))
347 ;;;###cal-autoload
348 (defun calendar-goto-date (date)
349 "Move cursor to DATE."
350 (interactive (list (calendar-read-date)))
351 (let ((month (extract-calendar-month date))
352 (year (extract-calendar-year date)))
353 (if (not (calendar-date-is-visible-p date))
354 (calendar-other-month
355 (if (and (= month 1) (= year 1))
357 month)
358 year)))
359 (calendar-cursor-to-visible-date date)
360 (run-hooks 'calendar-move-hook))
362 ;;;###cal-autoload
363 (defun calendar-goto-day-of-year (year day &optional noecho)
364 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
365 Negative DAY counts backward from end of year."
366 (interactive
367 (let* ((year (calendar-read
368 "Year (>0): "
369 (lambda (x) (> x 0))
370 (int-to-string (extract-calendar-year
371 (calendar-current-date)))))
372 (last (if (calendar-leap-year-p year) 366 365))
373 (day (calendar-read
374 (format "Day number (+/- 1-%d): " last)
375 (lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
376 (list year day)))
377 (calendar-goto-date
378 (calendar-gregorian-from-absolute
379 (if (< 0 day)
380 (+ -1 day (calendar-absolute-from-gregorian (list 1 1 year)))
381 (+ 1 day (calendar-absolute-from-gregorian (list 12 31 year))))))
382 (or noecho (calendar-print-day-of-year)))
384 (provide 'cal-move)
386 ;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
387 ;;; cal-move.el ends here