1 ;;; cal-move.el --- calendar functions for movement in the calendar
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Human-Keywords: calendar
11 ;; This file is part of XEmacs.
13 ;; XEmacs 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 2, or (at your option)
18 ;; XEmacs 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 XEmacs; 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 ;;; Synched up with: FSF Emacs 22 CVS 2007-03-30
32 ;; This collection of functions implements movement in the calendar for
35 ;; Comments, corrections, and improvements should be sent to
36 ;; Edward M. Reingold Department of Computer Science
37 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
38 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
39 ;; Urbana, Illinois 61801
43 (defvar displayed-month)
44 (defvar displayed-year)
48 (defun calendar-goto-today ()
49 "Reposition the calendar window so the current date is visible."
51 (let ((today (calendar-current-date)));; The date might have changed.
52 (if (not (calendar-date-is-visible-p today))
53 (generate-calendar-window)
54 (update-calendar-mode-line)
55 (calendar-cursor-to-visible-date today)))
56 (run-hooks 'calendar-move-hook))
58 (defun calendar-forward-month (arg)
59 "Move the cursor forward ARG months.
60 Movement is backward if ARG is negative."
62 (calendar-cursor-to-nearest-date)
63 (let* ((cursor-date (calendar-cursor-to-date t))
64 (month (extract-calendar-month cursor-date))
65 (day (extract-calendar-day cursor-date))
66 (year (extract-calendar-year cursor-date)))
67 (increment-calendar-month month year arg)
68 (let ((last (calendar-last-day-of-month month year)))
71 ;; Put the new month on the screen, if needed, and go to the new date.
72 (let ((new-cursor-date (list month day year)))
73 (if (not (calendar-date-is-visible-p new-cursor-date))
74 (calendar-other-month month year))
75 (calendar-cursor-to-visible-date new-cursor-date)))
76 (run-hooks 'calendar-move-hook))
78 (defun calendar-forward-year (arg)
79 "Move the cursor forward by ARG years.
80 Movement is backward if ARG is negative."
82 (calendar-forward-month (* 12 arg)))
84 (defun calendar-backward-month (arg)
85 "Move the cursor backward by ARG months.
86 Movement is forward if ARG is negative."
88 (calendar-forward-month (- arg)))
90 (defun calendar-backward-year (arg)
91 "Move the cursor backward ARG years.
92 Movement is forward is ARG is negative."
94 (calendar-forward-month (* -12 arg)))
96 (defun scroll-calendar-left (&optional arg)
97 "Scroll the displayed calendar left by ARG months.
98 If ARG is negative the calendar is scrolled right. Maintains the relative
99 position of the cursor with respect to the calendar as well as possible."
101 (unless arg (setq arg 1))
102 (calendar-cursor-to-nearest-date)
103 (let ((old-date (calendar-cursor-to-date))
104 (today (calendar-current-date)))
106 (let ((month displayed-month)
107 (year displayed-year))
108 (increment-calendar-month month year arg)
109 (generate-calendar-window month year)
110 (calendar-cursor-to-visible-date
112 ((calendar-date-is-visible-p old-date) old-date)
113 ((calendar-date-is-visible-p today) today)
114 (t (list month 1 year)))))))
115 (run-hooks 'calendar-move-hook))
117 (defun scroll-calendar-right (&optional arg)
118 "Scroll the displayed calendar window right by 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 (- (or arg 1))))
124 (defun scroll-calendar-left-three-months (arg)
125 "Scroll the displayed calendar window left by 3*ARG months.
126 If ARG is negative the calendar is scrolled right. 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 scroll-calendar-right-three-months (arg)
132 "Scroll the displayed calendar window right by 3*ARG months.
133 If ARG is negative the calendar is scrolled left. Maintains the relative
134 position of the cursor with respect to the calendar as well as possible."
136 (scroll-calendar-left (* -3 arg)))
138 (defun calendar-cursor-to-nearest-date ()
139 "Move the cursor to the closest date.
140 The position of the cursor is unchanged if it is already on a date.
141 Returns the list (month day year) giving the cursor position."
142 (let ((date (calendar-cursor-to-date))
143 (column (current-column)))
146 (if (> 3 (count-lines (point-min) (point)))
149 (move-to-column column)))
150 (if (not (looking-at "[0-9]"))
151 (if (and (not (looking-at " *$"))
158 (re-search-forward "[0-9]" nil t)
160 (re-search-backward "[0-9]" nil t)))
161 (calendar-cursor-to-date))))
163 (defun calendar-forward-day (arg)
164 "Move the cursor forward ARG days.
165 Moves backward if ARG is negative."
169 ((cursor-date (calendar-cursor-to-date))
170 (cursor-date (if cursor-date
172 (if (> arg 0) (setq arg (1- arg)))
173 (calendar-cursor-to-nearest-date)))
175 (calendar-gregorian-from-absolute
176 (+ (calendar-absolute-from-gregorian cursor-date) arg)))
177 (new-display-month (extract-calendar-month new-cursor-date))
178 (new-display-year (extract-calendar-year new-cursor-date)))
179 ;; Put the new month on the screen, if needed, and go to the new date.
180 (if (not (calendar-date-is-visible-p new-cursor-date))
181 (calendar-other-month new-display-month new-display-year))
182 (calendar-cursor-to-visible-date new-cursor-date)))
183 (run-hooks 'calendar-move-hook))
185 (defun calendar-backward-day (arg)
186 "Move the cursor back ARG days.
187 Moves forward if ARG is negative."
189 (calendar-forward-day (- arg)))
191 (defun calendar-forward-week (arg)
192 "Move the cursor forward ARG weeks.
193 Moves backward if ARG is negative."
195 (calendar-forward-day (* arg 7)))
197 (defun calendar-backward-week (arg)
198 "Move the cursor back ARG weeks.
199 Moves forward if ARG is negative."
201 (calendar-forward-day (* arg -7)))
203 (defun calendar-beginning-of-week (arg)
204 "Move the cursor back ARG calendar-week-start-day's."
206 (calendar-cursor-to-nearest-date)
207 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
208 (calendar-backward-day
209 (if (= day calendar-week-start-day)
211 (+ (mod (- day calendar-week-start-day) 7)
214 (defun calendar-end-of-week (arg)
215 "Move the cursor forward ARG calendar-week-start-day+6's."
217 (calendar-cursor-to-nearest-date)
218 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
219 (calendar-forward-day
220 (if (= day (mod (1- calendar-week-start-day) 7))
222 (+ (- 6 (mod (- day calendar-week-start-day) 7))
225 (defun calendar-beginning-of-month (arg)
226 "Move the cursor backward ARG month beginnings."
228 (calendar-cursor-to-nearest-date)
229 (let* ((date (calendar-cursor-to-date))
230 (month (extract-calendar-month date))
231 (day (extract-calendar-day date))
232 (year (extract-calendar-year date)))
234 (calendar-backward-month arg)
235 (calendar-cursor-to-visible-date (list month 1 year))
236 (calendar-backward-month (1- arg)))))
238 (defun calendar-end-of-month (arg)
239 "Move the cursor forward ARG month ends."
241 (calendar-cursor-to-nearest-date)
242 (let* ((date (calendar-cursor-to-date))
243 (month (extract-calendar-month date))
244 (day (extract-calendar-day date))
245 (year (extract-calendar-year date))
246 (last-day (calendar-last-day-of-month month year)))
247 (if (/= day last-day)
249 (calendar-cursor-to-visible-date (list month last-day year))
250 (setq arg (1- arg))))
251 (increment-calendar-month month year arg)
252 (let ((last-day (list
254 (calendar-last-day-of-month month year)
256 (if (not (calendar-date-is-visible-p last-day))
257 (calendar-other-month month year)
258 (calendar-cursor-to-visible-date last-day))))
259 (run-hooks 'calendar-move-hook))
261 (defun calendar-beginning-of-year (arg)
262 "Move the cursor backward 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 (jan-first (list 1 1 year))
270 (calendar-move-hook nil))
271 (if (and (= day 1) (= 1 month))
272 (calendar-backward-month (* 12 arg))
274 (calendar-date-is-visible-p jan-first))
275 (calendar-cursor-to-visible-date jan-first)
276 (calendar-other-month 1 (- year (1- arg)))
277 (calendar-cursor-to-visible-date (list 1 1 displayed-year)))))
278 (run-hooks 'calendar-move-hook))
280 (defun calendar-end-of-year (arg)
281 "Move the cursor forward ARG year beginnings."
283 (calendar-cursor-to-nearest-date)
284 (let* ((date (calendar-cursor-to-date))
285 (month (extract-calendar-month date))
286 (day (extract-calendar-day date))
287 (year (extract-calendar-year date))
288 (dec-31 (list 12 31 year))
289 (calendar-move-hook nil))
290 (if (and (= day 31) (= 12 month))
291 (calendar-forward-month (* 12 arg))
293 (calendar-date-is-visible-p dec-31))
294 (calendar-cursor-to-visible-date dec-31)
295 (calendar-other-month 12 (+ year (1- arg)))
296 (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
297 (run-hooks 'calendar-move-hook))
299 (defun calendar-cursor-to-visible-date (date)
300 "Move the cursor to DATE that is on the screen."
301 (let* ((month (extract-calendar-month date))
302 (day (extract-calendar-day date))
303 (year (extract-calendar-year date))
304 (first-of-month-weekday (calendar-day-of-week (list month 1 year))))
308 (- (calendar-day-of-week (list month 1 year))
309 calendar-week-start-day)
314 (1+ (calendar-interval
315 displayed-month displayed-year month year)))
317 (- (calendar-day-of-week date)
318 calendar-week-start-day)
321 (defun calendar-goto-date (date)
322 "Move cursor to DATE."
323 (interactive (list (calendar-read-date)))
324 (let ((month (extract-calendar-month date))
325 (year (extract-calendar-year date)))
326 (if (not (calendar-date-is-visible-p date))
327 (calendar-other-month
328 (if (and (= month 1) (= year 1))
332 (calendar-cursor-to-visible-date date)
333 (run-hooks 'calendar-move-hook))
335 (defun calendar-goto-day-of-year (year day &optional noecho)
336 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is t.
337 Negative DAY counts backward from end of year."
339 (let* ((year (calendar-read
342 (int-to-string (extract-calendar-year
343 (calendar-current-date)))))
344 (last (if (calendar-leap-year-p year) 366 365))
346 (format "Day number (+/- 1-%d): " last)
347 '(lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
350 (calendar-gregorian-from-absolute
352 (+ -1 day (calendar-absolute-from-gregorian (list 1 1 year)))
353 (+ 1 day (calendar-absolute-from-gregorian (list 12 31 year))))))
354 (or noecho (calendar-print-day-of-year)))
358 ;;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
359 ;;; cal-move.el ends here