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