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
38 (defun calendar-cursor-to-nearest-date ()
39 "Move the cursor to the closest date.
40 The position of the cursor is unchanged if it is already on a date.
41 Returns the list (month day year) giving the cursor position."
42 (let ((date (calendar-cursor-to-date))
43 (column (current-column)))
45 (when (> 3 (count-lines (point-min) (point)))
47 (move-to-column column
))
48 (if (not (looking-at "[0-9]"))
49 (if (and (not (looking-at " *$"))
56 (re-search-forward "[0-9]" nil t
)
58 (re-search-backward "[0-9]" nil t
)))
59 (calendar-cursor-to-date))))
61 (defvar displayed-month
) ; from generate-calendar
62 (defvar displayed-year
)
65 (defun calendar-cursor-to-visible-date (date)
66 "Move the cursor to DATE that is on the screen."
67 (let* ((month (extract-calendar-month date
))
68 (day (extract-calendar-day date
))
69 (year (extract-calendar-year date
))
70 (first-of-month-weekday (calendar-day-of-week (list month
1 year
))))
74 (- (calendar-day-of-week (list month
1 year
))
75 calendar-week-start-day
)
80 (1+ (calendar-interval
81 displayed-month displayed-year month year
)))
83 (- (calendar-day-of-week date
)
84 calendar-week-start-day
)
88 (defun calendar-goto-today ()
89 "Reposition the calendar window so the current date is visible."
91 (let ((today (calendar-current-date))) ; the date might have changed
92 (if (not (calendar-date-is-visible-p today
))
93 (generate-calendar-window)
94 (update-calendar-mode-line)
95 (calendar-cursor-to-visible-date today
)))
96 (run-hooks 'calendar-move-hook
))
99 (defun calendar-forward-month (arg)
100 "Move the cursor forward ARG months.
101 Movement is backward if ARG is negative."
103 (calendar-cursor-to-nearest-date)
104 (let* ((cursor-date (calendar-cursor-to-date t
))
105 (month (extract-calendar-month cursor-date
))
106 (day (extract-calendar-day cursor-date
))
107 (year (extract-calendar-year cursor-date
)))
108 (increment-calendar-month month year arg
)
109 (let ((last (calendar-last-day-of-month month year
)))
112 ;; Put the new month on the screen, if needed, and go to the new date.
113 (let ((new-cursor-date (list month day year
)))
114 (if (not (calendar-date-is-visible-p new-cursor-date
))
115 (calendar-other-month month year
))
116 (calendar-cursor-to-visible-date new-cursor-date
)))
117 (run-hooks 'calendar-move-hook
))
120 (defun calendar-forward-year (arg)
121 "Move the cursor forward by ARG years.
122 Movement is backward if ARG is negative."
124 (calendar-forward-month (* 12 arg
)))
127 (defun calendar-backward-month (arg)
128 "Move the cursor backward by ARG months.
129 Movement is forward if ARG is negative."
131 (calendar-forward-month (- arg
)))
134 (defun calendar-backward-year (arg)
135 "Move the cursor backward ARG years.
136 Movement is forward is ARG is negative."
138 (calendar-forward-month (* -
12 arg
)))
141 (defun calendar-scroll-left (&optional arg event
)
142 "Scroll the displayed calendar left by ARG months.
143 If ARG is negative the calendar is scrolled right. Maintains the relative
144 position of the cursor with respect to the calendar as well as possible.
145 EVENT is an event like `last-nonmenu-event'."
146 (interactive (list (prefix-numeric-value current-prefix-arg
)
148 (unless arg
(setq arg
1))
149 (save-selected-window
150 (select-window (posn-window (event-start event
)))
151 (calendar-cursor-to-nearest-date)
153 (let ((old-date (calendar-cursor-to-date))
154 (today (calendar-current-date))
155 (month displayed-month
)
156 (year displayed-year
))
157 (increment-calendar-month month year arg
)
158 (generate-calendar-window month year
)
159 (calendar-cursor-to-visible-date
161 ((calendar-date-is-visible-p old-date
) old-date
)
162 ((calendar-date-is-visible-p today
) today
)
163 (t (list month
1 year
))))))
164 (run-hooks 'calendar-move-hook
)))
166 (define-obsolete-function-alias
167 'scroll-calendar-left
'calendar-scroll-left
"23.1")
170 (defun calendar-scroll-right (&optional arg event
)
171 "Scroll the displayed calendar window right by ARG months.
172 If ARG is negative the calendar is scrolled left. Maintains the relative
173 position of the cursor with respect to the calendar as well as possible.
174 EVENT is an event like `last-nonmenu-event'."
175 (interactive (list (prefix-numeric-value current-prefix-arg
)
177 (calendar-scroll-left (- (or arg
1)) event
))
179 (define-obsolete-function-alias
180 'scroll-calendar-right
'calendar-scroll-right
"23.1")
183 (defun calendar-scroll-left-three-months (arg)
184 "Scroll the displayed calendar window left by 3*ARG months.
185 If ARG is negative the calendar is scrolled right. Maintains the relative
186 position of the cursor with respect to the calendar as well as possible."
188 (calendar-scroll-left (* 3 arg
)))
190 (define-obsolete-function-alias 'scroll-calendar-left-three-months
191 'calendar-scroll-left-three-months
"23.1")
194 (defun calendar-scroll-right-three-months (arg)
195 "Scroll the displayed calendar window right by 3*ARG months.
196 If ARG is negative the calendar is scrolled left. Maintains the relative
197 position of the cursor with respect to the calendar as well as possible."
199 (calendar-scroll-left (* -
3 arg
)))
201 (define-obsolete-function-alias 'scroll-calendar-right-three-months
202 'calendar-scroll-right-three-months
"23.1")
205 (defun calendar-forward-day (arg)
206 "Move the cursor forward ARG days.
207 Moves backward if ARG is negative."
210 (let* ((cursor-date (or (calendar-cursor-to-date)
212 (if (> arg
0) (setq arg
(1- arg
)))
213 (calendar-cursor-to-nearest-date))))
215 (calendar-gregorian-from-absolute
216 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
217 (new-display-month (extract-calendar-month new-cursor-date
))
218 (new-display-year (extract-calendar-year new-cursor-date
)))
219 ;; Put the new month on the screen, if needed, and go to the new date.
220 (if (not (calendar-date-is-visible-p new-cursor-date
))
221 (calendar-other-month new-display-month new-display-year
))
222 (calendar-cursor-to-visible-date new-cursor-date
)))
223 (run-hooks 'calendar-move-hook
))
226 (defun calendar-backward-day (arg)
227 "Move the cursor back ARG days.
228 Moves forward if ARG is negative."
230 (calendar-forward-day (- arg
)))
233 (defun calendar-forward-week (arg)
234 "Move the cursor forward ARG weeks.
235 Moves backward if ARG is negative."
237 (calendar-forward-day (* arg
7)))
240 (defun calendar-backward-week (arg)
241 "Move the cursor back ARG weeks.
242 Moves forward if ARG is negative."
244 (calendar-forward-day (* arg -
7)))
247 (defun calendar-beginning-of-week (arg)
248 "Move the cursor back ARG calendar-week-start-day's."
250 (calendar-cursor-to-nearest-date)
251 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
252 (calendar-backward-day
253 (if (= day calendar-week-start-day
)
255 (+ (mod (- day calendar-week-start-day
) 7)
259 (defun calendar-end-of-week (arg)
260 "Move the cursor forward ARG calendar-week-start-day+6's."
262 (calendar-cursor-to-nearest-date)
263 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
264 (calendar-forward-day
265 (if (= day
(mod (1- calendar-week-start-day
) 7))
267 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
271 (defun calendar-beginning-of-month (arg)
272 "Move the cursor backward ARG month beginnings."
274 (calendar-cursor-to-nearest-date)
275 (let* ((date (calendar-cursor-to-date))
276 (month (extract-calendar-month date
))
277 (day (extract-calendar-day date
))
278 (year (extract-calendar-year date
)))
280 (calendar-backward-month arg
)
281 (calendar-cursor-to-visible-date (list month
1 year
))
282 (calendar-backward-month (1- arg
)))))
285 (defun calendar-end-of-month (arg)
286 "Move the cursor forward ARG month ends."
288 (calendar-cursor-to-nearest-date)
289 (let* ((date (calendar-cursor-to-date))
290 (month (extract-calendar-month date
))
291 (day (extract-calendar-day date
))
292 (year (extract-calendar-year date
))
293 (last-day (calendar-last-day-of-month month year
)))
294 (unless (= day last-day
)
295 (calendar-cursor-to-visible-date (list month last-day year
))
297 (increment-calendar-month month year arg
)
298 (let ((last-day (list
300 (calendar-last-day-of-month month year
)
302 (if (not (calendar-date-is-visible-p last-day
))
303 (calendar-other-month month year
)
304 (calendar-cursor-to-visible-date last-day
))))
305 (run-hooks 'calendar-move-hook
))
308 (defun calendar-beginning-of-year (arg)
309 "Move the cursor backward ARG year beginnings."
311 (calendar-cursor-to-nearest-date)
312 (let* ((date (calendar-cursor-to-date))
313 (month (extract-calendar-month date
))
314 (day (extract-calendar-day date
))
315 (year (extract-calendar-year date
))
316 (jan-first (list 1 1 year
))
317 (calendar-move-hook nil
))
318 (if (and (= day
1) (= 1 month
))
319 (calendar-backward-month (* 12 arg
))
321 (calendar-date-is-visible-p jan-first
))
322 (calendar-cursor-to-visible-date jan-first
)
323 (calendar-other-month 1 (- year
(1- arg
)))
324 (calendar-cursor-to-visible-date (list 1 1 displayed-year
)))))
325 (run-hooks 'calendar-move-hook
))
328 (defun calendar-end-of-year (arg)
329 "Move the cursor forward ARG year beginnings."
331 (calendar-cursor-to-nearest-date)
332 (let* ((date (calendar-cursor-to-date))
333 (month (extract-calendar-month date
))
334 (day (extract-calendar-day date
))
335 (year (extract-calendar-year date
))
336 (dec-31 (list 12 31 year
))
337 (calendar-move-hook nil
))
338 (if (and (= day
31) (= 12 month
))
339 (calendar-forward-month (* 12 arg
))
341 (calendar-date-is-visible-p dec-31
))
342 (calendar-cursor-to-visible-date dec-31
)
343 (calendar-other-month 12 (+ year
(1- arg
)))
344 (calendar-cursor-to-visible-date (list 12 31 displayed-year
)))))
345 (run-hooks 'calendar-move-hook
))
348 (defun calendar-goto-date (date)
349 "Move cursor to DATE."
350 (interactive (list (calendar-read-date)))
351 (let ((month (extract-calendar-month date
))
352 (year (extract-calendar-year date
)))
353 (if (not (calendar-date-is-visible-p date
))
354 (calendar-other-month
355 (if (and (= month
1) (= year
1))
359 (calendar-cursor-to-visible-date date
)
360 (run-hooks 'calendar-move-hook
))
363 (defun calendar-goto-day-of-year (year day
&optional noecho
)
364 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
365 Negative DAY counts backward from end of year."
367 (let* ((year (calendar-read
370 (int-to-string (extract-calendar-year
371 (calendar-current-date)))))
372 (last (if (calendar-leap-year-p year
) 366 365))
374 (format "Day number (+/- 1-%d): " last
)
375 (lambda (x) (and (<= 1 (abs x
)) (<= (abs x
) last
))))))
378 (calendar-gregorian-from-absolute
380 (+ -
1 day
(calendar-absolute-from-gregorian (list 1 1 year
)))
381 (+ 1 day
(calendar-absolute-from-gregorian (list 12 31 year
))))))
382 (or noecho
(calendar-print-day-of-year)))
386 ;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
387 ;;; cal-move.el ends here