Restricting buffers to popup in a particular window on emacs - emacs

How can I redirect some buffers to popup in a particular window? For example, when you are using the shell, and you press <TAB>, a *completions* buffer will popup (usually) on a right window... but how can I make this buffer to always popup on another window?
I'd prefer solutions that doesn't involve installing new libraries (even though, programming my own elisp function would be neat).
I'm asking this, because when I use emacs, O like to split it into 3 windows:
The left window is where I code, let's call this the plotting window. The right top window, is where compilation messages will pop-up, let's call it the information window. The right bottom window is where I use the shell, let's call it the shell window. When I use the shell window, and press <TAB> for completions proposals, the *completions* buffer will popup on my plotting window, instead of on my information window.
Until now, I have tried just pressing q when I go to my plotting window, but this is becoming annoying, because it wont always return to the code I was working in, so I have to C-right until I find my code, or C-x b (if I can remember the name of the file I was editing).

The easiest way I can think of is to dedicate the plotting window and the shell window to their buffers. If you have already installed emacs-goodies then you can use M-x dedicated-mode for that purpose.
In the case that the plotting window and the shell window are dedicated emacs only uses the information window to pop-up new buffers.
I for myself use a menu-item that I added to the Buffers menu:
(defun window-dedicated-toggle ()
"Toggle wether window is dedicated to buffer or not."
(interactive)
(message
(if (window-dedicated-p)
(progn
(set-window-dedicated-p nil nil)
"Window not dedicated.")
(progn
(set-window-dedicated-p nil t)
"Window dedicated."))))
(easy-menu-add-item nil '("Buffers") ["Toggle Dedicate Window To Buffer" window-dedicated-toggle t])

Related

Emacs: How to kill (C-x 4 0) a buffer of a frame by using the GUI Close button?

I know it is a bad habit to use a mouse as an Emacs user. However, there are times when it is just convenient to close "some" of my multiple Emacs frames by clicking the red "x" Close button on the upper left corner of Finder rather than just repeatedly using C-x 4 0.
Is there is a way to rebind OS X GUI Close button to kill (C-x 4 0) the buffer of a frame - that is, kill the buffer and the frame at the same time? And if the buffer has no filename, Emacs will ask me to save or discard the buffer.
My Emacs is 24.3.1 on OS X.
First, it is not a bad idea to use a mouse with Emacs. Quite the contrary. But it can be a bad habit, as you say: anything overdone can be a bad habit. Emacs works especially well with a mouse (the Emacs mouse is more powerful than usual), and it works fine without a mouse. Use a mouse for what it is good for: direct access to any position, just by pointing.
And yes, you can make Emacs perform a command (such as delete-frame) when you click the window-manager X icon (or whatever icon your window manager uses to delete windows). You do this:
(define-key special-event-map [delete-frame] 'delete-frame)
See (elisp) Special Events.
(In library thumb-frm.el I use the Minimize icon (optionally) to thumbify frames, as another example of this.)
To answer your comment question more specifically. This code prompts for the buffer to kill (default: current buffer). If there is only one frame, then it quits Emacs. Otherwise, it deletes only the selected frame.
(defun foo (&optional buffer)
(interactive (list (read-buffer "Buffer: " (current-buffer))))
(if (not (cadr (frame-list)))
(save-buffers-kill-terminal) ; Kill Emacs, if last frame
(kill-buffer buffer)
(delete-frame (selected-frame) 'FORCE)))
(define-key special-event-map [delete-frame] 'foo)

Emacs+AucTex: can't display the output window when the reftex-toc window is dedicated

I am edting latex files with Emacs+AucTeX. When working I have two windows in the frame: the latex file window and the reftex-toc window. I set the reftex-toc window dedicated to its buffer by
(defadvice reftex-toc (after reftex-toc-window-dedicated activate)
(set-window-dedicated-p (selected-window) t))
to keep it from being replaced.
But problems arise when I call (TeX-recenter-output-buffer) by C-c C-l. The output buffer can't be displayed. I have to manually switch to the output buffer by C-x b. And if I call help commands, e.g. C-h f, the help buffer is not displayed in a new window as it should be. Instead, it replaces the latex file window. How can I fix this problem?
EDIT
I found that the problem is due to function (display-buffer BUFFER), which is internally called by (TeX-recenter-output-buffer). Under normal circumstances where there is no dedicated window in the frame, (display-buffer BUFFER) creates a new window for BUFFER. But if there are a dedicated window and some normal windows, (display-buffer BUFFER) just displays BUFFER in one of the normal windows w/o creating a new one.
As a workaround, I open another (the 3rd) window, so that if I call C-c C-l, the compilation output is displayed there and the latex file window is kept. However, I am still looking forward to a real solution. Could any one help? Thank you.

How to jump into the other window immediately when C-x 2/3 in Emacs

When I use C-x 2/3 to open a new window in Emacs, I usually want to do something in the other window, such as shell, open a buffer or visit a new file, but I always change into the other window manually. Is there a way(maybe defun a new function in .emacs, but I'm new to Emacs) that I can switch my point into the other window immediately after C-x 2/3 just like when you open a new tab and switch to it immediately in a browser???
How about just wrapping the function using something like this:
(defun my-split-window-below ()
(interactive)
(let ((win (split-window-below)))
(set-frame-selected-window (selected-frame) win)))
You could also do defadvice on split-window-below using after, and have it select the window, but I'm not sure that it's a better solution.

How do I close an automatically opened window in Emacs?

This is probably a very naive Emacs question - I'm new to it.
When I'm evaluating a lisp expression, if there's an error the debugger automatically comes up in another window. If I have *scratch* and *info* open (the former for trying out lisp and the latter for reading about it), then the debugger opens up in the window that *info* was in. At the moment, I have to switch to that window, then change it back to *info*, before returning to *scratch*. (The same thing happens if I do C-x C-b for a list of buffers.) I'm guessing there has to be a way to just close that window without this long sequence of commands. Can anyone enlighten me?
At least here on my emacs (22.3), when the debugger pops up, its window becomes the active one. There, pressing q just quits the debugger, if that's what you want. At that point, it also gets out of recursive editing.
From what I understand, you want to close the buffer in the other window without moving your cursor from the current window.
I don't any existing function does that, so I rolled my own.
(defun other-window-kill-buffer ()
"Kill the buffer in the other window"
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(select-window win-curr)))
You can bind it to something like "C-x K" or some other somewhat difficult-to-press key so you won't press it by mistake.
(global-set-key (kbd "C-x K") 'other-window-kill-buffer)
I use this a LOT! (for Help buffers, Compilation buffers, Grep buffers, and just plain old buffers I want to close now, without moving the point)
I'm usually using the delete-other-windows command. C-x 1.
It's so regullar, that I rebinded to F4.
Official docs: https://www.gnu.org/software/emacs/manual/html_node/emacs/Change-Window.html#Change-Window
HTH
Using winner mode, you can use the keybinding C-C left arrow to return to the previous window configuration. Of course, this doesn't actually kill the new buffer, but it does hide it.
I think you're looking for C-x 4 C-o, which displays a buffer in the "other" window without switching to it.
As mentioned above, in the case of the backtrace buffer you probably want to exit from it with q, to get out of the recursive edit.
Try to use popwin-mode. It is really good about closing windows automatically.
I want to describe how mechanism work|
You open undo-tree-visualize than you find your correct branch, after that when you change your active window which has cursor via switch-window undo-tree related buffer will close other window automatically.
One possible solution is to use a window management package. Store your "preferred" window arrangement, and after the debugger/buffer window pops up and you're done with it, revert to your preferred arrangement.
There are a bunch of packages to choose from, see: switching window configurations on the wiki.
Additionally, you might want to figure out the actions you commonly do that trigger the extra (unwanted) window popping up, and use that to trigger saving your window configuration just before the window pops up.
If you want to roll your own (it's pretty easy), you can just save off the window configuration, and restore it like so:
(setq the-window-configuration-i-want (current-window-configuration))
(global-set-key (kbd "<f7>")
(lambda () (interactive)
(set-window-configuration the-window-configuration-i-want)))
The trick is figuring out where to put the setting of the-window-configuration-i-want.
Thanks go to #spk for a nice function. I merely modified it to also close the window too and bound it to the already nice set of window control functions found in evil-windows (via "C-w"):
(use-package evil
:bind (:map evil-window-map ("O" . delete-most-recent-window))
:preface
(defun delete-most-recent-window ()
"Kill the buffer in the most recent window."
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(delete-window) ; <-- Added this
(select-window win-curr))))

Pin Emacs buffers to windows (for cscope)

For my day job, I live in Emacs. Utterly. I also have become pretty dependent on CScope to help me find things in the code.
Normally, I have 2 windows in a split (C-x 3):
alt text http://bitthicket.com/files/emacs-2split.JPG
And I use the right window for code buffers and the left window for the CScope search buffer. When you do a CScope search and select a result, it automatically updates the right-side window to show the buffer referred to by the result. This is all well and good, except that it causes me to lose my place in some other buffer that I was studying. Sometimes this is no biggie, because [C-s u] gets me back to where I was.
What would be better, though, is to have 3 split windows like this ([C-x 2] in the left window):
alt text http://bitthicket.com/files/emacs-3split.jpg
And have the bottom left window contain the CScope search buffer, and the top left window be the only buffer that CScope ever updates. That way, I can see my CScope searches and navigate around the code without losing the buffer I'm focused on.
Anyone know how I can do that?
Put this in your .emacs file:
;; Toggle window dedication
(defun toggle-window-dedicated ()
"Toggle whether the current active window is dedicated or not"
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
(set-window-dedicated-p window
(not (window-dedicated-p window))))
"Window '%s' is dedicated"
"Window '%s' is normal")
(current-buffer)))
Then bind it to some key - I use the Pause key:
(global-set-key [pause] 'toggle-window-dedicated)
And then use it to "dedicate" the window you want locked. then cscope can only open files from its result window in some OTHER window. Works a charm. I specifically use it for exactly this purpose - keeping one source file always on screen, while using cscope in a second buffer/window, and looking at cscope results in a third.
Well, I decided to not be a reputation-whore and find the answer myself. I looked in cscope.el as shown on the Emacs wiki, as well as the xcscope.el that comes with the cscope RPM package on RHEL.
Neither appear to give a way to do what I'm wanting. The way is probably to edit the ELisp by adding a package variable like *browse-buffer* or something and just initialize that variable if not already initialized the first time the user does [C-c C-s g] or whatever, and always have the resulting code shown in *browse-buffer*. Then the user can put the *browse-buffer* wherever he wants it.