Use emacs to copy kill-ring to window/buffer based on position - emacs

This may be too involved.
Considering:
In emacs in r-mode or lisp mode (etc) information can be sent directly (copied, pasted, evaluated) from one buffer to the the R or Lisp interpreter.
I typically configure an emacs session to have 3 windows - a large horizontal window on top and two windows beneath it. (How) could I configure, which keys/ commands might I use to send the kill-ring to the last cursor position of the top window / buffer?
The buffer / window will not always necessarily have the same contents/file. (How) could I name it upon initialization?
Similar to C-X, C-B or C-X, B how might I specify which of the three window positions to go to (based on position)?

See window-at. For example,
(defun yank-into-top-window (&optional arg)
(interactive "*P")
(with-selected-window (window-at 0 0)
(yank arg)))

I think you're going to have to write lisp code to do this effectively. Basically, you'd want a minor mode that sets up the two subwindows -- which isn't hard, it happens in compile mode from M-x compile -- and then make special keybindings for the keys you want to use.

Related

Start Emacs with R session and side by side windows?

I am very bad at customizing emacs. I desire that if i start with a file ending in ".r", emacs starts maximized, with two windows side by side (vertical division of the frame), in one my source code file, and in the other the ESS R interpreter. If I can understand the example, maybe I can generalize it to other extensions and modes. I still do not get the syntax of hooks in elisp.
The usual way to use Emacs is to have it always running instead of
opening and closing it all the time.
I suggest that you define a function that would make an existing Emacs
look the way you want:
(defun my-R-window-configuration ()
"Prepare the current emacs frame for R work."
(interactive)
;; maximimize the current frame:
(set-frame-parameter nil 'fullscreen 'maximized)
;; keep just the current window, presumably containing the R code
(delete-other-windows)
;; create ESS R interaction buffer and go there
(ess-switch-to-end-of-ESS)
;; go back to the code
(other-window 1))
Now you can do M-x my-R-window-configuration RET in an R buffer to get what you want.

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 buffer shown after closing a buffer

How can I modify the way emacs picks which buffer to show after closing a buffer?
When I have multiple columns showing the same buffer, and then open another file in one of the buffers and then close the newly opened buffer, it doesn't switch back to the previous buffer, but to another buffer.
I'll try to explain with an example:
Start with a new emacs at *scratch*
C-x 2 (split into two columns)
C-x C-f 1 (find file 1)
C-x o (switch to other frame)
C-x b 1 (find file 1)
C-x C-f 2 (find file 2)
C-x k (kill buffer)
Now it switches to scratch but I would like it to show 1 in both windows again, is it possible to make emacs behave this way?
This may not be a direct answer to your question, but it might help.
Emacs manages its buffer list, including deciding which buffer gets displayed when you kill one (via kill-buffer). I haven't looked into how it's done, but the documentation is "out there". Lots of people have created custom buffer-stack management magic to change the way emacs does things, maybe some of them are based on bayesian analysis, or whatever. You can imagine the possibilities.
I've never looked into changing the way emacs manages its buffers. Instead I just bind other-window and switch-to-buffer to easy keystrokes (C-x o, C-x b) and I get really good at using them.
you could create a simple function for what you want: it should destroys all other windows, then split the window so that the current buffer is displayed in both. Luckily, emacs has functions that do exactly those things.
(defun cheeso-show-buffer-two-windows ()
"Close all other windows; then split, and show the current
buffer in both windows."
(interactive)
(delete-other-windows)
(split-window-vertically))
Bind that to a keystroke, and badda-bing, you're there. This is a vertical split - the windows are displayed in a vertical stack. If you want it horizontally split (the windows are side-by-side), then replace ... well, you know.
This also doesn't quite help directly, but Winner mode might help you get where you want to get.
Are you using tabbar-mode? I had the same problem and for me tabbar was the cause. Tabbar adds the function tabbar-buffer-kill-buffer-hook to kill-buffer-hook. You can remove it with (remove-hook 'kill-buffer-hook 'tabbar-buffer-kill-buffer-hook).
If you don't use tabbar try M-x describe-variable kill-buffer-hook. One of the functions in this list should be responsible for messing with your buffers.

Is there any way to get Ediff to not open its navigation interface in an external window?

Not being using Emacs all that long (v23, windows) and just discovered M-x ediff. Fantastic.
Although I'm not to keen on the fact it opens its help/navigation in a separate frame/window, meaning that if I lose focus to that window, the single key shortcuts don't work.
For example as soon as I press ? to expand the window, it shifts over top of my current window, so I have to pick up my mouse and move it to another screen. Then if I lose focus to that window and press p / n / j or any other key to work with the diff, it inserts it into my document. So i have to undo, grab mouse, focus to other window, and repeat.
Is there any way to configure these options to show in a split instead?
I didn't know how to do it but it is usually easy to learn with Emacs. First I asked about ediff customizations:
M-x customize-apropos
ediff
I saw there is something called Ediff Window Setup Function which takes the values Multi Frame, Single Frame, or Other Function. Mine was set to Multi Frame and changed it to Single Frame and saved it for future sessions. And Voila! as they say somewhere.
Simply:
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
M-x describe-variable ediff-window-setup-function will enlighten you
further.
For reference my ediff customisation is fairly simple:
(if (locate-library "ediff")
(progn
(autoload 'ediff-files "ediff")
(autoload 'ediff-buffers "ediff")
(eval-after-load "ediff" '(progn
(message "doing ediff customisation")
(setq diff-switches "-u"
ediff-custom-diff-options "-U3"
ediff-split-window-function 'split-window-horizontally
ediff-window-setup-function 'ediff-setup-windows-plain)
(add-hook 'ediff-startup-hook 'ediff-toggle-wide-display)
(add-hook 'ediff-cleanup-hook 'ediff-toggle-wide-display)
(add-hook 'ediff-suspend-hook 'ediff-toggle-wide-display)))))
From chapter Window and Frame Configuration in Ediff User's Manual:
The following variable controls how
windows are set up:
ediff-window-setup-function
The multiframe setup is done by the ediff-setup-windows-multiframe
function, which is the default on
windowing displays. The plain setup,
one where all windows are always in
one frame, is done by
ediff-setup-windows-plain, which is
the default on a non-windowing display
(or in an xterm window). In fact,
under Emacs, you can switch freely
between these two setups by executing
the command ediff-toggle-multiframe
using the Minibuffer of the Menubar.
(custom-set-variables
...
'(ediff-window-setup-function (quote ediff-setup-windows-plain))
...)
Not that you would set the variable this way, but it allows you to know these things:
The variable you are interested in is ediff-window-setup-function
The value it needs to be set to is ediff-setup-windows-plain
You can configure the variable from customize: M-x customize-group RET ediff-window
Ediff Window Setup Function: Menu Single Frame
Note: you can avoid using the mouse to go back to the ediff control window by using M-x other-frame. Also found on C-x 5 o.
This no longer works in 2017 gnu emacs (24.5, 25.2, 2017) on windows
(setq ediff-window-setup-function 'ediff-setup-windows-plain) ; stopped working
Even
ediff-toggle-multiframe ; no longer has any effect now.
It works in emacs22.3 on windows, so I have use older emacs from 2008!