Update copyright year to 2015
[emacs.git] / lisp / calendar / cal-move.el
blobf7bae5f6441091f073f76cfa0aedd5082a25b0f1
1 ;;; cal-move.el --- calendar functions for movement in the calendar
3 ;; Copyright (C) 1995, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: calendar
8 ;; Human-Keywords: calendar
9 ;; Package: 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/>.
26 ;;; Commentary:
28 ;; See calendar.el.
30 ;;; Code:
32 ;; FIXME should calendar just require this?
33 (require 'calendar)
36 ;; Note that this is not really the "closest" date.
37 ;; In most cases, it just searches forwards for the next day.
38 ;;;###cal-autoload
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)))
47 (last (nth 2 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))
52 (move-to-column col))
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))))
68 (backward-char 1)))
69 (calendar-cursor-to-date))))
71 (defvar displayed-month) ; from calendar-generate
72 (defvar displayed-year)
74 ;;;###cal-autoload
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
82 (/ (+ day -1
83 (mod
84 (- (calendar-day-of-week (list month 1 year))
85 calendar-week-start-day)
86 7))
87 7)))
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
93 (mod
94 (- (calendar-day-of-week date)
95 calendar-week-start-day)
96 7))))))
98 ;;;###cal-autoload
99 (defun calendar-goto-today ()
100 "Reposition the calendar window so the current date is visible."
101 (interactive)
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))
109 ;;;###cal-autoload
110 (defun calendar-forward-month (arg)
111 "Move the cursor forward ARG months.
112 Movement is backward if ARG is negative."
113 (interactive "p")
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))
119 (last (progn
120 (calendar-increment-month month year arg)
121 (calendar-last-day-of-month month year)))
122 (day (min last day))
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))
130 ;;;###cal-autoload
131 (defun calendar-forward-year (arg)
132 "Move the cursor forward by ARG years.
133 Movement is backward if ARG is negative."
134 (interactive "p")
135 (calendar-forward-month (* 12 arg)))
137 ;;;###cal-autoload
138 (defun calendar-backward-month (arg)
139 "Move the cursor backward by ARG months.
140 Movement is forward if ARG is negative."
141 (interactive "p")
142 (calendar-forward-month (- arg)))
144 ;;;###cal-autoload
145 (defun calendar-backward-year (arg)
146 "Move the cursor backward ARG years.
147 Movement is forward is ARG is negative."
148 (interactive "p")
149 (calendar-forward-month (* -12 arg)))
151 ;;;###cal-autoload
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)
158 last-nonmenu-event))
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)
164 (unless (zerop arg)
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
172 (cond
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 ;;;###cal-autoload
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)
185 last-nonmenu-event))
186 (calendar-scroll-left (- (or arg 1)) event))
188 ;;;###cal-autoload
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)
195 last-nonmenu-event))
196 (calendar-scroll-left (* 3 arg) event))
198 ;; cf scroll-bar-toolkit-scroll
199 ;;;###cal-autoload
200 (defun calendar-scroll-toolkit-scroll (event)
201 "Function to scroll the calendar after a toolkit scroll-bar click."
202 (interactive "e")
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)))))
210 ;;;###cal-autoload
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)
217 last-nonmenu-event))
218 (calendar-scroll-left (* -3 arg) event))
220 ;;;###cal-autoload
221 (defun calendar-forward-day (arg)
222 "Move the cursor forward ARG days.
223 Moves backward if ARG is negative."
224 (interactive "p")
225 (unless (zerop arg)
226 (let* ((cursor-date (or (calendar-cursor-to-date)
227 (progn
228 (if (> arg 0) (setq arg (1- arg)))
229 (calendar-cursor-to-nearest-date))))
230 (new-cursor-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
240 (if (< arg 0) 1 -1))
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))
246 ;;;###cal-autoload
247 (defun calendar-backward-day (arg)
248 "Move the cursor back ARG days.
249 Moves forward if ARG is negative."
250 (interactive "p")
251 (calendar-forward-day (- arg)))
253 ;;;###cal-autoload
254 (defun calendar-forward-week (arg)
255 "Move the cursor forward ARG weeks.
256 Moves backward if ARG is negative."
257 (interactive "p")
258 (calendar-forward-day (* arg 7)))
260 ;;;###cal-autoload
261 (defun calendar-backward-week (arg)
262 "Move the cursor back ARG weeks.
263 Moves forward if ARG is negative."
264 (interactive "p")
265 (calendar-forward-day (* arg -7)))
267 ;;;###cal-autoload
268 (defun calendar-beginning-of-week (arg)
269 "Move the cursor back ARG calendar-week-start-day's."
270 (interactive "p")
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)
275 (* 7 arg)
276 (+ (mod (- day calendar-week-start-day) 7)
277 (* 7 (1- arg)))))))
279 ;;;###cal-autoload
280 (defun calendar-end-of-week (arg)
281 "Move the cursor forward ARG calendar-week-start-day+6's."
282 (interactive "p")
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))
287 (* 7 arg)
288 (+ (- 6 (mod (- day calendar-week-start-day) 7))
289 (* 7 (1- arg)))))))
291 ;;;###cal-autoload
292 (defun calendar-beginning-of-month (arg)
293 "Move the cursor backward ARG month beginnings."
294 (interactive "p")
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)))
300 (if (= day 1)
301 (calendar-backward-month arg)
302 (calendar-cursor-to-visible-date (list month 1 year))
303 (calendar-backward-month (1- arg)))))
305 ;;;###cal-autoload
306 (defun calendar-end-of-month (arg)
307 "Move the cursor forward ARG month ends."
308 (interactive "p")
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))
315 (last-day (progn
316 (unless (= day last-day)
317 (calendar-cursor-to-visible-date
318 (list month last-day year))
319 (setq arg (1- arg)))
320 (calendar-increment-month month year arg)
321 (list month
322 (calendar-last-day-of-month month year)
323 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))
329 ;;;###cal-autoload
330 (defun calendar-beginning-of-year (arg)
331 "Move the cursor backward ARG year beginnings."
332 (interactive "p")
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))
342 (if (and (= arg 1)
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))
349 ;;;###cal-autoload
350 (defun calendar-end-of-year (arg)
351 "Move the cursor forward ARG year beginnings."
352 (interactive "p")
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))
362 (if (and (= arg 1)
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))
369 ;;;###cal-autoload
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))
379 month)
380 year)))
381 (calendar-cursor-to-visible-date date)
382 (run-hooks 'calendar-move-hook))
384 ;;;###cal-autoload
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."
388 (interactive
389 (let* ((year (calendar-read
390 "Year (>0): "
391 (lambda (x) (> x 0))
392 (number-to-string (calendar-extract-year
393 (calendar-current-date)))))
394 (last (if (calendar-leap-year-p year) 366 365))
395 (day (calendar-read
396 (format "Day number (+/- 1-%d): " last)
397 (lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
398 (list year day)))
399 (calendar-goto-date
400 (calendar-gregorian-from-absolute
401 (if (< 0 day)
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)))
406 (provide 'cal-move)
408 ;;; cal-move.el ends here