Initial Commit
[packages] / xemacs-packages / calendar / cal-move.el
1 ;;; cal-move.el --- calendar functions for movement in the calendar
2
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: calendar
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Synched up with: FSF Emacs 22 CVS 2007-03-30
29
30 ;;; Commentary:
31
32 ;; This collection of functions implements movement in the calendar for
33 ;; calendar.el.
34
35 ;; Comments, corrections, and improvements should be sent to
36 ;;  Edward M. Reingold               Department of Computer Science
37 ;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
38 ;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
39 ;;                                   Urbana, Illinois 61801
40
41 ;;; Code:
42
43 (defvar displayed-month)
44 (defvar displayed-year)
45
46 (require 'calendar)
47
48 (defun calendar-goto-today ()
49   "Reposition the calendar window so the current date is visible."
50   (interactive)
51   (let ((today (calendar-current-date)));; The date might have changed.
52     (if (not (calendar-date-is-visible-p today))
53         (generate-calendar-window)
54       (update-calendar-mode-line)
55       (calendar-cursor-to-visible-date today)))
56   (run-hooks 'calendar-move-hook))
57
58 (defun calendar-forward-month (arg)
59   "Move the cursor forward ARG months.
60 Movement is backward if ARG is negative."
61   (interactive "p")
62   (calendar-cursor-to-nearest-date)
63   (let* ((cursor-date (calendar-cursor-to-date t))
64          (month (extract-calendar-month cursor-date))
65          (day (extract-calendar-day cursor-date))
66          (year (extract-calendar-year cursor-date)))
67     (increment-calendar-month month year arg)
68     (let ((last (calendar-last-day-of-month month year)))
69       (if (< last day)
70         (setq day last)))
71     ;; Put the new month on the screen, if needed, and go to the new date.
72     (let ((new-cursor-date (list month day year)))
73       (if (not (calendar-date-is-visible-p new-cursor-date))
74           (calendar-other-month month year))
75       (calendar-cursor-to-visible-date new-cursor-date)))
76   (run-hooks 'calendar-move-hook))
77
78 (defun calendar-forward-year (arg)
79   "Move the cursor forward by ARG years.
80 Movement is backward if ARG is negative."
81   (interactive "p")
82   (calendar-forward-month (* 12 arg)))
83
84 (defun calendar-backward-month (arg)
85   "Move the cursor backward by ARG months.
86 Movement is forward if ARG is negative."
87   (interactive "p")
88   (calendar-forward-month (- arg)))
89
90 (defun calendar-backward-year (arg)
91   "Move the cursor backward ARG years.
92 Movement is forward is ARG is negative."
93   (interactive "p")
94   (calendar-forward-month (* -12 arg)))
95
96 (defun scroll-calendar-left (&optional arg)
97   "Scroll the displayed calendar left by ARG months.
98 If ARG is negative the calendar is scrolled right.  Maintains the relative
99 position of the cursor with respect to the calendar as well as possible."
100   (interactive "p")
101   (unless arg (setq arg 1))
102   (calendar-cursor-to-nearest-date)
103   (let ((old-date (calendar-cursor-to-date))
104         (today (calendar-current-date)))
105     (if (/= arg 0)
106         (let ((month displayed-month)
107               (year displayed-year))
108           (increment-calendar-month month year arg)
109           (generate-calendar-window month year)
110           (calendar-cursor-to-visible-date
111            (cond
112             ((calendar-date-is-visible-p old-date) old-date)
113             ((calendar-date-is-visible-p today) today)
114             (t (list month 1 year)))))))
115   (run-hooks 'calendar-move-hook))
116
117 (defun scroll-calendar-right (&optional arg)
118   "Scroll the displayed calendar window right by 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 (- (or arg 1))))
123
124 (defun scroll-calendar-left-three-months (arg)
125   "Scroll the displayed calendar window left by 3*ARG months.
126 If ARG is negative the calendar is scrolled right.  Maintains the relative
127 position of the cursor with respect to the calendar as well as possible."
128   (interactive "p")
129   (scroll-calendar-left (* 3 arg)))
130
131 (defun scroll-calendar-right-three-months (arg)
132   "Scroll the displayed calendar window right by 3*ARG months.
133 If ARG is negative the calendar is scrolled left.  Maintains the relative
134 position of the cursor with respect to the calendar as well as possible."
135   (interactive "p")
136   (scroll-calendar-left (* -3 arg)))
137
138 (defun calendar-cursor-to-nearest-date ()
139   "Move the cursor to the closest date.
140 The position of the cursor is unchanged if it is already on a date.
141 Returns the list (month day year) giving the cursor position."
142   (let ((date (calendar-cursor-to-date))
143         (column (current-column)))
144     (if date
145         date
146       (if (> 3 (count-lines (point-min) (point)))
147           (progn
148             (goto-line 3)
149             (move-to-column column)))
150       (if (not (looking-at "[0-9]"))
151           (if (and (not (looking-at " *$"))
152                    (or (< column 25)
153                        (and (> column 27)
154                             (< column 50))
155                        (and (> column 52)
156                             (< column 75))))
157               (progn
158                 (re-search-forward "[0-9]" nil t)
159                 (backward-char 1))
160             (re-search-backward "[0-9]" nil t)))
161       (calendar-cursor-to-date))))
162
163 (defun calendar-forward-day (arg)
164   "Move the cursor forward ARG days.
165 Moves backward if ARG is negative."
166   (interactive "p")
167   (if (/= 0 arg)
168       (let*
169           ((cursor-date (calendar-cursor-to-date))
170            (cursor-date (if cursor-date
171                             cursor-date
172                           (if (> arg 0) (setq arg (1- arg)))
173                           (calendar-cursor-to-nearest-date)))
174            (new-cursor-date
175             (calendar-gregorian-from-absolute
176              (+ (calendar-absolute-from-gregorian cursor-date) arg)))
177            (new-display-month (extract-calendar-month new-cursor-date))
178            (new-display-year (extract-calendar-year new-cursor-date)))
179         ;; Put the new month on the screen, if needed, and go to the new date.
180         (if (not (calendar-date-is-visible-p new-cursor-date))
181             (calendar-other-month new-display-month new-display-year))
182         (calendar-cursor-to-visible-date new-cursor-date)))
183   (run-hooks 'calendar-move-hook))
184
185 (defun calendar-backward-day (arg)
186   "Move the cursor back ARG days.
187 Moves forward if ARG is negative."
188   (interactive "p")
189   (calendar-forward-day (- arg)))
190
191 (defun calendar-forward-week (arg)
192   "Move the cursor forward ARG weeks.
193 Moves backward if ARG is negative."
194   (interactive "p")
195   (calendar-forward-day (* arg 7)))
196
197 (defun calendar-backward-week (arg)
198   "Move the cursor back ARG weeks.
199 Moves forward if ARG is negative."
200   (interactive "p")
201   (calendar-forward-day (* arg -7)))
202
203 (defun calendar-beginning-of-week (arg)
204   "Move the cursor back ARG calendar-week-start-day's."
205   (interactive "p")
206   (calendar-cursor-to-nearest-date)
207   (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
208     (calendar-backward-day
209      (if (= day calendar-week-start-day)
210          (* 7 arg)
211        (+ (mod (- day calendar-week-start-day) 7)
212           (* 7 (1- arg)))))))
213
214 (defun calendar-end-of-week (arg)
215   "Move the cursor forward ARG calendar-week-start-day+6's."
216   (interactive "p")
217   (calendar-cursor-to-nearest-date)
218   (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
219     (calendar-forward-day
220      (if (= day (mod (1- calendar-week-start-day) 7))
221          (* 7 arg)
222        (+ (- 6 (mod (- day calendar-week-start-day) 7))
223           (* 7 (1- arg)))))))
224
225 (defun calendar-beginning-of-month (arg)
226   "Move the cursor backward ARG month beginnings."
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     (if (= day 1)
234         (calendar-backward-month arg)
235       (calendar-cursor-to-visible-date (list month 1 year))
236       (calendar-backward-month (1- arg)))))
237
238 (defun calendar-end-of-month (arg)
239   "Move the cursor forward ARG month ends."
240   (interactive "p")
241   (calendar-cursor-to-nearest-date)
242   (let* ((date (calendar-cursor-to-date))
243          (month (extract-calendar-month date))
244          (day (extract-calendar-day date))
245          (year (extract-calendar-year date))
246          (last-day (calendar-last-day-of-month month year)))
247     (if (/= day last-day)
248         (progn
249           (calendar-cursor-to-visible-date (list month last-day year))
250           (setq arg (1- arg))))
251     (increment-calendar-month month year arg)
252     (let ((last-day (list
253                      month
254                      (calendar-last-day-of-month month year)
255                      year)))
256       (if (not (calendar-date-is-visible-p last-day))
257           (calendar-other-month month year)
258       (calendar-cursor-to-visible-date last-day))))
259   (run-hooks 'calendar-move-hook))
260
261 (defun calendar-beginning-of-year (arg)
262   "Move the cursor backward 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          (jan-first (list 1 1 year))
270          (calendar-move-hook nil))
271     (if (and (= day 1) (= 1 month))
272         (calendar-backward-month (* 12 arg))
273       (if (and (= arg 1)
274                (calendar-date-is-visible-p jan-first))
275           (calendar-cursor-to-visible-date jan-first)
276         (calendar-other-month 1 (- year (1- arg)))
277         (calendar-cursor-to-visible-date (list 1 1 displayed-year)))))
278   (run-hooks 'calendar-move-hook))
279
280 (defun calendar-end-of-year (arg)
281   "Move the cursor forward ARG year beginnings."
282   (interactive "p")
283   (calendar-cursor-to-nearest-date)
284   (let* ((date (calendar-cursor-to-date))
285          (month (extract-calendar-month date))
286          (day (extract-calendar-day date))
287          (year (extract-calendar-year date))
288          (dec-31 (list 12 31 year))
289          (calendar-move-hook nil))
290     (if (and (= day 31) (= 12 month))
291         (calendar-forward-month (* 12 arg))
292       (if (and (= arg 1)
293                (calendar-date-is-visible-p dec-31))
294           (calendar-cursor-to-visible-date dec-31)
295         (calendar-other-month 12 (+ year (1- arg)))
296         (calendar-cursor-to-visible-date (list 12 31 displayed-year)))))
297   (run-hooks 'calendar-move-hook))
298
299 (defun calendar-cursor-to-visible-date (date)
300   "Move the cursor to DATE that is on the screen."
301   (let* ((month (extract-calendar-month date))
302          (day (extract-calendar-day date))
303          (year (extract-calendar-year date))
304          (first-of-month-weekday (calendar-day-of-week (list month 1 year))))
305     (goto-line (+ 3
306                   (/ (+ day  -1
307                         (mod
308                          (- (calendar-day-of-week (list month 1 year))
309                             calendar-week-start-day)
310                          7))
311                      7)))
312     (move-to-column (+ 6
313                        (* 25
314                           (1+ (calendar-interval
315                                displayed-month displayed-year month year)))
316                        (* 3 (mod
317                              (- (calendar-day-of-week date)
318                                 calendar-week-start-day)
319                              7))))))
320
321 (defun calendar-goto-date (date)
322   "Move cursor to DATE."
323   (interactive (list (calendar-read-date)))
324   (let ((month (extract-calendar-month date))
325         (year (extract-calendar-year date)))
326     (if (not (calendar-date-is-visible-p date))
327         (calendar-other-month
328          (if (and (= month 1) (= year 1))
329              2
330            month)
331          year)))
332   (calendar-cursor-to-visible-date date)
333   (run-hooks 'calendar-move-hook))
334
335 (defun calendar-goto-day-of-year (year day &optional noecho)
336   "Move cursor to YEAR, DAY number; echo DAY/YEAR unless NOECHO is t.
337 Negative DAY counts backward from end of year."
338   (interactive
339    (let* ((year (calendar-read
340                  "Year (>0): "
341                  (lambda (x) (> x 0))
342                  (int-to-string (extract-calendar-year
343                                  (calendar-current-date)))))
344           (last (if (calendar-leap-year-p year) 366 365))
345           (day (calendar-read
346                 (format "Day number (+/- 1-%d): " last)
347                 '(lambda (x) (and (<= 1 (abs x)) (<= (abs x) last))))))
348      (list year day)))
349   (calendar-goto-date
350    (calendar-gregorian-from-absolute
351     (if (< 0 day)
352         (+ -1 day (calendar-absolute-from-gregorian (list 1 1 year)))
353       (+ 1 day (calendar-absolute-from-gregorian (list 12 31 year))))))
354   (or noecho (calendar-print-day-of-year)))
355
356 (provide 'cal-move)
357
358 ;;; arch-tag: d0883c46-7e16-4914-8ff8-8f67e699b781
359 ;;; cal-move.el ends here