Add arch taglines
[bpt/emacs.git] / lisp / calendar / cal-menu.el
index 3baaa37..3c6cc78 100644 (file)
@@ -1,6 +1,6 @@
 ;;; cal-menu.el --- calendar functions for menu bar and popup menu support
 
-;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1995, 2001, 2003 Free Software Foundation, Inc.
 
 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
 ;;     Lara Rios <lrios@coewl.cen.uiuc.edu>
@@ -20,8 +20,9 @@
 ;; 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, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
 
 ;;; Code:
 
+(defvar displayed-month)
+(defvar displayed-year)
+
+(eval-when-compile (require 'calendar))
+(require 'easymenu)
+
 (define-key calendar-mode-map [menu-bar edit] 'undefined)
 (define-key calendar-mode-map [menu-bar search] 'undefined)
 
@@ -72,7 +79,7 @@
 (define-key calendar-mode-map [menu-bar diary wk]
   '("Insert Weekly" . insert-weekly-diary-entry))
 (define-key calendar-mode-map [menu-bar diary ent]
-  '("Insert Daily". insert-diary-entry))
+  '("Insert Diary Entry" . insert-diary-entry))
 (define-key calendar-mode-map [menu-bar diary all]
   '("Show All" . show-all-diary-entries))
 (define-key calendar-mode-map [menu-bar diary mark]
 (define-key calendar-mode-map [menu-bar diary view]
   '("Other File" . view-other-diary-entries))
 
-(define-key calendar-mode-map [menu-bar holidays]
+(define-key calendar-mode-map [menu-bar Holidays]
   (cons "Holidays" (make-sparse-keymap "Holidays")))
 
-(define-key calendar-mode-map [menu-bar holidays unmark]
-  '("Unmark" . calendar-unmark))
-(define-key calendar-mode-map [menu-bar holidays mark]
-  '("Mark" . mark-calendar-holidays))
-(define-key calendar-mode-map [menu-bar holidays 3-mon]
-  '("3 Months" . list-calendar-holidays))
-(define-key calendar-mode-map [menu-bar holidays 1-day]
-  '("One Day" . calendar-cursor-holidays))
-
 (define-key calendar-mode-map [menu-bar goto]
   (cons "Goto" (make-sparse-keymap "Goto")))
 
   '("Julian Date" . calendar-goto-julian-date))
 (define-key calendar-mode-map [menu-bar goto islamic]
   '("Islamic Date" . calendar-goto-islamic-date))
+(define-key calendar-mode-map [menu-bar goto persian]
+  '("Persian Date" . calendar-goto-persian-date))
 (define-key calendar-mode-map [menu-bar goto hebrew]
   '("Hebrew Date" . calendar-goto-hebrew-date))
 (define-key calendar-mode-map [menu-bar goto astro]
   '("Astronomical Date" . calendar-goto-astro-day-number))
 (define-key calendar-mode-map [menu-bar goto iso]
   '("ISO Date" . calendar-goto-iso-date))
+(define-key calendar-mode-map [menu-bar goto day-of-year]
+  '("Day of Year" . calendar-goto-day-of-year))
 (define-key calendar-mode-map [menu-bar goto gregorian]
   '("Other Date" . calendar-goto-date))
 (define-key calendar-mode-map [menu-bar goto end-of-year]
 (define-key calendar-mode-map [menu-bar scroll fwd-1]
   '("Forward 1 Month" . scroll-calendar-left))
 
-(put 'calendar-forward-day 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-backward-day 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-forward-week 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-backward-week 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-forward-month 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-backward-month 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-forward-year 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-backward-year 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-beginning-of-year 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-end-of-year 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-beginning-of-month 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-end-of-month 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-end-of-week 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-beginning-of-week 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-mouse-print-dates 'menu-enable '(calendar-event-to-date))
-(put 'calendar-sunrise-sunset 'menu-enable '(calendar-event-to-date))
-(put 'calendar-cursor-holidays 'menu-enable '(calendar-cursor-to-date))
-(put 'view-diary-entries 'menu-enable '(calendar-cursor-to-date))
-(put 'view-other-diary-entries 'menu-enable '(calendar-cursor-to-date))
-(put 'calendar-mouse-insert-hebrew-diary-entry
-     'menu-enable
-     '(calendar-cursor-to-date))
-(put 'calendar-mouse-insert-islamic-diary-entry
-     'menu-enable
-     '(calendar-cursor-to-date))
-(put 'insert-cyclic-diary-entry 'menu-enable '(calendar-cursor-to-date))
-(put 'insert-block-diary-entry 'menu-enable '(calendar-cursor-to-date))
-(put 'insert-anniversary-diary-entry 'menu-enable '(calendar-cursor-to-date))
-(put 'insert-yearly-diary-entry 'menu-enable '(calendar-cursor-to-date))
-(put 'insert-monthly-diary-entry 'menu-enable '(calendar-cursor-to-date))
-(put 'insert-weekly-diary-entry 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-day 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-week 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-week2 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-week-iso 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-week-monday 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-filofax-2week
-     'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-filofax-week 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-month 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-month-landscape 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-year 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-filofax-year 'menu-enable '(calendar-cursor-to-date))
-(put 'cal-tex-cursor-year-landscape 'menu-enable '(calendar-cursor-to-date))
+(defun calendar-flatten (list)
+  "Flatten LIST eliminating sublists structure; result is a list of atoms.
+This is the same as the preorder list of leaves in a rooted forest."
+  (if (atom list)
+      (list list)
+    (if (cdr list)
+        (append (calendar-flatten (car list)) (calendar-flatten (cdr list)))
+      (calendar-flatten (car list)))))
+
+(defun cal-menu-x-popup-menu (position menu)
+  "Like `x-popup-menu', but prints an error message if popup menus are
+not available."
+  (if (display-popup-menus-p)
+      (x-popup-menu position menu)
+    (error "Popup menus are not available on this system")))
+
+(defun cal-menu-list-holidays-year ()
+  "Display a list of the holidays of the selected date's year."
+  (interactive)
+  (let ((year (extract-calendar-year (calendar-cursor-to-date))))
+    (list-holidays year year)))
+
+(defun cal-menu-list-holidays-following-year ()
+  "Display a list of the holidays of the following year."
+  (interactive)
+  (let ((year (1+ (extract-calendar-year (calendar-cursor-to-date)))))
+    (list-holidays year year)))
+
+(defun cal-menu-list-holidays-previous-year ()
+  "Display a list of the holidays of the previous year."
+  (interactive)
+  (let ((year (1- (extract-calendar-year (calendar-cursor-to-date)))))
+    (list-holidays year year)))
+
+(defun cal-menu-update ()
+  ;; Update the holiday part of calendar menu bar for the current display.
+  (condition-case nil
+      (if (eq major-mode 'calendar-mode)
+          (let ((l))
+            (calendar-for-loop;; Show 11 years--5 before, 5 after year of
+                   ;; middle month
+             i from (- displayed-year 5) to (+ displayed-year 5) do
+             (setq l (cons (vector (format "For Year %s" i)
+                                   (list (list 'lambda 'nil '(interactive)
+                                               (list 'list-holidays i i)))
+                                   t)
+                           l)))
+            (setq l (cons ["Mark Holidays" mark-calendar-holidays t]
+                          (cons ["Unmark Calendar" calendar-unmark t]
+                                (cons ["--" '("--") t] l))))
+            (define-key calendar-mode-map [menu-bar Holidays]
+             (cons "Holidays" (easy-menu-create-menu "Holidays" (nreverse l))))
+            (define-key calendar-mode-map [menu-bar Holidays separator]
+              '("--"))
+            (define-key calendar-mode-map [menu-bar Holidays today]
+                `(,(format "For Today (%s)"
+                           (calendar-date-string (calendar-current-date) t t))
+                  . cal-menu-today-holidays))
+            (let ((title
+                   (let ((m1 displayed-month)
+                         (y1 displayed-year)
+                         (m2 displayed-month)
+                         (y2 displayed-year))
+                     (increment-calendar-month m1 y1 -1)
+                     (increment-calendar-month m2 y2 1)
+                     (if (= y1 y2)
+                         (format "%s-%s, %d"
+                                 (calendar-month-name m1 'abbrev)
+                                 (calendar-month-name m2 'abbrev)
+                                 y2)
+                       (format "%s, %d-%s, %d"
+                               (calendar-month-name m1 'abbrev)
+                               y1
+                               (calendar-month-name m2 'abbrev)
+                               y2)))))
+              (define-key  calendar-mode-map [menu-bar Holidays 3-month]
+                `(,(format "For Window (%s)" title)
+                  . list-calendar-holidays)))
+            (let ((date (calendar-cursor-to-date)))
+              (if date
+                  (define-key calendar-mode-map [menu-bar Holidays 1-day]
+                    `(,(format "For Cursor Date (%s)"
+                               (calendar-date-string date t t))
+                      . calendar-cursor-holidays))))))
+    ;; Try to avoid entering infinite beep mode in case of errors.
+    (error (ding))))
 
 (defun calendar-event-to-date (&optional error)
   "Date of last event.
 If event is not on a specific date, signals an error if optional parameter
 ERROR is t, otherwise just returns nil."
   (save-excursion
+    (set-buffer (window-buffer (posn-window (event-start last-input-event))))
     (goto-char (posn-point (event-start last-input-event)))
     (calendar-cursor-to-date error)))
 
@@ -221,7 +266,7 @@ ERROR is t, otherwise just returns nil."
   "Pop up menu to insert a Hebrew-date diary entry."
   (interactive "e")
   (let ((hebrew-selection
-         (x-popup-menu
+         (cal-menu-x-popup-menu
           event
           (list "Hebrew insert menu"
                 (list (calendar-hebrew-date-string (calendar-cursor-to-date))
@@ -234,7 +279,7 @@ ERROR is t, otherwise just returns nil."
   "Pop up menu to insert an Islamic-date diary entry."
   (interactive "e")
   (let ((islamic-selection
-         (x-popup-menu
+         (cal-menu-x-popup-menu
           event
           (list "Islamic insert menu"
                 (list (calendar-islamic-date-string (calendar-cursor-to-date))
@@ -247,49 +292,94 @@ ERROR is t, otherwise just returns nil."
   "Show sunrise/sunset times for mouse-selected date."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (calendar-sunrise-sunset)))
 
-(defun calendar-mouse-holidays ()
-  "Show holidays for mouse-selected date."
+(defun cal-menu-today-holidays ()
+  "Show holidays for today's date."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-cursor-to-date (calendar-current-date))
     (calendar-cursor-holidays)))
 
-(defun calendar-mouse-view-diary-entries ()
-  "View diary entries on mouse-selected date."
+(defun calendar-mouse-holidays ()
+  "Pop up menu of holidays for mouse selected date."
   (interactive)
-  (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
-    (view-diary-entries 1)))
+  (let* ((date (calendar-event-to-date))
+         (l (mapcar '(lambda (x) (list x))
+                    (check-calendar-holidays date)))
+         (selection
+          (cal-menu-x-popup-menu
+           event
+           (list
+            (format "Holidays for %s" (calendar-date-string date))
+            (append
+             (list (format "Holidays for %s" (calendar-date-string date)))
+             (if l l '("None")))))))
+    (and selection (call-interactively selection))))
+
+(defun calendar-mouse-view-diary-entries (&optional date diary)
+  "Pop up menu of diary entries for mouse-selected date.
+Use optional DATE and alternative file DIARY.
+
+Any holidays are shown if `holidays-in-diary-buffer' is t."
+  (interactive)
+  (let* ((date (if date date (calendar-event-to-date)))
+         (diary-file (if diary diary diary-file))
+         (diary-list-include-blanks nil)
+         (diary-display-hook 'ignore)
+         (diary-entries
+          (mapcar '(lambda (x) (split-string (car (cdr x)) "\^M\\|\n"))
+                  (list-diary-entries date 1)))
+         (holidays (if holidays-in-diary-buffer
+                       (mapcar '(lambda (x) (list x))
+                               (check-calendar-holidays date))))
+         (title (concat "Diary entries "
+                        (if diary (format "from %s " diary) "")
+                        "for "
+                        (calendar-date-string date)))
+         (selection
+          (cal-menu-x-popup-menu
+           event
+           (list title
+                 (append
+                  (list title)
+                  (if holidays
+                      (mapcar '(lambda (x) (list (concat "     " (car x))))
+                              holidays))
+                  (if holidays
+                      (list "--shadow-etched-in" "--shadow-etched-in"))
+                  (if diary-entries
+                      (mapcar 'list (calendar-flatten diary-entries))
+                    '("None")))))))
+    (and selection (call-interactively selection))))
 
 (defun calendar-mouse-view-other-diary-entries ()
-  "View diary entries from alternative file on mouse-selected date."
+  "Pop up menu of diary entries from alternative file on mouse-selected date."
   (interactive)
-  (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
-    (call-interactively 'view-other-diary-entries)))
+  (calendar-mouse-view-diary-entries
+   (calendar-event-to-date)
+   (read-file-name "Enter diary file name: " default-directory nil t)))
 
 (defun calendar-mouse-insert-diary-entry ()
   "Insert diary entry for mouse-selected date."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (insert-diary-entry nil)))
 
 (defun calendar-mouse-set-mark ()
   "Mark the date under the cursor."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (calendar-set-mark nil)))
 
 (defun cal-tex-mouse-day ()
   "Make a buffer with LaTeX commands for the day mouse is on."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-day nil)))
 
 (defun cal-tex-mouse-week ()
@@ -297,7 +387,7 @@ ERROR is t, otherwise just returns nil."
 Holidays are included if `cal-tex-holidays' is t."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-week nil)))
 
 (defun cal-tex-mouse-week2 ()
@@ -305,7 +395,7 @@ Holidays are included if `cal-tex-holidays' is t."
 The printed output will be on two pages."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-week2 nil)))
 
 (defun cal-tex-mouse-week-iso ()
@@ -313,28 +403,35 @@ The printed output will be on two pages."
 Holidays are included if `cal-tex-holidays' is t."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-week-iso nil)))
 
 (defun cal-tex-mouse-week-monday ()
   "One page calendar for week indicated by cursor."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-week-monday nil)))
 
+(defun cal-tex-mouse-filofax-daily ()
+  "Day-per-page Filofax calendar for week indicated by cursor."
+  (interactive)
+  (save-excursion
+    (calendar-mouse-goto-date (calendar-event-to-date))
+    (cal-tex-cursor-filofax-daily nil)))
+
 (defun cal-tex-mouse-filofax-2week ()
   "One page Filofax calendar for week indicated by cursor."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-filofax-2week nil)))
 
 (defun cal-tex-mouse-filofax-week ()
   "Two page Filofax calendar for week indicated by cursor."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-filofax-week nil)))
 
 (defun cal-tex-mouse-month ()
@@ -342,7 +439,7 @@ Holidays are included if `cal-tex-holidays' is t."
 Calendar is condensed onto one page."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-month nil)))
 
 (defun cal-tex-mouse-month-landscape ()
@@ -350,28 +447,28 @@ Calendar is condensed onto one page."
 The output is in landscape format, one month to a page."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-month-landscape nil)))
 
 (defun cal-tex-mouse-year ()
   "Make a buffer with LaTeX commands for the year cursor is on."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-year nil)))
 
 (defun cal-tex-mouse-filofax-year ()
   "Make a buffer with LaTeX commands for Filofax calendar of year cursor is on."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-filofax-year nil)))
 
 (defun cal-tex-mouse-year-landscape ()
   "Make a buffer with LaTeX commands for the year cursor is on."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (cal-tex-cursor-year-landscape nil)))
 
 (defun calendar-mouse-print-dates ()
@@ -379,7 +476,7 @@ The output is in landscape format, one month to a page."
   (interactive)
   (let ((date (calendar-event-to-date))
         (selection
-         (x-popup-menu
+         (cal-menu-x-popup-menu
           event
           (list
            (concat (calendar-date-string date) " (Gregorian)")
@@ -391,10 +488,15 @@ The output is in landscape format, one month to a page."
              (list (format "Julian date: %s"
                            (calendar-julian-date-string date)))
              (list
-              (format "Astronomical (Julian) day number (after noon UTC): %s"
+              (format "Astronomical (Julian) day number (at noon UTC): %s.0"
                            (calendar-astro-date-string date)))
+             (list
+              (format "Fixed (RD) date: %s"
+                      (calendar-absolute-from-gregorian date)))
              (list (format "Hebrew date (before sunset): %s"
-                           (calendar-hebrew-date-string date))))
+                           (calendar-hebrew-date-string date)))
+             (list (format "Persian date: %s"
+                           (calendar-persian-date-string date))))
             (let ((i (calendar-islamic-date-string date)))
               (if (not (string-equal i ""))
                   (list (list (format "Islamic date (before sunset): %s" i)))))
@@ -422,15 +524,19 @@ The output is in landscape format, one month to a page."
   "Show Chinese equivalent for mouse-selected date."
   (interactive)
   (save-excursion
-    (calendar-goto-date (calendar-event-to-date))
+    (calendar-mouse-goto-date (calendar-event-to-date))
     (calendar-print-chinese-date)))
 
+(defun calendar-mouse-goto-date (date)
+  (set-buffer (window-buffer (posn-window (event-start last-input-event))))
+  (calendar-goto-date date))
+
 (defun calendar-mouse-2-date-menu (event)
   "Pop up menu for Mouse-2 for selected date in the calendar window."
   (interactive "e")
   (let* ((date (calendar-event-to-date t))
          (selection
-          (x-popup-menu
+          (cal-menu-x-popup-menu
            event
            (list (calendar-date-string date t nil)
                  (list
@@ -451,7 +557,7 @@ The output is in landscape format, one month to a page."
   "Pop up submenu for Mouse-2 for cal-tex commands for selected date in the calendar window."
   (interactive "e")
   (let* ((selection
-          (x-popup-menu
+          (cal-menu-x-popup-menu
            event
            (list (calendar-date-string date t nil)
                  (list
@@ -474,11 +580,13 @@ The output is in landscape format, one month to a page."
   "Pop up sub-submenu for Mouse-2 for Filofax cal-tex commands for selected date."
   (interactive "e")
   (let* ((selection
-          (x-popup-menu
+          (cal-menu-x-popup-menu
            event
            (list (calendar-date-string date t nil)
                  (list
                   ""
+                  '("Filofax Daily (one-day-per-page)" .
+                    cal-tex-mouse-filofax-daily)
                   '("Filofax Weekly (2-weeks-at-a-glance)" .
                     cal-tex-mouse-filofax-2week)
                   '("Filofax Weekly (week-at-a-glance)" .
@@ -510,4 +618,5 @@ The output is in landscape format, one month to a page."
 
 (provide 'cal-menu)
 
+;;; arch-tag: aa81cf73-ce89-48a4-97ec-9ef861e87fe9
 ;;; cal-menu.el ends here