Subject: bug#78298: [PATCH] Scroll with mouse wheel in calendar

From: Manuel Giraud <[email-masked]>
Date: Fri, 16 May 2025 10:52:36 +0200

Michael Heerdegen <[email-masked]> writes:

[...]

> | Debugger entered--Lisp error: (void-variable displayed-month)

Ok.  So AFAIU, one (overlooked) difference between backward/forward
commands and scroll commands is that the latter are taking care of an
event parameter that is used to execute the command in the context of
the calendar buffer.

So after all, I think we should be using this "scroll framework" to
scroll years also.  Here is an updated patch that could be applied on
top of master.
>From 087b4a2a5b3db6e2e59e6052ca2e2a8bccb63ef1 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Fri, 16 May 2025 10:31:43 +0200
Subject: [PATCH] Dedicated commands to scroll calendar by year

Follow-up to bug#78298

* lisp/calendar/cal-move.el (calendar-scroll-left-year)
(calendar-scroll-right-year): New commands to scroll the
calendar by year.
* lisp/calendar/calendar.el (calendar-mode-map): Use a
keybinding.
* etc/NEWS: Announce new commands.
---
 etc/NEWS                  |  5 +++++
 lisp/calendar/cal-move.el | 20 ++++++++++++++++++++
 lisp/calendar/calendar.el |  4 ++--
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index d4e97883322..70d5a808f50 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2062,6 +2062,11 @@ is bound to 'C-l' in the calendar buffer.
 You can now use the mouse wheel to scroll the calendar by 3 months.
 With the shift modifier, it scrolls by one month.  With the meta
 modifier, it scrolls by year.
+
+---
+*** New commands 'calendar-scroll-left-year' and 'calendar-scroll-right-year'.
+These commands scroll the calendar one year at a time.
+
 
 * New Modes and Packages in Emacs 31.1
 
diff --git a/lisp/calendar/cal-move.el b/lisp/calendar/cal-move.el
index aad05f572d6..efa21862725 100644
--- a/lisp/calendar/cal-move.el
+++ b/lisp/calendar/cal-move.el
@@ -220,6 +220,26 @@ calendar-scroll-right-three-months
 (defvar calendar-recenter-last-op nil
   "Last calendar recenter operation performed.")
 
+;;;###cal-autoload
+(defun calendar-scroll-left-year (arg &optional event)
+  "Scroll the displayed calendar window left by 12*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.
+EVENT is an event like `last-nonmenu-event'."
+  (interactive (list (prefix-numeric-value current-prefix-arg)
+                     last-nonmenu-event))
+  (calendar-scroll-left (* 12 arg) event))
+
+;;;###cal-autoload
+(defun calendar-scroll-right-year (arg &optional event)
+  "Scroll the displayed calendar window right by 12*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.
+EVENT is an event like `last-nonmenu-event'."
+  (interactive (list (prefix-numeric-value current-prefix-arg)
+                     last-nonmenu-event))
+  (calendar-scroll-left (* -12 arg) event))
+
 ;;;###cal-autoload
 (defun calendar-recenter ()
   "Scroll the calendar so that the month of the date at point is centered.
diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el
index c448955450e..02626f547c1 100644
--- a/lisp/calendar/calendar.el
+++ b/lisp/calendar/calendar.el
@@ -1591,14 +1591,14 @@ calendar-mode-map
     (define-key map [prior] 'calendar-scroll-right-three-months)
     (define-key map "\ev"   'calendar-scroll-right-three-months)
     (define-key map [wheel-up] 'calendar-scroll-right-three-months)
-    (define-key map [M-wheel-up] 'calendar-backward-year)
+    (define-key map [M-wheel-up] 'calendar-scroll-right-year)
     (define-key map ">"     'calendar-scroll-left)
     (define-key map "\C-x>" 'calendar-scroll-left)
     (define-key map [S-wheel-down] 'calendar-scroll-left)
     (define-key map [next]  'calendar-scroll-left-three-months)
     (define-key map "\C-v"  'calendar-scroll-left-three-months)
     (define-key map [wheel-down] 'calendar-scroll-left-three-months)
-    (define-key map [M-wheel-down] 'calendar-forward-year)
+    (define-key map [M-wheel-down] 'calendar-scroll-left-year)
     (define-key map "\C-l"  'calendar-recenter)
     (define-key map "\C-b"  'calendar-backward-day)
     (define-key map "\C-p"  'calendar-backward-week)
-- 
2.49.0

-- 
Manuel Giraud