1 ;;; cal-move.el --- calendar functions for movement in the calendar
3 ;; Copyright (C) 1995, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; 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 of the License, or
16 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
32 ;; FIXME should calendar just require this?
36 ;; Note that this is not really the "closest" date.
37 ;; In most cases, it just searches forwards for the next day.
39 (defun calendar-cursor-to-nearest-date ()
40 "Move the cursor to the closest date.
41 The position of the cursor is unchanged if it is already on a date.
42 Returns the list (month day year) giving the cursor position."
43 (or (calendar-cursor-to-date)
44 (let* ((col (current-column))
45 (edges (cdr (assoc (calendar-column-to-segment)
46 calendar-month-edges
)))
48 (right (nth 3 edges
)))
49 (when (< (count-lines (point-min) (point)) calendar-first-date-row
)
50 (goto-char (point-min))
51 (forward-line (1- calendar-first-date-row
))
53 ;; The date positions are fixed and computable, but searching
54 ;; is probably more flexible. Need to consider blank days at
55 ;; start and end of month if computing positions.
56 ;; 'date text-property is used to exclude intermonth text.
57 (unless (and (looking-at "[0-9]")
58 (get-text-property (point) 'date
))
59 ;; We search forwards for a number, except close to the RH
60 ;; margin of a month, where we search backwards.
61 ;; Note that the searches can go to other lines.
62 (if (or (looking-at " *$")
63 (and (> col last
) (< col right
)))
64 (while (and (re-search-backward "[0-9]" nil t
)
65 (not (get-text-property (point) 'date
))))
66 (while (and (re-search-forward "[0-9]" nil t
)
67 (not (get-text-property (1- (point)) 'date
))))
69 (calendar-cursor-to-date))))
71 (defvar displayed-month
) ; from calendar-generate
72 (defvar displayed-year
)
75 (defun calendar-cursor-to-visible-date (date)
76 "Move the cursor to DATE that is on the screen."
77 (let ((month (calendar-extract-month date
))
78 (day (calendar-extract-day date
))
79 (year (calendar-extract-year date
)))
80 (goto-char (point-min))
81 (forward-line (+ calendar-first-date-row -
1
84 (- (calendar-day-of-week (list month
1 year
))
85 calendar-week-start-day
)
88 (move-to-column (+ calendar-left-margin
(1- calendar-day-digit-width
)
89 (* calendar-month-width
90 (1+ (calendar-interval
91 displayed-month displayed-year month year
)))
92 (* calendar-column-width
94 (- (calendar-day-of-week date
)
95 calendar-week-start-day
)
99 (defun calendar-goto-today ()
100 "Reposition the calendar window so the current date is visible."
102 (let ((today (calendar-current-date))) ; the date might have changed
103 (if (not (calendar-date-is-visible-p today
))
104 (calendar-generate-window)
105 (calendar-update-mode-line)
106 (calendar-cursor-to-visible-date today
)))
107 (run-hooks 'calendar-move-hook
))
110 (defun calendar-forward-month (arg)
111 "Move the cursor forward ARG months.
112 Movement is backward if ARG is negative."
114 (calendar-cursor-to-nearest-date)
115 (let* ((cursor-date (calendar-cursor-to-date t
))
116 (month (calendar-extract-month cursor-date
))
117 (day (calendar-extract-day cursor-date
))
118 (year (calendar-extract-year cursor-date
))
120 (calendar-increment-month month year arg
)
121 (calendar-last-day-of-month month year
)))
123 ;; Put the new month on the screen, if needed, and go to the new date.
124 (new-cursor-date (list month day year
)))
125 (if (not (calendar-date-is-visible-p new-cursor-date
))
126 (calendar-other-month month year
))
127 (calendar-cursor-to-visible-date new-cursor-date
))
128 (run-hooks 'calendar-move-hook
))
131 (defun calendar-forward-year (arg)
132 "Move the cursor forward by ARG years.
133 Movement is backward if ARG is negative."
135 (calendar-forward-month (* 12 arg
)))
138 (defun calendar-backward-month (arg)
139 "Move the cursor backward by ARG months.
140 Movement is forward if ARG is negative."
142 (calendar-forward-month (- arg
)))
145 (defun calendar-backward-year (arg)
146 "Move the cursor backward ARG years.
147 Movement is forward is ARG is negative."
149 (calendar-forward-month (* -
12 arg
)))
152 (defun calendar-scroll-left (&optional arg event
)
153 "Scroll the displayed calendar left by ARG months.
154 If ARG is negative the calendar is scrolled right. Maintains the relative
155 position of the cursor with respect to the calendar as well as possible.
156 EVENT is an event like `last-nonmenu-event'."
157 (interactive (list (prefix-numeric-value current-prefix-arg
)
159 (unless arg
(setq arg
1))
160 (save-selected-window
161 ;; Nil if called from menu-bar.
162 (if (setq event
(event-start event
)) (select-window (posn-window event
)))
163 (calendar-cursor-to-nearest-date)
165 (let ((old-date (calendar-cursor-to-date))
166 (today (calendar-current-date))
167 (month displayed-month
)
168 (year displayed-year
))
169 (calendar-increment-month month year arg
)
170 (calendar-generate-window month year
)
171 (calendar-cursor-to-visible-date
173 ((calendar-date-is-visible-p old-date
) old-date
)
174 ((calendar-date-is-visible-p today
) today
)
175 (t (list month
1 year
))))))
176 (run-hooks 'calendar-move-hook
)))
178 (define-obsolete-function-alias
179 'scroll-calendar-left
'calendar-scroll-left
"23.1")
182 (defun calendar-scroll-right (&optional arg event
)
183 "Scroll the displayed calendar window right by ARG months.
184 If ARG is negative the calendar is scrolled left. Maintains the relative
185 position of the cursor with respect to the calendar as well as possible.
186 EVENT is an event like `last-nonmenu-event'."
187 (interactive (list (prefix-numeric-value current-prefix-arg
)
189 (calendar-scroll-left (- (or arg
1)) event
))
191 (define-obsolete-function-alias
192 'scroll-calendar-right
'calendar-scroll-right
"23.1")
195 (defun calendar-scroll-left-three-months (arg &optional event
)
196 "Scroll the displayed calendar window left by 3*ARG months.
197 If ARG is negative the calendar is scrolled right. Maintains the relative
198 position of the cursor with respect to the calendar as well as possible.
199 EVENT is an event like `last-nonmenu-event'."
200 (interactive (list (prefix-numeric-value current-prefix-arg
)
202 (calendar-scroll-left (* 3 arg
) event
))
204 (define-obsolete-function-alias 'scroll-calendar-left-three-months
205 'calendar-scroll-left-three-months
"23.1")
207 ;; cf scroll-bar-toolkit-scroll
209 (defun calendar-scroll-toolkit-scroll (event)
210 "Function to scroll the calendar after a toolkit scroll-bar click."
212 (let ((part (nth 4 (event-end event
))))
213 ;; Not bothering with drag events (handle, end-scroll).
214 (cond ((memq part
'(above-handle up top
))
215 (calendar-scroll-right nil event
))
216 ((memq part
'(below-handle down bottom
))
217 (calendar-scroll-left nil event
)))))
220 (defun calendar-scroll-right-three-months (arg &optional event
)
221 "Scroll the displayed calendar window right by 3*ARG months.
222 If ARG is negative the calendar is scrolled left. Maintains the relative
223 position of the cursor with respect to the calendar as well as possible.
224 EVENT is an event like `last-nonmenu-event'."
225 (interactive (list (prefix-numeric-value current-prefix-arg
)
227 (calendar-scroll-left (* -
3 arg
) event
))
229 (define-obsolete-function-alias 'scroll-calendar-right-three-months
230 'calendar-scroll-right-three-months
"23.1")
233 (defun calendar-forward-day (arg)
234 "Move the cursor forward ARG days.
235 Moves backward if ARG is negative."
238 (let* ((cursor-date (or (calendar-cursor-to-date)
240 (if (> arg
0) (setq arg
(1- arg
)))
241 (calendar-cursor-to-nearest-date))))
243 (calendar-gregorian-from-absolute
244 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
245 (new-display-month (calendar-extract-month new-cursor-date
))
246 (new-display-year (calendar-extract-year new-cursor-date
)))
247 ;; Put the new month on the screen, if needed.
248 (unless (calendar-date-is-visible-p new-cursor-date
)
249 ;; The next line gives smoother scrolling IMO (one month at a
250 ;; time rather than two).
251 (calendar-increment-month new-display-month new-display-year
253 (calendar-other-month new-display-month new-display-year
))
254 ;; Go to the new date.
255 (calendar-cursor-to-visible-date new-cursor-date
)))
256 (run-hooks 'calendar-move-hook
))
259 (defun calendar-backward-day (arg)
260 "Move the cursor back ARG days.
261 Moves forward if ARG is negative."
263 (calendar-forward-day (- arg
)))
266 (defun calendar-forward-week (arg)
267 "Move the cursor forward ARG weeks.
268 Moves backward if ARG is negative."
270 (calendar-forward-day (* arg
7)))
273 (defun calendar-backward-week (arg)
274 "Move the cursor back ARG weeks.
275 Moves forward if ARG is negative."
277 (calendar-forward-day (* arg -
7)))
280 (defun calendar-beginning-of-week (arg)
281 "Move the cursor back ARG calendar-week-start-day's."
283 (calendar-cursor-to-nearest-date)
284 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
285 (calendar-backward-day
286 (if (= day calendar-week-start-day
)
288 (+ (mod (- day calendar-week-start-day
) 7)
292 (defun calendar-end-of-week (arg)
293 "Move the cursor forward ARG calendar-week-start-day+6's."
295 (calendar-cursor-to-nearest-date)
296 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
297 (calendar-forward-day
298 (if (= day
(mod (1- calendar-week-start-day
) 7))
300 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
304 (defun calendar-beginning-of-month (arg)
305 "Move the cursor backward ARG month beginnings."
307 (calendar-cursor-to-nearest-date)
308 (let* ((date (calendar-cursor-to-date))
309 (month (calendar-extract-month date
))
310 (day (calendar-extract-day date
))
311 (year (calendar-extract-year date
)))
313 (calendar-backward-month arg
)
314 (calendar-cursor-to-visible-date (list month
1 year
))
315 (calendar-backward-month (1- arg
)))))
318 (defun calendar-end-of-month (arg)
319 "Move the cursor forward ARG month ends."
321 (calendar-cursor-to-nearest-date)
322 (let* ((date (calendar-cursor-to-date))
323 (month (calendar-extract-month date
))
324 (day (calendar-extract-day date
))
325 (year (calendar-extract-year date
))
326 (last-day (calendar-last-day-of-month month year
))
328 (unless (= day last-day
)
329 (calendar-cursor-to-visible-date
330 (list month last-day year
))
332 (calendar-increment-month month year arg
)
334 (calendar-last-day-of-month month year
)
336 (if (not (calendar-date-is-visible-p last-day
))
337 (calendar-other-month month year
)
338 (calendar-cursor-to-visible-date last-day
)))
339 (run-hooks 'calendar-move-hook
))
342 (defun calendar-beginning-of-year (arg)
343 "Move the cursor backward ARG year beginnings."
345 (calendar-cursor-to-nearest-date)
346 (let* ((date (calendar-cursor-to-date))
347 (month (calendar-extract-month date
))
348 (day (calendar-extract-day date
))
349 (year (calendar-extract-year date
))
350 (jan-first (list 1 1 year
))
351 (calendar-move-hook nil
))
352 (if (and (= day
1) (= 1 month
))
353 (calendar-backward-month (* 12 arg
))
355 (calendar-date-is-visible-p jan-first
))
356 (calendar-cursor-to-visible-date jan-first
)
357 (calendar-other-month 1 (- year
(1- arg
)))
358 (calendar-cursor-to-visible-date (list 1 1 displayed-year
)))))
359 (run-hooks 'calendar-move-hook
))
362 (defun calendar-end-of-year (arg)
363 "Move the cursor forward ARG year beginnings."
365 (calendar-cursor-to-nearest-date)
366 (let* ((date (calendar-cursor-to-date))
367 (month (calendar-extract-month date
))
368 (day (calendar-extract-day date
))
369 (year (calendar-extract-year date
))
370 (dec-31 (list 12 31 year
))
371 (calendar-move-hook nil
))
372 (if (and (= day
31) (= 12 month
))
373 (calendar-forward-month (* 12 arg
))
375 (calendar-date-is-visible-p dec-31
))
376 (calendar-cursor-to-visible-date dec-31
)
377 (calendar-other-month 12 (+ year
(1- arg
)))
378 (calendar-cursor-to-visible-date (list 12 31 displayed-year
)))))
379 (run-hooks 'calendar-move-hook
))
382 (defun calendar-goto-date (date)
383 "Move cursor to DATE."
384 (interactive (list (calendar-read-date)))
385 (let ((month (calendar-extract-month date
))
386 (year (calendar-extract-year date
)))
387 (if (not (calendar-date-is-visible-p date
))
388 (calendar-other-month
389 (if (and (= month
1) (= year
1))
393 (calendar-cursor-to-visible-date date
)
394 (run-hooks 'calendar-move-hook
))
397 (defun calendar-goto-day-of-year (year day
&optional noecho
)
398 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
399 Negative DAY counts backward from end of year."
401 (let* ((year (calendar-read
404 (number-to-string (calendar-extract-year
405 (calendar-current-date)))))
406 (last (if (calendar-leap-year-p year
) 366 365))
408 (format "Day number (+/- 1-%d): " last
)
409 (lambda (x) (and (<= 1 (abs x
)) (<= (abs x
) last
))))))
412 (calendar-gregorian-from-absolute
414 (+ -
1 day
(calendar-absolute-from-gregorian (list 1 1 year
)))
415 (+ 1 day
(calendar-absolute-from-gregorian (list 12 31 year
))))))
416 (or noecho
(calendar-print-day-of-year)))
420 ;;; cal-move.el ends here