Unquote lambda functions.
[emacs.git] / lisp / calendar / cal-menu.el
blob2b2efd1cb3a4b20dadf8c2c368e72a6704d107e8
1 ;;; cal-menu.el --- calendar functions for menu bar and popup menu support
3 ;; Copyright (C) 1994, 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Lara Rios <lrios@coewl.cen.uiuc.edu>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Keywords: calendar
10 ;; Human-Keywords: calendar, popup menus, menu bar
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; This collection of functions implements menu bar and popup menu support for
32 ;; calendar.el.
34 ;;; Code:
36 ;; The code in this file is only called from calendar.el, but can't
37 ;; require it (to supress undefined function warnings from compiler)
38 ;; without a recursive require.
39 ;; All these functions are either autoloaded, or autoloaded or defined
40 ;; in calendar.el.
41 (declare-function calendar-increment-month "calendar" (n &optional mon yr))
42 (declare-function calendar-month-name "calendar" (month &optional abbrev))
43 (declare-function extract-calendar-year "calendar" (date))
44 (declare-function calendar-cursor-to-date "calendar" (&optional error))
45 (declare-function holiday-list "holidays" (y1 y2 &optional l label))
46 (declare-function calendar-sunrise-sunset "solar" nil)
47 (declare-function calendar-current-date "calendar" nil)
48 (declare-function calendar-cursor-holidays "holidays" nil)
49 (declare-function calendar-date-string "calendar"
50 (date &optional abbreviate nodayname))
51 (declare-function insert-diary-entry "diary-lib" (arg))
52 (declare-function calendar-set-mark "calendar" (arg))
53 (declare-function cal-tex-cursor-day "cal-tex" (&optional arg))
54 (declare-function cal-tex-cursor-week "cal-tex" (&optional arg))
55 (declare-function cal-tex-cursor-week2 "cal-tex" (&optional arg))
56 (declare-function cal-tex-cursor-week-iso "cal-tex" (&optional arg))
57 (declare-function cal-tex-cursor-week-monday "cal-tex" (&optional arg))
58 (declare-function cal-tex-cursor-filofax-daily "cal-tex" (&optional arg))
59 (declare-function cal-tex-cursor-filofax-2week "cal-tex" (&optional arg))
60 (declare-function cal-tex-cursor-filofax-week "cal-tex" (&optional arg))
61 (declare-function cal-tex-cursor-month "cal-tex" (arg))
62 (declare-function cal-tex-cursor-month-landscape "cal-tex" (&optional arg))
63 (declare-function cal-tex-cursor-year "cal-tex" (&optional arg))
64 (declare-function cal-tex-cursor-filofax-year "cal-tex" (&optional arg))
65 (declare-function cal-tex-cursor-year-landscape "cal-tex" (&optional arg))
66 (declare-function calendar-day-of-year-string "calendar" (&optional date))
67 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
68 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
69 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
70 (declare-function calendar-absolute-from-gregorian "calendar" (date))
71 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
72 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
73 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
74 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
75 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
76 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
77 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
78 (declare-function calendar-french-date-string "cal-french" (&optional date))
79 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
80 (declare-function calendar-print-chinese-date "cal-china" nil)
81 (declare-function calendar-goto-date "cal-move" (date))
83 (defvar displayed-year)
85 (defconst cal-menu-moon-menu
86 '("Moon"
87 ["Lunar Phases" calendar-phases-of-moon]))
89 (defconst cal-menu-diary-menu
90 '("Diary"
91 ["Other File" view-other-diary-entries]
92 ["Cursor Date" diary-view-entries]
93 ["Mark All" mark-diary-entries]
94 ["Show All" diary-show-all-entries]
95 ["Insert Diary Entry" insert-diary-entry]
96 ["Insert Weekly" insert-weekly-diary-entry]
97 ["Insert Monthly" insert-monthly-diary-entry]
98 ["Insert Yearly" insert-yearly-diary-entry]
99 ["Insert Anniversary" insert-anniversary-diary-entry]
100 ["Insert Block" insert-block-diary-entry]
101 ["Insert Cyclic" insert-cyclic-diary-entry]
102 ("Insert Baha'i"
103 [" " nil :suffix (calendar-bahai-date-string (calendar-cursor-to-date))]
104 ["One time" diary-bahai-insert-entry]
105 ["Monthly" diary-bahai-insert-monthly-entry]
106 ["Yearly" diary-bahai-insert-yearly-entry])
107 ("Insert Islamic"
108 [" " nil :suffix (calendar-islamic-date-string (calendar-cursor-to-date))]
109 ["One time" insert-islamic-diary-entry]
110 ["Monthly" insert-monthly-islamic-diary-entry]
111 ["Yearly" insert-yearly-islamic-diary-entry])
112 ("Insert Hebrew"
113 [" " nil :suffix (calendar-hebrew-date-string (calendar-cursor-to-date))]
114 ["One time" insert-hebrew-diary-entry]
115 ["Monthly" insert-monthly-hebrew-diary-entry]
116 ["Yearly" insert-yearly-hebrew-diary-entry])))
118 (defun cal-menu-holiday-window-suffix ()
119 (let ((my1 (calendar-increment-month -1))
120 (my2 (calendar-increment-month 1)))
121 (if (= (cdr my1) (cdr my2))
122 (format "%s-%s, %d"
123 (calendar-month-name (car my1) 'abbrev)
124 (calendar-month-name (car my2) 'abbrev)
125 (cdr my2))
126 (format "%s, %d-%s, %d"
127 (calendar-month-name (car my1) 'abbrev)
128 (cdr my1)
129 (calendar-month-name (car my2) 'abbrev)
130 (cdr my2)))))
132 (defconst cal-menu-holidays-menu
133 `("Holidays"
134 ["For Cursor Date -" calendar-cursor-holidays
135 :suffix (calendar-date-string (calendar-cursor-to-date) t t)
136 :visible (calendar-cursor-to-date)]
137 ["For Window -" calendar-list-holidays
138 :suffix (cal-menu-holiday-window-suffix)]
139 ["For Today -" cal-menu-today-holidays
140 :suffix (calendar-date-string (calendar-current-date) t t)]
141 "--"
142 ,@(let ((l ()))
143 ;; Show 11 years--5 before, 5 after year of middle month.
144 ;; We used to use :suffix rather than :label and bumped into
145 ;; an easymenu bug:
146 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01813.html
147 ;; The bug has since been fixed.
148 (dotimes (i 11)
149 (push (vector (format "hol-year-%d" i)
150 `(lambda ()
151 (interactive)
152 (holiday-list (+ displayed-year ,(- i 5))
153 (+ displayed-year ,(- i 5))))
154 :label `(format "For Year %d"
155 (+ displayed-year ,(- i 5))))
157 (nreverse l))
158 "--"
159 ["Unmark Calendar" calendar-unmark]
160 ["Mark Holidays" mark-calendar-holidays]))
162 (defconst cal-menu-goto-menu
163 '("Goto"
164 ["Today" calendar-goto-today]
165 ["Beginning of Week" calendar-beginning-of-week]
166 ["End of Week" calendar-end-of-week]
167 ["Beginning of Month" calendar-beginning-of-month]
168 ["End of Month" calendar-end-of-month]
169 ["Beginning of Year" calendar-beginning-of-year]
170 ["End of Year" calendar-end-of-year]
171 ["Other Date" calendar-goto-date]
172 ["Day of Year" calendar-goto-day-of-year]
173 ["ISO Week" calendar-goto-iso-week]
174 ["ISO Date" calendar-goto-iso-date]
175 ["Astronomical Date" calendar-goto-astro-day-number]
176 ["Hebrew Date" calendar-goto-hebrew-date]
177 ["Persian Date" calendar-goto-persian-date]
178 ["Baha'i Date" calendar-bahai-goto-date]
179 ["Islamic Date" calendar-goto-islamic-date]
180 ["Julian Date" calendar-goto-julian-date]
181 ["Chinese Date" calendar-goto-chinese-date]
182 ["Coptic Date" calendar-goto-coptic-date]
183 ["Ethiopic Date" calendar-goto-ethiopic-date]
184 ("Mayan Date"
185 ["Next Tzolkin" calendar-next-tzolkin-date]
186 ["Previous Tzolkin" calendar-previous-tzolkin-date]
187 ["Next Haab" calendar-next-haab-date]
188 ["Previous Haab" calendar-previous-haab-date]
189 ["Next Round" calendar-next-calendar-round-date]
190 ["Previous Round" calendar-previous-calendar-round-date])
191 ["French Date" calendar-goto-french-date]))
193 (defconst cal-menu-scroll-menu
194 '("Scroll"
195 ["Forward 1 Month" calendar-scroll-left]
196 ["Forward 3 Months" calendar-scroll-left-three-months]
197 ["Forward 1 Year" "4\C-v"]
198 ["Backward 1 Month" calendar-scroll-right]
199 ["Backward 3 Months" calendar-scroll-right-three-months]
200 ["Backward 1 Year" "4\ev"]))
202 (defun cal-menu-x-popup-menu (position menu)
203 "Like `x-popup-menu', but prints an error message if popup menus are
204 not available."
205 (if (display-popup-menus-p)
206 (x-popup-menu position menu)
207 (error "Popup menus are not available on this system")))
209 (defun cal-menu-list-holidays-year ()
210 "Display a list of the holidays of the selected date's year."
211 (interactive)
212 (let ((year (extract-calendar-year (calendar-cursor-to-date))))
213 (holiday-list year year)))
215 (defun cal-menu-list-holidays-following-year ()
216 "Display a list of the holidays of the following year."
217 (interactive)
218 (let ((year (1+ (extract-calendar-year (calendar-cursor-to-date)))))
219 (holiday-list year year)))
221 (defun cal-menu-list-holidays-previous-year ()
222 "Display a list of the holidays of the previous year."
223 (interactive)
224 (let ((year (1- (extract-calendar-year (calendar-cursor-to-date)))))
225 (holiday-list year year)))
227 (defun calendar-event-to-date (&optional error)
228 "Date of last event.
229 If event is not on a specific date, signals an error if optional parameter
230 ERROR is t, otherwise just returns nil."
231 (with-current-buffer
232 (window-buffer (posn-window (event-start last-input-event)))
233 (goto-char (posn-point (event-start last-input-event)))
234 (calendar-cursor-to-date error)))
236 (defun calendar-mouse-sunrise/sunset ()
237 "Show sunrise/sunset times for mouse-selected date."
238 (interactive)
239 (save-excursion
240 (calendar-mouse-goto-date (calendar-event-to-date))
241 (calendar-sunrise-sunset)))
243 (defun cal-menu-today-holidays ()
244 "Show holidays for today's date."
245 (interactive)
246 (save-excursion
247 (calendar-cursor-to-date (calendar-current-date))
248 (calendar-cursor-holidays)))
250 (autoload 'calendar-check-holidays "holidays")
251 (autoload 'diary-list-entries "diary-lib")
253 (defun calendar-mouse-holidays (&optional event)
254 "Pop up menu of holidays for mouse selected date."
255 (interactive "e")
256 (let* ((date (calendar-event-to-date))
257 (l (mapcar 'list (calendar-check-holidays date)))
258 (selection
259 (cal-menu-x-popup-menu
260 event
261 (list
262 (format "Holidays for %s" (calendar-date-string date))
263 (append
264 (list (format "Holidays for %s" (calendar-date-string date)))
265 (if l l '("None")))))))
266 (and selection (call-interactively selection))))
268 (defvar holidays-in-diary-buffer) ; only called from calendar.el
270 (defun calendar-mouse-view-diary-entries (&optional date diary event)
271 "Pop up menu of diary entries for mouse-selected date.
272 Use optional DATE and alternative file DIARY.
274 Any holidays are shown if `holidays-in-diary-buffer' is t."
275 (interactive "i\ni\ne")
276 (let* ((date (or date (calendar-event-to-date)))
277 (diary-file (if diary diary diary-file))
278 (diary-list-include-blanks nil)
279 (diary-display-hook 'ignore)
280 (diary-entries
281 (mapcar (lambda (x) (split-string (cadr x) "\n"))
282 (diary-list-entries date 1 'list-only)))
283 (holidays (if holidays-in-diary-buffer
284 (calendar-check-holidays date)))
285 (title (concat "Diary entries "
286 (if diary (format "from %s " diary) "")
287 "for "
288 (calendar-date-string date)))
289 (selection
290 (cal-menu-x-popup-menu
291 event
292 (list title
293 (append
294 (list title)
295 (mapcar (lambda (x) (list (concat " " x))) holidays)
296 (if holidays
297 (list "--shadow-etched-in" "--shadow-etched-in"))
298 (if diary-entries
299 (mapcar 'list (apply 'append diary-entries))
300 '("None")))))))
301 (and selection (call-interactively selection))))
303 (defun calendar-mouse-view-other-diary-entries ()
304 "Pop up menu of diary entries from alternative file on mouse-selected date."
305 (interactive)
306 (calendar-mouse-view-diary-entries
307 (calendar-event-to-date)
308 (read-file-name "Enter diary file name: " default-directory nil t)))
310 (defun calendar-mouse-insert-diary-entry ()
311 "Insert diary entry for mouse-selected date."
312 (interactive)
313 (save-excursion
314 (calendar-mouse-goto-date (calendar-event-to-date))
315 (insert-diary-entry nil)))
317 (defun calendar-mouse-set-mark ()
318 "Mark the date under the cursor."
319 (interactive)
320 (save-excursion
321 (calendar-mouse-goto-date (calendar-event-to-date))
322 (calendar-set-mark nil)))
324 (defun cal-tex-mouse-day ()
325 "Make a buffer with LaTeX commands for the day mouse is on."
326 (interactive)
327 (save-excursion
328 (calendar-mouse-goto-date (calendar-event-to-date))
329 (cal-tex-cursor-day nil)))
331 (defun cal-tex-mouse-week ()
332 "One page calendar for week indicated by cursor.
333 Holidays are included if `cal-tex-holidays' is t."
334 (interactive)
335 (save-excursion
336 (calendar-mouse-goto-date (calendar-event-to-date))
337 (cal-tex-cursor-week nil)))
339 (defun cal-tex-mouse-week2 ()
340 "Make a buffer with LaTeX commands for the week cursor is on.
341 The printed output will be on two pages."
342 (interactive)
343 (save-excursion
344 (calendar-mouse-goto-date (calendar-event-to-date))
345 (cal-tex-cursor-week2 nil)))
347 (defun cal-tex-mouse-week-iso ()
348 "One page calendar for week indicated by cursor.
349 Holidays are included if `cal-tex-holidays' is t."
350 (interactive)
351 (save-excursion
352 (calendar-mouse-goto-date (calendar-event-to-date))
353 (cal-tex-cursor-week-iso nil)))
355 (defun cal-tex-mouse-week-monday ()
356 "One page calendar for week indicated by cursor."
357 (interactive)
358 (save-excursion
359 (calendar-mouse-goto-date (calendar-event-to-date))
360 (cal-tex-cursor-week-monday nil)))
362 (defun cal-tex-mouse-filofax-daily ()
363 "Day-per-page Filofax calendar for week indicated by cursor."
364 (interactive)
365 (save-excursion
366 (calendar-mouse-goto-date (calendar-event-to-date))
367 (cal-tex-cursor-filofax-daily nil)))
369 (defun cal-tex-mouse-filofax-2week ()
370 "One page Filofax calendar for week indicated by cursor."
371 (interactive)
372 (save-excursion
373 (calendar-mouse-goto-date (calendar-event-to-date))
374 (cal-tex-cursor-filofax-2week nil)))
376 (defun cal-tex-mouse-filofax-week ()
377 "Two page Filofax calendar for week indicated by cursor."
378 (interactive)
379 (save-excursion
380 (calendar-mouse-goto-date (calendar-event-to-date))
381 (cal-tex-cursor-filofax-week nil)))
383 (defun cal-tex-mouse-month ()
384 "Make a buffer with LaTeX commands for the month cursor is on.
385 Calendar is condensed onto one page."
386 (interactive)
387 (save-excursion
388 (calendar-mouse-goto-date (calendar-event-to-date))
389 (cal-tex-cursor-month nil)))
391 (defun cal-tex-mouse-month-landscape ()
392 "Make a buffer with LaTeX commands for the month cursor is on.
393 The output is in landscape format, one month to a page."
394 (interactive)
395 (save-excursion
396 (calendar-mouse-goto-date (calendar-event-to-date))
397 (cal-tex-cursor-month-landscape nil)))
399 (defun cal-tex-mouse-year ()
400 "Make a buffer with LaTeX commands for the year cursor is on."
401 (interactive)
402 (save-excursion
403 (calendar-mouse-goto-date (calendar-event-to-date))
404 (cal-tex-cursor-year nil)))
406 (defun cal-tex-mouse-filofax-year ()
407 "Make a buffer with LaTeX commands for Filofax calendar of year cursor is on."
408 (interactive)
409 (save-excursion
410 (calendar-mouse-goto-date (calendar-event-to-date))
411 (cal-tex-cursor-filofax-year nil)))
413 (defun cal-tex-mouse-year-landscape ()
414 "Make a buffer with LaTeX commands for the year cursor is on."
415 (interactive)
416 (save-excursion
417 (calendar-mouse-goto-date (calendar-event-to-date))
418 (cal-tex-cursor-year-landscape nil)))
420 (defun calendar-mouse-print-dates (&optional event)
421 "Pop up menu of equivalent dates to mouse selected date."
422 (interactive "e")
423 (let* ((date (calendar-event-to-date))
424 (selection
425 (cal-menu-x-popup-menu
426 event
427 (list
428 (concat (calendar-date-string date) " (Gregorian)")
429 (append
430 (list
431 (concat (calendar-date-string date) " (Gregorian)")
432 (list (calendar-day-of-year-string date))
433 (list (format "ISO date: %s" (calendar-iso-date-string date)))
434 (list (format "Julian date: %s"
435 (calendar-julian-date-string date)))
436 (list
437 (format "Astronomical (Julian) day number (at noon UTC): %s.0"
438 (calendar-astro-date-string date)))
439 (list
440 (format "Fixed (RD) date: %s"
441 (calendar-absolute-from-gregorian date)))
442 (list (format "Hebrew date (before sunset): %s"
443 (calendar-hebrew-date-string date)))
444 (list (format "Persian date: %s"
445 (calendar-persian-date-string date)))
446 (list (format "Baha'i date (before sunset): %s"
447 (calendar-bahai-date-string date))))
448 (let ((i (calendar-islamic-date-string date)))
449 (if (not (string-equal i ""))
450 (list (list (format "Islamic date (before sunset): %s" i)))))
451 (list
452 (list (format "Chinese date: %s"
453 (calendar-chinese-date-string date))))
454 ;; (list '("Chinese date (select to echo Chinese date)"
455 ;; . calendar-mouse-chinese-date))
456 (let ((c (calendar-coptic-date-string date)))
457 (if (not (string-equal c ""))
458 (list (list (format "Coptic date: %s" c)))))
459 (let ((e (calendar-ethiopic-date-string date)))
460 (if (not (string-equal e ""))
461 (list (list (format "Ethiopic date: %s" e)))))
462 (let ((f (calendar-french-date-string date)))
463 (if (not (string-equal f ""))
464 (list (list (format "French Revolutionary date: %s" f)))))
465 (list
466 (list
467 (format "Mayan date: %s"
468 (calendar-mayan-date-string date)))))))))
469 (and selection (call-interactively selection))))
471 (defun calendar-mouse-chinese-date ()
472 "Show Chinese equivalent for mouse-selected date."
473 (interactive)
474 (save-excursion
475 (calendar-mouse-goto-date (calendar-event-to-date))
476 (calendar-print-chinese-date)))
478 (defun calendar-mouse-goto-date (date)
479 (set-buffer (window-buffer (posn-window (event-start last-input-event))))
480 (calendar-goto-date date))
482 (easy-menu-define cal-menu-context-mouse-menu nil
483 "Pop up menu for Mouse-2 for selected date in the calendar window."
484 '("foo" :filter cal-menu-set-date-title
485 "--"
486 ["Holidays" calendar-mouse-holidays]
487 ["Mark date" calendar-mouse-set-mark]
488 ["Sunrise/sunset" calendar-mouse-sunrise/sunset]
489 ["Other calendars" calendar-mouse-print-dates]
490 ("Prepare LaTeX buffer"
491 ["Daily (1 page)" cal-tex-mouse-day]
492 ["Weekly (1 page)" cal-tex-mouse-week]
493 ["Weekly (2 pages)" cal-tex-mouse-week2]
494 ["Weekly (other style; 1 page)" cal-tex-mouse-week-iso]
495 ["Weekly (yet another style; 1 page)" cal-tex-mouse-week-monday]
496 ["Monthly" cal-tex-mouse-month]
497 ["Monthly (landscape)" cal-tex-mouse-month-landscape]
498 ["Yearly" cal-tex-mouse-year]
499 ["Yearly (landscape)" cal-tex-mouse-year-landscape]
500 ("Filofax styles"
501 ["Filofax Daily (one-day-per-page)" cal-tex-mouse-filofax-daily]
502 ["Filofax Weekly (2-weeks-at-a-glance)" cal-tex-mouse-filofax-2week]
503 ["Filofax Weekly (week-at-a-glance)" cal-tex-mouse-filofax-week]
504 ["Filofax Yearly" cal-tex-mouse-filofax-year]))
505 ["Diary entries" calendar-mouse-view-diary-entries]
506 ["Insert diary entry" calendar-mouse-insert-diary-entry]
507 ["Other diary file entries" calendar-mouse-view-other-diary-entries]))
509 (defun cal-menu-set-date-title (menu)
510 (easy-menu-filter-return
511 menu (calendar-date-string (calendar-event-to-date t) t nil)))
513 (easy-menu-define cal-menu-global-mouse-menu nil
514 "Menu bound to a mouse event, not specific to the mouse-click location."
515 '("Calendar"
516 ["Scroll forward" calendar-scroll-left-three-months]
517 ["Scroll backward" calendar-scroll-right-three-months]
518 ["Mark diary entries" mark-diary-entries]
519 ["List holidays" calendar-list-holidays]
520 ["Mark holidays" calendar-mark-holidays]
521 ["Unmark" calendar-unmark]
522 ["Lunar phases" calendar-phases-of-moon]
523 ["Show diary" diary-show-all-entries]
524 ["Exit calendar" exit-calendar]))
526 (run-hooks 'cal-menu-load-hook)
528 (provide 'cal-menu)
530 ;; arch-tag: aa81cf73-ce89-48a4-97ec-9ef861e87fe9
531 ;;; cal-menu.el ends here