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.
37 (defun calendar-cursor-to-nearest-date ()
38 "Move the cursor to the closest date.
39 The position of the cursor is unchanged if it is already on a date.
40 Returns the list (month day year) giving the cursor position."
41 (or (calendar-cursor-to-date)
42 (let ((column (current-column)))
43 (when (> 3 (count-lines (point-min) (point)))
45 (move-to-column column
))
46 (if (not (looking-at "[0-9]"))
47 (if (and (not (looking-at " *$"))
54 (re-search-forward "[0-9]" nil t
)
56 (re-search-backward "[0-9]" nil t
)))
57 (calendar-cursor-to-date))))
59 (defvar displayed-month
) ; from calendar-generate
60 (defvar displayed-year
)
63 (defun calendar-cursor-to-visible-date (date)
64 "Move the cursor to DATE that is on the screen."
65 (let ((month (calendar-extract-month date
))
66 (day (calendar-extract-day date
))
67 (year (calendar-extract-year date
)))
71 (- (calendar-day-of-week (list month
1 year
))
72 calendar-week-start-day
)
77 (1+ (calendar-interval
78 displayed-month displayed-year month year
)))
80 (- (calendar-day-of-week date
)
81 calendar-week-start-day
)
85 (defun calendar-goto-today ()
86 "Reposition the calendar window so the current date is visible."
88 (let ((today (calendar-current-date))) ; the date might have changed
89 (if (not (calendar-date-is-visible-p today
))
90 (calendar-generate-window)
91 (calendar-update-mode-line)
92 (calendar-cursor-to-visible-date today
)))
93 (run-hooks 'calendar-move-hook
))
96 (defun calendar-forward-month (arg)
97 "Move the cursor forward ARG months.
98 Movement is backward if ARG is negative."
100 (calendar-cursor-to-nearest-date)
101 (let* ((cursor-date (calendar-cursor-to-date t
))
102 (month (calendar-extract-month cursor-date
))
103 (day (calendar-extract-day cursor-date
))
104 (year (calendar-extract-year cursor-date
))
106 (calendar-increment-month month year arg
)
107 (calendar-last-day-of-month month year
)))
109 ;; Put the new month on the screen, if needed, and go to the new date.
110 (new-cursor-date (list month day year
)))
111 (if (not (calendar-date-is-visible-p new-cursor-date
))
112 (calendar-other-month month year
))
113 (calendar-cursor-to-visible-date new-cursor-date
))
114 (run-hooks 'calendar-move-hook
))
117 (defun calendar-forward-year (arg)
118 "Move the cursor forward by ARG years.
119 Movement is backward if ARG is negative."
121 (calendar-forward-month (* 12 arg
)))
124 (defun calendar-backward-month (arg)
125 "Move the cursor backward by ARG months.
126 Movement is forward if ARG is negative."
128 (calendar-forward-month (- arg
)))
131 (defun calendar-backward-year (arg)
132 "Move the cursor backward ARG years.
133 Movement is forward is ARG is negative."
135 (calendar-forward-month (* -
12 arg
)))
138 (defun calendar-scroll-left (&optional arg event
)
139 "Scroll the displayed calendar left by ARG months.
140 If ARG is negative the calendar is scrolled right. Maintains the relative
141 position of the cursor with respect to the calendar as well as possible.
142 EVENT is an event like `last-nonmenu-event'."
143 (interactive (list (prefix-numeric-value current-prefix-arg
)
145 (unless arg
(setq arg
1))
146 (save-selected-window
147 (select-window (posn-window (event-start event
)))
148 (calendar-cursor-to-nearest-date)
150 (let ((old-date (calendar-cursor-to-date))
151 (today (calendar-current-date))
152 (month displayed-month
)
153 (year displayed-year
))
154 (calendar-increment-month month year arg
)
155 (calendar-generate-window month year
)
156 (calendar-cursor-to-visible-date
158 ((calendar-date-is-visible-p old-date
) old-date
)
159 ((calendar-date-is-visible-p today
) today
)
160 (t (list month
1 year
))))))
161 (run-hooks 'calendar-move-hook
)))
163 (define-obsolete-function-alias
164 'scroll-calendar-left
'calendar-scroll-left
"23.1")
167 (defun calendar-scroll-right (&optional arg event
)
168 "Scroll the displayed calendar window right by ARG months.
169 If ARG is negative the calendar is scrolled left. Maintains the relative
170 position of the cursor with respect to the calendar as well as possible.
171 EVENT is an event like `last-nonmenu-event'."
172 (interactive (list (prefix-numeric-value current-prefix-arg
)
174 (calendar-scroll-left (- (or arg
1)) event
))
176 (define-obsolete-function-alias
177 'scroll-calendar-right
'calendar-scroll-right
"23.1")
180 (defun calendar-scroll-left-three-months (arg)
181 "Scroll the displayed calendar window left by 3*ARG months.
182 If ARG is negative the calendar is scrolled right. Maintains the relative
183 position of the cursor with respect to the calendar as well as possible."
185 (calendar-scroll-left (* 3 arg
)))
187 (define-obsolete-function-alias 'scroll-calendar-left-three-months
188 'calendar-scroll-left-three-months
"23.1")
191 (defun calendar-scroll-right-three-months (arg)
192 "Scroll the displayed calendar window right by 3*ARG months.
193 If ARG is negative the calendar is scrolled left. Maintains the relative
194 position of the cursor with respect to the calendar as well as possible."
196 (calendar-scroll-left (* -
3 arg
)))
198 (define-obsolete-function-alias 'scroll-calendar-right-three-months
199 'calendar-scroll-right-three-months
"23.1")
202 (defun calendar-forward-day (arg)
203 "Move the cursor forward ARG days.
204 Moves backward if ARG is negative."
207 (let* ((cursor-date (or (calendar-cursor-to-date)
209 (if (> arg
0) (setq arg
(1- arg
)))
210 (calendar-cursor-to-nearest-date))))
212 (calendar-gregorian-from-absolute
213 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
214 (new-display-month (calendar-extract-month new-cursor-date
))
215 (new-display-year (calendar-extract-year new-cursor-date
)))
216 ;; Put the new month on the screen, if needed, and go to the new date.
217 (if (not (calendar-date-is-visible-p new-cursor-date
))
218 (calendar-other-month new-display-month new-display-year
))
219 (calendar-cursor-to-visible-date new-cursor-date
)))
220 (run-hooks 'calendar-move-hook
))
223 (defun calendar-backward-day (arg)
224 "Move the cursor back ARG days.
225 Moves forward if ARG is negative."
227 (calendar-forward-day (- arg
)))
230 (defun calendar-forward-week (arg)
231 "Move the cursor forward ARG weeks.
232 Moves backward if ARG is negative."
234 (calendar-forward-day (* arg
7)))
237 (defun calendar-backward-week (arg)
238 "Move the cursor back ARG weeks.
239 Moves forward if ARG is negative."
241 (calendar-forward-day (* arg -
7)))
244 (defun calendar-beginning-of-week (arg)
245 "Move the cursor back ARG calendar-week-start-day's."
247 (calendar-cursor-to-nearest-date)
248 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
249 (calendar-backward-day
250 (if (= day calendar-week-start-day
)
252 (+ (mod (- day calendar-week-start-day
) 7)
256 (defun calendar-end-of-week (arg)
257 "Move the cursor forward ARG calendar-week-start-day+6's."
259 (calendar-cursor-to-nearest-date)
260 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
261 (calendar-forward-day
262 (if (= day
(mod (1- calendar-week-start-day
) 7))
264 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
268 (defun calendar-beginning-of-month (arg)
269 "Move the cursor backward ARG month beginnings."
271 (calendar-cursor-to-nearest-date)
272 (let* ((date (calendar-cursor-to-date))
273 (month (calendar-extract-month date
))
274 (day (calendar-extract-day date
))
275 (year (calendar-extract-year date
)))
277 (calendar-backward-month arg
)
278 (calendar-cursor-to-visible-date (list month
1 year
))
279 (calendar-backward-month (1- arg
)))))
282 (defun calendar-end-of-month (arg)
283 "Move the cursor forward ARG month ends."
285 (calendar-cursor-to-nearest-date)
286 (let* ((date (calendar-cursor-to-date))
287 (month (calendar-extract-month date
))
288 (day (calendar-extract-day date
))
289 (year (calendar-extract-year date
))
290 (last-day (calendar-last-day-of-month month year
))
292 (unless (= day last-day
)
293 (calendar-cursor-to-visible-date
294 (list month last-day year
))
296 (calendar-increment-month month year arg
)
298 (calendar-last-day-of-month month year
)
300 (if (not (calendar-date-is-visible-p last-day
))
301 (calendar-other-month month year
)
302 (calendar-cursor-to-visible-date last-day
)))
303 (run-hooks 'calendar-move-hook
))
306 (defun calendar-beginning-of-year (arg)
307 "Move the cursor backward ARG year beginnings."
309 (calendar-cursor-to-nearest-date)
310 (let* ((date (calendar-cursor-to-date))
311 (month (calendar-extract-month date
))
312 (day (calendar-extract-day date
))
313 (year (calendar-extract-year date
))
314 (jan-first (list 1 1 year
))
315 (calendar-move-hook nil
))
316 (if (and (= day
1) (= 1 month
))
317 (calendar-backward-month (* 12 arg
))
319 (calendar-date-is-visible-p jan-first
))
320 (calendar-cursor-to-visible-date jan-first
)
321 (calendar-other-month 1 (- year
(1- arg
)))
322 (calendar-cursor-to-visible-date (list 1 1 displayed-year
)))))
323 (run-hooks 'calendar-move-hook
))
326 (defun calendar-end-of-year (arg)
327 "Move the cursor forward ARG year beginnings."
329 (calendar-cursor-to-nearest-date)
330 (let* ((date (calendar-cursor-to-date))
331 (month (calendar-extract-month date
))
332 (day (calendar-extract-day date
))
333 (year (calendar-extract-year date
))
334 (dec-31 (list 12 31 year
))
335 (calendar-move-hook nil
))
336 (if (and (= day
31) (= 12 month
))
337 (calendar-forward-month (* 12 arg
))
339 (calendar-date-is-visible-p dec-31
))
340 (calendar-cursor-to-visible-date dec-31
)
341 (calendar-other-month 12 (+ year
(1- arg
)))
342 (calendar-cursor-to-visible-date (list 12 31 displayed-year
)))))
343 (run-hooks 'calendar-move-hook
))
346 (defun calendar-goto-date (date)
347 "Move cursor to DATE."
348 (interactive (list (calendar-read-date)))
349 (let ((month (calendar-extract-month date
))
350 (year (calendar-extract-year date
)))
351 (if (not (calendar-date-is-visible-p date
))
352 (calendar-other-month
353 (if (and (= month
1) (= year
1))
357 (calendar-cursor-to-visible-date date
)
358 (run-hooks 'calendar-move-hook
))
361 (defun calendar-goto-day-of-year (year day
&optional noecho
)
362 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
363 Negative DAY counts backward from end of year."
365 (let* ((year (calendar-read
368 (number-to-string (calendar-extract-year
369 (calendar-current-date)))))
370 (last (if (calendar-leap-year-p year
) 366 365))
372 (format "Day number (+/- 1-%d): " last
)
373 (lambda (x) (and (<= 1 (abs x
)) (<= (abs x
) last
))))))
376 (calendar-gregorian-from-absolute
378 (+ -
1 day
(calendar-absolute-from-gregorian (list 1 1 year
)))
379 (+ 1 day
(calendar-absolute-from-gregorian (list 12 31 year
))))))
380 (or noecho
(calendar-print-day-of-year)))
384 ;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
385 ;;; cal-move.el ends here