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