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