1 ;;; cal-move.el --- calendar functions for movement in the calendar
3 ;; Copyright (C) 1995, 2001-2016 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
)))
179 (defun calendar-scroll-right (&optional arg event
)
180 "Scroll the displayed calendar window right by ARG months.
181 If ARG is negative the calendar is scrolled left. Maintains the relative
182 position of the cursor with respect to the calendar as well as possible.
183 EVENT is an event like `last-nonmenu-event'."
184 (interactive (list (prefix-numeric-value current-prefix-arg
)
186 (calendar-scroll-left (- (or arg
1)) event
))
189 (defun calendar-scroll-left-three-months (arg &optional event
)
190 "Scroll the displayed calendar window left by 3*ARG months.
191 If ARG is negative the calendar is scrolled right. Maintains the relative
192 position of the cursor with respect to the calendar as well as possible.
193 EVENT is an event like `last-nonmenu-event'."
194 (interactive (list (prefix-numeric-value current-prefix-arg
)
196 (calendar-scroll-left (* 3 arg
) event
))
198 ;; cf scroll-bar-toolkit-scroll
200 (defun calendar-scroll-toolkit-scroll (event)
201 "Function to scroll the calendar after a toolkit scroll-bar click."
203 (let ((part (nth 4 (event-end event
))))
204 ;; Not bothering with drag events (handle, end-scroll).
205 (cond ((memq part
'(above-handle up top
))
206 (calendar-scroll-right nil event
))
207 ((memq part
'(below-handle down bottom
))
208 (calendar-scroll-left nil event
)))))
211 (defun calendar-scroll-right-three-months (arg &optional event
)
212 "Scroll the displayed calendar window right by 3*ARG months.
213 If ARG is negative the calendar is scrolled left. Maintains the relative
214 position of the cursor with respect to the calendar as well as possible.
215 EVENT is an event like `last-nonmenu-event'."
216 (interactive (list (prefix-numeric-value current-prefix-arg
)
218 (calendar-scroll-left (* -
3 arg
) event
))
221 (defun calendar-forward-day (arg)
222 "Move the cursor forward ARG days.
223 Moves backward if ARG is negative."
226 (let* ((cursor-date (or (calendar-cursor-to-date)
228 (if (> arg
0) (setq arg
(1- arg
)))
229 (calendar-cursor-to-nearest-date))))
231 (calendar-gregorian-from-absolute
232 (+ (calendar-absolute-from-gregorian cursor-date
) arg
)))
233 (new-display-month (calendar-extract-month new-cursor-date
))
234 (new-display-year (calendar-extract-year new-cursor-date
)))
235 ;; Put the new month on the screen, if needed.
236 (unless (calendar-date-is-visible-p new-cursor-date
)
237 ;; The next line gives smoother scrolling IMO (one month at a
238 ;; time rather than two).
239 (calendar-increment-month new-display-month new-display-year
241 (calendar-other-month new-display-month new-display-year
))
242 ;; Go to the new date.
243 (calendar-cursor-to-visible-date new-cursor-date
)))
244 (run-hooks 'calendar-move-hook
))
247 (defun calendar-backward-day (arg)
248 "Move the cursor back ARG days.
249 Moves forward if ARG is negative."
251 (calendar-forward-day (- arg
)))
254 (defun calendar-forward-week (arg)
255 "Move the cursor forward ARG weeks.
256 Moves backward if ARG is negative."
258 (calendar-forward-day (* arg
7)))
261 (defun calendar-backward-week (arg)
262 "Move the cursor back ARG weeks.
263 Moves forward if ARG is negative."
265 (calendar-forward-day (* arg -
7)))
268 (defun calendar-beginning-of-week (arg)
269 "Move the cursor back ARG calendar-week-start-day's."
271 (calendar-cursor-to-nearest-date)
272 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
273 (calendar-backward-day
274 (if (= day calendar-week-start-day
)
276 (+ (mod (- day calendar-week-start-day
) 7)
280 (defun calendar-end-of-week (arg)
281 "Move the cursor forward ARG calendar-week-start-day+6's."
283 (calendar-cursor-to-nearest-date)
284 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
285 (calendar-forward-day
286 (if (= day
(mod (1- calendar-week-start-day
) 7))
288 (+ (- 6 (mod (- day calendar-week-start-day
) 7))
292 (defun calendar-beginning-of-month (arg)
293 "Move the cursor backward ARG month beginnings."
295 (calendar-cursor-to-nearest-date)
296 (let* ((date (calendar-cursor-to-date))
297 (month (calendar-extract-month date
))
298 (day (calendar-extract-day date
))
299 (year (calendar-extract-year date
)))
301 (calendar-backward-month arg
)
302 (calendar-cursor-to-visible-date (list month
1 year
))
303 (calendar-backward-month (1- arg
)))))
306 (defun calendar-end-of-month (arg)
307 "Move the cursor forward ARG month ends."
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 (last-day (calendar-last-day-of-month month year
))
316 (unless (= day last-day
)
317 (calendar-cursor-to-visible-date
318 (list month last-day year
))
320 (calendar-increment-month month year arg
)
322 (calendar-last-day-of-month month year
)
324 (if (not (calendar-date-is-visible-p last-day
))
325 (calendar-other-month month year
)
326 (calendar-cursor-to-visible-date last-day
)))
327 (run-hooks 'calendar-move-hook
))
330 (defun calendar-beginning-of-year (arg)
331 "Move the cursor backward ARG year beginnings."
333 (calendar-cursor-to-nearest-date)
334 (let* ((date (calendar-cursor-to-date))
335 (month (calendar-extract-month date
))
336 (day (calendar-extract-day date
))
337 (year (calendar-extract-year date
))
338 (jan-first (list 1 1 year
))
339 (calendar-move-hook nil
))
340 (if (and (= day
1) (= 1 month
))
341 (calendar-backward-month (* 12 arg
))
343 (calendar-date-is-visible-p jan-first
))
344 (calendar-cursor-to-visible-date jan-first
)
345 (calendar-other-month 1 (- year
(1- arg
)))
346 (calendar-cursor-to-visible-date (list 1 1 displayed-year
)))))
347 (run-hooks 'calendar-move-hook
))
350 (defun calendar-end-of-year (arg)
351 "Move the cursor forward ARG year beginnings."
353 (calendar-cursor-to-nearest-date)
354 (let* ((date (calendar-cursor-to-date))
355 (month (calendar-extract-month date
))
356 (day (calendar-extract-day date
))
357 (year (calendar-extract-year date
))
358 (dec-31 (list 12 31 year
))
359 (calendar-move-hook nil
))
360 (if (and (= day
31) (= 12 month
))
361 (calendar-forward-month (* 12 arg
))
363 (calendar-date-is-visible-p dec-31
))
364 (calendar-cursor-to-visible-date dec-31
)
365 (calendar-other-month 12 (+ year
(1- arg
)))
366 (calendar-cursor-to-visible-date (list 12 31 displayed-year
)))))
367 (run-hooks 'calendar-move-hook
))
370 (defun calendar-goto-date (date)
371 "Move cursor to DATE."
372 (interactive (list (calendar-read-date)))
373 (let ((month (calendar-extract-month date
))
374 (year (calendar-extract-year date
)))
375 (if (not (calendar-date-is-visible-p date
))
376 (calendar-other-month
377 (if (and (= month
1) (= year
1))
381 (calendar-cursor-to-visible-date date
)
382 (run-hooks 'calendar-move-hook
))
385 (defun calendar-goto-day-of-year (year day
&optional noecho
)
386 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is non-nil.
387 Negative DAY counts backward from end of year."
389 (let* ((year (calendar-read
392 (number-to-string (calendar-extract-year
393 (calendar-current-date)))))
394 (last (if (calendar-leap-year-p year
) 366 365))
396 (format "Day number (+/- 1-%d): " last
)
397 (lambda (x) (and (<= 1 (abs x
)) (<= (abs x
) last
))))))
400 (calendar-gregorian-from-absolute
402 (+ -
1 day
(calendar-absolute-from-gregorian (list 1 1 year
)))
403 (+ 1 day
(calendar-absolute-from-gregorian (list 12 31 year
))))))
404 (or noecho
(calendar-print-day-of-year)))
408 ;;; cal-move.el ends here