(frame.o, keyboard.o, xdisp.o, xfaces.o): Depend on macgui.h.
[emacs.git] / lisp / calendar / cal-move.el
blob8361fa6beb36f08e1c9606c888d9061bbf87a1a6
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 ;; This collection of functions implements movement in the calendar for
31 ;; calendar.el.
33 ;;; Code:
35 (defvar displayed-month)
36 (defvar displayed-year)
38 (require 'calendar)
40 (defun calendar-goto-today ()
41 "Reposition the calendar window so the current date is visible."
42 (interactive)
43 (let ((today (calendar-current-date)));; The date might have changed.
44 (if (not (calendar-date-is-visible-p today))
45 (generate-calendar-window)
46 (update-calendar-mode-line)
47 (calendar-cursor-to-visible-date today)))
48 (run-hooks 'calendar-move-hook))
50 (defun calendar-forward-month (arg)
51 "Move the cursor forward ARG months.
52 Movement is backward if ARG is negative."
53 (interactive "p")
54 (calendar-cursor-to-nearest-date)
55 (let* ((cursor-date (calendar-cursor-to-date t))
56 (month (extract-calendar-month cursor-date))
57 (day (extract-calendar-day cursor-date))
58 (year (extract-calendar-year cursor-date)))
59 (increment-calendar-month month year arg)
60 (let ((last (calendar-last-day-of-month month year)))
61 (if (< last day)
62 (setq day last)))
63 ;; Put the new month on the screen, if needed, and go to the new date.
64 (let ((new-cursor-date (list month day year)))
65 (if (not (calendar-date-is-visible-p new-cursor-date))
66 (calendar-other-month month year))
67 (calendar-cursor-to-visible-date new-cursor-date)))
68 (run-hooks 'calendar-move-hook))
70 (defun calendar-forward-year (arg)
71 "Move the cursor forward by ARG years.
72 Movement is backward if ARG is negative."
73 (interactive "p")
74 (calendar-forward-month (* 12 arg)))
76 (defun calendar-backward-month (arg)
77 "Move the cursor backward by ARG months.
78 Movement is forward if ARG is negative."
79 (interactive "p")
80 (calendar-forward-month (- arg)))
82 (defun calendar-backward-year (arg)
83 "Move the cursor backward ARG years.
84 Movement is forward is ARG is negative."
85 (interactive "p")
86 (calendar-forward-month (* -12 arg)))
88 (defun scroll-calendar-left (&optional arg)
89 "Scroll the displayed calendar left by ARG months.
90 If ARG is negative the calendar is scrolled right. Maintains the relative
91 position of the cursor with respect to the calendar as well as possible."
92 (interactive "p")
93 (unless arg (setq arg 1))
94 (calendar-cursor-to-nearest-date)
95 (let ((old-date (calendar-cursor-to-date))
96 (today (calendar-current-date)))
97 (if (/= arg 0)
98 (let ((month displayed-month)
99 (year displayed-year))
100 (increment-calendar-month month year arg)
101 (generate-calendar-window month year)
102 (calendar-cursor-to-visible-date
103 (cond
104 ((calendar-date-is-visible-p old-date) old-date)
105 ((calendar-date-is-visible-p today) today)
106 (t (list month 1 year)))))))
107 (run-hooks 'calendar-move-hook))
109 (defun scroll-calendar-right (&optional arg)
110 "Scroll the displayed calendar window right by ARG months.
111 If ARG is negative the calendar is scrolled left. Maintains the relative
112 position of the cursor with respect to the calendar as well as possible."
113 (interactive "p")
114 (scroll-calendar-left (- (or arg 1))))
116 (defun scroll-calendar-left-three-months (arg)
117 "Scroll the displayed calendar window left by 3*ARG months.
118 If ARG is negative the calendar is scrolled right. Maintains the relative
119 position of the cursor with respect to the calendar as well as possible."
120 (interactive "p")
121 (scroll-calendar-left (* 3 arg)))
123 (defun scroll-calendar-right-three-months (arg)
124 "Scroll the displayed calendar window right by 3*ARG months.
125 If ARG is negative the calendar is scrolled left. Maintains the relative
126 position of the cursor with respect to the calendar as well as possible."
127 (interactive "p")
128 (scroll-calendar-left (* -3 arg)))
130 (defun calendar-cursor-to-nearest-date ()
131 "Move the cursor to the closest date.
132 The position of the cursor is unchanged if it is already on a date.
133 Returns the list (month day year) giving the cursor position."
134 (let ((date (calendar-cursor-to-date))
135 (column (current-column)))
136 (if date
137 date
138 (if (> 3 (count-lines (point-min) (point)))
139 (progn
140 (goto-line 3)
141 (move-to-column column)))
142 (if (not (looking-at "[0-9]"))
143 (if (and (not (looking-at " *$"))
144 (or (< column 25)
145 (and (> column 27)
146 (< column 50))
147 (and (> column 52)
148 (< column 75))))
149 (progn
150 (re-search-forward "[0-9]" nil t)
151 (backward-char 1))
152 (re-search-backward "[0-9]" nil t)))
153 (calendar-cursor-to-date))))
155 (defun calendar-forward-day (arg)
156 "Move the cursor forward ARG days.
157 Moves backward if ARG is negative."
158 (interactive "p")
159 (if (/= 0 arg)
160 (let*
161 ((cursor-date (calendar-cursor-to-date))
162 (cursor-date (if cursor-date
163 cursor-date
164 (if (> arg 0) (setq arg (1- arg)))
165 (calendar-cursor-to-nearest-date)))
166 (new-cursor-date
167 (calendar-gregorian-from-absolute
168 (+ (calendar-absolute-from-gregorian cursor-date) arg)))
169 (new-display-month (extract-calendar-month new-cursor-date))
170 (new-display-year (extract-calendar-year new-cursor-date)))
171 ;; Put the new month on the screen, if needed, and go to the new date.
172 (if (not (calendar-date-is-visible-p new-cursor-date))
173 (calendar-other-month new-display-month new-display-year))
174 (calendar-cursor-to-visible-date new-cursor-date)))
175 (run-hooks 'calendar-move-hook))
177 (defun calendar-backward-day (arg)
178 "Move the cursor back ARG days.
179 Moves forward if ARG is negative."
180 (interactive "p")
181 (calendar-forward-day (- arg)))
183 (defun calendar-forward-week (arg)
184 "Move the cursor forward ARG weeks.
185 Moves backward if ARG is negative."
186 (interactive "p")
187 (calendar-forward-day (* arg 7)))
189 (defun calendar-backward-week (arg)
190 "Move the cursor back ARG weeks.
191 Moves forward if ARG is negative."
192 (interactive "p")
193 (calendar-forward-day (* arg -7)))
195 (defun calendar-beginning-of-week (arg)
196 "Move the cursor back ARG calendar-week-start-day's."
197 (interactive "p")
198 (calendar-cursor-to-nearest-date)
199 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
200 (calendar-backward-day
201 (if (= day calendar-week-start-day)
202 (* 7 arg)
203 (+ (mod (- day calendar-week-start-day) 7)
204 (* 7 (1- arg)))))))
206 (defun calendar-end-of-week (arg)
207 "Move the cursor forward ARG calendar-week-start-day+6's."
208 (interactive "p")
209 (calendar-cursor-to-nearest-date)
210 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
211 (calendar-forward-day
212 (if (= day (mod (1- calendar-week-start-day) 7))
213 (* 7 arg)
214 (+ (- 6 (mod (- day calendar-week-start-day) 7))
215 (* 7 (1- arg)))))))
217 (defun calendar-beginning-of-month (arg)
218 "Move the cursor backward ARG month beginnings."
219 (interactive "p")
220 (calendar-cursor-to-nearest-date)
221 (let* ((date (calendar-cursor-to-date))
222 (month (extract-calendar-month date))
223 (day (extract-calendar-day date))
224 (year (extract-calendar-year date)))
225 (if (= day 1)
226 (calendar-backward-month arg)
227 (calendar-cursor-to-visible-date (list month 1 year))
228 (calendar-backward-month (1- arg)))))
230 (defun calendar-end-of-month (arg)
231 "Move the cursor forward ARG month ends."
232 (interactive "p")
233 (calendar-cursor-to-nearest-date)
234 (let* ((date (calendar-cursor-to-date))
235 (month (extract-calendar-month date))
236 (day (extract-calendar-day date))
237 (year (extract-calendar-year date))
238 (last-day (calendar-last-day-of-month month year)))
239 (if (/= day last-day)
240 (progn
241 (calendar-cursor-to-visible-date (list month last-day year))
242 (setq arg (1- arg))))
243 (increment-calendar-month month year arg)
244 (let ((last-day (list
245 month
246 (calendar-last-day-of-month month year)
247 year)))
248 (if (not (calendar-date-is-visible-p last-day))
249 (calendar-other-month month year)
250 (calendar-cursor-to-visible-date last-day))))
251 (run-hooks 'calendar-move-hook))
253 (defun calendar-beginning-of-year (arg)
254 "Move the cursor backward ARG year beginnings."
255 (interactive "p")
256 (calendar-cursor-to-nearest-date)
257 (let* ((date (calendar-cursor-to-date))
258 (month (extract-calendar-month date))
259 (day (extract-calendar-day date))
260 (year (extract-calendar-year date))
261 (jan-first (list 1 1 year))
262 (calendar-move-hook nil))
263 (if (and (= day 1) (= 1 month))
264 (calendar-backward-month (* 12 arg))
265 (if (and (= arg 1)
266 (calendar-date-is-visible-p jan-first))
267 (calendar-cursor-to-visible-date jan-first)
268 (calendar-other-month 1 (- year (1- arg)))
269 (calendar-cursor-to-visible-date (list 1 1 displayed-year)))))
270 (run-hooks 'calendar-move-hook))
272 (defun calendar-end-of-year (arg)
273 "Move the cursor forward ARG year beginnings."
274 (interactive "p")
275 (calendar-cursor-to-nearest-date)
276 (let* ((date (calendar-cursor-to-date))
277 (month (extract-calendar-month date))
278 (day (extract-calendar-day date))
279 (year (extract-calendar-year date))
280 (dec-31 (list 12 31 year))
281 (calendar-move-hook nil))
282 (if (and (= day 31) (= 12 month))
283 (calendar-forward-month (* 12 arg))
284 (if (and (= arg 1)
285 (calendar-date-is-visible-p dec-31))
286 (calendar-cursor-to-visible-date dec-31)
287 (calendar-other-month 12 (+ year (1- arg)))
288 (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
289 (run-hooks 'calendar-move-hook))
291 (defun calendar-cursor-to-visible-date (date)
292 "Move the cursor to DATE that is on the screen."
293 (let* ((month (extract-calendar-month date))
294 (day (extract-calendar-day date))
295 (year (extract-calendar-year date))
296 (first-of-month-weekday (calendar-day-of-week (list month 1 year))))
297 (goto-line (+ 3
298 (/ (+ day -1
299 (mod
300 (- (calendar-day-of-week (list month 1 year))
301 calendar-week-start-day)
303 7)))
304 (move-to-column (+ 6
305 (* 25
306 (1+ (calendar-interval
307 displayed-month displayed-year month year)))
308 (* 3 (mod
309 (- (calendar-day-of-week date)
310 calendar-week-start-day)
311 7))))))
313 (defun calendar-goto-date (date)
314 "Move cursor to DATE."
315 (interactive (list (calendar-read-date)))
316 (let ((month (extract-calendar-month date))
317 (year (extract-calendar-year date)))
318 (if (not (calendar-date-is-visible-p date))
319 (calendar-other-month
320 (if (and (= month 1) (= year 1))
322 month)
323 year)))
324 (calendar-cursor-to-visible-date date)
325 (run-hooks 'calendar-move-hook))
327 (defun calendar-goto-day-of-year (year day &optional noecho)
328 "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is t.
329 Negative DAY counts backward from end of year."
330 (interactive
331 (let* ((year (calendar-read
332 "Year (>0): "
333 (lambda (x) (> x 0))
334 (int-to-string (extract-calendar-year
335 (calendar-current-date)))))
336 (last (if (calendar-leap-year-p year) 366 365))
337 (day (calendar-read
338 (format "Day number (+/- 1-%d): " last)
339 '(lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
340 (list year day)))
341 (calendar-goto-date
342 (calendar-gregorian-from-absolute
343 (if (< 0 day)
344 (+ -1 day (calendar-absolute-from-gregorian (list 1 1 year)))
345 (+ 1 day (calendar-absolute-from-gregorian (list 12 31 year))))))
346 (or noecho (calendar-print-day-of-year)))
348 (provide 'cal-move)
350 ;;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
351 ;;; cal-move.el ends here