1 ;;; cal-move.el --- calendar functions for movement in the calendar
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Human-Keywords: calendar
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; This collection of functions implements movement in the calendar for
30 ;; Comments, corrections, and improvements should be sent to
31 ;; Edward M. Reingold Department of Computer Science
32 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
33 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
34 ;; Urbana, Illinois 61801
38 (defun calendar-goto-today ()
39 "Reposition the calendar window so the current date is visible."
41 (let ((today (calendar-current-date)));; The date might have changed.
42 (if (not (calendar-date-is-visible-p today
))
43 (generate-calendar-window)
44 (update-calendar-mode-line)
45 (calendar-cursor-to-visible-date today
))))
47 (defun calendar-forward-month (arg)
48 "Move the cursor forward ARG months.
49 Movement is backward if ARG is negative."
51 (calendar-cursor-to-nearest-date)
52 (let* ((cursor-date (calendar-cursor-to-date t
))
53 (month (extract-calendar-month cursor-date
))
54 (day (extract-calendar-day cursor-date
))
55 (year (extract-calendar-year cursor-date
)))
56 (increment-calendar-month month year arg
)
57 (let ((last (calendar-last-day-of-month month year
)))
60 ;; Put the new month on the screen, if needed, and go to the new date.
61 (let ((new-cursor-date (list month day year
)))
62 (if (not (calendar-date-is-visible-p new-cursor-date
))
63 (calendar-other-month month year
))
64 (calendar-cursor-to-visible-date new-cursor-date
))))
66 (defun calendar-forward-year (arg)
67 "Move the cursor forward by ARG years.
68 Movement is backward if ARG is negative."
70 (calendar-forward-month (* 12 arg
)))
72 (defun calendar-backward-month (arg)
73 "Move the cursor backward by ARG months.
74 Movement is forward if ARG is negative."
76 (calendar-forward-month (- arg
)))
78 (defun calendar-backward-year (arg)
79 "Move the cursor backward ARG years.
80 Movement is forward is ARG is negative."
82 (calendar-forward-month (* -
12 arg
)))
84 (defun scroll-calendar-left (arg)
85 "Scroll the displayed calendar left by ARG months.
86 If ARG is negative the calendar is scrolled right. Maintains the relative
87 position of the cursor with respect to the calendar as well as possible."
89 (calendar-cursor-to-nearest-date)
90 (let ((old-date (calendar-cursor-to-date))
91 (today (calendar-current-date)))
94 (increment-calendar-month displayed-month displayed-year arg
)
95 (generate-calendar-window displayed-month displayed-year
)
96 (calendar-cursor-to-visible-date
98 ((calendar-date-is-visible-p old-date
) old-date
)
99 ((calendar-date-is-visible-p today
) today
)
100 (t (list displayed-month
1 displayed-year
))))))))
102 (defun scroll-calendar-right (arg)
103 "Scroll the displayed calendar window right by ARG months.
104 If ARG is negative the calendar is scrolled left. Maintains the relative
105 position of the cursor with respect to the calendar as well as possible."
107 (scroll-calendar-left (- arg
)))
109 (defun scroll-calendar-left-three-months (arg)
110 "Scroll the displayed calendar window left by 3*ARG months.
111 If ARG is negative the calendar is scrolled right. Maintains the relative
112 position of the cursor with respect to the calendar as well as possible."
114 (scroll-calendar-left (* 3 arg
)))
116 (defun scroll-calendar-right-three-months (arg)
117 "Scroll the displayed calendar window right by 3*ARG months.
118 If ARG is negative the calendar is scrolled left. Maintains the relative
119 position of the cursor with respect to the calendar as well as possible."
121 (scroll-calendar-left (* -
3 arg
)))
123 (defun calendar-cursor-to-nearest-date ()
124 "Move the cursor to the closest date.
125 The position of the cursor is unchanged if it is already on a date.
126 Returns the list (month day year) giving the cursor position."
127 (let ((date (calendar-cursor-to-date))
128 (column (current-column)))
131 (if (> 3 (count-lines (point-min) (point)))
134 (move-to-column column
)))
135 (if (not (looking-at "[0-9]"))
136 (if (and (not (looking-at " *$"))
143 (re-search-forward "[0-9]" nil t
)
145 (re-search-backward "[0-9]" nil t
)))
146 (calendar-cursor-to-date))))
148 (defun calendar-forward-day (arg)
149 "Move the cursor forward ARG days.
150 Moves backward if ARG is negative."
154 ((cursor-date (calendar-cursor-to-date))
155 (cursor-date (if cursor-date
157 (if (> arg
0) (setq arg
(1- arg
)))
158 (calendar-cursor-to-nearest-date)))
160 (calendar-gregorian-from-absolute
161 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
162 (new-display-month (extract-calendar-month new-cursor-date
))
163 (new-display-year (extract-calendar-year new-cursor-date
)))
164 ;; Put the new month on the screen, if needed, and go to the new date.
165 (if (not (calendar-date-is-visible-p new-cursor-date
))
166 (calendar-other-month new-display-month new-display-year
))
167 (calendar-cursor-to-visible-date new-cursor-date
))))
169 (defun calendar-backward-day (arg)
170 "Move the cursor back ARG days.
171 Moves forward if ARG is negative."
173 (calendar-forward-day (- arg
)))
175 (defun calendar-forward-week (arg)
176 "Move the cursor forward ARG weeks.
177 Moves backward if ARG is negative."
179 (calendar-forward-day (* arg
7)))
181 (defun calendar-backward-week (arg)
182 "Move the cursor back ARG weeks.
183 Moves forward if ARG is negative."
185 (calendar-forward-day (* arg -
7)))
187 (defun calendar-beginning-of-week (arg)
188 "Move the cursor back ARG calendar-week-start-day's."
190 (calendar-cursor-to-nearest-date)
191 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
192 (calendar-backward-day
193 (if (= day calendar-week-start-day
)
195 (+ (mod (- day calendar-week-start-day
) 7)
198 (defun calendar-end-of-week (arg)
199 "Move the cursor forward ARG calendar-week-start-day+6's."
201 (calendar-cursor-to-nearest-date)
202 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
203 (calendar-forward-day
204 (if (= day
(mod (1- calendar-week-start-day
) 7))
206 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
209 (defun calendar-beginning-of-month (arg)
210 "Move the cursor backward ARG month beginnings."
212 (calendar-cursor-to-nearest-date)
213 (let* ((date (calendar-cursor-to-date))
214 (month (extract-calendar-month date
))
215 (day (extract-calendar-day date
))
216 (year (extract-calendar-year date
)))
218 (calendar-backward-month arg
)
219 (calendar-cursor-to-visible-date (list month
1 year
))
220 (calendar-backward-month (1- arg
)))))
222 (defun calendar-end-of-month (arg)
223 "Move the cursor forward ARG month ends."
225 (calendar-cursor-to-nearest-date)
226 (let* ((date (calendar-cursor-to-date))
227 (month (extract-calendar-month date
))
228 (day (extract-calendar-day date
))
229 (year (extract-calendar-year date
))
230 (last-day (calendar-last-day-of-month month year
)))
231 (if (/= day last-day
)
233 (calendar-cursor-to-visible-date (list month last-day year
))
234 (setq arg
(1- arg
))))
235 (increment-calendar-month month year arg
)
236 (let ((last-day (list
238 (calendar-last-day-of-month month year
)
240 (if (not (calendar-date-is-visible-p last-day
))
241 (calendar-other-month month year
)
242 (calendar-cursor-to-visible-date last-day
)))))
244 (defun calendar-beginning-of-year (arg)
245 "Move the cursor backward ARG year beginnings."
247 (calendar-cursor-to-nearest-date)
248 (let* ((date (calendar-cursor-to-date))
249 (month (extract-calendar-month date
))
250 (day (extract-calendar-day date
))
251 (year (extract-calendar-year date
))
252 (jan-first (list 1 1 year
)))
253 (if (and (= day
1) (= 1 month
))
254 (calendar-backward-month (* 12 arg
))
256 (calendar-date-is-visible-p jan-first
))
257 (calendar-cursor-to-visible-date jan-first
)
258 (calendar-other-month 1 (- year
(1- arg
)))))))
260 (defun calendar-end-of-year (arg)
261 "Move the cursor forward ARG year beginnings."
263 (calendar-cursor-to-nearest-date)
264 (let* ((date (calendar-cursor-to-date))
265 (month (extract-calendar-month date
))
266 (day (extract-calendar-day date
))
267 (year (extract-calendar-year date
))
268 (dec-31 (list 12 31 year
)))
269 (if (and (= day
31) (= 12 month
))
270 (calendar-forward-month (* 12 arg
))
272 (calendar-date-is-visible-p dec-31
))
273 (calendar-cursor-to-visible-date dec-31
)
274 (calendar-other-month 12 (- year
(1- arg
)))
275 (calendar-cursor-to-visible-date (list 12 31 displayed-year
))))))
277 (defun calendar-cursor-to-visible-date (date)
278 "Move the cursor to DATE that is on the screen."
279 (let* ((month (extract-calendar-month date
))
280 (day (extract-calendar-day date
))
281 (year (extract-calendar-year date
))
282 (first-of-month-weekday (calendar-day-of-week (list month
1 year
))))
286 (- (calendar-day-of-week (list month
1 year
))
287 calendar-week-start-day
)
292 (1+ (calendar-interval
293 displayed-month displayed-year month year
)))
295 (- (calendar-day-of-week date
)
296 calendar-week-start-day
)
299 (defun calendar-goto-date (date)
300 "Move cursor to DATE."
301 (interactive (list (calendar-read-date)))
302 (let ((month (extract-calendar-month date
))
303 (year (extract-calendar-year date
)))
304 (if (not (calendar-date-is-visible-p date
))
305 (calendar-other-month
306 (if (and (= month
1) (= year
1))
310 (calendar-cursor-to-visible-date date
))
314 ;;; cal-move.el ends here