cal-menu.el 11.9 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2
;;; cal-menu.el --- calendar functions for menu bar and popup menu support

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1994-1995, 2001-2019 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5

;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
Glenn Morris's avatar
Glenn Morris committed
6
;;         Lara Rios <lrios@coewl.cen.uiuc.edu>
Glenn Morris's avatar
Glenn Morris committed
7
;; Maintainer: emacs-devel@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
8 9
;; Keywords: calendar
;; Human-Keywords: calendar, popup menus, menu bar
10
;; Package: calendar
Richard M. Stallman's avatar
Richard M. Stallman committed
11 12 13

;; 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 17
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
18

Richard M. Stallman's avatar
Richard M. Stallman committed
19
;; GNU Emacs is distributed in the hope that it will be useful,
20 21 22 23 24
;; 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
25
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
26 27 28

;;; Commentary:

29
;; See calendar.el.
Richard M. Stallman's avatar
Richard M. Stallman committed
30 31 32

;;; Code:

33
(require 'calendar)
Glenn Morris's avatar
Glenn Morris committed
34

35 36
(defconst cal-menu-sunmoon-menu
  '("Sun/Moon"
37
    ["Lunar Phases" calendar-lunar-phases]
38 39 40
    ["Sunrise/sunset for cursor date" calendar-sunrise-sunset]
    ["Sunrise/sunset for cursor month" calendar-sunrise-sunset-month])
  "Key map for \"Sun/Moon\" menu in the calendar.")
41 42 43

(defconst cal-menu-diary-menu
  '("Diary"
44
    ["Other File" diary-view-other-diary-entries]
45
    ["Cursor Date" diary-view-entries]
46
    ["Mark All" diary-mark-entries]
47
    ["Show All" diary-show-all-entries]
48 49 50 51 52 53 54
    ["Insert Diary Entry" diary-insert-entry]
    ["Insert Weekly" diary-insert-weekly-entry]
    ["Insert Monthly" diary-insert-monthly-entry]
    ["Insert Yearly" diary-insert-yearly-entry]
    ["Insert Anniversary" diary-insert-anniversary-entry]
    ["Insert Block" diary-insert-block-entry]
    ["Insert Cyclic" diary-insert-cyclic-entry]
55
    ("Insert Bahá’í"
56 57 58
     ["One time" diary-bahai-insert-entry]
     ["Monthly" diary-bahai-insert-monthly-entry]
     ["Yearly" diary-bahai-insert-yearly-entry])
59 60 61 62 63
    ("Insert Chinese"
     ["One time" diary-chinese-insert-entry]
     ["Monthly" diary-chinese-insert-monthly-entry]
     ["Yearly" diary-chinese-insert-yearly-entry]
     ["Anniversary" diary-chinese-insert-anniversary-entry])
64
    ("Insert Islamic"
65 66 67
     ["One time" diary-islamic-insert-entry]
     ["Monthly" diary-islamic-insert-monthly-entry]
     ["Yearly" diary-islamic-insert-yearly-entry])
68
    ("Insert Hebrew"
69
     ["One time" diary-hebrew-insert-entry]
70
     ["Monthly" diary-hebrew-insert-monthly-entry]
71 72
     ["Yearly" diary-hebrew-insert-yearly-entry]))
    "Key map for \"Diary\" menu in the calendar.")
73 74

