Commit | Line | Data |
---|---|---|
0808d911 ER |
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 | |
b578f267 EN |
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. | |
0808d911 ER |
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 | ||
20eff799 RS |
39 | (require 'calendar) |
40 | ||
0808d911 ER |
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 | ||
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 | ||
69 | (defun calendar-forward-year (arg) | |
70 | "Move the cursor forward by ARG years. | |
71 | Movement is backward if ARG is negative." | |
72 | (interactive "p") | |
73 | (calendar-forward-month (* 12 arg))) | |
74 | ||
75 | (defun calendar-backward-month (arg) | |
76 | "Move the cursor backward by ARG months. | |
77 | Movement is forward if ARG is negative." | |
78 | (interactive "p") | |
79 | (calendar-forward-month (- arg))) | |
80 | ||
81 | (defun calendar-backward-year (arg) | |
82 | "Move the cursor backward ARG years. | |
83 | Movement is forward is ARG is negative." | |
84 | (interactive "p") | |
85 | (calendar-forward-month (* -12 arg))) | |
86 | ||
87 | (defun scroll-calendar-left (arg) | |
88 | "Scroll the displayed calendar left by ARG months. | |
89 | If ARG is negative the calendar is scrolled right. Maintains the relative | |
90 | position of the cursor with respect to the calendar as well as possible." | |
91 | (interactive "p") | |
92 | (calendar-cursor-to-nearest-date) | |
93 | (let ((old-date (calendar-cursor-to-date)) | |
94 | (today (calendar-current-date))) | |
95 | (if (/= arg 0) | |
9aa84f7f AS |
96 | (let ((month displayed-month) |
97 | (year displayed-year)) | |
98 | (increment-calendar-month month year arg) | |
99 | (generate-calendar-window month year) | |
0808d911 ER |
100 | (calendar-cursor-to-visible-date |
101 | (cond | |
102 | ((calendar-date-is-visible-p old-date) old-date) | |
103 | ((calendar-date-is-visible-p today) today) | |
9aa84f7f | 104 | (t (list month 1 year)))))))) |
0808d911 ER |
105 | |
106 | (defun scroll-calendar-right (arg) | |
107 | "Scroll the displayed calendar window right by ARG months. | |
108 | If ARG is negative the calendar is scrolled left. Maintains the relative | |
109 | position of the cursor with respect to the calendar as well as possible." | |
110 | (interactive "p") | |
111 | (scroll-calendar-left (- arg))) | |
112 | ||
113 | (defun scroll-calendar-left-three-months (arg) | |
114 | "Scroll the displayed calendar window left by 3*ARG months. | |
115 | If ARG is negative the calendar is scrolled right. Maintains the relative | |
116 | position of the cursor with respect to the calendar as well as possible." | |
117 | (interactive "p") | |
118 | (scroll-calendar-left (* 3 arg))) | |
119 | ||
120 | (defun scroll-calendar-right-three-months (arg) | |
121 | "Scroll the displayed calendar window right by 3*ARG months. | |
122 | If ARG is negative the calendar is scrolled left. Maintains the relative | |
123 | position of the cursor with respect to the calendar as well as possible." | |
124 | (interactive "p") | |
125 | (scroll-calendar-left (* -3 arg))) | |
126 | ||
127 | (defun calendar-cursor-to-nearest-date () | |
128 | "Move the cursor to the closest date. | |
129 | The position of the cursor is unchanged if it is already on a date. | |
130 | Returns the list (month day year) giving the cursor position." | |
131 | (let ((date (calendar-cursor-to-date)) | |
132 | (column (current-column))) | |
133 | (if date | |
134 | date | |
135 | (if (> 3 (count-lines (point-min) (point))) | |
136 | (progn | |
137 | (goto-line 3) | |
138 | (move-to-column column))) | |
139 | (if (not (looking-at "[0-9]")) | |
140 | (if (and (not (looking-at " *$")) | |
141 | (or (< column 25) | |
142 | (and (> column 27) | |
143 | (< column 50)) | |
144 | (and (> column 52) | |
145 | (< column 75)))) | |
146 | (progn | |
147 | (re-search-forward "[0-9]" nil t) | |
148 | (backward-char 1)) | |
149 | (re-search-backward "[0-9]" nil t))) | |
150 | (calendar-cursor-to-date)))) | |
151 | ||
152 | (defun calendar-forward-day (arg) | |
153 | "Move the cursor forward ARG days. | |
154 | Moves backward if ARG is negative." | |
155 | (interactive "p") | |
156 | (if (/= 0 arg) | |
157 | (let* | |
158 | ((cursor-date (calendar-cursor-to-date)) | |
159 | (cursor-date (if cursor-date | |
160 | cursor-date | |
161 | (if (> arg 0) (setq arg (1- arg))) | |
162 | (calendar-cursor-to-nearest-date))) | |
163 | (new-cursor-date | |
164 | (calendar-gregorian-from-absolute | |
165 | (+ (calendar-absolute-from-gregorian cursor-date) arg))) | |
166 | (new-display-month (extract-calendar-month new-cursor-date)) | |
167 | (new-display-year (extract-calendar-year new-cursor-date))) | |
168 | ;; Put the new month on the screen, if needed, and go to the new date. | |
169 | (if (not (calendar-date-is-visible-p new-cursor-date)) | |
170 | (calendar-other-month new-display-month new-display-year)) | |
171 | (calendar-cursor-to-visible-date new-cursor-date)))) | |
172 | ||
173 | (defun calendar-backward-day (arg) | |
174 | "Move the cursor back ARG days. | |
175 | Moves forward if ARG is negative." | |
176 | (interactive "p") | |
177 | (calendar-forward-day (- arg))) | |
178 | ||
179 | (defun calendar-forward-week (arg) | |
180 | "Move the cursor forward ARG weeks. | |
181 | Moves backward if ARG is negative." | |
182 | (interactive "p") | |
183 | (calendar-forward-day (* arg 7))) | |
184 | ||
185 | (defun calendar-backward-week (arg) | |
186 | "Move the cursor back ARG weeks. | |
187 | Moves forward if ARG is negative." | |
188 | (interactive "p") | |
189 | (calendar-forward-day (* arg -7))) | |
190 | ||
191 | (defun calendar-beginning-of-week (arg) | |
192 | "Move the cursor back ARG calendar-week-start-day's." | |
193 | (interactive "p") | |
194 | (calendar-cursor-to-nearest-date) | |
195 | (let ((day (calendar-day-of-week (calendar-cursor-to-date)))) | |
196 | (calendar-backward-day | |
197 | (if (= day calendar-week-start-day) | |
198 | (* 7 arg) | |
199 | (+ (mod (- day calendar-week-start-day) 7) | |
200 | (* 7 (1- arg))))))) | |
201 | ||
202 | (defun calendar-end-of-week (arg) | |
203 | "Move the cursor forward ARG calendar-week-start-day+6's." | |
204 | (interactive "p") | |
205 | (calendar-cursor-to-nearest-date) | |
206 | (let ((day (calendar-day-of-week (calendar-cursor-to-date)))) | |
207 | (calendar-forward-day | |
208 | (if (= day (mod (1- calendar-week-start-day) 7)) | |
209 | (* 7 arg) | |
210 | (+ (- 6 (mod (- day calendar-week-start-day) 7)) | |
211 | (* 7 (1- arg))))))) | |
212 | ||
213 | (defun calendar-beginning-of-month (arg) | |
214 | "Move the cursor backward ARG month beginnings." | |
215 | (interactive "p") | |
216 | (calendar-cursor-to-nearest-date) | |
217 | (let* ((date (calendar-cursor-to-date)) | |
218 | (month (extract-calendar-month date)) | |
219 | (day (extract-calendar-day date)) | |
220 | (year (extract-calendar-year date))) | |
221 | (if (= day 1) | |
222 | (calendar-backward-month arg) | |
223 | (calendar-cursor-to-visible-date (list month 1 year)) | |
224 | (calendar-backward-month (1- arg))))) | |
225 | ||
226 | (defun calendar-end-of-month (arg) | |
227 | "Move the cursor forward ARG month ends." | |
228 | (interactive "p") | |
229 | (calendar-cursor-to-nearest-date) | |
230 | (let* ((date (calendar-cursor-to-date)) | |
231 | (month (extract-calendar-month date)) | |
232 | (day (extract-calendar-day date)) | |
233 | (year (extract-calendar-year date)) | |
234 | (last-day (calendar-last-day-of-month month year))) | |
235 | (if (/= day last-day) | |
236 | (progn | |
237 | (calendar-cursor-to-visible-date (list month last-day year)) | |
238 | (setq arg (1- arg)))) | |
239 | (increment-calendar-month month year arg) | |
240 | (let ((last-day (list | |
241 | month | |
242 | (calendar-last-day-of-month month year) | |
243 | year))) | |
244 | (if (not (calendar-date-is-visible-p last-day)) | |
245 | (calendar-other-month month year) | |
246 | (calendar-cursor-to-visible-date last-day))))) | |
247 | ||
248 | (defun calendar-beginning-of-year (arg) | |
249 | "Move the cursor backward ARG year beginnings." | |
250 | (interactive "p") | |
251 | (calendar-cursor-to-nearest-date) | |
252 | (let* ((date (calendar-cursor-to-date)) | |
253 | (month (extract-calendar-month date)) | |
254 | (day (extract-calendar-day date)) | |
255 | (year (extract-calendar-year date)) | |
256 | (jan-first (list 1 1 year))) | |
257 | (if (and (= day 1) (= 1 month)) | |
258 | (calendar-backward-month (* 12 arg)) | |
259 | (if (and (= arg 1) | |
260 | (calendar-date-is-visible-p jan-first)) | |
261 | (calendar-cursor-to-visible-date jan-first) | |
262 | (calendar-other-month 1 (- year (1- arg))))))) | |
263 | ||
264 | (defun calendar-end-of-year (arg) | |
265 | "Move the cursor forward ARG year beginnings." | |
266 | (interactive "p") | |
267 | (calendar-cursor-to-nearest-date) | |
268 | (let* ((date (calendar-cursor-to-date)) | |
269 | (month (extract-calendar-month date)) | |
270 | (day (extract-calendar-day date)) | |
271 | (year (extract-calendar-year date)) | |
272 | (dec-31 (list 12 31 year))) | |
273 | (if (and (= day 31) (= 12 month)) | |
274 | (calendar-forward-month (* 12 arg)) | |
275 | (if (and (= arg 1) | |
276 | (calendar-date-is-visible-p dec-31)) | |
277 | (calendar-cursor-to-visible-date dec-31) | |
278 | (calendar-other-month 12 (- year (1- arg))) | |
279 | (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))) | |
280 | ||
281 | (defun calendar-cursor-to-visible-date (date) | |
282 | "Move the cursor to DATE that is on the screen." | |
283 | (let* ((month (extract-calendar-month date)) | |
284 | (day (extract-calendar-day date)) | |
285 | (year (extract-calendar-year date)) | |
286 | (first-of-month-weekday (calendar-day-of-week (list month 1 year)))) | |
287 | (goto-line (+ 3 | |
288 | (/ (+ day -1 | |
289 | (mod | |
290 | (- (calendar-day-of-week (list month 1 year)) | |
291 | calendar-week-start-day) | |
292 | 7)) | |
293 | 7))) | |
294 | (move-to-column (+ 6 | |
295 | (* 25 | |
296 | (1+ (calendar-interval | |
297 | displayed-month displayed-year month year))) | |
298 | (* 3 (mod | |
299 | (- (calendar-day-of-week date) | |
300 | calendar-week-start-day) | |
301 | 7)))))) | |
302 | ||
303 | (defun calendar-goto-date (date) | |
304 | "Move cursor to DATE." | |
305 | (interactive (list (calendar-read-date))) | |
306 | (let ((month (extract-calendar-month date)) | |
307 | (year (extract-calendar-year date))) | |
308 | (if (not (calendar-date-is-visible-p date)) | |
309 | (calendar-other-month | |
310 | (if (and (= month 1) (= year 1)) | |
311 | 2 | |
312 | month) | |
313 | year))) | |
314 | (calendar-cursor-to-visible-date date)) | |
315 | ||
316 | (provide 'cal-move) | |
317 | ||
318 | ;;; cal-move.el ends here |