Subject: Re: bug#78803: [PATCH] Allow starting with 'C-x O' for other-window

From: James Thomas <[email-masked]>
Date: Fri, 11 Jul 2025 12:06:17 +0530

James Thomas writes:

> "Paul D. Nelson" writes:
>
>> Thanks, I see.  Your patch looks good to me (perhaps modulo NEWS entry)
>
> May this not die for want of that... :

Or for a more complete commit message:

From 3cacdd6519c65a09b8397ae337083595d4d0f191 Mon Sep 17 00:00:00 2001
From: James Thomas <[email-masked]>
Date: Mon, 16 Jun 2025 01:20:31 +0530
Subject: [PATCH] Add 'other-window-backward' for a default binding

This moves in the opposite direction to 'other-window' by
default, and is consistent with its repeat map.
* lisp/window.el (other-window-backward): New function bound to
'C-x O'.
(ctl-x-map): Update definition.
* doc/emacs/windows.texi (Other Window): Update docs.
* etc/NEWS: Announce the new command.
---
 doc/emacs/windows.texi |  5 ++++-
 etc/NEWS               |  4 ++++
 lisp/window.el         | 14 ++++++++++----
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/doc/emacs/windows.texi b/doc/emacs/windows.texi
index 4a35fd54f66..f80aeb08681 100644
--- a/doc/emacs/windows.texi
+++ b/doc/emacs/windows.texi
@@ -158,6 +158,8 @@ Other Window
 @table @kbd
 @item C-x o
 Select another window (@code{other-window}).
+@item C-x O
+Select another window, backwards (@code{other-window-backward}).
 @item C-M-v
 Scroll the next window upward (@code{scroll-other-window}).
 @item C-M-S-v
@@ -183,7 +185,8 @@ Other Window
 minibuffer window is the last window in the cycle; you can switch from
 the minibuffer window to one of the other windows, and later switch
 back and finish supplying the minibuffer argument that is requested.
-@xref{Minibuffer Edit}.
+@xref{Minibuffer Edit}. @kbd{C-x O} is similar, but defaults to the
+opposite order.
 
 @findex next-window-any-frame
 The @code{other-window} command will normally only switch to the next
diff --git a/etc/NEWS b/etc/NEWS
index 81f0215deda..32085fd7e61 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -317,6 +317,10 @@ helps to restore window buffers across Emacs sessions.
 You can use this in 'display-buffer-alist' to match buffers displayed
 during the execution of particular commands.
 
+*** New command 'other-window-backward' ('C-x O').
+This moves in the opposite direction of 'other-window' and is for its
+default keybinding consistent with 'repeat-mode'.
+
 ** Frames
 
 +++
diff --git a/lisp/window.el b/lisp/window.el
index e372ef8b9bb..1c030504188 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -4072,6 +4072,14 @@ other-window
 	;; Always return nil.
 	nil))))
 
+(defun other-window-backward (count &optional all-frames interactive)
+    "Select another window in the cyclic ordering of windows, skipping
+COUNT windows backward, starting with the selected window, before making
+the selection.  Like `other-window', but moves in the opposite
+direction."
+    (interactive "p\ni\np")
+    (other-window (- (or count 1)) all-frames interactive))
+
 (defun other-window-prefix ()
   "Display the buffer of the next command in a new window.
 The next buffer is the buffer displayed by the next command invoked
@@ -11350,6 +11358,7 @@ ctl-x-map
 (define-key ctl-x-map "2" 'split-window-below)
 (define-key ctl-x-map "3" 'split-window-right)
 (define-key ctl-x-map "o" 'other-window)
+(define-key ctl-x-map "O" 'other-window-backward)
 (define-key ctl-x-map "^" 'enlarge-window)
 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
 (define-key ctl-x-map "{" 'shrink-window-horizontally)
@@ -11363,10 +11372,7 @@ other-window-repeat-map
   :doc "Keymap to repeat `other-window'.  Used in `repeat-mode'."
   :repeat t
   "o" #'other-window
-  "O" (lambda ()
-        (interactive)
-        (setq repeat-map 'other-window-repeat-map)
-        (other-window -1)))
+  "O" #'other-window-backward)
 
 (defvar-keymap resize-window-repeat-map
   :doc "Keymap to repeat window resizing commands.
-- 
2.50.0

--