]> code.delx.au - gnu-emacs/blob - lisp/calendar/cal-move.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / calendar / cal-move.el
1 ;;; cal-move.el --- calendar functions for movement in the calendar
2
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: calendar
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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.
27
28 ;;; Commentary:
29
30 ;; See calendar.el.
31
32 ;;; Code:
33
34 (require 'calendar)
35
36 ;;;###cal-autoload
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)))
44 (goto-line 3)
45 (move-to-column column))
46 (if (not (looking-at "[0-9]"))
47 (if (and (not (looking-at " *$"))
48 (or (< column 25)
49 (and (> column 27)
50 (< column 50))
51 (and (> column 52)
52 (< column 75))))
53 (progn
54 (re-search-forward "[0-9]" nil t)
55 (backward-char 1))
56 (re-search-backward "[0-9]" nil t)))
57 (calendar-cursor-to-date))))
58
59 (defvar displayed-month) ; from calendar-generate
60 (defvar displayed-year)
61
62 ;;;###cal-autoload
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)))
68 (goto-line (+ 3
69 (/ (+ day -1
70 (mod
71 (- (calendar-day-of-week (list month 1 year))
72 calendar-week-start-day)
73 7))
74 7)))
75 (move-to-column (+ 6
76 (* 25
77 (1+ (calendar-interval
78 displayed-month displayed-year month year)))
79 (* 3 (mod
80 (- (calendar-day-of-week date)
81 calendar-week-start-day)
82 7))))))
83
84 ;;;###cal-autoload
85 (defun calendar-goto-today ()
86 "Reposition the calendar window so the current date is visible."
87 (interactive)
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))
94
95 ;;;###cal-autoload
96 (defun calendar-forward-month (arg)
97 "Move the cursor forward ARG months.
98 Movement is backward if ARG is negative."
99 (interactive "p")
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))
105 (last (progn
106 (calendar-increment-month month year arg)
107 (calendar-last-day-of-month month year)))
108 (day (min last day))
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))
115
116 ;;;###cal-autoload
117 (defun calendar-forward-year (arg)
118 "Move the cursor forward by ARG years.
119 Movement is backward if ARG is negative."
120 (interactive "p")
121 (calendar-forward-month (* 12 arg)))
122
123 ;;;###cal-autoload
124 (defun calendar-backward-month (arg)
125 "Move the cursor backward by ARG months.
126 Movement is forward if ARG is negative."
127 (interactive "p")
128 (calendar-forward-month (- arg)))
129
130 ;;;###cal-autoload
131 (defun calendar-backward-year (arg)
132 "Move the cursor backward ARG years.
133 Movement is forward is ARG is negative."
134 (interactive "p")
135 (calendar-forward-month (* -12 arg)))
136
137 ;;;###cal-autoload
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)
144 last-nonmenu-event))
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)
150 (unless (zerop arg)
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
158 (cond
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)))
163
164 (define-obsolete-function-alias
165 'scroll-calendar-left 'calendar-scroll-left "23.1")
166
167 ;;;###cal-autoload
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)
174 last-nonmenu-event))
175 (calendar-scroll-left (- (or arg 1)) event))
176
177 (define-obsolete-function-alias
178 'scroll-calendar-right 'calendar-scroll-right "23.1")
179
180 ;;;###cal-autoload
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."
185 (interactive "p")
186 (calendar-scroll-left (* 3 arg)))
187
188 (define-obsolete-function-alias 'scroll-calendar-left-three-months
189 'calendar-scroll-left-three-months "23.1")
190
191 ;;;###cal-autoload
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."
196 (interactive "p")
197 (calendar-scroll-left (* -3 arg)))
198
199 (define-obsolete-function-alias 'scroll-calendar-right-three-months
200 'calendar-scroll-right-three-months "23.1")
201
202 ;;;###cal-autoload
203 (defun calendar-forward-day (arg)
204 "Move the cursor forward ARG days.
205 Moves backward if ARG is negative."
206 (interactive "p")
207 (unless (zerop arg)
208 (let* ((cursor-date (or (calendar-cursor-to-date)
209 (progn
210 (if (> arg 0) (setq arg (1- arg)))
211 (calendar-cursor-to-nearest-date))))
212 (new-cursor-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))
222
223 ;;;###cal-autoload
224 (defun calendar-backward-day (arg)
225 "Move the cursor back ARG days.
226 Moves forward if ARG is negative."
227 (interactive "p")
228 (calendar-forward-day (- arg)))
229
230 ;;;###cal-autoload
231 (defun calendar-forward-week (arg)
232 "Move the cursor forward ARG weeks.
233 Moves backward if ARG is negative."
234 (interactive "p")
235 (calendar-forward-day (* arg 7)))
236
237 ;;;###cal-autoload
238 (defun calendar-backward-week (arg)
239 "Move the cursor back ARG weeks.
240 Moves forward if ARG is negative."
241 (interactive "p")
242 (calendar-forward-day (* arg -7)))
243
244 ;;;###cal-autoload
245 (defun calendar-beginning-of-week (arg)
246 "Move the cursor back ARG calendar-week-start-day's."
247 (interactive "p")
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)
252 (* 7 arg)
253 (+ (mod (- day calendar-week-start-day) 7)
254 (* 7 (1- arg)))))))
255
256 ;;;###cal-autoload
257 (defun calendar-end-of-week (arg)
258 "Move the cursor forward ARG calendar-week-start-day+6's."
259 (interactive "p")
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))
264 (* 7 arg)
265 (+ (- 6 (mod (- day calendar-week-start-day) 7))
266 (* 7 (1- arg)))))))
267
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
282 ;;;###cal-autoload
283 (defun calendar-end-of-month (arg)
284 "Move the cursor forward ARG month ends."
285 (interactive "p")
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))
292 (last-day (progn
293 (unless (= day last-day)
294 (calendar-cursor-to-visible-date
295 (list month last-day year))
296 (setq arg (1- arg)))
297 (calendar-increment-month month year arg)
298 (list month
299 (calendar-last-day-of-month month year)
300 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))
305
306 ;;;###cal-autoload
307 (defun calendar-beginning-of-year (arg)
308 "Move the cursor backward ARG year beginnings."
309 (interactive "p")
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))
319 (if (and (= arg 1)
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))
325
326 ;;;###cal-autoload
327 (defun calendar-end-of-year (arg)
328 "Move the cursor forward ARG year beginnings."
329 (interactive "p")
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))
339 (if (and (= arg 1)
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))
345
346 ;;;###cal-autoload
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))
355 2
356 month)
357 year)))
358 (calendar-cursor-to-visible-date date)
359 (run-hooks 'calendar-move-hook))
360
361 ;;;###cal-autoload
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."
365 (interactive
366 (let* ((year (calendar-read
367 "Year (>0): "
368 (lambda (x) (> x 0))
369 (number-to-string (calendar-extract-year
370 (calendar-current-date)))))
371 (last (if (calendar-leap-year-p year) 366 365))
372 (day (calendar-read
373 (format "Day number (+/- 1-%d): " last)
374 (lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
375 (list year day)))
376 (calendar-goto-date
377 (calendar-gregorian-from-absolute
378 (if (< 0 day)
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)))
382
383 (provide 'cal-move)
384
385 ;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
386 ;;; cal-move.el ends here