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