Replace int-to-string with number-to-string.
[emacs.git] / lisp / calendar / cal-move.el
blobcd59ed7ff281c2bdd86ef2e1f0fd284cd0c0e0a4
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>
8 ;; Keywords: calendar
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)
16 ;; 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; 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.
28 ;;; Commentary:
30 ;; See calendar.el.
32 ;;; Code:
34 (require 'calendar)
36 ;;;###cal-autoload
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)))
44 (goto-line 3)
45 (move-to-column column))
46 (if (not (looking-at "[0-9]"))
47 (if (and (not (looking-at " *$"))
48 (or (< column 25)
49 (and (> column 27)
50 (< column 50))
51 (and (> column 52)
52 (< column 75))))
53 (progn
54 (re-search-forward "[0-9]" nil t)
55 (backward-char 1))
56 (re-search-backward "[0-9]" nil t)))
57 (calendar-cursor-to-date))))
59 (defvar displayed-month) ; from calendar-generate
60 (defvar displayed-year)
62 ;;;###cal-autoload
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)))
68 (goto-line (+ 3
69 (/ (+ day -1
70 (mod
71 (- (calendar-day-of-week (list month 1 year))
72 calendar-week-start-day)
73 7))
74 7)))
75 (move-to-column (+ 6
76 (* 25
77 (1+ (calendar-interval
78 displayed-month displayed-year month year)))
79 (* 3 (mod
80 (- (calendar-day-of-week date)
81 calendar-week-start-day)
82 7))))))
84 ;;;###cal-autoload
85 (defun calendar-goto-today ()
86 "Reposition the calendar window so the current date is visible."
87 (interactive)
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))
95 ;;;###cal-autoload
96 (defun calendar-forward-month (arg)
97 "Move the cursor forward ARG months.
98 Movement is backward if ARG is negative."
99 (interactive "p")
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))
105 (last (progn
106 (calendar-increment-month month year arg)
107 (calendar-last-day-of-month month year)))
108 (day (min last day))
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))
116 ;;;###cal-autoload
117 (defun calendar-forward-year (arg)
118 "Move the cursor forward by ARG years.
119 Movement is backward if ARG is negative."
120 (interactive "p")
121 (calendar-forward-month (* 12 arg)))
123 ;;;###cal-autoload
124 (defun calendar-backward-month (arg)
125 "Move the cursor backward by ARG months.
126 Movement is forward if ARG is negative."
127 (interactive "p")
128 (calendar-forward-month (- arg)))
130 ;;;###cal-autoload
131 (defun calendar-backward-year (arg)
132 "Move the cursor backward ARG years.
133 Movement is forward is ARG is negative."
134 (interactive "p")
135 (calendar-forward-month (* -12 arg)))
137 ;;;###cal-autoload
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)
144 last-nonmenu-event))
145 (unless arg (setq arg 1))
146 (save-selected-window
147 (select-window (posn-window (event-start event)))
148 (calendar-cursor-to-nearest-date)
149 (unless (zerop arg)
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
157 (cond
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")
166 ;;;###cal-autoload
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)
173 last-nonmenu-event))
174 (calendar-scroll-left (- (or arg 1)) event))
176 (define-obsolete-function-alias
177 'scroll-calendar-right 'calendar-scroll-right "23.1")
179 ;;;###cal-autoload
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."
184 (interactive "p")
185 (calendar-scroll-left (* 3 arg)))
187 (define-obsolete-function-alias 'scroll-calendar-left-three-months
188 'calendar-scroll-left-three-months "23.1")
190 ;;;###cal-autoload
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."
195 (interactive "p")
196 (calendar-scroll-left (* -3 arg)))
198 (define-obsolete-function-alias 'scroll-calendar-right-three-months
199 'calendar-scroll-right-three-months "23.1")
201 ;;;###cal-autoload
202 (defun calendar-forward-day (arg)
203 "Move the cursor forward ARG days.
204 Moves backward if ARG is negative."
205 (interactive "p")
206 (unless (zerop arg)
207 (let* ((cursor-date (or (calendar-cursor-to-date)
208 (progn
209 (if (> arg 0) (setq arg (1- arg)))
210 (calendar-cursor-to-nearest-date))))
211 (new-cursor-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))
222 ;;;###cal-autoload
223 (defun calendar-backward-day (arg)
224 "Move the cursor back ARG days.
225 Moves forward if ARG is negative."
226 (interactive "p")
227 (calendar-forward-day (- arg)))
229 ;;;###cal-autoload
230 (defun calendar-forward-week (arg)
231 "Move the cursor forward ARG weeks.
232 Moves backward if ARG is negative."
233 (interactive "p")
234 (calendar-forward-day (* arg 7)))
236 ;;;###cal-autoload
237 (defun calendar-backward-week (arg)
238 "Move the cursor back ARG weeks.
239 Moves forward if ARG is negative."
240 (interactive "p")
241 (calendar-forward-day (* arg -7)))
243 ;;;###cal-autoload
244 (defun calendar-beginning-of-week (arg)
245 "Move the cursor back ARG calendar-week-start-day's."
246 (interactive "p")
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)
251 (* 7 arg)
252 (+ (mod (- day calendar-week-start-day) 7)
253 (* 7 (1- arg)))))))
255 ;;;###cal-autoload
256 (defun calendar-end-of-week (arg)
257 "Move the cursor forward ARG calendar-week-start-day+6's."
258 (interactive "p")
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))
263 (* 7 arg)
264 (+ (- 6 (mod (- day calendar-week-start-day) 7))
265 (* 7 (1- arg)))))))
267 ;;;###cal-autoload
268 (defun calendar-beginning-of-month (arg)
269 "Move the cursor backward ARG month beginnings."
270 (interactive "p")
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)))
276 (if (= day 1)
277 (calendar-backward-month arg)
278 (calendar-cursor-to-visible-date (list month 1 year))
279 (calendar-backward-month (1- arg)))))
281 ;;;###cal-autoload
282 (defun calendar-end-of-month (arg)
283 "Move the cursor forward ARG month ends."
284 (interactive "p")
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))
291 (last-day (progn
292 (unless (= day last-day)
293 (calendar-cursor-to-visible-date
294 (list month last-day year))
295 (setq arg (1- arg)))
296 (calendar-increment-month month year arg)
297 (list month
298 (calendar-last-day-of-month month year)
299 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))
305 ;;;###cal-autoload
306 (defun calendar-beginning-of-year (arg)
307 "Move the cursor backward ARG year beginnings."
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 (jan-first (list 1 1 year))
315 (calendar-move-hook nil))
316 (if (and (= day 1) (= 1 month))
317 (calendar-backward-month (* 12 arg))
318 (if (and (= arg 1)
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))
325 ;;;###cal-autoload
326 (defun calendar-end-of-year (arg)
327 "Move the cursor forward ARG year beginnings."
328 (interactive "p")
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))
338 (if (and (= arg 1)
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))
345 ;;;###cal-autoload
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))
355 month)
356 year)))
357 (calendar-cursor-to-visible-date date)
358 (run-hooks 'calendar-move-hook))
360 ;;;###cal-autoload
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."
364 (interactive
365 (let* ((year (calendar-read
366 "Year (>0): "
367 (lambda (x) (> x 0))
368 (number-to-string (calendar-extract-year
369 (calendar-current-date)))))
370 (last (if (calendar-leap-year-p year) 366 365))
371 (day (calendar-read
372 (format "Day number (+/- 1-%d): " last)
373 (lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
374 (list year day)))
375 (calendar-goto-date
376 (calendar-gregorian-from-absolute
377 (if (< 0 day)
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)))
382 (provide 'cal-move)
384 ;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
385 ;;; cal-move.el ends here