]> code.delx.au - gnu-emacs/blob - lisp/calendar/cal-move.el
Fix autoload for calendar-absolute-from-astro. Add autoload for
[gnu-emacs] / lisp / calendar / cal-move.el
1 ;;; cal-move.el --- calendar functions for movement in the calendar
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Keywords: calendar
7 ;; Human-Keywords: calendar
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This collection of functions implements movement in the calendar for
29 ;; calendar.el.
30
31 ;; Comments, corrections, and improvements should be sent to
32 ;; Edward M. Reingold Department of Computer Science
33 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
34 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
35 ;; Urbana, Illinois 61801
36
37 ;;; Code:
38
39 (require 'calendar)
40
41 (defun calendar-goto-today ()
42 "Reposition the calendar window so the current date is visible."
43 (interactive)
44 (let ((today (calendar-current-date)));; The date might have changed.
45 (if (not (calendar-date-is-visible-p today))
46 (generate-calendar-window)
47 (update-calendar-mode-line)
48 (calendar-cursor-to-visible-date today)))
49 (run-hooks 'calendar-move-hook))
50
51 (defun calendar-forward-month (arg)
52 "Move the cursor forward ARG months.
53 Movement is backward if ARG is negative."
54 (interactive "p")
55 (calendar-cursor-to-nearest-date)
56 (let* ((cursor-date (calendar-cursor-to-date t))
57 (month (extract-calendar-month cursor-date))
58 (day (extract-calendar-day cursor-date))
59 (year (extract-calendar-year cursor-date)))
60 (increment-calendar-month month year arg)
61 (let ((last (calendar-last-day-of-month month year)))
62 (if (< last day)
63 (setq day last)))
64 ;; Put the new month on the screen, if needed, and go to the new date.
65 (let ((new-cursor-date (list month day year)))
66 (if (not (calendar-date-is-visible-p new-cursor-date))
67 (calendar-other-month month year))
68 (calendar-cursor-to-visible-date new-cursor-date)))
69 (run-hooks 'calendar-move-hook))
70
71 (defun calendar-forward-year (arg)
72 "Move the cursor forward by ARG years.
73 Movement is backward if ARG is negative."
74 (interactive "p")
75 (calendar-forward-month (* 12 arg)))
76
77 (defun calendar-backward-month (arg)
78 "Move the cursor backward by ARG months.
79 Movement is forward if ARG is negative."
80 (interactive "p")
81 (calendar-forward-month (- arg)))
82
83 (defun calendar-backward-year (arg)
84 "Move the cursor backward ARG years.
85 Movement is forward is ARG is negative."
86 (interactive "p")
87 (calendar-forward-month (* -12 arg)))
88
89 (defun scroll-calendar-left (arg)
90 "Scroll the displayed calendar left by ARG months.
91 If ARG is negative the calendar is scrolled right. Maintains the relative
92 position of the cursor with respect to the calendar as well as possible."
93 (interactive "p")
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))
108
109 (defun scroll-calendar-right (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 (- arg)))
115
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)))
122
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)))
129
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))))
154
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))
176
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)))
182
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)))
188
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)))
194
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)))))))
205
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)))))))
216
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)))))
229
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))
252
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 (run-hooks 'calendar-move-hook))
270
271 (defun calendar-end-of-year (arg)
272 "Move the cursor forward ARG year beginnings."
273 (interactive "p")
274 (calendar-cursor-to-nearest-date)
275 (let* ((date (calendar-cursor-to-date))
276 (month (extract-calendar-month date))
277 (day (extract-calendar-day date))
278 (year (extract-calendar-year date))
279 (dec-31 (list 12 31 year))
280 (calendar-move-hook nil))
281 (if (and (= day 31) (= 12 month))
282 (calendar-forward-month (* 12 arg))
283 (if (and (= arg 1)
284 (calendar-date-is-visible-p dec-31))
285 (calendar-cursor-to-visible-date dec-31)
286 (calendar-other-month 12 (- year (1- arg)))
287 (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
288 (run-hooks 'calendar-move-hook))
289
290 (defun calendar-cursor-to-visible-date (date)
291 "Move the cursor to DATE that is on the screen."
292 (let* ((month (extract-calendar-month date))
293 (day (extract-calendar-day date))
294 (year (extract-calendar-year date))
295 (first-of-month-weekday (calendar-day-of-week (list month 1 year))))
296 (goto-line (+ 3
297 (/ (+ day -1
298 (mod
299 (- (calendar-day-of-week (list month 1 year))
300 calendar-week-start-day)
301 7))
302 7)))
303 (move-to-column (+ 6
304 (* 25
305 (1+ (calendar-interval
306 displayed-month displayed-year month year)))
307 (* 3 (mod
308 (- (calendar-day-of-week date)
309 calendar-week-start-day)
310 7))))))
311
312 (defun calendar-goto-date (date)
313 "Move cursor to DATE."
314 (interactive (list (calendar-read-date)))
315 (let ((month (extract-calendar-month date))
316 (year (extract-calendar-year date)))
317 (if (not (calendar-date-is-visible-p date))
318 (calendar-other-month
319 (if (and (= month 1) (= year 1))
320 2
321 month)
322 year)))
323 (calendar-cursor-to-visible-date date)
324 (run-hooks 'calendar-move-hook))
325
326 (provide 'cal-move)
327
328 ;;; cal-move.el ends here