1 ;;; cal-move.el --- calendar functions for movement in the calendar
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 GNU Emacs.
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)
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.
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.
30 ;; This collection of functions implements movement in the calendar for
35 (defvar displayed-month
)
36 (defvar displayed-year
)
40 (defun calendar-goto-today ()
41 "Reposition the calendar window so the current date is visible."
43 (let ((today (calendar-current-date)));; The date might have changed.
44 (if (not (calendar-date-is-visible-p today
))
45 (generate-calendar-window)
46 (update-calendar-mode-line)
47 (calendar-cursor-to-visible-date today
)))
48 (run-hooks 'calendar-move-hook
))
50 (defun calendar-forward-month (arg)
51 "Move the cursor forward ARG months.
52 Movement is backward if ARG is negative."
54 (calendar-cursor-to-nearest-date)
55 (let* ((cursor-date (calendar-cursor-to-date t
))
56 (month (extract-calendar-month cursor-date
))
57 (day (extract-calendar-day cursor-date
))
58 (year (extract-calendar-year cursor-date
)))
59 (increment-calendar-month month year arg
)
60 (let ((last (calendar-last-day-of-month month year
)))
63 ;; Put the new month on the screen, if needed, and go to the new date.
64 (let ((new-cursor-date (list month day year
)))
65 (if (not (calendar-date-is-visible-p new-cursor-date
))
66 (calendar-other-month month year
))
67 (calendar-cursor-to-visible-date new-cursor-date
)))
68 (run-hooks 'calendar-move-hook
))
70 (defun calendar-forward-year (arg)
71 "Move the cursor forward by ARG years.
72 Movement is backward if ARG is negative."
74 (calendar-forward-month (* 12 arg
)))
76 (defun calendar-backward-month (arg)
77 "Move the cursor backward by ARG months.
78 Movement is forward if ARG is negative."
80 (calendar-forward-month (- arg
)))
82 (defun calendar-backward-year (arg)
83 "Move the cursor backward ARG years.
84 Movement is forward is ARG is negative."
86 (calendar-forward-month (* -
12 arg
)))
88 (defun scroll-calendar-left (&optional arg
)
89 "Scroll the displayed calendar left by ARG months.
90 If ARG is negative the calendar is scrolled right. Maintains the relative
91 position of the cursor with respect to the calendar as well as possible."
93 (unless arg
(setq arg
1))
94 (calendar-cursor-to-nearest-date)
95 (let ((old-date (calendar-cursor-to-date))
96 (today (calendar-current-date)))
98 (let ((month displayed-month
)
99 (year displayed-year
))
100 (increment-calendar-month month year arg
)
101 (generate-calendar-window month year
)
102 (calendar-cursor-to-visible-date
104 ((calendar-date-is-visible-p old-date
) old-date
)
105 ((calendar-date-is-visible-p today
) today
)
106 (t (list month
1 year
)))))))
107 (run-hooks 'calendar-move-hook
))
109 (defun scroll-calendar-right (&optional arg
)
110 "Scroll the displayed calendar window right by ARG months.
111 If ARG is negative the calendar is scrolled left. Maintains the relative
112 position of the cursor with respect to the calendar as well as possible."
114 (scroll-calendar-left (- (or arg
1))))
116 (defun scroll-calendar-left-three-months (arg)
117 "Scroll the displayed calendar window left by 3*ARG months.
118 If ARG is negative the calendar is scrolled right. 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 scroll-calendar-right-three-months (arg)
124 "Scroll the displayed calendar window right by 3*ARG months.
125 If ARG is negative the calendar is scrolled left. Maintains the relative
126 position of the cursor with respect to the calendar as well as possible."
128 (scroll-calendar-left (* -
3 arg
)))
130 (defun calendar-cursor-to-nearest-date ()
131 "Move the cursor to the closest date.
132 The position of the cursor is unchanged if it is already on a date.
133 Returns the list (month day year) giving the cursor position."
134 (let ((date (calendar-cursor-to-date))
135 (column (current-column)))
138 (if (> 3 (count-lines (point-min) (point)))
141 (move-to-column column
)))
142 (if (not (looking-at "[0-9]"))
143 (if (and (not (looking-at " *$"))
150 (re-search-forward "[0-9]" nil t
)
152 (re-search-backward "[0-9]" nil t
)))
153 (calendar-cursor-to-date))))
155 (defun calendar-forward-day (arg)
156 "Move the cursor forward ARG days.
157 Moves backward if ARG is negative."
161 ((cursor-date (calendar-cursor-to-date))
162 (cursor-date (if cursor-date
164 (if (> arg
0) (setq arg
(1- arg
)))
165 (calendar-cursor-to-nearest-date)))
167 (calendar-gregorian-from-absolute
168 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
169 (new-display-month (extract-calendar-month new-cursor-date
))
170 (new-display-year (extract-calendar-year new-cursor-date
)))
171 ;; Put the new month on the screen, if needed, and go to the new date.
172 (if (not (calendar-date-is-visible-p new-cursor-date
))
173 (calendar-other-month new-display-month new-display-year
))
174 (calendar-cursor-to-visible-date new-cursor-date
)))
175 (run-hooks 'calendar-move-hook
))
177 (defun calendar-backward-day (arg)
178 "Move the cursor back ARG days.
179 Moves forward if ARG is negative."
181 (calendar-forward-day (- arg
)))
183 (defun calendar-forward-week (arg)
184 "Move the cursor forward ARG weeks.
185 Moves backward if ARG is negative."
187 (calendar-forward-day (* arg
7)))
189 (defun calendar-backward-week (arg)
190 "Move the cursor back ARG weeks.
191 Moves forward if ARG is negative."
193 (calendar-forward-day (* arg -
7)))
195 (defun calendar-beginning-of-week (arg)
196 "Move the cursor back ARG calendar-week-start-day's."
198 (calendar-cursor-to-nearest-date)
199 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
200 (calendar-backward-day
201 (if (= day calendar-week-start-day
)
203 (+ (mod (- day calendar-week-start-day
) 7)
206 (defun calendar-end-of-week (arg)
207 "Move the cursor forward ARG calendar-week-start-day+6's."
209 (calendar-cursor-to-nearest-date)
210 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
211 (calendar-forward-day
212 (if (= day
(mod (1- calendar-week-start-day
) 7))
214 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
217 (defun calendar-beginning-of-month (arg)
218 "Move the cursor backward ARG month beginnings."
220 (calendar-cursor-to-nearest-date)
221 (let* ((date (calendar-cursor-to-date))
222 (month (extract-calendar-month date
))
223 (day (extract-calendar-day date
))
224 (year (extract-calendar-year date
)))
226 (calendar-backward-month arg
)
227 (calendar-cursor-to-visible-date (list month
1 year
))
228 (calendar-backward-month (1- arg
)))))
230 (defun calendar-end-of-month (arg)
231 "Move the cursor forward ARG month ends."
233 (calendar-cursor-to-nearest-date)
234 (let* ((date (calendar-cursor-to-date))
235 (month (extract-calendar-month date
))
236 (day (extract-calendar-day date
))
237 (year (extract-calendar-year date
))
238 (last-day (calendar-last-day-of-month month year
)))
239 (if (/= day last-day
)
241 (calendar-cursor-to-visible-date (list month last-day year
))
242 (setq arg
(1- arg
))))
243 (increment-calendar-month month year arg
)
244 (let ((last-day (list
246 (calendar-last-day-of-month month year
)
248 (if (not (calendar-date-is-visible-p last-day
))
249 (calendar-other-month month year
)
250 (calendar-cursor-to-visible-date last-day
))))
251 (run-hooks 'calendar-move-hook
))
253 (defun calendar-beginning-of-year (arg)
254 "Move the cursor backward ARG year beginnings."
256 (calendar-cursor-to-nearest-date)
257 (let* ((date (calendar-cursor-to-date))
258 (month (extract-calendar-month date
))
259 (day (extract-calendar-day date
))
260 (year (extract-calendar-year date
))
261 (jan-first (list 1 1 year
))
262 (calendar-move-hook nil
))
263 (if (and (= day
1) (= 1 month
))
264 (calendar-backward-month (* 12 arg
))
266 (calendar-date-is-visible-p jan-first
))
267 (calendar-cursor-to-visible-date jan-first
)
268 (calendar-other-month 1 (- year
(1- arg
)))
269 (calendar-cursor-to-visible-date (list 1 1 displayed-year
)))))
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
))
327 (defun calendar-goto-day-of-year (year day
&optional noecho
)
328 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is t.
329 Negative DAY counts backward from end of year."
331 (let* ((year (calendar-read
334 (int-to-string (extract-calendar-year
335 (calendar-current-date)))))
336 (last (if (calendar-leap-year-p year
) 366 365))
338 (format "Day number (+/- 1-%d): " last
)
339 '(lambda (x) (and (<= 1 (abs x
)) (<= (abs x
) last
))))))
342 (calendar-gregorian-from-absolute
344 (+ -
1 day
(calendar-absolute-from-gregorian (list 1 1 year
)))
345 (+ 1 day
(calendar-absolute-from-gregorian (list 12 31 year
))))))
346 (or noecho
(calendar-print-day-of-year)))
350 ;;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
351 ;;; cal-move.el ends here