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
41 (defun calendar-goto-today ()
42 "Reposition the calendar window so the current date is visible."
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 (defun calendar-forward-month (arg)
52 "Move the cursor forward ARG months.
53 Movement is backward if ARG is negative."
55 (calendar-cursor-to-nearest-date)
56 (let* ((cursor-date (calendar-cursor-to-date t
))
57 (month (extract-calendar-month cursor-date
))
58 (day (extract-calendar-day cursor-date
))
59 (year (extract-calendar-year cursor-date
)))
60 (increment-calendar-month month year arg
)
61 (let ((last (calendar-last-day-of-month month year
)))
64 ;; Put the new month on the screen, if needed, and go to the new date.
65 (let ((new-cursor-date (list month day year
)))
66 (if (not (calendar-date-is-visible-p new-cursor-date
))
67 (calendar-other-month month year
))
68 (calendar-cursor-to-visible-date new-cursor-date
)))
69 (run-hooks 'calendar-move-hook
))
71 (defun calendar-forward-year (arg)
72 "Move the cursor forward by ARG years.
73 Movement is backward if ARG is negative."
75 (calendar-forward-month (* 12 arg
)))
77 (defun calendar-backward-month (arg)
78 "Move the cursor backward by ARG months.
79 Movement is forward if ARG is negative."
81 (calendar-forward-month (- arg
)))
83 (defun calendar-backward-year (arg)
84 "Move the cursor backward ARG years.
85 Movement is forward is ARG is negative."
87 (calendar-forward-month (* -
12 arg
)))
89 (defun scroll-calendar-left (&optional arg
)
90 "Scroll the displayed calendar left by ARG months.
91 If ARG is negative the calendar is scrolled right. Maintains the relative
92 position of the cursor with respect to the calendar as well as possible."
94 (unless arg
(setq arg
1))
95 (calendar-cursor-to-nearest-date)
96 (let ((old-date (calendar-cursor-to-date))
97 (today (calendar-current-date)))
99 (let ((month displayed-month
)
100 (year displayed-year
))
101 (increment-calendar-month month year arg
)
102 (generate-calendar-window month year
)
103 (calendar-cursor-to-visible-date
105 ((calendar-date-is-visible-p old-date
) old-date
)
106 ((calendar-date-is-visible-p today
) today
)
107 (t (list month
1 year
)))))))
108 (run-hooks 'calendar-move-hook
))
110 (defun scroll-calendar-right (&optional arg
)
111 "Scroll the displayed calendar window right by ARG months.
112 If ARG is negative the calendar is scrolled left. Maintains the relative
113 position of the cursor with respect to the calendar as well as possible."
115 (scroll-calendar-left (- (or arg
1))))
117 (defun scroll-calendar-left-three-months (arg)
118 "Scroll the displayed calendar window left by 3*ARG months.
119 If ARG is negative the calendar is scrolled right. 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 scroll-calendar-right-three-months (arg)
125 "Scroll the displayed calendar window right by 3*ARG months.
126 If ARG is negative the calendar is scrolled left. Maintains the relative
127 position of the cursor with respect to the calendar as well as possible."
129 (scroll-calendar-left (* -
3 arg
)))
131 (defun calendar-cursor-to-nearest-date ()
132 "Move the cursor to the closest date.
133 The position of the cursor is unchanged if it is already on a date.
134 Returns the list (month day year) giving the cursor position."
135 (let ((date (calendar-cursor-to-date))
136 (column (current-column)))
139 (if (> 3 (count-lines (point-min) (point)))
142 (move-to-column column
)))
143 (if (not (looking-at "[0-9]"))
144 (if (and (not (looking-at " *$"))
151 (re-search-forward "[0-9]" nil t
)
153 (re-search-backward "[0-9]" nil t
)))
154 (calendar-cursor-to-date))))
156 (defun calendar-forward-day (arg)
157 "Move the cursor forward ARG days.
158 Moves backward if ARG is negative."
162 ((cursor-date (calendar-cursor-to-date))
163 (cursor-date (if cursor-date
165 (if (> arg
0) (setq arg
(1- arg
)))
166 (calendar-cursor-to-nearest-date)))
168 (calendar-gregorian-from-absolute
169 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
170 (new-display-month (extract-calendar-month new-cursor-date
))
171 (new-display-year (extract-calendar-year new-cursor-date
)))
172 ;; Put the new month on the screen, if needed, and go to the new date.
173 (if (not (calendar-date-is-visible-p new-cursor-date
))
174 (calendar-other-month new-display-month new-display-year
))
175 (calendar-cursor-to-visible-date new-cursor-date
)))
176 (run-hooks 'calendar-move-hook
))
178 (defun calendar-backward-day (arg)
179 "Move the cursor back ARG days.
180 Moves forward if ARG is negative."
182 (calendar-forward-day (- arg
)))
184 (defun calendar-forward-week (arg)
185 "Move the cursor forward ARG weeks.
186 Moves backward if ARG is negative."
188 (calendar-forward-day (* arg
7)))
190 (defun calendar-backward-week (arg)
191 "Move the cursor back ARG weeks.
192 Moves forward if ARG is negative."
194 (calendar-forward-day (* arg -
7)))
196 (defun calendar-beginning-of-week (arg)
197 "Move the cursor back ARG calendar-week-start-day's."
199 (calendar-cursor-to-nearest-date)
200 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
201 (calendar-backward-day
202 (if (= day calendar-week-start-day
)
204 (+ (mod (- day calendar-week-start-day
) 7)
207 (defun calendar-end-of-week (arg)
208 "Move the cursor forward ARG calendar-week-start-day+6's."
210 (calendar-cursor-to-nearest-date)
211 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
212 (calendar-forward-day
213 (if (= day
(mod (1- calendar-week-start-day
) 7))
215 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
218 (defun calendar-beginning-of-month (arg)
219 "Move the cursor backward ARG month beginnings."
221 (calendar-cursor-to-nearest-date)
222 (let* ((date (calendar-cursor-to-date))
223 (month (extract-calendar-month date
))
224 (day (extract-calendar-day date
))
225 (year (extract-calendar-year date
)))
227 (calendar-backward-month arg
)
228 (calendar-cursor-to-visible-date (list month
1 year
))
229 (calendar-backward-month (1- arg
)))))
231 (defun calendar-end-of-month (arg)
232 "Move the cursor forward ARG month ends."
234 (calendar-cursor-to-nearest-date)
235 (let* ((date (calendar-cursor-to-date))
236 (month (extract-calendar-month date
))
237 (day (extract-calendar-day date
))
238 (year (extract-calendar-year date
))
239 (last-day (calendar-last-day-of-month month year
)))
240 (if (/= day last-day
)
242 (calendar-cursor-to-visible-date (list month last-day year
))
243 (setq arg
(1- arg
))))
244 (increment-calendar-month month year arg
)
245 (let ((last-day (list
247 (calendar-last-day-of-month month year
)
249 (if (not (calendar-date-is-visible-p last-day
))
250 (calendar-other-month month year
)
251 (calendar-cursor-to-visible-date last-day
))))
252 (run-hooks 'calendar-move-hook
))
254 (defun calendar-beginning-of-year (arg)
255 "Move the cursor backward ARG year beginnings."
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 (jan-first (list 1 1 year
))
263 (calendar-move-hook nil
))
264 (if (and (= day
1) (= 1 month
))
265 (calendar-backward-month (* 12 arg
))
267 (calendar-date-is-visible-p jan-first
))
268 (calendar-cursor-to-visible-date jan-first
)
269 (calendar-other-month 1 (- year
(1- arg
))))))
270 (run-hooks 'calendar-move-hook
))
272 (defun calendar-end-of-year (arg)
273 "Move the cursor forward ARG year beginnings."
275 (calendar-cursor-to-nearest-date)
276 (let* ((date (calendar-cursor-to-date))
277 (month (extract-calendar-month date
))
278 (day (extract-calendar-day date
))
279 (year (extract-calendar-year date
))
280 (dec-31 (list 12 31 year
))
281 (calendar-move-hook nil
))
282 (if (and (= day
31) (= 12 month
))
283 (calendar-forward-month (* 12 arg
))
285 (calendar-date-is-visible-p dec-31
))
286 (calendar-cursor-to-visible-date dec-31
)
287 (calendar-other-month 12 (- year
(1- arg
)))
288 (calendar-cursor-to-visible-date (list 12 31 displayed-year
)))))
289 (run-hooks 'calendar-move-hook
))
291 (defun calendar-cursor-to-visible-date (date)
292 "Move the cursor to DATE that is on the screen."
293 (let* ((month (extract-calendar-month date
))
294 (day (extract-calendar-day date
))
295 (year (extract-calendar-year date
))
296 (first-of-month-weekday (calendar-day-of-week (list month
1 year
))))
300 (- (calendar-day-of-week (list month
1 year
))
301 calendar-week-start-day
)
306 (1+ (calendar-interval
307 displayed-month displayed-year month year
)))
309 (- (calendar-day-of-week date
)
310 calendar-week-start-day
)
313 (defun calendar-goto-date (date)
314 "Move cursor to DATE."
315 (interactive (list (calendar-read-date)))
316 (let ((month (extract-calendar-month date
))
317 (year (extract-calendar-year date
)))
318 (if (not (calendar-date-is-visible-p date
))
319 (calendar-other-month
320 (if (and (= month
1) (= year
1))
324 (calendar-cursor-to-visible-date date
)
325 (run-hooks 'calendar-move-hook
))
329 ;;; cal-move.el ends here