How to reproduce:
- write some faulty elisp code
- the debugger will appear (
*backtrace*window) and take the focus of the cursor.
I'd like the focus to remain with the original window so. How can I do that?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
So the sequencing is a bit tricky as mentioned in the comment below, but reselecting the previous window seems like the easiest way:
(defun my-debug-hook () ;; Selecting the window in `debug-mode-hook' is too early, it will ;; confuse the debugger's code, causing it to replace the current ;; buffer contents with the backtrace, and reset `buffer-undo-list'! ;; We want to do it when the debugger enters a `recursive-edit'. ;; The (recursive) command loop will call `post-command-hook' once ;; at startup, which is exactly the right time for us. (add-hook 'post-command-hook #'select-previous-window-once)) (defun select-previous-window-once () (select-window (previous-window)) (remove-hook 'post-command-hook #'select-previous-window-once)) (add-hook 'debugger-mode-hook #'my-debug-hook)
In your comment you say this:
I have a
close-and-kill-next-panefunction defined in my
.init. I use that to kill windows that I just needed temporarily, help docs, etc.
After I identify the issue, I want to close the debugger and work on my code. By habit, I hit the keybinding for
close-and-kill-next-paneand instead of the debugger going away, what I was working on goes away. Does that make sense? Please tell me if that workflow doesn't make sense.
My answer is to tell you that that workflow doesn't make much sense, to me, at least. I'd say that it is better to get out of that habit, or at least to temper it enough to take a quick look at the
*Backtrace* (debugger) window, to see what's going on.
The debugger window is selected by default so you can address the error. What you really want to do, if you're uninterested in what it tells you or you've already taken a look at it, is to dismiss the debugger by hitting
q. That exits the recursive edit that was entered. (Just deleting the window does not exit the recursive edit.)
If, for some reason, the debugger window is no longer selected, you can still exit the recursive edit (without bothering to select that window and use
q). You can use
C-], which is
C-M-c, which is
exit-recursive-edit, from anywhere.