Subject: bug#13420: 24.2.92; [PATCH] calendar-redraw moves window-point

From: Leo Liu <[email-masked]>
Date: Sat, 12 Jan 2013 16:11:12 +0800

Due to a bug/trap as reported in http://debbugs.gnu.org/13361, here is
one bug in calendar that moves window-point to (point-min).

1. Emacs -q
2. M-x calendar
3. C-x o so that the *calendar* window is not selected
4. M-x calendar-redraw

When you C-x o back to the calendar window, your window-point is no
longer at today's date but (point-min).

Note also calendar-redraw is called when saving in the diary file where
it has a save-hook: diary-redraw-calendar; so it is not uncommon for
this annoyance to happen.

For example, I often move to a future date and `i d' to insert a
reminder in the diary file and then C-x C-s. When I switch back to
calendar for further work, point is at the inconvenient point-min.

The proposed fix is in the patch attached.

I'd bet there are more occurrences in emacs source that fall into this
trap.

Leo

diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el
index 9b0eb3e9..74d3ce80 100644
--- a/lisp/calendar/calendar.el
+++ b/lisp/calendar/calendar.el
@@ -1562,11 +1562,13 @@ (defun calendar-generate-month (month year indent)
 (defun calendar-redraw ()
   "Redraw the calendar display, if `calendar-buffer' is live."
   (interactive)
-  (if (get-buffer calendar-buffer)
-      (with-current-buffer calendar-buffer
-        (let ((cursor-date (calendar-cursor-to-nearest-date)))
-          (calendar-generate-window displayed-month displayed-year)
-          (calendar-cursor-to-visible-date cursor-date)))))
+  (when (get-buffer calendar-buffer)
+    (with-current-buffer calendar-buffer
+      (let ((cursor-date (calendar-cursor-to-nearest-date)))
+        (calendar-generate-window displayed-month displayed-year)
+        (calendar-cursor-to-visible-date cursor-date))
+      (when (window-live-p (get-buffer-window))
+        (set-window-point (get-buffer-window) (point))))))
 
 (defvar calendar-mode-map
   (let ((map (make-keymap)))