Emacs Lisp - opening new window - emacs

I'd like to write an emacs lisp function that will write output to a window other than the current window. It should create a new window if only the current one exists, and it should use an existing one otherwise. This is similar to what happens when you run C-h-f (Describe Function), and the description pops up in another window. What is the best way to do this?

See display-buffer:
display-buffer is an interactive
compiled Lisp function in `window.el'.
It is bound to C-x 4 C-o.
(display-buffer buffer-or-name
&optional not-this-window frame)
Make buffer buffer-or-name appear in
some window but don't select it.
buffer-or-name must be a buffer or the
name of an existing buffer. Return
the window chosen to display
buffer-or-name or nil if no such
window is found.
Optional argument not-this-window
non-nil means display the buffer in a
window other than the selected one,
even if it is already displayed in the
selected window.
Optional argument frame specifies
which frames to investigate when the
specified buffer is already displayed.
If the buffer is already displayed in
some window on one of these frames
simply return that window. Possible
values of frame are:
`visible' - consider windows on all
visible frames.
0 - consider windows on all visible or
iconified frames.
t - consider windows on all frames.
A specific frame - consider windows on
that frame only.
nil - consider windows on the selected
frame (actually the last
non-minibuffer frame) only. If,
however, either
display-buffer-reuse-frames' or
pop-up-frames' is non-nil (non-nil
and not graphic-only on a text-only
terminal), consider all visible or
iconified frames.
Or you can use pop-to-buffer if you want that buffer to be selected (which it sounds like you don't), or with-output-to-temp-buffer which binds the standard-output to be sent to the temporary buffer - read the documentation for more details (hat tip to Michael for that).

Related

geiser-eval-buffer switch to different frame

I am trying out geiser and after evaluating it always switch to different frame with different buffer than my source code. Could this be prevented? Is it possible to stay after pressing C-c C-b in my current buffer?
Do you mean it switches to a different window? If so, you may want to check out the variables geiser-debug-jump-to-debug-p and geiser-debug-show-debug-p, which control changing focus to the * Geiser dbg * buffer and showing it at all, respectively.

Emacs switch-to-buffer (C-x b) will change window if the buffer is opened in some other window

I would like to change the behavior of switch-to-buffer to always open the desired buffer in the current window instead of switching to another window if already opened.
My rationale for doing this :
I have 2 screens, one Emacs on each. One is for reading, the other one for writing. Some buffers will be in the reading side for a while, but now I want to edit them. I'd like to have them in front of me instead of automatically switch to this other window on the side.
Thank you.
That is what switch-to-buffer does - exactly what you say you want. Do you see something different when you start Emacs without an init file (emacs -Q)?
The only exception is when the window cannot be used for that buffer. As the doc string says:
If the selected window cannot display the specified
buffer (e.g. if it is a minibuffer window or strongly dedicated
to another buffer), call `pop-to-buffer' to select the buffer in
another window.

Can I disable window-splitting in a particular frame?

I'm a developer who uses emacs. In emacs I use multiple frames (what most people would call X windows), and the compile function for my builds. I like to have one frame for my compilation buffer, and the others for source. That allows me to navigate to build errors easily and get a nice big view of the source I'm investigating along side a nice big view of my build output. This works fine when I use the 'next-error' function from a source frame.
But when I move my pointer into the compilation frame, and click on an error, it vertically splits that frame to show the relevant source. I want it to show the relevant source in one of my other frames.
Is there a way to "lock" a frame so that it won't be split into windows, and so other frames will be used instead? I'm OK if it splits one of my other frames to display the new source files - just not the compilation frame (because that means I have to unsplit that frame and then switch the buffer of a different frame to display the buffer in question - that's cumbersome).
Alternatively it would be fine if I could use a different mouse button on an error in the compilation buffer to say "visit this file and line in a different frame".
I believe you can achieve your goals by making the window in your "compile frame" dedicated:
Functions for displaying a buffer can be told to not use specific windows by marking these windows as dedicated to their buffers.
Interactively, M-x set-window-dedicated-p should make your window dedicated.
From elisp, something like
(set-window-dedicated-p (selected-window) 1)
should do the same. Replacing 1 with t will make the window strongly dedicated:
As a special case, if flag is t, window becomes strongly dedicated to its buffer. set-window-buffer signals an error when the window it acts upon is strongly dedicated to its buffer and does not already display the buffer it is asked to display. Other functions do not treat t differently from any non-nil value.

How to make emacs allow opening one buffer in different frames?

I have two frames of the same Emacs instance (got them by C-x 5 2) and a file opened in one of these frames. Then I try to show this file in a buffer in another frame (by C-x b) and nothing happens.
Actually, I think it shows this file in a buffer in the first frame, but this is not what I want it to do. I want it to show this buffer in both frames.
When there is only one frame and some windows in it, opening one buffer in different windows is no problem. I want the same behaviour but with several frames.
Most likely you are using iswitch, as what you describe is its default behavior.
You can change it by doing M-x customize-variable RET iswitchb-default-method. probably you want to choose option samewindow
The ido equivalent variable , in case you are using it, is ido-default-buffer-method (ido defaults to raise-frame, that does not match with your observed behavior).
You may find if you are using one or the other using the way #Olaf describes.
Maybe, you have C-x b redefined. I have the same behaviour with iswitch-buffer. But when I enter M-x switch-to-buffer, it shows the buffer in the second window.
You can find out, which command is executed with C-h c C-x b.

Buffer control when using gdb under emacs

I am using gdb under emacs. During a debug session, when emacs/gdb jumps to source code i usually open up another frame (C-x 5 2) and pull that onto my second monitor.
The problem comes when a new source code file needs to be opened (for example when the program enters a function defined in another source file, walking the stack, etc ...) emacs tends to automatically open up the corresponding source code in a new window, but unfortunately in the gdb frame.
I would like for it pull up new source code in the new frame i opened up and leave the gdb frame completely untouched. My question then is this:
How can i set emacs to open up any new file such that the file is displayed in the new frame?
I hope i am making sense.
Not a simple answer, but an appropriate entry in display-buffer-alist could tell display-buffer to always use the frame you want as the destination for new buffers.
In 24.1, the doc is:
display-buffer-alist is a variable defined in `window.el'. Its value
is nil
This variable may be risky if used as a file-local variable.
Documentation: Alist of conditional actions for `display-buffer'. This
is a list of elements (CONDITION . ACTION), where:
CONDITION is either a regexp matching buffer names, or a function
that takes two arguments - a buffer name and the ACTION argument of
`display-buffer' - and returns a boolean.
ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
function or a list of functions. Each such function should accept
two arguments: a buffer to display and an alist of the same form as
ALIST. See `display-buffer' for details.
`display-buffer' scans this alist until it either finds a matching
regular expression or the function specified by a condition returns
non-nil. In any of these cases, it adds the associated action to the
list of actions it will try.
You can customize this variable.
This variable was introduced, or its default value was changed, in
version 24.1 of Emacs.