(defun cal-menu-holiday-window-suffix ()
75
  "Return a string suffix for the \"Window\" entry in `cal-menu-holidays-menu'."
76 77
  (let ((my1 (calendar-increment-month-cons -1))
        (my2 (calendar-increment-month-cons 1)))
78 79 80 81 82 83 84 85
    ;; Mon1-Mon2, Year  or  Mon1, Year1-Mon2, Year2.
    (format "%s%s-%s, %d"
            (calendar-month-name (car my1) 'abbrev)
            (if (= (cdr my1) (cdr my2))
                ""
              (format ", %d" (cdr my1)))
            (calendar-month-name (car my2) 'abbrev)
            (cdr my2))))
86

87
(defvar displayed-year)                 ; from calendar-generate
88

89 90 91 92 93
(defconst cal-menu-holidays-menu
  `("Holidays"
    ["For Cursor Date -" calendar-cursor-holidays
     :suffix (calendar-date-string (calendar-cursor-to-date) t t)
     :visible (calendar-cursor-to-date)]
94
    ["For Window -" calendar-list-holidays
95
     :suffix (cal-menu-holiday-window-suffix)]
96
    ["For Today -" (calendar-cursor-holidays (calendar-current-date))
97 98 99 100
     :suffix (calendar-date-string (calendar-current-date) t t)]
    "--"
    ,@(let ((l ()))
        ;; Show 11 years--5 before, 5 after year of middle month.
101 102
        ;; We used to use :suffix rather than :label and bumped into
        ;; an easymenu bug:
103
        ;; https://lists.gnu.org/r/emacs-devel/2007-11/msg01813.html
104
        ;; The bug has since been fixed.
105
        (dotimes (i 11)
106
          (push (vector (format "hol-year-%d" i)
107 108
                        `(lambda ()
                           (interactive)
109
                           (holiday-list (+ displayed-year ,(- i 5))))
110 111
                        :label `(format "For Year %d"
                                       (+ displayed-year ,(- i 5))))
112
                l))
113 114 115
        (nreverse l))
    "--"
    ["Unmark Calendar" calendar-unmark]
116 117
    ["Mark Holidays" calendar-mark-holidays])
  "Key map for \"Holidays\" menu in the calendar.")
118 119

(defconst cal-menu-goto-menu
120
  '("Goto"
121 122 123 124 125 126 127 128 129
    ["Today" calendar-goto-today]
    ["Beginning of Week" calendar-beginning-of-week]
    ["End of Week" calendar-end-of-week]
    ["Beginning of Month" calendar-beginning-of-month]
    ["End of Month" calendar-end-of-month]
    ["Beginning of Year" calendar-beginning-of-year]
    ["End of Year" calendar-end-of-year]
    ["Other Date" calendar-goto-date]
    ["Day of Year" calendar-goto-day-of-year]
130 131 132
    ["ISO Week" calendar-iso-goto-week]
    ["ISO Date" calendar-iso-goto-date]
    ["Astronomical Date" calendar-astro-goto-day-number]
133
    ["Hebrew Date" calendar-hebrew-goto-date]
134
    ["Persian Date" calendar-persian-goto-date]
135
    ["Bahá’í Date" calendar-bahai-goto-date]
136 137
    ["Islamic Date" calendar-islamic-goto-date]
    ["Julian Date" calendar-julian-goto-date]
138
    ["Chinese Date" calendar-chinese-goto-date]
139 140
    ["Coptic Date" calendar-coptic-goto-date]
    ["Ethiopic Date" calendar-ethiopic-goto-date]
141
    ("Mayan Date"
142 143 144 145 146 147
     ["Next Tzolkin" calendar-mayan-next-tzolkin-date]
     ["Previous Tzolkin" calendar-mayan-previous-tzolkin-date]
     ["Next Haab" calendar-mayan-next-haab-date]
     ["Previous Haab" calendar-mayan-previous-haab-date]
     ["Next Round" calendar-mayan-next-round-date]
     ["Previous Round" calendar-mayan-previous-round-date])
148
    ["French Date" calendar-french-goto-date])
149
  "Key map for \"Goto\" menu in the calendar.")
150 151 152

(defconst cal-menu-scroll-menu
  '("Scroll"
153
    ["Scroll Commands" nil :help "Commands that scroll the visible window"]
154 155
    ["Forward 1 Month" calendar-scroll-left]
    ["Forward 3 Months" calendar-scroll-left-three-months]
156
    ["Forward 1 Year" (calendar-scroll-left 12) :keys "4 C-v"]
157 158
    ["Backward 1 Month" calendar-scroll-right]
    ["Backward 3 Months" calendar-scroll-right-three-months]
159 160 161 162 163 164 165 166 167 168 169
    ["Backward 1 Year" (calendar-scroll-right 12) :keys "4 M-v"]
    "--"
    ["Motion Commands" nil :help "Commands that move point"]
    ["Forward 1 Day" calendar-forward-day]
    ["Forward 1 Week" calendar-forward-week]
    ["Forward 1 Month" calendar-forward-month]
    ["Forward 1 Year" calendar-forward-year]
    ["Backward 1 Day" calendar-backward-day]
    ["Backward 1 Week" calendar-backward-week]
    ["Backward 1 Month" calendar-backward-month]
    ["Backward 1 Year" calendar-backward-year])
170
  "Key map for \"Scroll\" menu in the calendar.")
171

172
(declare-function x-popup-menu "menu.c" (position menu))
173

174 175 176 177 178 179 180
(defmacro cal-menu-x-popup-menu (event title &rest body)
  "Call `x-popup-menu' at position EVENT, with TITLE and contents BODY.
Signals an error if popups are unavailable."
  (declare (indent 2))
  `(if (display-popup-menus-p)
       (x-popup-menu ,event (list ,title (append (list ,title) ,@body)))
     (error "Popup menus are not available on this system")))
181

182
(autoload 'diary-list-entries "diary-lib")
183 184
;; Autoloaded in diary-lib.
(declare-function calendar-check-holidays "holidays" (date))
185

186
(defun calendar-mouse-view-diary-entries (&optional date diary event)
187
  "Pop up menu of diary entries for mouse-selected date.
188
Use optional DATE and alternative file DIARY.  EVENT is the event
189
that invoked this command.  Shows holidays if `diary-show-holidays-flag'
190
is non-nil."
191
  (interactive "i\ni\ne")
192
  (let* ((date (or date (calendar-cursor-to-date nil event)))
193
         (diary-file (or diary diary-file))
194
         (diary-list-include-blanks nil)
195 196
         (diary-entries (mapcar (lambda (x) (split-string (cadr x) "\n"))
                                (diary-list-entries date 1 'list-only)))
197
         (holidays (if diary-show-holidays-flag
198
                       (calendar-check-holidays date)))
199 200
         (title (format "Diary entries%s for %s"
                        (if diary (format " from %s" diary) "")
201
                        (calendar-date-string date)))
202 203 204 205 206 207 208
         (selection (cal-menu-x-popup-menu event title
                      (mapcar (lambda (x) (list (concat "     " x))) holidays)
                      (if holidays
                          (list "--shadow-etched-in" "--shadow-etched-in"))
                      (if diary-entries
                          (mapcar 'list (apply 'append diary-entries))
                        '("None")))))
209
    (and selection (call-interactively selection))))
Richard M. Stallman's avatar
Richard M. Stallman committed
210

211
(defun calendar-mouse-view-other-diary-entries (&optional event)
212
  "Pop up menu of diary entries from alternative file on mouse-selected date."
213
  (interactive "e")
214
  (calendar-mouse-view-diary-entries
215 216 217
   (calendar-cursor-to-date nil event)
   (read-file-name "Enter diary file name: " default-directory nil t)
   event))
218

219 220 221 222
;; In 22, the equivalent code gave an error when not called on a date,
;; but easymenu does not seem to allow this (?).
;; The ignore-errors is because `documentation' can end up calling
;; this in a non-calendar buffer where displayed-month is unbound.  (Bug#3862)
Glenn Morris's avatar
Glenn Morris committed
223
;; This still has issues - bug#9976, so added derived-mode-p call.
224 225
(defun cal-menu-set-date-title (menu)
  "Convert date of last event to title suitable for MENU."
Glenn Morris's avatar
Glenn Morris committed
226 227 228 229 230 231
  (when (derived-mode-p 'calendar-mode)
    (let ((date (ignore-errors (calendar-cursor-to-date nil last-input-event))))
      (if date
          (easy-menu-filter-return menu (calendar-date-string date t nil))
        (message "Not on a date!")
        nil))))
232

233
(easy-menu-define cal-menu-context-mouse-menu nil
234 235
  "Pop up mouse menu for selected date in the calendar window."
  '("cal-menu-context-mouse-menu" :filter cal-menu-set-date-title
236
    "--"
237
    ["Holidays" calendar-cursor-holidays]
238 239
    ["Mark date" calendar-set-mark]
    ["Sunrise/sunset" calendar-sunrise-sunset]
240
    ["Other calendars" calendar-print-other-dates]
241 242
    ;; There was a bug (#447; fixed) with last-nonmenu-event and submenus.
    ;; These did not work if called without calendar window selected.
243
    ("Prepare LaTeX buffer"
244
     ["Daily (1 page)" cal-tex-cursor-day]
245 246 247 248 249
     ["Weekly (1 page, with hours)" cal-tex-cursor-week]
     ["Weekly (2 pages, with hours)" cal-tex-cursor-week2]
     ["Weekly (1 page, no hours)" cal-tex-cursor-week-iso]
     ["Weekly (1 page, with hours, different style)" cal-tex-cursor-week-monday]
     ["Weekly (2 pages, no hours)" cal-tex-cursor-week2-summary]
250 251 252 253
     ["Monthly" cal-tex-cursor-month]
     ["Monthly (landscape)" cal-tex-cursor-month-landscape]
     ["Yearly" cal-tex-cursor-year]
     ["Yearly (landscape)" cal-tex-cursor-year-landscape]
254
     ("Filofax styles"
255 256 257 258 259 260 261
      ["Filofax Daily (one-day-per-page)" cal-tex-cursor-filofax-daily]
      ["Filofax Weekly (2-weeks-at-a-glance)" cal-tex-cursor-filofax-2week]
      ["Filofax Weekly (week-at-a-glance)" cal-tex-cursor-filofax-week]
      ["Filofax Yearly" cal-tex-cursor-filofax-year]))
    ("Write HTML calendar"
     ["For selected month" cal-html-cursor-month]
     ["For selected year" cal-html-cursor-year])
262
    ["Diary entries" calendar-mouse-view-diary-entries :keys "d"]
263
    ["Insert diary entry" diary-insert-entry]
264 265
    ["Other diary file entries" calendar-mouse-view-other-diary-entries
     :keys "D"]))
266 267 268 269

(easy-menu-define cal-menu-global-mouse-menu nil
  "Menu bound to a mouse event, not specific to the mouse-click location."
  '("Calendar"
270 271
    ["Scroll forward" calendar-scroll-left-three-months]
    ["Scroll backward" calendar-scroll-right-three-months]
272
    ["Mark diary entries" diary-mark-entries]
273 274
    ["List holidays" calendar-list-holidays]
    ["Mark holidays" calendar-mark-holidays]
275
    ["Unmark" calendar-unmark]
276
    ["Lunar phases" calendar-lunar-phases]
277
    ["Sunrise times for month" calendar-sunrise-sunset-month]
278
    ["Show diary" diary-show-all-entries]
279
    ["Exit calendar" calendar-exit]))
Richard M. Stallman's avatar
Richard M. Stallman committed
280 281 282 283

(provide 'cal-menu)

;;; cal-menu.el ends here