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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This collection of functions implements movement in the calendar for
31 ;; Comments, corrections, and improvements should be sent to
32 ;; Edward M. Reingold Department of Computer Science
33 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
34 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
35 ;; Urbana, Illinois 61801
39 (defun calendar-goto-today ()
40 "Reposition the calendar window so the current date is visible."
42 (let ((today (calendar-current-date)));; The date might have changed.
43 (if (not (calendar-date-is-visible-p today
))
44 (generate-calendar-window)
45 (update-calendar-mode-line)
46 (calendar-cursor-to-visible-date today
))))
48 (defun calendar-forward-month (arg)
49 "Move the cursor forward ARG months.
50 Movement is backward if ARG is negative."
52 (calendar-cursor-to-nearest-date)
53 (let* ((cursor-date (calendar-cursor-to-date t
))
54 (month (extract-calendar-month cursor-date
))
55 (day (extract-calendar-day cursor-date
))
56 (year (extract-calendar-year cursor-date
)))
57 (increment-calendar-month month year arg
)
58 (let ((last (calendar-last-day-of-month month year
)))
61 ;; Put the new month on the screen, if needed, and go to the new date.
62 (let ((new-cursor-date (list month day year
)))
63 (if (not (calendar-date-is-visible-p new-cursor-date
))
64 (calendar-other-month month year
))
65 (calendar-cursor-to-visible-date new-cursor-date
))))
67 (defun calendar-forward-year (arg)
68 "Move the cursor forward by ARG years.
69 Movement is backward if ARG is negative."
71 (calendar-forward-month (* 12 arg
)))
73 (defun calendar-backward-month (arg)
74 "Move the cursor backward by ARG months.
75 Movement is forward if ARG is negative."
77 (calendar-forward-month (- arg
)))
79 (defun calendar-backward-year (arg)
80 "Move the cursor backward ARG years.
81 Movement is forward is ARG is negative."
83 (calendar-forward-month (* -
12 arg
)))
85 (defun scroll-calendar-left (arg)
86 "Scroll the displayed calendar left by ARG months.
87 If ARG is negative the calendar is scrolled right. Maintains the relative
88 position of the cursor with respect to the calendar as well as possible."
90 (calendar-cursor-to-nearest-date)
91 (let ((old-date (calendar-cursor-to-date))
92 (today (calendar-current-date)))
95 (increment-calendar-month displayed-month displayed-year arg
)
96 (generate-calendar-window displayed-month displayed-year
)
97 (calendar-cursor-to-visible-date
99 ((calendar-date-is-visible-p old-date
) old-date
)
100 ((calendar-date-is-visible-p today
) today
)
101 (t (list displayed-month
1 displayed-year
))))))))
103 (defun scroll-calendar-right (arg)
104 "Scroll the displayed calendar window right by ARG months.
105 If ARG is negative the calendar is scrolled left. Maintains the relative
106 position of the cursor with respect to the calendar as well as possible."
108 (scroll-calendar-left (- arg
)))
110 (defun scroll-calendar-left-three-months (arg)
111 "Scroll the displayed calendar window left by 3*ARG months.
112 If ARG is negative the calendar is scrolled right. Maintains the relative
113 position of the cursor with respect to the calendar as well as possible."
115 (scroll-calendar-left (* 3 arg
)))
117 (defun scroll-calendar-right-three-months (arg)
118 "Scroll the displayed calendar window right by 3*ARG months.
119 If ARG is negative the calendar is scrolled left. Maintains the relative
120 position of the cursor with respect to the calendar as well as possible."
122 (scroll-calendar-left (* -
3 arg
)))
124 (defun calendar-cursor-to-nearest-date ()
125 "Move the cursor to the closest date.
126 The position of the cursor is unchanged if it is already on a date.
127 Returns the list (month day year) giving the cursor position."
128 (let ((date (calendar-cursor-to-date))
129 (column (current-column)))
132 (if (> 3 (count-lines (point-min) (point)))
135 (move-to-column column
)))
136 (if (not (looking-at "[0-9]"))
137 (if (and (not (looking-at " *$"))
144 (re-search-forward "[0-9]" nil t
)
146 (re-search-backward "[0-9]" nil t
)))
147 (calendar-cursor-to-date))))
149 (defun calendar-forward-day (arg)
150 "Move the cursor forward ARG days.
151 Moves backward if ARG is negative."
155 ((cursor-date (calendar-cursor-to-date))
156 (cursor-date (if cursor-date
158 (if (> arg
0) (setq arg
(1- arg
)))
159 (calendar-cursor-to-nearest-date)))
161 (calendar-gregorian-from-absolute
162 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
163 (new-display-month (extract-calendar-month new-cursor-date
))
164 (new-display-year (extract-calendar-year new-cursor-date
)))
165 ;; Put the new month on the screen, if needed, and go to the new date.
166 (if (not (calendar-date-is-visible-p new-cursor-date
))
167 (calendar-other-month new-display-month new-display-year
))
168 (calendar-cursor-to-visible-date new-cursor-date
))))
170 (defun calendar-backward-day (arg)
171 "Move the cursor back ARG days.
172 Moves forward if ARG is negative."
174 (calendar-forward-day (- arg
)))
176 (defun calendar-forward-week (arg)
177 "Move the cursor forward ARG weeks.
178 Moves backward if ARG is negative."
180 (calendar-forward-day (* arg
7)))
182 (defun calendar-backward-week (arg)
183 "Move the cursor back ARG weeks.
184 Moves forward if ARG is negative."
186 (calendar-forward-day (* arg -
7)))
188 (defun calendar-beginning-of-week (arg)
189 "Move the cursor back ARG calendar-week-start-day's."
191 (calendar-cursor-to-nearest-date)
192 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
193 (calendar-backward-day
194 (if (= day calendar-week-start-day
)
196 (+ (mod (- day calendar-week-start-day
) 7)
199 (defun calendar-end-of-week (arg)
200 "Move the cursor forward ARG calendar-week-start-day+6's."
202 (calendar-cursor-to-nearest-date)
203 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
204 (calendar-forward-day
205 (if (= day
(mod (1- calendar-week-start-day
) 7))
207 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
210 (defun calendar-beginning-of-month (arg)
211 "Move the cursor backward ARG month beginnings."
213 (calendar-cursor-to-nearest-date)
214 (let* ((date (calendar-cursor-to-date))
215 (month (extract-calendar-month date
))
216 (day (extract-calendar-day date
))
217 (year (extract-calendar-year date
)))
219 (calendar-backward-month arg
)
220 (calendar-cursor-to-visible-date (list month
1 year
))
221 (calendar-backward-month (1- arg
)))))
223 (defun calendar-end-of-month (arg)
224 "Move the cursor forward ARG month ends."
226 (calendar-cursor-to-nearest-date)
227 (let* ((date (calendar-cursor-to-date))
228 (month (extract-calendar-month date
))
229 (day (extract-calendar-day date
))
230 (year (extract-calendar-year date
))
231 (last-day (calendar-last-day-of-month month year
)))
232 (if (/= day last-day
)
234 (calendar-cursor-to-visible-date (list month last-day year
))
235 (setq arg
(1- arg
))))
236 (increment-calendar-month month year arg
)
237 (let ((last-day (list
239 (calendar-last-day-of-month month year
)
241 (if (not (calendar-date-is-visible-p last-day
))
242 (calendar-other-month month year
)
243 (calendar-cursor-to-visible-date last-day
)))))
245 (defun calendar-beginning-of-year (arg)
246 "Move the cursor backward ARG year beginnings."
248 (calendar-cursor-to-nearest-date)
249 (let* ((date (calendar-cursor-to-date))
250 (month (extract-calendar-month date
))
251 (day (extract-calendar-day date
))
252 (year (extract-calendar-year date
))
253 (jan-first (list 1 1 year
)))
254 (if (and (= day
1) (= 1 month
))
255 (calendar-backward-month (* 12 arg
))
257 (calendar-date-is-visible-p jan-first
))
258 (calendar-cursor-to-visible-date jan-first
)
259 (calendar-other-month 1 (- year
(1- arg
)))))))
261 (defun calendar-end-of-year (arg)
262 "Move the cursor forward ARG year beginnings."
264 (calendar-cursor-to-nearest-date)
265 (let* ((date (calendar-cursor-to-date))
266 (month (extract-calendar-month date
))
267 (day (extract-calendar-day date
))
268 (year (extract-calendar-year date
))
269 (dec-31 (list 12 31 year
)))
270 (if (and (= day
31) (= 12 month
))
271 (calendar-forward-month (* 12 arg
))
273 (calendar-date-is-visible-p dec-31
))
274 (calendar-cursor-to-visible-date dec-31
)
275 (calendar-other-month 12 (- year
(1- arg
)))
276 (calendar-cursor-to-visible-date (list 12 31 displayed-year
))))))
278 (defun calendar-cursor-to-visible-date (date)
279 "Move the cursor to DATE that is on the screen."
280 (let* ((month (extract-calendar-month date
))
281 (day (extract-calendar-day date
))
282 (year (extract-calendar-year date
))
283 (first-of-month-weekday (calendar-day-of-week (list month
1 year
))))
287 (- (calendar-day-of-week (list month
1 year
))
288 calendar-week-start-day
)
293 (1+ (calendar-interval
294 displayed-month displayed-year month year
)))
296 (- (calendar-day-of-week date
)
297 calendar-week-start-day
)
300 (defun calendar-goto-date (date)
301 "Move cursor to DATE."
302 (interactive (list (calendar-read-date)))
303 (let ((month (extract-calendar-month date
))
304 (year (extract-calendar-year date
)))
305 (if (not (calendar-date-is-visible-p date
))
306 (calendar-other-month
307 (if (and (= month
1) (= year
1))
311 (calendar-cursor-to-visible-date date
))
315 ;;; cal-move.el ends here