Run Emacs Command in Other Window - emacs

I'm looking for a way to send the output of an arbitrary Emacs command (in my case sql-send-region) to another window. I would prefer to maintain focus in the window I am currently in, which would effectively give me one window to edit queries and one window to view the output.

I was able to write some Emacs Lisp to solve my problem:
(defun sql-send-region-and-return (start end)
(interactive "r")
(let ((oldbuf (buffer-name)))
(sql-send-region start end)
(switch-to-buffer oldbuf)))
This sends the result of your region to the SQL buffer and returns back to your current buffer, effectively accomplishing the stated objective.
Thanks justinhj for giving me some new leads to solve my problem.

Have you tried setting this variable to t, it sounds like the behaviour you want.
sql-pop-to-buffer-after-send-region
After a call to sql-send-region' orsql-send-buffer',
the window is split and the SQLi buffer is shown. If this
variable is not nil, that buffer's window will be selected
by calling pop-to-buffer'. If this variable is nil, that
buffer is shown usingdisplay-buffer'.

Related

emacs dispatch help window from original buffer

Whenever I do apropos, describe-key, or some other help function in emacs, it displays the help in my other window. In order to get rid of it, I must change windows/buffers to go there, type "q", and then change back to my original working buffer.
Is there a way I can do this in code somehow? I know how to save-excursion, switch buffers, etc, but I don't know how to send the "q" to the minibuffer/emacs while I'm over in the other buffer. Thanks
The help-window-select variable might be exactly what you want.
If you set its value to true (setq help-window-select t) then the help window will automatically be selected when you open it via one of the help commands. You can then press q to quit out of it and go back to your original buffer. There are many other options so you should check those out too.
For the apropos window or any window that uses display-buffer you can use.
(add-to-list 'display-buffer-alist
'("*Apropos*" display-buffer-same-window))
display-buffer-same-window is one options of many; it opens the buffer in the current window. The other possible options can be seen by looking up the docs on the display-buffer function.
Here's my solution to this problem. I bind this command to C-c q:
(defvar my/help-window-names
'(
;; Ubiquitous help buffers
"*Help*"
"*Apropos*"
"*Messages*"
"*Completions*"
;; Other general buffers
"*Command History*"
"*Compile-Log*"
"*disabled command*")
"Names of buffers that `my/quit-help-windows' should quit.")
(defun my/quit-help-windows (&optional kill frame)
"Quit all windows with help-like buffers.
Call `quit-windows-on' for every buffer named in
`my/help-windows-name'. The optional parameters KILL and FRAME
are just as in `quit-windows-on', except FRAME defaults to t (so
that only windows on the selected frame are considered).
Note that a nil value for FRAME cannot be distinguished from an
omitted parameter and will be ignored; use some other value if
you want to quit windows on all frames."
(interactive)
(let ((frame (or frame t)))
(dolist (name my/help-window-names)
(ignore-errors
(quit-windows-on name kill frame)))))
I suggest putting (winner-mode 1) in your init file, and then using C-c<left> to call winner-undo (repeatedly, if necessary) to return to a previous window configuration.

Emacs - Open buffer list without replacing another buffer

I have one frame, one window.
I use Cx 3, I now have two windows.
I use Cx Cb in order to see the list of buffers, however it opens it in another window but doesn't put the focus on it. It is even more annoying if I had opened a buffer on the 2nd window.
I would prefer to either open the buffer list in the window which currently has the focus, or temporarily change the focus to the buffer list.
First of all I want to start by saying that the ibuffer function does similary to what you want and does so in the current window, its definitely worth checking out.
Now onto your actual question. C-x C-b by default calls the function list-buffers. If you search for that command using C-h f it will show you the documentation, from there you can view the source code for the function by clicking on the underlined text that says buff-menu.el.
Now we can view the source of the list-buffers, the first function called is display-buffer. That sounds promising. We can now use C-h f once again to search for the display-buffer command. Reading though this documentation we see that the variable display-buffer-alist dictates how the display-buffer and in turn how list-buffers works. We can then see that by adding ("*Buffer List*" . display-buffer-same-window) to display-buffer-alist you will get the desired result.
All in all you simply need to put (add-to-list 'display-buffer-alist '("*Buffer List*" . display-buffer-same-window)) in your init file for your changes to take place.
Please just try one of these:
open the buffer list who currently has the focus:
(defun my:list-buffers (&optional arg)
(interactive "P")
(display-buffer (list-buffers-no-select arg) '(display-buffer-same-window)))
change the focus
(defun my:list-buffers2 (&optional arg)
(interactive "P")
(select-window (list-buffers arg)))

How to control which window to be used for new contents in Emacs?

Often I have multiples windows carefully arranged within a frame.
However, certain command, say M-x man RET will grab one of the visible windows to display its own content. Sometimes this is annoying because the window that was taken away is one that I need to keep it visible.
e.g. I have 3 windows on screen, one useful source-code window and two useless windows. I want to keep the soure-code window visible while checking the man page. But very often Emacs just take away the code-window for the newly opened man page.
One way I can think of is to display the (chronological) open order of each window, so that I can focus point on n-th window and be confident that Emacs will grab (n+1)-th window for new content.
Is there a way to display such order, e.g. in the mode-line of each window?
Or is there another way for better control for displaying new window?
A little late to the party but as discussed in the comments, using dedicated windows is a good way to control where new content is displayed (+1 to #lawlist for bringing it up and to #phils for mentioning toggling!).
I'm pretty sure you'd be able to implement a command that toggles dedicatedness yourself at this point, but since I have the code for this handy I'll share it anyway:
(defun toggle-window-dedicated ()
"Control whether or not Emacs is allowed to display another
buffer in current window."
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
; set-window-dedicated-p returns FLAG that was passed as
; second argument, thus can be used as COND for if:
(set-window-dedicated-p window (not (window-dedicated-p window))))
"%s: Can't touch this!"
"%s is up for grabs.")
(current-buffer)))
(global-set-key (kbd "C-c d") 'toggle-window-dedicated)
Now, in a multi-window setup you can simply press C-c d in each window you would like to "protect".
I'll throw this example usage of display-buffer-alist into the mix, given that it was mentioned in the comments, and is indeed a general mechanism for controlling how and where buffers are displayed (even though, as Drew indicates, it is far from trivial).
Start with the help for the variable itself:
C-hv display-buffer-alist RET
From there you'll get to the help for display-buffer, and no doubt acquire some idea of the complexities involved :)
In any case, the following is an example of using a custom function to decide how to display buffers named *Buffer List*.
You can easily see that if you can write a function which can figure out how to display a buffer where you want it to, then you can use display-buffer-alist to make it happen.
(defun my-display-buffer-pop-up-same-width-window (buffer alist)
"A `display-buffer' ACTION forcing a vertical window split.
See `split-window-sensibly' and `display-buffer-pop-up-window'."
(let ((split-width-threshold nil)
(split-height-threshold 0))
(display-buffer-pop-up-window buffer alist)))
(add-to-list 'display-buffer-alist
'("\\*Buffer List\\*" my-display-buffer-pop-up-same-width-window))
A key quote regarding the ACTION functions is:
Each such FUNCTION should accept two arguments: the buffer to
display and an alist. Based on those arguments, it should
display the buffer and return the window.

Emacs switch to the next window regardless of frame

I'd like for the C-x o command (next window) to include windows in other frames as well as windows in the current frame.
Does anyone know how to pull this off? Is there another command that I should be using? Is there some snippet of elisp magic that can do this with ease?
C-x o is other-window. To go to an other frame use C-x 5 o which is other-frame.
Not sure if this is what you mean, but if you want to just cycle through buffers in the buffer list, regardless of frame:
Ctrl x→
Ctrl x←
These are bound to (next-buffer) and (previous-buffer), respectively.
This can be a first approximation.
http://www.gnu.org/software/emacs/manual/html_node/elisp/Cyclic-Window-Ordering.html
http://www.gnu.org/software/emacs/manual/html_node/elisp/Frames.html
other-window has a parameter to control how it deals with frames.
(global-set-key (kbd "C-x o") (lambda ()
(interactive)
(other-window 1 t)
(let ((nframe (window-frame (selected-window))))
(select-frame-set-input-focus nframe)
(make-frame-visible nframe))))
You must press C-x 5 o C-h to see all functions about working with frames.
Some of these function is other-frame.
I use the version 2.0 of ace-jump-mode. It takes about two minutes to understand how it works and since version 2.0 it allows to "jump" to another frame. You can jump to any character from any buffer/frame/window that you can actually see on a screen in three or four keypresses. It's very hard to beat.
It's a gigantic time saver anyway so I'd recommend checking it out because it's really convenient.
http://www.emacswiki.org/emacs/AceJump
And the "Emacs Rocks! Episode 10: Jumping around" two minutes screencast showing it in action:
http://www.youtube.com/watch?v=UZkpmegySnc
From C-h f next-window:
(next-window &optional WINDOW MINIBUF ALL-FRAMES) ...
ALL-FRAMES nil or omitted means consider all windows on WINDOW's
frame, plus the minibuffer window if specified by the MINIBUF
argument. If the minibuffer counts, consider all windows on all
frames that share that minibuffer too. The following non-nil values
of ALL-FRAMES have special meanings:
t means consider all windows on all existing frames.
`visible' means consider all windows on all visible frames.
0 (the number zero) means consider all windows on all visible and iconified frames.
A frame means consider all windows on that frame only.
Anything else means consider all windows on WINDOW's frame and no
others.
Somewhat ironically, other-window supports this as well, as it uses next-window. Unfortunately, I don't know of a way to pass non-numeric arguments interactively, but a simple function should do the trick:
(defun my-other-window (count)
(interactive "p")
(other-window count t))
You say "Is there a way to cycle through windows regardless of what frame they're in? That's really what I'm looking for?"
Yes, there is, with Icicles.
What you request is what command icicle-select-window does when you use a prefix arg. If you want that behavior always, you can define your own command that does it without a prefix arg:
(defun my-select-window ()
"Select window by name. Windows of all visible frames are candidates."
(interactive)
(let ((current-prefix-arg 1)) (icicle-select-window)))
You are prompted for the window name. But if you just want to cycle, without narrowing the candidates by typing part of the name, then just use C-down to get the window you want.
(A window name is the name of its displayed buffer, but suffixed as
needed by [NUMBER], to make the name unique. For example, if you have
two windows showing buffer *Help*, one of the windows will be called
*Help*[2] for use with this command.)

Emacs switch to buffer in different frame

I'm currently programming away in emacs. I have a function defined in my .emacs that saves all my work and executes the interpreter to run my work in the currently open shell buffer. Typically I'll be editing in one or more frames and have the shell open in a separate frame. The problem I have is when I run my save and execute function, everything is saved but the shell buffer then gets displayed in the frame I'm currently editing. So I have two frames showing the shell buffer and I can't see the source code I was just editing. Quite often when I'm programming, I'd immediately like to compare the output of the code with the code I've just written. It's a bit annoying to have to switch back to my code buffer and then go to the end of the other shell buffer to look at the output while referencing the just written code.
(defun execute-script ()
"Switch to shell buffer and re-execute the last command."
(interactive)
(save-some-buffers)
(switch-to-buffer "*shell*")
(end-of-buffer)
(comint-previous-input 0)
(comint-send-input))
As you can see my function is rather primitive at the moment, just re-executing the most recently used command in the shell.
I know that Emacs has the functionality to switch to a buffer in a different frame, as the ido buffer switcher code does this. Does anybody have any pointers as to what I need to replace my call to switch-to-buffer with to get the desired effect?
Regards Giles.
Exchange
(switch-to-buffer "*shell*")
for
(switch-to-buffer-other-window "*shell*")
This will enforce a two window layout with your previous buffer on the left side and the shell buffer on the right side.
Edit: If you're using a multiple frame layout and not a multiple window layout you can use
(switch-to-buffer-other-frame "*shell*")
You can also use pop-to-buffer which will later let you customize exactly how that behaves, e.g. via display-buffer-reuse-frame, or display-buffer-alist, or special-display-regexp.