5

Emacs has the function other-window that lets you jump between windows. The function's documentation states that it takes an optional argument that tells the function how many windows to jump ahead.

Once upon a time, I defined a keyboard shortcut to call (other-window -1) to jump to the previous window. But at some point (GNU Emacs 25, I think), this stopped working. At first, it did not bother me much, but over time it has become a real nuisance. So if anybody knows a way to replicate that behavior, I would be very grateful!

The function I wrote looks like this:

(defun previous-window ()
    (interactive nil)
    (other-window -1))

And it is bound to a command key like so:

(global-set-key "\C-xp" 'previous-window)

The error message I get:

other-window: Wrong number of arguments: (lambda nil (interactive nil) (other-window -1)), 3

When I run emacs -Q and enter the function and keybinding manually, I get the same error.

Also, I have a function to switch to the previous frame:

(defun prev-frame ()
    (interactive nil)
    (other-frame -1))

This function works as expected.

krylon
  • 53
  • 5
  • 4
    (other-window -1) works fine for me, and the docstring suggests it should. Try it without your init file (emacs -Q). If it works appropriately, it's something in your init file that is interfering. – Dan Aug 02 '18 at 15:53
  • What @dan said. The question is unclear so far, so it risks being deleted. Specify what you mean by "stopped working": say what happens. Show your code that binds the key. Remember that a key needs to be bound to a command, which means a function that has an interactive spec. – Drew Aug 02 '18 at 16:28
  • I can't reproduce in 26.1 as well. –  Aug 02 '18 at 17:04

1 Answers1

4

The problem is the function previous-window does already exist and is called by other-window. You can get info about it with C-h f previous-window (before you define your own previous-window):

previous-window is a built-in function in ‘C source code’.

(previous-window &optional WINDOW MINIBUF ALL-FRAMES)

...

So the fix is simply to call your own function some other name.

(defun myprevious-window ()
    (interactive)
    (other-window -1))

Note: it turns out there is also a function previous-frame so your second function would fail in the same way if you call it previous-frame instead of prev-frame.

JeanPierre
  • 7,465
  • 1
  • 20
  • 39