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